blob: 9abd50b60e83e6457d2686aaf1178c53d8da29cc [file] [log] [blame]
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001/*
2 * linux/kernel/hrtimer.c
3 *
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08004 * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08005 * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08006 * Copyright(C) 2006-2007 Timesys Corp., Thomas Gleixner
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08007 *
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 *
Thomas Gleixner66188fa2006-02-01 03:05:13 -080025 * Help, testing, suggestions, bugfixes, improvements were
26 * provided by:
27 *
28 * George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel
29 * et. al.
30 *
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080031 * For licencing details see kernel-base/COPYING
32 */
33
34#include <linux/cpu.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040035#include <linux/export.h>
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080036#include <linux/percpu.h>
37#include <linux/hrtimer.h>
38#include <linux/notifier.h>
39#include <linux/syscalls.h>
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080040#include <linux/kallsyms.h>
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080041#include <linux/interrupt.h>
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080042#include <linux/tick.h>
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080043#include <linux/seq_file.h>
44#include <linux/err.h>
Thomas Gleixner237fc6e2008-04-30 00:55:04 -070045#include <linux/debugobjects.h>
Arun R Bharadwajeea08f32009-04-16 12:16:41 +053046#include <linux/sched.h>
Clark Williamscf4aebc22013-02-07 09:46:59 -060047#include <linux/sched/sysctl.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060048#include <linux/sched/rt.h>
Dario Faggioliaab03e02013-11-28 11:14:43 +010049#include <linux/sched/deadline.h>
Arun R Bharadwajeea08f32009-04-16 12:16:41 +053050#include <linux/timer.h>
Colin Crossb0f8c442013-05-06 23:50:19 +000051#include <linux/freezer.h>
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080052
53#include <asm/uaccess.h>
54
Xiao Guangrongc6a2a172009-08-10 10:51:23 +080055#include <trace/events/timer.h>
56
Thomas Gleixnerc1797ba2015-03-25 13:07:37 +010057#include "tick-internal.h"
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000058
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080059/*
60 * The timer bases:
George Anzinger7978672c2006-02-01 03:05:11 -080061 *
John Stultze06383d2010-12-14 19:37:07 -080062 * There are more clockids then hrtimer bases. Thus, we index
63 * into the timer bases by the hrtimer_base_type enum. When trying
64 * to reach a base using a clockid, hrtimer_clockid_to_base()
65 * is used to convert from clockid to the proper hrtimer_base_type.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080066 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080067DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080068{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080069
Michael Bohan84cc8fd2013-03-19 19:19:25 -070070 .lock = __RAW_SPIN_LOCK_UNLOCKED(hrtimer_bases.lock),
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080071 .clock_base =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080072 {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080073 {
Thomas Gleixnerab8177b2011-05-20 13:05:15 +020074 .index = HRTIMER_BASE_MONOTONIC,
75 .clockid = CLOCK_MONOTONIC,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080076 .get_time = &ktime_get,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080077 .resolution = KTIME_LOW_RES,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080078 },
John Stultz70a08cc2011-02-15 10:45:16 -080079 {
Thomas Gleixner68fa61c2011-05-20 23:14:04 +020080 .index = HRTIMER_BASE_REALTIME,
81 .clockid = CLOCK_REALTIME,
82 .get_time = &ktime_get_real,
83 .resolution = KTIME_LOW_RES,
84 },
85 {
Thomas Gleixnerab8177b2011-05-20 13:05:15 +020086 .index = HRTIMER_BASE_BOOTTIME,
87 .clockid = CLOCK_BOOTTIME,
John Stultz70a08cc2011-02-15 10:45:16 -080088 .get_time = &ktime_get_boottime,
89 .resolution = KTIME_LOW_RES,
90 },
John Stultz90adda92013-01-21 17:00:11 -080091 {
92 .index = HRTIMER_BASE_TAI,
93 .clockid = CLOCK_TAI,
94 .get_time = &ktime_get_clocktai,
95 .resolution = KTIME_LOW_RES,
96 },
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080097 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080098};
99
Mike Frysinger942c3c52011-05-02 15:24:27 -0400100static const int hrtimer_clock_to_base_table[MAX_CLOCKS] = {
Thomas Gleixnerce313322011-04-29 00:02:00 +0200101 [CLOCK_REALTIME] = HRTIMER_BASE_REALTIME,
102 [CLOCK_MONOTONIC] = HRTIMER_BASE_MONOTONIC,
103 [CLOCK_BOOTTIME] = HRTIMER_BASE_BOOTTIME,
John Stultz90adda92013-01-21 17:00:11 -0800104 [CLOCK_TAI] = HRTIMER_BASE_TAI,
Thomas Gleixnerce313322011-04-29 00:02:00 +0200105};
John Stultze06383d2010-12-14 19:37:07 -0800106
107static inline int hrtimer_clockid_to_base(clockid_t clock_id)
108{
109 return hrtimer_clock_to_base_table[clock_id];
110}
111
112
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800113/*
Thomas Gleixner92127c72006-03-26 01:38:05 -0800114 * Get the coarse grained time at the softirq based on xtime and
115 * wall_to_monotonic.
116 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800117static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
Thomas Gleixner92127c72006-03-26 01:38:05 -0800118{
John Stultz76f41082014-07-16 21:03:52 +0000119 ktime_t xtim, mono, boot, tai;
120 ktime_t off_real, off_boot, off_tai;
Thomas Gleixner92127c72006-03-26 01:38:05 -0800121
John Stultz76f41082014-07-16 21:03:52 +0000122 mono = ktime_get_update_offsets_tick(&off_real, &off_boot, &off_tai);
123 boot = ktime_add(mono, off_boot);
124 xtim = ktime_add(mono, off_real);
John Stultz2d926c12015-02-04 16:45:26 -0800125 tai = ktime_add(mono, off_tai);
Thomas Gleixner92127c72006-03-26 01:38:05 -0800126
John Stultze06383d2010-12-14 19:37:07 -0800127 base->clock_base[HRTIMER_BASE_REALTIME].softirq_time = xtim;
John Stultz70a08cc2011-02-15 10:45:16 -0800128 base->clock_base[HRTIMER_BASE_MONOTONIC].softirq_time = mono;
129 base->clock_base[HRTIMER_BASE_BOOTTIME].softirq_time = boot;
John Stultz76f41082014-07-16 21:03:52 +0000130 base->clock_base[HRTIMER_BASE_TAI].softirq_time = tai;
Thomas Gleixner92127c72006-03-26 01:38:05 -0800131}
132
133/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800134 * Functions and macros which are different for UP/SMP systems are kept in a
135 * single place
136 */
137#ifdef CONFIG_SMP
138
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800139/*
140 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
141 * means that all timers which are tied to this base via timer->base are
142 * locked, and the base itself is locked too.
143 *
144 * So __run_timers/migrate_timers can safely modify all timers which could
145 * be found on the lists/queues.
146 *
147 * When the timer's base is locked, and the timer removed from list, it is
148 * possible to set timer->base = NULL and drop the lock: the timer remains
149 * locked.
150 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800151static
152struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
153 unsigned long *flags)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800154{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800155 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800156
157 for (;;) {
158 base = timer->base;
159 if (likely(base != NULL)) {
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100160 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800161 if (likely(base == timer->base))
162 return base;
163 /* The timer has migrated to another CPU: */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100164 raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800165 }
166 cpu_relax();
167 }
168}
169
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200170/*
171 * With HIGHRES=y we do not migrate the timer when it is expiring
172 * before the next event on the target cpu because we cannot reprogram
173 * the target cpu hardware and we would cause it to fire late.
174 *
175 * Called with cpu_base->lock of target cpu held.
176 */
177static int
178hrtimer_check_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base)
179{
180#ifdef CONFIG_HIGH_RES_TIMERS
181 ktime_t expires;
182
183 if (!new_base->cpu_base->hres_active)
184 return 0;
185
186 expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
187 return expires.tv64 <= new_base->cpu_base->expires_next.tv64;
188#else
189 return 0;
190#endif
191}
192
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800193/*
194 * Switch the timer base to the current CPU when possible.
195 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800196static inline struct hrtimer_clock_base *
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530197switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
198 int pinned)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800199{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800200 struct hrtimer_clock_base *new_base;
201 struct hrtimer_cpu_base *new_cpu_base;
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200202 int this_cpu = smp_processor_id();
Viresh Kumar6201b4d2014-03-18 16:26:07 +0530203 int cpu = get_nohz_timer_target(pinned);
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200204 int basenum = base->index;
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530205
206again:
207 new_cpu_base = &per_cpu(hrtimer_bases, cpu);
John Stultze06383d2010-12-14 19:37:07 -0800208 new_base = &new_cpu_base->clock_base[basenum];
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800209
210 if (base != new_base) {
211 /*
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200212 * We are trying to move timer to new_base.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800213 * However we can't change timer's base while it is running,
214 * so we keep it on the same CPU. No hassle vs. reprogramming
215 * the event source in the high resolution case. The softirq
216 * code will take care of this when the timer function has
217 * completed. There is no conflict as we hold the lock until
218 * the timer is enqueued.
219 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800220 if (unlikely(hrtimer_callback_running(timer)))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800221 return base;
222
223 /* See the comment in lock_timer_base() */
224 timer->base = NULL;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100225 raw_spin_unlock(&base->cpu_base->lock);
226 raw_spin_lock(&new_base->cpu_base->lock);
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530227
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200228 if (cpu != this_cpu && hrtimer_check_target(timer, new_base)) {
229 cpu = this_cpu;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100230 raw_spin_unlock(&new_base->cpu_base->lock);
231 raw_spin_lock(&base->cpu_base->lock);
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200232 timer->base = base;
233 goto again;
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530234 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800235 timer->base = new_base;
Leon Ma012a45e2014-04-30 16:43:10 +0800236 } else {
237 if (cpu != this_cpu && hrtimer_check_target(timer, new_base)) {
238 cpu = this_cpu;
239 goto again;
240 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800241 }
242 return new_base;
243}
244
245#else /* CONFIG_SMP */
246
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800247static inline struct hrtimer_clock_base *
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800248lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
249{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800250 struct hrtimer_clock_base *base = timer->base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800251
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100252 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800253
254 return base;
255}
256
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530257# define switch_hrtimer_base(t, b, p) (b)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800258
259#endif /* !CONFIG_SMP */
260
261/*
262 * Functions for the union type storage format of ktime_t which are
263 * too large for inlining:
264 */
265#if BITS_PER_LONG < 64
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800266/*
267 * Divide a ktime value by a nanosecond value
268 */
Nicolas Pitre8b618622014-12-03 14:43:06 -0500269u64 __ktime_divns(const ktime_t kt, s64 div)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800270{
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300271 u64 dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800272 int sft = 0;
273
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300274 dclc = ktime_to_ns(kt);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800275 /* Make sure the divisor is less than 2^32: */
276 while (div >> 32) {
277 sft++;
278 div >>= 1;
279 }
280 dclc >>= sft;
281 do_div(dclc, (unsigned long) div);
282
Davide Libenzi4d672e72008-02-04 22:27:26 -0800283 return dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800284}
Nicolas Pitre8b618622014-12-03 14:43:06 -0500285EXPORT_SYMBOL_GPL(__ktime_divns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800286#endif /* BITS_PER_LONG >= 64 */
287
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100288/*
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100289 * Add two ktime values and do a safety check for overflow:
290 */
291ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
292{
293 ktime_t res = ktime_add(lhs, rhs);
294
295 /*
296 * We use KTIME_SEC_MAX here, the maximum timeout which we can
297 * return to user space in a timespec:
298 */
299 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
300 res = ktime_set(KTIME_SEC_MAX, 0);
301
302 return res;
303}
304
Artem Bityutskiy8daa21e2009-05-28 16:21:24 +0300305EXPORT_SYMBOL_GPL(ktime_add_safe);
306
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700307#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
308
309static struct debug_obj_descr hrtimer_debug_descr;
310
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100311static void *hrtimer_debug_hint(void *addr)
312{
313 return ((struct hrtimer *) addr)->function;
314}
315
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700316/*
317 * fixup_init is called when:
318 * - an active object is initialized
319 */
320static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
321{
322 struct hrtimer *timer = addr;
323
324 switch (state) {
325 case ODEBUG_STATE_ACTIVE:
326 hrtimer_cancel(timer);
327 debug_object_init(timer, &hrtimer_debug_descr);
328 return 1;
329 default:
330 return 0;
331 }
332}
333
334/*
335 * fixup_activate is called when:
336 * - an active object is activated
337 * - an unknown object is activated (might be a statically initialized object)
338 */
339static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
340{
341 switch (state) {
342
343 case ODEBUG_STATE_NOTAVAILABLE:
344 WARN_ON_ONCE(1);
345 return 0;
346
347 case ODEBUG_STATE_ACTIVE:
348 WARN_ON(1);
349
350 default:
351 return 0;
352 }
353}
354
355/*
356 * fixup_free is called when:
357 * - an active object is freed
358 */
359static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
360{
361 struct hrtimer *timer = addr;
362
363 switch (state) {
364 case ODEBUG_STATE_ACTIVE:
365 hrtimer_cancel(timer);
366 debug_object_free(timer, &hrtimer_debug_descr);
367 return 1;
368 default:
369 return 0;
370 }
371}
372
373static struct debug_obj_descr hrtimer_debug_descr = {
374 .name = "hrtimer",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100375 .debug_hint = hrtimer_debug_hint,
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700376 .fixup_init = hrtimer_fixup_init,
377 .fixup_activate = hrtimer_fixup_activate,
378 .fixup_free = hrtimer_fixup_free,
379};
380
381static inline void debug_hrtimer_init(struct hrtimer *timer)
382{
383 debug_object_init(timer, &hrtimer_debug_descr);
384}
385
386static inline void debug_hrtimer_activate(struct hrtimer *timer)
387{
388 debug_object_activate(timer, &hrtimer_debug_descr);
389}
390
391static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
392{
393 debug_object_deactivate(timer, &hrtimer_debug_descr);
394}
395
396static inline void debug_hrtimer_free(struct hrtimer *timer)
397{
398 debug_object_free(timer, &hrtimer_debug_descr);
399}
400
401static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
402 enum hrtimer_mode mode);
403
404void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
405 enum hrtimer_mode mode)
406{
407 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
408 __hrtimer_init(timer, clock_id, mode);
409}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -0700410EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700411
412void destroy_hrtimer_on_stack(struct hrtimer *timer)
413{
414 debug_object_free(timer, &hrtimer_debug_descr);
415}
416
417#else
418static inline void debug_hrtimer_init(struct hrtimer *timer) { }
419static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
420static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
421#endif
422
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800423static inline void
424debug_init(struct hrtimer *timer, clockid_t clockid,
425 enum hrtimer_mode mode)
426{
427 debug_hrtimer_init(timer);
428 trace_hrtimer_init(timer, clockid, mode);
429}
430
431static inline void debug_activate(struct hrtimer *timer)
432{
433 debug_hrtimer_activate(timer);
434 trace_hrtimer_start(timer);
435}
436
437static inline void debug_deactivate(struct hrtimer *timer)
438{
439 debug_hrtimer_deactivate(timer);
440 trace_hrtimer_cancel(timer);
441}
442
Thomas Gleixner9bc749192015-01-20 21:24:10 +0100443#if defined(CONFIG_NO_HZ_COMMON) || defined(CONFIG_HIGH_RES_TIMERS)
kbuild test robot4ebbda52015-01-23 20:12:06 +0800444static ktime_t __hrtimer_get_next_event(struct hrtimer_cpu_base *cpu_base)
Thomas Gleixner9bc749192015-01-20 21:24:10 +0100445{
446 struct hrtimer_clock_base *base = cpu_base->clock_base;
447 ktime_t expires, expires_next = { .tv64 = KTIME_MAX };
448 int i;
449
450 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
451 struct timerqueue_node *next;
452 struct hrtimer *timer;
453
454 next = timerqueue_getnext(&base->active);
455 if (!next)
456 continue;
457
458 timer = container_of(next, struct hrtimer, node);
459 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
460 if (expires.tv64 < expires_next.tv64)
461 expires_next = expires;
462 }
463 /*
464 * clock_was_set() might have changed base->offset of any of
465 * the clock bases so the result might be negative. Fix it up
466 * to prevent a false positive in clockevents_program_event().
467 */
468 if (expires_next.tv64 < 0)
469 expires_next.tv64 = 0;
470 return expires_next;
471}
472#endif
473
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800474/* High resolution timer related functions */
475#ifdef CONFIG_HIGH_RES_TIMERS
476
477/*
478 * High resolution timer enabled ?
479 */
480static int hrtimer_hres_enabled __read_mostly = 1;
481
482/*
483 * Enable / Disable high resolution mode
484 */
485static int __init setup_hrtimer_hres(char *str)
486{
487 if (!strcmp(str, "off"))
488 hrtimer_hres_enabled = 0;
489 else if (!strcmp(str, "on"))
490 hrtimer_hres_enabled = 1;
491 else
492 return 0;
493 return 1;
494}
495
496__setup("highres=", setup_hrtimer_hres);
497
498/*
499 * hrtimer_high_res_enabled - query, if the highres mode is enabled
500 */
501static inline int hrtimer_is_hres_enabled(void)
502{
503 return hrtimer_hres_enabled;
504}
505
506/*
507 * Is the high resolution mode active ?
508 */
509static inline int hrtimer_hres_active(void)
510{
Christoph Lameter909ea962010-12-08 16:22:55 +0100511 return __this_cpu_read(hrtimer_bases.hres_active);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800512}
513
514/*
515 * Reprogram the event source with checking both queues for the
516 * next event
517 * Called with interrupts disabled and base->lock held
518 */
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400519static void
520hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800521{
Thomas Gleixner9bc749192015-01-20 21:24:10 +0100522 ktime_t expires_next = __hrtimer_get_next_event(cpu_base);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800523
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400524 if (skip_equal && expires_next.tv64 == cpu_base->expires_next.tv64)
525 return;
526
527 cpu_base->expires_next.tv64 = expires_next.tv64;
528
Stuart Hayes6c6c0d52014-04-29 17:55:02 -0500529 /*
530 * If a hang was detected in the last timer interrupt then we
531 * leave the hang delay active in the hardware. We want the
532 * system to make progress. That also prevents the following
533 * scenario:
534 * T1 expires 50ms from now
535 * T2 expires 5s from now
536 *
537 * T1 is removed, so this code is called and would reprogram
538 * the hardware to 5s from now. Any hrtimer_start after that
539 * will not reprogram the hardware due to hang_detected being
540 * set. So we'd effectivly block all timers until the T2 event
541 * fires.
542 */
543 if (cpu_base->hang_detected)
544 return;
545
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800546 if (cpu_base->expires_next.tv64 != KTIME_MAX)
547 tick_program_event(cpu_base->expires_next, 1);
548}
549
550/*
551 * Shared reprogramming for clock_realtime and clock_monotonic
552 *
553 * When a timer is enqueued and expires earlier than the already enqueued
554 * timers, we have to check, whether it expires earlier than the timer for
555 * which the clock event device was armed.
556 *
Viresh Kumar9e1e01d2014-06-22 01:29:17 +0200557 * Note, that in case the state has HRTIMER_STATE_CALLBACK set, no reprogramming
558 * and no expiry check happens. The timer gets enqueued into the rbtree. The
559 * reprogramming and expiry check is done in the hrtimer_interrupt or in the
560 * softirq.
561 *
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800562 * Called with interrupts disabled and base->cpu_base.lock held
563 */
564static int hrtimer_reprogram(struct hrtimer *timer,
565 struct hrtimer_clock_base *base)
566{
Christoph Lameterdc5df732014-08-17 12:30:26 -0500567 struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700568 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800569 int res;
570
Arjan van de Vencc584b22008-09-01 15:02:30 -0700571 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
Thomas Gleixner63070a72008-02-14 00:58:36 +0100572
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800573 /*
574 * When the callback is running, we do not reprogram the clock event
575 * device. The timer callback is either running on a different CPU or
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200576 * the callback is executed in the hrtimer_interrupt context. The
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800577 * reprogramming is handled either by the softirq, which called the
578 * callback or at the end of the hrtimer_interrupt.
579 */
580 if (hrtimer_callback_running(timer))
581 return 0;
582
Thomas Gleixner63070a72008-02-14 00:58:36 +0100583 /*
584 * CLOCK_REALTIME timer might be requested with an absolute
585 * expiry time which is less than base->offset. Nothing wrong
586 * about that, just avoid to call into the tick code, which
587 * has now objections against negative expiry values.
588 */
589 if (expires.tv64 < 0)
590 return -ETIME;
591
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100592 if (expires.tv64 >= cpu_base->expires_next.tv64)
593 return 0;
594
595 /*
Thomas Gleixner9bc749192015-01-20 21:24:10 +0100596 * When the target cpu of the timer is currently executing
597 * hrtimer_interrupt(), then we do not touch the clock event
598 * device. hrtimer_interrupt() will reevaluate all clock bases
599 * before reprogramming the device.
600 */
601 if (cpu_base->in_hrtirq)
602 return 0;
603
604 /*
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100605 * If a hang was detected in the last timer interrupt then we
606 * do not schedule a timer which is earlier than the expiry
607 * which we enforced in the hang detection. We want the system
608 * to make progress.
609 */
610 if (cpu_base->hang_detected)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800611 return 0;
612
613 /*
614 * Clockevents returns -ETIME, when the event was in the past.
615 */
616 res = tick_program_event(expires, 0);
617 if (!IS_ERR_VALUE(res))
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100618 cpu_base->expires_next = expires;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800619 return res;
620}
621
Ingo Molnar995f0542007-04-07 12:05:00 +0200622/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800623 * Initialize the high resolution related parts of cpu_base
624 */
625static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
626{
627 base->expires_next.tv64 = KTIME_MAX;
628 base->hres_active = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800629}
630
John Stultz5baefd62012-07-10 18:43:25 -0400631static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
632{
633 ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
634 ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset;
John Stultz90adda92013-01-21 17:00:11 -0800635 ktime_t *offs_tai = &base->clock_base[HRTIMER_BASE_TAI].offset;
John Stultz5baefd62012-07-10 18:43:25 -0400636
John Stultz76f41082014-07-16 21:03:52 +0000637 return ktime_get_update_offsets_now(offs_real, offs_boot, offs_tai);
John Stultz5baefd62012-07-10 18:43:25 -0400638}
639
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200640/*
641 * Retrigger next event is called after clock was set
642 *
643 * Called with interrupts disabled via on_each_cpu()
644 */
645static void retrigger_next_event(void *arg)
646{
Christoph Lameterdc5df732014-08-17 12:30:26 -0500647 struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200648
649 if (!hrtimer_hres_active())
650 return;
651
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200652 raw_spin_lock(&base->lock);
John Stultz5baefd62012-07-10 18:43:25 -0400653 hrtimer_update_base(base);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200654 hrtimer_force_reprogram(base, 0);
655 raw_spin_unlock(&base->lock);
656}
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200657
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800658/*
659 * Switch to high resolution mode
660 */
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800661static int hrtimer_switch_to_hres(void)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800662{
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200663 int i, cpu = smp_processor_id();
Ingo Molnar820de5c2007-07-21 04:37:36 -0700664 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800665 unsigned long flags;
666
667 if (base->hres_active)
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800668 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800669
670 local_irq_save(flags);
671
672 if (tick_init_highres()) {
673 local_irq_restore(flags);
Ingo Molnar820de5c2007-07-21 04:37:36 -0700674 printk(KERN_WARNING "Could not switch to high resolution "
675 "mode on CPU %d\n", cpu);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800676 return 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800677 }
678 base->hres_active = 1;
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200679 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
680 base->clock_base[i].resolution = KTIME_HIGH_RES;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800681
682 tick_setup_sched_timer();
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800683 /* "Retrigger" the interrupt to get things going */
684 retrigger_next_event(NULL);
685 local_irq_restore(flags);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800686 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800687}
688
Thomas Gleixner5ec24812013-07-05 12:09:18 +0200689static void clock_was_set_work(struct work_struct *work)
690{
691 clock_was_set();
692}
693
694static DECLARE_WORK(hrtimer_work, clock_was_set_work);
695
John Stultzf55a6fa2012-07-10 18:43:19 -0400696/*
Thomas Gleixner5ec24812013-07-05 12:09:18 +0200697 * Called from timekeeping and resume code to reprogramm the hrtimer
698 * interrupt device on all cpus.
John Stultzf55a6fa2012-07-10 18:43:19 -0400699 */
700void clock_was_set_delayed(void)
701{
Thomas Gleixner5ec24812013-07-05 12:09:18 +0200702 schedule_work(&hrtimer_work);
John Stultzf55a6fa2012-07-10 18:43:19 -0400703}
704
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800705#else
706
707static inline int hrtimer_hres_active(void) { return 0; }
708static inline int hrtimer_is_hres_enabled(void) { return 0; }
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800709static inline int hrtimer_switch_to_hres(void) { return 0; }
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400710static inline void
711hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
Viresh Kumar9e1e01d2014-06-22 01:29:17 +0200712static inline int hrtimer_reprogram(struct hrtimer *timer,
713 struct hrtimer_clock_base *base)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800714{
715 return 0;
716}
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800717static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200718static inline void retrigger_next_event(void *arg) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800719
720#endif /* CONFIG_HIGH_RES_TIMERS */
721
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200722/*
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200723 * Clock realtime was set
724 *
725 * Change the offset of the realtime clock vs. the monotonic
726 * clock.
727 *
728 * We might have to reprogram the high resolution timer interrupt. On
729 * SMP we call the architecture specific code to retrigger _all_ high
730 * resolution timer interrupts. On UP we just disable interrupts and
731 * call the high resolution interrupt code.
732 */
733void clock_was_set(void)
734{
Thomas Gleixner90ff1f32011-05-25 23:08:17 +0200735#ifdef CONFIG_HIGH_RES_TIMERS
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200736 /* Retrigger the CPU local events everywhere */
737 on_each_cpu(retrigger_next_event, NULL, 1);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200738#endif
739 timerfd_clock_was_set();
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200740}
741
742/*
743 * During resume we might have to reprogram the high resolution timer
David Vrabel7c4c3a02013-06-27 11:35:44 +0100744 * interrupt on all online CPUs. However, all other CPUs will be
745 * stopped with IRQs interrupts disabled so the clock_was_set() call
Thomas Gleixner5ec24812013-07-05 12:09:18 +0200746 * must be deferred.
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200747 */
748void hrtimers_resume(void)
749{
750 WARN_ONCE(!irqs_disabled(),
751 KERN_INFO "hrtimers_resume() called with IRQs enabled!");
752
Thomas Gleixner5ec24812013-07-05 12:09:18 +0200753 /* Retrigger on the local CPU */
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200754 retrigger_next_event(NULL);
Thomas Gleixner5ec24812013-07-05 12:09:18 +0200755 /* And schedule a retrigger for all others */
756 clock_was_set_delayed();
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200757}
758
Heiko Carstens5f201902009-12-10 10:56:29 +0100759static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800760{
Heiko Carstens5f201902009-12-10 10:56:29 +0100761#ifdef CONFIG_TIMER_STATS
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800762 if (timer->start_site)
763 return;
Heiko Carstens5f201902009-12-10 10:56:29 +0100764 timer->start_site = __builtin_return_address(0);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800765 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
766 timer->start_pid = current->pid;
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800767#endif
Heiko Carstens5f201902009-12-10 10:56:29 +0100768}
769
770static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
771{
772#ifdef CONFIG_TIMER_STATS
773 timer->start_site = NULL;
774#endif
775}
776
777static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
778{
779#ifdef CONFIG_TIMER_STATS
780 if (likely(!timer_stats_active))
781 return;
782 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
783 timer->function, timer->start_comm, 0);
784#endif
785}
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800786
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800787/*
Uwe Kleine-König6506f2a2007-10-20 01:56:53 +0200788 * Counterpart to lock_hrtimer_base above:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800789 */
790static inline
791void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
792{
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100793 raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800794}
795
796/**
797 * hrtimer_forward - forward the timer expiry
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800798 * @timer: hrtimer to forward
Roman Zippel44f21472006-03-26 01:38:06 -0800799 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800800 * @interval: the interval to forward
801 *
802 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700803 * Returns the number of overruns.
Thomas Gleixner91e5a212015-04-13 21:02:22 +0000804 *
805 * Can be safely called from the callback function of @timer. If
806 * called from other contexts @timer must neither be enqueued nor
807 * running the callback and the caller needs to take care of
808 * serialization.
809 *
810 * Note: This only updates the timer expiry value and does not requeue
811 * the timer.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800812 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800813u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800814{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800815 u64 orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800816 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800817
Arjan van de Vencc584b22008-09-01 15:02:30 -0700818 delta = ktime_sub(now, hrtimer_get_expires(timer));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800819
820 if (delta.tv64 < 0)
821 return 0;
822
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100823 if (interval.tv64 < timer->base->resolution.tv64)
824 interval.tv64 = timer->base->resolution.tv64;
825
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800826 if (unlikely(delta.tv64 >= interval.tv64)) {
Roman Zippeldf869b62006-03-26 01:38:11 -0800827 s64 incr = ktime_to_ns(interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800828
829 orun = ktime_divns(delta, incr);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700830 hrtimer_add_expires_ns(timer, incr * orun);
831 if (hrtimer_get_expires_tv64(timer) > now.tv64)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800832 return orun;
833 /*
834 * This (and the ktime_add() below) is the
835 * correction for exact:
836 */
837 orun++;
838 }
Arjan van de Vencc584b22008-09-01 15:02:30 -0700839 hrtimer_add_expires(timer, interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800840
841 return orun;
842}
Stas Sergeev6bdb6b62007-05-08 00:31:58 -0700843EXPORT_SYMBOL_GPL(hrtimer_forward);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800844
845/*
846 * enqueue_hrtimer - internal function to (re)start a timer
847 *
848 * The timer is inserted in expiry order. Insertion into the
849 * red black tree is O(log(n)). Must hold the base lock.
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100850 *
851 * Returns 1 when the new timer is the leftmost timer in the tree.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800852 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100853static int enqueue_hrtimer(struct hrtimer *timer,
854 struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800855{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800856 debug_activate(timer);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700857
John Stultz998adc32010-09-20 19:19:17 -0700858 timerqueue_add(&base->active, &timer->node);
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200859 base->cpu_base->active_bases |= 1 << base->index;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800860
861 /*
Thomas Gleixner303e9672007-02-16 01:27:51 -0800862 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
863 * state of a possibly running callback.
864 */
865 timer->state |= HRTIMER_STATE_ENQUEUED;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100866
John Stultz998adc32010-09-20 19:19:17 -0700867 return (&timer->node == base->active.next);
Thomas Gleixner288867e2006-01-12 11:25:54 +0100868}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800869
870/*
871 * __remove_hrtimer - internal function to remove a timer
872 *
873 * Caller must hold the base lock.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800874 *
875 * High resolution timer mode reprograms the clock event device when the
876 * timer is the one which expires next. The caller can disable this by setting
877 * reprogram to zero. This is useful, when the context does a reprogramming
878 * anyway (e.g. timer interrupt)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800879 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800880static void __remove_hrtimer(struct hrtimer *timer,
Thomas Gleixner303e9672007-02-16 01:27:51 -0800881 struct hrtimer_clock_base *base,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800882 unsigned long newstate, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800883{
Jeff Ohlstein27c9cd72011-11-18 15:47:10 -0800884 struct timerqueue_node *next_timer;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400885 if (!(timer->state & HRTIMER_STATE_ENQUEUED))
886 goto out;
887
Jeff Ohlstein27c9cd72011-11-18 15:47:10 -0800888 next_timer = timerqueue_getnext(&base->active);
889 timerqueue_del(&base->active, &timer->node);
Viresh Kumard9f0acd2015-04-14 21:08:25 +0000890 if (!timerqueue_getnext(&base->active))
891 base->cpu_base->active_bases &= ~(1 << base->index);
892
Jeff Ohlstein27c9cd72011-11-18 15:47:10 -0800893 if (&timer->node == next_timer) {
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400894#ifdef CONFIG_HIGH_RES_TIMERS
895 /* Reprogram the clock event device. if enabled */
896 if (reprogram && hrtimer_hres_active()) {
897 ktime_t expires;
898
899 expires = ktime_sub(hrtimer_get_expires(timer),
900 base->offset);
901 if (base->cpu_base->expires_next.tv64 == expires.tv64)
902 hrtimer_force_reprogram(base->cpu_base, 1);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800903 }
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400904#endif
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800905 }
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400906out:
Thomas Gleixner303e9672007-02-16 01:27:51 -0800907 timer->state = newstate;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800908}
909
910/*
911 * remove hrtimer, called with base lock held
912 */
913static inline int
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800914remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800915{
Thomas Gleixner303e9672007-02-16 01:27:51 -0800916 if (hrtimer_is_queued(timer)) {
Salman Qazif13d4f92010-10-12 07:25:19 -0700917 unsigned long state;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800918 int reprogram;
919
920 /*
921 * Remove the timer and force reprogramming when high
922 * resolution mode is active and the timer is on the current
923 * CPU. If we remove a timer on another CPU, reprogramming is
924 * skipped. The interrupt event on this CPU is fired and
925 * reprogramming happens in the interrupt handler. This is a
926 * rare case and less expensive than a smp call.
927 */
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800928 debug_deactivate(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800929 timer_stats_hrtimer_clear_start_info(timer);
Christoph Lameterdc5df732014-08-17 12:30:26 -0500930 reprogram = base->cpu_base == this_cpu_ptr(&hrtimer_bases);
Salman Qazif13d4f92010-10-12 07:25:19 -0700931 /*
932 * We must preserve the CALLBACK state flag here,
933 * otherwise we could move the timer base in
934 * switch_hrtimer_base.
935 */
936 state = timer->state & HRTIMER_STATE_CALLBACK;
937 __remove_hrtimer(timer, base, state, reprogram);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800938 return 1;
939 }
940 return 0;
941}
942
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100943int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
944 unsigned long delta_ns, const enum hrtimer_mode mode,
945 int wakeup)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800946{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800947 struct hrtimer_clock_base *base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800948 unsigned long flags;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100949 int ret, leftmost;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800950
951 base = lock_hrtimer_base(timer, &flags);
952
953 /* Remove an active timer from the queue: */
954 ret = remove_hrtimer(timer, base);
955
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530956 if (mode & HRTIMER_MODE_REL) {
Viresh Kumar84ea7fe2014-05-12 13:42:29 +0530957 tim = ktime_add_safe(tim, base->get_time());
Ingo Molnar06027bd2006-02-14 13:53:15 -0800958 /*
959 * CONFIG_TIME_LOW_RES is a temporary way for architectures
960 * to signal that they simply return xtime in
961 * do_gettimeoffset(). In this case we want to round up by
962 * resolution when starting a relative timer, to avoid short
963 * timeouts. This will go away with the GTOD framework.
964 */
965#ifdef CONFIG_TIME_LOW_RES
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100966 tim = ktime_add_safe(tim, base->resolution);
Ingo Molnar06027bd2006-02-14 13:53:15 -0800967#endif
968 }
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700969
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700970 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800971
Viresh Kumar84ea7fe2014-05-12 13:42:29 +0530972 /* Switch the timer base, if necessary: */
973 new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
974
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800975 timer_stats_hrtimer_set_start_info(timer);
976
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100977 leftmost = enqueue_hrtimer(timer, new_base);
978
Viresh Kumar49a2a072014-06-23 13:39:37 +0530979 if (!leftmost) {
980 unlock_hrtimer_base(timer, &flags);
981 return ret;
982 }
983
984 if (!hrtimer_is_hres_active(timer)) {
985 /*
986 * Kick to reschedule the next tick to handle the new timer
987 * on dynticks target.
988 */
989 wake_up_nohz_cpu(new_base->cpu_base->cpu);
Christoph Lameterdc5df732014-08-17 12:30:26 -0500990 } else if (new_base->cpu_base == this_cpu_ptr(&hrtimer_bases) &&
Viresh Kumar9e1e01d2014-06-22 01:29:17 +0200991 hrtimer_reprogram(timer, new_base)) {
Viresh Kumar49a2a072014-06-23 13:39:37 +0530992 /*
993 * Only allow reprogramming if the new base is on this CPU.
994 * (it might still be on another CPU if the timer was pending)
995 *
996 * XXX send_remote_softirq() ?
997 */
Leonid Shatzb22affe2013-02-04 14:33:37 +0200998 if (wakeup) {
999 /*
1000 * We need to drop cpu_base->lock to avoid a
1001 * lock ordering issue vs. rq->lock.
1002 */
1003 raw_spin_unlock(&new_base->cpu_base->lock);
1004 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
1005 local_irq_restore(flags);
1006 return ret;
1007 } else {
1008 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
1009 }
1010 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001011
1012 unlock_hrtimer_base(timer, &flags);
1013
1014 return ret;
1015}
Yan, Zheng8588a2bb2014-03-18 16:56:42 +08001016EXPORT_SYMBOL_GPL(__hrtimer_start_range_ns);
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001017
1018/**
1019 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
1020 * @timer: the timer to be added
1021 * @tim: expiry time
1022 * @delta_ns: "slack" range for the timer
David Daney8ffbc7d2013-03-13 12:20:38 -07001023 * @mode: expiry mode: absolute (HRTIMER_MODE_ABS) or
1024 * relative (HRTIMER_MODE_REL)
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001025 *
1026 * Returns:
1027 * 0 on success
1028 * 1 when the timer was active
1029 */
1030int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1031 unsigned long delta_ns, const enum hrtimer_mode mode)
1032{
1033 return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
1034}
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001035EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
1036
1037/**
Thomas Gleixnere1dd7bc2008-10-20 13:33:36 +02001038 * hrtimer_start - (re)start an hrtimer on the current CPU
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001039 * @timer: the timer to be added
1040 * @tim: expiry time
David Daney8ffbc7d2013-03-13 12:20:38 -07001041 * @mode: expiry mode: absolute (HRTIMER_MODE_ABS) or
1042 * relative (HRTIMER_MODE_REL)
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001043 *
1044 * Returns:
1045 * 0 on success
1046 * 1 when the timer was active
1047 */
1048int
1049hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
1050{
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001051 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001052}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001053EXPORT_SYMBOL_GPL(hrtimer_start);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001054
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001055
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001056/**
1057 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001058 * @timer: hrtimer to stop
1059 *
1060 * Returns:
1061 * 0 when the timer was not active
1062 * 1 when the timer was active
1063 * -1 when the timer is currently excuting the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -07001064 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001065 */
1066int hrtimer_try_to_cancel(struct hrtimer *timer)
1067{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001068 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001069 unsigned long flags;
1070 int ret = -1;
1071
1072 base = lock_hrtimer_base(timer, &flags);
1073
Thomas Gleixner303e9672007-02-16 01:27:51 -08001074 if (!hrtimer_callback_running(timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001075 ret = remove_hrtimer(timer, base);
1076
1077 unlock_hrtimer_base(timer, &flags);
1078
1079 return ret;
1080
1081}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001082EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001083
1084/**
1085 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001086 * @timer: the timer to be cancelled
1087 *
1088 * Returns:
1089 * 0 when the timer was not active
1090 * 1 when the timer was active
1091 */
1092int hrtimer_cancel(struct hrtimer *timer)
1093{
1094 for (;;) {
1095 int ret = hrtimer_try_to_cancel(timer);
1096
1097 if (ret >= 0)
1098 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -07001099 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001100 }
1101}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001102EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001103
1104/**
1105 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001106 * @timer: the timer to read
1107 */
1108ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1109{
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001110 unsigned long flags;
1111 ktime_t rem;
1112
Andi Kleenb3bd3de2010-08-10 14:17:51 -07001113 lock_hrtimer_base(timer, &flags);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001114 rem = hrtimer_expires_remaining(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001115 unlock_hrtimer_base(timer, &flags);
1116
1117 return rem;
1118}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001119EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001120
Frederic Weisbecker3451d022011-08-10 23:21:01 +02001121#ifdef CONFIG_NO_HZ_COMMON
Tony Lindgren69239742006-03-06 15:42:45 -08001122/**
1123 * hrtimer_get_next_event - get the time until next expiry event
1124 *
1125 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1126 * is pending.
1127 */
1128ktime_t hrtimer_get_next_event(void)
1129{
Christoph Lameterdc5df732014-08-17 12:30:26 -05001130 struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
Thomas Gleixner9bc749192015-01-20 21:24:10 +01001131 ktime_t mindelta = { .tv64 = KTIME_MAX };
Tony Lindgren69239742006-03-06 15:42:45 -08001132 unsigned long flags;
Tony Lindgren69239742006-03-06 15:42:45 -08001133
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001134 raw_spin_lock_irqsave(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001135
Thomas Gleixner9bc749192015-01-20 21:24:10 +01001136 if (!hrtimer_hres_active())
1137 mindelta = ktime_sub(__hrtimer_get_next_event(cpu_base),
1138 ktime_get());
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001139
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001140 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001141
Tony Lindgren69239742006-03-06 15:42:45 -08001142 if (mindelta.tv64 < 0)
1143 mindelta.tv64 = 0;
1144 return mindelta;
1145}
1146#endif
1147
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001148static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1149 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001150{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001151 struct hrtimer_cpu_base *cpu_base;
John Stultze06383d2010-12-14 19:37:07 -08001152 int base;
George Anzinger7978672c2006-02-01 03:05:11 -08001153
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001154 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger7978672c2006-02-01 03:05:11 -08001155
Christoph Lameter22127e92014-08-17 12:30:25 -05001156 cpu_base = raw_cpu_ptr(&hrtimer_bases);
George Anzinger7978672c2006-02-01 03:05:11 -08001157
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001158 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger7978672c2006-02-01 03:05:11 -08001159 clock_id = CLOCK_MONOTONIC;
1160
John Stultze06383d2010-12-14 19:37:07 -08001161 base = hrtimer_clockid_to_base(clock_id);
1162 timer->base = &cpu_base->clock_base[base];
John Stultz998adc32010-09-20 19:19:17 -07001163 timerqueue_init(&timer->node);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001164
1165#ifdef CONFIG_TIMER_STATS
1166 timer->start_site = NULL;
1167 timer->start_pid = -1;
1168 memset(timer->start_comm, 0, TASK_COMM_LEN);
1169#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001170}
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001171
1172/**
1173 * hrtimer_init - initialize a timer to the given clock
1174 * @timer: the timer to be initialized
1175 * @clock_id: the clock to be used
1176 * @mode: timer mode abs/rel
1177 */
1178void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1179 enum hrtimer_mode mode)
1180{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001181 debug_init(timer, clock_id, mode);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001182 __hrtimer_init(timer, clock_id, mode);
1183}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001184EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001185
1186/**
1187 * hrtimer_get_res - get the timer resolution for a clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001188 * @which_clock: which clock to query
1189 * @tp: pointer to timespec variable to store the resolution
1190 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001191 * Store the resolution of the clock selected by @which_clock in the
1192 * variable pointed to by @tp.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001193 */
1194int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1195{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001196 struct hrtimer_cpu_base *cpu_base;
John Stultze06383d2010-12-14 19:37:07 -08001197 int base = hrtimer_clockid_to_base(which_clock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001198
Christoph Lameter22127e92014-08-17 12:30:25 -05001199 cpu_base = raw_cpu_ptr(&hrtimer_bases);
John Stultze06383d2010-12-14 19:37:07 -08001200 *tp = ktime_to_timespec(cpu_base->clock_base[base].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001201
1202 return 0;
1203}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001204EXPORT_SYMBOL_GPL(hrtimer_get_res);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001205
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001206static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001207{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001208 struct hrtimer_clock_base *base = timer->base;
1209 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1210 enum hrtimer_restart (*fn)(struct hrtimer *);
1211 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001212
Peter Zijlstraca109492008-11-25 12:43:51 +01001213 WARN_ON(!irqs_disabled());
1214
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001215 debug_deactivate(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001216 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1217 timer_stats_account_hrtimer(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001218 fn = timer->function;
Peter Zijlstraca109492008-11-25 12:43:51 +01001219
1220 /*
1221 * Because we run timers from hardirq context, there is no chance
1222 * they get migrated to another cpu, therefore its safe to unlock
1223 * the timer base.
1224 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001225 raw_spin_unlock(&cpu_base->lock);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001226 trace_hrtimer_expire_entry(timer, now);
Peter Zijlstraca109492008-11-25 12:43:51 +01001227 restart = fn(timer);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001228 trace_hrtimer_expire_exit(timer);
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001229 raw_spin_lock(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001230
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001231 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001232 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1233 * we do not reprogramm the event hardware. Happens either in
1234 * hrtimer_start_range_ns() or in hrtimer_interrupt()
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001235 */
1236 if (restart != HRTIMER_NORESTART) {
1237 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001238 enqueue_hrtimer(timer, base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001239 }
Salman Qazif13d4f92010-10-12 07:25:19 -07001240
1241 WARN_ON_ONCE(!(timer->state & HRTIMER_STATE_CALLBACK));
1242
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001243 timer->state &= ~HRTIMER_STATE_CALLBACK;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001244}
1245
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001246#ifdef CONFIG_HIGH_RES_TIMERS
1247
1248/*
1249 * High resolution timer interrupt
1250 * Called with interrupts disabled
1251 */
1252void hrtimer_interrupt(struct clock_event_device *dev)
1253{
Christoph Lameterdc5df732014-08-17 12:30:26 -05001254 struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001255 ktime_t expires_next, now, entry_time, delta;
1256 int i, retries = 0;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001257
1258 BUG_ON(!cpu_base->hres_active);
1259 cpu_base->nr_events++;
1260 dev->next_event.tv64 = KTIME_MAX;
1261
Thomas Gleixner196951e2012-07-10 18:43:23 -04001262 raw_spin_lock(&cpu_base->lock);
John Stultz5baefd62012-07-10 18:43:25 -04001263 entry_time = now = hrtimer_update_base(cpu_base);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001264retry:
Thomas Gleixner9bc749192015-01-20 21:24:10 +01001265 cpu_base->in_hrtirq = 1;
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001266 /*
1267 * We set expires_next to KTIME_MAX here with cpu_base->lock
1268 * held to prevent that a timer is enqueued in our queue via
1269 * the migration code. This does not affect enqueueing of
1270 * timers which run their callback and need to be requeued on
1271 * this CPU.
1272 */
1273 cpu_base->expires_next.tv64 = KTIME_MAX;
1274
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001275 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001276 struct hrtimer_clock_base *base;
John Stultz998adc32010-09-20 19:19:17 -07001277 struct timerqueue_node *node;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001278 ktime_t basenow;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001279
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001280 if (!(cpu_base->active_bases & (1 << i)))
1281 continue;
1282
1283 base = cpu_base->clock_base + i;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001284 basenow = ktime_add(now, base->offset);
1285
John Stultz998adc32010-09-20 19:19:17 -07001286 while ((node = timerqueue_getnext(&base->active))) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001287 struct hrtimer *timer;
1288
John Stultz998adc32010-09-20 19:19:17 -07001289 timer = container_of(node, struct hrtimer, node);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001290
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001291 /*
1292 * The immediate goal for using the softexpires is
1293 * minimizing wakeups, not running timers at the
1294 * earliest interrupt after their soft expiration.
1295 * This allows us to avoid using a Priority Search
1296 * Tree, which can answer a stabbing querry for
1297 * overlapping intervals and instead use the simple
1298 * BST we already have.
1299 * We don't add extra wakeups by delaying timers that
1300 * are right-of a not yet expired timer, because that
1301 * timer will have to trigger a wakeup anyway.
1302 */
Thomas Gleixner9bc749192015-01-20 21:24:10 +01001303 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001304 break;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001305
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001306 __run_hrtimer(timer, &basenow);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001307 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001308 }
Thomas Gleixner9bc749192015-01-20 21:24:10 +01001309 /* Reevaluate the clock bases for the next expiry */
1310 expires_next = __hrtimer_get_next_event(cpu_base);
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001311 /*
1312 * Store the new expiry value so the migration code can verify
1313 * against it.
1314 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001315 cpu_base->expires_next = expires_next;
Thomas Gleixner9bc749192015-01-20 21:24:10 +01001316 cpu_base->in_hrtirq = 0;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001317 raw_spin_unlock(&cpu_base->lock);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001318
1319 /* Reprogramming necessary ? */
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001320 if (expires_next.tv64 == KTIME_MAX ||
1321 !tick_program_event(expires_next, 0)) {
1322 cpu_base->hang_detected = 0;
1323 return;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001324 }
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001325
1326 /*
1327 * The next timer was already expired due to:
1328 * - tracing
1329 * - long lasting callbacks
1330 * - being scheduled away when running in a VM
1331 *
1332 * We need to prevent that we loop forever in the hrtimer
1333 * interrupt routine. We give it 3 attempts to avoid
1334 * overreacting on some spurious event.
John Stultz5baefd62012-07-10 18:43:25 -04001335 *
1336 * Acquire base lock for updating the offsets and retrieving
1337 * the current time.
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001338 */
Thomas Gleixner196951e2012-07-10 18:43:23 -04001339 raw_spin_lock(&cpu_base->lock);
John Stultz5baefd62012-07-10 18:43:25 -04001340 now = hrtimer_update_base(cpu_base);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001341 cpu_base->nr_retries++;
1342 if (++retries < 3)
1343 goto retry;
1344 /*
1345 * Give the system a chance to do something else than looping
1346 * here. We stored the entry time, so we know exactly how long
1347 * we spent here. We schedule the next event this amount of
1348 * time away.
1349 */
1350 cpu_base->nr_hangs++;
1351 cpu_base->hang_detected = 1;
Thomas Gleixner196951e2012-07-10 18:43:23 -04001352 raw_spin_unlock(&cpu_base->lock);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001353 delta = ktime_sub(now, entry_time);
1354 if (delta.tv64 > cpu_base->max_hang_time.tv64)
1355 cpu_base->max_hang_time = delta;
1356 /*
1357 * Limit it to a sensible value as we enforce a longer
1358 * delay. Give the CPU at least 100ms to catch up.
1359 */
1360 if (delta.tv64 > 100 * NSEC_PER_MSEC)
1361 expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
1362 else
1363 expires_next = ktime_add(now, delta);
1364 tick_program_event(expires_next, 1);
1365 printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
1366 ktime_to_ns(delta));
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001367}
1368
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001369/*
1370 * local version of hrtimer_peek_ahead_timers() called with interrupts
1371 * disabled.
1372 */
1373static void __hrtimer_peek_ahead_timers(void)
1374{
1375 struct tick_device *td;
1376
1377 if (!hrtimer_hres_active())
1378 return;
1379
Christoph Lameter22127e92014-08-17 12:30:25 -05001380 td = this_cpu_ptr(&tick_cpu_device);
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001381 if (td && td->evtdev)
1382 hrtimer_interrupt(td->evtdev);
1383}
1384
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001385/**
1386 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1387 *
1388 * hrtimer_peek_ahead_timers will peek at the timer queue of
1389 * the current cpu and check if there are any timers for which
1390 * the soft expires time has passed. If any such timers exist,
1391 * they are run immediately and then removed from the timer queue.
1392 *
1393 */
1394void hrtimer_peek_ahead_timers(void)
1395{
Thomas Gleixner643bdf62008-10-20 13:38:11 +02001396 unsigned long flags;
Arjan van de Vendc4304f2008-10-13 10:32:15 -04001397
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001398 local_irq_save(flags);
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001399 __hrtimer_peek_ahead_timers();
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001400 local_irq_restore(flags);
1401}
1402
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001403static void run_hrtimer_softirq(struct softirq_action *h)
1404{
1405 hrtimer_peek_ahead_timers();
1406}
1407
Ingo Molnar82c5b7b2009-01-05 14:11:10 +01001408#else /* CONFIG_HIGH_RES_TIMERS */
1409
1410static inline void __hrtimer_peek_ahead_timers(void) { }
1411
1412#endif /* !CONFIG_HIGH_RES_TIMERS */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001413
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001414/*
1415 * Called from timer softirq every jiffy, expire hrtimers:
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001416 *
1417 * For HRT its the fall back code to run the softirq in the timer
1418 * softirq context in case the hrtimer initialization failed or has
1419 * not been done yet.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001420 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001421void hrtimer_run_pending(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001422{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001423 if (hrtimer_hres_active())
1424 return;
1425
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001426 /*
1427 * This _is_ ugly: We have to check in the softirq context,
1428 * whether we can switch to highres and / or nohz mode. The
1429 * clocksource switch happens in the timer interrupt with
1430 * xtime_lock held. Notification from there only sets the
1431 * check bit in the tick_oneshot code, otherwise we might
1432 * deadlock vs. xtime_lock.
1433 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001434 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001435 hrtimer_switch_to_hres();
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001436}
1437
1438/*
1439 * Called from hardirq context every jiffy
1440 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001441void hrtimer_run_queues(void)
1442{
John Stultz998adc32010-09-20 19:19:17 -07001443 struct timerqueue_node *node;
Christoph Lameterdc5df732014-08-17 12:30:26 -05001444 struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001445 struct hrtimer_clock_base *base;
1446 int index, gettime = 1;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001447
1448 if (hrtimer_hres_active())
1449 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001450
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001451 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1452 base = &cpu_base->clock_base[index];
John Stultzb007c382010-12-10 22:19:53 -08001453 if (!timerqueue_getnext(&base->active))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001454 continue;
1455
Mark McLoughlind7cfb602008-09-19 13:13:44 +01001456 if (gettime) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001457 hrtimer_get_softirq_time(cpu_base);
1458 gettime = 0;
1459 }
1460
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001461 raw_spin_lock(&cpu_base->lock);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001462
John Stultzb007c382010-12-10 22:19:53 -08001463 while ((node = timerqueue_getnext(&base->active))) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001464 struct hrtimer *timer;
1465
John Stultz998adc32010-09-20 19:19:17 -07001466 timer = container_of(node, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001467 if (base->softirq_time.tv64 <=
1468 hrtimer_get_expires_tv64(timer))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001469 break;
1470
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001471 __run_hrtimer(timer, &base->softirq_time);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001472 }
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001473 raw_spin_unlock(&cpu_base->lock);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001474 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001475}
1476
1477/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001478 * Sleep related functions:
1479 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001480static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001481{
1482 struct hrtimer_sleeper *t =
1483 container_of(timer, struct hrtimer_sleeper, timer);
1484 struct task_struct *task = t->task;
1485
1486 t->task = NULL;
1487 if (task)
1488 wake_up_process(task);
1489
1490 return HRTIMER_NORESTART;
1491}
1492
Ingo Molnar36c8b582006-07-03 00:25:41 -07001493void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001494{
1495 sl->timer.function = hrtimer_wakeup;
1496 sl->task = task;
1497}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07001498EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
Thomas Gleixner00362e32006-03-31 02:31:17 -08001499
Thomas Gleixner669d7862006-03-31 02:31:19 -08001500static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001501{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001502 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001503
Roman Zippel432569b2006-03-26 01:38:08 -08001504 do {
1505 set_current_state(TASK_INTERRUPTIBLE);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001506 hrtimer_start_expires(&t->timer, mode);
Peter Zijlstra37bb6cb2008-01-25 21:08:32 +01001507 if (!hrtimer_active(&t->timer))
1508 t->task = NULL;
Roman Zippel432569b2006-03-26 01:38:08 -08001509
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001510 if (likely(t->task))
Colin Crossb0f8c442013-05-06 23:50:19 +00001511 freezable_schedule();
Roman Zippel432569b2006-03-26 01:38:08 -08001512
Thomas Gleixner669d7862006-03-31 02:31:19 -08001513 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001514 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -08001515
Thomas Gleixner669d7862006-03-31 02:31:19 -08001516 } while (t->task && !signal_pending(current));
1517
Peter Zijlstra3588a082008-02-01 17:45:13 +01001518 __set_current_state(TASK_RUNNING);
1519
Thomas Gleixner669d7862006-03-31 02:31:19 -08001520 return t->task == NULL;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001521}
1522
Oleg Nesterov080344b2008-02-01 17:29:05 +03001523static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1524{
1525 struct timespec rmt;
1526 ktime_t rem;
1527
Arjan van de Vencc584b22008-09-01 15:02:30 -07001528 rem = hrtimer_expires_remaining(timer);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001529 if (rem.tv64 <= 0)
1530 return 0;
1531 rmt = ktime_to_timespec(rem);
1532
1533 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1534 return -EFAULT;
1535
1536 return 1;
1537}
1538
Toyo Abe1711ef32006-09-29 02:00:28 -07001539long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001540{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001541 struct hrtimer_sleeper t;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001542 struct timespec __user *rmtp;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001543 int ret = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001544
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001545 hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001546 HRTIMER_MODE_ABS);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001547 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001548
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001549 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001550 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001551
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001552 rmtp = restart->nanosleep.rmtp;
Roman Zippel432569b2006-03-26 01:38:08 -08001553 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001554 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001555 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001556 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001557 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001558
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001559 /* The other values in restart are already filled in */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001560 ret = -ERESTART_RESTARTBLOCK;
1561out:
1562 destroy_hrtimer_on_stack(&t.timer);
1563 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001564}
1565
Oleg Nesterov080344b2008-02-01 17:29:05 +03001566long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001567 const enum hrtimer_mode mode, const clockid_t clockid)
1568{
1569 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -08001570 struct hrtimer_sleeper t;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001571 int ret = 0;
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001572 unsigned long slack;
1573
1574 slack = current->timer_slack_ns;
Dario Faggioliaab03e02013-11-28 11:14:43 +01001575 if (dl_task(current) || rt_task(current))
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001576 slack = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001577
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001578 hrtimer_init_on_stack(&t.timer, clockid, mode);
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001579 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
Roman Zippel432569b2006-03-26 01:38:08 -08001580 if (do_nanosleep(&t, mode))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001581 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001582
George Anzinger7978672c2006-02-01 03:05:11 -08001583 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001584 if (mode == HRTIMER_MODE_ABS) {
1585 ret = -ERESTARTNOHAND;
1586 goto out;
1587 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001588
Roman Zippel432569b2006-03-26 01:38:08 -08001589 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001590 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001591 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001592 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001593 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001594
Andy Lutomirskif56141e2015-02-12 15:01:14 -08001595 restart = &current->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -07001596 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001597 restart->nanosleep.clockid = t.timer.base->clockid;
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001598 restart->nanosleep.rmtp = rmtp;
Arjan van de Vencc584b22008-09-01 15:02:30 -07001599 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001600
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001601 ret = -ERESTART_RESTARTBLOCK;
1602out:
1603 destroy_hrtimer_on_stack(&t.timer);
1604 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001605}
1606
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001607SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1608 struct timespec __user *, rmtp)
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001609{
Oleg Nesterov080344b2008-02-01 17:29:05 +03001610 struct timespec tu;
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001611
1612 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1613 return -EFAULT;
1614
1615 if (!timespec_valid(&tu))
1616 return -EINVAL;
1617
Oleg Nesterov080344b2008-02-01 17:29:05 +03001618 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001619}
1620
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001621/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001622 * Functions related to boot-time initialization:
1623 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04001624static void init_hrtimers_cpu(int cpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001625{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001626 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001627 int i;
1628
John Stultz998adc32010-09-20 19:19:17 -07001629 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001630 cpu_base->clock_base[i].cpu_base = cpu_base;
John Stultz998adc32010-09-20 19:19:17 -07001631 timerqueue_init_head(&cpu_base->clock_base[i].active);
1632 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001633
Viresh Kumarcddd0242014-06-22 01:29:15 +02001634 cpu_base->cpu = cpu;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001635 hrtimer_init_hres(cpu_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001636}
1637
1638#ifdef CONFIG_HOTPLUG_CPU
1639
Peter Zijlstraca109492008-11-25 12:43:51 +01001640static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
Peter Zijlstra37810652008-12-04 11:17:10 +01001641 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001642{
1643 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001644 struct timerqueue_node *node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001645
John Stultz998adc32010-09-20 19:19:17 -07001646 while ((node = timerqueue_getnext(&old_base->active))) {
1647 timer = container_of(node, struct hrtimer, node);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001648 BUG_ON(hrtimer_callback_running(timer));
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001649 debug_deactivate(timer);
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001650
1651 /*
1652 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1653 * timer could be seen as !active and just vanish away
1654 * under us on another CPU
1655 */
1656 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001657 timer->base = new_base;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001658 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001659 * Enqueue the timers on the new cpu. This does not
1660 * reprogram the event device in case the timer
1661 * expires before the earliest on this CPU, but we run
1662 * hrtimer_interrupt after we migrated everything to
1663 * sort out already expired timers and reprogram the
1664 * event device.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001665 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001666 enqueue_hrtimer(timer, new_base);
Thomas Gleixner41e10222008-09-29 14:09:39 +02001667
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001668 /* Clear the migration state bit */
1669 timer->state &= ~HRTIMER_STATE_MIGRATE;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001670 }
1671}
1672
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001673static void migrate_hrtimers(int scpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001674{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001675 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001676 int i;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001677
Peter Zijlstra37810652008-12-04 11:17:10 +01001678 BUG_ON(cpu_online(scpu));
Peter Zijlstra37810652008-12-04 11:17:10 +01001679 tick_cancel_sched_timer(scpu);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001680
1681 local_irq_disable();
1682 old_base = &per_cpu(hrtimer_bases, scpu);
Christoph Lameterdc5df732014-08-17 12:30:26 -05001683 new_base = this_cpu_ptr(&hrtimer_bases);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001684 /*
1685 * The caller is globally serialized and nobody else
1686 * takes two locks at once, deadlock is not possible.
1687 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001688 raw_spin_lock(&new_base->lock);
1689 raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001690
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001691 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Peter Zijlstraca109492008-11-25 12:43:51 +01001692 migrate_hrtimer_list(&old_base->clock_base[i],
Peter Zijlstra37810652008-12-04 11:17:10 +01001693 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001694 }
1695
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001696 raw_spin_unlock(&old_base->lock);
1697 raw_spin_unlock(&new_base->lock);
Peter Zijlstra37810652008-12-04 11:17:10 +01001698
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001699 /* Check, if we got expired work to do */
1700 __hrtimer_peek_ahead_timers();
1701 local_irq_enable();
Peter Zijlstra37810652008-12-04 11:17:10 +01001702}
1703
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001704#endif /* CONFIG_HOTPLUG_CPU */
1705
Paul Gortmaker0db06282013-06-19 14:53:51 -04001706static int hrtimer_cpu_notify(struct notifier_block *self,
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001707 unsigned long action, void *hcpu)
1708{
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001709 int scpu = (long)hcpu;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001710
1711 switch (action) {
1712
1713 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001714 case CPU_UP_PREPARE_FROZEN:
Peter Zijlstra37810652008-12-04 11:17:10 +01001715 init_hrtimers_cpu(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001716 break;
1717
1718#ifdef CONFIG_HOTPLUG_CPU
1719 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001720 case CPU_DEAD_FROZEN:
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001721 migrate_hrtimers(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001722 break;
1723#endif
1724
1725 default:
1726 break;
1727 }
1728
1729 return NOTIFY_OK;
1730}
1731
Paul Gortmaker0db06282013-06-19 14:53:51 -04001732static struct notifier_block hrtimers_nb = {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001733 .notifier_call = hrtimer_cpu_notify,
1734};
1735
1736void __init hrtimers_init(void)
1737{
1738 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1739 (void *)(long)smp_processor_id());
1740 register_cpu_notifier(&hrtimers_nb);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001741#ifdef CONFIG_HIGH_RES_TIMERS
1742 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1743#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001744}
1745
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001746/**
Carsten Emde351b3f72010-04-02 22:40:19 +02001747 * schedule_hrtimeout_range_clock - sleep until timeout
1748 * @expires: timeout value (ktime_t)
1749 * @delta: slack in expires timeout (ktime_t)
1750 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1751 * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
1752 */
1753int __sched
1754schedule_hrtimeout_range_clock(ktime_t *expires, unsigned long delta,
1755 const enum hrtimer_mode mode, int clock)
1756{
1757 struct hrtimer_sleeper t;
1758
1759 /*
1760 * Optimize when a zero timeout value is given. It does not
1761 * matter whether this is an absolute or a relative time.
1762 */
1763 if (expires && !expires->tv64) {
1764 __set_current_state(TASK_RUNNING);
1765 return 0;
1766 }
1767
1768 /*
Namhyung Kim43b21012010-12-22 19:01:47 +01001769 * A NULL parameter means "infinite"
Carsten Emde351b3f72010-04-02 22:40:19 +02001770 */
1771 if (!expires) {
1772 schedule();
Carsten Emde351b3f72010-04-02 22:40:19 +02001773 return -EINTR;
1774 }
1775
1776 hrtimer_init_on_stack(&t.timer, clock, mode);
1777 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1778
1779 hrtimer_init_sleeper(&t, current);
1780
1781 hrtimer_start_expires(&t.timer, mode);
1782 if (!hrtimer_active(&t.timer))
1783 t.task = NULL;
1784
1785 if (likely(t.task))
1786 schedule();
1787
1788 hrtimer_cancel(&t.timer);
1789 destroy_hrtimer_on_stack(&t.timer);
1790
1791 __set_current_state(TASK_RUNNING);
1792
1793 return !t.task ? 0 : -EINTR;
1794}
1795
1796/**
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001797 * schedule_hrtimeout_range - sleep until timeout
1798 * @expires: timeout value (ktime_t)
1799 * @delta: slack in expires timeout (ktime_t)
1800 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1801 *
1802 * Make the current task sleep until the given expiry time has
1803 * elapsed. The routine will return immediately unless
1804 * the current task state has been set (see set_current_state()).
1805 *
1806 * The @delta argument gives the kernel the freedom to schedule the
1807 * actual wakeup to a time that is both power and performance friendly.
1808 * The kernel give the normal best effort behavior for "@expires+@delta",
1809 * but may decide to fire the timer earlier, but no earlier than @expires.
1810 *
1811 * You can set the task state as follows -
1812 *
1813 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1814 * pass before the routine returns.
1815 *
1816 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1817 * delivered to the current task.
1818 *
1819 * The current task state is guaranteed to be TASK_RUNNING when this
1820 * routine returns.
1821 *
1822 * Returns 0 when the timer has expired otherwise -EINTR
1823 */
1824int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
Carsten Emde351b3f72010-04-02 22:40:19 +02001825 const enum hrtimer_mode mode)
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001826{
Carsten Emde351b3f72010-04-02 22:40:19 +02001827 return schedule_hrtimeout_range_clock(expires, delta, mode,
1828 CLOCK_MONOTONIC);
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001829}
1830EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1831
1832/**
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001833 * schedule_hrtimeout - sleep until timeout
1834 * @expires: timeout value (ktime_t)
1835 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1836 *
1837 * Make the current task sleep until the given expiry time has
1838 * elapsed. The routine will return immediately unless
1839 * the current task state has been set (see set_current_state()).
1840 *
1841 * You can set the task state as follows -
1842 *
1843 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1844 * pass before the routine returns.
1845 *
1846 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1847 * delivered to the current task.
1848 *
1849 * The current task state is guaranteed to be TASK_RUNNING when this
1850 * routine returns.
1851 *
1852 * Returns 0 when the timer has expired otherwise -EINTR
1853 */
1854int __sched schedule_hrtimeout(ktime_t *expires,
1855 const enum hrtimer_mode mode)
1856{
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001857 return schedule_hrtimeout_range(expires, 0, mode);
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001858}
1859EXPORT_SYMBOL_GPL(schedule_hrtimeout);