]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/kernel/timer.c | |
3 | * | |
4 | * Kernel internal timers, kernel timekeeping, basic process system calls | |
5 | * | |
6 | * Copyright (C) 1991, 1992 Linus Torvalds | |
7 | * | |
8 | * 1997-01-28 Modified by Finn Arne Gangstad to make timers scale better. | |
9 | * | |
10 | * 1997-09-10 Updated NTP code according to technical memorandum Jan '96 | |
11 | * "A Kernel Model for Precision Timekeeping" by Dave Mills | |
12 | * 1998-12-24 Fixed a xtime SMP race (we need the xtime_lock rw spinlock to | |
13 | * serialize accesses to xtime/lost_ticks). | |
14 | * Copyright (C) 1998 Andrea Arcangeli | |
15 | * 1999-03-10 Improved NTP compatibility by Ulrich Windl | |
16 | * 2002-05-31 Move sys_sysinfo here and make its locking sane, Robert Love | |
17 | * 2000-10-05 Implemented scalable SMP per-CPU timer handling. | |
18 | * Copyright (C) 2000, 2001, 2002 Ingo Molnar | |
19 | * Designed by David S. Miller, Alexey Kuznetsov and Ingo Molnar | |
20 | */ | |
21 | ||
22 | #include <linux/kernel_stat.h> | |
23 | #include <linux/module.h> | |
24 | #include <linux/interrupt.h> | |
25 | #include <linux/percpu.h> | |
26 | #include <linux/init.h> | |
27 | #include <linux/mm.h> | |
28 | #include <linux/swap.h> | |
29 | #include <linux/notifier.h> | |
30 | #include <linux/thread_info.h> | |
31 | #include <linux/time.h> | |
32 | #include <linux/jiffies.h> | |
33 | #include <linux/posix-timers.h> | |
34 | #include <linux/cpu.h> | |
35 | #include <linux/syscalls.h> | |
97a41e26 | 36 | #include <linux/delay.h> |
1da177e4 LT |
37 | |
38 | #include <asm/uaccess.h> | |
39 | #include <asm/unistd.h> | |
40 | #include <asm/div64.h> | |
41 | #include <asm/timex.h> | |
42 | #include <asm/io.h> | |
43 | ||
ecea8d19 TG |
44 | u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES; |
45 | ||
46 | EXPORT_SYMBOL(jiffies_64); | |
47 | ||
1da177e4 LT |
48 | /* |
49 | * per-CPU timer vector definitions: | |
50 | */ | |
1da177e4 LT |
51 | #define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6) |
52 | #define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8) | |
53 | #define TVN_SIZE (1 << TVN_BITS) | |
54 | #define TVR_SIZE (1 << TVR_BITS) | |
55 | #define TVN_MASK (TVN_SIZE - 1) | |
56 | #define TVR_MASK (TVR_SIZE - 1) | |
57 | ||
58 | typedef struct tvec_s { | |
59 | struct list_head vec[TVN_SIZE]; | |
60 | } tvec_t; | |
61 | ||
62 | typedef struct tvec_root_s { | |
63 | struct list_head vec[TVR_SIZE]; | |
64 | } tvec_root_t; | |
65 | ||
66 | struct tvec_t_base_s { | |
3691c519 ON |
67 | spinlock_t lock; |
68 | struct timer_list *running_timer; | |
1da177e4 | 69 | unsigned long timer_jiffies; |
1da177e4 LT |
70 | tvec_root_t tv1; |
71 | tvec_t tv2; | |
72 | tvec_t tv3; | |
73 | tvec_t tv4; | |
74 | tvec_t tv5; | |
75 | } ____cacheline_aligned_in_smp; | |
76 | ||
77 | typedef struct tvec_t_base_s tvec_base_t; | |
ba6edfcd | 78 | |
3691c519 ON |
79 | tvec_base_t boot_tvec_bases; |
80 | EXPORT_SYMBOL(boot_tvec_bases); | |
51d8c5ed | 81 | static DEFINE_PER_CPU(tvec_base_t *, tvec_bases) = &boot_tvec_bases; |
1da177e4 | 82 | |
4c36a5de AV |
83 | /** |
84 | * __round_jiffies - function to round jiffies to a full second | |
85 | * @j: the time in (absolute) jiffies that should be rounded | |
86 | * @cpu: the processor number on which the timeout will happen | |
87 | * | |
72fd4a35 | 88 | * __round_jiffies() rounds an absolute time in the future (in jiffies) |
4c36a5de AV |
89 | * up or down to (approximately) full seconds. This is useful for timers |
90 | * for which the exact time they fire does not matter too much, as long as | |
91 | * they fire approximately every X seconds. | |
92 | * | |
93 | * By rounding these timers to whole seconds, all such timers will fire | |
94 | * at the same time, rather than at various times spread out. The goal | |
95 | * of this is to have the CPU wake up less, which saves power. | |
96 | * | |
97 | * The exact rounding is skewed for each processor to avoid all | |
98 | * processors firing at the exact same time, which could lead | |
99 | * to lock contention or spurious cache line bouncing. | |
100 | * | |
72fd4a35 | 101 | * The return value is the rounded version of the @j parameter. |
4c36a5de AV |
102 | */ |
103 | unsigned long __round_jiffies(unsigned long j, int cpu) | |
104 | { | |
105 | int rem; | |
106 | unsigned long original = j; | |
107 | ||
108 | /* | |
109 | * We don't want all cpus firing their timers at once hitting the | |
110 | * same lock or cachelines, so we skew each extra cpu with an extra | |
111 | * 3 jiffies. This 3 jiffies came originally from the mm/ code which | |
112 | * already did this. | |
113 | * The skew is done by adding 3*cpunr, then round, then subtract this | |
114 | * extra offset again. | |
115 | */ | |
116 | j += cpu * 3; | |
117 | ||
118 | rem = j % HZ; | |
119 | ||
120 | /* | |
121 | * If the target jiffie is just after a whole second (which can happen | |
122 | * due to delays of the timer irq, long irq off times etc etc) then | |
123 | * we should round down to the whole second, not up. Use 1/4th second | |
124 | * as cutoff for this rounding as an extreme upper bound for this. | |
125 | */ | |
126 | if (rem < HZ/4) /* round down */ | |
127 | j = j - rem; | |
128 | else /* round up */ | |
129 | j = j - rem + HZ; | |
130 | ||
131 | /* now that we have rounded, subtract the extra skew again */ | |
132 | j -= cpu * 3; | |
133 | ||
134 | if (j <= jiffies) /* rounding ate our timeout entirely; */ | |
135 | return original; | |
136 | return j; | |
137 | } | |
138 | EXPORT_SYMBOL_GPL(__round_jiffies); | |
139 | ||
140 | /** | |
141 | * __round_jiffies_relative - function to round jiffies to a full second | |
142 | * @j: the time in (relative) jiffies that should be rounded | |
143 | * @cpu: the processor number on which the timeout will happen | |
144 | * | |
72fd4a35 | 145 | * __round_jiffies_relative() rounds a time delta in the future (in jiffies) |
4c36a5de AV |
146 | * up or down to (approximately) full seconds. This is useful for timers |
147 | * for which the exact time they fire does not matter too much, as long as | |
148 | * they fire approximately every X seconds. | |
149 | * | |
150 | * By rounding these timers to whole seconds, all such timers will fire | |
151 | * at the same time, rather than at various times spread out. The goal | |
152 | * of this is to have the CPU wake up less, which saves power. | |
153 | * | |
154 | * The exact rounding is skewed for each processor to avoid all | |
155 | * processors firing at the exact same time, which could lead | |
156 | * to lock contention or spurious cache line bouncing. | |
157 | * | |
72fd4a35 | 158 | * The return value is the rounded version of the @j parameter. |
4c36a5de AV |
159 | */ |
160 | unsigned long __round_jiffies_relative(unsigned long j, int cpu) | |
161 | { | |
162 | /* | |
163 | * In theory the following code can skip a jiffy in case jiffies | |
164 | * increments right between the addition and the later subtraction. | |
165 | * However since the entire point of this function is to use approximate | |
166 | * timeouts, it's entirely ok to not handle that. | |
167 | */ | |
168 | return __round_jiffies(j + jiffies, cpu) - jiffies; | |
169 | } | |
170 | EXPORT_SYMBOL_GPL(__round_jiffies_relative); | |
171 | ||
172 | /** | |
173 | * round_jiffies - function to round jiffies to a full second | |
174 | * @j: the time in (absolute) jiffies that should be rounded | |
175 | * | |
72fd4a35 | 176 | * round_jiffies() rounds an absolute time in the future (in jiffies) |
4c36a5de AV |
177 | * up or down to (approximately) full seconds. This is useful for timers |
178 | * for which the exact time they fire does not matter too much, as long as | |
179 | * they fire approximately every X seconds. | |
180 | * | |
181 | * By rounding these timers to whole seconds, all such timers will fire | |
182 | * at the same time, rather than at various times spread out. The goal | |
183 | * of this is to have the CPU wake up less, which saves power. | |
184 | * | |
72fd4a35 | 185 | * The return value is the rounded version of the @j parameter. |
4c36a5de AV |
186 | */ |
187 | unsigned long round_jiffies(unsigned long j) | |
188 | { | |
189 | return __round_jiffies(j, raw_smp_processor_id()); | |
190 | } | |
191 | EXPORT_SYMBOL_GPL(round_jiffies); | |
192 | ||
193 | /** | |
194 | * round_jiffies_relative - function to round jiffies to a full second | |
195 | * @j: the time in (relative) jiffies that should be rounded | |
196 | * | |
72fd4a35 | 197 | * round_jiffies_relative() rounds a time delta in the future (in jiffies) |
4c36a5de AV |
198 | * up or down to (approximately) full seconds. This is useful for timers |
199 | * for which the exact time they fire does not matter too much, as long as | |
200 | * they fire approximately every X seconds. | |
201 | * | |
202 | * By rounding these timers to whole seconds, all such timers will fire | |
203 | * at the same time, rather than at various times spread out. The goal | |
204 | * of this is to have the CPU wake up less, which saves power. | |
205 | * | |
72fd4a35 | 206 | * The return value is the rounded version of the @j parameter. |
4c36a5de AV |
207 | */ |
208 | unsigned long round_jiffies_relative(unsigned long j) | |
209 | { | |
210 | return __round_jiffies_relative(j, raw_smp_processor_id()); | |
211 | } | |
212 | EXPORT_SYMBOL_GPL(round_jiffies_relative); | |
213 | ||
214 | ||
1da177e4 LT |
215 | static inline void set_running_timer(tvec_base_t *base, |
216 | struct timer_list *timer) | |
217 | { | |
218 | #ifdef CONFIG_SMP | |
3691c519 | 219 | base->running_timer = timer; |
1da177e4 LT |
220 | #endif |
221 | } | |
222 | ||
1da177e4 LT |
223 | static void internal_add_timer(tvec_base_t *base, struct timer_list *timer) |
224 | { | |
225 | unsigned long expires = timer->expires; | |
226 | unsigned long idx = expires - base->timer_jiffies; | |
227 | struct list_head *vec; | |
228 | ||
229 | if (idx < TVR_SIZE) { | |
230 | int i = expires & TVR_MASK; | |
231 | vec = base->tv1.vec + i; | |
232 | } else if (idx < 1 << (TVR_BITS + TVN_BITS)) { | |
233 | int i = (expires >> TVR_BITS) & TVN_MASK; | |
234 | vec = base->tv2.vec + i; | |
235 | } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) { | |
236 | int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK; | |
237 | vec = base->tv3.vec + i; | |
238 | } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) { | |
239 | int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK; | |
240 | vec = base->tv4.vec + i; | |
241 | } else if ((signed long) idx < 0) { | |
242 | /* | |
243 | * Can happen if you add a timer with expires == jiffies, | |
244 | * or you set a timer to go off in the past | |
245 | */ | |
246 | vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK); | |
247 | } else { | |
248 | int i; | |
249 | /* If the timeout is larger than 0xffffffff on 64-bit | |
250 | * architectures then we use the maximum timeout: | |
251 | */ | |
252 | if (idx > 0xffffffffUL) { | |
253 | idx = 0xffffffffUL; | |
254 | expires = idx + base->timer_jiffies; | |
255 | } | |
256 | i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK; | |
257 | vec = base->tv5.vec + i; | |
258 | } | |
259 | /* | |
260 | * Timers are FIFO: | |
261 | */ | |
262 | list_add_tail(&timer->entry, vec); | |
263 | } | |
264 | ||
2aae4a10 | 265 | /** |
55c888d6 ON |
266 | * init_timer - initialize a timer. |
267 | * @timer: the timer to be initialized | |
268 | * | |
269 | * init_timer() must be done to a timer prior calling *any* of the | |
270 | * other timer functions. | |
271 | */ | |
272 | void fastcall init_timer(struct timer_list *timer) | |
273 | { | |
274 | timer->entry.next = NULL; | |
bfe5d834 | 275 | timer->base = __raw_get_cpu_var(tvec_bases); |
55c888d6 ON |
276 | } |
277 | EXPORT_SYMBOL(init_timer); | |
278 | ||
279 | static inline void detach_timer(struct timer_list *timer, | |
280 | int clear_pending) | |
281 | { | |
282 | struct list_head *entry = &timer->entry; | |
283 | ||
284 | __list_del(entry->prev, entry->next); | |
285 | if (clear_pending) | |
286 | entry->next = NULL; | |
287 | entry->prev = LIST_POISON2; | |
288 | } | |
289 | ||
290 | /* | |
3691c519 | 291 | * We are using hashed locking: holding per_cpu(tvec_bases).lock |
55c888d6 ON |
292 | * means that all timers which are tied to this base via timer->base are |
293 | * locked, and the base itself is locked too. | |
294 | * | |
295 | * So __run_timers/migrate_timers can safely modify all timers which could | |
296 | * be found on ->tvX lists. | |
297 | * | |
298 | * When the timer's base is locked, and the timer removed from list, it is | |
299 | * possible to set timer->base = NULL and drop the lock: the timer remains | |
300 | * locked. | |
301 | */ | |
3691c519 | 302 | static tvec_base_t *lock_timer_base(struct timer_list *timer, |
55c888d6 | 303 | unsigned long *flags) |
89e7e374 | 304 | __acquires(timer->base->lock) |
55c888d6 | 305 | { |
3691c519 | 306 | tvec_base_t *base; |
55c888d6 ON |
307 | |
308 | for (;;) { | |
309 | base = timer->base; | |
310 | if (likely(base != NULL)) { | |
311 | spin_lock_irqsave(&base->lock, *flags); | |
312 | if (likely(base == timer->base)) | |
313 | return base; | |
314 | /* The timer has migrated to another CPU */ | |
315 | spin_unlock_irqrestore(&base->lock, *flags); | |
316 | } | |
317 | cpu_relax(); | |
318 | } | |
319 | } | |
320 | ||
1da177e4 LT |
321 | int __mod_timer(struct timer_list *timer, unsigned long expires) |
322 | { | |
3691c519 | 323 | tvec_base_t *base, *new_base; |
1da177e4 LT |
324 | unsigned long flags; |
325 | int ret = 0; | |
326 | ||
327 | BUG_ON(!timer->function); | |
1da177e4 | 328 | |
55c888d6 ON |
329 | base = lock_timer_base(timer, &flags); |
330 | ||
331 | if (timer_pending(timer)) { | |
332 | detach_timer(timer, 0); | |
333 | ret = 1; | |
334 | } | |
335 | ||
a4a6198b | 336 | new_base = __get_cpu_var(tvec_bases); |
1da177e4 | 337 | |
3691c519 | 338 | if (base != new_base) { |
1da177e4 | 339 | /* |
55c888d6 ON |
340 | * We are trying to schedule the timer on the local CPU. |
341 | * However we can't change timer's base while it is running, | |
342 | * otherwise del_timer_sync() can't detect that the timer's | |
343 | * handler yet has not finished. This also guarantees that | |
344 | * the timer is serialized wrt itself. | |
1da177e4 | 345 | */ |
a2c348fe | 346 | if (likely(base->running_timer != timer)) { |
55c888d6 ON |
347 | /* See the comment in lock_timer_base() */ |
348 | timer->base = NULL; | |
349 | spin_unlock(&base->lock); | |
a2c348fe ON |
350 | base = new_base; |
351 | spin_lock(&base->lock); | |
352 | timer->base = base; | |
1da177e4 LT |
353 | } |
354 | } | |
355 | ||
1da177e4 | 356 | timer->expires = expires; |
a2c348fe ON |
357 | internal_add_timer(base, timer); |
358 | spin_unlock_irqrestore(&base->lock, flags); | |
1da177e4 LT |
359 | |
360 | return ret; | |
361 | } | |
362 | ||
363 | EXPORT_SYMBOL(__mod_timer); | |
364 | ||
2aae4a10 | 365 | /** |
1da177e4 LT |
366 | * add_timer_on - start a timer on a particular CPU |
367 | * @timer: the timer to be added | |
368 | * @cpu: the CPU to start it on | |
369 | * | |
370 | * This is not very scalable on SMP. Double adds are not possible. | |
371 | */ | |
372 | void add_timer_on(struct timer_list *timer, int cpu) | |
373 | { | |
a4a6198b | 374 | tvec_base_t *base = per_cpu(tvec_bases, cpu); |
1da177e4 | 375 | unsigned long flags; |
55c888d6 | 376 | |
1da177e4 | 377 | BUG_ON(timer_pending(timer) || !timer->function); |
3691c519 ON |
378 | spin_lock_irqsave(&base->lock, flags); |
379 | timer->base = base; | |
1da177e4 | 380 | internal_add_timer(base, timer); |
3691c519 | 381 | spin_unlock_irqrestore(&base->lock, flags); |
1da177e4 LT |
382 | } |
383 | ||
384 | ||
2aae4a10 | 385 | /** |
1da177e4 LT |
386 | * mod_timer - modify a timer's timeout |
387 | * @timer: the timer to be modified | |
2aae4a10 | 388 | * @expires: new timeout in jiffies |
1da177e4 | 389 | * |
72fd4a35 | 390 | * mod_timer() is a more efficient way to update the expire field of an |
1da177e4 LT |
391 | * active timer (if the timer is inactive it will be activated) |
392 | * | |
393 | * mod_timer(timer, expires) is equivalent to: | |
394 | * | |
395 | * del_timer(timer); timer->expires = expires; add_timer(timer); | |
396 | * | |
397 | * Note that if there are multiple unserialized concurrent users of the | |
398 | * same timer, then mod_timer() is the only safe way to modify the timeout, | |
399 | * since add_timer() cannot modify an already running timer. | |
400 | * | |
401 | * The function returns whether it has modified a pending timer or not. | |
402 | * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an | |
403 | * active timer returns 1.) | |
404 | */ | |
405 | int mod_timer(struct timer_list *timer, unsigned long expires) | |
406 | { | |
407 | BUG_ON(!timer->function); | |
408 | ||
1da177e4 LT |
409 | /* |
410 | * This is a common optimization triggered by the | |
411 | * networking code - if the timer is re-modified | |
412 | * to be the same thing then just return: | |
413 | */ | |
414 | if (timer->expires == expires && timer_pending(timer)) | |
415 | return 1; | |
416 | ||
417 | return __mod_timer(timer, expires); | |
418 | } | |
419 | ||
420 | EXPORT_SYMBOL(mod_timer); | |
421 | ||
2aae4a10 | 422 | /** |
1da177e4 LT |
423 | * del_timer - deactive a timer. |
424 | * @timer: the timer to be deactivated | |
425 | * | |
426 | * del_timer() deactivates a timer - this works on both active and inactive | |
427 | * timers. | |
428 | * | |
429 | * The function returns whether it has deactivated a pending timer or not. | |
430 | * (ie. del_timer() of an inactive timer returns 0, del_timer() of an | |
431 | * active timer returns 1.) | |
432 | */ | |
433 | int del_timer(struct timer_list *timer) | |
434 | { | |
3691c519 | 435 | tvec_base_t *base; |
1da177e4 | 436 | unsigned long flags; |
55c888d6 | 437 | int ret = 0; |
1da177e4 | 438 | |
55c888d6 ON |
439 | if (timer_pending(timer)) { |
440 | base = lock_timer_base(timer, &flags); | |
441 | if (timer_pending(timer)) { | |
442 | detach_timer(timer, 1); | |
443 | ret = 1; | |
444 | } | |
1da177e4 | 445 | spin_unlock_irqrestore(&base->lock, flags); |
1da177e4 | 446 | } |
1da177e4 | 447 | |
55c888d6 | 448 | return ret; |
1da177e4 LT |
449 | } |
450 | ||
451 | EXPORT_SYMBOL(del_timer); | |
452 | ||
453 | #ifdef CONFIG_SMP | |
2aae4a10 REB |
454 | /** |
455 | * try_to_del_timer_sync - Try to deactivate a timer | |
456 | * @timer: timer do del | |
457 | * | |
fd450b73 ON |
458 | * This function tries to deactivate a timer. Upon successful (ret >= 0) |
459 | * exit the timer is not queued and the handler is not running on any CPU. | |
460 | * | |
461 | * It must not be called from interrupt contexts. | |
462 | */ | |
463 | int try_to_del_timer_sync(struct timer_list *timer) | |
464 | { | |
3691c519 | 465 | tvec_base_t *base; |
fd450b73 ON |
466 | unsigned long flags; |
467 | int ret = -1; | |
468 | ||
469 | base = lock_timer_base(timer, &flags); | |
470 | ||
471 | if (base->running_timer == timer) | |
472 | goto out; | |
473 | ||
474 | ret = 0; | |
475 | if (timer_pending(timer)) { | |
476 | detach_timer(timer, 1); | |
477 | ret = 1; | |
478 | } | |
479 | out: | |
480 | spin_unlock_irqrestore(&base->lock, flags); | |
481 | ||
482 | return ret; | |
483 | } | |
484 | ||
2aae4a10 | 485 | /** |
1da177e4 LT |
486 | * del_timer_sync - deactivate a timer and wait for the handler to finish. |
487 | * @timer: the timer to be deactivated | |
488 | * | |
489 | * This function only differs from del_timer() on SMP: besides deactivating | |
490 | * the timer it also makes sure the handler has finished executing on other | |
491 | * CPUs. | |
492 | * | |
72fd4a35 | 493 | * Synchronization rules: Callers must prevent restarting of the timer, |
1da177e4 LT |
494 | * otherwise this function is meaningless. It must not be called from |
495 | * interrupt contexts. The caller must not hold locks which would prevent | |
55c888d6 ON |
496 | * completion of the timer's handler. The timer's handler must not call |
497 | * add_timer_on(). Upon exit the timer is not queued and the handler is | |
498 | * not running on any CPU. | |
1da177e4 LT |
499 | * |
500 | * The function returns whether it has deactivated a pending timer or not. | |
1da177e4 LT |
501 | */ |
502 | int del_timer_sync(struct timer_list *timer) | |
503 | { | |
fd450b73 ON |
504 | for (;;) { |
505 | int ret = try_to_del_timer_sync(timer); | |
506 | if (ret >= 0) | |
507 | return ret; | |
a0009652 | 508 | cpu_relax(); |
fd450b73 | 509 | } |
1da177e4 | 510 | } |
1da177e4 | 511 | |
55c888d6 | 512 | EXPORT_SYMBOL(del_timer_sync); |
1da177e4 LT |
513 | #endif |
514 | ||
515 | static int cascade(tvec_base_t *base, tvec_t *tv, int index) | |
516 | { | |
517 | /* cascade all the timers from tv up one level */ | |
3439dd86 P |
518 | struct timer_list *timer, *tmp; |
519 | struct list_head tv_list; | |
520 | ||
521 | list_replace_init(tv->vec + index, &tv_list); | |
1da177e4 | 522 | |
1da177e4 | 523 | /* |
3439dd86 P |
524 | * We are removing _all_ timers from the list, so we |
525 | * don't have to detach them individually. | |
1da177e4 | 526 | */ |
3439dd86 P |
527 | list_for_each_entry_safe(timer, tmp, &tv_list, entry) { |
528 | BUG_ON(timer->base != base); | |
529 | internal_add_timer(base, timer); | |
1da177e4 | 530 | } |
1da177e4 LT |
531 | |
532 | return index; | |
533 | } | |
534 | ||
2aae4a10 REB |
535 | #define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK) |
536 | ||
537 | /** | |
1da177e4 LT |
538 | * __run_timers - run all expired timers (if any) on this CPU. |
539 | * @base: the timer vector to be processed. | |
540 | * | |
541 | * This function cascades all vectors and executes all expired timer | |
542 | * vectors. | |
543 | */ | |
1da177e4 LT |
544 | static inline void __run_timers(tvec_base_t *base) |
545 | { | |
546 | struct timer_list *timer; | |
547 | ||
3691c519 | 548 | spin_lock_irq(&base->lock); |
1da177e4 | 549 | while (time_after_eq(jiffies, base->timer_jiffies)) { |
626ab0e6 | 550 | struct list_head work_list; |
1da177e4 LT |
551 | struct list_head *head = &work_list; |
552 | int index = base->timer_jiffies & TVR_MASK; | |
626ab0e6 | 553 | |
1da177e4 LT |
554 | /* |
555 | * Cascade timers: | |
556 | */ | |
557 | if (!index && | |
558 | (!cascade(base, &base->tv2, INDEX(0))) && | |
559 | (!cascade(base, &base->tv3, INDEX(1))) && | |
560 | !cascade(base, &base->tv4, INDEX(2))) | |
561 | cascade(base, &base->tv5, INDEX(3)); | |
626ab0e6 ON |
562 | ++base->timer_jiffies; |
563 | list_replace_init(base->tv1.vec + index, &work_list); | |
55c888d6 | 564 | while (!list_empty(head)) { |
1da177e4 LT |
565 | void (*fn)(unsigned long); |
566 | unsigned long data; | |
567 | ||
568 | timer = list_entry(head->next,struct timer_list,entry); | |
569 | fn = timer->function; | |
570 | data = timer->data; | |
571 | ||
1da177e4 | 572 | set_running_timer(base, timer); |
55c888d6 | 573 | detach_timer(timer, 1); |
3691c519 | 574 | spin_unlock_irq(&base->lock); |
1da177e4 | 575 | { |
be5b4fbd | 576 | int preempt_count = preempt_count(); |
1da177e4 LT |
577 | fn(data); |
578 | if (preempt_count != preempt_count()) { | |
be5b4fbd JJ |
579 | printk(KERN_WARNING "huh, entered %p " |
580 | "with preempt_count %08x, exited" | |
581 | " with %08x?\n", | |
582 | fn, preempt_count, | |
583 | preempt_count()); | |
1da177e4 LT |
584 | BUG(); |
585 | } | |
586 | } | |
3691c519 | 587 | spin_lock_irq(&base->lock); |
1da177e4 LT |
588 | } |
589 | } | |
590 | set_running_timer(base, NULL); | |
3691c519 | 591 | spin_unlock_irq(&base->lock); |
1da177e4 LT |
592 | } |
593 | ||
594 | #ifdef CONFIG_NO_IDLE_HZ | |
595 | /* | |
596 | * Find out when the next timer event is due to happen. This | |
597 | * is used on S/390 to stop all activity when a cpus is idle. | |
598 | * This functions needs to be called disabled. | |
599 | */ | |
600 | unsigned long next_timer_interrupt(void) | |
601 | { | |
602 | tvec_base_t *base; | |
603 | struct list_head *list; | |
604 | struct timer_list *nte; | |
605 | unsigned long expires; | |
69239749 TL |
606 | unsigned long hr_expires = MAX_JIFFY_OFFSET; |
607 | ktime_t hr_delta; | |
1da177e4 LT |
608 | tvec_t *varray[4]; |
609 | int i, j; | |
610 | ||
69239749 TL |
611 | hr_delta = hrtimer_get_next_event(); |
612 | if (hr_delta.tv64 != KTIME_MAX) { | |
613 | struct timespec tsdelta; | |
614 | tsdelta = ktime_to_timespec(hr_delta); | |
615 | hr_expires = timespec_to_jiffies(&tsdelta); | |
616 | if (hr_expires < 3) | |
617 | return hr_expires + jiffies; | |
618 | } | |
619 | hr_expires += jiffies; | |
620 | ||
a4a6198b | 621 | base = __get_cpu_var(tvec_bases); |
3691c519 | 622 | spin_lock(&base->lock); |
1da177e4 | 623 | expires = base->timer_jiffies + (LONG_MAX >> 1); |
53f087fe | 624 | list = NULL; |
1da177e4 LT |
625 | |
626 | /* Look for timer events in tv1. */ | |
627 | j = base->timer_jiffies & TVR_MASK; | |
628 | do { | |
629 | list_for_each_entry(nte, base->tv1.vec + j, entry) { | |
630 | expires = nte->expires; | |
631 | if (j < (base->timer_jiffies & TVR_MASK)) | |
632 | list = base->tv2.vec + (INDEX(0)); | |
633 | goto found; | |
634 | } | |
635 | j = (j + 1) & TVR_MASK; | |
636 | } while (j != (base->timer_jiffies & TVR_MASK)); | |
637 | ||
638 | /* Check tv2-tv5. */ | |
639 | varray[0] = &base->tv2; | |
640 | varray[1] = &base->tv3; | |
641 | varray[2] = &base->tv4; | |
642 | varray[3] = &base->tv5; | |
643 | for (i = 0; i < 4; i++) { | |
644 | j = INDEX(i); | |
645 | do { | |
646 | if (list_empty(varray[i]->vec + j)) { | |
647 | j = (j + 1) & TVN_MASK; | |
648 | continue; | |
649 | } | |
650 | list_for_each_entry(nte, varray[i]->vec + j, entry) | |
651 | if (time_before(nte->expires, expires)) | |
652 | expires = nte->expires; | |
653 | if (j < (INDEX(i)) && i < 3) | |
654 | list = varray[i + 1]->vec + (INDEX(i + 1)); | |
655 | goto found; | |
656 | } while (j != (INDEX(i))); | |
657 | } | |
658 | found: | |
659 | if (list) { | |
660 | /* | |
661 | * The search wrapped. We need to look at the next list | |
662 | * from next tv element that would cascade into tv element | |
663 | * where we found the timer element. | |
664 | */ | |
665 | list_for_each_entry(nte, list, entry) { | |
666 | if (time_before(nte->expires, expires)) | |
667 | expires = nte->expires; | |
668 | } | |
669 | } | |
3691c519 | 670 | spin_unlock(&base->lock); |
69239749 | 671 | |
0662b713 ZA |
672 | /* |
673 | * It can happen that other CPUs service timer IRQs and increment | |
674 | * jiffies, but we have not yet got a local timer tick to process | |
675 | * the timer wheels. In that case, the expiry time can be before | |
676 | * jiffies, but since the high-resolution timer here is relative to | |
677 | * jiffies, the default expression when high-resolution timers are | |
678 | * not active, | |
679 | * | |
680 | * time_before(MAX_JIFFY_OFFSET + jiffies, expires) | |
681 | * | |
682 | * would falsely evaluate to true. If that is the case, just | |
683 | * return jiffies so that we can immediately fire the local timer | |
684 | */ | |
685 | if (time_before(expires, jiffies)) | |
686 | return jiffies; | |
687 | ||
69239749 TL |
688 | if (time_before(hr_expires, expires)) |
689 | return hr_expires; | |
690 | ||
1da177e4 LT |
691 | return expires; |
692 | } | |
693 | #endif | |
694 | ||
695 | /******************************************************************/ | |
696 | ||
1da177e4 LT |
697 | /* |
698 | * The current time | |
699 | * wall_to_monotonic is what we need to add to xtime (or xtime corrected | |
700 | * for sub jiffie times) to get to monotonic time. Monotonic is pegged | |
701 | * at zero at system boot time, so wall_to_monotonic will be negative, | |
702 | * however, we will ALWAYS keep the tv_nsec part positive so we can use | |
703 | * the usual normalization. | |
704 | */ | |
705 | struct timespec xtime __attribute__ ((aligned (16))); | |
706 | struct timespec wall_to_monotonic __attribute__ ((aligned (16))); | |
707 | ||
708 | EXPORT_SYMBOL(xtime); | |
709 | ||
726c14bf | 710 | |
ad596171 JS |
711 | /* XXX - all of this timekeeping code should be later moved to time.c */ |
712 | #include <linux/clocksource.h> | |
713 | static struct clocksource *clock; /* pointer to current clocksource */ | |
cf3c769b JS |
714 | |
715 | #ifdef CONFIG_GENERIC_TIME | |
716 | /** | |
717 | * __get_nsec_offset - Returns nanoseconds since last call to periodic_hook | |
718 | * | |
719 | * private function, must hold xtime_lock lock when being | |
720 | * called. Returns the number of nanoseconds since the | |
721 | * last call to update_wall_time() (adjusted by NTP scaling) | |
722 | */ | |
723 | static inline s64 __get_nsec_offset(void) | |
724 | { | |
725 | cycle_t cycle_now, cycle_delta; | |
726 | s64 ns_offset; | |
727 | ||
728 | /* read clocksource: */ | |
a2752549 | 729 | cycle_now = clocksource_read(clock); |
cf3c769b JS |
730 | |
731 | /* calculate the delta since the last update_wall_time: */ | |
19923c19 | 732 | cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; |
cf3c769b JS |
733 | |
734 | /* convert to nanoseconds: */ | |
735 | ns_offset = cyc2ns(clock, cycle_delta); | |
736 | ||
737 | return ns_offset; | |
738 | } | |
739 | ||
740 | /** | |
741 | * __get_realtime_clock_ts - Returns the time of day in a timespec | |
742 | * @ts: pointer to the timespec to be set | |
743 | * | |
744 | * Returns the time of day in a timespec. Used by | |
745 | * do_gettimeofday() and get_realtime_clock_ts(). | |
746 | */ | |
747 | static inline void __get_realtime_clock_ts(struct timespec *ts) | |
748 | { | |
749 | unsigned long seq; | |
750 | s64 nsecs; | |
751 | ||
752 | do { | |
753 | seq = read_seqbegin(&xtime_lock); | |
754 | ||
755 | *ts = xtime; | |
756 | nsecs = __get_nsec_offset(); | |
757 | ||
758 | } while (read_seqretry(&xtime_lock, seq)); | |
759 | ||
760 | timespec_add_ns(ts, nsecs); | |
761 | } | |
762 | ||
763 | /** | |
a2752549 | 764 | * getnstimeofday - Returns the time of day in a timespec |
cf3c769b JS |
765 | * @ts: pointer to the timespec to be set |
766 | * | |
767 | * Returns the time of day in a timespec. | |
768 | */ | |
769 | void getnstimeofday(struct timespec *ts) | |
770 | { | |
771 | __get_realtime_clock_ts(ts); | |
772 | } | |
773 | ||
774 | EXPORT_SYMBOL(getnstimeofday); | |
775 | ||
776 | /** | |
777 | * do_gettimeofday - Returns the time of day in a timeval | |
778 | * @tv: pointer to the timeval to be set | |
779 | * | |
780 | * NOTE: Users should be converted to using get_realtime_clock_ts() | |
781 | */ | |
782 | void do_gettimeofday(struct timeval *tv) | |
783 | { | |
784 | struct timespec now; | |
785 | ||
786 | __get_realtime_clock_ts(&now); | |
787 | tv->tv_sec = now.tv_sec; | |
788 | tv->tv_usec = now.tv_nsec/1000; | |
789 | } | |
790 | ||
791 | EXPORT_SYMBOL(do_gettimeofday); | |
792 | /** | |
793 | * do_settimeofday - Sets the time of day | |
794 | * @tv: pointer to the timespec variable containing the new time | |
795 | * | |
796 | * Sets the time of day to the new time and update NTP and notify hrtimers | |
797 | */ | |
798 | int do_settimeofday(struct timespec *tv) | |
799 | { | |
800 | unsigned long flags; | |
801 | time_t wtm_sec, sec = tv->tv_sec; | |
802 | long wtm_nsec, nsec = tv->tv_nsec; | |
803 | ||
804 | if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC) | |
805 | return -EINVAL; | |
806 | ||
807 | write_seqlock_irqsave(&xtime_lock, flags); | |
808 | ||
809 | nsec -= __get_nsec_offset(); | |
810 | ||
811 | wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec); | |
812 | wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec); | |
813 | ||
814 | set_normalized_timespec(&xtime, sec, nsec); | |
815 | set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec); | |
816 | ||
e154ff3d | 817 | clock->error = 0; |
cf3c769b JS |
818 | ntp_clear(); |
819 | ||
820 | write_sequnlock_irqrestore(&xtime_lock, flags); | |
821 | ||
822 | /* signal hrtimers about time change */ | |
823 | clock_was_set(); | |
824 | ||
825 | return 0; | |
826 | } | |
827 | ||
828 | EXPORT_SYMBOL(do_settimeofday); | |
829 | ||
830 | /** | |
831 | * change_clocksource - Swaps clocksources if a new one is available | |
832 | * | |
833 | * Accumulates current time interval and initializes new clocksource | |
834 | */ | |
835 | static int change_clocksource(void) | |
836 | { | |
837 | struct clocksource *new; | |
838 | cycle_t now; | |
839 | u64 nsec; | |
a2752549 | 840 | new = clocksource_get_next(); |
cf3c769b | 841 | if (clock != new) { |
a2752549 | 842 | now = clocksource_read(new); |
cf3c769b JS |
843 | nsec = __get_nsec_offset(); |
844 | timespec_add_ns(&xtime, nsec); | |
845 | ||
846 | clock = new; | |
19923c19 | 847 | clock->cycle_last = now; |
cf3c769b | 848 | printk(KERN_INFO "Time: %s clocksource has been installed.\n", |
f5f1a24a | 849 | clock->name); |
cf3c769b JS |
850 | return 1; |
851 | } else if (clock->update_callback) { | |
852 | return clock->update_callback(); | |
853 | } | |
854 | return 0; | |
855 | } | |
856 | #else | |
f5f1a24a DW |
857 | static inline int change_clocksource(void) |
858 | { | |
859 | return 0; | |
860 | } | |
cf3c769b JS |
861 | #endif |
862 | ||
863 | /** | |
864 | * timeofday_is_continuous - check to see if timekeeping is free running | |
865 | */ | |
866 | int timekeeping_is_continuous(void) | |
867 | { | |
868 | unsigned long seq; | |
869 | int ret; | |
870 | ||
871 | do { | |
872 | seq = read_seqbegin(&xtime_lock); | |
873 | ||
73b08d2a | 874 | ret = clock->flags & CLOCK_SOURCE_IS_CONTINUOUS; |
cf3c769b JS |
875 | |
876 | } while (read_seqretry(&xtime_lock, seq)); | |
877 | ||
878 | return ret; | |
879 | } | |
880 | ||
411187fb JS |
881 | /** |
882 | * read_persistent_clock - Return time in seconds from the persistent clock. | |
883 | * | |
884 | * Weak dummy function for arches that do not yet support it. | |
885 | * Returns seconds from epoch using the battery backed persistent clock. | |
886 | * Returns zero if unsupported. | |
887 | * | |
888 | * XXX - Do be sure to remove it once all arches implement it. | |
889 | */ | |
890 | unsigned long __attribute__((weak)) read_persistent_clock(void) | |
891 | { | |
892 | return 0; | |
893 | } | |
894 | ||
1da177e4 | 895 | /* |
ad596171 | 896 | * timekeeping_init - Initializes the clocksource and common timekeeping values |
1da177e4 | 897 | */ |
ad596171 | 898 | void __init timekeeping_init(void) |
1da177e4 | 899 | { |
ad596171 | 900 | unsigned long flags; |
411187fb | 901 | unsigned long sec = read_persistent_clock(); |
ad596171 JS |
902 | |
903 | write_seqlock_irqsave(&xtime_lock, flags); | |
b0ee7556 RZ |
904 | |
905 | ntp_clear(); | |
906 | ||
a2752549 | 907 | clock = clocksource_get_next(); |
f4304ab2 | 908 | clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH); |
19923c19 | 909 | clock->cycle_last = clocksource_read(clock); |
b0ee7556 | 910 | |
411187fb JS |
911 | xtime.tv_sec = sec; |
912 | xtime.tv_nsec = 0; | |
913 | set_normalized_timespec(&wall_to_monotonic, | |
914 | -xtime.tv_sec, -xtime.tv_nsec); | |
915 | ||
ad596171 JS |
916 | write_sequnlock_irqrestore(&xtime_lock, flags); |
917 | } | |
918 | ||
919 | ||
411187fb | 920 | /* flag for if timekeeping is suspended */ |
3e143475 | 921 | static int timekeeping_suspended; |
411187fb JS |
922 | /* time in seconds when suspend began */ |
923 | static unsigned long timekeeping_suspend_time; | |
924 | ||
2aae4a10 | 925 | /** |
ad596171 JS |
926 | * timekeeping_resume - Resumes the generic timekeeping subsystem. |
927 | * @dev: unused | |
928 | * | |
929 | * This is for the generic clocksource timekeeping. | |
8ef38609 | 930 | * xtime/wall_to_monotonic/jiffies/etc are |
ad596171 JS |
931 | * still managed by arch specific suspend/resume code. |
932 | */ | |
933 | static int timekeeping_resume(struct sys_device *dev) | |
934 | { | |
935 | unsigned long flags; | |
411187fb | 936 | unsigned long now = read_persistent_clock(); |
ad596171 JS |
937 | |
938 | write_seqlock_irqsave(&xtime_lock, flags); | |
411187fb JS |
939 | |
940 | if (now && (now > timekeeping_suspend_time)) { | |
941 | unsigned long sleep_length = now - timekeeping_suspend_time; | |
942 | ||
943 | xtime.tv_sec += sleep_length; | |
944 | wall_to_monotonic.tv_sec -= sleep_length; | |
945 | } | |
946 | /* re-base the last cycle value */ | |
19923c19 | 947 | clock->cycle_last = clocksource_read(clock); |
3e143475 JS |
948 | clock->error = 0; |
949 | timekeeping_suspended = 0; | |
950 | write_sequnlock_irqrestore(&xtime_lock, flags); | |
411187fb JS |
951 | |
952 | touch_softlockup_watchdog(); | |
953 | hrtimer_notify_resume(); | |
954 | ||
3e143475 JS |
955 | return 0; |
956 | } | |
957 | ||
958 | static int timekeeping_suspend(struct sys_device *dev, pm_message_t state) | |
959 | { | |
960 | unsigned long flags; | |
961 | ||
962 | write_seqlock_irqsave(&xtime_lock, flags); | |
963 | timekeeping_suspended = 1; | |
411187fb | 964 | timekeeping_suspend_time = read_persistent_clock(); |
ad596171 JS |
965 | write_sequnlock_irqrestore(&xtime_lock, flags); |
966 | return 0; | |
967 | } | |
968 | ||
969 | /* sysfs resume/suspend bits for timekeeping */ | |
970 | static struct sysdev_class timekeeping_sysclass = { | |
971 | .resume = timekeeping_resume, | |
3e143475 | 972 | .suspend = timekeeping_suspend, |
ad596171 JS |
973 | set_kset_name("timekeeping"), |
974 | }; | |
975 | ||
976 | static struct sys_device device_timer = { | |
977 | .id = 0, | |
978 | .cls = &timekeeping_sysclass, | |
979 | }; | |
980 | ||
981 | static int __init timekeeping_init_device(void) | |
982 | { | |
983 | int error = sysdev_class_register(&timekeeping_sysclass); | |
984 | if (!error) | |
985 | error = sysdev_register(&device_timer); | |
986 | return error; | |
987 | } | |
988 | ||
989 | device_initcall(timekeeping_init_device); | |
990 | ||
19923c19 | 991 | /* |
e154ff3d | 992 | * If the error is already larger, we look ahead even further |
19923c19 RZ |
993 | * to compensate for late or lost adjustments. |
994 | */ | |
f5f1a24a DW |
995 | static __always_inline int clocksource_bigadjust(s64 error, s64 *interval, |
996 | s64 *offset) | |
19923c19 | 997 | { |
e154ff3d RZ |
998 | s64 tick_error, i; |
999 | u32 look_ahead, adj; | |
1000 | s32 error2, mult; | |
19923c19 RZ |
1001 | |
1002 | /* | |
e154ff3d RZ |
1003 | * Use the current error value to determine how much to look ahead. |
1004 | * The larger the error the slower we adjust for it to avoid problems | |
1005 | * with losing too many ticks, otherwise we would overadjust and | |
1006 | * produce an even larger error. The smaller the adjustment the | |
1007 | * faster we try to adjust for it, as lost ticks can do less harm | |
1008 | * here. This is tuned so that an error of about 1 msec is adusted | |
1009 | * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks). | |
19923c19 | 1010 | */ |
e154ff3d RZ |
1011 | error2 = clock->error >> (TICK_LENGTH_SHIFT + 22 - 2 * SHIFT_HZ); |
1012 | error2 = abs(error2); | |
1013 | for (look_ahead = 0; error2 > 0; look_ahead++) | |
1014 | error2 >>= 2; | |
19923c19 RZ |
1015 | |
1016 | /* | |
e154ff3d RZ |
1017 | * Now calculate the error in (1 << look_ahead) ticks, but first |
1018 | * remove the single look ahead already included in the error. | |
19923c19 | 1019 | */ |
f5f1a24a DW |
1020 | tick_error = current_tick_length() >> |
1021 | (TICK_LENGTH_SHIFT - clock->shift + 1); | |
e154ff3d RZ |
1022 | tick_error -= clock->xtime_interval >> 1; |
1023 | error = ((error - tick_error) >> look_ahead) + tick_error; | |
1024 | ||
1025 | /* Finally calculate the adjustment shift value. */ | |
1026 | i = *interval; | |
1027 | mult = 1; | |
1028 | if (error < 0) { | |
1029 | error = -error; | |
1030 | *interval = -*interval; | |
1031 | *offset = -*offset; | |
1032 | mult = -1; | |
19923c19 | 1033 | } |
e154ff3d RZ |
1034 | for (adj = 0; error > i; adj++) |
1035 | error >>= 1; | |
19923c19 RZ |
1036 | |
1037 | *interval <<= adj; | |
1038 | *offset <<= adj; | |
e154ff3d | 1039 | return mult << adj; |
19923c19 RZ |
1040 | } |
1041 | ||
1042 | /* | |
1043 | * Adjust the multiplier to reduce the error value, | |
1044 | * this is optimized for the most common adjustments of -1,0,1, | |
1045 | * for other values we can do a bit more work. | |
1046 | */ | |
1047 | static void clocksource_adjust(struct clocksource *clock, s64 offset) | |
1048 | { | |
1049 | s64 error, interval = clock->cycle_interval; | |
1050 | int adj; | |
1051 | ||
1052 | error = clock->error >> (TICK_LENGTH_SHIFT - clock->shift - 1); | |
1053 | if (error > interval) { | |
e154ff3d RZ |
1054 | error >>= 2; |
1055 | if (likely(error <= interval)) | |
1056 | adj = 1; | |
1057 | else | |
1058 | adj = clocksource_bigadjust(error, &interval, &offset); | |
19923c19 | 1059 | } else if (error < -interval) { |
e154ff3d RZ |
1060 | error >>= 2; |
1061 | if (likely(error >= -interval)) { | |
1062 | adj = -1; | |
1063 | interval = -interval; | |
1064 | offset = -offset; | |
1065 | } else | |
1066 | adj = clocksource_bigadjust(error, &interval, &offset); | |
19923c19 RZ |
1067 | } else |
1068 | return; | |
1069 | ||
1070 | clock->mult += adj; | |
1071 | clock->xtime_interval += interval; | |
1072 | clock->xtime_nsec -= offset; | |
f5f1a24a DW |
1073 | clock->error -= (interval - offset) << |
1074 | (TICK_LENGTH_SHIFT - clock->shift); | |
19923c19 RZ |
1075 | } |
1076 | ||
2aae4a10 | 1077 | /** |
ad596171 JS |
1078 | * update_wall_time - Uses the current clocksource to increment the wall time |
1079 | * | |
1080 | * Called from the timer interrupt, must hold a write on xtime_lock. | |
1081 | */ | |
1082 | static void update_wall_time(void) | |
1083 | { | |
19923c19 | 1084 | cycle_t offset; |
ad596171 | 1085 | |
3e143475 JS |
1086 | /* Make sure we're fully resumed: */ |
1087 | if (unlikely(timekeeping_suspended)) | |
1088 | return; | |
5eb6d205 | 1089 | |
19923c19 RZ |
1090 | #ifdef CONFIG_GENERIC_TIME |
1091 | offset = (clocksource_read(clock) - clock->cycle_last) & clock->mask; | |
1092 | #else | |
1093 | offset = clock->cycle_interval; | |
1094 | #endif | |
3e143475 | 1095 | clock->xtime_nsec += (s64)xtime.tv_nsec << clock->shift; |
ad596171 JS |
1096 | |
1097 | /* normally this loop will run just once, however in the | |
1098 | * case of lost or late ticks, it will accumulate correctly. | |
1099 | */ | |
19923c19 | 1100 | while (offset >= clock->cycle_interval) { |
ad596171 | 1101 | /* accumulate one interval */ |
19923c19 RZ |
1102 | clock->xtime_nsec += clock->xtime_interval; |
1103 | clock->cycle_last += clock->cycle_interval; | |
1104 | offset -= clock->cycle_interval; | |
1105 | ||
1106 | if (clock->xtime_nsec >= (u64)NSEC_PER_SEC << clock->shift) { | |
1107 | clock->xtime_nsec -= (u64)NSEC_PER_SEC << clock->shift; | |
1108 | xtime.tv_sec++; | |
1109 | second_overflow(); | |
1110 | } | |
ad596171 | 1111 | |
5eb6d205 | 1112 | /* interpolator bits */ |
19923c19 | 1113 | time_interpolator_update(clock->xtime_interval |
5eb6d205 | 1114 | >> clock->shift); |
5eb6d205 JS |
1115 | |
1116 | /* accumulate error between NTP and clock interval */ | |
19923c19 RZ |
1117 | clock->error += current_tick_length(); |
1118 | clock->error -= clock->xtime_interval << (TICK_LENGTH_SHIFT - clock->shift); | |
1119 | } | |
5eb6d205 | 1120 | |
19923c19 RZ |
1121 | /* correct the clock when NTP error is too big */ |
1122 | clocksource_adjust(clock, offset); | |
5eb6d205 | 1123 | |
5eb6d205 | 1124 | /* store full nanoseconds into xtime */ |
e154ff3d | 1125 | xtime.tv_nsec = (s64)clock->xtime_nsec >> clock->shift; |
19923c19 | 1126 | clock->xtime_nsec -= (s64)xtime.tv_nsec << clock->shift; |
cf3c769b JS |
1127 | |
1128 | /* check to see if there is a new clocksource to use */ | |
1129 | if (change_clocksource()) { | |
19923c19 RZ |
1130 | clock->error = 0; |
1131 | clock->xtime_nsec = 0; | |
f4304ab2 | 1132 | clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH); |
cf3c769b | 1133 | } |
1da177e4 LT |
1134 | } |
1135 | ||
1136 | /* | |
1137 | * Called from the timer interrupt handler to charge one tick to the current | |
1138 | * process. user_tick is 1 if the tick is user time, 0 for system. | |
1139 | */ | |
1140 | void update_process_times(int user_tick) | |
1141 | { | |
1142 | struct task_struct *p = current; | |
1143 | int cpu = smp_processor_id(); | |
1144 | ||
1145 | /* Note: this timer irq context must be accounted for as well. */ | |
1146 | if (user_tick) | |
1147 | account_user_time(p, jiffies_to_cputime(1)); | |
1148 | else | |
1149 | account_system_time(p, HARDIRQ_OFFSET, jiffies_to_cputime(1)); | |
1150 | run_local_timers(); | |
1151 | if (rcu_pending(cpu)) | |
1152 | rcu_check_callbacks(cpu, user_tick); | |
1153 | scheduler_tick(); | |
1154 | run_posix_cpu_timers(p); | |
1155 | } | |
1156 | ||
1157 | /* | |
1158 | * Nr of active tasks - counted in fixed-point numbers | |
1159 | */ | |
1160 | static unsigned long count_active_tasks(void) | |
1161 | { | |
db1b1fef | 1162 | return nr_active() * FIXED_1; |
1da177e4 LT |
1163 | } |
1164 | ||
1165 | /* | |
1166 | * Hmm.. Changed this, as the GNU make sources (load.c) seems to | |
1167 | * imply that avenrun[] is the standard name for this kind of thing. | |
1168 | * Nothing else seems to be standardized: the fractional size etc | |
1169 | * all seem to differ on different machines. | |
1170 | * | |
1171 | * Requires xtime_lock to access. | |
1172 | */ | |
1173 | unsigned long avenrun[3]; | |
1174 | ||
1175 | EXPORT_SYMBOL(avenrun); | |
1176 | ||
1177 | /* | |
1178 | * calc_load - given tick count, update the avenrun load estimates. | |
1179 | * This is called while holding a write_lock on xtime_lock. | |
1180 | */ | |
1181 | static inline void calc_load(unsigned long ticks) | |
1182 | { | |
1183 | unsigned long active_tasks; /* fixed-point */ | |
1184 | static int count = LOAD_FREQ; | |
1185 | ||
cd7175ed ED |
1186 | count -= ticks; |
1187 | if (unlikely(count < 0)) { | |
1188 | active_tasks = count_active_tasks(); | |
1189 | do { | |
1190 | CALC_LOAD(avenrun[0], EXP_1, active_tasks); | |
1191 | CALC_LOAD(avenrun[1], EXP_5, active_tasks); | |
1192 | CALC_LOAD(avenrun[2], EXP_15, active_tasks); | |
1193 | count += LOAD_FREQ; | |
1194 | } while (count < 0); | |
1da177e4 LT |
1195 | } |
1196 | } | |
1197 | ||
1da177e4 LT |
1198 | /* |
1199 | * This read-write spinlock protects us from races in SMP while | |
1200 | * playing with xtime and avenrun. | |
1201 | */ | |
5809f9d4 | 1202 | __attribute__((weak)) __cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock); |
1da177e4 LT |
1203 | |
1204 | EXPORT_SYMBOL(xtime_lock); | |
1da177e4 LT |
1205 | |
1206 | /* | |
1207 | * This function runs timers and the timer-tq in bottom half context. | |
1208 | */ | |
1209 | static void run_timer_softirq(struct softirq_action *h) | |
1210 | { | |
a4a6198b | 1211 | tvec_base_t *base = __get_cpu_var(tvec_bases); |
1da177e4 | 1212 | |
c0a31329 | 1213 | hrtimer_run_queues(); |
1da177e4 LT |
1214 | if (time_after_eq(jiffies, base->timer_jiffies)) |
1215 | __run_timers(base); | |
1216 | } | |
1217 | ||
1218 | /* | |
1219 | * Called by the local, per-CPU timer interrupt on SMP. | |
1220 | */ | |
1221 | void run_local_timers(void) | |
1222 | { | |
1223 | raise_softirq(TIMER_SOFTIRQ); | |
6687a97d | 1224 | softlockup_tick(); |
1da177e4 LT |
1225 | } |
1226 | ||
1227 | /* | |
1228 | * Called by the timer interrupt. xtime_lock must already be taken | |
1229 | * by the timer IRQ! | |
1230 | */ | |
3171a030 | 1231 | static inline void update_times(unsigned long ticks) |
1da177e4 | 1232 | { |
ad596171 | 1233 | update_wall_time(); |
1da177e4 LT |
1234 | calc_load(ticks); |
1235 | } | |
1236 | ||
1237 | /* | |
1238 | * The 64-bit jiffies value is not atomic - you MUST NOT read it | |
1239 | * without sampling the sequence number in xtime_lock. | |
1240 | * jiffies is defined in the linker script... | |
1241 | */ | |
1242 | ||
3171a030 | 1243 | void do_timer(unsigned long ticks) |
1da177e4 | 1244 | { |
3171a030 AN |
1245 | jiffies_64 += ticks; |
1246 | update_times(ticks); | |
1da177e4 LT |
1247 | } |
1248 | ||
1249 | #ifdef __ARCH_WANT_SYS_ALARM | |
1250 | ||
1251 | /* | |
1252 | * For backwards compatibility? This can be done in libc so Alpha | |
1253 | * and all newer ports shouldn't need it. | |
1254 | */ | |
1255 | asmlinkage unsigned long sys_alarm(unsigned int seconds) | |
1256 | { | |
c08b8a49 | 1257 | return alarm_setitimer(seconds); |
1da177e4 LT |
1258 | } |
1259 | ||
1260 | #endif | |
1261 | ||
1262 | #ifndef __alpha__ | |
1263 | ||
1264 | /* | |
1265 | * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this | |
1266 | * should be moved into arch/i386 instead? | |
1267 | */ | |
1268 | ||
1269 | /** | |
1270 | * sys_getpid - return the thread group id of the current process | |
1271 | * | |
1272 | * Note, despite the name, this returns the tgid not the pid. The tgid and | |
1273 | * the pid are identical unless CLONE_THREAD was specified on clone() in | |
1274 | * which case the tgid is the same in all threads of the same group. | |
1275 | * | |
1276 | * This is SMP safe as current->tgid does not change. | |
1277 | */ | |
1278 | asmlinkage long sys_getpid(void) | |
1279 | { | |
1280 | return current->tgid; | |
1281 | } | |
1282 | ||
1283 | /* | |
6997a6fa KK |
1284 | * Accessing ->real_parent is not SMP-safe, it could |
1285 | * change from under us. However, we can use a stale | |
1286 | * value of ->real_parent under rcu_read_lock(), see | |
1287 | * release_task()->call_rcu(delayed_put_task_struct). | |
1da177e4 LT |
1288 | */ |
1289 | asmlinkage long sys_getppid(void) | |
1290 | { | |
1291 | int pid; | |
1da177e4 | 1292 | |
6997a6fa KK |
1293 | rcu_read_lock(); |
1294 | pid = rcu_dereference(current->real_parent)->tgid; | |
1295 | rcu_read_unlock(); | |
1da177e4 | 1296 | |
1da177e4 LT |
1297 | return pid; |
1298 | } | |
1299 | ||
1300 | asmlinkage long sys_getuid(void) | |
1301 | { | |
1302 | /* Only we change this so SMP safe */ | |
1303 | return current->uid; | |
1304 | } | |
1305 | ||
1306 | asmlinkage long sys_geteuid(void) | |
1307 | { | |
1308 | /* Only we change this so SMP safe */ | |
1309 | return current->euid; | |
1310 | } | |
1311 | ||
1312 | asmlinkage long sys_getgid(void) | |
1313 | { | |
1314 | /* Only we change this so SMP safe */ | |
1315 | return current->gid; | |
1316 | } | |
1317 | ||
1318 | asmlinkage long sys_getegid(void) | |
1319 | { | |
1320 | /* Only we change this so SMP safe */ | |
1321 | return current->egid; | |
1322 | } | |
1323 | ||
1324 | #endif | |
1325 | ||
1326 | static void process_timeout(unsigned long __data) | |
1327 | { | |
36c8b586 | 1328 | wake_up_process((struct task_struct *)__data); |
1da177e4 LT |
1329 | } |
1330 | ||
1331 | /** | |
1332 | * schedule_timeout - sleep until timeout | |
1333 | * @timeout: timeout value in jiffies | |
1334 | * | |
1335 | * Make the current task sleep until @timeout jiffies have | |
1336 | * elapsed. The routine will return immediately unless | |
1337 | * the current task state has been set (see set_current_state()). | |
1338 | * | |
1339 | * You can set the task state as follows - | |
1340 | * | |
1341 | * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to | |
1342 | * pass before the routine returns. The routine will return 0 | |
1343 | * | |
1344 | * %TASK_INTERRUPTIBLE - the routine may return early if a signal is | |
1345 | * delivered to the current task. In this case the remaining time | |
1346 | * in jiffies will be returned, or 0 if the timer expired in time | |
1347 | * | |
1348 | * The current task state is guaranteed to be TASK_RUNNING when this | |
1349 | * routine returns. | |
1350 | * | |
1351 | * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule | |
1352 | * the CPU away without a bound on the timeout. In this case the return | |
1353 | * value will be %MAX_SCHEDULE_TIMEOUT. | |
1354 | * | |
1355 | * In all cases the return value is guaranteed to be non-negative. | |
1356 | */ | |
1357 | fastcall signed long __sched schedule_timeout(signed long timeout) | |
1358 | { | |
1359 | struct timer_list timer; | |
1360 | unsigned long expire; | |
1361 | ||
1362 | switch (timeout) | |
1363 | { | |
1364 | case MAX_SCHEDULE_TIMEOUT: | |
1365 | /* | |
1366 | * These two special cases are useful to be comfortable | |
1367 | * in the caller. Nothing more. We could take | |
1368 | * MAX_SCHEDULE_TIMEOUT from one of the negative value | |
1369 | * but I' d like to return a valid offset (>=0) to allow | |
1370 | * the caller to do everything it want with the retval. | |
1371 | */ | |
1372 | schedule(); | |
1373 | goto out; | |
1374 | default: | |
1375 | /* | |
1376 | * Another bit of PARANOID. Note that the retval will be | |
1377 | * 0 since no piece of kernel is supposed to do a check | |
1378 | * for a negative retval of schedule_timeout() (since it | |
1379 | * should never happens anyway). You just have the printk() | |
1380 | * that will tell you if something is gone wrong and where. | |
1381 | */ | |
5b149bcc | 1382 | if (timeout < 0) { |
1da177e4 | 1383 | printk(KERN_ERR "schedule_timeout: wrong timeout " |
5b149bcc AM |
1384 | "value %lx\n", timeout); |
1385 | dump_stack(); | |
1da177e4 LT |
1386 | current->state = TASK_RUNNING; |
1387 | goto out; | |
1388 | } | |
1389 | } | |
1390 | ||
1391 | expire = timeout + jiffies; | |
1392 | ||
a8db2db1 ON |
1393 | setup_timer(&timer, process_timeout, (unsigned long)current); |
1394 | __mod_timer(&timer, expire); | |
1da177e4 LT |
1395 | schedule(); |
1396 | del_singleshot_timer_sync(&timer); | |
1397 | ||
1398 | timeout = expire - jiffies; | |
1399 | ||
1400 | out: | |
1401 | return timeout < 0 ? 0 : timeout; | |
1402 | } | |
1da177e4 LT |
1403 | EXPORT_SYMBOL(schedule_timeout); |
1404 | ||
8a1c1757 AM |
1405 | /* |
1406 | * We can use __set_current_state() here because schedule_timeout() calls | |
1407 | * schedule() unconditionally. | |
1408 | */ | |
64ed93a2 NA |
1409 | signed long __sched schedule_timeout_interruptible(signed long timeout) |
1410 | { | |
a5a0d52c AM |
1411 | __set_current_state(TASK_INTERRUPTIBLE); |
1412 | return schedule_timeout(timeout); | |
64ed93a2 NA |
1413 | } |
1414 | EXPORT_SYMBOL(schedule_timeout_interruptible); | |
1415 | ||
1416 | signed long __sched schedule_timeout_uninterruptible(signed long timeout) | |
1417 | { | |
a5a0d52c AM |
1418 | __set_current_state(TASK_UNINTERRUPTIBLE); |
1419 | return schedule_timeout(timeout); | |
64ed93a2 NA |
1420 | } |
1421 | EXPORT_SYMBOL(schedule_timeout_uninterruptible); | |
1422 | ||
1da177e4 LT |
1423 | /* Thread ID - the internal kernel "pid" */ |
1424 | asmlinkage long sys_gettid(void) | |
1425 | { | |
1426 | return current->pid; | |
1427 | } | |
1428 | ||
2aae4a10 | 1429 | /** |
d4d23add | 1430 | * do_sysinfo - fill in sysinfo struct |
2aae4a10 | 1431 | * @info: pointer to buffer to fill |
1da177e4 | 1432 | */ |
d4d23add | 1433 | int do_sysinfo(struct sysinfo *info) |
1da177e4 | 1434 | { |
1da177e4 LT |
1435 | unsigned long mem_total, sav_total; |
1436 | unsigned int mem_unit, bitcount; | |
1437 | unsigned long seq; | |
1438 | ||
d4d23add | 1439 | memset(info, 0, sizeof(struct sysinfo)); |
1da177e4 LT |
1440 | |
1441 | do { | |
1442 | struct timespec tp; | |
1443 | seq = read_seqbegin(&xtime_lock); | |
1444 | ||
1445 | /* | |
1446 | * This is annoying. The below is the same thing | |
1447 | * posix_get_clock_monotonic() does, but it wants to | |
1448 | * take the lock which we want to cover the loads stuff | |
1449 | * too. | |
1450 | */ | |
1451 | ||
1452 | getnstimeofday(&tp); | |
1453 | tp.tv_sec += wall_to_monotonic.tv_sec; | |
1454 | tp.tv_nsec += wall_to_monotonic.tv_nsec; | |
1455 | if (tp.tv_nsec - NSEC_PER_SEC >= 0) { | |
1456 | tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC; | |
1457 | tp.tv_sec++; | |
1458 | } | |
d4d23add | 1459 | info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0); |
1da177e4 | 1460 | |
d4d23add KM |
1461 | info->loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT); |
1462 | info->loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT); | |
1463 | info->loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT); | |
1da177e4 | 1464 | |
d4d23add | 1465 | info->procs = nr_threads; |
1da177e4 LT |
1466 | } while (read_seqretry(&xtime_lock, seq)); |
1467 | ||
d4d23add KM |
1468 | si_meminfo(info); |
1469 | si_swapinfo(info); | |
1da177e4 LT |
1470 | |
1471 | /* | |
1472 | * If the sum of all the available memory (i.e. ram + swap) | |
1473 | * is less than can be stored in a 32 bit unsigned long then | |
1474 | * we can be binary compatible with 2.2.x kernels. If not, | |
1475 | * well, in that case 2.2.x was broken anyways... | |
1476 | * | |
1477 | * -Erik Andersen <[email protected]> | |
1478 | */ | |
1479 | ||
d4d23add KM |
1480 | mem_total = info->totalram + info->totalswap; |
1481 | if (mem_total < info->totalram || mem_total < info->totalswap) | |
1da177e4 LT |
1482 | goto out; |
1483 | bitcount = 0; | |
d4d23add | 1484 | mem_unit = info->mem_unit; |
1da177e4 LT |
1485 | while (mem_unit > 1) { |
1486 | bitcount++; | |
1487 | mem_unit >>= 1; | |
1488 | sav_total = mem_total; | |
1489 | mem_total <<= 1; | |
1490 | if (mem_total < sav_total) | |
1491 | goto out; | |
1492 | } | |
1493 | ||
1494 | /* | |
1495 | * If mem_total did not overflow, multiply all memory values by | |
d4d23add | 1496 | * info->mem_unit and set it to 1. This leaves things compatible |
1da177e4 LT |
1497 | * with 2.2.x, and also retains compatibility with earlier 2.4.x |
1498 | * kernels... | |
1499 | */ | |
1500 | ||
d4d23add KM |
1501 | info->mem_unit = 1; |
1502 | info->totalram <<= bitcount; | |
1503 | info->freeram <<= bitcount; | |
1504 | info->sharedram <<= bitcount; | |
1505 | info->bufferram <<= bitcount; | |
1506 | info->totalswap <<= bitcount; | |
1507 | info->freeswap <<= bitcount; | |
1508 | info->totalhigh <<= bitcount; | |
1509 | info->freehigh <<= bitcount; | |
1510 | ||
1511 | out: | |
1512 | return 0; | |
1513 | } | |
1514 | ||
1515 | asmlinkage long sys_sysinfo(struct sysinfo __user *info) | |
1516 | { | |
1517 | struct sysinfo val; | |
1518 | ||
1519 | do_sysinfo(&val); | |
1da177e4 | 1520 | |
1da177e4 LT |
1521 | if (copy_to_user(info, &val, sizeof(struct sysinfo))) |
1522 | return -EFAULT; | |
1523 | ||
1524 | return 0; | |
1525 | } | |
1526 | ||
d730e882 IM |
1527 | /* |
1528 | * lockdep: we want to track each per-CPU base as a separate lock-class, | |
1529 | * but timer-bases are kmalloc()-ed, so we need to attach separate | |
1530 | * keys to them: | |
1531 | */ | |
1532 | static struct lock_class_key base_lock_keys[NR_CPUS]; | |
1533 | ||
a4a6198b | 1534 | static int __devinit init_timers_cpu(int cpu) |
1da177e4 LT |
1535 | { |
1536 | int j; | |
1537 | tvec_base_t *base; | |
ba6edfcd | 1538 | static char __devinitdata tvec_base_done[NR_CPUS]; |
55c888d6 | 1539 | |
ba6edfcd | 1540 | if (!tvec_base_done[cpu]) { |
a4a6198b JB |
1541 | static char boot_done; |
1542 | ||
a4a6198b | 1543 | if (boot_done) { |
ba6edfcd AM |
1544 | /* |
1545 | * The APs use this path later in boot | |
1546 | */ | |
a4a6198b JB |
1547 | base = kmalloc_node(sizeof(*base), GFP_KERNEL, |
1548 | cpu_to_node(cpu)); | |
1549 | if (!base) | |
1550 | return -ENOMEM; | |
1551 | memset(base, 0, sizeof(*base)); | |
ba6edfcd | 1552 | per_cpu(tvec_bases, cpu) = base; |
a4a6198b | 1553 | } else { |
ba6edfcd AM |
1554 | /* |
1555 | * This is for the boot CPU - we use compile-time | |
1556 | * static initialisation because per-cpu memory isn't | |
1557 | * ready yet and because the memory allocators are not | |
1558 | * initialised either. | |
1559 | */ | |
a4a6198b | 1560 | boot_done = 1; |
ba6edfcd | 1561 | base = &boot_tvec_bases; |
a4a6198b | 1562 | } |
ba6edfcd AM |
1563 | tvec_base_done[cpu] = 1; |
1564 | } else { | |
1565 | base = per_cpu(tvec_bases, cpu); | |
a4a6198b | 1566 | } |
ba6edfcd | 1567 | |
3691c519 | 1568 | spin_lock_init(&base->lock); |
d730e882 IM |
1569 | lockdep_set_class(&base->lock, base_lock_keys + cpu); |
1570 | ||
1da177e4 LT |
1571 | for (j = 0; j < TVN_SIZE; j++) { |
1572 | INIT_LIST_HEAD(base->tv5.vec + j); | |
1573 | INIT_LIST_HEAD(base->tv4.vec + j); | |
1574 | INIT_LIST_HEAD(base->tv3.vec + j); | |
1575 | INIT_LIST_HEAD(base->tv2.vec + j); | |
1576 | } | |
1577 | for (j = 0; j < TVR_SIZE; j++) | |
1578 | INIT_LIST_HEAD(base->tv1.vec + j); | |
1579 | ||
1580 | base->timer_jiffies = jiffies; | |
a4a6198b | 1581 | return 0; |
1da177e4 LT |
1582 | } |
1583 | ||
1584 | #ifdef CONFIG_HOTPLUG_CPU | |
55c888d6 | 1585 | static void migrate_timer_list(tvec_base_t *new_base, struct list_head *head) |
1da177e4 LT |
1586 | { |
1587 | struct timer_list *timer; | |
1588 | ||
1589 | while (!list_empty(head)) { | |
1590 | timer = list_entry(head->next, struct timer_list, entry); | |
55c888d6 | 1591 | detach_timer(timer, 0); |
3691c519 | 1592 | timer->base = new_base; |
1da177e4 | 1593 | internal_add_timer(new_base, timer); |
1da177e4 | 1594 | } |
1da177e4 LT |
1595 | } |
1596 | ||
1597 | static void __devinit migrate_timers(int cpu) | |
1598 | { | |
1599 | tvec_base_t *old_base; | |
1600 | tvec_base_t *new_base; | |
1601 | int i; | |
1602 | ||
1603 | BUG_ON(cpu_online(cpu)); | |
a4a6198b JB |
1604 | old_base = per_cpu(tvec_bases, cpu); |
1605 | new_base = get_cpu_var(tvec_bases); | |
1da177e4 LT |
1606 | |
1607 | local_irq_disable(); | |
3691c519 ON |
1608 | spin_lock(&new_base->lock); |
1609 | spin_lock(&old_base->lock); | |
1610 | ||
1611 | BUG_ON(old_base->running_timer); | |
1da177e4 | 1612 | |
1da177e4 | 1613 | for (i = 0; i < TVR_SIZE; i++) |
55c888d6 ON |
1614 | migrate_timer_list(new_base, old_base->tv1.vec + i); |
1615 | for (i = 0; i < TVN_SIZE; i++) { | |
1616 | migrate_timer_list(new_base, old_base->tv2.vec + i); | |
1617 | migrate_timer_list(new_base, old_base->tv3.vec + i); | |
1618 | migrate_timer_list(new_base, old_base->tv4.vec + i); | |
1619 | migrate_timer_list(new_base, old_base->tv5.vec + i); | |
1620 | } | |
1621 | ||
3691c519 ON |
1622 | spin_unlock(&old_base->lock); |
1623 | spin_unlock(&new_base->lock); | |
1da177e4 LT |
1624 | local_irq_enable(); |
1625 | put_cpu_var(tvec_bases); | |
1da177e4 LT |
1626 | } |
1627 | #endif /* CONFIG_HOTPLUG_CPU */ | |
1628 | ||
8c78f307 | 1629 | static int __cpuinit timer_cpu_notify(struct notifier_block *self, |
1da177e4 LT |
1630 | unsigned long action, void *hcpu) |
1631 | { | |
1632 | long cpu = (long)hcpu; | |
1633 | switch(action) { | |
1634 | case CPU_UP_PREPARE: | |
a4a6198b JB |
1635 | if (init_timers_cpu(cpu) < 0) |
1636 | return NOTIFY_BAD; | |
1da177e4 LT |
1637 | break; |
1638 | #ifdef CONFIG_HOTPLUG_CPU | |
1639 | case CPU_DEAD: | |
1640 | migrate_timers(cpu); | |
1641 | break; | |
1642 | #endif | |
1643 | default: | |
1644 | break; | |
1645 | } | |
1646 | return NOTIFY_OK; | |
1647 | } | |
1648 | ||
8c78f307 | 1649 | static struct notifier_block __cpuinitdata timers_nb = { |
1da177e4 LT |
1650 | .notifier_call = timer_cpu_notify, |
1651 | }; | |
1652 | ||
1653 | ||
1654 | void __init init_timers(void) | |
1655 | { | |
07dccf33 | 1656 | int err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE, |
1da177e4 | 1657 | (void *)(long)smp_processor_id()); |
07dccf33 AM |
1658 | |
1659 | BUG_ON(err == NOTIFY_BAD); | |
1da177e4 LT |
1660 | register_cpu_notifier(&timers_nb); |
1661 | open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL); | |
1662 | } | |
1663 | ||
1664 | #ifdef CONFIG_TIME_INTERPOLATION | |
1665 | ||
67890d70 CL |
1666 | struct time_interpolator *time_interpolator __read_mostly; |
1667 | static struct time_interpolator *time_interpolator_list __read_mostly; | |
1da177e4 LT |
1668 | static DEFINE_SPINLOCK(time_interpolator_lock); |
1669 | ||
3db5db4f | 1670 | static inline cycles_t time_interpolator_get_cycles(unsigned int src) |
1da177e4 LT |
1671 | { |
1672 | unsigned long (*x)(void); | |
1673 | ||
1674 | switch (src) | |
1675 | { | |
1676 | case TIME_SOURCE_FUNCTION: | |
1677 | x = time_interpolator->addr; | |
1678 | return x(); | |
1679 | ||
1680 | case TIME_SOURCE_MMIO64 : | |
685db65e | 1681 | return readq_relaxed((void __iomem *)time_interpolator->addr); |
1da177e4 LT |
1682 | |
1683 | case TIME_SOURCE_MMIO32 : | |
685db65e | 1684 | return readl_relaxed((void __iomem *)time_interpolator->addr); |
1da177e4 LT |
1685 | |
1686 | default: return get_cycles(); | |
1687 | } | |
1688 | } | |
1689 | ||
486d46ae | 1690 | static inline u64 time_interpolator_get_counter(int writelock) |
1da177e4 LT |
1691 | { |
1692 | unsigned int src = time_interpolator->source; | |
1693 | ||
1694 | if (time_interpolator->jitter) | |
1695 | { | |
3db5db4f HD |
1696 | cycles_t lcycle; |
1697 | cycles_t now; | |
1da177e4 LT |
1698 | |
1699 | do { | |
1700 | lcycle = time_interpolator->last_cycle; | |
1701 | now = time_interpolator_get_cycles(src); | |
1702 | if (lcycle && time_after(lcycle, now)) | |
1703 | return lcycle; | |
486d46ae AW |
1704 | |
1705 | /* When holding the xtime write lock, there's no need | |
1706 | * to add the overhead of the cmpxchg. Readers are | |
1707 | * force to retry until the write lock is released. | |
1708 | */ | |
1709 | if (writelock) { | |
1710 | time_interpolator->last_cycle = now; | |
1711 | return now; | |
1712 | } | |
1da177e4 LT |
1713 | /* Keep track of the last timer value returned. The use of cmpxchg here |
1714 | * will cause contention in an SMP environment. | |
1715 | */ | |
1716 | } while (unlikely(cmpxchg(&time_interpolator->last_cycle, lcycle, now) != lcycle)); | |
1717 | return now; | |
1718 | } | |
1719 | else | |
1720 | return time_interpolator_get_cycles(src); | |
1721 | } | |
1722 | ||
1723 | void time_interpolator_reset(void) | |
1724 | { | |
1725 | time_interpolator->offset = 0; | |
486d46ae | 1726 | time_interpolator->last_counter = time_interpolator_get_counter(1); |
1da177e4 LT |
1727 | } |
1728 | ||
1729 | #define GET_TI_NSECS(count,i) (((((count) - i->last_counter) & (i)->mask) * (i)->nsec_per_cyc) >> (i)->shift) | |
1730 | ||
1731 | unsigned long time_interpolator_get_offset(void) | |
1732 | { | |
1733 | /* If we do not have a time interpolator set up then just return zero */ | |
1734 | if (!time_interpolator) | |
1735 | return 0; | |
1736 | ||
1737 | return time_interpolator->offset + | |
486d46ae | 1738 | GET_TI_NSECS(time_interpolator_get_counter(0), time_interpolator); |
1da177e4 LT |
1739 | } |
1740 | ||
1741 | #define INTERPOLATOR_ADJUST 65536 | |
1742 | #define INTERPOLATOR_MAX_SKIP 10*INTERPOLATOR_ADJUST | |
1743 | ||
4c7ee8de | 1744 | void time_interpolator_update(long delta_nsec) |
1da177e4 LT |
1745 | { |
1746 | u64 counter; | |
1747 | unsigned long offset; | |
1748 | ||
1749 | /* If there is no time interpolator set up then do nothing */ | |
1750 | if (!time_interpolator) | |
1751 | return; | |
1752 | ||
a5a0d52c AM |
1753 | /* |
1754 | * The interpolator compensates for late ticks by accumulating the late | |
1755 | * time in time_interpolator->offset. A tick earlier than expected will | |
1756 | * lead to a reset of the offset and a corresponding jump of the clock | |
1757 | * forward. Again this only works if the interpolator clock is running | |
1758 | * slightly slower than the regular clock and the tuning logic insures | |
1759 | * that. | |
1760 | */ | |
1da177e4 | 1761 | |
486d46ae | 1762 | counter = time_interpolator_get_counter(1); |
a5a0d52c AM |
1763 | offset = time_interpolator->offset + |
1764 | GET_TI_NSECS(counter, time_interpolator); | |
1da177e4 LT |
1765 | |
1766 | if (delta_nsec < 0 || (unsigned long) delta_nsec < offset) | |
1767 | time_interpolator->offset = offset - delta_nsec; | |
1768 | else { | |
1769 | time_interpolator->skips++; | |
1770 | time_interpolator->ns_skipped += delta_nsec - offset; | |
1771 | time_interpolator->offset = 0; | |
1772 | } | |
1773 | time_interpolator->last_counter = counter; | |
1774 | ||
1775 | /* Tuning logic for time interpolator invoked every minute or so. | |
1776 | * Decrease interpolator clock speed if no skips occurred and an offset is carried. | |
1777 | * Increase interpolator clock speed if we skip too much time. | |
1778 | */ | |
1779 | if (jiffies % INTERPOLATOR_ADJUST == 0) | |
1780 | { | |
b20367a6 | 1781 | if (time_interpolator->skips == 0 && time_interpolator->offset > tick_nsec) |
1da177e4 LT |
1782 | time_interpolator->nsec_per_cyc--; |
1783 | if (time_interpolator->ns_skipped > INTERPOLATOR_MAX_SKIP && time_interpolator->offset == 0) | |
1784 | time_interpolator->nsec_per_cyc++; | |
1785 | time_interpolator->skips = 0; | |
1786 | time_interpolator->ns_skipped = 0; | |
1787 | } | |
1788 | } | |
1789 | ||
1790 | static inline int | |
1791 | is_better_time_interpolator(struct time_interpolator *new) | |
1792 | { | |
1793 | if (!time_interpolator) | |
1794 | return 1; | |
1795 | return new->frequency > 2*time_interpolator->frequency || | |
1796 | (unsigned long)new->drift < (unsigned long)time_interpolator->drift; | |
1797 | } | |
1798 | ||
1799 | void | |
1800 | register_time_interpolator(struct time_interpolator *ti) | |
1801 | { | |
1802 | unsigned long flags; | |
1803 | ||
1804 | /* Sanity check */ | |
9f31252c | 1805 | BUG_ON(ti->frequency == 0 || ti->mask == 0); |
1da177e4 LT |
1806 | |
1807 | ti->nsec_per_cyc = ((u64)NSEC_PER_SEC << ti->shift) / ti->frequency; | |
1808 | spin_lock(&time_interpolator_lock); | |
1809 | write_seqlock_irqsave(&xtime_lock, flags); | |
1810 | if (is_better_time_interpolator(ti)) { | |
1811 | time_interpolator = ti; | |
1812 | time_interpolator_reset(); | |
1813 | } | |
1814 | write_sequnlock_irqrestore(&xtime_lock, flags); | |
1815 | ||
1816 | ti->next = time_interpolator_list; | |
1817 | time_interpolator_list = ti; | |
1818 | spin_unlock(&time_interpolator_lock); | |
1819 | } | |
1820 | ||
1821 | void | |
1822 | unregister_time_interpolator(struct time_interpolator *ti) | |
1823 | { | |
1824 | struct time_interpolator *curr, **prev; | |
1825 | unsigned long flags; | |
1826 | ||
1827 | spin_lock(&time_interpolator_lock); | |
1828 | prev = &time_interpolator_list; | |
1829 | for (curr = *prev; curr; curr = curr->next) { | |
1830 | if (curr == ti) { | |
1831 | *prev = curr->next; | |
1832 | break; | |
1833 | } | |
1834 | prev = &curr->next; | |
1835 | } | |
1836 | ||
1837 | write_seqlock_irqsave(&xtime_lock, flags); | |
1838 | if (ti == time_interpolator) { | |
1839 | /* we lost the best time-interpolator: */ | |
1840 | time_interpolator = NULL; | |
1841 | /* find the next-best interpolator */ | |
1842 | for (curr = time_interpolator_list; curr; curr = curr->next) | |
1843 | if (is_better_time_interpolator(curr)) | |
1844 | time_interpolator = curr; | |
1845 | time_interpolator_reset(); | |
1846 | } | |
1847 | write_sequnlock_irqrestore(&xtime_lock, flags); | |
1848 | spin_unlock(&time_interpolator_lock); | |
1849 | } | |
1850 | #endif /* CONFIG_TIME_INTERPOLATION */ | |
1851 | ||
1852 | /** | |
1853 | * msleep - sleep safely even with waitqueue interruptions | |
1854 | * @msecs: Time in milliseconds to sleep for | |
1855 | */ | |
1856 | void msleep(unsigned int msecs) | |
1857 | { | |
1858 | unsigned long timeout = msecs_to_jiffies(msecs) + 1; | |
1859 | ||
75bcc8c5 NA |
1860 | while (timeout) |
1861 | timeout = schedule_timeout_uninterruptible(timeout); | |
1da177e4 LT |
1862 | } |
1863 | ||
1864 | EXPORT_SYMBOL(msleep); | |
1865 | ||
1866 | /** | |
96ec3efd | 1867 | * msleep_interruptible - sleep waiting for signals |
1da177e4 LT |
1868 | * @msecs: Time in milliseconds to sleep for |
1869 | */ | |
1870 | unsigned long msleep_interruptible(unsigned int msecs) | |
1871 | { | |
1872 | unsigned long timeout = msecs_to_jiffies(msecs) + 1; | |
1873 | ||
75bcc8c5 NA |
1874 | while (timeout && !signal_pending(current)) |
1875 | timeout = schedule_timeout_interruptible(timeout); | |
1da177e4 LT |
1876 | return jiffies_to_msecs(timeout); |
1877 | } | |
1878 | ||
1879 | EXPORT_SYMBOL(msleep_interruptible); |