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