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