]>
Commit | Line | Data |
---|---|---|
c0a31329 TG |
1 | /* |
2 | * linux/kernel/hrtimer.c | |
3 | * | |
3c8aa39d | 4 | * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de> |
79bf2bb3 | 5 | * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar |
54cdfdb4 | 6 | * Copyright(C) 2006-2007 Timesys Corp., Thomas Gleixner |
c0a31329 TG |
7 | * |
8 | * High-resolution kernel timers | |
9 | * | |
10 | * In contrast to the low-resolution timeout API implemented in | |
11 | * kernel/timer.c, hrtimers provide finer resolution and accuracy | |
12 | * depending on system configuration and capabilities. | |
13 | * | |
14 | * These timers are currently used for: | |
15 | * - itimers | |
16 | * - POSIX timers | |
17 | * - nanosleep | |
18 | * - precise in-kernel timing | |
19 | * | |
20 | * Started by: Thomas Gleixner and Ingo Molnar | |
21 | * | |
22 | * Credits: | |
23 | * based on kernel/timer.c | |
24 | * | |
66188fae TG |
25 | * Help, testing, suggestions, bugfixes, improvements were |
26 | * provided by: | |
27 | * | |
28 | * George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel | |
29 | * et. al. | |
30 | * | |
c0a31329 TG |
31 | * For licencing details see kernel-base/COPYING |
32 | */ | |
33 | ||
34 | #include <linux/cpu.h> | |
35 | #include <linux/module.h> | |
36 | #include <linux/percpu.h> | |
37 | #include <linux/hrtimer.h> | |
38 | #include <linux/notifier.h> | |
39 | #include <linux/syscalls.h> | |
54cdfdb4 | 40 | #include <linux/kallsyms.h> |
c0a31329 | 41 | #include <linux/interrupt.h> |
79bf2bb3 | 42 | #include <linux/tick.h> |
54cdfdb4 TG |
43 | #include <linux/seq_file.h> |
44 | #include <linux/err.h> | |
237fc6e7 | 45 | #include <linux/debugobjects.h> |
eea08f32 AB |
46 | #include <linux/sched.h> |
47 | #include <linux/timer.h> | |
c0a31329 TG |
48 | |
49 | #include <asm/uaccess.h> | |
50 | ||
c0a31329 TG |
51 | /* |
52 | * The timer bases: | |
7978672c GA |
53 | * |
54 | * Note: If we want to add new timer bases, we have to skip the two | |
55 | * clock ids captured by the cpu-timers. We do this by holding empty | |
56 | * entries rather than doing math adjustment of the clock ids. | |
57 | * This ensures that we capture erroneous accesses to these clock ids | |
58 | * rather than moving them into the range of valid clock id's. | |
c0a31329 | 59 | */ |
54cdfdb4 | 60 | DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) = |
c0a31329 | 61 | { |
3c8aa39d TG |
62 | |
63 | .clock_base = | |
c0a31329 | 64 | { |
3c8aa39d TG |
65 | { |
66 | .index = CLOCK_REALTIME, | |
67 | .get_time = &ktime_get_real, | |
54cdfdb4 | 68 | .resolution = KTIME_LOW_RES, |
3c8aa39d TG |
69 | }, |
70 | { | |
71 | .index = CLOCK_MONOTONIC, | |
72 | .get_time = &ktime_get, | |
54cdfdb4 | 73 | .resolution = KTIME_LOW_RES, |
3c8aa39d TG |
74 | }, |
75 | } | |
c0a31329 TG |
76 | }; |
77 | ||
92127c7a TG |
78 | /* |
79 | * Get the coarse grained time at the softirq based on xtime and | |
80 | * wall_to_monotonic. | |
81 | */ | |
3c8aa39d | 82 | static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base) |
92127c7a TG |
83 | { |
84 | ktime_t xtim, tomono; | |
ad28d94a | 85 | struct timespec xts, tom; |
92127c7a TG |
86 | unsigned long seq; |
87 | ||
88 | do { | |
89 | seq = read_seqbegin(&xtime_lock); | |
2c6b47de | 90 | xts = current_kernel_time(); |
ad28d94a | 91 | tom = wall_to_monotonic; |
92127c7a TG |
92 | } while (read_seqretry(&xtime_lock, seq)); |
93 | ||
f4304ab2 | 94 | xtim = timespec_to_ktime(xts); |
ad28d94a | 95 | tomono = timespec_to_ktime(tom); |
3c8aa39d TG |
96 | base->clock_base[CLOCK_REALTIME].softirq_time = xtim; |
97 | base->clock_base[CLOCK_MONOTONIC].softirq_time = | |
98 | ktime_add(xtim, tomono); | |
92127c7a TG |
99 | } |
100 | ||
c0a31329 TG |
101 | /* |
102 | * Functions and macros which are different for UP/SMP systems are kept in a | |
103 | * single place | |
104 | */ | |
105 | #ifdef CONFIG_SMP | |
106 | ||
c0a31329 TG |
107 | /* |
108 | * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock | |
109 | * means that all timers which are tied to this base via timer->base are | |
110 | * locked, and the base itself is locked too. | |
111 | * | |
112 | * So __run_timers/migrate_timers can safely modify all timers which could | |
113 | * be found on the lists/queues. | |
114 | * | |
115 | * When the timer's base is locked, and the timer removed from list, it is | |
116 | * possible to set timer->base = NULL and drop the lock: the timer remains | |
117 | * locked. | |
118 | */ | |
3c8aa39d TG |
119 | static |
120 | struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer, | |
121 | unsigned long *flags) | |
c0a31329 | 122 | { |
3c8aa39d | 123 | struct hrtimer_clock_base *base; |
c0a31329 TG |
124 | |
125 | for (;;) { | |
126 | base = timer->base; | |
127 | if (likely(base != NULL)) { | |
3c8aa39d | 128 | spin_lock_irqsave(&base->cpu_base->lock, *flags); |
c0a31329 TG |
129 | if (likely(base == timer->base)) |
130 | return base; | |
131 | /* The timer has migrated to another CPU: */ | |
3c8aa39d | 132 | spin_unlock_irqrestore(&base->cpu_base->lock, *flags); |
c0a31329 TG |
133 | } |
134 | cpu_relax(); | |
135 | } | |
136 | } | |
137 | ||
6ff7041d TG |
138 | |
139 | /* | |
140 | * Get the preferred target CPU for NOHZ | |
141 | */ | |
142 | static int hrtimer_get_target(int this_cpu, int pinned) | |
143 | { | |
144 | #ifdef CONFIG_NO_HZ | |
145 | if (!pinned && get_sysctl_timer_migration() && idle_cpu(this_cpu)) { | |
146 | int preferred_cpu = get_nohz_load_balancer(); | |
147 | ||
148 | if (preferred_cpu >= 0) | |
149 | return preferred_cpu; | |
150 | } | |
151 | #endif | |
152 | return this_cpu; | |
153 | } | |
154 | ||
155 | /* | |
156 | * With HIGHRES=y we do not migrate the timer when it is expiring | |
157 | * before the next event on the target cpu because we cannot reprogram | |
158 | * the target cpu hardware and we would cause it to fire late. | |
159 | * | |
160 | * Called with cpu_base->lock of target cpu held. | |
161 | */ | |
162 | static int | |
163 | hrtimer_check_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base) | |
164 | { | |
165 | #ifdef CONFIG_HIGH_RES_TIMERS | |
166 | ktime_t expires; | |
167 | ||
168 | if (!new_base->cpu_base->hres_active) | |
169 | return 0; | |
170 | ||
171 | expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset); | |
172 | return expires.tv64 <= new_base->cpu_base->expires_next.tv64; | |
173 | #else | |
174 | return 0; | |
175 | #endif | |
176 | } | |
177 | ||
c0a31329 TG |
178 | /* |
179 | * Switch the timer base to the current CPU when possible. | |
180 | */ | |
3c8aa39d | 181 | static inline struct hrtimer_clock_base * |
597d0275 AB |
182 | switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base, |
183 | int pinned) | |
c0a31329 | 184 | { |
3c8aa39d TG |
185 | struct hrtimer_clock_base *new_base; |
186 | struct hrtimer_cpu_base *new_cpu_base; | |
6ff7041d TG |
187 | int this_cpu = smp_processor_id(); |
188 | int cpu = hrtimer_get_target(this_cpu, pinned); | |
c0a31329 | 189 | |
eea08f32 AB |
190 | again: |
191 | new_cpu_base = &per_cpu(hrtimer_bases, cpu); | |
3c8aa39d | 192 | new_base = &new_cpu_base->clock_base[base->index]; |
c0a31329 TG |
193 | |
194 | if (base != new_base) { | |
195 | /* | |
6ff7041d | 196 | * We are trying to move timer to new_base. |
c0a31329 TG |
197 | * However we can't change timer's base while it is running, |
198 | * so we keep it on the same CPU. No hassle vs. reprogramming | |
199 | * the event source in the high resolution case. The softirq | |
200 | * code will take care of this when the timer function has | |
201 | * completed. There is no conflict as we hold the lock until | |
202 | * the timer is enqueued. | |
203 | */ | |
54cdfdb4 | 204 | if (unlikely(hrtimer_callback_running(timer))) |
c0a31329 TG |
205 | return base; |
206 | ||
207 | /* See the comment in lock_timer_base() */ | |
208 | timer->base = NULL; | |
3c8aa39d TG |
209 | spin_unlock(&base->cpu_base->lock); |
210 | spin_lock(&new_base->cpu_base->lock); | |
eea08f32 | 211 | |
6ff7041d TG |
212 | if (cpu != this_cpu && hrtimer_check_target(timer, new_base)) { |
213 | cpu = this_cpu; | |
214 | spin_unlock(&new_base->cpu_base->lock); | |
215 | spin_lock(&base->cpu_base->lock); | |
216 | timer->base = base; | |
217 | goto again; | |
eea08f32 | 218 | } |
c0a31329 TG |
219 | timer->base = new_base; |
220 | } | |
221 | return new_base; | |
222 | } | |
223 | ||
224 | #else /* CONFIG_SMP */ | |
225 | ||
3c8aa39d | 226 | static inline struct hrtimer_clock_base * |
c0a31329 TG |
227 | lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags) |
228 | { | |
3c8aa39d | 229 | struct hrtimer_clock_base *base = timer->base; |
c0a31329 | 230 | |
3c8aa39d | 231 | spin_lock_irqsave(&base->cpu_base->lock, *flags); |
c0a31329 TG |
232 | |
233 | return base; | |
234 | } | |
235 | ||
eea08f32 | 236 | # define switch_hrtimer_base(t, b, p) (b) |
c0a31329 TG |
237 | |
238 | #endif /* !CONFIG_SMP */ | |
239 | ||
240 | /* | |
241 | * Functions for the union type storage format of ktime_t which are | |
242 | * too large for inlining: | |
243 | */ | |
244 | #if BITS_PER_LONG < 64 | |
245 | # ifndef CONFIG_KTIME_SCALAR | |
246 | /** | |
247 | * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable | |
c0a31329 TG |
248 | * @kt: addend |
249 | * @nsec: the scalar nsec value to add | |
250 | * | |
251 | * Returns the sum of kt and nsec in ktime_t format | |
252 | */ | |
253 | ktime_t ktime_add_ns(const ktime_t kt, u64 nsec) | |
254 | { | |
255 | ktime_t tmp; | |
256 | ||
257 | if (likely(nsec < NSEC_PER_SEC)) { | |
258 | tmp.tv64 = nsec; | |
259 | } else { | |
260 | unsigned long rem = do_div(nsec, NSEC_PER_SEC); | |
261 | ||
262 | tmp = ktime_set((long)nsec, rem); | |
263 | } | |
264 | ||
265 | return ktime_add(kt, tmp); | |
266 | } | |
b8b8fd2d DH |
267 | |
268 | EXPORT_SYMBOL_GPL(ktime_add_ns); | |
a272378d ACM |
269 | |
270 | /** | |
271 | * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable | |
272 | * @kt: minuend | |
273 | * @nsec: the scalar nsec value to subtract | |
274 | * | |
275 | * Returns the subtraction of @nsec from @kt in ktime_t format | |
276 | */ | |
277 | ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec) | |
278 | { | |
279 | ktime_t tmp; | |
280 | ||
281 | if (likely(nsec < NSEC_PER_SEC)) { | |
282 | tmp.tv64 = nsec; | |
283 | } else { | |
284 | unsigned long rem = do_div(nsec, NSEC_PER_SEC); | |
285 | ||
286 | tmp = ktime_set((long)nsec, rem); | |
287 | } | |
288 | ||
289 | return ktime_sub(kt, tmp); | |
290 | } | |
291 | ||
292 | EXPORT_SYMBOL_GPL(ktime_sub_ns); | |
c0a31329 TG |
293 | # endif /* !CONFIG_KTIME_SCALAR */ |
294 | ||
295 | /* | |
296 | * Divide a ktime value by a nanosecond value | |
297 | */ | |
4d672e7a | 298 | u64 ktime_divns(const ktime_t kt, s64 div) |
c0a31329 | 299 | { |
900cfa46 | 300 | u64 dclc; |
c0a31329 TG |
301 | int sft = 0; |
302 | ||
900cfa46 | 303 | dclc = ktime_to_ns(kt); |
c0a31329 TG |
304 | /* Make sure the divisor is less than 2^32: */ |
305 | while (div >> 32) { | |
306 | sft++; | |
307 | div >>= 1; | |
308 | } | |
309 | dclc >>= sft; | |
310 | do_div(dclc, (unsigned long) div); | |
311 | ||
4d672e7a | 312 | return dclc; |
c0a31329 | 313 | } |
c0a31329 TG |
314 | #endif /* BITS_PER_LONG >= 64 */ |
315 | ||
5a7780e7 TG |
316 | /* |
317 | * Add two ktime values and do a safety check for overflow: | |
318 | */ | |
319 | ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs) | |
320 | { | |
321 | ktime_t res = ktime_add(lhs, rhs); | |
322 | ||
323 | /* | |
324 | * We use KTIME_SEC_MAX here, the maximum timeout which we can | |
325 | * return to user space in a timespec: | |
326 | */ | |
327 | if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64) | |
328 | res = ktime_set(KTIME_SEC_MAX, 0); | |
329 | ||
330 | return res; | |
331 | } | |
332 | ||
8daa21e6 AB |
333 | EXPORT_SYMBOL_GPL(ktime_add_safe); |
334 | ||
237fc6e7 TG |
335 | #ifdef CONFIG_DEBUG_OBJECTS_TIMERS |
336 | ||
337 | static struct debug_obj_descr hrtimer_debug_descr; | |
338 | ||
339 | /* | |
340 | * fixup_init is called when: | |
341 | * - an active object is initialized | |
342 | */ | |
343 | static int hrtimer_fixup_init(void *addr, enum debug_obj_state state) | |
344 | { | |
345 | struct hrtimer *timer = addr; | |
346 | ||
347 | switch (state) { | |
348 | case ODEBUG_STATE_ACTIVE: | |
349 | hrtimer_cancel(timer); | |
350 | debug_object_init(timer, &hrtimer_debug_descr); | |
351 | return 1; | |
352 | default: | |
353 | return 0; | |
354 | } | |
355 | } | |
356 | ||
357 | /* | |
358 | * fixup_activate is called when: | |
359 | * - an active object is activated | |
360 | * - an unknown object is activated (might be a statically initialized object) | |
361 | */ | |
362 | static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state) | |
363 | { | |
364 | switch (state) { | |
365 | ||
366 | case ODEBUG_STATE_NOTAVAILABLE: | |
367 | WARN_ON_ONCE(1); | |
368 | return 0; | |
369 | ||
370 | case ODEBUG_STATE_ACTIVE: | |
371 | WARN_ON(1); | |
372 | ||
373 | default: | |
374 | return 0; | |
375 | } | |
376 | } | |
377 | ||
378 | /* | |
379 | * fixup_free is called when: | |
380 | * - an active object is freed | |
381 | */ | |
382 | static int hrtimer_fixup_free(void *addr, enum debug_obj_state state) | |
383 | { | |
384 | struct hrtimer *timer = addr; | |
385 | ||
386 | switch (state) { | |
387 | case ODEBUG_STATE_ACTIVE: | |
388 | hrtimer_cancel(timer); | |
389 | debug_object_free(timer, &hrtimer_debug_descr); | |
390 | return 1; | |
391 | default: | |
392 | return 0; | |
393 | } | |
394 | } | |
395 | ||
396 | static struct debug_obj_descr hrtimer_debug_descr = { | |
397 | .name = "hrtimer", | |
398 | .fixup_init = hrtimer_fixup_init, | |
399 | .fixup_activate = hrtimer_fixup_activate, | |
400 | .fixup_free = hrtimer_fixup_free, | |
401 | }; | |
402 | ||
403 | static inline void debug_hrtimer_init(struct hrtimer *timer) | |
404 | { | |
405 | debug_object_init(timer, &hrtimer_debug_descr); | |
406 | } | |
407 | ||
408 | static inline void debug_hrtimer_activate(struct hrtimer *timer) | |
409 | { | |
410 | debug_object_activate(timer, &hrtimer_debug_descr); | |
411 | } | |
412 | ||
413 | static inline void debug_hrtimer_deactivate(struct hrtimer *timer) | |
414 | { | |
415 | debug_object_deactivate(timer, &hrtimer_debug_descr); | |
416 | } | |
417 | ||
418 | static inline void debug_hrtimer_free(struct hrtimer *timer) | |
419 | { | |
420 | debug_object_free(timer, &hrtimer_debug_descr); | |
421 | } | |
422 | ||
423 | static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id, | |
424 | enum hrtimer_mode mode); | |
425 | ||
426 | void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id, | |
427 | enum hrtimer_mode mode) | |
428 | { | |
429 | debug_object_init_on_stack(timer, &hrtimer_debug_descr); | |
430 | __hrtimer_init(timer, clock_id, mode); | |
431 | } | |
2bc481cf | 432 | EXPORT_SYMBOL_GPL(hrtimer_init_on_stack); |
237fc6e7 TG |
433 | |
434 | void destroy_hrtimer_on_stack(struct hrtimer *timer) | |
435 | { | |
436 | debug_object_free(timer, &hrtimer_debug_descr); | |
437 | } | |
438 | ||
439 | #else | |
440 | static inline void debug_hrtimer_init(struct hrtimer *timer) { } | |
441 | static inline void debug_hrtimer_activate(struct hrtimer *timer) { } | |
442 | static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { } | |
443 | #endif | |
444 | ||
54cdfdb4 TG |
445 | /* High resolution timer related functions */ |
446 | #ifdef CONFIG_HIGH_RES_TIMERS | |
447 | ||
448 | /* | |
449 | * High resolution timer enabled ? | |
450 | */ | |
451 | static int hrtimer_hres_enabled __read_mostly = 1; | |
452 | ||
453 | /* | |
454 | * Enable / Disable high resolution mode | |
455 | */ | |
456 | static int __init setup_hrtimer_hres(char *str) | |
457 | { | |
458 | if (!strcmp(str, "off")) | |
459 | hrtimer_hres_enabled = 0; | |
460 | else if (!strcmp(str, "on")) | |
461 | hrtimer_hres_enabled = 1; | |
462 | else | |
463 | return 0; | |
464 | return 1; | |
465 | } | |
466 | ||
467 | __setup("highres=", setup_hrtimer_hres); | |
468 | ||
469 | /* | |
470 | * hrtimer_high_res_enabled - query, if the highres mode is enabled | |
471 | */ | |
472 | static inline int hrtimer_is_hres_enabled(void) | |
473 | { | |
474 | return hrtimer_hres_enabled; | |
475 | } | |
476 | ||
477 | /* | |
478 | * Is the high resolution mode active ? | |
479 | */ | |
480 | static inline int hrtimer_hres_active(void) | |
481 | { | |
482 | return __get_cpu_var(hrtimer_bases).hres_active; | |
483 | } | |
484 | ||
485 | /* | |
486 | * Reprogram the event source with checking both queues for the | |
487 | * next event | |
488 | * Called with interrupts disabled and base->lock held | |
489 | */ | |
490 | static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base) | |
491 | { | |
492 | int i; | |
493 | struct hrtimer_clock_base *base = cpu_base->clock_base; | |
494 | ktime_t expires; | |
495 | ||
496 | cpu_base->expires_next.tv64 = KTIME_MAX; | |
497 | ||
498 | for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) { | |
499 | struct hrtimer *timer; | |
500 | ||
501 | if (!base->first) | |
502 | continue; | |
503 | timer = rb_entry(base->first, struct hrtimer, node); | |
cc584b21 | 504 | expires = ktime_sub(hrtimer_get_expires(timer), base->offset); |
b0a9b511 TG |
505 | /* |
506 | * clock_was_set() has changed base->offset so the | |
507 | * result might be negative. Fix it up to prevent a | |
508 | * false positive in clockevents_program_event() | |
509 | */ | |
510 | if (expires.tv64 < 0) | |
511 | expires.tv64 = 0; | |
54cdfdb4 TG |
512 | if (expires.tv64 < cpu_base->expires_next.tv64) |
513 | cpu_base->expires_next = expires; | |
514 | } | |
515 | ||
516 | if (cpu_base->expires_next.tv64 != KTIME_MAX) | |
517 | tick_program_event(cpu_base->expires_next, 1); | |
518 | } | |
519 | ||
520 | /* | |
521 | * Shared reprogramming for clock_realtime and clock_monotonic | |
522 | * | |
523 | * When a timer is enqueued and expires earlier than the already enqueued | |
524 | * timers, we have to check, whether it expires earlier than the timer for | |
525 | * which the clock event device was armed. | |
526 | * | |
527 | * Called with interrupts disabled and base->cpu_base.lock held | |
528 | */ | |
529 | static int hrtimer_reprogram(struct hrtimer *timer, | |
530 | struct hrtimer_clock_base *base) | |
531 | { | |
532 | ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next; | |
cc584b21 | 533 | ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset); |
54cdfdb4 TG |
534 | int res; |
535 | ||
cc584b21 | 536 | WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0); |
63070a79 | 537 | |
54cdfdb4 TG |
538 | /* |
539 | * When the callback is running, we do not reprogram the clock event | |
540 | * device. The timer callback is either running on a different CPU or | |
3a4fa0a2 | 541 | * the callback is executed in the hrtimer_interrupt context. The |
54cdfdb4 TG |
542 | * reprogramming is handled either by the softirq, which called the |
543 | * callback or at the end of the hrtimer_interrupt. | |
544 | */ | |
545 | if (hrtimer_callback_running(timer)) | |
546 | return 0; | |
547 | ||
63070a79 TG |
548 | /* |
549 | * CLOCK_REALTIME timer might be requested with an absolute | |
550 | * expiry time which is less than base->offset. Nothing wrong | |
551 | * about that, just avoid to call into the tick code, which | |
552 | * has now objections against negative expiry values. | |
553 | */ | |
554 | if (expires.tv64 < 0) | |
555 | return -ETIME; | |
556 | ||
54cdfdb4 TG |
557 | if (expires.tv64 >= expires_next->tv64) |
558 | return 0; | |
559 | ||
560 | /* | |
561 | * Clockevents returns -ETIME, when the event was in the past. | |
562 | */ | |
563 | res = tick_program_event(expires, 0); | |
564 | if (!IS_ERR_VALUE(res)) | |
565 | *expires_next = expires; | |
566 | return res; | |
567 | } | |
568 | ||
569 | ||
570 | /* | |
571 | * Retrigger next event is called after clock was set | |
572 | * | |
573 | * Called with interrupts disabled via on_each_cpu() | |
574 | */ | |
575 | static void retrigger_next_event(void *arg) | |
576 | { | |
577 | struct hrtimer_cpu_base *base; | |
578 | struct timespec realtime_offset; | |
579 | unsigned long seq; | |
580 | ||
581 | if (!hrtimer_hres_active()) | |
582 | return; | |
583 | ||
584 | do { | |
585 | seq = read_seqbegin(&xtime_lock); | |
586 | set_normalized_timespec(&realtime_offset, | |
587 | -wall_to_monotonic.tv_sec, | |
588 | -wall_to_monotonic.tv_nsec); | |
589 | } while (read_seqretry(&xtime_lock, seq)); | |
590 | ||
591 | base = &__get_cpu_var(hrtimer_bases); | |
592 | ||
593 | /* Adjust CLOCK_REALTIME offset */ | |
594 | spin_lock(&base->lock); | |
595 | base->clock_base[CLOCK_REALTIME].offset = | |
596 | timespec_to_ktime(realtime_offset); | |
597 | ||
598 | hrtimer_force_reprogram(base); | |
599 | spin_unlock(&base->lock); | |
600 | } | |
601 | ||
602 | /* | |
603 | * Clock realtime was set | |
604 | * | |
605 | * Change the offset of the realtime clock vs. the monotonic | |
606 | * clock. | |
607 | * | |
608 | * We might have to reprogram the high resolution timer interrupt. On | |
609 | * SMP we call the architecture specific code to retrigger _all_ high | |
610 | * resolution timer interrupts. On UP we just disable interrupts and | |
611 | * call the high resolution interrupt code. | |
612 | */ | |
613 | void clock_was_set(void) | |
614 | { | |
615 | /* Retrigger the CPU local events everywhere */ | |
15c8b6c1 | 616 | on_each_cpu(retrigger_next_event, NULL, 1); |
54cdfdb4 TG |
617 | } |
618 | ||
995f054f IM |
619 | /* |
620 | * During resume we might have to reprogram the high resolution timer | |
621 | * interrupt (on the local CPU): | |
622 | */ | |
623 | void hres_timers_resume(void) | |
624 | { | |
1d4a7f1c PZ |
625 | WARN_ONCE(!irqs_disabled(), |
626 | KERN_INFO "hres_timers_resume() called with IRQs enabled!"); | |
627 | ||
995f054f IM |
628 | retrigger_next_event(NULL); |
629 | } | |
630 | ||
54cdfdb4 TG |
631 | /* |
632 | * Initialize the high resolution related parts of cpu_base | |
633 | */ | |
634 | static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) | |
635 | { | |
636 | base->expires_next.tv64 = KTIME_MAX; | |
637 | base->hres_active = 0; | |
54cdfdb4 TG |
638 | } |
639 | ||
640 | /* | |
641 | * Initialize the high resolution related parts of a hrtimer | |
642 | */ | |
643 | static inline void hrtimer_init_timer_hres(struct hrtimer *timer) | |
644 | { | |
54cdfdb4 TG |
645 | } |
646 | ||
ca109491 | 647 | |
54cdfdb4 TG |
648 | /* |
649 | * When High resolution timers are active, try to reprogram. Note, that in case | |
650 | * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry | |
651 | * check happens. The timer gets enqueued into the rbtree. The reprogramming | |
652 | * and expiry check is done in the hrtimer_interrupt or in the softirq. | |
653 | */ | |
654 | static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer, | |
7f1e2ca9 PZ |
655 | struct hrtimer_clock_base *base, |
656 | int wakeup) | |
54cdfdb4 TG |
657 | { |
658 | if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) { | |
7f1e2ca9 PZ |
659 | if (wakeup) { |
660 | spin_unlock(&base->cpu_base->lock); | |
661 | raise_softirq_irqoff(HRTIMER_SOFTIRQ); | |
662 | spin_lock(&base->cpu_base->lock); | |
663 | } else | |
664 | __raise_softirq_irqoff(HRTIMER_SOFTIRQ); | |
665 | ||
ca109491 | 666 | return 1; |
54cdfdb4 | 667 | } |
7f1e2ca9 | 668 | |
54cdfdb4 TG |
669 | return 0; |
670 | } | |
671 | ||
672 | /* | |
673 | * Switch to high resolution mode | |
674 | */ | |
f8953856 | 675 | static int hrtimer_switch_to_hres(void) |
54cdfdb4 | 676 | { |
820de5c3 IM |
677 | int cpu = smp_processor_id(); |
678 | struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu); | |
54cdfdb4 TG |
679 | unsigned long flags; |
680 | ||
681 | if (base->hres_active) | |
f8953856 | 682 | return 1; |
54cdfdb4 TG |
683 | |
684 | local_irq_save(flags); | |
685 | ||
686 | if (tick_init_highres()) { | |
687 | local_irq_restore(flags); | |
820de5c3 IM |
688 | printk(KERN_WARNING "Could not switch to high resolution " |
689 | "mode on CPU %d\n", cpu); | |
f8953856 | 690 | return 0; |
54cdfdb4 TG |
691 | } |
692 | base->hres_active = 1; | |
693 | base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES; | |
694 | base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES; | |
695 | ||
696 | tick_setup_sched_timer(); | |
697 | ||
698 | /* "Retrigger" the interrupt to get things going */ | |
699 | retrigger_next_event(NULL); | |
700 | local_irq_restore(flags); | |
edfed66e | 701 | printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n", |
54cdfdb4 | 702 | smp_processor_id()); |
f8953856 | 703 | return 1; |
54cdfdb4 TG |
704 | } |
705 | ||
706 | #else | |
707 | ||
708 | static inline int hrtimer_hres_active(void) { return 0; } | |
709 | static inline int hrtimer_is_hres_enabled(void) { return 0; } | |
f8953856 | 710 | static inline int hrtimer_switch_to_hres(void) { return 0; } |
54cdfdb4 TG |
711 | static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { } |
712 | static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer, | |
7f1e2ca9 PZ |
713 | struct hrtimer_clock_base *base, |
714 | int wakeup) | |
54cdfdb4 TG |
715 | { |
716 | return 0; | |
717 | } | |
54cdfdb4 TG |
718 | static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { } |
719 | static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { } | |
720 | ||
721 | #endif /* CONFIG_HIGH_RES_TIMERS */ | |
722 | ||
82f67cd9 IM |
723 | #ifdef CONFIG_TIMER_STATS |
724 | void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr) | |
725 | { | |
726 | if (timer->start_site) | |
727 | return; | |
728 | ||
729 | timer->start_site = addr; | |
730 | memcpy(timer->start_comm, current->comm, TASK_COMM_LEN); | |
731 | timer->start_pid = current->pid; | |
732 | } | |
733 | #endif | |
734 | ||
c0a31329 | 735 | /* |
6506f2aa | 736 | * Counterpart to lock_hrtimer_base above: |
c0a31329 TG |
737 | */ |
738 | static inline | |
739 | void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags) | |
740 | { | |
3c8aa39d | 741 | spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags); |
c0a31329 TG |
742 | } |
743 | ||
744 | /** | |
745 | * hrtimer_forward - forward the timer expiry | |
c0a31329 | 746 | * @timer: hrtimer to forward |
44f21475 | 747 | * @now: forward past this time |
c0a31329 TG |
748 | * @interval: the interval to forward |
749 | * | |
750 | * Forward the timer expiry so it will expire in the future. | |
8dca6f33 | 751 | * Returns the number of overruns. |
c0a31329 | 752 | */ |
4d672e7a | 753 | u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval) |
c0a31329 | 754 | { |
4d672e7a | 755 | u64 orun = 1; |
44f21475 | 756 | ktime_t delta; |
c0a31329 | 757 | |
cc584b21 | 758 | delta = ktime_sub(now, hrtimer_get_expires(timer)); |
c0a31329 TG |
759 | |
760 | if (delta.tv64 < 0) | |
761 | return 0; | |
762 | ||
c9db4fa1 TG |
763 | if (interval.tv64 < timer->base->resolution.tv64) |
764 | interval.tv64 = timer->base->resolution.tv64; | |
765 | ||
c0a31329 | 766 | if (unlikely(delta.tv64 >= interval.tv64)) { |
df869b63 | 767 | s64 incr = ktime_to_ns(interval); |
c0a31329 TG |
768 | |
769 | orun = ktime_divns(delta, incr); | |
cc584b21 AV |
770 | hrtimer_add_expires_ns(timer, incr * orun); |
771 | if (hrtimer_get_expires_tv64(timer) > now.tv64) | |
c0a31329 TG |
772 | return orun; |
773 | /* | |
774 | * This (and the ktime_add() below) is the | |
775 | * correction for exact: | |
776 | */ | |
777 | orun++; | |
778 | } | |
cc584b21 | 779 | hrtimer_add_expires(timer, interval); |
c0a31329 TG |
780 | |
781 | return orun; | |
782 | } | |
6bdb6b62 | 783 | EXPORT_SYMBOL_GPL(hrtimer_forward); |
c0a31329 TG |
784 | |
785 | /* | |
786 | * enqueue_hrtimer - internal function to (re)start a timer | |
787 | * | |
788 | * The timer is inserted in expiry order. Insertion into the | |
789 | * red black tree is O(log(n)). Must hold the base lock. | |
a6037b61 PZ |
790 | * |
791 | * Returns 1 when the new timer is the leftmost timer in the tree. | |
c0a31329 | 792 | */ |
a6037b61 PZ |
793 | static int enqueue_hrtimer(struct hrtimer *timer, |
794 | struct hrtimer_clock_base *base) | |
c0a31329 TG |
795 | { |
796 | struct rb_node **link = &base->active.rb_node; | |
c0a31329 TG |
797 | struct rb_node *parent = NULL; |
798 | struct hrtimer *entry; | |
99bc2fcb | 799 | int leftmost = 1; |
c0a31329 | 800 | |
237fc6e7 TG |
801 | debug_hrtimer_activate(timer); |
802 | ||
c0a31329 TG |
803 | /* |
804 | * Find the right place in the rbtree: | |
805 | */ | |
806 | while (*link) { | |
807 | parent = *link; | |
808 | entry = rb_entry(parent, struct hrtimer, node); | |
809 | /* | |
810 | * We dont care about collisions. Nodes with | |
811 | * the same expiry time stay together. | |
812 | */ | |
cc584b21 AV |
813 | if (hrtimer_get_expires_tv64(timer) < |
814 | hrtimer_get_expires_tv64(entry)) { | |
c0a31329 | 815 | link = &(*link)->rb_left; |
99bc2fcb | 816 | } else { |
c0a31329 | 817 | link = &(*link)->rb_right; |
99bc2fcb IM |
818 | leftmost = 0; |
819 | } | |
c0a31329 TG |
820 | } |
821 | ||
822 | /* | |
288867ec TG |
823 | * Insert the timer to the rbtree and check whether it |
824 | * replaces the first pending timer | |
c0a31329 | 825 | */ |
a6037b61 | 826 | if (leftmost) |
54cdfdb4 | 827 | base->first = &timer->node; |
54cdfdb4 | 828 | |
c0a31329 TG |
829 | rb_link_node(&timer->node, parent, link); |
830 | rb_insert_color(&timer->node, &base->active); | |
303e967f TG |
831 | /* |
832 | * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the | |
833 | * state of a possibly running callback. | |
834 | */ | |
835 | timer->state |= HRTIMER_STATE_ENQUEUED; | |
a6037b61 PZ |
836 | |
837 | return leftmost; | |
288867ec | 838 | } |
c0a31329 TG |
839 | |
840 | /* | |
841 | * __remove_hrtimer - internal function to remove a timer | |
842 | * | |
843 | * Caller must hold the base lock. | |
54cdfdb4 TG |
844 | * |
845 | * High resolution timer mode reprograms the clock event device when the | |
846 | * timer is the one which expires next. The caller can disable this by setting | |
847 | * reprogram to zero. This is useful, when the context does a reprogramming | |
848 | * anyway (e.g. timer interrupt) | |
c0a31329 | 849 | */ |
3c8aa39d | 850 | static void __remove_hrtimer(struct hrtimer *timer, |
303e967f | 851 | struct hrtimer_clock_base *base, |
54cdfdb4 | 852 | unsigned long newstate, int reprogram) |
c0a31329 | 853 | { |
ca109491 | 854 | if (timer->state & HRTIMER_STATE_ENQUEUED) { |
54cdfdb4 TG |
855 | /* |
856 | * Remove the timer from the rbtree and replace the | |
857 | * first entry pointer if necessary. | |
858 | */ | |
859 | if (base->first == &timer->node) { | |
860 | base->first = rb_next(&timer->node); | |
861 | /* Reprogram the clock event device. if enabled */ | |
862 | if (reprogram && hrtimer_hres_active()) | |
863 | hrtimer_force_reprogram(base->cpu_base); | |
864 | } | |
865 | rb_erase(&timer->node, &base->active); | |
866 | } | |
303e967f | 867 | timer->state = newstate; |
c0a31329 TG |
868 | } |
869 | ||
870 | /* | |
871 | * remove hrtimer, called with base lock held | |
872 | */ | |
873 | static inline int | |
3c8aa39d | 874 | remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base) |
c0a31329 | 875 | { |
303e967f | 876 | if (hrtimer_is_queued(timer)) { |
54cdfdb4 TG |
877 | int reprogram; |
878 | ||
879 | /* | |
880 | * Remove the timer and force reprogramming when high | |
881 | * resolution mode is active and the timer is on the current | |
882 | * CPU. If we remove a timer on another CPU, reprogramming is | |
883 | * skipped. The interrupt event on this CPU is fired and | |
884 | * reprogramming happens in the interrupt handler. This is a | |
885 | * rare case and less expensive than a smp call. | |
886 | */ | |
237fc6e7 | 887 | debug_hrtimer_deactivate(timer); |
82f67cd9 | 888 | timer_stats_hrtimer_clear_start_info(timer); |
54cdfdb4 TG |
889 | reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases); |
890 | __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, | |
891 | reprogram); | |
c0a31329 TG |
892 | return 1; |
893 | } | |
894 | return 0; | |
895 | } | |
896 | ||
7f1e2ca9 PZ |
897 | int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, |
898 | unsigned long delta_ns, const enum hrtimer_mode mode, | |
899 | int wakeup) | |
c0a31329 | 900 | { |
3c8aa39d | 901 | struct hrtimer_clock_base *base, *new_base; |
c0a31329 | 902 | unsigned long flags; |
a6037b61 | 903 | int ret, leftmost; |
c0a31329 TG |
904 | |
905 | base = lock_hrtimer_base(timer, &flags); | |
906 | ||
907 | /* Remove an active timer from the queue: */ | |
908 | ret = remove_hrtimer(timer, base); | |
909 | ||
910 | /* Switch the timer base, if necessary: */ | |
597d0275 | 911 | new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED); |
c0a31329 | 912 | |
597d0275 | 913 | if (mode & HRTIMER_MODE_REL) { |
5a7780e7 | 914 | tim = ktime_add_safe(tim, new_base->get_time()); |
06027bdd IM |
915 | /* |
916 | * CONFIG_TIME_LOW_RES is a temporary way for architectures | |
917 | * to signal that they simply return xtime in | |
918 | * do_gettimeoffset(). In this case we want to round up by | |
919 | * resolution when starting a relative timer, to avoid short | |
920 | * timeouts. This will go away with the GTOD framework. | |
921 | */ | |
922 | #ifdef CONFIG_TIME_LOW_RES | |
5a7780e7 | 923 | tim = ktime_add_safe(tim, base->resolution); |
06027bdd IM |
924 | #endif |
925 | } | |
237fc6e7 | 926 | |
da8f2e17 | 927 | hrtimer_set_expires_range_ns(timer, tim, delta_ns); |
c0a31329 | 928 | |
82f67cd9 IM |
929 | timer_stats_hrtimer_set_start_info(timer); |
930 | ||
a6037b61 PZ |
931 | leftmost = enqueue_hrtimer(timer, new_base); |
932 | ||
935c631d IM |
933 | /* |
934 | * Only allow reprogramming if the new base is on this CPU. | |
935 | * (it might still be on another CPU if the timer was pending) | |
a6037b61 PZ |
936 | * |
937 | * XXX send_remote_softirq() ? | |
935c631d | 938 | */ |
a6037b61 | 939 | if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases)) |
7f1e2ca9 | 940 | hrtimer_enqueue_reprogram(timer, new_base, wakeup); |
c0a31329 TG |
941 | |
942 | unlock_hrtimer_base(timer, &flags); | |
943 | ||
944 | return ret; | |
945 | } | |
7f1e2ca9 PZ |
946 | |
947 | /** | |
948 | * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU | |
949 | * @timer: the timer to be added | |
950 | * @tim: expiry time | |
951 | * @delta_ns: "slack" range for the timer | |
952 | * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL) | |
953 | * | |
954 | * Returns: | |
955 | * 0 on success | |
956 | * 1 when the timer was active | |
957 | */ | |
958 | int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, | |
959 | unsigned long delta_ns, const enum hrtimer_mode mode) | |
960 | { | |
961 | return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1); | |
962 | } | |
da8f2e17 AV |
963 | EXPORT_SYMBOL_GPL(hrtimer_start_range_ns); |
964 | ||
965 | /** | |
e1dd7bc5 | 966 | * hrtimer_start - (re)start an hrtimer on the current CPU |
da8f2e17 AV |
967 | * @timer: the timer to be added |
968 | * @tim: expiry time | |
969 | * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL) | |
970 | * | |
971 | * Returns: | |
972 | * 0 on success | |
973 | * 1 when the timer was active | |
974 | */ | |
975 | int | |
976 | hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode) | |
977 | { | |
7f1e2ca9 | 978 | return __hrtimer_start_range_ns(timer, tim, 0, mode, 1); |
da8f2e17 | 979 | } |
8d16b764 | 980 | EXPORT_SYMBOL_GPL(hrtimer_start); |
c0a31329 | 981 | |
da8f2e17 | 982 | |
c0a31329 TG |
983 | /** |
984 | * hrtimer_try_to_cancel - try to deactivate a timer | |
c0a31329 TG |
985 | * @timer: hrtimer to stop |
986 | * | |
987 | * Returns: | |
988 | * 0 when the timer was not active | |
989 | * 1 when the timer was active | |
990 | * -1 when the timer is currently excuting the callback function and | |
fa9799e3 | 991 | * cannot be stopped |
c0a31329 TG |
992 | */ |
993 | int hrtimer_try_to_cancel(struct hrtimer *timer) | |
994 | { | |
3c8aa39d | 995 | struct hrtimer_clock_base *base; |
c0a31329 TG |
996 | unsigned long flags; |
997 | int ret = -1; | |
998 | ||
999 | base = lock_hrtimer_base(timer, &flags); | |
1000 | ||
303e967f | 1001 | if (!hrtimer_callback_running(timer)) |
c0a31329 TG |
1002 | ret = remove_hrtimer(timer, base); |
1003 | ||
1004 | unlock_hrtimer_base(timer, &flags); | |
1005 | ||
1006 | return ret; | |
1007 | ||
1008 | } | |
8d16b764 | 1009 | EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel); |
c0a31329 TG |
1010 | |
1011 | /** | |
1012 | * hrtimer_cancel - cancel a timer and wait for the handler to finish. | |
c0a31329 TG |
1013 | * @timer: the timer to be cancelled |
1014 | * | |
1015 | * Returns: | |
1016 | * 0 when the timer was not active | |
1017 | * 1 when the timer was active | |
1018 | */ | |
1019 | int hrtimer_cancel(struct hrtimer *timer) | |
1020 | { | |
1021 | for (;;) { | |
1022 | int ret = hrtimer_try_to_cancel(timer); | |
1023 | ||
1024 | if (ret >= 0) | |
1025 | return ret; | |
5ef37b19 | 1026 | cpu_relax(); |
c0a31329 TG |
1027 | } |
1028 | } | |
8d16b764 | 1029 | EXPORT_SYMBOL_GPL(hrtimer_cancel); |
c0a31329 TG |
1030 | |
1031 | /** | |
1032 | * hrtimer_get_remaining - get remaining time for the timer | |
c0a31329 TG |
1033 | * @timer: the timer to read |
1034 | */ | |
1035 | ktime_t hrtimer_get_remaining(const struct hrtimer *timer) | |
1036 | { | |
3c8aa39d | 1037 | struct hrtimer_clock_base *base; |
c0a31329 TG |
1038 | unsigned long flags; |
1039 | ktime_t rem; | |
1040 | ||
1041 | base = lock_hrtimer_base(timer, &flags); | |
cc584b21 | 1042 | rem = hrtimer_expires_remaining(timer); |
c0a31329 TG |
1043 | unlock_hrtimer_base(timer, &flags); |
1044 | ||
1045 | return rem; | |
1046 | } | |
8d16b764 | 1047 | EXPORT_SYMBOL_GPL(hrtimer_get_remaining); |
c0a31329 | 1048 | |
ee9c5785 | 1049 | #ifdef CONFIG_NO_HZ |
69239749 TL |
1050 | /** |
1051 | * hrtimer_get_next_event - get the time until next expiry event | |
1052 | * | |
1053 | * Returns the delta to the next expiry event or KTIME_MAX if no timer | |
1054 | * is pending. | |
1055 | */ | |
1056 | ktime_t hrtimer_get_next_event(void) | |
1057 | { | |
3c8aa39d TG |
1058 | struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases); |
1059 | struct hrtimer_clock_base *base = cpu_base->clock_base; | |
69239749 TL |
1060 | ktime_t delta, mindelta = { .tv64 = KTIME_MAX }; |
1061 | unsigned long flags; | |
1062 | int i; | |
1063 | ||
3c8aa39d TG |
1064 | spin_lock_irqsave(&cpu_base->lock, flags); |
1065 | ||
54cdfdb4 TG |
1066 | if (!hrtimer_hres_active()) { |
1067 | for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) { | |
1068 | struct hrtimer *timer; | |
69239749 | 1069 | |
54cdfdb4 TG |
1070 | if (!base->first) |
1071 | continue; | |
3c8aa39d | 1072 | |
54cdfdb4 | 1073 | timer = rb_entry(base->first, struct hrtimer, node); |
cc584b21 | 1074 | delta.tv64 = hrtimer_get_expires_tv64(timer); |
54cdfdb4 TG |
1075 | delta = ktime_sub(delta, base->get_time()); |
1076 | if (delta.tv64 < mindelta.tv64) | |
1077 | mindelta.tv64 = delta.tv64; | |
1078 | } | |
69239749 | 1079 | } |
3c8aa39d TG |
1080 | |
1081 | spin_unlock_irqrestore(&cpu_base->lock, flags); | |
1082 | ||
69239749 TL |
1083 | if (mindelta.tv64 < 0) |
1084 | mindelta.tv64 = 0; | |
1085 | return mindelta; | |
1086 | } | |
1087 | #endif | |
1088 | ||
237fc6e7 TG |
1089 | static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id, |
1090 | enum hrtimer_mode mode) | |
c0a31329 | 1091 | { |
3c8aa39d | 1092 | struct hrtimer_cpu_base *cpu_base; |
c0a31329 | 1093 | |
7978672c GA |
1094 | memset(timer, 0, sizeof(struct hrtimer)); |
1095 | ||
3c8aa39d | 1096 | cpu_base = &__raw_get_cpu_var(hrtimer_bases); |
c0a31329 | 1097 | |
c9cb2e3d | 1098 | if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS) |
7978672c GA |
1099 | clock_id = CLOCK_MONOTONIC; |
1100 | ||
3c8aa39d | 1101 | timer->base = &cpu_base->clock_base[clock_id]; |
54cdfdb4 | 1102 | hrtimer_init_timer_hres(timer); |
82f67cd9 IM |
1103 | |
1104 | #ifdef CONFIG_TIMER_STATS | |
1105 | timer->start_site = NULL; | |
1106 | timer->start_pid = -1; | |
1107 | memset(timer->start_comm, 0, TASK_COMM_LEN); | |
1108 | #endif | |
c0a31329 | 1109 | } |
237fc6e7 TG |
1110 | |
1111 | /** | |
1112 | * hrtimer_init - initialize a timer to the given clock | |
1113 | * @timer: the timer to be initialized | |
1114 | * @clock_id: the clock to be used | |
1115 | * @mode: timer mode abs/rel | |
1116 | */ | |
1117 | void hrtimer_init(struct hrtimer *timer, clockid_t clock_id, | |
1118 | enum hrtimer_mode mode) | |
1119 | { | |
1120 | debug_hrtimer_init(timer); | |
1121 | __hrtimer_init(timer, clock_id, mode); | |
1122 | } | |
8d16b764 | 1123 | EXPORT_SYMBOL_GPL(hrtimer_init); |
c0a31329 TG |
1124 | |
1125 | /** | |
1126 | * hrtimer_get_res - get the timer resolution for a clock | |
c0a31329 TG |
1127 | * @which_clock: which clock to query |
1128 | * @tp: pointer to timespec variable to store the resolution | |
1129 | * | |
72fd4a35 RD |
1130 | * Store the resolution of the clock selected by @which_clock in the |
1131 | * variable pointed to by @tp. | |
c0a31329 TG |
1132 | */ |
1133 | int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp) | |
1134 | { | |
3c8aa39d | 1135 | struct hrtimer_cpu_base *cpu_base; |
c0a31329 | 1136 | |
3c8aa39d TG |
1137 | cpu_base = &__raw_get_cpu_var(hrtimer_bases); |
1138 | *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution); | |
c0a31329 TG |
1139 | |
1140 | return 0; | |
1141 | } | |
8d16b764 | 1142 | EXPORT_SYMBOL_GPL(hrtimer_get_res); |
c0a31329 | 1143 | |
d3d74453 PZ |
1144 | static void __run_hrtimer(struct hrtimer *timer) |
1145 | { | |
1146 | struct hrtimer_clock_base *base = timer->base; | |
1147 | struct hrtimer_cpu_base *cpu_base = base->cpu_base; | |
1148 | enum hrtimer_restart (*fn)(struct hrtimer *); | |
1149 | int restart; | |
1150 | ||
ca109491 PZ |
1151 | WARN_ON(!irqs_disabled()); |
1152 | ||
237fc6e7 | 1153 | debug_hrtimer_deactivate(timer); |
d3d74453 PZ |
1154 | __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0); |
1155 | timer_stats_account_hrtimer(timer); | |
d3d74453 | 1156 | fn = timer->function; |
ca109491 PZ |
1157 | |
1158 | /* | |
1159 | * Because we run timers from hardirq context, there is no chance | |
1160 | * they get migrated to another cpu, therefore its safe to unlock | |
1161 | * the timer base. | |
1162 | */ | |
1163 | spin_unlock(&cpu_base->lock); | |
1164 | restart = fn(timer); | |
1165 | spin_lock(&cpu_base->lock); | |
d3d74453 PZ |
1166 | |
1167 | /* | |
e3f1d883 TG |
1168 | * Note: We clear the CALLBACK bit after enqueue_hrtimer and |
1169 | * we do not reprogramm the event hardware. Happens either in | |
1170 | * hrtimer_start_range_ns() or in hrtimer_interrupt() | |
d3d74453 PZ |
1171 | */ |
1172 | if (restart != HRTIMER_NORESTART) { | |
1173 | BUG_ON(timer->state != HRTIMER_STATE_CALLBACK); | |
a6037b61 | 1174 | enqueue_hrtimer(timer, base); |
d3d74453 PZ |
1175 | } |
1176 | timer->state &= ~HRTIMER_STATE_CALLBACK; | |
1177 | } | |
1178 | ||
54cdfdb4 TG |
1179 | #ifdef CONFIG_HIGH_RES_TIMERS |
1180 | ||
7f22391c FW |
1181 | static int force_clock_reprogram; |
1182 | ||
1183 | /* | |
1184 | * After 5 iteration's attempts, we consider that hrtimer_interrupt() | |
1185 | * is hanging, which could happen with something that slows the interrupt | |
1186 | * such as the tracing. Then we force the clock reprogramming for each future | |
1187 | * hrtimer interrupts to avoid infinite loops and use the min_delta_ns | |
1188 | * threshold that we will overwrite. | |
1189 | * The next tick event will be scheduled to 3 times we currently spend on | |
1190 | * hrtimer_interrupt(). This gives a good compromise, the cpus will spend | |
1191 | * 1/4 of their time to process the hrtimer interrupts. This is enough to | |
1192 | * let it running without serious starvation. | |
1193 | */ | |
1194 | ||
1195 | static inline void | |
1196 | hrtimer_interrupt_hanging(struct clock_event_device *dev, | |
1197 | ktime_t try_time) | |
1198 | { | |
1199 | force_clock_reprogram = 1; | |
1200 | dev->min_delta_ns = (unsigned long)try_time.tv64 * 3; | |
1201 | printk(KERN_WARNING "hrtimer: interrupt too slow, " | |
1202 | "forcing clock min delta to %lu ns\n", dev->min_delta_ns); | |
1203 | } | |
54cdfdb4 TG |
1204 | /* |
1205 | * High resolution timer interrupt | |
1206 | * Called with interrupts disabled | |
1207 | */ | |
1208 | void hrtimer_interrupt(struct clock_event_device *dev) | |
1209 | { | |
1210 | struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases); | |
1211 | struct hrtimer_clock_base *base; | |
1212 | ktime_t expires_next, now; | |
7f22391c | 1213 | int nr_retries = 0; |
ca109491 | 1214 | int i; |
54cdfdb4 TG |
1215 | |
1216 | BUG_ON(!cpu_base->hres_active); | |
1217 | cpu_base->nr_events++; | |
1218 | dev->next_event.tv64 = KTIME_MAX; | |
1219 | ||
1220 | retry: | |
7f22391c FW |
1221 | /* 5 retries is enough to notice a hang */ |
1222 | if (!(++nr_retries % 5)) | |
1223 | hrtimer_interrupt_hanging(dev, ktime_sub(ktime_get(), now)); | |
1224 | ||
54cdfdb4 TG |
1225 | now = ktime_get(); |
1226 | ||
1227 | expires_next.tv64 = KTIME_MAX; | |
1228 | ||
6ff7041d TG |
1229 | spin_lock(&cpu_base->lock); |
1230 | /* | |
1231 | * We set expires_next to KTIME_MAX here with cpu_base->lock | |
1232 | * held to prevent that a timer is enqueued in our queue via | |
1233 | * the migration code. This does not affect enqueueing of | |
1234 | * timers which run their callback and need to be requeued on | |
1235 | * this CPU. | |
1236 | */ | |
1237 | cpu_base->expires_next.tv64 = KTIME_MAX; | |
1238 | ||
54cdfdb4 TG |
1239 | base = cpu_base->clock_base; |
1240 | ||
1241 | for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) { | |
1242 | ktime_t basenow; | |
1243 | struct rb_node *node; | |
1244 | ||
54cdfdb4 TG |
1245 | basenow = ktime_add(now, base->offset); |
1246 | ||
1247 | while ((node = base->first)) { | |
1248 | struct hrtimer *timer; | |
1249 | ||
1250 | timer = rb_entry(node, struct hrtimer, node); | |
1251 | ||
654c8e0b AV |
1252 | /* |
1253 | * The immediate goal for using the softexpires is | |
1254 | * minimizing wakeups, not running timers at the | |
1255 | * earliest interrupt after their soft expiration. | |
1256 | * This allows us to avoid using a Priority Search | |
1257 | * Tree, which can answer a stabbing querry for | |
1258 | * overlapping intervals and instead use the simple | |
1259 | * BST we already have. | |
1260 | * We don't add extra wakeups by delaying timers that | |
1261 | * are right-of a not yet expired timer, because that | |
1262 | * timer will have to trigger a wakeup anyway. | |
1263 | */ | |
1264 | ||
1265 | if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) { | |
54cdfdb4 TG |
1266 | ktime_t expires; |
1267 | ||
cc584b21 | 1268 | expires = ktime_sub(hrtimer_get_expires(timer), |
54cdfdb4 TG |
1269 | base->offset); |
1270 | if (expires.tv64 < expires_next.tv64) | |
1271 | expires_next = expires; | |
1272 | break; | |
1273 | } | |
1274 | ||
d3d74453 | 1275 | __run_hrtimer(timer); |
54cdfdb4 | 1276 | } |
54cdfdb4 TG |
1277 | base++; |
1278 | } | |
1279 | ||
6ff7041d TG |
1280 | /* |
1281 | * Store the new expiry value so the migration code can verify | |
1282 | * against it. | |
1283 | */ | |
54cdfdb4 | 1284 | cpu_base->expires_next = expires_next; |
6ff7041d | 1285 | spin_unlock(&cpu_base->lock); |
54cdfdb4 TG |
1286 | |
1287 | /* Reprogramming necessary ? */ | |
1288 | if (expires_next.tv64 != KTIME_MAX) { | |
7f22391c | 1289 | if (tick_program_event(expires_next, force_clock_reprogram)) |
54cdfdb4 TG |
1290 | goto retry; |
1291 | } | |
54cdfdb4 TG |
1292 | } |
1293 | ||
8bdec955 TG |
1294 | /* |
1295 | * local version of hrtimer_peek_ahead_timers() called with interrupts | |
1296 | * disabled. | |
1297 | */ | |
1298 | static void __hrtimer_peek_ahead_timers(void) | |
1299 | { | |
1300 | struct tick_device *td; | |
1301 | ||
1302 | if (!hrtimer_hres_active()) | |
1303 | return; | |
1304 | ||
1305 | td = &__get_cpu_var(tick_cpu_device); | |
1306 | if (td && td->evtdev) | |
1307 | hrtimer_interrupt(td->evtdev); | |
1308 | } | |
1309 | ||
2e94d1f7 AV |
1310 | /** |
1311 | * hrtimer_peek_ahead_timers -- run soft-expired timers now | |
1312 | * | |
1313 | * hrtimer_peek_ahead_timers will peek at the timer queue of | |
1314 | * the current cpu and check if there are any timers for which | |
1315 | * the soft expires time has passed. If any such timers exist, | |
1316 | * they are run immediately and then removed from the timer queue. | |
1317 | * | |
1318 | */ | |
1319 | void hrtimer_peek_ahead_timers(void) | |
1320 | { | |
643bdf68 | 1321 | unsigned long flags; |
dc4304f7 | 1322 | |
2e94d1f7 | 1323 | local_irq_save(flags); |
8bdec955 | 1324 | __hrtimer_peek_ahead_timers(); |
2e94d1f7 AV |
1325 | local_irq_restore(flags); |
1326 | } | |
1327 | ||
a6037b61 PZ |
1328 | static void run_hrtimer_softirq(struct softirq_action *h) |
1329 | { | |
1330 | hrtimer_peek_ahead_timers(); | |
1331 | } | |
1332 | ||
82c5b7b5 IM |
1333 | #else /* CONFIG_HIGH_RES_TIMERS */ |
1334 | ||
1335 | static inline void __hrtimer_peek_ahead_timers(void) { } | |
1336 | ||
1337 | #endif /* !CONFIG_HIGH_RES_TIMERS */ | |
82f67cd9 | 1338 | |
d3d74453 PZ |
1339 | /* |
1340 | * Called from timer softirq every jiffy, expire hrtimers: | |
1341 | * | |
1342 | * For HRT its the fall back code to run the softirq in the timer | |
1343 | * softirq context in case the hrtimer initialization failed or has | |
1344 | * not been done yet. | |
1345 | */ | |
1346 | void hrtimer_run_pending(void) | |
1347 | { | |
d3d74453 PZ |
1348 | if (hrtimer_hres_active()) |
1349 | return; | |
54cdfdb4 | 1350 | |
d3d74453 PZ |
1351 | /* |
1352 | * This _is_ ugly: We have to check in the softirq context, | |
1353 | * whether we can switch to highres and / or nohz mode. The | |
1354 | * clocksource switch happens in the timer interrupt with | |
1355 | * xtime_lock held. Notification from there only sets the | |
1356 | * check bit in the tick_oneshot code, otherwise we might | |
1357 | * deadlock vs. xtime_lock. | |
1358 | */ | |
1359 | if (tick_check_oneshot_change(!hrtimer_is_hres_enabled())) | |
1360 | hrtimer_switch_to_hres(); | |
54cdfdb4 TG |
1361 | } |
1362 | ||
c0a31329 | 1363 | /* |
d3d74453 | 1364 | * Called from hardirq context every jiffy |
c0a31329 | 1365 | */ |
833883d9 | 1366 | void hrtimer_run_queues(void) |
c0a31329 | 1367 | { |
288867ec | 1368 | struct rb_node *node; |
833883d9 DS |
1369 | struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases); |
1370 | struct hrtimer_clock_base *base; | |
1371 | int index, gettime = 1; | |
c0a31329 | 1372 | |
833883d9 | 1373 | if (hrtimer_hres_active()) |
3055adda DS |
1374 | return; |
1375 | ||
833883d9 DS |
1376 | for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) { |
1377 | base = &cpu_base->clock_base[index]; | |
c0a31329 | 1378 | |
833883d9 | 1379 | if (!base->first) |
d3d74453 | 1380 | continue; |
833883d9 | 1381 | |
d7cfb60c | 1382 | if (gettime) { |
833883d9 DS |
1383 | hrtimer_get_softirq_time(cpu_base); |
1384 | gettime = 0; | |
b75f7a51 | 1385 | } |
d3d74453 | 1386 | |
833883d9 | 1387 | spin_lock(&cpu_base->lock); |
c0a31329 | 1388 | |
833883d9 DS |
1389 | while ((node = base->first)) { |
1390 | struct hrtimer *timer; | |
54cdfdb4 | 1391 | |
833883d9 | 1392 | timer = rb_entry(node, struct hrtimer, node); |
cc584b21 AV |
1393 | if (base->softirq_time.tv64 <= |
1394 | hrtimer_get_expires_tv64(timer)) | |
833883d9 DS |
1395 | break; |
1396 | ||
833883d9 DS |
1397 | __run_hrtimer(timer); |
1398 | } | |
1399 | spin_unlock(&cpu_base->lock); | |
1400 | } | |
c0a31329 TG |
1401 | } |
1402 | ||
10c94ec1 TG |
1403 | /* |
1404 | * Sleep related functions: | |
1405 | */ | |
c9cb2e3d | 1406 | static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer) |
00362e33 TG |
1407 | { |
1408 | struct hrtimer_sleeper *t = | |
1409 | container_of(timer, struct hrtimer_sleeper, timer); | |
1410 | struct task_struct *task = t->task; | |
1411 | ||
1412 | t->task = NULL; | |
1413 | if (task) | |
1414 | wake_up_process(task); | |
1415 | ||
1416 | return HRTIMER_NORESTART; | |
1417 | } | |
1418 | ||
36c8b586 | 1419 | void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task) |
00362e33 TG |
1420 | { |
1421 | sl->timer.function = hrtimer_wakeup; | |
1422 | sl->task = task; | |
1423 | } | |
2bc481cf | 1424 | EXPORT_SYMBOL_GPL(hrtimer_init_sleeper); |
00362e33 | 1425 | |
669d7868 | 1426 | static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode) |
432569bb | 1427 | { |
669d7868 | 1428 | hrtimer_init_sleeper(t, current); |
10c94ec1 | 1429 | |
432569bb RZ |
1430 | do { |
1431 | set_current_state(TASK_INTERRUPTIBLE); | |
cc584b21 | 1432 | hrtimer_start_expires(&t->timer, mode); |
37bb6cb4 PZ |
1433 | if (!hrtimer_active(&t->timer)) |
1434 | t->task = NULL; | |
432569bb | 1435 | |
54cdfdb4 TG |
1436 | if (likely(t->task)) |
1437 | schedule(); | |
432569bb | 1438 | |
669d7868 | 1439 | hrtimer_cancel(&t->timer); |
c9cb2e3d | 1440 | mode = HRTIMER_MODE_ABS; |
669d7868 TG |
1441 | |
1442 | } while (t->task && !signal_pending(current)); | |
432569bb | 1443 | |
3588a085 PZ |
1444 | __set_current_state(TASK_RUNNING); |
1445 | ||
669d7868 | 1446 | return t->task == NULL; |
10c94ec1 TG |
1447 | } |
1448 | ||
080344b9 ON |
1449 | static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp) |
1450 | { | |
1451 | struct timespec rmt; | |
1452 | ktime_t rem; | |
1453 | ||
cc584b21 | 1454 | rem = hrtimer_expires_remaining(timer); |
080344b9 ON |
1455 | if (rem.tv64 <= 0) |
1456 | return 0; | |
1457 | rmt = ktime_to_timespec(rem); | |
1458 | ||
1459 | if (copy_to_user(rmtp, &rmt, sizeof(*rmtp))) | |
1460 | return -EFAULT; | |
1461 | ||
1462 | return 1; | |
1463 | } | |
1464 | ||
1711ef38 | 1465 | long __sched hrtimer_nanosleep_restart(struct restart_block *restart) |
10c94ec1 | 1466 | { |
669d7868 | 1467 | struct hrtimer_sleeper t; |
080344b9 | 1468 | struct timespec __user *rmtp; |
237fc6e7 | 1469 | int ret = 0; |
10c94ec1 | 1470 | |
237fc6e7 TG |
1471 | hrtimer_init_on_stack(&t.timer, restart->nanosleep.index, |
1472 | HRTIMER_MODE_ABS); | |
cc584b21 | 1473 | hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires); |
10c94ec1 | 1474 | |
c9cb2e3d | 1475 | if (do_nanosleep(&t, HRTIMER_MODE_ABS)) |
237fc6e7 | 1476 | goto out; |
10c94ec1 | 1477 | |
029a07e0 | 1478 | rmtp = restart->nanosleep.rmtp; |
432569bb | 1479 | if (rmtp) { |
237fc6e7 | 1480 | ret = update_rmtp(&t.timer, rmtp); |
080344b9 | 1481 | if (ret <= 0) |
237fc6e7 | 1482 | goto out; |
432569bb | 1483 | } |
10c94ec1 | 1484 | |
10c94ec1 | 1485 | /* The other values in restart are already filled in */ |
237fc6e7 TG |
1486 | ret = -ERESTART_RESTARTBLOCK; |
1487 | out: | |
1488 | destroy_hrtimer_on_stack(&t.timer); | |
1489 | return ret; | |
10c94ec1 TG |
1490 | } |
1491 | ||
080344b9 | 1492 | long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp, |
10c94ec1 TG |
1493 | const enum hrtimer_mode mode, const clockid_t clockid) |
1494 | { | |
1495 | struct restart_block *restart; | |
669d7868 | 1496 | struct hrtimer_sleeper t; |
237fc6e7 | 1497 | int ret = 0; |
3bd01206 AV |
1498 | unsigned long slack; |
1499 | ||
1500 | slack = current->timer_slack_ns; | |
1501 | if (rt_task(current)) | |
1502 | slack = 0; | |
10c94ec1 | 1503 | |
237fc6e7 | 1504 | hrtimer_init_on_stack(&t.timer, clockid, mode); |
3bd01206 | 1505 | hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack); |
432569bb | 1506 | if (do_nanosleep(&t, mode)) |
237fc6e7 | 1507 | goto out; |
10c94ec1 | 1508 | |
7978672c | 1509 | /* Absolute timers do not update the rmtp value and restart: */ |
237fc6e7 TG |
1510 | if (mode == HRTIMER_MODE_ABS) { |
1511 | ret = -ERESTARTNOHAND; | |
1512 | goto out; | |
1513 | } | |
10c94ec1 | 1514 | |
432569bb | 1515 | if (rmtp) { |
237fc6e7 | 1516 | ret = update_rmtp(&t.timer, rmtp); |
080344b9 | 1517 | if (ret <= 0) |
237fc6e7 | 1518 | goto out; |
432569bb | 1519 | } |
10c94ec1 TG |
1520 | |
1521 | restart = ¤t_thread_info()->restart_block; | |
1711ef38 | 1522 | restart->fn = hrtimer_nanosleep_restart; |
029a07e0 TG |
1523 | restart->nanosleep.index = t.timer.base->index; |
1524 | restart->nanosleep.rmtp = rmtp; | |
cc584b21 | 1525 | restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer); |
10c94ec1 | 1526 | |
237fc6e7 TG |
1527 | ret = -ERESTART_RESTARTBLOCK; |
1528 | out: | |
1529 | destroy_hrtimer_on_stack(&t.timer); | |
1530 | return ret; | |
10c94ec1 TG |
1531 | } |
1532 | ||
58fd3aa2 HC |
1533 | SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp, |
1534 | struct timespec __user *, rmtp) | |
6ba1b912 | 1535 | { |
080344b9 | 1536 | struct timespec tu; |
6ba1b912 TG |
1537 | |
1538 | if (copy_from_user(&tu, rqtp, sizeof(tu))) | |
1539 | return -EFAULT; | |
1540 | ||
1541 | if (!timespec_valid(&tu)) | |
1542 | return -EINVAL; | |
1543 | ||
080344b9 | 1544 | return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC); |
6ba1b912 TG |
1545 | } |
1546 | ||
c0a31329 TG |
1547 | /* |
1548 | * Functions related to boot-time initialization: | |
1549 | */ | |
0ec160dd | 1550 | static void __cpuinit init_hrtimers_cpu(int cpu) |
c0a31329 | 1551 | { |
3c8aa39d | 1552 | struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu); |
c0a31329 TG |
1553 | int i; |
1554 | ||
3c8aa39d | 1555 | spin_lock_init(&cpu_base->lock); |
3c8aa39d TG |
1556 | |
1557 | for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) | |
1558 | cpu_base->clock_base[i].cpu_base = cpu_base; | |
1559 | ||
54cdfdb4 | 1560 | hrtimer_init_hres(cpu_base); |
c0a31329 TG |
1561 | } |
1562 | ||
1563 | #ifdef CONFIG_HOTPLUG_CPU | |
1564 | ||
ca109491 | 1565 | static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base, |
37810659 | 1566 | struct hrtimer_clock_base *new_base) |
c0a31329 TG |
1567 | { |
1568 | struct hrtimer *timer; | |
1569 | struct rb_node *node; | |
1570 | ||
1571 | while ((node = rb_first(&old_base->active))) { | |
1572 | timer = rb_entry(node, struct hrtimer, node); | |
54cdfdb4 | 1573 | BUG_ON(hrtimer_callback_running(timer)); |
237fc6e7 | 1574 | debug_hrtimer_deactivate(timer); |
b00c1a99 TG |
1575 | |
1576 | /* | |
1577 | * Mark it as STATE_MIGRATE not INACTIVE otherwise the | |
1578 | * timer could be seen as !active and just vanish away | |
1579 | * under us on another CPU | |
1580 | */ | |
1581 | __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0); | |
c0a31329 | 1582 | timer->base = new_base; |
54cdfdb4 | 1583 | /* |
e3f1d883 TG |
1584 | * Enqueue the timers on the new cpu. This does not |
1585 | * reprogram the event device in case the timer | |
1586 | * expires before the earliest on this CPU, but we run | |
1587 | * hrtimer_interrupt after we migrated everything to | |
1588 | * sort out already expired timers and reprogram the | |
1589 | * event device. | |
54cdfdb4 | 1590 | */ |
a6037b61 | 1591 | enqueue_hrtimer(timer, new_base); |
41e1022e | 1592 | |
b00c1a99 TG |
1593 | /* Clear the migration state bit */ |
1594 | timer->state &= ~HRTIMER_STATE_MIGRATE; | |
c0a31329 TG |
1595 | } |
1596 | } | |
1597 | ||
d5fd43c4 | 1598 | static void migrate_hrtimers(int scpu) |
c0a31329 | 1599 | { |
3c8aa39d | 1600 | struct hrtimer_cpu_base *old_base, *new_base; |
731a55ba | 1601 | int i; |
c0a31329 | 1602 | |
37810659 | 1603 | BUG_ON(cpu_online(scpu)); |
37810659 | 1604 | tick_cancel_sched_timer(scpu); |
731a55ba TG |
1605 | |
1606 | local_irq_disable(); | |
1607 | old_base = &per_cpu(hrtimer_bases, scpu); | |
1608 | new_base = &__get_cpu_var(hrtimer_bases); | |
d82f0b0f ON |
1609 | /* |
1610 | * The caller is globally serialized and nobody else | |
1611 | * takes two locks at once, deadlock is not possible. | |
1612 | */ | |
731a55ba | 1613 | spin_lock(&new_base->lock); |
8e60e05f | 1614 | spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING); |
c0a31329 | 1615 | |
3c8aa39d | 1616 | for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) { |
ca109491 | 1617 | migrate_hrtimer_list(&old_base->clock_base[i], |
37810659 | 1618 | &new_base->clock_base[i]); |
c0a31329 TG |
1619 | } |
1620 | ||
8e60e05f | 1621 | spin_unlock(&old_base->lock); |
731a55ba | 1622 | spin_unlock(&new_base->lock); |
37810659 | 1623 | |
731a55ba TG |
1624 | /* Check, if we got expired work to do */ |
1625 | __hrtimer_peek_ahead_timers(); | |
1626 | local_irq_enable(); | |
c0a31329 | 1627 | } |
37810659 | 1628 | |
c0a31329 TG |
1629 | #endif /* CONFIG_HOTPLUG_CPU */ |
1630 | ||
8c78f307 | 1631 | static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self, |
c0a31329 TG |
1632 | unsigned long action, void *hcpu) |
1633 | { | |
b2e3c0ad | 1634 | int scpu = (long)hcpu; |
c0a31329 TG |
1635 | |
1636 | switch (action) { | |
1637 | ||
1638 | case CPU_UP_PREPARE: | |
8bb78442 | 1639 | case CPU_UP_PREPARE_FROZEN: |
37810659 | 1640 | init_hrtimers_cpu(scpu); |
c0a31329 TG |
1641 | break; |
1642 | ||
1643 | #ifdef CONFIG_HOTPLUG_CPU | |
94df7de0 SD |
1644 | case CPU_DYING: |
1645 | case CPU_DYING_FROZEN: | |
1646 | clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu); | |
1647 | break; | |
c0a31329 | 1648 | case CPU_DEAD: |
8bb78442 | 1649 | case CPU_DEAD_FROZEN: |
b2e3c0ad | 1650 | { |
37810659 | 1651 | clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu); |
d5fd43c4 | 1652 | migrate_hrtimers(scpu); |
c0a31329 | 1653 | break; |
b2e3c0ad | 1654 | } |
c0a31329 TG |
1655 | #endif |
1656 | ||
1657 | default: | |
1658 | break; | |
1659 | } | |
1660 | ||
1661 | return NOTIFY_OK; | |
1662 | } | |
1663 | ||
8c78f307 | 1664 | static struct notifier_block __cpuinitdata hrtimers_nb = { |
c0a31329 TG |
1665 | .notifier_call = hrtimer_cpu_notify, |
1666 | }; | |
1667 | ||
1668 | void __init hrtimers_init(void) | |
1669 | { | |
1670 | hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE, | |
1671 | (void *)(long)smp_processor_id()); | |
1672 | register_cpu_notifier(&hrtimers_nb); | |
a6037b61 PZ |
1673 | #ifdef CONFIG_HIGH_RES_TIMERS |
1674 | open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq); | |
1675 | #endif | |
c0a31329 TG |
1676 | } |
1677 | ||
7bb67439 | 1678 | /** |
654c8e0b | 1679 | * schedule_hrtimeout_range - sleep until timeout |
7bb67439 | 1680 | * @expires: timeout value (ktime_t) |
654c8e0b | 1681 | * @delta: slack in expires timeout (ktime_t) |
7bb67439 AV |
1682 | * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL |
1683 | * | |
1684 | * Make the current task sleep until the given expiry time has | |
1685 | * elapsed. The routine will return immediately unless | |
1686 | * the current task state has been set (see set_current_state()). | |
1687 | * | |
654c8e0b AV |
1688 | * The @delta argument gives the kernel the freedom to schedule the |
1689 | * actual wakeup to a time that is both power and performance friendly. | |
1690 | * The kernel give the normal best effort behavior for "@expires+@delta", | |
1691 | * but may decide to fire the timer earlier, but no earlier than @expires. | |
1692 | * | |
7bb67439 AV |
1693 | * You can set the task state as follows - |
1694 | * | |
1695 | * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to | |
1696 | * pass before the routine returns. | |
1697 | * | |
1698 | * %TASK_INTERRUPTIBLE - the routine may return early if a signal is | |
1699 | * delivered to the current task. | |
1700 | * | |
1701 | * The current task state is guaranteed to be TASK_RUNNING when this | |
1702 | * routine returns. | |
1703 | * | |
1704 | * Returns 0 when the timer has expired otherwise -EINTR | |
1705 | */ | |
654c8e0b | 1706 | int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta, |
7bb67439 AV |
1707 | const enum hrtimer_mode mode) |
1708 | { | |
1709 | struct hrtimer_sleeper t; | |
1710 | ||
1711 | /* | |
1712 | * Optimize when a zero timeout value is given. It does not | |
1713 | * matter whether this is an absolute or a relative time. | |
1714 | */ | |
1715 | if (expires && !expires->tv64) { | |
1716 | __set_current_state(TASK_RUNNING); | |
1717 | return 0; | |
1718 | } | |
1719 | ||
1720 | /* | |
1721 | * A NULL parameter means "inifinte" | |
1722 | */ | |
1723 | if (!expires) { | |
1724 | schedule(); | |
1725 | __set_current_state(TASK_RUNNING); | |
1726 | return -EINTR; | |
1727 | } | |
1728 | ||
1729 | hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, mode); | |
654c8e0b | 1730 | hrtimer_set_expires_range_ns(&t.timer, *expires, delta); |
7bb67439 AV |
1731 | |
1732 | hrtimer_init_sleeper(&t, current); | |
1733 | ||
cc584b21 | 1734 | hrtimer_start_expires(&t.timer, mode); |
7bb67439 AV |
1735 | if (!hrtimer_active(&t.timer)) |
1736 | t.task = NULL; | |
1737 | ||
1738 | if (likely(t.task)) | |
1739 | schedule(); | |
1740 | ||
1741 | hrtimer_cancel(&t.timer); | |
1742 | destroy_hrtimer_on_stack(&t.timer); | |
1743 | ||
1744 | __set_current_state(TASK_RUNNING); | |
1745 | ||
1746 | return !t.task ? 0 : -EINTR; | |
1747 | } | |
654c8e0b AV |
1748 | EXPORT_SYMBOL_GPL(schedule_hrtimeout_range); |
1749 | ||
1750 | /** | |
1751 | * schedule_hrtimeout - sleep until timeout | |
1752 | * @expires: timeout value (ktime_t) | |
1753 | * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL | |
1754 | * | |
1755 | * Make the current task sleep until the given expiry time has | |
1756 | * elapsed. The routine will return immediately unless | |
1757 | * the current task state has been set (see set_current_state()). | |
1758 | * | |
1759 | * You can set the task state as follows - | |
1760 | * | |
1761 | * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to | |
1762 | * pass before the routine returns. | |
1763 | * | |
1764 | * %TASK_INTERRUPTIBLE - the routine may return early if a signal is | |
1765 | * delivered to the current task. | |
1766 | * | |
1767 | * The current task state is guaranteed to be TASK_RUNNING when this | |
1768 | * routine returns. | |
1769 | * | |
1770 | * Returns 0 when the timer has expired otherwise -EINTR | |
1771 | */ | |
1772 | int __sched schedule_hrtimeout(ktime_t *expires, | |
1773 | const enum hrtimer_mode mode) | |
1774 | { | |
1775 | return schedule_hrtimeout_range(expires, 0, mode); | |
1776 | } | |
7bb67439 | 1777 | EXPORT_SYMBOL_GPL(schedule_hrtimeout); |