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