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