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