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