]>
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 | ||
438 | static inline void debug_activate(struct hrtimer *timer) | |
439 | { | |
440 | debug_hrtimer_activate(timer); | |
441 | trace_hrtimer_start(timer); | |
442 | } | |
443 | ||
444 | static inline void debug_deactivate(struct hrtimer *timer) | |
445 | { | |
446 | debug_hrtimer_deactivate(timer); | |
447 | trace_hrtimer_cancel(timer); | |
448 | } | |
449 | ||
9bc74919 | 450 | #if defined(CONFIG_NO_HZ_COMMON) || defined(CONFIG_HIGH_RES_TIMERS) |
895bdfa7 TG |
451 | static inline void hrtimer_update_next_timer(struct hrtimer_cpu_base *cpu_base, |
452 | struct hrtimer *timer) | |
453 | { | |
454 | #ifdef CONFIG_HIGH_RES_TIMERS | |
455 | cpu_base->next_timer = timer; | |
456 | #endif | |
457 | } | |
458 | ||
4ebbda52 | 459 | static ktime_t __hrtimer_get_next_event(struct hrtimer_cpu_base *cpu_base) |
9bc74919 TG |
460 | { |
461 | struct hrtimer_clock_base *base = cpu_base->clock_base; | |
34aee88a | 462 | unsigned int active = cpu_base->active_bases; |
2456e855 | 463 | ktime_t expires, expires_next = KTIME_MAX; |
9bc74919 | 464 | |
895bdfa7 | 465 | hrtimer_update_next_timer(cpu_base, NULL); |
34aee88a | 466 | for (; active; base++, active >>= 1) { |
9bc74919 TG |
467 | struct timerqueue_node *next; |
468 | struct hrtimer *timer; | |
469 | ||
34aee88a | 470 | if (!(active & 0x01)) |
9bc74919 TG |
471 | continue; |
472 | ||
34aee88a | 473 | next = timerqueue_getnext(&base->active); |
9bc74919 TG |
474 | timer = container_of(next, struct hrtimer, node); |
475 | expires = ktime_sub(hrtimer_get_expires(timer), base->offset); | |
2456e855 | 476 | if (expires < expires_next) { |
9bc74919 | 477 | expires_next = expires; |
895bdfa7 TG |
478 | hrtimer_update_next_timer(cpu_base, timer); |
479 | } | |
9bc74919 TG |
480 | } |
481 | /* | |
482 | * clock_was_set() might have changed base->offset of any of | |
483 | * the clock bases so the result might be negative. Fix it up | |
484 | * to prevent a false positive in clockevents_program_event(). | |
485 | */ | |
2456e855 TG |
486 | if (expires_next < 0) |
487 | expires_next = 0; | |
9bc74919 TG |
488 | return expires_next; |
489 | } | |
490 | #endif | |
491 | ||
21d6d52a TG |
492 | static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base) |
493 | { | |
494 | ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset; | |
495 | ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset; | |
496 | ktime_t *offs_tai = &base->clock_base[HRTIMER_BASE_TAI].offset; | |
497 | ||
868a3e91 TG |
498 | return ktime_get_update_offsets_now(&base->clock_was_set_seq, |
499 | offs_real, offs_boot, offs_tai); | |
21d6d52a TG |
500 | } |
501 | ||
54cdfdb4 TG |
502 | /* High resolution timer related functions */ |
503 | #ifdef CONFIG_HIGH_RES_TIMERS | |
504 | ||
505 | /* | |
506 | * High resolution timer enabled ? | |
507 | */ | |
4cc7ecb7 | 508 | static bool hrtimer_hres_enabled __read_mostly = true; |
398ca17f TG |
509 | unsigned int hrtimer_resolution __read_mostly = LOW_RES_NSEC; |
510 | EXPORT_SYMBOL_GPL(hrtimer_resolution); | |
54cdfdb4 TG |
511 | |
512 | /* | |
513 | * Enable / Disable high resolution mode | |
514 | */ | |
515 | static int __init setup_hrtimer_hres(char *str) | |
516 | { | |
4cc7ecb7 | 517 | return (kstrtobool(str, &hrtimer_hres_enabled) == 0); |
54cdfdb4 TG |
518 | } |
519 | ||
520 | __setup("highres=", setup_hrtimer_hres); | |
521 | ||
522 | /* | |
523 | * hrtimer_high_res_enabled - query, if the highres mode is enabled | |
524 | */ | |
525 | static inline int hrtimer_is_hres_enabled(void) | |
526 | { | |
527 | return hrtimer_hres_enabled; | |
528 | } | |
529 | ||
530 | /* | |
531 | * Is the high resolution mode active ? | |
532 | */ | |
e19ffe8b TG |
533 | static inline int __hrtimer_hres_active(struct hrtimer_cpu_base *cpu_base) |
534 | { | |
535 | return cpu_base->hres_active; | |
536 | } | |
537 | ||
54cdfdb4 TG |
538 | static inline int hrtimer_hres_active(void) |
539 | { | |
e19ffe8b | 540 | return __hrtimer_hres_active(this_cpu_ptr(&hrtimer_bases)); |
54cdfdb4 TG |
541 | } |
542 | ||
543 | /* | |
544 | * Reprogram the event source with checking both queues for the | |
545 | * next event | |
546 | * Called with interrupts disabled and base->lock held | |
547 | */ | |
7403f41f AC |
548 | static void |
549 | hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal) | |
54cdfdb4 | 550 | { |
21d6d52a TG |
551 | ktime_t expires_next; |
552 | ||
553 | if (!cpu_base->hres_active) | |
554 | return; | |
555 | ||
556 | expires_next = __hrtimer_get_next_event(cpu_base); | |
54cdfdb4 | 557 | |
2456e855 | 558 | if (skip_equal && expires_next == cpu_base->expires_next) |
7403f41f AC |
559 | return; |
560 | ||
2456e855 | 561 | cpu_base->expires_next = expires_next; |
7403f41f | 562 | |
6c6c0d5a SH |
563 | /* |
564 | * If a hang was detected in the last timer interrupt then we | |
565 | * leave the hang delay active in the hardware. We want the | |
566 | * system to make progress. That also prevents the following | |
567 | * scenario: | |
568 | * T1 expires 50ms from now | |
569 | * T2 expires 5s from now | |
570 | * | |
571 | * T1 is removed, so this code is called and would reprogram | |
572 | * the hardware to 5s from now. Any hrtimer_start after that | |
573 | * will not reprogram the hardware due to hang_detected being | |
574 | * set. So we'd effectivly block all timers until the T2 event | |
575 | * fires. | |
576 | */ | |
577 | if (cpu_base->hang_detected) | |
578 | return; | |
579 | ||
d2540875 | 580 | tick_program_event(cpu_base->expires_next, 1); |
54cdfdb4 TG |
581 | } |
582 | ||
583 | /* | |
54cdfdb4 TG |
584 | * When a timer is enqueued and expires earlier than the already enqueued |
585 | * timers, we have to check, whether it expires earlier than the timer for | |
586 | * which the clock event device was armed. | |
587 | * | |
588 | * Called with interrupts disabled and base->cpu_base.lock held | |
589 | */ | |
c6eb3f70 TG |
590 | static void hrtimer_reprogram(struct hrtimer *timer, |
591 | struct hrtimer_clock_base *base) | |
54cdfdb4 | 592 | { |
dc5df73b | 593 | struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases); |
cc584b21 | 594 | ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset); |
54cdfdb4 | 595 | |
cc584b21 | 596 | WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0); |
63070a79 | 597 | |
54cdfdb4 | 598 | /* |
c6eb3f70 TG |
599 | * If the timer is not on the current cpu, we cannot reprogram |
600 | * the other cpus clock event device. | |
54cdfdb4 | 601 | */ |
c6eb3f70 TG |
602 | if (base->cpu_base != cpu_base) |
603 | return; | |
604 | ||
605 | /* | |
606 | * If the hrtimer interrupt is running, then it will | |
607 | * reevaluate the clock bases and reprogram the clock event | |
608 | * device. The callbacks are always executed in hard interrupt | |
609 | * context so we don't need an extra check for a running | |
610 | * callback. | |
611 | */ | |
612 | if (cpu_base->in_hrtirq) | |
613 | return; | |
54cdfdb4 | 614 | |
63070a79 TG |
615 | /* |
616 | * CLOCK_REALTIME timer might be requested with an absolute | |
c6eb3f70 | 617 | * expiry time which is less than base->offset. Set it to 0. |
63070a79 | 618 | */ |
2456e855 TG |
619 | if (expires < 0) |
620 | expires = 0; | |
63070a79 | 621 | |
2456e855 | 622 | if (expires >= cpu_base->expires_next) |
c6eb3f70 | 623 | return; |
41d2e494 | 624 | |
c6eb3f70 | 625 | /* Update the pointer to the next expiring timer */ |
895bdfa7 | 626 | cpu_base->next_timer = timer; |
9bc74919 | 627 | |
41d2e494 TG |
628 | /* |
629 | * If a hang was detected in the last timer interrupt then we | |
630 | * do not schedule a timer which is earlier than the expiry | |
631 | * which we enforced in the hang detection. We want the system | |
632 | * to make progress. | |
633 | */ | |
634 | if (cpu_base->hang_detected) | |
c6eb3f70 | 635 | return; |
54cdfdb4 TG |
636 | |
637 | /* | |
c6eb3f70 TG |
638 | * Program the timer hardware. We enforce the expiry for |
639 | * events which are already in the past. | |
54cdfdb4 | 640 | */ |
c6eb3f70 TG |
641 | cpu_base->expires_next = expires; |
642 | tick_program_event(expires, 1); | |
54cdfdb4 TG |
643 | } |
644 | ||
54cdfdb4 TG |
645 | /* |
646 | * Initialize the high resolution related parts of cpu_base | |
647 | */ | |
648 | static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) | |
649 | { | |
2456e855 | 650 | base->expires_next = KTIME_MAX; |
54cdfdb4 | 651 | base->hres_active = 0; |
54cdfdb4 TG |
652 | } |
653 | ||
9ec26907 TG |
654 | /* |
655 | * Retrigger next event is called after clock was set | |
656 | * | |
657 | * Called with interrupts disabled via on_each_cpu() | |
658 | */ | |
659 | static void retrigger_next_event(void *arg) | |
660 | { | |
dc5df73b | 661 | struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases); |
9ec26907 | 662 | |
e19ffe8b | 663 | if (!base->hres_active) |
9ec26907 TG |
664 | return; |
665 | ||
9ec26907 | 666 | raw_spin_lock(&base->lock); |
5baefd6d | 667 | hrtimer_update_base(base); |
9ec26907 TG |
668 | hrtimer_force_reprogram(base, 0); |
669 | raw_spin_unlock(&base->lock); | |
670 | } | |
b12a03ce | 671 | |
54cdfdb4 TG |
672 | /* |
673 | * Switch to high resolution mode | |
674 | */ | |
75e3b37d | 675 | static void hrtimer_switch_to_hres(void) |
54cdfdb4 | 676 | { |
c6eb3f70 | 677 | struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases); |
54cdfdb4 TG |
678 | |
679 | if (tick_init_highres()) { | |
820de5c3 | 680 | printk(KERN_WARNING "Could not switch to high resolution " |
c6eb3f70 | 681 | "mode on CPU %d\n", base->cpu); |
85e1cd6e | 682 | return; |
54cdfdb4 TG |
683 | } |
684 | base->hres_active = 1; | |
398ca17f | 685 | hrtimer_resolution = HIGH_RES_NSEC; |
54cdfdb4 TG |
686 | |
687 | tick_setup_sched_timer(); | |
54cdfdb4 TG |
688 | /* "Retrigger" the interrupt to get things going */ |
689 | retrigger_next_event(NULL); | |
54cdfdb4 TG |
690 | } |
691 | ||
5ec2481b TG |
692 | static void clock_was_set_work(struct work_struct *work) |
693 | { | |
694 | clock_was_set(); | |
695 | } | |
696 | ||
697 | static DECLARE_WORK(hrtimer_work, clock_was_set_work); | |
698 | ||
f55a6faa | 699 | /* |
b4d90e9f | 700 | * Called from timekeeping and resume code to reprogram the hrtimer |
5ec2481b | 701 | * interrupt device on all cpus. |
f55a6faa JS |
702 | */ |
703 | void clock_was_set_delayed(void) | |
704 | { | |
5ec2481b | 705 | schedule_work(&hrtimer_work); |
f55a6faa JS |
706 | } |
707 | ||
54cdfdb4 TG |
708 | #else |
709 | ||
e19ffe8b | 710 | static inline int __hrtimer_hres_active(struct hrtimer_cpu_base *b) { return 0; } |
54cdfdb4 TG |
711 | static inline int hrtimer_hres_active(void) { return 0; } |
712 | static inline int hrtimer_is_hres_enabled(void) { return 0; } | |
75e3b37d | 713 | static inline void hrtimer_switch_to_hres(void) { } |
7403f41f AC |
714 | static inline void |
715 | hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { } | |
9e1e01dd VK |
716 | static inline int hrtimer_reprogram(struct hrtimer *timer, |
717 | struct hrtimer_clock_base *base) | |
54cdfdb4 TG |
718 | { |
719 | return 0; | |
720 | } | |
54cdfdb4 | 721 | static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { } |
9ec26907 | 722 | static inline void retrigger_next_event(void *arg) { } |
54cdfdb4 TG |
723 | |
724 | #endif /* CONFIG_HIGH_RES_TIMERS */ | |
725 | ||
b12a03ce TG |
726 | /* |
727 | * Clock realtime was set | |
728 | * | |
729 | * Change the offset of the realtime clock vs. the monotonic | |
730 | * clock. | |
731 | * | |
732 | * We might have to reprogram the high resolution timer interrupt. On | |
733 | * SMP we call the architecture specific code to retrigger _all_ high | |
734 | * resolution timer interrupts. On UP we just disable interrupts and | |
735 | * call the high resolution interrupt code. | |
736 | */ | |
737 | void clock_was_set(void) | |
738 | { | |
90ff1f30 | 739 | #ifdef CONFIG_HIGH_RES_TIMERS |
b12a03ce TG |
740 | /* Retrigger the CPU local events everywhere */ |
741 | on_each_cpu(retrigger_next_event, NULL, 1); | |
9ec26907 TG |
742 | #endif |
743 | timerfd_clock_was_set(); | |
b12a03ce TG |
744 | } |
745 | ||
746 | /* | |
747 | * During resume we might have to reprogram the high resolution timer | |
7c4c3a0f DV |
748 | * interrupt on all online CPUs. However, all other CPUs will be |
749 | * stopped with IRQs interrupts disabled so the clock_was_set() call | |
5ec2481b | 750 | * must be deferred. |
b12a03ce TG |
751 | */ |
752 | void hrtimers_resume(void) | |
753 | { | |
53bef3fd | 754 | lockdep_assert_irqs_disabled(); |
5ec2481b | 755 | /* Retrigger on the local CPU */ |
b12a03ce | 756 | retrigger_next_event(NULL); |
5ec2481b TG |
757 | /* And schedule a retrigger for all others */ |
758 | clock_was_set_delayed(); | |
b12a03ce TG |
759 | } |
760 | ||
c0a31329 | 761 | /* |
6506f2aa | 762 | * Counterpart to lock_hrtimer_base above: |
c0a31329 TG |
763 | */ |
764 | static inline | |
765 | void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags) | |
766 | { | |
ecb49d1a | 767 | raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags); |
c0a31329 TG |
768 | } |
769 | ||
770 | /** | |
771 | * hrtimer_forward - forward the timer expiry | |
c0a31329 | 772 | * @timer: hrtimer to forward |
44f21475 | 773 | * @now: forward past this time |
c0a31329 TG |
774 | * @interval: the interval to forward |
775 | * | |
776 | * Forward the timer expiry so it will expire in the future. | |
8dca6f33 | 777 | * Returns the number of overruns. |
91e5a217 TG |
778 | * |
779 | * Can be safely called from the callback function of @timer. If | |
780 | * called from other contexts @timer must neither be enqueued nor | |
781 | * running the callback and the caller needs to take care of | |
782 | * serialization. | |
783 | * | |
784 | * Note: This only updates the timer expiry value and does not requeue | |
785 | * the timer. | |
c0a31329 | 786 | */ |
4d672e7a | 787 | u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval) |
c0a31329 | 788 | { |
4d672e7a | 789 | u64 orun = 1; |
44f21475 | 790 | ktime_t delta; |
c0a31329 | 791 | |
cc584b21 | 792 | delta = ktime_sub(now, hrtimer_get_expires(timer)); |
c0a31329 | 793 | |
2456e855 | 794 | if (delta < 0) |
c0a31329 TG |
795 | return 0; |
796 | ||
5de2755c PZ |
797 | if (WARN_ON(timer->state & HRTIMER_STATE_ENQUEUED)) |
798 | return 0; | |
799 | ||
2456e855 TG |
800 | if (interval < hrtimer_resolution) |
801 | interval = hrtimer_resolution; | |
c9db4fa1 | 802 | |
2456e855 | 803 | if (unlikely(delta >= interval)) { |
df869b63 | 804 | s64 incr = ktime_to_ns(interval); |
c0a31329 TG |
805 | |
806 | orun = ktime_divns(delta, incr); | |
cc584b21 | 807 | hrtimer_add_expires_ns(timer, incr * orun); |
2456e855 | 808 | if (hrtimer_get_expires_tv64(timer) > now) |
c0a31329 TG |
809 | return orun; |
810 | /* | |
811 | * This (and the ktime_add() below) is the | |
812 | * correction for exact: | |
813 | */ | |
814 | orun++; | |
815 | } | |
cc584b21 | 816 | hrtimer_add_expires(timer, interval); |
c0a31329 TG |
817 | |
818 | return orun; | |
819 | } | |
6bdb6b62 | 820 | EXPORT_SYMBOL_GPL(hrtimer_forward); |
c0a31329 TG |
821 | |
822 | /* | |
823 | * enqueue_hrtimer - internal function to (re)start a timer | |
824 | * | |
825 | * The timer is inserted in expiry order. Insertion into the | |
826 | * red black tree is O(log(n)). Must hold the base lock. | |
a6037b61 PZ |
827 | * |
828 | * Returns 1 when the new timer is the leftmost timer in the tree. | |
c0a31329 | 829 | */ |
a6037b61 PZ |
830 | static int enqueue_hrtimer(struct hrtimer *timer, |
831 | struct hrtimer_clock_base *base) | |
c0a31329 | 832 | { |
c6a2a177 | 833 | debug_activate(timer); |
237fc6e7 | 834 | |
ab8177bc | 835 | base->cpu_base->active_bases |= 1 << base->index; |
54cdfdb4 | 836 | |
887d9dc9 | 837 | timer->state = HRTIMER_STATE_ENQUEUED; |
a6037b61 | 838 | |
b97f44c9 | 839 | return timerqueue_add(&base->active, &timer->node); |
288867ec | 840 | } |
c0a31329 TG |
841 | |
842 | /* | |
843 | * __remove_hrtimer - internal function to remove a timer | |
844 | * | |
845 | * Caller must hold the base lock. | |
54cdfdb4 TG |
846 | * |
847 | * High resolution timer mode reprograms the clock event device when the | |
848 | * timer is the one which expires next. The caller can disable this by setting | |
849 | * reprogram to zero. This is useful, when the context does a reprogramming | |
850 | * anyway (e.g. timer interrupt) | |
c0a31329 | 851 | */ |
3c8aa39d | 852 | static void __remove_hrtimer(struct hrtimer *timer, |
303e967f | 853 | struct hrtimer_clock_base *base, |
203cbf77 | 854 | u8 newstate, int reprogram) |
c0a31329 | 855 | { |
e19ffe8b | 856 | struct hrtimer_cpu_base *cpu_base = base->cpu_base; |
203cbf77 | 857 | u8 state = timer->state; |
e19ffe8b | 858 | |
895bdfa7 TG |
859 | timer->state = newstate; |
860 | if (!(state & HRTIMER_STATE_ENQUEUED)) | |
861 | return; | |
7403f41f | 862 | |
b97f44c9 | 863 | if (!timerqueue_del(&base->active, &timer->node)) |
e19ffe8b | 864 | cpu_base->active_bases &= ~(1 << base->index); |
7403f41f | 865 | |
7403f41f | 866 | #ifdef CONFIG_HIGH_RES_TIMERS |
895bdfa7 TG |
867 | /* |
868 | * Note: If reprogram is false we do not update | |
869 | * cpu_base->next_timer. This happens when we remove the first | |
870 | * timer on a remote cpu. No harm as we never dereference | |
871 | * cpu_base->next_timer. So the worst thing what can happen is | |
872 | * an superflous call to hrtimer_force_reprogram() on the | |
873 | * remote cpu later on if the same timer gets enqueued again. | |
874 | */ | |
875 | if (reprogram && timer == cpu_base->next_timer) | |
876 | hrtimer_force_reprogram(cpu_base, 1); | |
7403f41f | 877 | #endif |
c0a31329 TG |
878 | } |
879 | ||
880 | /* | |
881 | * remove hrtimer, called with base lock held | |
882 | */ | |
883 | static inline int | |
8edfb036 | 884 | remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base, bool restart) |
c0a31329 | 885 | { |
303e967f | 886 | if (hrtimer_is_queued(timer)) { |
203cbf77 | 887 | u8 state = timer->state; |
54cdfdb4 TG |
888 | int reprogram; |
889 | ||
890 | /* | |
891 | * Remove the timer and force reprogramming when high | |
892 | * resolution mode is active and the timer is on the current | |
893 | * CPU. If we remove a timer on another CPU, reprogramming is | |
894 | * skipped. The interrupt event on this CPU is fired and | |
895 | * reprogramming happens in the interrupt handler. This is a | |
896 | * rare case and less expensive than a smp call. | |
897 | */ | |
c6a2a177 | 898 | debug_deactivate(timer); |
dc5df73b | 899 | reprogram = base->cpu_base == this_cpu_ptr(&hrtimer_bases); |
8edfb036 | 900 | |
887d9dc9 PZ |
901 | if (!restart) |
902 | state = HRTIMER_STATE_INACTIVE; | |
903 | ||
f13d4f97 | 904 | __remove_hrtimer(timer, base, state, reprogram); |
c0a31329 TG |
905 | return 1; |
906 | } | |
907 | return 0; | |
908 | } | |
909 | ||
203cbf77 TG |
910 | static inline ktime_t hrtimer_update_lowres(struct hrtimer *timer, ktime_t tim, |
911 | const enum hrtimer_mode mode) | |
912 | { | |
913 | #ifdef CONFIG_TIME_LOW_RES | |
914 | /* | |
915 | * CONFIG_TIME_LOW_RES indicates that the system has no way to return | |
916 | * granular time values. For relative timers we add hrtimer_resolution | |
917 | * (i.e. one jiffie) to prevent short timeouts. | |
918 | */ | |
919 | timer->is_rel = mode & HRTIMER_MODE_REL; | |
920 | if (timer->is_rel) | |
8b0e1953 | 921 | tim = ktime_add_safe(tim, hrtimer_resolution); |
203cbf77 TG |
922 | #endif |
923 | return tim; | |
924 | } | |
925 | ||
58f1f803 TG |
926 | /** |
927 | * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU | |
928 | * @timer: the timer to be added | |
929 | * @tim: expiry time | |
930 | * @delta_ns: "slack" range for the timer | |
931 | * @mode: expiry mode: absolute (HRTIMER_MODE_ABS) or | |
932 | * relative (HRTIMER_MODE_REL) | |
58f1f803 | 933 | */ |
61699e13 | 934 | void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, |
da8b44d5 | 935 | u64 delta_ns, const enum hrtimer_mode mode) |
c0a31329 | 936 | { |
3c8aa39d | 937 | struct hrtimer_clock_base *base, *new_base; |
c0a31329 | 938 | unsigned long flags; |
61699e13 | 939 | int leftmost; |
c0a31329 TG |
940 | |
941 | base = lock_hrtimer_base(timer, &flags); | |
942 | ||
943 | /* Remove an active timer from the queue: */ | |
8edfb036 | 944 | remove_hrtimer(timer, base, true); |
c0a31329 | 945 | |
203cbf77 | 946 | if (mode & HRTIMER_MODE_REL) |
84ea7fe3 | 947 | tim = ktime_add_safe(tim, base->get_time()); |
203cbf77 TG |
948 | |
949 | tim = hrtimer_update_lowres(timer, tim, mode); | |
237fc6e7 | 950 | |
da8f2e17 | 951 | hrtimer_set_expires_range_ns(timer, tim, delta_ns); |
c0a31329 | 952 | |
84ea7fe3 VK |
953 | /* Switch the timer base, if necessary: */ |
954 | new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED); | |
955 | ||
a6037b61 | 956 | leftmost = enqueue_hrtimer(timer, new_base); |
61699e13 TG |
957 | if (!leftmost) |
958 | goto unlock; | |
49a2a075 VK |
959 | |
960 | if (!hrtimer_is_hres_active(timer)) { | |
961 | /* | |
962 | * Kick to reschedule the next tick to handle the new timer | |
963 | * on dynticks target. | |
964 | */ | |
ae67bada | 965 | if (is_timers_nohz_active()) |
683be13a | 966 | wake_up_nohz_cpu(new_base->cpu_base->cpu); |
c6eb3f70 TG |
967 | } else { |
968 | hrtimer_reprogram(timer, new_base); | |
b22affe0 | 969 | } |
61699e13 | 970 | unlock: |
c0a31329 | 971 | unlock_hrtimer_base(timer, &flags); |
7f1e2ca9 | 972 | } |
da8f2e17 AV |
973 | EXPORT_SYMBOL_GPL(hrtimer_start_range_ns); |
974 | ||
c0a31329 TG |
975 | /** |
976 | * hrtimer_try_to_cancel - try to deactivate a timer | |
c0a31329 TG |
977 | * @timer: hrtimer to stop |
978 | * | |
979 | * Returns: | |
980 | * 0 when the timer was not active | |
981 | * 1 when the timer was active | |
0ba42a59 | 982 | * -1 when the timer is currently executing the callback function and |
fa9799e3 | 983 | * cannot be stopped |
c0a31329 TG |
984 | */ |
985 | int hrtimer_try_to_cancel(struct hrtimer *timer) | |
986 | { | |
3c8aa39d | 987 | struct hrtimer_clock_base *base; |
c0a31329 TG |
988 | unsigned long flags; |
989 | int ret = -1; | |
990 | ||
19d9f422 TG |
991 | /* |
992 | * Check lockless first. If the timer is not active (neither | |
993 | * enqueued nor running the callback, nothing to do here. The | |
994 | * base lock does not serialize against a concurrent enqueue, | |
995 | * so we can avoid taking it. | |
996 | */ | |
997 | if (!hrtimer_active(timer)) | |
998 | return 0; | |
999 | ||
c0a31329 TG |
1000 | base = lock_hrtimer_base(timer, &flags); |
1001 | ||
303e967f | 1002 | if (!hrtimer_callback_running(timer)) |
8edfb036 | 1003 | ret = remove_hrtimer(timer, base, false); |
c0a31329 TG |
1004 | |
1005 | unlock_hrtimer_base(timer, &flags); | |
1006 | ||
1007 | return ret; | |
1008 | ||
1009 | } | |
8d16b764 | 1010 | EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel); |
c0a31329 TG |
1011 | |
1012 | /** | |
1013 | * hrtimer_cancel - cancel a timer and wait for the handler to finish. | |
c0a31329 TG |
1014 | * @timer: the timer to be cancelled |
1015 | * | |
1016 | * Returns: | |
1017 | * 0 when the timer was not active | |
1018 | * 1 when the timer was active | |
1019 | */ | |
1020 | int hrtimer_cancel(struct hrtimer *timer) | |
1021 | { | |
1022 | for (;;) { | |
1023 | int ret = hrtimer_try_to_cancel(timer); | |
1024 | ||
1025 | if (ret >= 0) | |
1026 | return ret; | |
5ef37b19 | 1027 | cpu_relax(); |
c0a31329 TG |
1028 | } |
1029 | } | |
8d16b764 | 1030 | EXPORT_SYMBOL_GPL(hrtimer_cancel); |
c0a31329 TG |
1031 | |
1032 | /** | |
1033 | * hrtimer_get_remaining - get remaining time for the timer | |
c0a31329 | 1034 | * @timer: the timer to read |
203cbf77 | 1035 | * @adjust: adjust relative timers when CONFIG_TIME_LOW_RES=y |
c0a31329 | 1036 | */ |
203cbf77 | 1037 | ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust) |
c0a31329 | 1038 | { |
c0a31329 TG |
1039 | unsigned long flags; |
1040 | ktime_t rem; | |
1041 | ||
b3bd3de6 | 1042 | lock_hrtimer_base(timer, &flags); |
203cbf77 TG |
1043 | if (IS_ENABLED(CONFIG_TIME_LOW_RES) && adjust) |
1044 | rem = hrtimer_expires_remaining_adjusted(timer); | |
1045 | else | |
1046 | rem = hrtimer_expires_remaining(timer); | |
c0a31329 TG |
1047 | unlock_hrtimer_base(timer, &flags); |
1048 | ||
1049 | return rem; | |
1050 | } | |
203cbf77 | 1051 | EXPORT_SYMBOL_GPL(__hrtimer_get_remaining); |
c0a31329 | 1052 | |
3451d024 | 1053 | #ifdef CONFIG_NO_HZ_COMMON |
69239749 TL |
1054 | /** |
1055 | * hrtimer_get_next_event - get the time until next expiry event | |
1056 | * | |
c1ad348b | 1057 | * Returns the next expiry time or KTIME_MAX if no timer is pending. |
69239749 | 1058 | */ |
c1ad348b | 1059 | u64 hrtimer_get_next_event(void) |
69239749 | 1060 | { |
dc5df73b | 1061 | struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases); |
c1ad348b | 1062 | u64 expires = KTIME_MAX; |
69239749 | 1063 | unsigned long flags; |
69239749 | 1064 | |
ecb49d1a | 1065 | raw_spin_lock_irqsave(&cpu_base->lock, flags); |
3c8aa39d | 1066 | |
e19ffe8b | 1067 | if (!__hrtimer_hres_active(cpu_base)) |
2456e855 | 1068 | expires = __hrtimer_get_next_event(cpu_base); |
3c8aa39d | 1069 | |
ecb49d1a | 1070 | raw_spin_unlock_irqrestore(&cpu_base->lock, flags); |
3c8aa39d | 1071 | |
c1ad348b | 1072 | return expires; |
69239749 TL |
1073 | } |
1074 | #endif | |
1075 | ||
336a9cde MZ |
1076 | static inline int hrtimer_clockid_to_base(clockid_t clock_id) |
1077 | { | |
1078 | if (likely(clock_id < MAX_CLOCKS)) { | |
1079 | int base = hrtimer_clock_to_base_table[clock_id]; | |
1080 | ||
1081 | if (likely(base != HRTIMER_MAX_CLOCK_BASES)) | |
1082 | return base; | |
1083 | } | |
1084 | WARN(1, "Invalid clockid %d. Using MONOTONIC\n", clock_id); | |
1085 | return HRTIMER_BASE_MONOTONIC; | |
1086 | } | |
1087 | ||
237fc6e7 TG |
1088 | static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id, |
1089 | enum hrtimer_mode mode) | |
c0a31329 | 1090 | { |
3c8aa39d | 1091 | struct hrtimer_cpu_base *cpu_base; |
e06383db | 1092 | int base; |
c0a31329 | 1093 | |
7978672c GA |
1094 | memset(timer, 0, sizeof(struct hrtimer)); |
1095 | ||
22127e93 | 1096 | cpu_base = raw_cpu_ptr(&hrtimer_bases); |
c0a31329 | 1097 | |
c9cb2e3d | 1098 | if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS) |
7978672c GA |
1099 | clock_id = CLOCK_MONOTONIC; |
1100 | ||
e06383db JS |
1101 | base = hrtimer_clockid_to_base(clock_id); |
1102 | timer->base = &cpu_base->clock_base[base]; | |
998adc3d | 1103 | timerqueue_init(&timer->node); |
c0a31329 | 1104 | } |
237fc6e7 TG |
1105 | |
1106 | /** | |
1107 | * hrtimer_init - initialize a timer to the given clock | |
1108 | * @timer: the timer to be initialized | |
1109 | * @clock_id: the clock to be used | |
1110 | * @mode: timer mode abs/rel | |
1111 | */ | |
1112 | void hrtimer_init(struct hrtimer *timer, clockid_t clock_id, | |
1113 | enum hrtimer_mode mode) | |
1114 | { | |
c6a2a177 | 1115 | debug_init(timer, clock_id, mode); |
237fc6e7 TG |
1116 | __hrtimer_init(timer, clock_id, mode); |
1117 | } | |
8d16b764 | 1118 | EXPORT_SYMBOL_GPL(hrtimer_init); |
c0a31329 | 1119 | |
887d9dc9 PZ |
1120 | /* |
1121 | * A timer is active, when it is enqueued into the rbtree or the | |
1122 | * callback function is running or it's in the state of being migrated | |
1123 | * to another cpu. | |
c0a31329 | 1124 | * |
887d9dc9 | 1125 | * It is important for this function to not return a false negative. |
c0a31329 | 1126 | */ |
887d9dc9 | 1127 | bool hrtimer_active(const struct hrtimer *timer) |
c0a31329 | 1128 | { |
3c8aa39d | 1129 | struct hrtimer_cpu_base *cpu_base; |
887d9dc9 | 1130 | unsigned int seq; |
c0a31329 | 1131 | |
887d9dc9 PZ |
1132 | do { |
1133 | cpu_base = READ_ONCE(timer->base->cpu_base); | |
1134 | seq = raw_read_seqcount_begin(&cpu_base->seq); | |
c0a31329 | 1135 | |
887d9dc9 PZ |
1136 | if (timer->state != HRTIMER_STATE_INACTIVE || |
1137 | cpu_base->running == timer) | |
1138 | return true; | |
1139 | ||
1140 | } while (read_seqcount_retry(&cpu_base->seq, seq) || | |
1141 | cpu_base != READ_ONCE(timer->base->cpu_base)); | |
1142 | ||
1143 | return false; | |
c0a31329 | 1144 | } |
887d9dc9 | 1145 | EXPORT_SYMBOL_GPL(hrtimer_active); |
c0a31329 | 1146 | |
887d9dc9 PZ |
1147 | /* |
1148 | * The write_seqcount_barrier()s in __run_hrtimer() split the thing into 3 | |
1149 | * distinct sections: | |
1150 | * | |
1151 | * - queued: the timer is queued | |
1152 | * - callback: the timer is being ran | |
1153 | * - post: the timer is inactive or (re)queued | |
1154 | * | |
1155 | * On the read side we ensure we observe timer->state and cpu_base->running | |
1156 | * from the same section, if anything changed while we looked at it, we retry. | |
1157 | * This includes timer->base changing because sequence numbers alone are | |
1158 | * insufficient for that. | |
1159 | * | |
1160 | * The sequence numbers are required because otherwise we could still observe | |
1161 | * a false negative if the read side got smeared over multiple consequtive | |
1162 | * __run_hrtimer() invocations. | |
1163 | */ | |
1164 | ||
21d6d52a TG |
1165 | static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base, |
1166 | struct hrtimer_clock_base *base, | |
1167 | struct hrtimer *timer, ktime_t *now) | |
d3d74453 | 1168 | { |
d3d74453 PZ |
1169 | enum hrtimer_restart (*fn)(struct hrtimer *); |
1170 | int restart; | |
1171 | ||
887d9dc9 | 1172 | lockdep_assert_held(&cpu_base->lock); |
ca109491 | 1173 | |
c6a2a177 | 1174 | debug_deactivate(timer); |
887d9dc9 PZ |
1175 | cpu_base->running = timer; |
1176 | ||
1177 | /* | |
1178 | * Separate the ->running assignment from the ->state assignment. | |
1179 | * | |
1180 | * As with a regular write barrier, this ensures the read side in | |
1181 | * hrtimer_active() cannot observe cpu_base->running == NULL && | |
1182 | * timer->state == INACTIVE. | |
1183 | */ | |
1184 | raw_write_seqcount_barrier(&cpu_base->seq); | |
1185 | ||
1186 | __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 0); | |
d3d74453 | 1187 | fn = timer->function; |
ca109491 | 1188 | |
203cbf77 TG |
1189 | /* |
1190 | * Clear the 'is relative' flag for the TIME_LOW_RES case. If the | |
1191 | * timer is restarted with a period then it becomes an absolute | |
1192 | * timer. If its not restarted it does not matter. | |
1193 | */ | |
1194 | if (IS_ENABLED(CONFIG_TIME_LOW_RES)) | |
1195 | timer->is_rel = false; | |
1196 | ||
ca109491 | 1197 | /* |
d05ca13b TG |
1198 | * The timer is marked as running in the CPU base, so it is |
1199 | * protected against migration to a different CPU even if the lock | |
1200 | * is dropped. | |
ca109491 | 1201 | */ |
ecb49d1a | 1202 | raw_spin_unlock(&cpu_base->lock); |
c6a2a177 | 1203 | trace_hrtimer_expire_entry(timer, now); |
ca109491 | 1204 | restart = fn(timer); |
c6a2a177 | 1205 | trace_hrtimer_expire_exit(timer); |
ecb49d1a | 1206 | raw_spin_lock(&cpu_base->lock); |
d3d74453 PZ |
1207 | |
1208 | /* | |
887d9dc9 | 1209 | * Note: We clear the running state after enqueue_hrtimer and |
b4d90e9f | 1210 | * we do not reprogram the event hardware. Happens either in |
e3f1d883 | 1211 | * hrtimer_start_range_ns() or in hrtimer_interrupt() |
5de2755c PZ |
1212 | * |
1213 | * Note: Because we dropped the cpu_base->lock above, | |
1214 | * hrtimer_start_range_ns() can have popped in and enqueued the timer | |
1215 | * for us already. | |
d3d74453 | 1216 | */ |
5de2755c PZ |
1217 | if (restart != HRTIMER_NORESTART && |
1218 | !(timer->state & HRTIMER_STATE_ENQUEUED)) | |
a6037b61 | 1219 | enqueue_hrtimer(timer, base); |
f13d4f97 | 1220 | |
887d9dc9 PZ |
1221 | /* |
1222 | * Separate the ->running assignment from the ->state assignment. | |
1223 | * | |
1224 | * As with a regular write barrier, this ensures the read side in | |
1225 | * hrtimer_active() cannot observe cpu_base->running == NULL && | |
1226 | * timer->state == INACTIVE. | |
1227 | */ | |
1228 | raw_write_seqcount_barrier(&cpu_base->seq); | |
f13d4f97 | 1229 | |
887d9dc9 PZ |
1230 | WARN_ON_ONCE(cpu_base->running != timer); |
1231 | cpu_base->running = NULL; | |
d3d74453 PZ |
1232 | } |
1233 | ||
21d6d52a | 1234 | static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now) |
54cdfdb4 | 1235 | { |
34aee88a TG |
1236 | struct hrtimer_clock_base *base = cpu_base->clock_base; |
1237 | unsigned int active = cpu_base->active_bases; | |
6ff7041d | 1238 | |
34aee88a | 1239 | for (; active; base++, active >>= 1) { |
998adc3d | 1240 | struct timerqueue_node *node; |
ab8177bc TG |
1241 | ktime_t basenow; |
1242 | ||
34aee88a | 1243 | if (!(active & 0x01)) |
ab8177bc | 1244 | continue; |
54cdfdb4 | 1245 | |
54cdfdb4 TG |
1246 | basenow = ktime_add(now, base->offset); |
1247 | ||
998adc3d | 1248 | while ((node = timerqueue_getnext(&base->active))) { |
54cdfdb4 TG |
1249 | struct hrtimer *timer; |
1250 | ||
998adc3d | 1251 | timer = container_of(node, struct hrtimer, node); |
54cdfdb4 | 1252 | |
654c8e0b AV |
1253 | /* |
1254 | * The immediate goal for using the softexpires is | |
1255 | * minimizing wakeups, not running timers at the | |
1256 | * earliest interrupt after their soft expiration. | |
1257 | * This allows us to avoid using a Priority Search | |
1258 | * Tree, which can answer a stabbing querry for | |
1259 | * overlapping intervals and instead use the simple | |
1260 | * BST we already have. | |
1261 | * We don't add extra wakeups by delaying timers that | |
1262 | * are right-of a not yet expired timer, because that | |
1263 | * timer will have to trigger a wakeup anyway. | |
1264 | */ | |
2456e855 | 1265 | if (basenow < hrtimer_get_softexpires_tv64(timer)) |
54cdfdb4 | 1266 | break; |
54cdfdb4 | 1267 | |
21d6d52a | 1268 | __run_hrtimer(cpu_base, base, timer, &basenow); |
54cdfdb4 | 1269 | } |
54cdfdb4 | 1270 | } |
21d6d52a TG |
1271 | } |
1272 | ||
1273 | #ifdef CONFIG_HIGH_RES_TIMERS | |
1274 | ||
1275 | /* | |
1276 | * High resolution timer interrupt | |
1277 | * Called with interrupts disabled | |
1278 | */ | |
1279 | void hrtimer_interrupt(struct clock_event_device *dev) | |
1280 | { | |
1281 | struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases); | |
1282 | ktime_t expires_next, now, entry_time, delta; | |
1283 | int retries = 0; | |
1284 | ||
1285 | BUG_ON(!cpu_base->hres_active); | |
1286 | cpu_base->nr_events++; | |
2456e855 | 1287 | dev->next_event = KTIME_MAX; |
21d6d52a TG |
1288 | |
1289 | raw_spin_lock(&cpu_base->lock); | |
1290 | entry_time = now = hrtimer_update_base(cpu_base); | |
1291 | retry: | |
1292 | cpu_base->in_hrtirq = 1; | |
1293 | /* | |
1294 | * We set expires_next to KTIME_MAX here with cpu_base->lock | |
1295 | * held to prevent that a timer is enqueued in our queue via | |
1296 | * the migration code. This does not affect enqueueing of | |
1297 | * timers which run their callback and need to be requeued on | |
1298 | * this CPU. | |
1299 | */ | |
2456e855 | 1300 | cpu_base->expires_next = KTIME_MAX; |
21d6d52a TG |
1301 | |
1302 | __hrtimer_run_queues(cpu_base, now); | |
1303 | ||
9bc74919 TG |
1304 | /* Reevaluate the clock bases for the next expiry */ |
1305 | expires_next = __hrtimer_get_next_event(cpu_base); | |
6ff7041d TG |
1306 | /* |
1307 | * Store the new expiry value so the migration code can verify | |
1308 | * against it. | |
1309 | */ | |
54cdfdb4 | 1310 | cpu_base->expires_next = expires_next; |
9bc74919 | 1311 | cpu_base->in_hrtirq = 0; |
ecb49d1a | 1312 | raw_spin_unlock(&cpu_base->lock); |
54cdfdb4 TG |
1313 | |
1314 | /* Reprogramming necessary ? */ | |
d2540875 | 1315 | if (!tick_program_event(expires_next, 0)) { |
41d2e494 TG |
1316 | cpu_base->hang_detected = 0; |
1317 | return; | |
54cdfdb4 | 1318 | } |
41d2e494 TG |
1319 | |
1320 | /* | |
1321 | * The next timer was already expired due to: | |
1322 | * - tracing | |
1323 | * - long lasting callbacks | |
1324 | * - being scheduled away when running in a VM | |
1325 | * | |
1326 | * We need to prevent that we loop forever in the hrtimer | |
1327 | * interrupt routine. We give it 3 attempts to avoid | |
1328 | * overreacting on some spurious event. | |
5baefd6d JS |
1329 | * |
1330 | * Acquire base lock for updating the offsets and retrieving | |
1331 | * the current time. | |
41d2e494 | 1332 | */ |
196951e9 | 1333 | raw_spin_lock(&cpu_base->lock); |
5baefd6d | 1334 | now = hrtimer_update_base(cpu_base); |
41d2e494 TG |
1335 | cpu_base->nr_retries++; |
1336 | if (++retries < 3) | |
1337 | goto retry; | |
1338 | /* | |
1339 | * Give the system a chance to do something else than looping | |
1340 | * here. We stored the entry time, so we know exactly how long | |
1341 | * we spent here. We schedule the next event this amount of | |
1342 | * time away. | |
1343 | */ | |
1344 | cpu_base->nr_hangs++; | |
1345 | cpu_base->hang_detected = 1; | |
196951e9 | 1346 | raw_spin_unlock(&cpu_base->lock); |
41d2e494 | 1347 | delta = ktime_sub(now, entry_time); |
2456e855 TG |
1348 | if ((unsigned int)delta > cpu_base->max_hang_time) |
1349 | cpu_base->max_hang_time = (unsigned int) delta; | |
41d2e494 TG |
1350 | /* |
1351 | * Limit it to a sensible value as we enforce a longer | |
1352 | * delay. Give the CPU at least 100ms to catch up. | |
1353 | */ | |
2456e855 | 1354 | if (delta > 100 * NSEC_PER_MSEC) |
41d2e494 TG |
1355 | expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC); |
1356 | else | |
1357 | expires_next = ktime_add(now, delta); | |
1358 | tick_program_event(expires_next, 1); | |
1359 | printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n", | |
1360 | ktime_to_ns(delta)); | |
54cdfdb4 TG |
1361 | } |
1362 | ||
016da201 | 1363 | /* called with interrupts disabled */ |
c6eb3f70 | 1364 | static inline void __hrtimer_peek_ahead_timers(void) |
8bdec955 TG |
1365 | { |
1366 | struct tick_device *td; | |
1367 | ||
1368 | if (!hrtimer_hres_active()) | |
1369 | return; | |
1370 | ||
22127e93 | 1371 | td = this_cpu_ptr(&tick_cpu_device); |
8bdec955 TG |
1372 | if (td && td->evtdev) |
1373 | hrtimer_interrupt(td->evtdev); | |
1374 | } | |
1375 | ||
82c5b7b5 IM |
1376 | #else /* CONFIG_HIGH_RES_TIMERS */ |
1377 | ||
1378 | static inline void __hrtimer_peek_ahead_timers(void) { } | |
1379 | ||
1380 | #endif /* !CONFIG_HIGH_RES_TIMERS */ | |
82f67cd9 | 1381 | |
d3d74453 | 1382 | /* |
c6eb3f70 | 1383 | * Called from run_local_timers in hardirq context every jiffy |
d3d74453 | 1384 | */ |
833883d9 | 1385 | void hrtimer_run_queues(void) |
d3d74453 | 1386 | { |
dc5df73b | 1387 | struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases); |
21d6d52a | 1388 | ktime_t now; |
c0a31329 | 1389 | |
e19ffe8b | 1390 | if (__hrtimer_hres_active(cpu_base)) |
d3d74453 | 1391 | return; |
54cdfdb4 | 1392 | |
d3d74453 | 1393 | /* |
c6eb3f70 TG |
1394 | * This _is_ ugly: We have to check periodically, whether we |
1395 | * can switch to highres and / or nohz mode. The clocksource | |
1396 | * switch happens with xtime_lock held. Notification from | |
1397 | * there only sets the check bit in the tick_oneshot code, | |
1398 | * otherwise we might deadlock vs. xtime_lock. | |
d3d74453 | 1399 | */ |
c6eb3f70 | 1400 | if (tick_check_oneshot_change(!hrtimer_is_hres_enabled())) { |
d3d74453 | 1401 | hrtimer_switch_to_hres(); |
3055adda | 1402 | return; |
833883d9 | 1403 | } |
c6eb3f70 | 1404 | |
21d6d52a TG |
1405 | raw_spin_lock(&cpu_base->lock); |
1406 | now = hrtimer_update_base(cpu_base); | |
1407 | __hrtimer_run_queues(cpu_base, now); | |
1408 | raw_spin_unlock(&cpu_base->lock); | |
c0a31329 TG |
1409 | } |
1410 | ||
10c94ec1 TG |
1411 | /* |
1412 | * Sleep related functions: | |
1413 | */ | |
c9cb2e3d | 1414 | static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer) |
00362e33 TG |
1415 | { |
1416 | struct hrtimer_sleeper *t = | |
1417 | container_of(timer, struct hrtimer_sleeper, timer); | |
1418 | struct task_struct *task = t->task; | |
1419 | ||
1420 | t->task = NULL; | |
1421 | if (task) | |
1422 | wake_up_process(task); | |
1423 | ||
1424 | return HRTIMER_NORESTART; | |
1425 | } | |
1426 | ||
36c8b586 | 1427 | void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task) |
00362e33 TG |
1428 | { |
1429 | sl->timer.function = hrtimer_wakeup; | |
1430 | sl->task = task; | |
1431 | } | |
2bc481cf | 1432 | EXPORT_SYMBOL_GPL(hrtimer_init_sleeper); |
00362e33 | 1433 | |
c0edd7c9 | 1434 | int nanosleep_copyout(struct restart_block *restart, struct timespec64 *ts) |
ce41aaf4 AV |
1435 | { |
1436 | switch(restart->nanosleep.type) { | |
1437 | #ifdef CONFIG_COMPAT | |
1438 | case TT_COMPAT: | |
c0edd7c9 | 1439 | if (compat_put_timespec64(ts, restart->nanosleep.compat_rmtp)) |
ce41aaf4 AV |
1440 | return -EFAULT; |
1441 | break; | |
1442 | #endif | |
1443 | case TT_NATIVE: | |
c0edd7c9 | 1444 | if (put_timespec64(ts, restart->nanosleep.rmtp)) |
ce41aaf4 AV |
1445 | return -EFAULT; |
1446 | break; | |
1447 | default: | |
1448 | BUG(); | |
1449 | } | |
1450 | return -ERESTART_RESTARTBLOCK; | |
1451 | } | |
1452 | ||
669d7868 | 1453 | static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode) |
432569bb | 1454 | { |
edbeda46 AV |
1455 | struct restart_block *restart; |
1456 | ||
669d7868 | 1457 | hrtimer_init_sleeper(t, current); |
10c94ec1 | 1458 | |
432569bb RZ |
1459 | do { |
1460 | set_current_state(TASK_INTERRUPTIBLE); | |
cc584b21 | 1461 | hrtimer_start_expires(&t->timer, mode); |
432569bb | 1462 | |
54cdfdb4 | 1463 | if (likely(t->task)) |
b0f8c44f | 1464 | freezable_schedule(); |
432569bb | 1465 | |
669d7868 | 1466 | hrtimer_cancel(&t->timer); |
c9cb2e3d | 1467 | mode = HRTIMER_MODE_ABS; |
669d7868 TG |
1468 | |
1469 | } while (t->task && !signal_pending(current)); | |
432569bb | 1470 | |
3588a085 PZ |
1471 | __set_current_state(TASK_RUNNING); |
1472 | ||
a7602681 | 1473 | if (!t->task) |
080344b9 | 1474 | return 0; |
080344b9 | 1475 | |
edbeda46 AV |
1476 | restart = ¤t->restart_block; |
1477 | if (restart->nanosleep.type != TT_NONE) { | |
a7602681 | 1478 | ktime_t rem = hrtimer_expires_remaining(&t->timer); |
c0edd7c9 | 1479 | struct timespec64 rmt; |
edbeda46 | 1480 | |
a7602681 AV |
1481 | if (rem <= 0) |
1482 | return 0; | |
c0edd7c9 | 1483 | rmt = ktime_to_timespec64(rem); |
a7602681 | 1484 | |
ce41aaf4 | 1485 | return nanosleep_copyout(restart, &rmt); |
a7602681 AV |
1486 | } |
1487 | return -ERESTART_RESTARTBLOCK; | |
080344b9 ON |
1488 | } |
1489 | ||
fb923c4a | 1490 | static long __sched hrtimer_nanosleep_restart(struct restart_block *restart) |
10c94ec1 | 1491 | { |
669d7868 | 1492 | struct hrtimer_sleeper t; |
a7602681 | 1493 | int ret; |
10c94ec1 | 1494 | |
ab8177bc | 1495 | hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid, |
237fc6e7 | 1496 | HRTIMER_MODE_ABS); |
cc584b21 | 1497 | hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires); |
10c94ec1 | 1498 | |
a7602681 | 1499 | ret = do_nanosleep(&t, HRTIMER_MODE_ABS); |
237fc6e7 TG |
1500 | destroy_hrtimer_on_stack(&t.timer); |
1501 | return ret; | |
10c94ec1 TG |
1502 | } |
1503 | ||
938e7cf2 | 1504 | long hrtimer_nanosleep(const struct timespec64 *rqtp, |
10c94ec1 TG |
1505 | const enum hrtimer_mode mode, const clockid_t clockid) |
1506 | { | |
a7602681 | 1507 | struct restart_block *restart; |
669d7868 | 1508 | struct hrtimer_sleeper t; |
237fc6e7 | 1509 | int ret = 0; |
da8b44d5 | 1510 | u64 slack; |
3bd01206 AV |
1511 | |
1512 | slack = current->timer_slack_ns; | |
aab03e05 | 1513 | if (dl_task(current) || rt_task(current)) |
3bd01206 | 1514 | slack = 0; |
10c94ec1 | 1515 | |
237fc6e7 | 1516 | hrtimer_init_on_stack(&t.timer, clockid, mode); |
ad196384 | 1517 | hrtimer_set_expires_range_ns(&t.timer, timespec64_to_ktime(*rqtp), slack); |
a7602681 AV |
1518 | ret = do_nanosleep(&t, mode); |
1519 | if (ret != -ERESTART_RESTARTBLOCK) | |
237fc6e7 | 1520 | goto out; |
10c94ec1 | 1521 | |
7978672c | 1522 | /* Absolute timers do not update the rmtp value and restart: */ |
237fc6e7 TG |
1523 | if (mode == HRTIMER_MODE_ABS) { |
1524 | ret = -ERESTARTNOHAND; | |
1525 | goto out; | |
1526 | } | |
10c94ec1 | 1527 | |
a7602681 | 1528 | restart = ¤t->restart_block; |
1711ef38 | 1529 | restart->fn = hrtimer_nanosleep_restart; |
ab8177bc | 1530 | restart->nanosleep.clockid = t.timer.base->clockid; |
cc584b21 | 1531 | restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer); |
237fc6e7 TG |
1532 | out: |
1533 | destroy_hrtimer_on_stack(&t.timer); | |
1534 | return ret; | |
10c94ec1 TG |
1535 | } |
1536 | ||
58fd3aa2 HC |
1537 | SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp, |
1538 | struct timespec __user *, rmtp) | |
6ba1b912 | 1539 | { |
c0edd7c9 | 1540 | struct timespec64 tu; |
6ba1b912 | 1541 | |
c0edd7c9 | 1542 | if (get_timespec64(&tu, rqtp)) |
6ba1b912 TG |
1543 | return -EFAULT; |
1544 | ||
c0edd7c9 | 1545 | if (!timespec64_valid(&tu)) |
6ba1b912 TG |
1546 | return -EINVAL; |
1547 | ||
edbeda46 | 1548 | current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE; |
192a82f9 | 1549 | current->restart_block.nanosleep.rmtp = rmtp; |
c0edd7c9 | 1550 | return hrtimer_nanosleep(&tu, HRTIMER_MODE_REL, CLOCK_MONOTONIC); |
6ba1b912 TG |
1551 | } |
1552 | ||
edbeda46 AV |
1553 | #ifdef CONFIG_COMPAT |
1554 | ||
1555 | COMPAT_SYSCALL_DEFINE2(nanosleep, struct compat_timespec __user *, rqtp, | |
1556 | struct compat_timespec __user *, rmtp) | |
1557 | { | |
c0edd7c9 | 1558 | struct timespec64 tu; |
edbeda46 | 1559 | |
c0edd7c9 | 1560 | if (compat_get_timespec64(&tu, rqtp)) |
edbeda46 AV |
1561 | return -EFAULT; |
1562 | ||
c0edd7c9 | 1563 | if (!timespec64_valid(&tu)) |
edbeda46 AV |
1564 | return -EINVAL; |
1565 | ||
1566 | current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE; | |
1567 | current->restart_block.nanosleep.compat_rmtp = rmtp; | |
c0edd7c9 | 1568 | return hrtimer_nanosleep(&tu, HRTIMER_MODE_REL, CLOCK_MONOTONIC); |
edbeda46 AV |
1569 | } |
1570 | #endif | |
1571 | ||
c0a31329 TG |
1572 | /* |
1573 | * Functions related to boot-time initialization: | |
1574 | */ | |
27590dc1 | 1575 | int hrtimers_prepare_cpu(unsigned int cpu) |
c0a31329 | 1576 | { |
3c8aa39d | 1577 | struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu); |
c0a31329 TG |
1578 | int i; |
1579 | ||
998adc3d | 1580 | for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) { |
3c8aa39d | 1581 | cpu_base->clock_base[i].cpu_base = cpu_base; |
998adc3d JS |
1582 | timerqueue_init_head(&cpu_base->clock_base[i].active); |
1583 | } | |
3c8aa39d | 1584 | |
cddd0248 | 1585 | cpu_base->cpu = cpu; |
54cdfdb4 | 1586 | hrtimer_init_hres(cpu_base); |
27590dc1 | 1587 | return 0; |
c0a31329 TG |
1588 | } |
1589 | ||
1590 | #ifdef CONFIG_HOTPLUG_CPU | |
1591 | ||
ca109491 | 1592 | static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base, |
37810659 | 1593 | struct hrtimer_clock_base *new_base) |
c0a31329 TG |
1594 | { |
1595 | struct hrtimer *timer; | |
998adc3d | 1596 | struct timerqueue_node *node; |
c0a31329 | 1597 | |
998adc3d JS |
1598 | while ((node = timerqueue_getnext(&old_base->active))) { |
1599 | timer = container_of(node, struct hrtimer, node); | |
54cdfdb4 | 1600 | BUG_ON(hrtimer_callback_running(timer)); |
c6a2a177 | 1601 | debug_deactivate(timer); |
b00c1a99 TG |
1602 | |
1603 | /* | |
c04dca02 | 1604 | * Mark it as ENQUEUED not INACTIVE otherwise the |
b00c1a99 TG |
1605 | * timer could be seen as !active and just vanish away |
1606 | * under us on another CPU | |
1607 | */ | |
c04dca02 | 1608 | __remove_hrtimer(timer, old_base, HRTIMER_STATE_ENQUEUED, 0); |
c0a31329 | 1609 | timer->base = new_base; |
54cdfdb4 | 1610 | /* |
e3f1d883 TG |
1611 | * Enqueue the timers on the new cpu. This does not |
1612 | * reprogram the event device in case the timer | |
1613 | * expires before the earliest on this CPU, but we run | |
1614 | * hrtimer_interrupt after we migrated everything to | |
1615 | * sort out already expired timers and reprogram the | |
1616 | * event device. | |
54cdfdb4 | 1617 | */ |
a6037b61 | 1618 | enqueue_hrtimer(timer, new_base); |
c0a31329 TG |
1619 | } |
1620 | } | |
1621 | ||
27590dc1 | 1622 | int hrtimers_dead_cpu(unsigned int scpu) |
c0a31329 | 1623 | { |
3c8aa39d | 1624 | struct hrtimer_cpu_base *old_base, *new_base; |
731a55ba | 1625 | int i; |
c0a31329 | 1626 | |
37810659 | 1627 | BUG_ON(cpu_online(scpu)); |
37810659 | 1628 | tick_cancel_sched_timer(scpu); |
731a55ba TG |
1629 | |
1630 | local_irq_disable(); | |
1631 | old_base = &per_cpu(hrtimer_bases, scpu); | |
dc5df73b | 1632 | new_base = this_cpu_ptr(&hrtimer_bases); |
d82f0b0f ON |
1633 | /* |
1634 | * The caller is globally serialized and nobody else | |
1635 | * takes two locks at once, deadlock is not possible. | |
1636 | */ | |
ecb49d1a TG |
1637 | raw_spin_lock(&new_base->lock); |
1638 | raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING); | |
c0a31329 | 1639 | |
3c8aa39d | 1640 | for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) { |
ca109491 | 1641 | migrate_hrtimer_list(&old_base->clock_base[i], |
37810659 | 1642 | &new_base->clock_base[i]); |
c0a31329 TG |
1643 | } |
1644 | ||
ecb49d1a TG |
1645 | raw_spin_unlock(&old_base->lock); |
1646 | raw_spin_unlock(&new_base->lock); | |
37810659 | 1647 | |
731a55ba TG |
1648 | /* Check, if we got expired work to do */ |
1649 | __hrtimer_peek_ahead_timers(); | |
1650 | local_irq_enable(); | |
27590dc1 | 1651 | return 0; |
c0a31329 | 1652 | } |
37810659 | 1653 | |
c0a31329 TG |
1654 | #endif /* CONFIG_HOTPLUG_CPU */ |
1655 | ||
c0a31329 TG |
1656 | void __init hrtimers_init(void) |
1657 | { | |
27590dc1 | 1658 | hrtimers_prepare_cpu(smp_processor_id()); |
c0a31329 TG |
1659 | } |
1660 | ||
7bb67439 | 1661 | /** |
351b3f7a | 1662 | * schedule_hrtimeout_range_clock - sleep until timeout |
7bb67439 | 1663 | * @expires: timeout value (ktime_t) |
654c8e0b | 1664 | * @delta: slack in expires timeout (ktime_t) |
7bb67439 | 1665 | * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL |
351b3f7a | 1666 | * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME |
7bb67439 | 1667 | */ |
351b3f7a | 1668 | int __sched |
da8b44d5 | 1669 | schedule_hrtimeout_range_clock(ktime_t *expires, u64 delta, |
351b3f7a | 1670 | const enum hrtimer_mode mode, int clock) |
7bb67439 AV |
1671 | { |
1672 | struct hrtimer_sleeper t; | |
1673 | ||
1674 | /* | |
1675 | * Optimize when a zero timeout value is given. It does not | |
1676 | * matter whether this is an absolute or a relative time. | |
1677 | */ | |
2456e855 | 1678 | if (expires && *expires == 0) { |
7bb67439 AV |
1679 | __set_current_state(TASK_RUNNING); |
1680 | return 0; | |
1681 | } | |
1682 | ||
1683 | /* | |
43b21013 | 1684 | * A NULL parameter means "infinite" |
7bb67439 AV |
1685 | */ |
1686 | if (!expires) { | |
1687 | schedule(); | |
7bb67439 AV |
1688 | return -EINTR; |
1689 | } | |
1690 | ||
351b3f7a | 1691 | hrtimer_init_on_stack(&t.timer, clock, mode); |
654c8e0b | 1692 | hrtimer_set_expires_range_ns(&t.timer, *expires, delta); |
7bb67439 AV |
1693 | |
1694 | hrtimer_init_sleeper(&t, current); | |
1695 | ||
cc584b21 | 1696 | hrtimer_start_expires(&t.timer, mode); |
7bb67439 AV |
1697 | |
1698 | if (likely(t.task)) | |
1699 | schedule(); | |
1700 | ||
1701 | hrtimer_cancel(&t.timer); | |
1702 | destroy_hrtimer_on_stack(&t.timer); | |
1703 | ||
1704 | __set_current_state(TASK_RUNNING); | |
1705 | ||
1706 | return !t.task ? 0 : -EINTR; | |
1707 | } | |
351b3f7a CE |
1708 | |
1709 | /** | |
1710 | * schedule_hrtimeout_range - sleep until timeout | |
1711 | * @expires: timeout value (ktime_t) | |
1712 | * @delta: slack in expires timeout (ktime_t) | |
1713 | * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL | |
1714 | * | |
1715 | * Make the current task sleep until the given expiry time has | |
1716 | * elapsed. The routine will return immediately unless | |
1717 | * the current task state has been set (see set_current_state()). | |
1718 | * | |
1719 | * The @delta argument gives the kernel the freedom to schedule the | |
1720 | * actual wakeup to a time that is both power and performance friendly. | |
1721 | * The kernel give the normal best effort behavior for "@expires+@delta", | |
1722 | * but may decide to fire the timer earlier, but no earlier than @expires. | |
1723 | * | |
1724 | * You can set the task state as follows - | |
1725 | * | |
1726 | * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to | |
4b7e9cf9 DA |
1727 | * pass before the routine returns unless the current task is explicitly |
1728 | * woken up, (e.g. by wake_up_process()). | |
351b3f7a CE |
1729 | * |
1730 | * %TASK_INTERRUPTIBLE - the routine may return early if a signal is | |
4b7e9cf9 DA |
1731 | * delivered to the current task or the current task is explicitly woken |
1732 | * up. | |
351b3f7a CE |
1733 | * |
1734 | * The current task state is guaranteed to be TASK_RUNNING when this | |
1735 | * routine returns. | |
1736 | * | |
4b7e9cf9 DA |
1737 | * Returns 0 when the timer has expired. If the task was woken before the |
1738 | * timer expired by a signal (only possible in state TASK_INTERRUPTIBLE) or | |
1739 | * by an explicit wakeup, it returns -EINTR. | |
351b3f7a | 1740 | */ |
da8b44d5 | 1741 | int __sched schedule_hrtimeout_range(ktime_t *expires, u64 delta, |
351b3f7a CE |
1742 | const enum hrtimer_mode mode) |
1743 | { | |
1744 | return schedule_hrtimeout_range_clock(expires, delta, mode, | |
1745 | CLOCK_MONOTONIC); | |
1746 | } | |
654c8e0b AV |
1747 | EXPORT_SYMBOL_GPL(schedule_hrtimeout_range); |
1748 | ||
1749 | /** | |
1750 | * schedule_hrtimeout - sleep until timeout | |
1751 | * @expires: timeout value (ktime_t) | |
1752 | * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL | |
1753 | * | |
1754 | * Make the current task sleep until the given expiry time has | |
1755 | * elapsed. The routine will return immediately unless | |
1756 | * the current task state has been set (see set_current_state()). | |
1757 | * | |
1758 | * You can set the task state as follows - | |
1759 | * | |
1760 | * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to | |
4b7e9cf9 DA |
1761 | * pass before the routine returns unless the current task is explicitly |
1762 | * woken up, (e.g. by wake_up_process()). | |
654c8e0b AV |
1763 | * |
1764 | * %TASK_INTERRUPTIBLE - the routine may return early if a signal is | |
4b7e9cf9 DA |
1765 | * delivered to the current task or the current task is explicitly woken |
1766 | * up. | |
654c8e0b AV |
1767 | * |
1768 | * The current task state is guaranteed to be TASK_RUNNING when this | |
1769 | * routine returns. | |
1770 | * | |
4b7e9cf9 DA |
1771 | * Returns 0 when the timer has expired. If the task was woken before the |
1772 | * timer expired by a signal (only possible in state TASK_INTERRUPTIBLE) or | |
1773 | * by an explicit wakeup, it returns -EINTR. | |
654c8e0b AV |
1774 | */ |
1775 | int __sched schedule_hrtimeout(ktime_t *expires, | |
1776 | const enum hrtimer_mode mode) | |
1777 | { | |
1778 | return schedule_hrtimeout_range(expires, 0, mode); | |
1779 | } | |
7bb67439 | 1780 | EXPORT_SYMBOL_GPL(schedule_hrtimeout); |