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