Commit 8a1c17574a01555e5d3fdf56d8d72969904c91ca

Authored by Andrew Morton
Committed by Linus Torvalds
1 parent 498d0c5711

[PATCH] schedule_timeout_[un]interruptible() speedup

These functions don't need schedule_timeout()'s barrier.

Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>

Showing 1 changed file with 6 additions and 3 deletions Inline Diff

1 /* 1 /*
2 * linux/kernel/timer.c 2 * linux/kernel/timer.c
3 * 3 *
4 * Kernel internal timers, kernel timekeeping, basic process system calls 4 * Kernel internal timers, kernel timekeeping, basic process system calls
5 * 5 *
6 * Copyright (C) 1991, 1992 Linus Torvalds 6 * Copyright (C) 1991, 1992 Linus Torvalds
7 * 7 *
8 * 1997-01-28 Modified by Finn Arne Gangstad to make timers scale better. 8 * 1997-01-28 Modified by Finn Arne Gangstad to make timers scale better.
9 * 9 *
10 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96 10 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
11 * "A Kernel Model for Precision Timekeeping" by Dave Mills 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 12 * 1998-12-24 Fixed a xtime SMP race (we need the xtime_lock rw spinlock to
13 * serialize accesses to xtime/lost_ticks). 13 * serialize accesses to xtime/lost_ticks).
14 * Copyright (C) 1998 Andrea Arcangeli 14 * Copyright (C) 1998 Andrea Arcangeli
15 * 1999-03-10 Improved NTP compatibility by Ulrich Windl 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 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. 17 * 2000-10-05 Implemented scalable SMP per-CPU timer handling.
18 * Copyright (C) 2000, 2001, 2002 Ingo Molnar 18 * Copyright (C) 2000, 2001, 2002 Ingo Molnar
19 * Designed by David S. Miller, Alexey Kuznetsov and Ingo Molnar 19 * Designed by David S. Miller, Alexey Kuznetsov and Ingo Molnar
20 */ 20 */
21 21
22 #include <linux/kernel_stat.h> 22 #include <linux/kernel_stat.h>
23 #include <linux/module.h> 23 #include <linux/module.h>
24 #include <linux/interrupt.h> 24 #include <linux/interrupt.h>
25 #include <linux/percpu.h> 25 #include <linux/percpu.h>
26 #include <linux/init.h> 26 #include <linux/init.h>
27 #include <linux/mm.h> 27 #include <linux/mm.h>
28 #include <linux/swap.h> 28 #include <linux/swap.h>
29 #include <linux/notifier.h> 29 #include <linux/notifier.h>
30 #include <linux/thread_info.h> 30 #include <linux/thread_info.h>
31 #include <linux/time.h> 31 #include <linux/time.h>
32 #include <linux/jiffies.h> 32 #include <linux/jiffies.h>
33 #include <linux/posix-timers.h> 33 #include <linux/posix-timers.h>
34 #include <linux/cpu.h> 34 #include <linux/cpu.h>
35 #include <linux/syscalls.h> 35 #include <linux/syscalls.h>
36 36
37 #include <asm/uaccess.h> 37 #include <asm/uaccess.h>
38 #include <asm/unistd.h> 38 #include <asm/unistd.h>
39 #include <asm/div64.h> 39 #include <asm/div64.h>
40 #include <asm/timex.h> 40 #include <asm/timex.h>
41 #include <asm/io.h> 41 #include <asm/io.h>
42 42
43 #ifdef CONFIG_TIME_INTERPOLATION 43 #ifdef CONFIG_TIME_INTERPOLATION
44 static void time_interpolator_update(long delta_nsec); 44 static void time_interpolator_update(long delta_nsec);
45 #else 45 #else
46 #define time_interpolator_update(x) 46 #define time_interpolator_update(x)
47 #endif 47 #endif
48 48
49 /* 49 /*
50 * per-CPU timer vector definitions: 50 * per-CPU timer vector definitions:
51 */ 51 */
52 52
53 #define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6) 53 #define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
54 #define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8) 54 #define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
55 #define TVN_SIZE (1 << TVN_BITS) 55 #define TVN_SIZE (1 << TVN_BITS)
56 #define TVR_SIZE (1 << TVR_BITS) 56 #define TVR_SIZE (1 << TVR_BITS)
57 #define TVN_MASK (TVN_SIZE - 1) 57 #define TVN_MASK (TVN_SIZE - 1)
58 #define TVR_MASK (TVR_SIZE - 1) 58 #define TVR_MASK (TVR_SIZE - 1)
59 59
60 struct timer_base_s { 60 struct timer_base_s {
61 spinlock_t lock; 61 spinlock_t lock;
62 struct timer_list *running_timer; 62 struct timer_list *running_timer;
63 }; 63 };
64 64
65 typedef struct tvec_s { 65 typedef struct tvec_s {
66 struct list_head vec[TVN_SIZE]; 66 struct list_head vec[TVN_SIZE];
67 } tvec_t; 67 } tvec_t;
68 68
69 typedef struct tvec_root_s { 69 typedef struct tvec_root_s {
70 struct list_head vec[TVR_SIZE]; 70 struct list_head vec[TVR_SIZE];
71 } tvec_root_t; 71 } tvec_root_t;
72 72
73 struct tvec_t_base_s { 73 struct tvec_t_base_s {
74 struct timer_base_s t_base; 74 struct timer_base_s t_base;
75 unsigned long timer_jiffies; 75 unsigned long timer_jiffies;
76 tvec_root_t tv1; 76 tvec_root_t tv1;
77 tvec_t tv2; 77 tvec_t tv2;
78 tvec_t tv3; 78 tvec_t tv3;
79 tvec_t tv4; 79 tvec_t tv4;
80 tvec_t tv5; 80 tvec_t tv5;
81 } ____cacheline_aligned_in_smp; 81 } ____cacheline_aligned_in_smp;
82 82
83 typedef struct tvec_t_base_s tvec_base_t; 83 typedef struct tvec_t_base_s tvec_base_t;
84 static DEFINE_PER_CPU(tvec_base_t, tvec_bases); 84 static DEFINE_PER_CPU(tvec_base_t, tvec_bases);
85 85
86 static inline void set_running_timer(tvec_base_t *base, 86 static inline void set_running_timer(tvec_base_t *base,
87 struct timer_list *timer) 87 struct timer_list *timer)
88 { 88 {
89 #ifdef CONFIG_SMP 89 #ifdef CONFIG_SMP
90 base->t_base.running_timer = timer; 90 base->t_base.running_timer = timer;
91 #endif 91 #endif
92 } 92 }
93 93
94 static void check_timer_failed(struct timer_list *timer) 94 static void check_timer_failed(struct timer_list *timer)
95 { 95 {
96 static int whine_count; 96 static int whine_count;
97 if (whine_count < 16) { 97 if (whine_count < 16) {
98 whine_count++; 98 whine_count++;
99 printk("Uninitialised timer!\n"); 99 printk("Uninitialised timer!\n");
100 printk("This is just a warning. Your computer is OK\n"); 100 printk("This is just a warning. Your computer is OK\n");
101 printk("function=0x%p, data=0x%lx\n", 101 printk("function=0x%p, data=0x%lx\n",
102 timer->function, timer->data); 102 timer->function, timer->data);
103 dump_stack(); 103 dump_stack();
104 } 104 }
105 /* 105 /*
106 * Now fix it up 106 * Now fix it up
107 */ 107 */
108 timer->magic = TIMER_MAGIC; 108 timer->magic = TIMER_MAGIC;
109 } 109 }
110 110
111 static inline void check_timer(struct timer_list *timer) 111 static inline void check_timer(struct timer_list *timer)
112 { 112 {
113 if (timer->magic != TIMER_MAGIC) 113 if (timer->magic != TIMER_MAGIC)
114 check_timer_failed(timer); 114 check_timer_failed(timer);
115 } 115 }
116 116
117 117
118 static void internal_add_timer(tvec_base_t *base, struct timer_list *timer) 118 static void internal_add_timer(tvec_base_t *base, struct timer_list *timer)
119 { 119 {
120 unsigned long expires = timer->expires; 120 unsigned long expires = timer->expires;
121 unsigned long idx = expires - base->timer_jiffies; 121 unsigned long idx = expires - base->timer_jiffies;
122 struct list_head *vec; 122 struct list_head *vec;
123 123
124 if (idx < TVR_SIZE) { 124 if (idx < TVR_SIZE) {
125 int i = expires & TVR_MASK; 125 int i = expires & TVR_MASK;
126 vec = base->tv1.vec + i; 126 vec = base->tv1.vec + i;
127 } else if (idx < 1 << (TVR_BITS + TVN_BITS)) { 127 } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
128 int i = (expires >> TVR_BITS) & TVN_MASK; 128 int i = (expires >> TVR_BITS) & TVN_MASK;
129 vec = base->tv2.vec + i; 129 vec = base->tv2.vec + i;
130 } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) { 130 } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
131 int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK; 131 int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
132 vec = base->tv3.vec + i; 132 vec = base->tv3.vec + i;
133 } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) { 133 } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
134 int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK; 134 int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
135 vec = base->tv4.vec + i; 135 vec = base->tv4.vec + i;
136 } else if ((signed long) idx < 0) { 136 } else if ((signed long) idx < 0) {
137 /* 137 /*
138 * Can happen if you add a timer with expires == jiffies, 138 * Can happen if you add a timer with expires == jiffies,
139 * or you set a timer to go off in the past 139 * or you set a timer to go off in the past
140 */ 140 */
141 vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK); 141 vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
142 } else { 142 } else {
143 int i; 143 int i;
144 /* If the timeout is larger than 0xffffffff on 64-bit 144 /* If the timeout is larger than 0xffffffff on 64-bit
145 * architectures then we use the maximum timeout: 145 * architectures then we use the maximum timeout:
146 */ 146 */
147 if (idx > 0xffffffffUL) { 147 if (idx > 0xffffffffUL) {
148 idx = 0xffffffffUL; 148 idx = 0xffffffffUL;
149 expires = idx + base->timer_jiffies; 149 expires = idx + base->timer_jiffies;
150 } 150 }
151 i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK; 151 i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
152 vec = base->tv5.vec + i; 152 vec = base->tv5.vec + i;
153 } 153 }
154 /* 154 /*
155 * Timers are FIFO: 155 * Timers are FIFO:
156 */ 156 */
157 list_add_tail(&timer->entry, vec); 157 list_add_tail(&timer->entry, vec);
158 } 158 }
159 159
160 typedef struct timer_base_s timer_base_t; 160 typedef struct timer_base_s timer_base_t;
161 /* 161 /*
162 * Used by TIMER_INITIALIZER, we can't use per_cpu(tvec_bases) 162 * Used by TIMER_INITIALIZER, we can't use per_cpu(tvec_bases)
163 * at compile time, and we need timer->base to lock the timer. 163 * at compile time, and we need timer->base to lock the timer.
164 */ 164 */
165 timer_base_t __init_timer_base 165 timer_base_t __init_timer_base
166 ____cacheline_aligned_in_smp = { .lock = SPIN_LOCK_UNLOCKED }; 166 ____cacheline_aligned_in_smp = { .lock = SPIN_LOCK_UNLOCKED };
167 EXPORT_SYMBOL(__init_timer_base); 167 EXPORT_SYMBOL(__init_timer_base);
168 168
169 /*** 169 /***
170 * init_timer - initialize a timer. 170 * init_timer - initialize a timer.
171 * @timer: the timer to be initialized 171 * @timer: the timer to be initialized
172 * 172 *
173 * init_timer() must be done to a timer prior calling *any* of the 173 * init_timer() must be done to a timer prior calling *any* of the
174 * other timer functions. 174 * other timer functions.
175 */ 175 */
176 void fastcall init_timer(struct timer_list *timer) 176 void fastcall init_timer(struct timer_list *timer)
177 { 177 {
178 timer->entry.next = NULL; 178 timer->entry.next = NULL;
179 timer->base = &per_cpu(tvec_bases, raw_smp_processor_id()).t_base; 179 timer->base = &per_cpu(tvec_bases, raw_smp_processor_id()).t_base;
180 timer->magic = TIMER_MAGIC; 180 timer->magic = TIMER_MAGIC;
181 } 181 }
182 EXPORT_SYMBOL(init_timer); 182 EXPORT_SYMBOL(init_timer);
183 183
184 static inline void detach_timer(struct timer_list *timer, 184 static inline void detach_timer(struct timer_list *timer,
185 int clear_pending) 185 int clear_pending)
186 { 186 {
187 struct list_head *entry = &timer->entry; 187 struct list_head *entry = &timer->entry;
188 188
189 __list_del(entry->prev, entry->next); 189 __list_del(entry->prev, entry->next);
190 if (clear_pending) 190 if (clear_pending)
191 entry->next = NULL; 191 entry->next = NULL;
192 entry->prev = LIST_POISON2; 192 entry->prev = LIST_POISON2;
193 } 193 }
194 194
195 /* 195 /*
196 * We are using hashed locking: holding per_cpu(tvec_bases).t_base.lock 196 * We are using hashed locking: holding per_cpu(tvec_bases).t_base.lock
197 * means that all timers which are tied to this base via timer->base are 197 * means that all timers which are tied to this base via timer->base are
198 * locked, and the base itself is locked too. 198 * locked, and the base itself is locked too.
199 * 199 *
200 * So __run_timers/migrate_timers can safely modify all timers which could 200 * So __run_timers/migrate_timers can safely modify all timers which could
201 * be found on ->tvX lists. 201 * be found on ->tvX lists.
202 * 202 *
203 * When the timer's base is locked, and the timer removed from list, it is 203 * When the timer's base is locked, and the timer removed from list, it is
204 * possible to set timer->base = NULL and drop the lock: the timer remains 204 * possible to set timer->base = NULL and drop the lock: the timer remains
205 * locked. 205 * locked.
206 */ 206 */
207 static timer_base_t *lock_timer_base(struct timer_list *timer, 207 static timer_base_t *lock_timer_base(struct timer_list *timer,
208 unsigned long *flags) 208 unsigned long *flags)
209 { 209 {
210 timer_base_t *base; 210 timer_base_t *base;
211 211
212 for (;;) { 212 for (;;) {
213 base = timer->base; 213 base = timer->base;
214 if (likely(base != NULL)) { 214 if (likely(base != NULL)) {
215 spin_lock_irqsave(&base->lock, *flags); 215 spin_lock_irqsave(&base->lock, *flags);
216 if (likely(base == timer->base)) 216 if (likely(base == timer->base))
217 return base; 217 return base;
218 /* The timer has migrated to another CPU */ 218 /* The timer has migrated to another CPU */
219 spin_unlock_irqrestore(&base->lock, *flags); 219 spin_unlock_irqrestore(&base->lock, *flags);
220 } 220 }
221 cpu_relax(); 221 cpu_relax();
222 } 222 }
223 } 223 }
224 224
225 int __mod_timer(struct timer_list *timer, unsigned long expires) 225 int __mod_timer(struct timer_list *timer, unsigned long expires)
226 { 226 {
227 timer_base_t *base; 227 timer_base_t *base;
228 tvec_base_t *new_base; 228 tvec_base_t *new_base;
229 unsigned long flags; 229 unsigned long flags;
230 int ret = 0; 230 int ret = 0;
231 231
232 BUG_ON(!timer->function); 232 BUG_ON(!timer->function);
233 check_timer(timer); 233 check_timer(timer);
234 234
235 base = lock_timer_base(timer, &flags); 235 base = lock_timer_base(timer, &flags);
236 236
237 if (timer_pending(timer)) { 237 if (timer_pending(timer)) {
238 detach_timer(timer, 0); 238 detach_timer(timer, 0);
239 ret = 1; 239 ret = 1;
240 } 240 }
241 241
242 new_base = &__get_cpu_var(tvec_bases); 242 new_base = &__get_cpu_var(tvec_bases);
243 243
244 if (base != &new_base->t_base) { 244 if (base != &new_base->t_base) {
245 /* 245 /*
246 * We are trying to schedule the timer on the local CPU. 246 * We are trying to schedule the timer on the local CPU.
247 * However we can't change timer's base while it is running, 247 * However we can't change timer's base while it is running,
248 * otherwise del_timer_sync() can't detect that the timer's 248 * otherwise del_timer_sync() can't detect that the timer's
249 * handler yet has not finished. This also guarantees that 249 * handler yet has not finished. This also guarantees that
250 * the timer is serialized wrt itself. 250 * the timer is serialized wrt itself.
251 */ 251 */
252 if (unlikely(base->running_timer == timer)) { 252 if (unlikely(base->running_timer == timer)) {
253 /* The timer remains on a former base */ 253 /* The timer remains on a former base */
254 new_base = container_of(base, tvec_base_t, t_base); 254 new_base = container_of(base, tvec_base_t, t_base);
255 } else { 255 } else {
256 /* See the comment in lock_timer_base() */ 256 /* See the comment in lock_timer_base() */
257 timer->base = NULL; 257 timer->base = NULL;
258 spin_unlock(&base->lock); 258 spin_unlock(&base->lock);
259 spin_lock(&new_base->t_base.lock); 259 spin_lock(&new_base->t_base.lock);
260 timer->base = &new_base->t_base; 260 timer->base = &new_base->t_base;
261 } 261 }
262 } 262 }
263 263
264 timer->expires = expires; 264 timer->expires = expires;
265 internal_add_timer(new_base, timer); 265 internal_add_timer(new_base, timer);
266 spin_unlock_irqrestore(&new_base->t_base.lock, flags); 266 spin_unlock_irqrestore(&new_base->t_base.lock, flags);
267 267
268 return ret; 268 return ret;
269 } 269 }
270 270
271 EXPORT_SYMBOL(__mod_timer); 271 EXPORT_SYMBOL(__mod_timer);
272 272
273 /*** 273 /***
274 * add_timer_on - start a timer on a particular CPU 274 * add_timer_on - start a timer on a particular CPU
275 * @timer: the timer to be added 275 * @timer: the timer to be added
276 * @cpu: the CPU to start it on 276 * @cpu: the CPU to start it on
277 * 277 *
278 * This is not very scalable on SMP. Double adds are not possible. 278 * This is not very scalable on SMP. Double adds are not possible.
279 */ 279 */
280 void add_timer_on(struct timer_list *timer, int cpu) 280 void add_timer_on(struct timer_list *timer, int cpu)
281 { 281 {
282 tvec_base_t *base = &per_cpu(tvec_bases, cpu); 282 tvec_base_t *base = &per_cpu(tvec_bases, cpu);
283 unsigned long flags; 283 unsigned long flags;
284 284
285 BUG_ON(timer_pending(timer) || !timer->function); 285 BUG_ON(timer_pending(timer) || !timer->function);
286 286
287 check_timer(timer); 287 check_timer(timer);
288 288
289 spin_lock_irqsave(&base->t_base.lock, flags); 289 spin_lock_irqsave(&base->t_base.lock, flags);
290 timer->base = &base->t_base; 290 timer->base = &base->t_base;
291 internal_add_timer(base, timer); 291 internal_add_timer(base, timer);
292 spin_unlock_irqrestore(&base->t_base.lock, flags); 292 spin_unlock_irqrestore(&base->t_base.lock, flags);
293 } 293 }
294 294
295 295
296 /*** 296 /***
297 * mod_timer - modify a timer's timeout 297 * mod_timer - modify a timer's timeout
298 * @timer: the timer to be modified 298 * @timer: the timer to be modified
299 * 299 *
300 * mod_timer is a more efficient way to update the expire field of an 300 * mod_timer is a more efficient way to update the expire field of an
301 * active timer (if the timer is inactive it will be activated) 301 * active timer (if the timer is inactive it will be activated)
302 * 302 *
303 * mod_timer(timer, expires) is equivalent to: 303 * mod_timer(timer, expires) is equivalent to:
304 * 304 *
305 * del_timer(timer); timer->expires = expires; add_timer(timer); 305 * del_timer(timer); timer->expires = expires; add_timer(timer);
306 * 306 *
307 * Note that if there are multiple unserialized concurrent users of the 307 * Note that if there are multiple unserialized concurrent users of the
308 * same timer, then mod_timer() is the only safe way to modify the timeout, 308 * same timer, then mod_timer() is the only safe way to modify the timeout,
309 * since add_timer() cannot modify an already running timer. 309 * since add_timer() cannot modify an already running timer.
310 * 310 *
311 * The function returns whether it has modified a pending timer or not. 311 * The function returns whether it has modified a pending timer or not.
312 * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an 312 * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
313 * active timer returns 1.) 313 * active timer returns 1.)
314 */ 314 */
315 int mod_timer(struct timer_list *timer, unsigned long expires) 315 int mod_timer(struct timer_list *timer, unsigned long expires)
316 { 316 {
317 BUG_ON(!timer->function); 317 BUG_ON(!timer->function);
318 318
319 check_timer(timer); 319 check_timer(timer);
320 320
321 /* 321 /*
322 * This is a common optimization triggered by the 322 * This is a common optimization triggered by the
323 * networking code - if the timer is re-modified 323 * networking code - if the timer is re-modified
324 * to be the same thing then just return: 324 * to be the same thing then just return:
325 */ 325 */
326 if (timer->expires == expires && timer_pending(timer)) 326 if (timer->expires == expires && timer_pending(timer))
327 return 1; 327 return 1;
328 328
329 return __mod_timer(timer, expires); 329 return __mod_timer(timer, expires);
330 } 330 }
331 331
332 EXPORT_SYMBOL(mod_timer); 332 EXPORT_SYMBOL(mod_timer);
333 333
334 /*** 334 /***
335 * del_timer - deactive a timer. 335 * del_timer - deactive a timer.
336 * @timer: the timer to be deactivated 336 * @timer: the timer to be deactivated
337 * 337 *
338 * del_timer() deactivates a timer - this works on both active and inactive 338 * del_timer() deactivates a timer - this works on both active and inactive
339 * timers. 339 * timers.
340 * 340 *
341 * The function returns whether it has deactivated a pending timer or not. 341 * The function returns whether it has deactivated a pending timer or not.
342 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an 342 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
343 * active timer returns 1.) 343 * active timer returns 1.)
344 */ 344 */
345 int del_timer(struct timer_list *timer) 345 int del_timer(struct timer_list *timer)
346 { 346 {
347 timer_base_t *base; 347 timer_base_t *base;
348 unsigned long flags; 348 unsigned long flags;
349 int ret = 0; 349 int ret = 0;
350 350
351 check_timer(timer); 351 check_timer(timer);
352 352
353 if (timer_pending(timer)) { 353 if (timer_pending(timer)) {
354 base = lock_timer_base(timer, &flags); 354 base = lock_timer_base(timer, &flags);
355 if (timer_pending(timer)) { 355 if (timer_pending(timer)) {
356 detach_timer(timer, 1); 356 detach_timer(timer, 1);
357 ret = 1; 357 ret = 1;
358 } 358 }
359 spin_unlock_irqrestore(&base->lock, flags); 359 spin_unlock_irqrestore(&base->lock, flags);
360 } 360 }
361 361
362 return ret; 362 return ret;
363 } 363 }
364 364
365 EXPORT_SYMBOL(del_timer); 365 EXPORT_SYMBOL(del_timer);
366 366
367 #ifdef CONFIG_SMP 367 #ifdef CONFIG_SMP
368 /* 368 /*
369 * This function tries to deactivate a timer. Upon successful (ret >= 0) 369 * This function tries to deactivate a timer. Upon successful (ret >= 0)
370 * exit the timer is not queued and the handler is not running on any CPU. 370 * exit the timer is not queued and the handler is not running on any CPU.
371 * 371 *
372 * It must not be called from interrupt contexts. 372 * It must not be called from interrupt contexts.
373 */ 373 */
374 int try_to_del_timer_sync(struct timer_list *timer) 374 int try_to_del_timer_sync(struct timer_list *timer)
375 { 375 {
376 timer_base_t *base; 376 timer_base_t *base;
377 unsigned long flags; 377 unsigned long flags;
378 int ret = -1; 378 int ret = -1;
379 379
380 base = lock_timer_base(timer, &flags); 380 base = lock_timer_base(timer, &flags);
381 381
382 if (base->running_timer == timer) 382 if (base->running_timer == timer)
383 goto out; 383 goto out;
384 384
385 ret = 0; 385 ret = 0;
386 if (timer_pending(timer)) { 386 if (timer_pending(timer)) {
387 detach_timer(timer, 1); 387 detach_timer(timer, 1);
388 ret = 1; 388 ret = 1;
389 } 389 }
390 out: 390 out:
391 spin_unlock_irqrestore(&base->lock, flags); 391 spin_unlock_irqrestore(&base->lock, flags);
392 392
393 return ret; 393 return ret;
394 } 394 }
395 395
396 /*** 396 /***
397 * del_timer_sync - deactivate a timer and wait for the handler to finish. 397 * del_timer_sync - deactivate a timer and wait for the handler to finish.
398 * @timer: the timer to be deactivated 398 * @timer: the timer to be deactivated
399 * 399 *
400 * This function only differs from del_timer() on SMP: besides deactivating 400 * This function only differs from del_timer() on SMP: besides deactivating
401 * the timer it also makes sure the handler has finished executing on other 401 * the timer it also makes sure the handler has finished executing on other
402 * CPUs. 402 * CPUs.
403 * 403 *
404 * Synchronization rules: callers must prevent restarting of the timer, 404 * Synchronization rules: callers must prevent restarting of the timer,
405 * otherwise this function is meaningless. It must not be called from 405 * otherwise this function is meaningless. It must not be called from
406 * interrupt contexts. The caller must not hold locks which would prevent 406 * interrupt contexts. The caller must not hold locks which would prevent
407 * completion of the timer's handler. The timer's handler must not call 407 * completion of the timer's handler. The timer's handler must not call
408 * add_timer_on(). Upon exit the timer is not queued and the handler is 408 * add_timer_on(). Upon exit the timer is not queued and the handler is
409 * not running on any CPU. 409 * not running on any CPU.
410 * 410 *
411 * The function returns whether it has deactivated a pending timer or not. 411 * The function returns whether it has deactivated a pending timer or not.
412 */ 412 */
413 int del_timer_sync(struct timer_list *timer) 413 int del_timer_sync(struct timer_list *timer)
414 { 414 {
415 check_timer(timer); 415 check_timer(timer);
416 416
417 for (;;) { 417 for (;;) {
418 int ret = try_to_del_timer_sync(timer); 418 int ret = try_to_del_timer_sync(timer);
419 if (ret >= 0) 419 if (ret >= 0)
420 return ret; 420 return ret;
421 } 421 }
422 } 422 }
423 423
424 EXPORT_SYMBOL(del_timer_sync); 424 EXPORT_SYMBOL(del_timer_sync);
425 #endif 425 #endif
426 426
427 static int cascade(tvec_base_t *base, tvec_t *tv, int index) 427 static int cascade(tvec_base_t *base, tvec_t *tv, int index)
428 { 428 {
429 /* cascade all the timers from tv up one level */ 429 /* cascade all the timers from tv up one level */
430 struct list_head *head, *curr; 430 struct list_head *head, *curr;
431 431
432 head = tv->vec + index; 432 head = tv->vec + index;
433 curr = head->next; 433 curr = head->next;
434 /* 434 /*
435 * We are removing _all_ timers from the list, so we don't have to 435 * We are removing _all_ timers from the list, so we don't have to
436 * detach them individually, just clear the list afterwards. 436 * detach them individually, just clear the list afterwards.
437 */ 437 */
438 while (curr != head) { 438 while (curr != head) {
439 struct timer_list *tmp; 439 struct timer_list *tmp;
440 440
441 tmp = list_entry(curr, struct timer_list, entry); 441 tmp = list_entry(curr, struct timer_list, entry);
442 BUG_ON(tmp->base != &base->t_base); 442 BUG_ON(tmp->base != &base->t_base);
443 curr = curr->next; 443 curr = curr->next;
444 internal_add_timer(base, tmp); 444 internal_add_timer(base, tmp);
445 } 445 }
446 INIT_LIST_HEAD(head); 446 INIT_LIST_HEAD(head);
447 447
448 return index; 448 return index;
449 } 449 }
450 450
451 /*** 451 /***
452 * __run_timers - run all expired timers (if any) on this CPU. 452 * __run_timers - run all expired timers (if any) on this CPU.
453 * @base: the timer vector to be processed. 453 * @base: the timer vector to be processed.
454 * 454 *
455 * This function cascades all vectors and executes all expired timer 455 * This function cascades all vectors and executes all expired timer
456 * vectors. 456 * vectors.
457 */ 457 */
458 #define INDEX(N) (base->timer_jiffies >> (TVR_BITS + N * TVN_BITS)) & TVN_MASK 458 #define INDEX(N) (base->timer_jiffies >> (TVR_BITS + N * TVN_BITS)) & TVN_MASK
459 459
460 static inline void __run_timers(tvec_base_t *base) 460 static inline void __run_timers(tvec_base_t *base)
461 { 461 {
462 struct timer_list *timer; 462 struct timer_list *timer;
463 463
464 spin_lock_irq(&base->t_base.lock); 464 spin_lock_irq(&base->t_base.lock);
465 while (time_after_eq(jiffies, base->timer_jiffies)) { 465 while (time_after_eq(jiffies, base->timer_jiffies)) {
466 struct list_head work_list = LIST_HEAD_INIT(work_list); 466 struct list_head work_list = LIST_HEAD_INIT(work_list);
467 struct list_head *head = &work_list; 467 struct list_head *head = &work_list;
468 int index = base->timer_jiffies & TVR_MASK; 468 int index = base->timer_jiffies & TVR_MASK;
469 469
470 /* 470 /*
471 * Cascade timers: 471 * Cascade timers:
472 */ 472 */
473 if (!index && 473 if (!index &&
474 (!cascade(base, &base->tv2, INDEX(0))) && 474 (!cascade(base, &base->tv2, INDEX(0))) &&
475 (!cascade(base, &base->tv3, INDEX(1))) && 475 (!cascade(base, &base->tv3, INDEX(1))) &&
476 !cascade(base, &base->tv4, INDEX(2))) 476 !cascade(base, &base->tv4, INDEX(2)))
477 cascade(base, &base->tv5, INDEX(3)); 477 cascade(base, &base->tv5, INDEX(3));
478 ++base->timer_jiffies; 478 ++base->timer_jiffies;
479 list_splice_init(base->tv1.vec + index, &work_list); 479 list_splice_init(base->tv1.vec + index, &work_list);
480 while (!list_empty(head)) { 480 while (!list_empty(head)) {
481 void (*fn)(unsigned long); 481 void (*fn)(unsigned long);
482 unsigned long data; 482 unsigned long data;
483 483
484 timer = list_entry(head->next,struct timer_list,entry); 484 timer = list_entry(head->next,struct timer_list,entry);
485 fn = timer->function; 485 fn = timer->function;
486 data = timer->data; 486 data = timer->data;
487 487
488 set_running_timer(base, timer); 488 set_running_timer(base, timer);
489 detach_timer(timer, 1); 489 detach_timer(timer, 1);
490 spin_unlock_irq(&base->t_base.lock); 490 spin_unlock_irq(&base->t_base.lock);
491 { 491 {
492 int preempt_count = preempt_count(); 492 int preempt_count = preempt_count();
493 fn(data); 493 fn(data);
494 if (preempt_count != preempt_count()) { 494 if (preempt_count != preempt_count()) {
495 printk(KERN_WARNING "huh, entered %p " 495 printk(KERN_WARNING "huh, entered %p "
496 "with preempt_count %08x, exited" 496 "with preempt_count %08x, exited"
497 " with %08x?\n", 497 " with %08x?\n",
498 fn, preempt_count, 498 fn, preempt_count,
499 preempt_count()); 499 preempt_count());
500 BUG(); 500 BUG();
501 } 501 }
502 } 502 }
503 spin_lock_irq(&base->t_base.lock); 503 spin_lock_irq(&base->t_base.lock);
504 } 504 }
505 } 505 }
506 set_running_timer(base, NULL); 506 set_running_timer(base, NULL);
507 spin_unlock_irq(&base->t_base.lock); 507 spin_unlock_irq(&base->t_base.lock);
508 } 508 }
509 509
510 #ifdef CONFIG_NO_IDLE_HZ 510 #ifdef CONFIG_NO_IDLE_HZ
511 /* 511 /*
512 * Find out when the next timer event is due to happen. This 512 * Find out when the next timer event is due to happen. This
513 * is used on S/390 to stop all activity when a cpus is idle. 513 * is used on S/390 to stop all activity when a cpus is idle.
514 * This functions needs to be called disabled. 514 * This functions needs to be called disabled.
515 */ 515 */
516 unsigned long next_timer_interrupt(void) 516 unsigned long next_timer_interrupt(void)
517 { 517 {
518 tvec_base_t *base; 518 tvec_base_t *base;
519 struct list_head *list; 519 struct list_head *list;
520 struct timer_list *nte; 520 struct timer_list *nte;
521 unsigned long expires; 521 unsigned long expires;
522 tvec_t *varray[4]; 522 tvec_t *varray[4];
523 int i, j; 523 int i, j;
524 524
525 base = &__get_cpu_var(tvec_bases); 525 base = &__get_cpu_var(tvec_bases);
526 spin_lock(&base->t_base.lock); 526 spin_lock(&base->t_base.lock);
527 expires = base->timer_jiffies + (LONG_MAX >> 1); 527 expires = base->timer_jiffies + (LONG_MAX >> 1);
528 list = 0; 528 list = 0;
529 529
530 /* Look for timer events in tv1. */ 530 /* Look for timer events in tv1. */
531 j = base->timer_jiffies & TVR_MASK; 531 j = base->timer_jiffies & TVR_MASK;
532 do { 532 do {
533 list_for_each_entry(nte, base->tv1.vec + j, entry) { 533 list_for_each_entry(nte, base->tv1.vec + j, entry) {
534 expires = nte->expires; 534 expires = nte->expires;
535 if (j < (base->timer_jiffies & TVR_MASK)) 535 if (j < (base->timer_jiffies & TVR_MASK))
536 list = base->tv2.vec + (INDEX(0)); 536 list = base->tv2.vec + (INDEX(0));
537 goto found; 537 goto found;
538 } 538 }
539 j = (j + 1) & TVR_MASK; 539 j = (j + 1) & TVR_MASK;
540 } while (j != (base->timer_jiffies & TVR_MASK)); 540 } while (j != (base->timer_jiffies & TVR_MASK));
541 541
542 /* Check tv2-tv5. */ 542 /* Check tv2-tv5. */
543 varray[0] = &base->tv2; 543 varray[0] = &base->tv2;
544 varray[1] = &base->tv3; 544 varray[1] = &base->tv3;
545 varray[2] = &base->tv4; 545 varray[2] = &base->tv4;
546 varray[3] = &base->tv5; 546 varray[3] = &base->tv5;
547 for (i = 0; i < 4; i++) { 547 for (i = 0; i < 4; i++) {
548 j = INDEX(i); 548 j = INDEX(i);
549 do { 549 do {
550 if (list_empty(varray[i]->vec + j)) { 550 if (list_empty(varray[i]->vec + j)) {
551 j = (j + 1) & TVN_MASK; 551 j = (j + 1) & TVN_MASK;
552 continue; 552 continue;
553 } 553 }
554 list_for_each_entry(nte, varray[i]->vec + j, entry) 554 list_for_each_entry(nte, varray[i]->vec + j, entry)
555 if (time_before(nte->expires, expires)) 555 if (time_before(nte->expires, expires))
556 expires = nte->expires; 556 expires = nte->expires;
557 if (j < (INDEX(i)) && i < 3) 557 if (j < (INDEX(i)) && i < 3)
558 list = varray[i + 1]->vec + (INDEX(i + 1)); 558 list = varray[i + 1]->vec + (INDEX(i + 1));
559 goto found; 559 goto found;
560 } while (j != (INDEX(i))); 560 } while (j != (INDEX(i)));
561 } 561 }
562 found: 562 found:
563 if (list) { 563 if (list) {
564 /* 564 /*
565 * The search wrapped. We need to look at the next list 565 * The search wrapped. We need to look at the next list
566 * from next tv element that would cascade into tv element 566 * from next tv element that would cascade into tv element
567 * where we found the timer element. 567 * where we found the timer element.
568 */ 568 */
569 list_for_each_entry(nte, list, entry) { 569 list_for_each_entry(nte, list, entry) {
570 if (time_before(nte->expires, expires)) 570 if (time_before(nte->expires, expires))
571 expires = nte->expires; 571 expires = nte->expires;
572 } 572 }
573 } 573 }
574 spin_unlock(&base->t_base.lock); 574 spin_unlock(&base->t_base.lock);
575 return expires; 575 return expires;
576 } 576 }
577 #endif 577 #endif
578 578
579 /******************************************************************/ 579 /******************************************************************/
580 580
581 /* 581 /*
582 * Timekeeping variables 582 * Timekeeping variables
583 */ 583 */
584 unsigned long tick_usec = TICK_USEC; /* USER_HZ period (usec) */ 584 unsigned long tick_usec = TICK_USEC; /* USER_HZ period (usec) */
585 unsigned long tick_nsec = TICK_NSEC; /* ACTHZ period (nsec) */ 585 unsigned long tick_nsec = TICK_NSEC; /* ACTHZ period (nsec) */
586 586
587 /* 587 /*
588 * The current time 588 * The current time
589 * wall_to_monotonic is what we need to add to xtime (or xtime corrected 589 * wall_to_monotonic is what we need to add to xtime (or xtime corrected
590 * for sub jiffie times) to get to monotonic time. Monotonic is pegged 590 * for sub jiffie times) to get to monotonic time. Monotonic is pegged
591 * at zero at system boot time, so wall_to_monotonic will be negative, 591 * at zero at system boot time, so wall_to_monotonic will be negative,
592 * however, we will ALWAYS keep the tv_nsec part positive so we can use 592 * however, we will ALWAYS keep the tv_nsec part positive so we can use
593 * the usual normalization. 593 * the usual normalization.
594 */ 594 */
595 struct timespec xtime __attribute__ ((aligned (16))); 595 struct timespec xtime __attribute__ ((aligned (16)));
596 struct timespec wall_to_monotonic __attribute__ ((aligned (16))); 596 struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
597 597
598 EXPORT_SYMBOL(xtime); 598 EXPORT_SYMBOL(xtime);
599 599
600 /* Don't completely fail for HZ > 500. */ 600 /* Don't completely fail for HZ > 500. */
601 int tickadj = 500/HZ ? : 1; /* microsecs */ 601 int tickadj = 500/HZ ? : 1; /* microsecs */
602 602
603 603
604 /* 604 /*
605 * phase-lock loop variables 605 * phase-lock loop variables
606 */ 606 */
607 /* TIME_ERROR prevents overwriting the CMOS clock */ 607 /* TIME_ERROR prevents overwriting the CMOS clock */
608 int time_state = TIME_OK; /* clock synchronization status */ 608 int time_state = TIME_OK; /* clock synchronization status */
609 int time_status = STA_UNSYNC; /* clock status bits */ 609 int time_status = STA_UNSYNC; /* clock status bits */
610 long time_offset; /* time adjustment (us) */ 610 long time_offset; /* time adjustment (us) */
611 long time_constant = 2; /* pll time constant */ 611 long time_constant = 2; /* pll time constant */
612 long time_tolerance = MAXFREQ; /* frequency tolerance (ppm) */ 612 long time_tolerance = MAXFREQ; /* frequency tolerance (ppm) */
613 long time_precision = 1; /* clock precision (us) */ 613 long time_precision = 1; /* clock precision (us) */
614 long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */ 614 long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */
615 long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */ 615 long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */
616 static long time_phase; /* phase offset (scaled us) */ 616 static long time_phase; /* phase offset (scaled us) */
617 long time_freq = (((NSEC_PER_SEC + HZ/2) % HZ - HZ/2) << SHIFT_USEC) / NSEC_PER_USEC; 617 long time_freq = (((NSEC_PER_SEC + HZ/2) % HZ - HZ/2) << SHIFT_USEC) / NSEC_PER_USEC;
618 /* frequency offset (scaled ppm)*/ 618 /* frequency offset (scaled ppm)*/
619 static long time_adj; /* tick adjust (scaled 1 / HZ) */ 619 static long time_adj; /* tick adjust (scaled 1 / HZ) */
620 long time_reftime; /* time at last adjustment (s) */ 620 long time_reftime; /* time at last adjustment (s) */
621 long time_adjust; 621 long time_adjust;
622 long time_next_adjust; 622 long time_next_adjust;
623 623
624 /* 624 /*
625 * this routine handles the overflow of the microsecond field 625 * this routine handles the overflow of the microsecond field
626 * 626 *
627 * The tricky bits of code to handle the accurate clock support 627 * The tricky bits of code to handle the accurate clock support
628 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame. 628 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
629 * They were originally developed for SUN and DEC kernels. 629 * They were originally developed for SUN and DEC kernels.
630 * All the kudos should go to Dave for this stuff. 630 * All the kudos should go to Dave for this stuff.
631 * 631 *
632 */ 632 */
633 static void second_overflow(void) 633 static void second_overflow(void)
634 { 634 {
635 long ltemp; 635 long ltemp;
636 636
637 /* Bump the maxerror field */ 637 /* Bump the maxerror field */
638 time_maxerror += time_tolerance >> SHIFT_USEC; 638 time_maxerror += time_tolerance >> SHIFT_USEC;
639 if ( time_maxerror > NTP_PHASE_LIMIT ) { 639 if ( time_maxerror > NTP_PHASE_LIMIT ) {
640 time_maxerror = NTP_PHASE_LIMIT; 640 time_maxerror = NTP_PHASE_LIMIT;
641 time_status |= STA_UNSYNC; 641 time_status |= STA_UNSYNC;
642 } 642 }
643 643
644 /* 644 /*
645 * Leap second processing. If in leap-insert state at 645 * Leap second processing. If in leap-insert state at
646 * the end of the day, the system clock is set back one 646 * the end of the day, the system clock is set back one
647 * second; if in leap-delete state, the system clock is 647 * second; if in leap-delete state, the system clock is
648 * set ahead one second. The microtime() routine or 648 * set ahead one second. The microtime() routine or
649 * external clock driver will insure that reported time 649 * external clock driver will insure that reported time
650 * is always monotonic. The ugly divides should be 650 * is always monotonic. The ugly divides should be
651 * replaced. 651 * replaced.
652 */ 652 */
653 switch (time_state) { 653 switch (time_state) {
654 654
655 case TIME_OK: 655 case TIME_OK:
656 if (time_status & STA_INS) 656 if (time_status & STA_INS)
657 time_state = TIME_INS; 657 time_state = TIME_INS;
658 else if (time_status & STA_DEL) 658 else if (time_status & STA_DEL)
659 time_state = TIME_DEL; 659 time_state = TIME_DEL;
660 break; 660 break;
661 661
662 case TIME_INS: 662 case TIME_INS:
663 if (xtime.tv_sec % 86400 == 0) { 663 if (xtime.tv_sec % 86400 == 0) {
664 xtime.tv_sec--; 664 xtime.tv_sec--;
665 wall_to_monotonic.tv_sec++; 665 wall_to_monotonic.tv_sec++;
666 /* The timer interpolator will make time change gradually instead 666 /* The timer interpolator will make time change gradually instead
667 * of an immediate jump by one second. 667 * of an immediate jump by one second.
668 */ 668 */
669 time_interpolator_update(-NSEC_PER_SEC); 669 time_interpolator_update(-NSEC_PER_SEC);
670 time_state = TIME_OOP; 670 time_state = TIME_OOP;
671 clock_was_set(); 671 clock_was_set();
672 printk(KERN_NOTICE "Clock: inserting leap second 23:59:60 UTC\n"); 672 printk(KERN_NOTICE "Clock: inserting leap second 23:59:60 UTC\n");
673 } 673 }
674 break; 674 break;
675 675
676 case TIME_DEL: 676 case TIME_DEL:
677 if ((xtime.tv_sec + 1) % 86400 == 0) { 677 if ((xtime.tv_sec + 1) % 86400 == 0) {
678 xtime.tv_sec++; 678 xtime.tv_sec++;
679 wall_to_monotonic.tv_sec--; 679 wall_to_monotonic.tv_sec--;
680 /* Use of time interpolator for a gradual change of time */ 680 /* Use of time interpolator for a gradual change of time */
681 time_interpolator_update(NSEC_PER_SEC); 681 time_interpolator_update(NSEC_PER_SEC);
682 time_state = TIME_WAIT; 682 time_state = TIME_WAIT;
683 clock_was_set(); 683 clock_was_set();
684 printk(KERN_NOTICE "Clock: deleting leap second 23:59:59 UTC\n"); 684 printk(KERN_NOTICE "Clock: deleting leap second 23:59:59 UTC\n");
685 } 685 }
686 break; 686 break;
687 687
688 case TIME_OOP: 688 case TIME_OOP:
689 time_state = TIME_WAIT; 689 time_state = TIME_WAIT;
690 break; 690 break;
691 691
692 case TIME_WAIT: 692 case TIME_WAIT:
693 if (!(time_status & (STA_INS | STA_DEL))) 693 if (!(time_status & (STA_INS | STA_DEL)))
694 time_state = TIME_OK; 694 time_state = TIME_OK;
695 } 695 }
696 696
697 /* 697 /*
698 * Compute the phase adjustment for the next second. In 698 * Compute the phase adjustment for the next second. In
699 * PLL mode, the offset is reduced by a fixed factor 699 * PLL mode, the offset is reduced by a fixed factor
700 * times the time constant. In FLL mode the offset is 700 * times the time constant. In FLL mode the offset is
701 * used directly. In either mode, the maximum phase 701 * used directly. In either mode, the maximum phase
702 * adjustment for each second is clamped so as to spread 702 * adjustment for each second is clamped so as to spread
703 * the adjustment over not more than the number of 703 * the adjustment over not more than the number of
704 * seconds between updates. 704 * seconds between updates.
705 */ 705 */
706 if (time_offset < 0) { 706 if (time_offset < 0) {
707 ltemp = -time_offset; 707 ltemp = -time_offset;
708 if (!(time_status & STA_FLL)) 708 if (!(time_status & STA_FLL))
709 ltemp >>= SHIFT_KG + time_constant; 709 ltemp >>= SHIFT_KG + time_constant;
710 if (ltemp > (MAXPHASE / MINSEC) << SHIFT_UPDATE) 710 if (ltemp > (MAXPHASE / MINSEC) << SHIFT_UPDATE)
711 ltemp = (MAXPHASE / MINSEC) << SHIFT_UPDATE; 711 ltemp = (MAXPHASE / MINSEC) << SHIFT_UPDATE;
712 time_offset += ltemp; 712 time_offset += ltemp;
713 time_adj = -ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE); 713 time_adj = -ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
714 } else { 714 } else {
715 ltemp = time_offset; 715 ltemp = time_offset;
716 if (!(time_status & STA_FLL)) 716 if (!(time_status & STA_FLL))
717 ltemp >>= SHIFT_KG + time_constant; 717 ltemp >>= SHIFT_KG + time_constant;
718 if (ltemp > (MAXPHASE / MINSEC) << SHIFT_UPDATE) 718 if (ltemp > (MAXPHASE / MINSEC) << SHIFT_UPDATE)
719 ltemp = (MAXPHASE / MINSEC) << SHIFT_UPDATE; 719 ltemp = (MAXPHASE / MINSEC) << SHIFT_UPDATE;
720 time_offset -= ltemp; 720 time_offset -= ltemp;
721 time_adj = ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE); 721 time_adj = ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
722 } 722 }
723 723
724 /* 724 /*
725 * Compute the frequency estimate and additional phase 725 * Compute the frequency estimate and additional phase
726 * adjustment due to frequency error for the next 726 * adjustment due to frequency error for the next
727 * second. When the PPS signal is engaged, gnaw on the 727 * second. When the PPS signal is engaged, gnaw on the
728 * watchdog counter and update the frequency computed by 728 * watchdog counter and update the frequency computed by
729 * the pll and the PPS signal. 729 * the pll and the PPS signal.
730 */ 730 */
731 pps_valid++; 731 pps_valid++;
732 if (pps_valid == PPS_VALID) { /* PPS signal lost */ 732 if (pps_valid == PPS_VALID) { /* PPS signal lost */
733 pps_jitter = MAXTIME; 733 pps_jitter = MAXTIME;
734 pps_stabil = MAXFREQ; 734 pps_stabil = MAXFREQ;
735 time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER | 735 time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER |
736 STA_PPSWANDER | STA_PPSERROR); 736 STA_PPSWANDER | STA_PPSERROR);
737 } 737 }
738 ltemp = time_freq + pps_freq; 738 ltemp = time_freq + pps_freq;
739 if (ltemp < 0) 739 if (ltemp < 0)
740 time_adj -= -ltemp >> 740 time_adj -= -ltemp >>
741 (SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE); 741 (SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE);
742 else 742 else
743 time_adj += ltemp >> 743 time_adj += ltemp >>
744 (SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE); 744 (SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE);
745 745
746 #if HZ == 100 746 #if HZ == 100
747 /* Compensate for (HZ==100) != (1 << SHIFT_HZ). 747 /* Compensate for (HZ==100) != (1 << SHIFT_HZ).
748 * Add 25% and 3.125% to get 128.125; => only 0.125% error (p. 14) 748 * Add 25% and 3.125% to get 128.125; => only 0.125% error (p. 14)
749 */ 749 */
750 if (time_adj < 0) 750 if (time_adj < 0)
751 time_adj -= (-time_adj >> 2) + (-time_adj >> 5); 751 time_adj -= (-time_adj >> 2) + (-time_adj >> 5);
752 else 752 else
753 time_adj += (time_adj >> 2) + (time_adj >> 5); 753 time_adj += (time_adj >> 2) + (time_adj >> 5);
754 #endif 754 #endif
755 #if HZ == 1000 755 #if HZ == 1000
756 /* Compensate for (HZ==1000) != (1 << SHIFT_HZ). 756 /* Compensate for (HZ==1000) != (1 << SHIFT_HZ).
757 * Add 1.5625% and 0.78125% to get 1023.4375; => only 0.05% error (p. 14) 757 * Add 1.5625% and 0.78125% to get 1023.4375; => only 0.05% error (p. 14)
758 */ 758 */
759 if (time_adj < 0) 759 if (time_adj < 0)
760 time_adj -= (-time_adj >> 6) + (-time_adj >> 7); 760 time_adj -= (-time_adj >> 6) + (-time_adj >> 7);
761 else 761 else
762 time_adj += (time_adj >> 6) + (time_adj >> 7); 762 time_adj += (time_adj >> 6) + (time_adj >> 7);
763 #endif 763 #endif
764 } 764 }
765 765
766 /* in the NTP reference this is called "hardclock()" */ 766 /* in the NTP reference this is called "hardclock()" */
767 static void update_wall_time_one_tick(void) 767 static void update_wall_time_one_tick(void)
768 { 768 {
769 long time_adjust_step, delta_nsec; 769 long time_adjust_step, delta_nsec;
770 770
771 if ( (time_adjust_step = time_adjust) != 0 ) { 771 if ( (time_adjust_step = time_adjust) != 0 ) {
772 /* We are doing an adjtime thing. 772 /* We are doing an adjtime thing.
773 * 773 *
774 * Prepare time_adjust_step to be within bounds. 774 * Prepare time_adjust_step to be within bounds.
775 * Note that a positive time_adjust means we want the clock 775 * Note that a positive time_adjust means we want the clock
776 * to run faster. 776 * to run faster.
777 * 777 *
778 * Limit the amount of the step to be in the range 778 * Limit the amount of the step to be in the range
779 * -tickadj .. +tickadj 779 * -tickadj .. +tickadj
780 */ 780 */
781 if (time_adjust > tickadj) 781 if (time_adjust > tickadj)
782 time_adjust_step = tickadj; 782 time_adjust_step = tickadj;
783 else if (time_adjust < -tickadj) 783 else if (time_adjust < -tickadj)
784 time_adjust_step = -tickadj; 784 time_adjust_step = -tickadj;
785 785
786 /* Reduce by this step the amount of time left */ 786 /* Reduce by this step the amount of time left */
787 time_adjust -= time_adjust_step; 787 time_adjust -= time_adjust_step;
788 } 788 }
789 delta_nsec = tick_nsec + time_adjust_step * 1000; 789 delta_nsec = tick_nsec + time_adjust_step * 1000;
790 /* 790 /*
791 * Advance the phase, once it gets to one microsecond, then 791 * Advance the phase, once it gets to one microsecond, then
792 * advance the tick more. 792 * advance the tick more.
793 */ 793 */
794 time_phase += time_adj; 794 time_phase += time_adj;
795 if (time_phase <= -FINENSEC) { 795 if (time_phase <= -FINENSEC) {
796 long ltemp = -time_phase >> (SHIFT_SCALE - 10); 796 long ltemp = -time_phase >> (SHIFT_SCALE - 10);
797 time_phase += ltemp << (SHIFT_SCALE - 10); 797 time_phase += ltemp << (SHIFT_SCALE - 10);
798 delta_nsec -= ltemp; 798 delta_nsec -= ltemp;
799 } 799 }
800 else if (time_phase >= FINENSEC) { 800 else if (time_phase >= FINENSEC) {
801 long ltemp = time_phase >> (SHIFT_SCALE - 10); 801 long ltemp = time_phase >> (SHIFT_SCALE - 10);
802 time_phase -= ltemp << (SHIFT_SCALE - 10); 802 time_phase -= ltemp << (SHIFT_SCALE - 10);
803 delta_nsec += ltemp; 803 delta_nsec += ltemp;
804 } 804 }
805 xtime.tv_nsec += delta_nsec; 805 xtime.tv_nsec += delta_nsec;
806 time_interpolator_update(delta_nsec); 806 time_interpolator_update(delta_nsec);
807 807
808 /* Changes by adjtime() do not take effect till next tick. */ 808 /* Changes by adjtime() do not take effect till next tick. */
809 if (time_next_adjust != 0) { 809 if (time_next_adjust != 0) {
810 time_adjust = time_next_adjust; 810 time_adjust = time_next_adjust;
811 time_next_adjust = 0; 811 time_next_adjust = 0;
812 } 812 }
813 } 813 }
814 814
815 /* 815 /*
816 * Using a loop looks inefficient, but "ticks" is 816 * Using a loop looks inefficient, but "ticks" is
817 * usually just one (we shouldn't be losing ticks, 817 * usually just one (we shouldn't be losing ticks,
818 * we're doing this this way mainly for interrupt 818 * we're doing this this way mainly for interrupt
819 * latency reasons, not because we think we'll 819 * latency reasons, not because we think we'll
820 * have lots of lost timer ticks 820 * have lots of lost timer ticks
821 */ 821 */
822 static void update_wall_time(unsigned long ticks) 822 static void update_wall_time(unsigned long ticks)
823 { 823 {
824 do { 824 do {
825 ticks--; 825 ticks--;
826 update_wall_time_one_tick(); 826 update_wall_time_one_tick();
827 if (xtime.tv_nsec >= 1000000000) { 827 if (xtime.tv_nsec >= 1000000000) {
828 xtime.tv_nsec -= 1000000000; 828 xtime.tv_nsec -= 1000000000;
829 xtime.tv_sec++; 829 xtime.tv_sec++;
830 second_overflow(); 830 second_overflow();
831 } 831 }
832 } while (ticks); 832 } while (ticks);
833 } 833 }
834 834
835 /* 835 /*
836 * Called from the timer interrupt handler to charge one tick to the current 836 * Called from the timer interrupt handler to charge one tick to the current
837 * process. user_tick is 1 if the tick is user time, 0 for system. 837 * process. user_tick is 1 if the tick is user time, 0 for system.
838 */ 838 */
839 void update_process_times(int user_tick) 839 void update_process_times(int user_tick)
840 { 840 {
841 struct task_struct *p = current; 841 struct task_struct *p = current;
842 int cpu = smp_processor_id(); 842 int cpu = smp_processor_id();
843 843
844 /* Note: this timer irq context must be accounted for as well. */ 844 /* Note: this timer irq context must be accounted for as well. */
845 if (user_tick) 845 if (user_tick)
846 account_user_time(p, jiffies_to_cputime(1)); 846 account_user_time(p, jiffies_to_cputime(1));
847 else 847 else
848 account_system_time(p, HARDIRQ_OFFSET, jiffies_to_cputime(1)); 848 account_system_time(p, HARDIRQ_OFFSET, jiffies_to_cputime(1));
849 run_local_timers(); 849 run_local_timers();
850 if (rcu_pending(cpu)) 850 if (rcu_pending(cpu))
851 rcu_check_callbacks(cpu, user_tick); 851 rcu_check_callbacks(cpu, user_tick);
852 scheduler_tick(); 852 scheduler_tick();
853 run_posix_cpu_timers(p); 853 run_posix_cpu_timers(p);
854 } 854 }
855 855
856 /* 856 /*
857 * Nr of active tasks - counted in fixed-point numbers 857 * Nr of active tasks - counted in fixed-point numbers
858 */ 858 */
859 static unsigned long count_active_tasks(void) 859 static unsigned long count_active_tasks(void)
860 { 860 {
861 return (nr_running() + nr_uninterruptible()) * FIXED_1; 861 return (nr_running() + nr_uninterruptible()) * FIXED_1;
862 } 862 }
863 863
864 /* 864 /*
865 * Hmm.. Changed this, as the GNU make sources (load.c) seems to 865 * Hmm.. Changed this, as the GNU make sources (load.c) seems to
866 * imply that avenrun[] is the standard name for this kind of thing. 866 * imply that avenrun[] is the standard name for this kind of thing.
867 * Nothing else seems to be standardized: the fractional size etc 867 * Nothing else seems to be standardized: the fractional size etc
868 * all seem to differ on different machines. 868 * all seem to differ on different machines.
869 * 869 *
870 * Requires xtime_lock to access. 870 * Requires xtime_lock to access.
871 */ 871 */
872 unsigned long avenrun[3]; 872 unsigned long avenrun[3];
873 873
874 EXPORT_SYMBOL(avenrun); 874 EXPORT_SYMBOL(avenrun);
875 875
876 /* 876 /*
877 * calc_load - given tick count, update the avenrun load estimates. 877 * calc_load - given tick count, update the avenrun load estimates.
878 * This is called while holding a write_lock on xtime_lock. 878 * This is called while holding a write_lock on xtime_lock.
879 */ 879 */
880 static inline void calc_load(unsigned long ticks) 880 static inline void calc_load(unsigned long ticks)
881 { 881 {
882 unsigned long active_tasks; /* fixed-point */ 882 unsigned long active_tasks; /* fixed-point */
883 static int count = LOAD_FREQ; 883 static int count = LOAD_FREQ;
884 884
885 count -= ticks; 885 count -= ticks;
886 if (count < 0) { 886 if (count < 0) {
887 count += LOAD_FREQ; 887 count += LOAD_FREQ;
888 active_tasks = count_active_tasks(); 888 active_tasks = count_active_tasks();
889 CALC_LOAD(avenrun[0], EXP_1, active_tasks); 889 CALC_LOAD(avenrun[0], EXP_1, active_tasks);
890 CALC_LOAD(avenrun[1], EXP_5, active_tasks); 890 CALC_LOAD(avenrun[1], EXP_5, active_tasks);
891 CALC_LOAD(avenrun[2], EXP_15, active_tasks); 891 CALC_LOAD(avenrun[2], EXP_15, active_tasks);
892 } 892 }
893 } 893 }
894 894
895 /* jiffies at the most recent update of wall time */ 895 /* jiffies at the most recent update of wall time */
896 unsigned long wall_jiffies = INITIAL_JIFFIES; 896 unsigned long wall_jiffies = INITIAL_JIFFIES;
897 897
898 /* 898 /*
899 * This read-write spinlock protects us from races in SMP while 899 * This read-write spinlock protects us from races in SMP while
900 * playing with xtime and avenrun. 900 * playing with xtime and avenrun.
901 */ 901 */
902 #ifndef ARCH_HAVE_XTIME_LOCK 902 #ifndef ARCH_HAVE_XTIME_LOCK
903 seqlock_t xtime_lock __cacheline_aligned_in_smp = SEQLOCK_UNLOCKED; 903 seqlock_t xtime_lock __cacheline_aligned_in_smp = SEQLOCK_UNLOCKED;
904 904
905 EXPORT_SYMBOL(xtime_lock); 905 EXPORT_SYMBOL(xtime_lock);
906 #endif 906 #endif
907 907
908 /* 908 /*
909 * This function runs timers and the timer-tq in bottom half context. 909 * This function runs timers and the timer-tq in bottom half context.
910 */ 910 */
911 static void run_timer_softirq(struct softirq_action *h) 911 static void run_timer_softirq(struct softirq_action *h)
912 { 912 {
913 tvec_base_t *base = &__get_cpu_var(tvec_bases); 913 tvec_base_t *base = &__get_cpu_var(tvec_bases);
914 914
915 if (time_after_eq(jiffies, base->timer_jiffies)) 915 if (time_after_eq(jiffies, base->timer_jiffies))
916 __run_timers(base); 916 __run_timers(base);
917 } 917 }
918 918
919 /* 919 /*
920 * Called by the local, per-CPU timer interrupt on SMP. 920 * Called by the local, per-CPU timer interrupt on SMP.
921 */ 921 */
922 void run_local_timers(void) 922 void run_local_timers(void)
923 { 923 {
924 raise_softirq(TIMER_SOFTIRQ); 924 raise_softirq(TIMER_SOFTIRQ);
925 } 925 }
926 926
927 /* 927 /*
928 * Called by the timer interrupt. xtime_lock must already be taken 928 * Called by the timer interrupt. xtime_lock must already be taken
929 * by the timer IRQ! 929 * by the timer IRQ!
930 */ 930 */
931 static inline void update_times(void) 931 static inline void update_times(void)
932 { 932 {
933 unsigned long ticks; 933 unsigned long ticks;
934 934
935 ticks = jiffies - wall_jiffies; 935 ticks = jiffies - wall_jiffies;
936 if (ticks) { 936 if (ticks) {
937 wall_jiffies += ticks; 937 wall_jiffies += ticks;
938 update_wall_time(ticks); 938 update_wall_time(ticks);
939 } 939 }
940 calc_load(ticks); 940 calc_load(ticks);
941 } 941 }
942 942
943 /* 943 /*
944 * The 64-bit jiffies value is not atomic - you MUST NOT read it 944 * The 64-bit jiffies value is not atomic - you MUST NOT read it
945 * without sampling the sequence number in xtime_lock. 945 * without sampling the sequence number in xtime_lock.
946 * jiffies is defined in the linker script... 946 * jiffies is defined in the linker script...
947 */ 947 */
948 948
949 void do_timer(struct pt_regs *regs) 949 void do_timer(struct pt_regs *regs)
950 { 950 {
951 jiffies_64++; 951 jiffies_64++;
952 update_times(); 952 update_times();
953 softlockup_tick(regs); 953 softlockup_tick(regs);
954 } 954 }
955 955
956 #ifdef __ARCH_WANT_SYS_ALARM 956 #ifdef __ARCH_WANT_SYS_ALARM
957 957
958 /* 958 /*
959 * For backwards compatibility? This can be done in libc so Alpha 959 * For backwards compatibility? This can be done in libc so Alpha
960 * and all newer ports shouldn't need it. 960 * and all newer ports shouldn't need it.
961 */ 961 */
962 asmlinkage unsigned long sys_alarm(unsigned int seconds) 962 asmlinkage unsigned long sys_alarm(unsigned int seconds)
963 { 963 {
964 struct itimerval it_new, it_old; 964 struct itimerval it_new, it_old;
965 unsigned int oldalarm; 965 unsigned int oldalarm;
966 966
967 it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0; 967 it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0;
968 it_new.it_value.tv_sec = seconds; 968 it_new.it_value.tv_sec = seconds;
969 it_new.it_value.tv_usec = 0; 969 it_new.it_value.tv_usec = 0;
970 do_setitimer(ITIMER_REAL, &it_new, &it_old); 970 do_setitimer(ITIMER_REAL, &it_new, &it_old);
971 oldalarm = it_old.it_value.tv_sec; 971 oldalarm = it_old.it_value.tv_sec;
972 /* ehhh.. We can't return 0 if we have an alarm pending.. */ 972 /* ehhh.. We can't return 0 if we have an alarm pending.. */
973 /* And we'd better return too much than too little anyway */ 973 /* And we'd better return too much than too little anyway */
974 if ((!oldalarm && it_old.it_value.tv_usec) || it_old.it_value.tv_usec >= 500000) 974 if ((!oldalarm && it_old.it_value.tv_usec) || it_old.it_value.tv_usec >= 500000)
975 oldalarm++; 975 oldalarm++;
976 return oldalarm; 976 return oldalarm;
977 } 977 }
978 978
979 #endif 979 #endif
980 980
981 #ifndef __alpha__ 981 #ifndef __alpha__
982 982
983 /* 983 /*
984 * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this 984 * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
985 * should be moved into arch/i386 instead? 985 * should be moved into arch/i386 instead?
986 */ 986 */
987 987
988 /** 988 /**
989 * sys_getpid - return the thread group id of the current process 989 * sys_getpid - return the thread group id of the current process
990 * 990 *
991 * Note, despite the name, this returns the tgid not the pid. The tgid and 991 * Note, despite the name, this returns the tgid not the pid. The tgid and
992 * the pid are identical unless CLONE_THREAD was specified on clone() in 992 * the pid are identical unless CLONE_THREAD was specified on clone() in
993 * which case the tgid is the same in all threads of the same group. 993 * which case the tgid is the same in all threads of the same group.
994 * 994 *
995 * This is SMP safe as current->tgid does not change. 995 * This is SMP safe as current->tgid does not change.
996 */ 996 */
997 asmlinkage long sys_getpid(void) 997 asmlinkage long sys_getpid(void)
998 { 998 {
999 return current->tgid; 999 return current->tgid;
1000 } 1000 }
1001 1001
1002 /* 1002 /*
1003 * Accessing ->group_leader->real_parent is not SMP-safe, it could 1003 * Accessing ->group_leader->real_parent is not SMP-safe, it could
1004 * change from under us. However, rather than getting any lock 1004 * change from under us. However, rather than getting any lock
1005 * we can use an optimistic algorithm: get the parent 1005 * we can use an optimistic algorithm: get the parent
1006 * pid, and go back and check that the parent is still 1006 * pid, and go back and check that the parent is still
1007 * the same. If it has changed (which is extremely unlikely 1007 * the same. If it has changed (which is extremely unlikely
1008 * indeed), we just try again.. 1008 * indeed), we just try again..
1009 * 1009 *
1010 * NOTE! This depends on the fact that even if we _do_ 1010 * NOTE! This depends on the fact that even if we _do_
1011 * get an old value of "parent", we can happily dereference 1011 * get an old value of "parent", we can happily dereference
1012 * the pointer (it was and remains a dereferencable kernel pointer 1012 * the pointer (it was and remains a dereferencable kernel pointer
1013 * no matter what): we just can't necessarily trust the result 1013 * no matter what): we just can't necessarily trust the result
1014 * until we know that the parent pointer is valid. 1014 * until we know that the parent pointer is valid.
1015 * 1015 *
1016 * NOTE2: ->group_leader never changes from under us. 1016 * NOTE2: ->group_leader never changes from under us.
1017 */ 1017 */
1018 asmlinkage long sys_getppid(void) 1018 asmlinkage long sys_getppid(void)
1019 { 1019 {
1020 int pid; 1020 int pid;
1021 struct task_struct *me = current; 1021 struct task_struct *me = current;
1022 struct task_struct *parent; 1022 struct task_struct *parent;
1023 1023
1024 parent = me->group_leader->real_parent; 1024 parent = me->group_leader->real_parent;
1025 for (;;) { 1025 for (;;) {
1026 pid = parent->tgid; 1026 pid = parent->tgid;
1027 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT) 1027 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
1028 { 1028 {
1029 struct task_struct *old = parent; 1029 struct task_struct *old = parent;
1030 1030
1031 /* 1031 /*
1032 * Make sure we read the pid before re-reading the 1032 * Make sure we read the pid before re-reading the
1033 * parent pointer: 1033 * parent pointer:
1034 */ 1034 */
1035 smp_rmb(); 1035 smp_rmb();
1036 parent = me->group_leader->real_parent; 1036 parent = me->group_leader->real_parent;
1037 if (old != parent) 1037 if (old != parent)
1038 continue; 1038 continue;
1039 } 1039 }
1040 #endif 1040 #endif
1041 break; 1041 break;
1042 } 1042 }
1043 return pid; 1043 return pid;
1044 } 1044 }
1045 1045
1046 asmlinkage long sys_getuid(void) 1046 asmlinkage long sys_getuid(void)
1047 { 1047 {
1048 /* Only we change this so SMP safe */ 1048 /* Only we change this so SMP safe */
1049 return current->uid; 1049 return current->uid;
1050 } 1050 }
1051 1051
1052 asmlinkage long sys_geteuid(void) 1052 asmlinkage long sys_geteuid(void)
1053 { 1053 {
1054 /* Only we change this so SMP safe */ 1054 /* Only we change this so SMP safe */
1055 return current->euid; 1055 return current->euid;
1056 } 1056 }
1057 1057
1058 asmlinkage long sys_getgid(void) 1058 asmlinkage long sys_getgid(void)
1059 { 1059 {
1060 /* Only we change this so SMP safe */ 1060 /* Only we change this so SMP safe */
1061 return current->gid; 1061 return current->gid;
1062 } 1062 }
1063 1063
1064 asmlinkage long sys_getegid(void) 1064 asmlinkage long sys_getegid(void)
1065 { 1065 {
1066 /* Only we change this so SMP safe */ 1066 /* Only we change this so SMP safe */
1067 return current->egid; 1067 return current->egid;
1068 } 1068 }
1069 1069
1070 #endif 1070 #endif
1071 1071
1072 static void process_timeout(unsigned long __data) 1072 static void process_timeout(unsigned long __data)
1073 { 1073 {
1074 wake_up_process((task_t *)__data); 1074 wake_up_process((task_t *)__data);
1075 } 1075 }
1076 1076
1077 /** 1077 /**
1078 * schedule_timeout - sleep until timeout 1078 * schedule_timeout - sleep until timeout
1079 * @timeout: timeout value in jiffies 1079 * @timeout: timeout value in jiffies
1080 * 1080 *
1081 * Make the current task sleep until @timeout jiffies have 1081 * Make the current task sleep until @timeout jiffies have
1082 * elapsed. The routine will return immediately unless 1082 * elapsed. The routine will return immediately unless
1083 * the current task state has been set (see set_current_state()). 1083 * the current task state has been set (see set_current_state()).
1084 * 1084 *
1085 * You can set the task state as follows - 1085 * You can set the task state as follows -
1086 * 1086 *
1087 * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to 1087 * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
1088 * pass before the routine returns. The routine will return 0 1088 * pass before the routine returns. The routine will return 0
1089 * 1089 *
1090 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is 1090 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1091 * delivered to the current task. In this case the remaining time 1091 * delivered to the current task. In this case the remaining time
1092 * in jiffies will be returned, or 0 if the timer expired in time 1092 * in jiffies will be returned, or 0 if the timer expired in time
1093 * 1093 *
1094 * The current task state is guaranteed to be TASK_RUNNING when this 1094 * The current task state is guaranteed to be TASK_RUNNING when this
1095 * routine returns. 1095 * routine returns.
1096 * 1096 *
1097 * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule 1097 * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
1098 * the CPU away without a bound on the timeout. In this case the return 1098 * the CPU away without a bound on the timeout. In this case the return
1099 * value will be %MAX_SCHEDULE_TIMEOUT. 1099 * value will be %MAX_SCHEDULE_TIMEOUT.
1100 * 1100 *
1101 * In all cases the return value is guaranteed to be non-negative. 1101 * In all cases the return value is guaranteed to be non-negative.
1102 */ 1102 */
1103 fastcall signed long __sched schedule_timeout(signed long timeout) 1103 fastcall signed long __sched schedule_timeout(signed long timeout)
1104 { 1104 {
1105 struct timer_list timer; 1105 struct timer_list timer;
1106 unsigned long expire; 1106 unsigned long expire;
1107 1107
1108 switch (timeout) 1108 switch (timeout)
1109 { 1109 {
1110 case MAX_SCHEDULE_TIMEOUT: 1110 case MAX_SCHEDULE_TIMEOUT:
1111 /* 1111 /*
1112 * These two special cases are useful to be comfortable 1112 * These two special cases are useful to be comfortable
1113 * in the caller. Nothing more. We could take 1113 * in the caller. Nothing more. We could take
1114 * MAX_SCHEDULE_TIMEOUT from one of the negative value 1114 * MAX_SCHEDULE_TIMEOUT from one of the negative value
1115 * but I' d like to return a valid offset (>=0) to allow 1115 * but I' d like to return a valid offset (>=0) to allow
1116 * the caller to do everything it want with the retval. 1116 * the caller to do everything it want with the retval.
1117 */ 1117 */
1118 schedule(); 1118 schedule();
1119 goto out; 1119 goto out;
1120 default: 1120 default:
1121 /* 1121 /*
1122 * Another bit of PARANOID. Note that the retval will be 1122 * Another bit of PARANOID. Note that the retval will be
1123 * 0 since no piece of kernel is supposed to do a check 1123 * 0 since no piece of kernel is supposed to do a check
1124 * for a negative retval of schedule_timeout() (since it 1124 * for a negative retval of schedule_timeout() (since it
1125 * should never happens anyway). You just have the printk() 1125 * should never happens anyway). You just have the printk()
1126 * that will tell you if something is gone wrong and where. 1126 * that will tell you if something is gone wrong and where.
1127 */ 1127 */
1128 if (timeout < 0) 1128 if (timeout < 0)
1129 { 1129 {
1130 printk(KERN_ERR "schedule_timeout: wrong timeout " 1130 printk(KERN_ERR "schedule_timeout: wrong timeout "
1131 "value %lx from %p\n", timeout, 1131 "value %lx from %p\n", timeout,
1132 __builtin_return_address(0)); 1132 __builtin_return_address(0));
1133 current->state = TASK_RUNNING; 1133 current->state = TASK_RUNNING;
1134 goto out; 1134 goto out;
1135 } 1135 }
1136 } 1136 }
1137 1137
1138 expire = timeout + jiffies; 1138 expire = timeout + jiffies;
1139 1139
1140 init_timer(&timer); 1140 init_timer(&timer);
1141 timer.expires = expire; 1141 timer.expires = expire;
1142 timer.data = (unsigned long) current; 1142 timer.data = (unsigned long) current;
1143 timer.function = process_timeout; 1143 timer.function = process_timeout;
1144 1144
1145 add_timer(&timer); 1145 add_timer(&timer);
1146 schedule(); 1146 schedule();
1147 del_singleshot_timer_sync(&timer); 1147 del_singleshot_timer_sync(&timer);
1148 1148
1149 timeout = expire - jiffies; 1149 timeout = expire - jiffies;
1150 1150
1151 out: 1151 out:
1152 return timeout < 0 ? 0 : timeout; 1152 return timeout < 0 ? 0 : timeout;
1153 } 1153 }
1154
1155 EXPORT_SYMBOL(schedule_timeout); 1154 EXPORT_SYMBOL(schedule_timeout);
1156 1155
1156 /*
1157 * We can use __set_current_state() here because schedule_timeout() calls
1158 * schedule() unconditionally.
1159 */
1157 signed long __sched schedule_timeout_interruptible(signed long timeout) 1160 signed long __sched schedule_timeout_interruptible(signed long timeout)
1158 { 1161 {
1159 set_current_state(TASK_INTERRUPTIBLE); 1162 __set_current_state(TASK_INTERRUPTIBLE);
1160 return schedule_timeout(timeout); 1163 return schedule_timeout(timeout);
1161 } 1164 }
1162 EXPORT_SYMBOL(schedule_timeout_interruptible); 1165 EXPORT_SYMBOL(schedule_timeout_interruptible);
1163 1166
1164 signed long __sched schedule_timeout_uninterruptible(signed long timeout) 1167 signed long __sched schedule_timeout_uninterruptible(signed long timeout)
1165 { 1168 {
1166 set_current_state(TASK_UNINTERRUPTIBLE); 1169 __set_current_state(TASK_UNINTERRUPTIBLE);
1167 return schedule_timeout(timeout); 1170 return schedule_timeout(timeout);
1168 } 1171 }
1169 EXPORT_SYMBOL(schedule_timeout_uninterruptible); 1172 EXPORT_SYMBOL(schedule_timeout_uninterruptible);
1170 1173
1171 /* Thread ID - the internal kernel "pid" */ 1174 /* Thread ID - the internal kernel "pid" */
1172 asmlinkage long sys_gettid(void) 1175 asmlinkage long sys_gettid(void)
1173 { 1176 {
1174 return current->pid; 1177 return current->pid;
1175 } 1178 }
1176 1179
1177 static long __sched nanosleep_restart(struct restart_block *restart) 1180 static long __sched nanosleep_restart(struct restart_block *restart)
1178 { 1181 {
1179 unsigned long expire = restart->arg0, now = jiffies; 1182 unsigned long expire = restart->arg0, now = jiffies;
1180 struct timespec __user *rmtp = (struct timespec __user *) restart->arg1; 1183 struct timespec __user *rmtp = (struct timespec __user *) restart->arg1;
1181 long ret; 1184 long ret;
1182 1185
1183 /* Did it expire while we handled signals? */ 1186 /* Did it expire while we handled signals? */
1184 if (!time_after(expire, now)) 1187 if (!time_after(expire, now))
1185 return 0; 1188 return 0;
1186 1189
1187 expire = schedule_timeout_interruptible(expire - now); 1190 expire = schedule_timeout_interruptible(expire - now);
1188 1191
1189 ret = 0; 1192 ret = 0;
1190 if (expire) { 1193 if (expire) {
1191 struct timespec t; 1194 struct timespec t;
1192 jiffies_to_timespec(expire, &t); 1195 jiffies_to_timespec(expire, &t);
1193 1196
1194 ret = -ERESTART_RESTARTBLOCK; 1197 ret = -ERESTART_RESTARTBLOCK;
1195 if (rmtp && copy_to_user(rmtp, &t, sizeof(t))) 1198 if (rmtp && copy_to_user(rmtp, &t, sizeof(t)))
1196 ret = -EFAULT; 1199 ret = -EFAULT;
1197 /* The 'restart' block is already filled in */ 1200 /* The 'restart' block is already filled in */
1198 } 1201 }
1199 return ret; 1202 return ret;
1200 } 1203 }
1201 1204
1202 asmlinkage long sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp) 1205 asmlinkage long sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
1203 { 1206 {
1204 struct timespec t; 1207 struct timespec t;
1205 unsigned long expire; 1208 unsigned long expire;
1206 long ret; 1209 long ret;
1207 1210
1208 if (copy_from_user(&t, rqtp, sizeof(t))) 1211 if (copy_from_user(&t, rqtp, sizeof(t)))
1209 return -EFAULT; 1212 return -EFAULT;
1210 1213
1211 if ((t.tv_nsec >= 1000000000L) || (t.tv_nsec < 0) || (t.tv_sec < 0)) 1214 if ((t.tv_nsec >= 1000000000L) || (t.tv_nsec < 0) || (t.tv_sec < 0))
1212 return -EINVAL; 1215 return -EINVAL;
1213 1216
1214 expire = timespec_to_jiffies(&t) + (t.tv_sec || t.tv_nsec); 1217 expire = timespec_to_jiffies(&t) + (t.tv_sec || t.tv_nsec);
1215 expire = schedule_timeout_interruptible(expire); 1218 expire = schedule_timeout_interruptible(expire);
1216 1219
1217 ret = 0; 1220 ret = 0;
1218 if (expire) { 1221 if (expire) {
1219 struct restart_block *restart; 1222 struct restart_block *restart;
1220 jiffies_to_timespec(expire, &t); 1223 jiffies_to_timespec(expire, &t);
1221 if (rmtp && copy_to_user(rmtp, &t, sizeof(t))) 1224 if (rmtp && copy_to_user(rmtp, &t, sizeof(t)))
1222 return -EFAULT; 1225 return -EFAULT;
1223 1226
1224 restart = &current_thread_info()->restart_block; 1227 restart = &current_thread_info()->restart_block;
1225 restart->fn = nanosleep_restart; 1228 restart->fn = nanosleep_restart;
1226 restart->arg0 = jiffies + expire; 1229 restart->arg0 = jiffies + expire;
1227 restart->arg1 = (unsigned long) rmtp; 1230 restart->arg1 = (unsigned long) rmtp;
1228 ret = -ERESTART_RESTARTBLOCK; 1231 ret = -ERESTART_RESTARTBLOCK;
1229 } 1232 }
1230 return ret; 1233 return ret;
1231 } 1234 }
1232 1235
1233 /* 1236 /*
1234 * sys_sysinfo - fill in sysinfo struct 1237 * sys_sysinfo - fill in sysinfo struct
1235 */ 1238 */
1236 asmlinkage long sys_sysinfo(struct sysinfo __user *info) 1239 asmlinkage long sys_sysinfo(struct sysinfo __user *info)
1237 { 1240 {
1238 struct sysinfo val; 1241 struct sysinfo val;
1239 unsigned long mem_total, sav_total; 1242 unsigned long mem_total, sav_total;
1240 unsigned int mem_unit, bitcount; 1243 unsigned int mem_unit, bitcount;
1241 unsigned long seq; 1244 unsigned long seq;
1242 1245
1243 memset((char *)&val, 0, sizeof(struct sysinfo)); 1246 memset((char *)&val, 0, sizeof(struct sysinfo));
1244 1247
1245 do { 1248 do {
1246 struct timespec tp; 1249 struct timespec tp;
1247 seq = read_seqbegin(&xtime_lock); 1250 seq = read_seqbegin(&xtime_lock);
1248 1251
1249 /* 1252 /*
1250 * This is annoying. The below is the same thing 1253 * This is annoying. The below is the same thing
1251 * posix_get_clock_monotonic() does, but it wants to 1254 * posix_get_clock_monotonic() does, but it wants to
1252 * take the lock which we want to cover the loads stuff 1255 * take the lock which we want to cover the loads stuff
1253 * too. 1256 * too.
1254 */ 1257 */
1255 1258
1256 getnstimeofday(&tp); 1259 getnstimeofday(&tp);
1257 tp.tv_sec += wall_to_monotonic.tv_sec; 1260 tp.tv_sec += wall_to_monotonic.tv_sec;
1258 tp.tv_nsec += wall_to_monotonic.tv_nsec; 1261 tp.tv_nsec += wall_to_monotonic.tv_nsec;
1259 if (tp.tv_nsec - NSEC_PER_SEC >= 0) { 1262 if (tp.tv_nsec - NSEC_PER_SEC >= 0) {
1260 tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC; 1263 tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC;
1261 tp.tv_sec++; 1264 tp.tv_sec++;
1262 } 1265 }
1263 val.uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0); 1266 val.uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
1264 1267
1265 val.loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT); 1268 val.loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
1266 val.loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT); 1269 val.loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
1267 val.loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT); 1270 val.loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
1268 1271
1269 val.procs = nr_threads; 1272 val.procs = nr_threads;
1270 } while (read_seqretry(&xtime_lock, seq)); 1273 } while (read_seqretry(&xtime_lock, seq));
1271 1274
1272 si_meminfo(&val); 1275 si_meminfo(&val);
1273 si_swapinfo(&val); 1276 si_swapinfo(&val);
1274 1277
1275 /* 1278 /*
1276 * If the sum of all the available memory (i.e. ram + swap) 1279 * If the sum of all the available memory (i.e. ram + swap)
1277 * is less than can be stored in a 32 bit unsigned long then 1280 * is less than can be stored in a 32 bit unsigned long then
1278 * we can be binary compatible with 2.2.x kernels. If not, 1281 * we can be binary compatible with 2.2.x kernels. If not,
1279 * well, in that case 2.2.x was broken anyways... 1282 * well, in that case 2.2.x was broken anyways...
1280 * 1283 *
1281 * -Erik Andersen <andersee@debian.org> 1284 * -Erik Andersen <andersee@debian.org>
1282 */ 1285 */
1283 1286
1284 mem_total = val.totalram + val.totalswap; 1287 mem_total = val.totalram + val.totalswap;
1285 if (mem_total < val.totalram || mem_total < val.totalswap) 1288 if (mem_total < val.totalram || mem_total < val.totalswap)
1286 goto out; 1289 goto out;
1287 bitcount = 0; 1290 bitcount = 0;
1288 mem_unit = val.mem_unit; 1291 mem_unit = val.mem_unit;
1289 while (mem_unit > 1) { 1292 while (mem_unit > 1) {
1290 bitcount++; 1293 bitcount++;
1291 mem_unit >>= 1; 1294 mem_unit >>= 1;
1292 sav_total = mem_total; 1295 sav_total = mem_total;
1293 mem_total <<= 1; 1296 mem_total <<= 1;
1294 if (mem_total < sav_total) 1297 if (mem_total < sav_total)
1295 goto out; 1298 goto out;
1296 } 1299 }
1297 1300
1298 /* 1301 /*
1299 * If mem_total did not overflow, multiply all memory values by 1302 * If mem_total did not overflow, multiply all memory values by
1300 * val.mem_unit and set it to 1. This leaves things compatible 1303 * val.mem_unit and set it to 1. This leaves things compatible
1301 * with 2.2.x, and also retains compatibility with earlier 2.4.x 1304 * with 2.2.x, and also retains compatibility with earlier 2.4.x
1302 * kernels... 1305 * kernels...
1303 */ 1306 */
1304 1307
1305 val.mem_unit = 1; 1308 val.mem_unit = 1;
1306 val.totalram <<= bitcount; 1309 val.totalram <<= bitcount;
1307 val.freeram <<= bitcount; 1310 val.freeram <<= bitcount;
1308 val.sharedram <<= bitcount; 1311 val.sharedram <<= bitcount;
1309 val.bufferram <<= bitcount; 1312 val.bufferram <<= bitcount;
1310 val.totalswap <<= bitcount; 1313 val.totalswap <<= bitcount;
1311 val.freeswap <<= bitcount; 1314 val.freeswap <<= bitcount;
1312 val.totalhigh <<= bitcount; 1315 val.totalhigh <<= bitcount;
1313 val.freehigh <<= bitcount; 1316 val.freehigh <<= bitcount;
1314 1317
1315 out: 1318 out:
1316 if (copy_to_user(info, &val, sizeof(struct sysinfo))) 1319 if (copy_to_user(info, &val, sizeof(struct sysinfo)))
1317 return -EFAULT; 1320 return -EFAULT;
1318 1321
1319 return 0; 1322 return 0;
1320 } 1323 }
1321 1324
1322 static void __devinit init_timers_cpu(int cpu) 1325 static void __devinit init_timers_cpu(int cpu)
1323 { 1326 {
1324 int j; 1327 int j;
1325 tvec_base_t *base; 1328 tvec_base_t *base;
1326 1329
1327 base = &per_cpu(tvec_bases, cpu); 1330 base = &per_cpu(tvec_bases, cpu);
1328 spin_lock_init(&base->t_base.lock); 1331 spin_lock_init(&base->t_base.lock);
1329 for (j = 0; j < TVN_SIZE; j++) { 1332 for (j = 0; j < TVN_SIZE; j++) {
1330 INIT_LIST_HEAD(base->tv5.vec + j); 1333 INIT_LIST_HEAD(base->tv5.vec + j);
1331 INIT_LIST_HEAD(base->tv4.vec + j); 1334 INIT_LIST_HEAD(base->tv4.vec + j);
1332 INIT_LIST_HEAD(base->tv3.vec + j); 1335 INIT_LIST_HEAD(base->tv3.vec + j);
1333 INIT_LIST_HEAD(base->tv2.vec + j); 1336 INIT_LIST_HEAD(base->tv2.vec + j);
1334 } 1337 }
1335 for (j = 0; j < TVR_SIZE; j++) 1338 for (j = 0; j < TVR_SIZE; j++)
1336 INIT_LIST_HEAD(base->tv1.vec + j); 1339 INIT_LIST_HEAD(base->tv1.vec + j);
1337 1340
1338 base->timer_jiffies = jiffies; 1341 base->timer_jiffies = jiffies;
1339 } 1342 }
1340 1343
1341 #ifdef CONFIG_HOTPLUG_CPU 1344 #ifdef CONFIG_HOTPLUG_CPU
1342 static void migrate_timer_list(tvec_base_t *new_base, struct list_head *head) 1345 static void migrate_timer_list(tvec_base_t *new_base, struct list_head *head)
1343 { 1346 {
1344 struct timer_list *timer; 1347 struct timer_list *timer;
1345 1348
1346 while (!list_empty(head)) { 1349 while (!list_empty(head)) {
1347 timer = list_entry(head->next, struct timer_list, entry); 1350 timer = list_entry(head->next, struct timer_list, entry);
1348 detach_timer(timer, 0); 1351 detach_timer(timer, 0);
1349 timer->base = &new_base->t_base; 1352 timer->base = &new_base->t_base;
1350 internal_add_timer(new_base, timer); 1353 internal_add_timer(new_base, timer);
1351 } 1354 }
1352 } 1355 }
1353 1356
1354 static void __devinit migrate_timers(int cpu) 1357 static void __devinit migrate_timers(int cpu)
1355 { 1358 {
1356 tvec_base_t *old_base; 1359 tvec_base_t *old_base;
1357 tvec_base_t *new_base; 1360 tvec_base_t *new_base;
1358 int i; 1361 int i;
1359 1362
1360 BUG_ON(cpu_online(cpu)); 1363 BUG_ON(cpu_online(cpu));
1361 old_base = &per_cpu(tvec_bases, cpu); 1364 old_base = &per_cpu(tvec_bases, cpu);
1362 new_base = &get_cpu_var(tvec_bases); 1365 new_base = &get_cpu_var(tvec_bases);
1363 1366
1364 local_irq_disable(); 1367 local_irq_disable();
1365 spin_lock(&new_base->t_base.lock); 1368 spin_lock(&new_base->t_base.lock);
1366 spin_lock(&old_base->t_base.lock); 1369 spin_lock(&old_base->t_base.lock);
1367 1370
1368 if (old_base->t_base.running_timer) 1371 if (old_base->t_base.running_timer)
1369 BUG(); 1372 BUG();
1370 for (i = 0; i < TVR_SIZE; i++) 1373 for (i = 0; i < TVR_SIZE; i++)
1371 migrate_timer_list(new_base, old_base->tv1.vec + i); 1374 migrate_timer_list(new_base, old_base->tv1.vec + i);
1372 for (i = 0; i < TVN_SIZE; i++) { 1375 for (i = 0; i < TVN_SIZE; i++) {
1373 migrate_timer_list(new_base, old_base->tv2.vec + i); 1376 migrate_timer_list(new_base, old_base->tv2.vec + i);
1374 migrate_timer_list(new_base, old_base->tv3.vec + i); 1377 migrate_timer_list(new_base, old_base->tv3.vec + i);
1375 migrate_timer_list(new_base, old_base->tv4.vec + i); 1378 migrate_timer_list(new_base, old_base->tv4.vec + i);
1376 migrate_timer_list(new_base, old_base->tv5.vec + i); 1379 migrate_timer_list(new_base, old_base->tv5.vec + i);
1377 } 1380 }
1378 1381
1379 spin_unlock(&old_base->t_base.lock); 1382 spin_unlock(&old_base->t_base.lock);
1380 spin_unlock(&new_base->t_base.lock); 1383 spin_unlock(&new_base->t_base.lock);
1381 local_irq_enable(); 1384 local_irq_enable();
1382 put_cpu_var(tvec_bases); 1385 put_cpu_var(tvec_bases);
1383 } 1386 }
1384 #endif /* CONFIG_HOTPLUG_CPU */ 1387 #endif /* CONFIG_HOTPLUG_CPU */
1385 1388
1386 static int __devinit timer_cpu_notify(struct notifier_block *self, 1389 static int __devinit timer_cpu_notify(struct notifier_block *self,
1387 unsigned long action, void *hcpu) 1390 unsigned long action, void *hcpu)
1388 { 1391 {
1389 long cpu = (long)hcpu; 1392 long cpu = (long)hcpu;
1390 switch(action) { 1393 switch(action) {
1391 case CPU_UP_PREPARE: 1394 case CPU_UP_PREPARE:
1392 init_timers_cpu(cpu); 1395 init_timers_cpu(cpu);
1393 break; 1396 break;
1394 #ifdef CONFIG_HOTPLUG_CPU 1397 #ifdef CONFIG_HOTPLUG_CPU
1395 case CPU_DEAD: 1398 case CPU_DEAD:
1396 migrate_timers(cpu); 1399 migrate_timers(cpu);
1397 break; 1400 break;
1398 #endif 1401 #endif
1399 default: 1402 default:
1400 break; 1403 break;
1401 } 1404 }
1402 return NOTIFY_OK; 1405 return NOTIFY_OK;
1403 } 1406 }
1404 1407
1405 static struct notifier_block __devinitdata timers_nb = { 1408 static struct notifier_block __devinitdata timers_nb = {
1406 .notifier_call = timer_cpu_notify, 1409 .notifier_call = timer_cpu_notify,
1407 }; 1410 };
1408 1411
1409 1412
1410 void __init init_timers(void) 1413 void __init init_timers(void)
1411 { 1414 {
1412 timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE, 1415 timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
1413 (void *)(long)smp_processor_id()); 1416 (void *)(long)smp_processor_id());
1414 register_cpu_notifier(&timers_nb); 1417 register_cpu_notifier(&timers_nb);
1415 open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL); 1418 open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL);
1416 } 1419 }
1417 1420
1418 #ifdef CONFIG_TIME_INTERPOLATION 1421 #ifdef CONFIG_TIME_INTERPOLATION
1419 1422
1420 struct time_interpolator *time_interpolator; 1423 struct time_interpolator *time_interpolator;
1421 static struct time_interpolator *time_interpolator_list; 1424 static struct time_interpolator *time_interpolator_list;
1422 static DEFINE_SPINLOCK(time_interpolator_lock); 1425 static DEFINE_SPINLOCK(time_interpolator_lock);
1423 1426
1424 static inline u64 time_interpolator_get_cycles(unsigned int src) 1427 static inline u64 time_interpolator_get_cycles(unsigned int src)
1425 { 1428 {
1426 unsigned long (*x)(void); 1429 unsigned long (*x)(void);
1427 1430
1428 switch (src) 1431 switch (src)
1429 { 1432 {
1430 case TIME_SOURCE_FUNCTION: 1433 case TIME_SOURCE_FUNCTION:
1431 x = time_interpolator->addr; 1434 x = time_interpolator->addr;
1432 return x(); 1435 return x();
1433 1436
1434 case TIME_SOURCE_MMIO64 : 1437 case TIME_SOURCE_MMIO64 :
1435 return readq((void __iomem *) time_interpolator->addr); 1438 return readq((void __iomem *) time_interpolator->addr);
1436 1439
1437 case TIME_SOURCE_MMIO32 : 1440 case TIME_SOURCE_MMIO32 :
1438 return readl((void __iomem *) time_interpolator->addr); 1441 return readl((void __iomem *) time_interpolator->addr);
1439 1442
1440 default: return get_cycles(); 1443 default: return get_cycles();
1441 } 1444 }
1442 } 1445 }
1443 1446
1444 static inline u64 time_interpolator_get_counter(int writelock) 1447 static inline u64 time_interpolator_get_counter(int writelock)
1445 { 1448 {
1446 unsigned int src = time_interpolator->source; 1449 unsigned int src = time_interpolator->source;
1447 1450
1448 if (time_interpolator->jitter) 1451 if (time_interpolator->jitter)
1449 { 1452 {
1450 u64 lcycle; 1453 u64 lcycle;
1451 u64 now; 1454 u64 now;
1452 1455
1453 do { 1456 do {
1454 lcycle = time_interpolator->last_cycle; 1457 lcycle = time_interpolator->last_cycle;
1455 now = time_interpolator_get_cycles(src); 1458 now = time_interpolator_get_cycles(src);
1456 if (lcycle && time_after(lcycle, now)) 1459 if (lcycle && time_after(lcycle, now))
1457 return lcycle; 1460 return lcycle;
1458 1461
1459 /* When holding the xtime write lock, there's no need 1462 /* When holding the xtime write lock, there's no need
1460 * to add the overhead of the cmpxchg. Readers are 1463 * to add the overhead of the cmpxchg. Readers are
1461 * force to retry until the write lock is released. 1464 * force to retry until the write lock is released.
1462 */ 1465 */
1463 if (writelock) { 1466 if (writelock) {
1464 time_interpolator->last_cycle = now; 1467 time_interpolator->last_cycle = now;
1465 return now; 1468 return now;
1466 } 1469 }
1467 /* Keep track of the last timer value returned. The use of cmpxchg here 1470 /* Keep track of the last timer value returned. The use of cmpxchg here
1468 * will cause contention in an SMP environment. 1471 * will cause contention in an SMP environment.
1469 */ 1472 */
1470 } while (unlikely(cmpxchg(&time_interpolator->last_cycle, lcycle, now) != lcycle)); 1473 } while (unlikely(cmpxchg(&time_interpolator->last_cycle, lcycle, now) != lcycle));
1471 return now; 1474 return now;
1472 } 1475 }
1473 else 1476 else
1474 return time_interpolator_get_cycles(src); 1477 return time_interpolator_get_cycles(src);
1475 } 1478 }
1476 1479
1477 void time_interpolator_reset(void) 1480 void time_interpolator_reset(void)
1478 { 1481 {
1479 time_interpolator->offset = 0; 1482 time_interpolator->offset = 0;
1480 time_interpolator->last_counter = time_interpolator_get_counter(1); 1483 time_interpolator->last_counter = time_interpolator_get_counter(1);
1481 } 1484 }
1482 1485
1483 #define GET_TI_NSECS(count,i) (((((count) - i->last_counter) & (i)->mask) * (i)->nsec_per_cyc) >> (i)->shift) 1486 #define GET_TI_NSECS(count,i) (((((count) - i->last_counter) & (i)->mask) * (i)->nsec_per_cyc) >> (i)->shift)
1484 1487
1485 unsigned long time_interpolator_get_offset(void) 1488 unsigned long time_interpolator_get_offset(void)
1486 { 1489 {
1487 /* If we do not have a time interpolator set up then just return zero */ 1490 /* If we do not have a time interpolator set up then just return zero */
1488 if (!time_interpolator) 1491 if (!time_interpolator)
1489 return 0; 1492 return 0;
1490 1493
1491 return time_interpolator->offset + 1494 return time_interpolator->offset +
1492 GET_TI_NSECS(time_interpolator_get_counter(0), time_interpolator); 1495 GET_TI_NSECS(time_interpolator_get_counter(0), time_interpolator);
1493 } 1496 }
1494 1497
1495 #define INTERPOLATOR_ADJUST 65536 1498 #define INTERPOLATOR_ADJUST 65536
1496 #define INTERPOLATOR_MAX_SKIP 10*INTERPOLATOR_ADJUST 1499 #define INTERPOLATOR_MAX_SKIP 10*INTERPOLATOR_ADJUST
1497 1500
1498 static void time_interpolator_update(long delta_nsec) 1501 static void time_interpolator_update(long delta_nsec)
1499 { 1502 {
1500 u64 counter; 1503 u64 counter;
1501 unsigned long offset; 1504 unsigned long offset;
1502 1505
1503 /* If there is no time interpolator set up then do nothing */ 1506 /* If there is no time interpolator set up then do nothing */
1504 if (!time_interpolator) 1507 if (!time_interpolator)
1505 return; 1508 return;
1506 1509
1507 /* The interpolator compensates for late ticks by accumulating 1510 /* The interpolator compensates for late ticks by accumulating
1508 * the late time in time_interpolator->offset. A tick earlier than 1511 * the late time in time_interpolator->offset. A tick earlier than
1509 * expected will lead to a reset of the offset and a corresponding 1512 * expected will lead to a reset of the offset and a corresponding
1510 * jump of the clock forward. Again this only works if the 1513 * jump of the clock forward. Again this only works if the
1511 * interpolator clock is running slightly slower than the regular clock 1514 * interpolator clock is running slightly slower than the regular clock
1512 * and the tuning logic insures that. 1515 * and the tuning logic insures that.
1513 */ 1516 */
1514 1517
1515 counter = time_interpolator_get_counter(1); 1518 counter = time_interpolator_get_counter(1);
1516 offset = time_interpolator->offset + GET_TI_NSECS(counter, time_interpolator); 1519 offset = time_interpolator->offset + GET_TI_NSECS(counter, time_interpolator);
1517 1520
1518 if (delta_nsec < 0 || (unsigned long) delta_nsec < offset) 1521 if (delta_nsec < 0 || (unsigned long) delta_nsec < offset)
1519 time_interpolator->offset = offset - delta_nsec; 1522 time_interpolator->offset = offset - delta_nsec;
1520 else { 1523 else {
1521 time_interpolator->skips++; 1524 time_interpolator->skips++;
1522 time_interpolator->ns_skipped += delta_nsec - offset; 1525 time_interpolator->ns_skipped += delta_nsec - offset;
1523 time_interpolator->offset = 0; 1526 time_interpolator->offset = 0;
1524 } 1527 }
1525 time_interpolator->last_counter = counter; 1528 time_interpolator->last_counter = counter;
1526 1529
1527 /* Tuning logic for time interpolator invoked every minute or so. 1530 /* Tuning logic for time interpolator invoked every minute or so.
1528 * Decrease interpolator clock speed if no skips occurred and an offset is carried. 1531 * Decrease interpolator clock speed if no skips occurred and an offset is carried.
1529 * Increase interpolator clock speed if we skip too much time. 1532 * Increase interpolator clock speed if we skip too much time.
1530 */ 1533 */
1531 if (jiffies % INTERPOLATOR_ADJUST == 0) 1534 if (jiffies % INTERPOLATOR_ADJUST == 0)
1532 { 1535 {
1533 if (time_interpolator->skips == 0 && time_interpolator->offset > TICK_NSEC) 1536 if (time_interpolator->skips == 0 && time_interpolator->offset > TICK_NSEC)
1534 time_interpolator->nsec_per_cyc--; 1537 time_interpolator->nsec_per_cyc--;
1535 if (time_interpolator->ns_skipped > INTERPOLATOR_MAX_SKIP && time_interpolator->offset == 0) 1538 if (time_interpolator->ns_skipped > INTERPOLATOR_MAX_SKIP && time_interpolator->offset == 0)
1536 time_interpolator->nsec_per_cyc++; 1539 time_interpolator->nsec_per_cyc++;
1537 time_interpolator->skips = 0; 1540 time_interpolator->skips = 0;
1538 time_interpolator->ns_skipped = 0; 1541 time_interpolator->ns_skipped = 0;
1539 } 1542 }
1540 } 1543 }
1541 1544
1542 static inline int 1545 static inline int
1543 is_better_time_interpolator(struct time_interpolator *new) 1546 is_better_time_interpolator(struct time_interpolator *new)
1544 { 1547 {
1545 if (!time_interpolator) 1548 if (!time_interpolator)
1546 return 1; 1549 return 1;
1547 return new->frequency > 2*time_interpolator->frequency || 1550 return new->frequency > 2*time_interpolator->frequency ||
1548 (unsigned long)new->drift < (unsigned long)time_interpolator->drift; 1551 (unsigned long)new->drift < (unsigned long)time_interpolator->drift;
1549 } 1552 }
1550 1553
1551 void 1554 void
1552 register_time_interpolator(struct time_interpolator *ti) 1555 register_time_interpolator(struct time_interpolator *ti)
1553 { 1556 {
1554 unsigned long flags; 1557 unsigned long flags;
1555 1558
1556 /* Sanity check */ 1559 /* Sanity check */
1557 if (ti->frequency == 0 || ti->mask == 0) 1560 if (ti->frequency == 0 || ti->mask == 0)
1558 BUG(); 1561 BUG();
1559 1562
1560 ti->nsec_per_cyc = ((u64)NSEC_PER_SEC << ti->shift) / ti->frequency; 1563 ti->nsec_per_cyc = ((u64)NSEC_PER_SEC << ti->shift) / ti->frequency;
1561 spin_lock(&time_interpolator_lock); 1564 spin_lock(&time_interpolator_lock);
1562 write_seqlock_irqsave(&xtime_lock, flags); 1565 write_seqlock_irqsave(&xtime_lock, flags);
1563 if (is_better_time_interpolator(ti)) { 1566 if (is_better_time_interpolator(ti)) {
1564 time_interpolator = ti; 1567 time_interpolator = ti;
1565 time_interpolator_reset(); 1568 time_interpolator_reset();
1566 } 1569 }
1567 write_sequnlock_irqrestore(&xtime_lock, flags); 1570 write_sequnlock_irqrestore(&xtime_lock, flags);
1568 1571
1569 ti->next = time_interpolator_list; 1572 ti->next = time_interpolator_list;
1570 time_interpolator_list = ti; 1573 time_interpolator_list = ti;
1571 spin_unlock(&time_interpolator_lock); 1574 spin_unlock(&time_interpolator_lock);
1572 } 1575 }
1573 1576
1574 void 1577 void
1575 unregister_time_interpolator(struct time_interpolator *ti) 1578 unregister_time_interpolator(struct time_interpolator *ti)
1576 { 1579 {
1577 struct time_interpolator *curr, **prev; 1580 struct time_interpolator *curr, **prev;
1578 unsigned long flags; 1581 unsigned long flags;
1579 1582
1580 spin_lock(&time_interpolator_lock); 1583 spin_lock(&time_interpolator_lock);
1581 prev = &time_interpolator_list; 1584 prev = &time_interpolator_list;
1582 for (curr = *prev; curr; curr = curr->next) { 1585 for (curr = *prev; curr; curr = curr->next) {
1583 if (curr == ti) { 1586 if (curr == ti) {
1584 *prev = curr->next; 1587 *prev = curr->next;
1585 break; 1588 break;
1586 } 1589 }
1587 prev = &curr->next; 1590 prev = &curr->next;
1588 } 1591 }
1589 1592
1590 write_seqlock_irqsave(&xtime_lock, flags); 1593 write_seqlock_irqsave(&xtime_lock, flags);
1591 if (ti == time_interpolator) { 1594 if (ti == time_interpolator) {
1592 /* we lost the best time-interpolator: */ 1595 /* we lost the best time-interpolator: */
1593 time_interpolator = NULL; 1596 time_interpolator = NULL;
1594 /* find the next-best interpolator */ 1597 /* find the next-best interpolator */
1595 for (curr = time_interpolator_list; curr; curr = curr->next) 1598 for (curr = time_interpolator_list; curr; curr = curr->next)
1596 if (is_better_time_interpolator(curr)) 1599 if (is_better_time_interpolator(curr))
1597 time_interpolator = curr; 1600 time_interpolator = curr;
1598 time_interpolator_reset(); 1601 time_interpolator_reset();
1599 } 1602 }
1600 write_sequnlock_irqrestore(&xtime_lock, flags); 1603 write_sequnlock_irqrestore(&xtime_lock, flags);
1601 spin_unlock(&time_interpolator_lock); 1604 spin_unlock(&time_interpolator_lock);
1602 } 1605 }
1603 #endif /* CONFIG_TIME_INTERPOLATION */ 1606 #endif /* CONFIG_TIME_INTERPOLATION */
1604 1607
1605 /** 1608 /**
1606 * msleep - sleep safely even with waitqueue interruptions 1609 * msleep - sleep safely even with waitqueue interruptions
1607 * @msecs: Time in milliseconds to sleep for 1610 * @msecs: Time in milliseconds to sleep for
1608 */ 1611 */
1609 void msleep(unsigned int msecs) 1612 void msleep(unsigned int msecs)
1610 { 1613 {
1611 unsigned long timeout = msecs_to_jiffies(msecs) + 1; 1614 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1612 1615
1613 while (timeout) 1616 while (timeout)
1614 timeout = schedule_timeout_uninterruptible(timeout); 1617 timeout = schedule_timeout_uninterruptible(timeout);
1615 } 1618 }
1616 1619
1617 EXPORT_SYMBOL(msleep); 1620 EXPORT_SYMBOL(msleep);
1618 1621
1619 /** 1622 /**
1620 * msleep_interruptible - sleep waiting for signals 1623 * msleep_interruptible - sleep waiting for signals
1621 * @msecs: Time in milliseconds to sleep for 1624 * @msecs: Time in milliseconds to sleep for
1622 */ 1625 */
1623 unsigned long msleep_interruptible(unsigned int msecs) 1626 unsigned long msleep_interruptible(unsigned int msecs)
1624 { 1627 {
1625 unsigned long timeout = msecs_to_jiffies(msecs) + 1; 1628 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1626 1629
1627 while (timeout && !signal_pending(current)) 1630 while (timeout && !signal_pending(current))
1628 timeout = schedule_timeout_interruptible(timeout); 1631 timeout = schedule_timeout_interruptible(timeout);
1629 return jiffies_to_msecs(timeout); 1632 return jiffies_to_msecs(timeout);
1630 } 1633 }
1631 1634
1632 EXPORT_SYMBOL(msleep_interruptible); 1635 EXPORT_SYMBOL(msleep_interruptible);