blob: d00e85ac10d66b212660d89dd5879dd9debd5538 [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>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010046#include <linux/sched/signal.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>
Ingo Molnar370c9132017-02-08 18:51:35 +010050#include <linux/sched/nohz.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010051#include <linux/sched/debug.h>
Arun R Bharadwajeea08f32009-04-16 12:16:41 +053052#include <linux/timer.h>
Colin Crossb0f8c442013-05-06 23:50:19 +000053#include <linux/freezer.h>
Al Viroedbeda42017-06-07 09:42:31 +010054#include <linux/compat.h>
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080055
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080056#include <linux/uaccess.h>
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080057
Xiao Guangrongc6a2a172009-08-10 10:51:23 +080058#include <trace/events/timer.h>
59
Thomas Gleixnerc1797ba2015-03-25 13:07:37 +010060#include "tick-internal.h"
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000061
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080062/*
63 * The timer bases:
George Anzinger7978672c2006-02-01 03:05:11 -080064 *
Zhen Lei571af552015-08-25 14:42:53 +080065 * There are more clockids than hrtimer bases. Thus, we index
John Stultze06383d2010-12-14 19:37:07 -080066 * 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.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080069 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080070DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080071{
Michael Bohan84cc8fd2013-03-19 19:19:25 -070072 .lock = __RAW_SPIN_LOCK_UNLOCKED(hrtimer_bases.lock),
Peter Zijlstra887d9dc2015-06-11 14:46:48 +020073 .seq = SEQCNT_ZERO(hrtimer_bases.seq),
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080074 .clock_base =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080075 {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080076 {
Thomas Gleixnerab8177b2011-05-20 13:05:15 +020077 .index = HRTIMER_BASE_MONOTONIC,
78 .clockid = CLOCK_MONOTONIC,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080079 .get_time = &ktime_get,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080080 },
John Stultz70a08cc2011-02-15 10:45:16 -080081 {
Thomas Gleixner68fa61c2011-05-20 23:14:04 +020082 .index = HRTIMER_BASE_REALTIME,
83 .clockid = CLOCK_REALTIME,
84 .get_time = &ktime_get_real,
Thomas Gleixner68fa61c2011-05-20 23:14:04 +020085 },
86 {
Thomas Gleixnerab8177b2011-05-20 13:05:15 +020087 .index = HRTIMER_BASE_BOOTTIME,
88 .clockid = CLOCK_BOOTTIME,
John Stultz70a08cc2011-02-15 10:45:16 -080089 .get_time = &ktime_get_boottime,
John Stultz70a08cc2011-02-15 10:45:16 -080090 },
John Stultz90adda92013-01-21 17:00:11 -080091 {
92 .index = HRTIMER_BASE_TAI,
93 .clockid = CLOCK_TAI,
94 .get_time = &ktime_get_clocktai,
John Stultz90adda92013-01-21 17:00:11 -080095 },
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080096 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080097};
98
Mike Frysinger942c3c52011-05-02 15:24:27 -040099static const int hrtimer_clock_to_base_table[MAX_CLOCKS] = {
Marc Zyngier336a9cd2016-01-15 17:41:09 +0000100 /* Make sure we catch unsupported clockids */
101 [0 ... MAX_CLOCKS - 1] = HRTIMER_MAX_CLOCK_BASES,
102
Thomas Gleixnerce313322011-04-29 00:02:00 +0200103 [CLOCK_REALTIME] = HRTIMER_BASE_REALTIME,
104 [CLOCK_MONOTONIC] = HRTIMER_BASE_MONOTONIC,
105 [CLOCK_BOOTTIME] = HRTIMER_BASE_BOOTTIME,
John Stultz90adda92013-01-21 17:00:11 -0800106 [CLOCK_TAI] = HRTIMER_BASE_TAI,
Thomas Gleixnerce313322011-04-29 00:02:00 +0200107};
John Stultze06383d2010-12-14 19:37:07 -0800108
Thomas Gleixner92127c72006-03-26 01:38:05 -0800109/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800110 * Functions and macros which are different for UP/SMP systems are kept in a
111 * single place
112 */
113#ifdef CONFIG_SMP
114
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800115/*
Peter Zijlstra887d9dc2015-06-11 14:46:48 +0200116 * 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 */
120static 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
127/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800128 * 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
Peter Zijlstra887d9dc2015-06-11 14:46:48 +0200136 * possible to set timer->base = &migration_base and drop the lock: the timer
137 * remains locked.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800138 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800139static
140struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
141 unsigned long *flags)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800142{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800143 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800144
145 for (;;) {
146 base = timer->base;
Peter Zijlstra887d9dc2015-06-11 14:46:48 +0200147 if (likely(base != &migration_base)) {
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100148 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800149 if (likely(base == timer->base))
150 return base;
151 /* The timer has migrated to another CPU: */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100152 raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800153 }
154 cpu_relax();
155 }
156}
157
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200158/*
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 */
165static int
166hrtimer_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);
Thomas Gleixner2456e852016-12-25 11:38:40 +0100175 return expires <= new_base->cpu_base->expires_next;
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200176#else
177 return 0;
178#endif
179}
180
Pratyush Patel86721ab2016-03-01 22:58:49 +0530181#ifdef CONFIG_NO_HZ_COMMON
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000182static inline
183struct hrtimer_cpu_base *get_target_base(struct hrtimer_cpu_base *base,
184 int pinned)
185{
186 if (pinned || !base->migration_enabled)
Frederic Weisbecker662b3e12015-08-18 16:18:28 +0200187 return base;
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000188 return &per_cpu(hrtimer_bases, get_nohz_timer_target());
189}
190#else
191static inline
192struct hrtimer_cpu_base *get_target_base(struct hrtimer_cpu_base *base,
193 int pinned)
194{
Frederic Weisbecker662b3e12015-08-18 16:18:28 +0200195 return base;
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000196}
197#endif
198
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800199/*
Frederic Weisbeckerb48362d2015-08-18 16:18:29 +0200200 * We switch the timer base to a power-optimized selected CPU target,
201 * if:
202 * - NO_HZ_COMMON is enabled
203 * - timer migration is enabled
204 * - the timer callback is not running
205 * - the timer is not the first expiring timer on the new target
206 *
207 * If one of the above requirements is not fulfilled we move the timer
208 * to the current CPU or leave it on the previously assigned CPU if
209 * the timer callback is currently running.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800210 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800211static inline struct hrtimer_clock_base *
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530212switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
213 int pinned)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800214{
Frederic Weisbeckerb48362d2015-08-18 16:18:29 +0200215 struct hrtimer_cpu_base *new_cpu_base, *this_cpu_base;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800216 struct hrtimer_clock_base *new_base;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200217 int basenum = base->index;
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530218
Frederic Weisbeckerb48362d2015-08-18 16:18:29 +0200219 this_cpu_base = this_cpu_ptr(&hrtimer_bases);
220 new_cpu_base = get_target_base(this_cpu_base, pinned);
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530221again:
John Stultze06383d2010-12-14 19:37:07 -0800222 new_base = &new_cpu_base->clock_base[basenum];
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800223
224 if (base != new_base) {
225 /*
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200226 * We are trying to move timer to new_base.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800227 * However we can't change timer's base while it is running,
228 * so we keep it on the same CPU. No hassle vs. reprogramming
229 * the event source in the high resolution case. The softirq
230 * code will take care of this when the timer function has
231 * completed. There is no conflict as we hold the lock until
232 * the timer is enqueued.
233 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800234 if (unlikely(hrtimer_callback_running(timer)))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800235 return base;
236
Peter Zijlstra887d9dc2015-06-11 14:46:48 +0200237 /* See the comment in lock_hrtimer_base() */
238 timer->base = &migration_base;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100239 raw_spin_unlock(&base->cpu_base->lock);
240 raw_spin_lock(&new_base->cpu_base->lock);
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530241
Frederic Weisbeckerb48362d2015-08-18 16:18:29 +0200242 if (new_cpu_base != this_cpu_base &&
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000243 hrtimer_check_target(timer, new_base)) {
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100244 raw_spin_unlock(&new_base->cpu_base->lock);
245 raw_spin_lock(&base->cpu_base->lock);
Frederic Weisbeckerb48362d2015-08-18 16:18:29 +0200246 new_cpu_base = this_cpu_base;
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200247 timer->base = base;
248 goto again;
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530249 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800250 timer->base = new_base;
Leon Ma012a45e2014-04-30 16:43:10 +0800251 } else {
Frederic Weisbeckerb48362d2015-08-18 16:18:29 +0200252 if (new_cpu_base != this_cpu_base &&
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000253 hrtimer_check_target(timer, new_base)) {
Frederic Weisbeckerb48362d2015-08-18 16:18:29 +0200254 new_cpu_base = this_cpu_base;
Leon Ma012a45e2014-04-30 16:43:10 +0800255 goto again;
256 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800257 }
258 return new_base;
259}
260
261#else /* CONFIG_SMP */
262
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800263static inline struct hrtimer_clock_base *
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800264lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
265{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800266 struct hrtimer_clock_base *base = timer->base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800267
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100268 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800269
270 return base;
271}
272
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530273# define switch_hrtimer_base(t, b, p) (b)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800274
275#endif /* !CONFIG_SMP */
276
277/*
278 * Functions for the union type storage format of ktime_t which are
279 * too large for inlining:
280 */
281#if BITS_PER_LONG < 64
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800282/*
283 * Divide a ktime value by a nanosecond value
284 */
John Stultzf7bcb702015-05-08 13:47:23 -0700285s64 __ktime_divns(const ktime_t kt, s64 div)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800286{
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800287 int sft = 0;
John Stultzf7bcb702015-05-08 13:47:23 -0700288 s64 dclc;
289 u64 tmp;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800290
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300291 dclc = ktime_to_ns(kt);
John Stultzf7bcb702015-05-08 13:47:23 -0700292 tmp = dclc < 0 ? -dclc : dclc;
293
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800294 /* Make sure the divisor is less than 2^32: */
295 while (div >> 32) {
296 sft++;
297 div >>= 1;
298 }
John Stultzf7bcb702015-05-08 13:47:23 -0700299 tmp >>= sft;
300 do_div(tmp, (unsigned long) div);
301 return dclc < 0 ? -tmp : tmp;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800302}
Nicolas Pitre8b618622014-12-03 14:43:06 -0500303EXPORT_SYMBOL_GPL(__ktime_divns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800304#endif /* BITS_PER_LONG >= 64 */
305
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100306/*
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100307 * Add two ktime values and do a safety check for overflow:
308 */
309ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
310{
Vegard Nossum979515c2016-08-13 01:37:04 +0200311 ktime_t res = ktime_add_unsafe(lhs, rhs);
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100312
313 /*
314 * We use KTIME_SEC_MAX here, the maximum timeout which we can
315 * return to user space in a timespec:
316 */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100317 if (res < 0 || res < lhs || res < rhs)
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100318 res = ktime_set(KTIME_SEC_MAX, 0);
319
320 return res;
321}
322
Artem Bityutskiy8daa21e2009-05-28 16:21:24 +0300323EXPORT_SYMBOL_GPL(ktime_add_safe);
324
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700325#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
326
327static struct debug_obj_descr hrtimer_debug_descr;
328
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100329static void *hrtimer_debug_hint(void *addr)
330{
331 return ((struct hrtimer *) addr)->function;
332}
333
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700334/*
335 * fixup_init is called when:
336 * - an active object is initialized
337 */
Du, Changbine3252462016-05-19 17:09:29 -0700338static bool hrtimer_fixup_init(void *addr, enum debug_obj_state state)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700339{
340 struct hrtimer *timer = addr;
341
342 switch (state) {
343 case ODEBUG_STATE_ACTIVE:
344 hrtimer_cancel(timer);
345 debug_object_init(timer, &hrtimer_debug_descr);
Du, Changbine3252462016-05-19 17:09:29 -0700346 return true;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700347 default:
Du, Changbine3252462016-05-19 17:09:29 -0700348 return false;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700349 }
350}
351
352/*
353 * fixup_activate is called when:
354 * - an active object is activated
Du, Changbinb9fdac72016-05-19 17:09:41 -0700355 * - an unknown non-static object is activated
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700356 */
Du, Changbine3252462016-05-19 17:09:29 -0700357static bool hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700358{
359 switch (state) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700360 case ODEBUG_STATE_ACTIVE:
361 WARN_ON(1);
362
363 default:
Du, Changbine3252462016-05-19 17:09:29 -0700364 return false;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700365 }
366}
367
368/*
369 * fixup_free is called when:
370 * - an active object is freed
371 */
Du, Changbine3252462016-05-19 17:09:29 -0700372static bool hrtimer_fixup_free(void *addr, enum debug_obj_state state)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700373{
374 struct hrtimer *timer = addr;
375
376 switch (state) {
377 case ODEBUG_STATE_ACTIVE:
378 hrtimer_cancel(timer);
379 debug_object_free(timer, &hrtimer_debug_descr);
Du, Changbine3252462016-05-19 17:09:29 -0700380 return true;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700381 default:
Du, Changbine3252462016-05-19 17:09:29 -0700382 return false;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700383 }
384}
385
386static struct debug_obj_descr hrtimer_debug_descr = {
387 .name = "hrtimer",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100388 .debug_hint = hrtimer_debug_hint,
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700389 .fixup_init = hrtimer_fixup_init,
390 .fixup_activate = hrtimer_fixup_activate,
391 .fixup_free = hrtimer_fixup_free,
392};
393
394static inline void debug_hrtimer_init(struct hrtimer *timer)
395{
396 debug_object_init(timer, &hrtimer_debug_descr);
397}
398
399static inline void debug_hrtimer_activate(struct hrtimer *timer)
400{
401 debug_object_activate(timer, &hrtimer_debug_descr);
402}
403
404static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
405{
406 debug_object_deactivate(timer, &hrtimer_debug_descr);
407}
408
409static inline void debug_hrtimer_free(struct hrtimer *timer)
410{
411 debug_object_free(timer, &hrtimer_debug_descr);
412}
413
414static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
415 enum hrtimer_mode mode);
416
417void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
418 enum hrtimer_mode mode)
419{
420 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
421 __hrtimer_init(timer, clock_id, mode);
422}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -0700423EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700424
425void destroy_hrtimer_on_stack(struct hrtimer *timer)
426{
427 debug_object_free(timer, &hrtimer_debug_descr);
428}
Guenter Roeckc08376a2016-05-26 17:21:05 -0700429EXPORT_SYMBOL_GPL(destroy_hrtimer_on_stack);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700430
431#else
432static inline void debug_hrtimer_init(struct hrtimer *timer) { }
433static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
434static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
435#endif
436
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800437static inline void
438debug_init(struct hrtimer *timer, clockid_t clockid,
439 enum hrtimer_mode mode)
440{
441 debug_hrtimer_init(timer);
442 trace_hrtimer_init(timer, clockid, mode);
443}
444
445static inline void debug_activate(struct hrtimer *timer)
446{
447 debug_hrtimer_activate(timer);
448 trace_hrtimer_start(timer);
449}
450
451static inline void debug_deactivate(struct hrtimer *timer)
452{
453 debug_hrtimer_deactivate(timer);
454 trace_hrtimer_cancel(timer);
455}
456
Thomas Gleixner9bc749192015-01-20 21:24:10 +0100457#if defined(CONFIG_NO_HZ_COMMON) || defined(CONFIG_HIGH_RES_TIMERS)
Thomas Gleixner895bdfa2015-04-14 21:08:49 +0000458static inline void hrtimer_update_next_timer(struct hrtimer_cpu_base *cpu_base,
459 struct hrtimer *timer)
460{
461#ifdef CONFIG_HIGH_RES_TIMERS
462 cpu_base->next_timer = timer;
463#endif
464}
465
kbuild test robot4ebbda52015-01-23 20:12:06 +0800466static ktime_t __hrtimer_get_next_event(struct hrtimer_cpu_base *cpu_base)
Thomas Gleixner9bc749192015-01-20 21:24:10 +0100467{
468 struct hrtimer_clock_base *base = cpu_base->clock_base;
Thomas Gleixner34aee882015-04-14 21:08:41 +0000469 unsigned int active = cpu_base->active_bases;
Thomas Gleixner2456e852016-12-25 11:38:40 +0100470 ktime_t expires, expires_next = KTIME_MAX;
Thomas Gleixner9bc749192015-01-20 21:24:10 +0100471
Thomas Gleixner895bdfa2015-04-14 21:08:49 +0000472 hrtimer_update_next_timer(cpu_base, NULL);
Thomas Gleixner34aee882015-04-14 21:08:41 +0000473 for (; active; base++, active >>= 1) {
Thomas Gleixner9bc749192015-01-20 21:24:10 +0100474 struct timerqueue_node *next;
475 struct hrtimer *timer;
476
Thomas Gleixner34aee882015-04-14 21:08:41 +0000477 if (!(active & 0x01))
Thomas Gleixner9bc749192015-01-20 21:24:10 +0100478 continue;
479
Thomas Gleixner34aee882015-04-14 21:08:41 +0000480 next = timerqueue_getnext(&base->active);
Thomas Gleixner9bc749192015-01-20 21:24:10 +0100481 timer = container_of(next, struct hrtimer, node);
482 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixner2456e852016-12-25 11:38:40 +0100483 if (expires < expires_next) {
Thomas Gleixner9bc749192015-01-20 21:24:10 +0100484 expires_next = expires;
Thomas Gleixner895bdfa2015-04-14 21:08:49 +0000485 hrtimer_update_next_timer(cpu_base, timer);
486 }
Thomas Gleixner9bc749192015-01-20 21:24:10 +0100487 }
488 /*
489 * clock_was_set() might have changed base->offset of any of
490 * the clock bases so the result might be negative. Fix it up
491 * to prevent a false positive in clockevents_program_event().
492 */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100493 if (expires_next < 0)
494 expires_next = 0;
Thomas Gleixner9bc749192015-01-20 21:24:10 +0100495 return expires_next;
496}
497#endif
498
Thomas Gleixner21d6d522015-04-14 21:08:35 +0000499static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
500{
501 ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
502 ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset;
503 ktime_t *offs_tai = &base->clock_base[HRTIMER_BASE_TAI].offset;
504
Thomas Gleixner868a3e92015-04-14 21:08:37 +0000505 return ktime_get_update_offsets_now(&base->clock_was_set_seq,
506 offs_real, offs_boot, offs_tai);
Thomas Gleixner21d6d522015-04-14 21:08:35 +0000507}
508
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800509/* High resolution timer related functions */
510#ifdef CONFIG_HIGH_RES_TIMERS
511
512/*
513 * High resolution timer enabled ?
514 */
Kees Cook4cc7ecb72016-03-17 14:23:00 -0700515static bool hrtimer_hres_enabled __read_mostly = true;
Thomas Gleixner398ca172015-04-14 21:08:27 +0000516unsigned int hrtimer_resolution __read_mostly = LOW_RES_NSEC;
517EXPORT_SYMBOL_GPL(hrtimer_resolution);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800518
519/*
520 * Enable / Disable high resolution mode
521 */
522static int __init setup_hrtimer_hres(char *str)
523{
Kees Cook4cc7ecb72016-03-17 14:23:00 -0700524 return (kstrtobool(str, &hrtimer_hres_enabled) == 0);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800525}
526
527__setup("highres=", setup_hrtimer_hres);
528
529/*
530 * hrtimer_high_res_enabled - query, if the highres mode is enabled
531 */
532static inline int hrtimer_is_hres_enabled(void)
533{
534 return hrtimer_hres_enabled;
535}
536
537/*
538 * Is the high resolution mode active ?
539 */
Thomas Gleixnere19ffe82015-04-14 21:08:39 +0000540static inline int __hrtimer_hres_active(struct hrtimer_cpu_base *cpu_base)
541{
542 return cpu_base->hres_active;
543}
544
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800545static inline int hrtimer_hres_active(void)
546{
Thomas Gleixnere19ffe82015-04-14 21:08:39 +0000547 return __hrtimer_hres_active(this_cpu_ptr(&hrtimer_bases));
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800548}
549
550/*
551 * Reprogram the event source with checking both queues for the
552 * next event
553 * Called with interrupts disabled and base->lock held
554 */
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400555static void
556hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800557{
Thomas Gleixner21d6d522015-04-14 21:08:35 +0000558 ktime_t expires_next;
559
560 if (!cpu_base->hres_active)
561 return;
562
563 expires_next = __hrtimer_get_next_event(cpu_base);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800564
Thomas Gleixner2456e852016-12-25 11:38:40 +0100565 if (skip_equal && expires_next == cpu_base->expires_next)
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400566 return;
567
Thomas Gleixner2456e852016-12-25 11:38:40 +0100568 cpu_base->expires_next = expires_next;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400569
Stuart Hayes6c6c0d52014-04-29 17:55:02 -0500570 /*
571 * If a hang was detected in the last timer interrupt then we
572 * leave the hang delay active in the hardware. We want the
573 * system to make progress. That also prevents the following
574 * scenario:
575 * T1 expires 50ms from now
576 * T2 expires 5s from now
577 *
578 * T1 is removed, so this code is called and would reprogram
579 * the hardware to 5s from now. Any hrtimer_start after that
580 * will not reprogram the hardware due to hang_detected being
581 * set. So we'd effectivly block all timers until the T2 event
582 * fires.
583 */
584 if (cpu_base->hang_detected)
585 return;
586
Viresh Kumard25408752015-04-03 09:04:05 +0530587 tick_program_event(cpu_base->expires_next, 1);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800588}
589
590/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800591 * When a timer is enqueued and expires earlier than the already enqueued
592 * timers, we have to check, whether it expires earlier than the timer for
593 * which the clock event device was armed.
594 *
595 * Called with interrupts disabled and base->cpu_base.lock held
596 */
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +0000597static void hrtimer_reprogram(struct hrtimer *timer,
598 struct hrtimer_clock_base *base)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800599{
Christoph Lameterdc5df73b2014-08-17 12:30:26 -0500600 struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700601 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800602
Arjan van de Vencc584b22008-09-01 15:02:30 -0700603 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
Thomas Gleixner63070a72008-02-14 00:58:36 +0100604
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800605 /*
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +0000606 * If the timer is not on the current cpu, we cannot reprogram
607 * the other cpus clock event device.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800608 */
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +0000609 if (base->cpu_base != cpu_base)
610 return;
611
612 /*
613 * If the hrtimer interrupt is running, then it will
614 * reevaluate the clock bases and reprogram the clock event
615 * device. The callbacks are always executed in hard interrupt
616 * context so we don't need an extra check for a running
617 * callback.
618 */
619 if (cpu_base->in_hrtirq)
620 return;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800621
Thomas Gleixner63070a72008-02-14 00:58:36 +0100622 /*
623 * CLOCK_REALTIME timer might be requested with an absolute
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +0000624 * expiry time which is less than base->offset. Set it to 0.
Thomas Gleixner63070a72008-02-14 00:58:36 +0100625 */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100626 if (expires < 0)
627 expires = 0;
Thomas Gleixner63070a72008-02-14 00:58:36 +0100628
Thomas Gleixner2456e852016-12-25 11:38:40 +0100629 if (expires >= cpu_base->expires_next)
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +0000630 return;
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100631
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +0000632 /* Update the pointer to the next expiring timer */
Thomas Gleixner895bdfa2015-04-14 21:08:49 +0000633 cpu_base->next_timer = timer;
Thomas Gleixner9bc749192015-01-20 21:24:10 +0100634
635 /*
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100636 * If a hang was detected in the last timer interrupt then we
637 * do not schedule a timer which is earlier than the expiry
638 * which we enforced in the hang detection. We want the system
639 * to make progress.
640 */
641 if (cpu_base->hang_detected)
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +0000642 return;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800643
644 /*
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +0000645 * Program the timer hardware. We enforce the expiry for
646 * events which are already in the past.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800647 */
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +0000648 cpu_base->expires_next = expires;
649 tick_program_event(expires, 1);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800650}
651
Ingo Molnar995f0542007-04-07 12:05:00 +0200652/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800653 * Initialize the high resolution related parts of cpu_base
654 */
655static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
656{
Thomas Gleixner2456e852016-12-25 11:38:40 +0100657 base->expires_next = KTIME_MAX;
Thomas Gleixnerfdd88d72018-01-26 14:54:32 +0100658 base->hang_detected = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800659 base->hres_active = 0;
Thomas Gleixnerfdd88d72018-01-26 14:54:32 +0100660 base->next_timer = NULL;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800661}
662
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200663/*
664 * Retrigger next event is called after clock was set
665 *
666 * Called with interrupts disabled via on_each_cpu()
667 */
668static void retrigger_next_event(void *arg)
669{
Christoph Lameterdc5df73b2014-08-17 12:30:26 -0500670 struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200671
Thomas Gleixnere19ffe82015-04-14 21:08:39 +0000672 if (!base->hres_active)
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200673 return;
674
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200675 raw_spin_lock(&base->lock);
John Stultz5baefd62012-07-10 18:43:25 -0400676 hrtimer_update_base(base);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200677 hrtimer_force_reprogram(base, 0);
678 raw_spin_unlock(&base->lock);
679}
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200680
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800681/*
682 * Switch to high resolution mode
683 */
Luiz Capitulino75e3b372015-08-11 16:40:43 -0400684static void hrtimer_switch_to_hres(void)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800685{
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +0000686 struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800687
688 if (tick_init_highres()) {
Ingo Molnar820de5c2007-07-21 04:37:36 -0700689 printk(KERN_WARNING "Could not switch to high resolution "
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +0000690 "mode on CPU %d\n", base->cpu);
Guenter Roeck85e1cd62015-08-22 01:10:47 -0700691 return;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800692 }
693 base->hres_active = 1;
Thomas Gleixner398ca172015-04-14 21:08:27 +0000694 hrtimer_resolution = HIGH_RES_NSEC;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800695
696 tick_setup_sched_timer();
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800697 /* "Retrigger" the interrupt to get things going */
698 retrigger_next_event(NULL);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800699}
700
Thomas Gleixner5ec24812013-07-05 12:09:18 +0200701static void clock_was_set_work(struct work_struct *work)
702{
703 clock_was_set();
704}
705
706static DECLARE_WORK(hrtimer_work, clock_was_set_work);
707
John Stultzf55a6fa2012-07-10 18:43:19 -0400708/*
Pratyush Patelb4d90e92016-06-23 20:50:37 +0200709 * Called from timekeeping and resume code to reprogram the hrtimer
Thomas Gleixner5ec24812013-07-05 12:09:18 +0200710 * interrupt device on all cpus.
John Stultzf55a6fa2012-07-10 18:43:19 -0400711 */
712void clock_was_set_delayed(void)
713{
Thomas Gleixner5ec24812013-07-05 12:09:18 +0200714 schedule_work(&hrtimer_work);
John Stultzf55a6fa2012-07-10 18:43:19 -0400715}
716
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800717#else
718
Thomas Gleixnere19ffe82015-04-14 21:08:39 +0000719static inline int __hrtimer_hres_active(struct hrtimer_cpu_base *b) { return 0; }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800720static inline int hrtimer_hres_active(void) { return 0; }
721static inline int hrtimer_is_hres_enabled(void) { return 0; }
Luiz Capitulino75e3b372015-08-11 16:40:43 -0400722static inline void hrtimer_switch_to_hres(void) { }
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400723static inline void
724hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
Viresh Kumar9e1e01d2014-06-22 01:29:17 +0200725static inline int hrtimer_reprogram(struct hrtimer *timer,
726 struct hrtimer_clock_base *base)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800727{
728 return 0;
729}
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800730static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200731static inline void retrigger_next_event(void *arg) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800732
733#endif /* CONFIG_HIGH_RES_TIMERS */
734
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200735/*
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200736 * Clock realtime was set
737 *
738 * Change the offset of the realtime clock vs. the monotonic
739 * clock.
740 *
741 * We might have to reprogram the high resolution timer interrupt. On
742 * SMP we call the architecture specific code to retrigger _all_ high
743 * resolution timer interrupts. On UP we just disable interrupts and
744 * call the high resolution interrupt code.
745 */
746void clock_was_set(void)
747{
Thomas Gleixner90ff1f32011-05-25 23:08:17 +0200748#ifdef CONFIG_HIGH_RES_TIMERS
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200749 /* Retrigger the CPU local events everywhere */
750 on_each_cpu(retrigger_next_event, NULL, 1);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200751#endif
752 timerfd_clock_was_set();
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200753}
754
755/*
756 * During resume we might have to reprogram the high resolution timer
David Vrabel7c4c3a02013-06-27 11:35:44 +0100757 * interrupt on all online CPUs. However, all other CPUs will be
758 * stopped with IRQs interrupts disabled so the clock_was_set() call
Thomas Gleixner5ec24812013-07-05 12:09:18 +0200759 * must be deferred.
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200760 */
761void hrtimers_resume(void)
762{
763 WARN_ONCE(!irqs_disabled(),
764 KERN_INFO "hrtimers_resume() called with IRQs enabled!");
765
Thomas Gleixner5ec24812013-07-05 12:09:18 +0200766 /* Retrigger on the local CPU */
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200767 retrigger_next_event(NULL);
Thomas Gleixner5ec24812013-07-05 12:09:18 +0200768 /* And schedule a retrigger for all others */
769 clock_was_set_delayed();
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200770}
771
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800772/*
Uwe Kleine-König6506f2a2007-10-20 01:56:53 +0200773 * Counterpart to lock_hrtimer_base above:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800774 */
775static inline
776void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
777{
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100778 raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800779}
780
781/**
782 * hrtimer_forward - forward the timer expiry
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800783 * @timer: hrtimer to forward
Roman Zippel44f21472006-03-26 01:38:06 -0800784 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800785 * @interval: the interval to forward
786 *
787 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700788 * Returns the number of overruns.
Thomas Gleixner91e5a212015-04-13 21:02:22 +0000789 *
790 * Can be safely called from the callback function of @timer. If
791 * called from other contexts @timer must neither be enqueued nor
792 * running the callback and the caller needs to take care of
793 * serialization.
794 *
795 * Note: This only updates the timer expiry value and does not requeue
796 * the timer.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800797 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800798u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800799{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800800 u64 orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800801 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800802
Arjan van de Vencc584b22008-09-01 15:02:30 -0700803 delta = ktime_sub(now, hrtimer_get_expires(timer));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800804
Thomas Gleixner2456e852016-12-25 11:38:40 +0100805 if (delta < 0)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800806 return 0;
807
Peter Zijlstra5de27552014-05-20 15:49:48 +0200808 if (WARN_ON(timer->state & HRTIMER_STATE_ENQUEUED))
809 return 0;
810
Thomas Gleixner2456e852016-12-25 11:38:40 +0100811 if (interval < hrtimer_resolution)
812 interval = hrtimer_resolution;
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100813
Thomas Gleixner2456e852016-12-25 11:38:40 +0100814 if (unlikely(delta >= interval)) {
Roman Zippeldf869b62006-03-26 01:38:11 -0800815 s64 incr = ktime_to_ns(interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800816
817 orun = ktime_divns(delta, incr);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700818 hrtimer_add_expires_ns(timer, incr * orun);
Thomas Gleixner2456e852016-12-25 11:38:40 +0100819 if (hrtimer_get_expires_tv64(timer) > now)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800820 return orun;
821 /*
822 * This (and the ktime_add() below) is the
823 * correction for exact:
824 */
825 orun++;
826 }
Arjan van de Vencc584b22008-09-01 15:02:30 -0700827 hrtimer_add_expires(timer, interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800828
829 return orun;
830}
Stas Sergeev6bdb6b62007-05-08 00:31:58 -0700831EXPORT_SYMBOL_GPL(hrtimer_forward);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800832
833/*
834 * enqueue_hrtimer - internal function to (re)start a timer
835 *
836 * The timer is inserted in expiry order. Insertion into the
837 * red black tree is O(log(n)). Must hold the base lock.
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100838 *
839 * Returns 1 when the new timer is the leftmost timer in the tree.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800840 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100841static int enqueue_hrtimer(struct hrtimer *timer,
842 struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800843{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800844 debug_activate(timer);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700845
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200846 base->cpu_base->active_bases |= 1 << base->index;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800847
Peter Zijlstra887d9dc2015-06-11 14:46:48 +0200848 timer->state = HRTIMER_STATE_ENQUEUED;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100849
Thomas Gleixnerb97f44c2015-04-14 21:08:47 +0000850 return timerqueue_add(&base->active, &timer->node);
Thomas Gleixner288867e2006-01-12 11:25:54 +0100851}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800852
853/*
854 * __remove_hrtimer - internal function to remove a timer
855 *
856 * Caller must hold the base lock.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800857 *
858 * High resolution timer mode reprograms the clock event device when the
859 * timer is the one which expires next. The caller can disable this by setting
860 * reprogram to zero. This is useful, when the context does a reprogramming
861 * anyway (e.g. timer interrupt)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800862 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800863static void __remove_hrtimer(struct hrtimer *timer,
Thomas Gleixner303e9672007-02-16 01:27:51 -0800864 struct hrtimer_clock_base *base,
Thomas Gleixner203cbf72016-01-14 16:54:46 +0000865 u8 newstate, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800866{
Thomas Gleixnere19ffe82015-04-14 21:08:39 +0000867 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
Thomas Gleixner203cbf72016-01-14 16:54:46 +0000868 u8 state = timer->state;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400869
Thomas Gleixner303e9672007-02-16 01:27:51 -0800870 timer->state = newstate;
Thomas Gleixner895bdfa2015-04-14 21:08:49 +0000871 if (!(state & HRTIMER_STATE_ENQUEUED))
872 return;
David Woodhouseed198cb2006-04-22 02:38:50 +0100873
Thomas Gleixnerb97f44c2015-04-14 21:08:47 +0000874 if (!timerqueue_del(&base->active, &timer->node))
Thomas Gleixnere19ffe82015-04-14 21:08:39 +0000875 cpu_base->active_bases &= ~(1 << base->index);
Viresh Kumard9f0acd2015-04-14 21:08:25 +0000876
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800877#ifdef CONFIG_HIGH_RES_TIMERS
Thomas Gleixner895bdfa2015-04-14 21:08:49 +0000878 /*
879 * Note: If reprogram is false we do not update
880 * cpu_base->next_timer. This happens when we remove the first
881 * timer on a remote cpu. No harm as we never dereference
882 * cpu_base->next_timer. So the worst thing what can happen is
883 * an superflous call to hrtimer_force_reprogram() on the
884 * remote cpu later on if the same timer gets enqueued again.
885 */
886 if (reprogram && timer == cpu_base->next_timer)
887 hrtimer_force_reprogram(cpu_base, 1);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800888#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800889}
890
891/*
892 * remove hrtimer, called with base lock held
893 */
894static inline int
Peter Zijlstra8edfb032015-06-11 14:46:45 +0200895remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base, bool restart)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800896{
Thomas Gleixner303e9672007-02-16 01:27:51 -0800897 if (hrtimer_is_queued(timer)) {
Thomas Gleixner203cbf72016-01-14 16:54:46 +0000898 u8 state = timer->state;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800899 int reprogram;
900
901 /*
902 * Remove the timer and force reprogramming when high
903 * resolution mode is active and the timer is on the current
904 * CPU. If we remove a timer on another CPU, reprogramming is
905 * skipped. The interrupt event on this CPU is fired and
906 * reprogramming happens in the interrupt handler. This is a
907 * rare case and less expensive than a smp call.
908 */
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800909 debug_deactivate(timer);
Christoph Lameterdc5df73b2014-08-17 12:30:26 -0500910 reprogram = base->cpu_base == this_cpu_ptr(&hrtimer_bases);
Peter Zijlstra8edfb032015-06-11 14:46:45 +0200911
Peter Zijlstra887d9dc2015-06-11 14:46:48 +0200912 if (!restart)
913 state = HRTIMER_STATE_INACTIVE;
914
Salman Qazif13d4f92010-10-12 07:25:19 -0700915 __remove_hrtimer(timer, base, state, reprogram);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800916 return 1;
917 }
918 return 0;
919}
920
Thomas Gleixner203cbf72016-01-14 16:54:46 +0000921static inline ktime_t hrtimer_update_lowres(struct hrtimer *timer, ktime_t tim,
922 const enum hrtimer_mode mode)
923{
924#ifdef CONFIG_TIME_LOW_RES
925 /*
926 * CONFIG_TIME_LOW_RES indicates that the system has no way to return
927 * granular time values. For relative timers we add hrtimer_resolution
928 * (i.e. one jiffie) to prevent short timeouts.
929 */
930 timer->is_rel = mode & HRTIMER_MODE_REL;
931 if (timer->is_rel)
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100932 tim = ktime_add_safe(tim, hrtimer_resolution);
Thomas Gleixner203cbf72016-01-14 16:54:46 +0000933#endif
934 return tim;
935}
936
Thomas Gleixner58f1f802015-04-14 21:09:08 +0000937/**
938 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
939 * @timer: the timer to be added
940 * @tim: expiry time
941 * @delta_ns: "slack" range for the timer
942 * @mode: expiry mode: absolute (HRTIMER_MODE_ABS) or
943 * relative (HRTIMER_MODE_REL)
Thomas Gleixner58f1f802015-04-14 21:09:08 +0000944 */
Thomas Gleixner61699e12015-04-14 21:09:23 +0000945void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
John Stultzda8b44d2016-03-17 14:20:51 -0700946 u64 delta_ns, const enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800947{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800948 struct hrtimer_clock_base *base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800949 unsigned long flags;
Thomas Gleixner61699e12015-04-14 21:09:23 +0000950 int leftmost;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800951
952 base = lock_hrtimer_base(timer, &flags);
953
954 /* Remove an active timer from the queue: */
Peter Zijlstra8edfb032015-06-11 14:46:45 +0200955 remove_hrtimer(timer, base, true);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800956
Thomas Gleixner203cbf72016-01-14 16:54:46 +0000957 if (mode & HRTIMER_MODE_REL)
Viresh Kumar84ea7fe2014-05-12 13:42:29 +0530958 tim = ktime_add_safe(tim, base->get_time());
Thomas Gleixner203cbf72016-01-14 16:54:46 +0000959
960 tim = hrtimer_update_lowres(timer, tim, mode);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700961
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700962 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800963
Viresh Kumar84ea7fe2014-05-12 13:42:29 +0530964 /* Switch the timer base, if necessary: */
965 new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
966
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100967 leftmost = enqueue_hrtimer(timer, new_base);
Thomas Gleixner61699e12015-04-14 21:09:23 +0000968 if (!leftmost)
969 goto unlock;
Viresh Kumar49a2a072014-06-23 13:39:37 +0530970
971 if (!hrtimer_is_hres_active(timer)) {
972 /*
973 * Kick to reschedule the next tick to handle the new timer
974 * on dynticks target.
975 */
Thomas Gleixner683be132015-05-26 22:50:35 +0000976 if (new_base->cpu_base->nohz_active)
977 wake_up_nohz_cpu(new_base->cpu_base->cpu);
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +0000978 } else {
979 hrtimer_reprogram(timer, new_base);
Leonid Shatzb22affe2013-02-04 14:33:37 +0200980 }
Thomas Gleixner61699e12015-04-14 21:09:23 +0000981unlock:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800982 unlock_hrtimer_base(timer, &flags);
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100983}
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700984EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
985
986/**
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800987 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800988 * @timer: hrtimer to stop
989 *
990 * Returns:
991 * 0 when the timer was not active
992 * 1 when the timer was active
Masanari Iida0ba42a52017-03-07 20:48:02 +0900993 * -1 when the timer is currently executing the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -0700994 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800995 */
996int hrtimer_try_to_cancel(struct hrtimer *timer)
997{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800998 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800999 unsigned long flags;
1000 int ret = -1;
1001
Thomas Gleixner19d9f422015-04-14 21:09:25 +00001002 /*
1003 * Check lockless first. If the timer is not active (neither
1004 * enqueued nor running the callback, nothing to do here. The
1005 * base lock does not serialize against a concurrent enqueue,
1006 * so we can avoid taking it.
1007 */
1008 if (!hrtimer_active(timer))
1009 return 0;
1010
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001011 base = lock_hrtimer_base(timer, &flags);
1012
Thomas Gleixner303e9672007-02-16 01:27:51 -08001013 if (!hrtimer_callback_running(timer))
Peter Zijlstra8edfb032015-06-11 14:46:45 +02001014 ret = remove_hrtimer(timer, base, false);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001015
1016 unlock_hrtimer_base(timer, &flags);
1017
1018 return ret;
1019
1020}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001021EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001022
1023/**
1024 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001025 * @timer: the timer to be cancelled
1026 *
1027 * Returns:
1028 * 0 when the timer was not active
1029 * 1 when the timer was active
1030 */
1031int hrtimer_cancel(struct hrtimer *timer)
1032{
1033 for (;;) {
1034 int ret = hrtimer_try_to_cancel(timer);
1035
1036 if (ret >= 0)
1037 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -07001038 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001039 }
1040}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001041EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001042
1043/**
1044 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001045 * @timer: the timer to read
Thomas Gleixner203cbf72016-01-14 16:54:46 +00001046 * @adjust: adjust relative timers when CONFIG_TIME_LOW_RES=y
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001047 */
Thomas Gleixner203cbf72016-01-14 16:54:46 +00001048ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001049{
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001050 unsigned long flags;
1051 ktime_t rem;
1052
Andi Kleenb3bd3de2010-08-10 14:17:51 -07001053 lock_hrtimer_base(timer, &flags);
Thomas Gleixner203cbf72016-01-14 16:54:46 +00001054 if (IS_ENABLED(CONFIG_TIME_LOW_RES) && adjust)
1055 rem = hrtimer_expires_remaining_adjusted(timer);
1056 else
1057 rem = hrtimer_expires_remaining(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001058 unlock_hrtimer_base(timer, &flags);
1059
1060 return rem;
1061}
Thomas Gleixner203cbf72016-01-14 16:54:46 +00001062EXPORT_SYMBOL_GPL(__hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001063
Frederic Weisbecker3451d022011-08-10 23:21:01 +02001064#ifdef CONFIG_NO_HZ_COMMON
Tony Lindgren69239742006-03-06 15:42:45 -08001065/**
1066 * hrtimer_get_next_event - get the time until next expiry event
1067 *
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +00001068 * Returns the next expiry time or KTIME_MAX if no timer is pending.
Tony Lindgren69239742006-03-06 15:42:45 -08001069 */
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +00001070u64 hrtimer_get_next_event(void)
Tony Lindgren69239742006-03-06 15:42:45 -08001071{
Christoph Lameterdc5df73b2014-08-17 12:30:26 -05001072 struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +00001073 u64 expires = KTIME_MAX;
Tony Lindgren69239742006-03-06 15:42:45 -08001074 unsigned long flags;
Tony Lindgren69239742006-03-06 15:42:45 -08001075
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001076 raw_spin_lock_irqsave(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001077
Thomas Gleixnere19ffe82015-04-14 21:08:39 +00001078 if (!__hrtimer_hres_active(cpu_base))
Thomas Gleixner2456e852016-12-25 11:38:40 +01001079 expires = __hrtimer_get_next_event(cpu_base);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001080
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001081 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001082
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +00001083 return expires;
Tony Lindgren69239742006-03-06 15:42:45 -08001084}
1085#endif
1086
Marc Zyngier336a9cd2016-01-15 17:41:09 +00001087static inline int hrtimer_clockid_to_base(clockid_t clock_id)
1088{
1089 if (likely(clock_id < MAX_CLOCKS)) {
1090 int base = hrtimer_clock_to_base_table[clock_id];
1091
1092 if (likely(base != HRTIMER_MAX_CLOCK_BASES))
1093 return base;
1094 }
1095 WARN(1, "Invalid clockid %d. Using MONOTONIC\n", clock_id);
1096 return HRTIMER_BASE_MONOTONIC;
1097}
1098
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001099static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1100 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001101{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001102 struct hrtimer_cpu_base *cpu_base;
John Stultze06383d2010-12-14 19:37:07 -08001103 int base;
George Anzinger7978672c2006-02-01 03:05:11 -08001104
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001105 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger7978672c2006-02-01 03:05:11 -08001106
Christoph Lameter22127e92014-08-17 12:30:25 -05001107 cpu_base = raw_cpu_ptr(&hrtimer_bases);
George Anzinger7978672c2006-02-01 03:05:11 -08001108
Anna-Maria Gleixnerf92679f2017-12-21 11:41:35 +01001109 /*
1110 * POSIX magic: Relative CLOCK_REALTIME timers are not affected by
1111 * clock modifications, so they needs to become CLOCK_MONOTONIC to
1112 * ensure POSIX compliance.
1113 */
1114 if (clock_id == CLOCK_REALTIME && mode & HRTIMER_MODE_REL)
George Anzinger7978672c2006-02-01 03:05:11 -08001115 clock_id = CLOCK_MONOTONIC;
1116
John Stultze06383d2010-12-14 19:37:07 -08001117 base = hrtimer_clockid_to_base(clock_id);
1118 timer->base = &cpu_base->clock_base[base];
John Stultz998adc32010-09-20 19:19:17 -07001119 timerqueue_init(&timer->node);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001120}
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001121
1122/**
1123 * hrtimer_init - initialize a timer to the given clock
1124 * @timer: the timer to be initialized
1125 * @clock_id: the clock to be used
1126 * @mode: timer mode abs/rel
1127 */
1128void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1129 enum hrtimer_mode mode)
1130{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001131 debug_init(timer, clock_id, mode);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001132 __hrtimer_init(timer, clock_id, mode);
1133}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001134EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001135
Peter Zijlstra887d9dc2015-06-11 14:46:48 +02001136/*
1137 * A timer is active, when it is enqueued into the rbtree or the
1138 * callback function is running or it's in the state of being migrated
1139 * to another cpu.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001140 *
Peter Zijlstra887d9dc2015-06-11 14:46:48 +02001141 * It is important for this function to not return a false negative.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001142 */
Peter Zijlstra887d9dc2015-06-11 14:46:48 +02001143bool hrtimer_active(const struct hrtimer *timer)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001144{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001145 struct hrtimer_cpu_base *cpu_base;
Peter Zijlstra887d9dc2015-06-11 14:46:48 +02001146 unsigned int seq;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001147
Peter Zijlstra887d9dc2015-06-11 14:46:48 +02001148 do {
1149 cpu_base = READ_ONCE(timer->base->cpu_base);
1150 seq = raw_read_seqcount_begin(&cpu_base->seq);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001151
Peter Zijlstra887d9dc2015-06-11 14:46:48 +02001152 if (timer->state != HRTIMER_STATE_INACTIVE ||
1153 cpu_base->running == timer)
1154 return true;
1155
1156 } while (read_seqcount_retry(&cpu_base->seq, seq) ||
1157 cpu_base != READ_ONCE(timer->base->cpu_base));
1158
1159 return false;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001160}
Peter Zijlstra887d9dc2015-06-11 14:46:48 +02001161EXPORT_SYMBOL_GPL(hrtimer_active);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001162
Peter Zijlstra887d9dc2015-06-11 14:46:48 +02001163/*
1164 * The write_seqcount_barrier()s in __run_hrtimer() split the thing into 3
1165 * distinct sections:
1166 *
1167 * - queued: the timer is queued
1168 * - callback: the timer is being ran
1169 * - post: the timer is inactive or (re)queued
1170 *
1171 * On the read side we ensure we observe timer->state and cpu_base->running
1172 * from the same section, if anything changed while we looked at it, we retry.
1173 * This includes timer->base changing because sequence numbers alone are
1174 * insufficient for that.
1175 *
1176 * The sequence numbers are required because otherwise we could still observe
1177 * a false negative if the read side got smeared over multiple consequtive
1178 * __run_hrtimer() invocations.
1179 */
1180
Thomas Gleixner21d6d522015-04-14 21:08:35 +00001181static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base,
1182 struct hrtimer_clock_base *base,
1183 struct hrtimer *timer, ktime_t *now)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001184{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001185 enum hrtimer_restart (*fn)(struct hrtimer *);
1186 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001187
Peter Zijlstra887d9dc2015-06-11 14:46:48 +02001188 lockdep_assert_held(&cpu_base->lock);
Peter Zijlstraca109492008-11-25 12:43:51 +01001189
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001190 debug_deactivate(timer);
Peter Zijlstra887d9dc2015-06-11 14:46:48 +02001191 cpu_base->running = timer;
1192
1193 /*
1194 * Separate the ->running assignment from the ->state assignment.
1195 *
1196 * As with a regular write barrier, this ensures the read side in
1197 * hrtimer_active() cannot observe cpu_base->running == NULL &&
1198 * timer->state == INACTIVE.
1199 */
1200 raw_write_seqcount_barrier(&cpu_base->seq);
1201
1202 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 0);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001203 fn = timer->function;
Peter Zijlstraca109492008-11-25 12:43:51 +01001204
1205 /*
Thomas Gleixner203cbf72016-01-14 16:54:46 +00001206 * Clear the 'is relative' flag for the TIME_LOW_RES case. If the
1207 * timer is restarted with a period then it becomes an absolute
1208 * timer. If its not restarted it does not matter.
1209 */
1210 if (IS_ENABLED(CONFIG_TIME_LOW_RES))
1211 timer->is_rel = false;
1212
1213 /*
Peter Zijlstraca109492008-11-25 12:43:51 +01001214 * Because we run timers from hardirq context, there is no chance
1215 * they get migrated to another cpu, therefore its safe to unlock
1216 * the timer base.
1217 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001218 raw_spin_unlock(&cpu_base->lock);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001219 trace_hrtimer_expire_entry(timer, now);
Peter Zijlstraca109492008-11-25 12:43:51 +01001220 restart = fn(timer);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001221 trace_hrtimer_expire_exit(timer);
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001222 raw_spin_lock(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001223
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001224 /*
Peter Zijlstra887d9dc2015-06-11 14:46:48 +02001225 * Note: We clear the running state after enqueue_hrtimer and
Pratyush Patelb4d90e92016-06-23 20:50:37 +02001226 * we do not reprogram the event hardware. Happens either in
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001227 * hrtimer_start_range_ns() or in hrtimer_interrupt()
Peter Zijlstra5de27552014-05-20 15:49:48 +02001228 *
1229 * Note: Because we dropped the cpu_base->lock above,
1230 * hrtimer_start_range_ns() can have popped in and enqueued the timer
1231 * for us already.
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001232 */
Peter Zijlstra5de27552014-05-20 15:49:48 +02001233 if (restart != HRTIMER_NORESTART &&
1234 !(timer->state & HRTIMER_STATE_ENQUEUED))
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001235 enqueue_hrtimer(timer, base);
Salman Qazif13d4f92010-10-12 07:25:19 -07001236
Peter Zijlstra887d9dc2015-06-11 14:46:48 +02001237 /*
1238 * Separate the ->running assignment from the ->state assignment.
1239 *
1240 * As with a regular write barrier, this ensures the read side in
1241 * hrtimer_active() cannot observe cpu_base->running == NULL &&
1242 * timer->state == INACTIVE.
1243 */
1244 raw_write_seqcount_barrier(&cpu_base->seq);
Salman Qazif13d4f92010-10-12 07:25:19 -07001245
Peter Zijlstra887d9dc2015-06-11 14:46:48 +02001246 WARN_ON_ONCE(cpu_base->running != timer);
1247 cpu_base->running = NULL;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001248}
1249
Thomas Gleixner21d6d522015-04-14 21:08:35 +00001250static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now)
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001251{
Thomas Gleixner34aee882015-04-14 21:08:41 +00001252 struct hrtimer_clock_base *base = cpu_base->clock_base;
1253 unsigned int active = cpu_base->active_bases;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001254
Thomas Gleixner34aee882015-04-14 21:08:41 +00001255 for (; active; base++, active >>= 1) {
John Stultz998adc32010-09-20 19:19:17 -07001256 struct timerqueue_node *node;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001257 ktime_t basenow;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001258
Thomas Gleixner34aee882015-04-14 21:08:41 +00001259 if (!(active & 0x01))
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001260 continue;
1261
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001262 basenow = ktime_add(now, base->offset);
1263
John Stultz998adc32010-09-20 19:19:17 -07001264 while ((node = timerqueue_getnext(&base->active))) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001265 struct hrtimer *timer;
1266
John Stultz998adc32010-09-20 19:19:17 -07001267 timer = container_of(node, struct hrtimer, node);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001268
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001269 /*
1270 * The immediate goal for using the softexpires is
1271 * minimizing wakeups, not running timers at the
1272 * earliest interrupt after their soft expiration.
1273 * This allows us to avoid using a Priority Search
1274 * Tree, which can answer a stabbing querry for
1275 * overlapping intervals and instead use the simple
1276 * BST we already have.
1277 * We don't add extra wakeups by delaying timers that
1278 * are right-of a not yet expired timer, because that
1279 * timer will have to trigger a wakeup anyway.
1280 */
Thomas Gleixner2456e852016-12-25 11:38:40 +01001281 if (basenow < hrtimer_get_softexpires_tv64(timer))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001282 break;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001283
Thomas Gleixner21d6d522015-04-14 21:08:35 +00001284 __run_hrtimer(cpu_base, base, timer, &basenow);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001285 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001286 }
Thomas Gleixner21d6d522015-04-14 21:08:35 +00001287}
1288
1289#ifdef CONFIG_HIGH_RES_TIMERS
1290
1291/*
1292 * High resolution timer interrupt
1293 * Called with interrupts disabled
1294 */
1295void hrtimer_interrupt(struct clock_event_device *dev)
1296{
1297 struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
1298 ktime_t expires_next, now, entry_time, delta;
1299 int retries = 0;
1300
1301 BUG_ON(!cpu_base->hres_active);
1302 cpu_base->nr_events++;
Thomas Gleixner2456e852016-12-25 11:38:40 +01001303 dev->next_event = KTIME_MAX;
Thomas Gleixner21d6d522015-04-14 21:08:35 +00001304
1305 raw_spin_lock(&cpu_base->lock);
1306 entry_time = now = hrtimer_update_base(cpu_base);
1307retry:
1308 cpu_base->in_hrtirq = 1;
1309 /*
1310 * We set expires_next to KTIME_MAX here with cpu_base->lock
1311 * held to prevent that a timer is enqueued in our queue via
1312 * the migration code. This does not affect enqueueing of
1313 * timers which run their callback and need to be requeued on
1314 * this CPU.
1315 */
Thomas Gleixner2456e852016-12-25 11:38:40 +01001316 cpu_base->expires_next = KTIME_MAX;
Thomas Gleixner21d6d522015-04-14 21:08:35 +00001317
1318 __hrtimer_run_queues(cpu_base, now);
1319
Thomas Gleixner9bc749192015-01-20 21:24:10 +01001320 /* Reevaluate the clock bases for the next expiry */
1321 expires_next = __hrtimer_get_next_event(cpu_base);
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001322 /*
1323 * Store the new expiry value so the migration code can verify
1324 * against it.
1325 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001326 cpu_base->expires_next = expires_next;
Thomas Gleixner9bc749192015-01-20 21:24:10 +01001327 cpu_base->in_hrtirq = 0;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001328 raw_spin_unlock(&cpu_base->lock);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001329
1330 /* Reprogramming necessary ? */
Viresh Kumard25408752015-04-03 09:04:05 +05301331 if (!tick_program_event(expires_next, 0)) {
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001332 cpu_base->hang_detected = 0;
1333 return;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001334 }
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001335
1336 /*
1337 * The next timer was already expired due to:
1338 * - tracing
1339 * - long lasting callbacks
1340 * - being scheduled away when running in a VM
1341 *
1342 * We need to prevent that we loop forever in the hrtimer
1343 * interrupt routine. We give it 3 attempts to avoid
1344 * overreacting on some spurious event.
John Stultz5baefd62012-07-10 18:43:25 -04001345 *
1346 * Acquire base lock for updating the offsets and retrieving
1347 * the current time.
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001348 */
Thomas Gleixner196951e2012-07-10 18:43:23 -04001349 raw_spin_lock(&cpu_base->lock);
John Stultz5baefd62012-07-10 18:43:25 -04001350 now = hrtimer_update_base(cpu_base);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001351 cpu_base->nr_retries++;
1352 if (++retries < 3)
1353 goto retry;
1354 /*
1355 * Give the system a chance to do something else than looping
1356 * here. We stored the entry time, so we know exactly how long
1357 * we spent here. We schedule the next event this amount of
1358 * time away.
1359 */
1360 cpu_base->nr_hangs++;
1361 cpu_base->hang_detected = 1;
Thomas Gleixner196951e2012-07-10 18:43:23 -04001362 raw_spin_unlock(&cpu_base->lock);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001363 delta = ktime_sub(now, entry_time);
Thomas Gleixner2456e852016-12-25 11:38:40 +01001364 if ((unsigned int)delta > cpu_base->max_hang_time)
1365 cpu_base->max_hang_time = (unsigned int) delta;
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001366 /*
1367 * Limit it to a sensible value as we enforce a longer
1368 * delay. Give the CPU at least 100ms to catch up.
1369 */
Thomas Gleixner2456e852016-12-25 11:38:40 +01001370 if (delta > 100 * NSEC_PER_MSEC)
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001371 expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
1372 else
1373 expires_next = ktime_add(now, delta);
1374 tick_program_event(expires_next, 1);
1375 printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
1376 ktime_to_ns(delta));
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001377}
1378
Stephen Boyd016da202017-03-16 18:08:13 -07001379/* called with interrupts disabled */
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +00001380static inline void __hrtimer_peek_ahead_timers(void)
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001381{
1382 struct tick_device *td;
1383
1384 if (!hrtimer_hres_active())
1385 return;
1386
Christoph Lameter22127e92014-08-17 12:30:25 -05001387 td = this_cpu_ptr(&tick_cpu_device);
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001388 if (td && td->evtdev)
1389 hrtimer_interrupt(td->evtdev);
1390}
1391
Ingo Molnar82c5b7b2009-01-05 14:11:10 +01001392#else /* CONFIG_HIGH_RES_TIMERS */
1393
1394static inline void __hrtimer_peek_ahead_timers(void) { }
1395
1396#endif /* !CONFIG_HIGH_RES_TIMERS */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001397
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001398/*
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +00001399 * Called from run_local_timers in hardirq context every jiffy
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001400 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001401void hrtimer_run_queues(void)
1402{
Christoph Lameterdc5df73b2014-08-17 12:30:26 -05001403 struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
Thomas Gleixner21d6d522015-04-14 21:08:35 +00001404 ktime_t now;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001405
Thomas Gleixnere19ffe82015-04-14 21:08:39 +00001406 if (__hrtimer_hres_active(cpu_base))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001407 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001408
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +00001409 /*
1410 * This _is_ ugly: We have to check periodically, whether we
1411 * can switch to highres and / or nohz mode. The clocksource
1412 * switch happens with xtime_lock held. Notification from
1413 * there only sets the check bit in the tick_oneshot code,
1414 * otherwise we might deadlock vs. xtime_lock.
1415 */
1416 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled())) {
1417 hrtimer_switch_to_hres();
1418 return;
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001419 }
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +00001420
Thomas Gleixner21d6d522015-04-14 21:08:35 +00001421 raw_spin_lock(&cpu_base->lock);
1422 now = hrtimer_update_base(cpu_base);
1423 __hrtimer_run_queues(cpu_base, now);
1424 raw_spin_unlock(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001425}
1426
1427/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001428 * Sleep related functions:
1429 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001430static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001431{
1432 struct hrtimer_sleeper *t =
1433 container_of(timer, struct hrtimer_sleeper, timer);
1434 struct task_struct *task = t->task;
1435
1436 t->task = NULL;
1437 if (task)
1438 wake_up_process(task);
1439
1440 return HRTIMER_NORESTART;
1441}
1442
Ingo Molnar36c8b582006-07-03 00:25:41 -07001443void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001444{
1445 sl->timer.function = hrtimer_wakeup;
1446 sl->task = task;
1447}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07001448EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
Thomas Gleixner00362e32006-03-31 02:31:17 -08001449
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001450int nanosleep_copyout(struct restart_block *restart, struct timespec64 *ts)
Al Viroce41aaf2017-06-07 09:42:32 +01001451{
1452 switch(restart->nanosleep.type) {
1453#ifdef CONFIG_COMPAT
1454 case TT_COMPAT:
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001455 if (compat_put_timespec64(ts, restart->nanosleep.compat_rmtp))
Al Viroce41aaf2017-06-07 09:42:32 +01001456 return -EFAULT;
1457 break;
1458#endif
1459 case TT_NATIVE:
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001460 if (put_timespec64(ts, restart->nanosleep.rmtp))
Al Viroce41aaf2017-06-07 09:42:32 +01001461 return -EFAULT;
1462 break;
1463 default:
1464 BUG();
1465 }
1466 return -ERESTART_RESTARTBLOCK;
1467}
1468
Thomas Gleixner669d7862006-03-31 02:31:19 -08001469static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001470{
Al Viroedbeda42017-06-07 09:42:31 +01001471 struct restart_block *restart;
1472
Thomas Gleixner669d7862006-03-31 02:31:19 -08001473 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001474
Roman Zippel432569b2006-03-26 01:38:08 -08001475 do {
1476 set_current_state(TASK_INTERRUPTIBLE);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001477 hrtimer_start_expires(&t->timer, mode);
Roman Zippel432569b2006-03-26 01:38:08 -08001478
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001479 if (likely(t->task))
Colin Crossb0f8c442013-05-06 23:50:19 +00001480 freezable_schedule();
Roman Zippel432569b2006-03-26 01:38:08 -08001481
Thomas Gleixner669d7862006-03-31 02:31:19 -08001482 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001483 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -08001484
Thomas Gleixner669d7862006-03-31 02:31:19 -08001485 } while (t->task && !signal_pending(current));
1486
Peter Zijlstra3588a082008-02-01 17:45:13 +01001487 __set_current_state(TASK_RUNNING);
1488
Al Viroa7602682017-06-07 09:42:29 +01001489 if (!t->task)
Oleg Nesterov080344b2008-02-01 17:29:05 +03001490 return 0;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001491
Al Viroedbeda42017-06-07 09:42:31 +01001492 restart = &current->restart_block;
1493 if (restart->nanosleep.type != TT_NONE) {
Al Viroa7602682017-06-07 09:42:29 +01001494 ktime_t rem = hrtimer_expires_remaining(&t->timer);
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001495 struct timespec64 rmt;
Al Viroedbeda42017-06-07 09:42:31 +01001496
Al Viroa7602682017-06-07 09:42:29 +01001497 if (rem <= 0)
1498 return 0;
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001499 rmt = ktime_to_timespec64(rem);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001500
Al Viroce41aaf2017-06-07 09:42:32 +01001501 return nanosleep_copyout(restart, &rmt);
Al Viroa7602682017-06-07 09:42:29 +01001502 }
1503 return -ERESTART_RESTARTBLOCK;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001504}
1505
Al Virofb923c42017-06-07 09:42:33 +01001506static long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001507{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001508 struct hrtimer_sleeper t;
Al Viroa7602682017-06-07 09:42:29 +01001509 int ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001510
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001511 hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001512 HRTIMER_MODE_ABS);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001513 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001514
Al Viroa7602682017-06-07 09:42:29 +01001515 ret = do_nanosleep(&t, HRTIMER_MODE_ABS);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001516 destroy_hrtimer_on_stack(&t.timer);
1517 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001518}
1519
Thomas Gleixner938e7cf2017-06-13 23:34:33 +02001520long hrtimer_nanosleep(const struct timespec64 *rqtp,
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001521 const enum hrtimer_mode mode, const clockid_t clockid)
1522{
Al Viroa7602682017-06-07 09:42:29 +01001523 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -08001524 struct hrtimer_sleeper t;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001525 int ret = 0;
John Stultzda8b44d2016-03-17 14:20:51 -07001526 u64 slack;
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001527
1528 slack = current->timer_slack_ns;
Dario Faggioliaab03e02013-11-28 11:14:43 +01001529 if (dl_task(current) || rt_task(current))
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001530 slack = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001531
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001532 hrtimer_init_on_stack(&t.timer, clockid, mode);
Deepa Dinamaniad196382017-03-26 12:04:18 -07001533 hrtimer_set_expires_range_ns(&t.timer, timespec64_to_ktime(*rqtp), slack);
Al Viroa7602682017-06-07 09:42:29 +01001534 ret = do_nanosleep(&t, mode);
1535 if (ret != -ERESTART_RESTARTBLOCK)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001536 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001537
George Anzinger7978672c2006-02-01 03:05:11 -08001538 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001539 if (mode == HRTIMER_MODE_ABS) {
1540 ret = -ERESTARTNOHAND;
1541 goto out;
1542 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001543
Al Viroa7602682017-06-07 09:42:29 +01001544 restart = &current->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -07001545 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001546 restart->nanosleep.clockid = t.timer.base->clockid;
Arjan van de Vencc584b22008-09-01 15:02:30 -07001547 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001548out:
1549 destroy_hrtimer_on_stack(&t.timer);
1550 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001551}
1552
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001553SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1554 struct timespec __user *, rmtp)
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001555{
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001556 struct timespec64 tu;
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001557
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001558 if (get_timespec64(&tu, rqtp))
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001559 return -EFAULT;
1560
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001561 if (!timespec64_valid(&tu))
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001562 return -EINVAL;
1563
Al Viroedbeda42017-06-07 09:42:31 +01001564 current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
Al Viro192a82f2017-06-07 09:42:28 +01001565 current->restart_block.nanosleep.rmtp = rmtp;
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001566 return hrtimer_nanosleep(&tu, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001567}
1568
Al Viroedbeda42017-06-07 09:42:31 +01001569#ifdef CONFIG_COMPAT
1570
1571COMPAT_SYSCALL_DEFINE2(nanosleep, struct compat_timespec __user *, rqtp,
1572 struct compat_timespec __user *, rmtp)
1573{
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001574 struct timespec64 tu;
Al Viroedbeda42017-06-07 09:42:31 +01001575
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001576 if (compat_get_timespec64(&tu, rqtp))
Al Viroedbeda42017-06-07 09:42:31 +01001577 return -EFAULT;
1578
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001579 if (!timespec64_valid(&tu))
Al Viroedbeda42017-06-07 09:42:31 +01001580 return -EINVAL;
1581
1582 current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
1583 current->restart_block.nanosleep.compat_rmtp = rmtp;
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001584 return hrtimer_nanosleep(&tu, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Al Viroedbeda42017-06-07 09:42:31 +01001585}
1586#endif
1587
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001588/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001589 * Functions related to boot-time initialization:
1590 */
Thomas Gleixner27590dc2016-07-15 10:41:04 +02001591int hrtimers_prepare_cpu(unsigned int cpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001592{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001593 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001594 int i;
1595
John Stultz998adc32010-09-20 19:19:17 -07001596 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001597 cpu_base->clock_base[i].cpu_base = cpu_base;
John Stultz998adc32010-09-20 19:19:17 -07001598 timerqueue_init_head(&cpu_base->clock_base[i].active);
1599 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001600
Thomas Gleixnerfdd88d72018-01-26 14:54:32 +01001601 cpu_base->active_bases = 0;
Viresh Kumarcddd0242014-06-22 01:29:15 +02001602 cpu_base->cpu = cpu;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001603 hrtimer_init_hres(cpu_base);
Thomas Gleixner27590dc2016-07-15 10:41:04 +02001604 return 0;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001605}
1606
1607#ifdef CONFIG_HOTPLUG_CPU
1608
Peter Zijlstraca109492008-11-25 12:43:51 +01001609static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
Peter Zijlstra37810652008-12-04 11:17:10 +01001610 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001611{
1612 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001613 struct timerqueue_node *node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001614
John Stultz998adc32010-09-20 19:19:17 -07001615 while ((node = timerqueue_getnext(&old_base->active))) {
1616 timer = container_of(node, struct hrtimer, node);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001617 BUG_ON(hrtimer_callback_running(timer));
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001618 debug_deactivate(timer);
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001619
1620 /*
Oleg Nesterovc04dca02015-06-11 14:46:44 +02001621 * Mark it as ENQUEUED not INACTIVE otherwise the
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001622 * timer could be seen as !active and just vanish away
1623 * under us on another CPU
1624 */
Oleg Nesterovc04dca02015-06-11 14:46:44 +02001625 __remove_hrtimer(timer, old_base, HRTIMER_STATE_ENQUEUED, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001626 timer->base = new_base;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001627 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001628 * Enqueue the timers on the new cpu. This does not
1629 * reprogram the event device in case the timer
1630 * expires before the earliest on this CPU, but we run
1631 * hrtimer_interrupt after we migrated everything to
1632 * sort out already expired timers and reprogram the
1633 * event device.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001634 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001635 enqueue_hrtimer(timer, new_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001636 }
1637}
1638
Thomas Gleixner27590dc2016-07-15 10:41:04 +02001639int hrtimers_dead_cpu(unsigned int scpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001640{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001641 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001642 int i;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001643
Peter Zijlstra37810652008-12-04 11:17:10 +01001644 BUG_ON(cpu_online(scpu));
Peter Zijlstra37810652008-12-04 11:17:10 +01001645 tick_cancel_sched_timer(scpu);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001646
1647 local_irq_disable();
1648 old_base = &per_cpu(hrtimer_bases, scpu);
Christoph Lameterdc5df73b2014-08-17 12:30:26 -05001649 new_base = this_cpu_ptr(&hrtimer_bases);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001650 /*
1651 * The caller is globally serialized and nobody else
1652 * takes two locks at once, deadlock is not possible.
1653 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001654 raw_spin_lock(&new_base->lock);
1655 raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001656
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001657 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Peter Zijlstraca109492008-11-25 12:43:51 +01001658 migrate_hrtimer_list(&old_base->clock_base[i],
Peter Zijlstra37810652008-12-04 11:17:10 +01001659 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001660 }
1661
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001662 raw_spin_unlock(&old_base->lock);
1663 raw_spin_unlock(&new_base->lock);
Peter Zijlstra37810652008-12-04 11:17:10 +01001664
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001665 /* Check, if we got expired work to do */
1666 __hrtimer_peek_ahead_timers();
1667 local_irq_enable();
Thomas Gleixner27590dc2016-07-15 10:41:04 +02001668 return 0;
Peter Zijlstra37810652008-12-04 11:17:10 +01001669}
1670
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001671#endif /* CONFIG_HOTPLUG_CPU */
1672
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001673void __init hrtimers_init(void)
1674{
Thomas Gleixner27590dc2016-07-15 10:41:04 +02001675 hrtimers_prepare_cpu(smp_processor_id());
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001676}
1677
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001678/**
Carsten Emde351b3f72010-04-02 22:40:19 +02001679 * schedule_hrtimeout_range_clock - sleep until timeout
1680 * @expires: timeout value (ktime_t)
1681 * @delta: slack in expires timeout (ktime_t)
1682 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1683 * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
1684 */
1685int __sched
John Stultzda8b44d2016-03-17 14:20:51 -07001686schedule_hrtimeout_range_clock(ktime_t *expires, u64 delta,
Carsten Emde351b3f72010-04-02 22:40:19 +02001687 const enum hrtimer_mode mode, int clock)
1688{
1689 struct hrtimer_sleeper t;
1690
1691 /*
1692 * Optimize when a zero timeout value is given. It does not
1693 * matter whether this is an absolute or a relative time.
1694 */
Thomas Gleixner2456e852016-12-25 11:38:40 +01001695 if (expires && *expires == 0) {
Carsten Emde351b3f72010-04-02 22:40:19 +02001696 __set_current_state(TASK_RUNNING);
1697 return 0;
1698 }
1699
1700 /*
Namhyung Kim43b21012010-12-22 19:01:47 +01001701 * A NULL parameter means "infinite"
Carsten Emde351b3f72010-04-02 22:40:19 +02001702 */
1703 if (!expires) {
1704 schedule();
Carsten Emde351b3f72010-04-02 22:40:19 +02001705 return -EINTR;
1706 }
1707
1708 hrtimer_init_on_stack(&t.timer, clock, mode);
1709 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1710
1711 hrtimer_init_sleeper(&t, current);
1712
1713 hrtimer_start_expires(&t.timer, mode);
Carsten Emde351b3f72010-04-02 22:40:19 +02001714
1715 if (likely(t.task))
1716 schedule();
1717
1718 hrtimer_cancel(&t.timer);
1719 destroy_hrtimer_on_stack(&t.timer);
1720
1721 __set_current_state(TASK_RUNNING);
1722
1723 return !t.task ? 0 : -EINTR;
1724}
1725
1726/**
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001727 * schedule_hrtimeout_range - sleep until timeout
1728 * @expires: timeout value (ktime_t)
1729 * @delta: slack in expires timeout (ktime_t)
1730 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1731 *
1732 * Make the current task sleep until the given expiry time has
1733 * elapsed. The routine will return immediately unless
1734 * the current task state has been set (see set_current_state()).
1735 *
1736 * The @delta argument gives the kernel the freedom to schedule the
1737 * actual wakeup to a time that is both power and performance friendly.
1738 * The kernel give the normal best effort behavior for "@expires+@delta",
1739 * but may decide to fire the timer earlier, but no earlier than @expires.
1740 *
1741 * You can set the task state as follows -
1742 *
1743 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
Douglas Anderson4b7e9cf2016-10-21 08:58:51 -07001744 * pass before the routine returns unless the current task is explicitly
1745 * woken up, (e.g. by wake_up_process()).
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001746 *
1747 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
Douglas Anderson4b7e9cf2016-10-21 08:58:51 -07001748 * delivered to the current task or the current task is explicitly woken
1749 * up.
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001750 *
1751 * The current task state is guaranteed to be TASK_RUNNING when this
1752 * routine returns.
1753 *
Douglas Anderson4b7e9cf2016-10-21 08:58:51 -07001754 * Returns 0 when the timer has expired. If the task was woken before the
1755 * timer expired by a signal (only possible in state TASK_INTERRUPTIBLE) or
1756 * by an explicit wakeup, it returns -EINTR.
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001757 */
John Stultzda8b44d2016-03-17 14:20:51 -07001758int __sched schedule_hrtimeout_range(ktime_t *expires, u64 delta,
Carsten Emde351b3f72010-04-02 22:40:19 +02001759 const enum hrtimer_mode mode)
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001760{
Carsten Emde351b3f72010-04-02 22:40:19 +02001761 return schedule_hrtimeout_range_clock(expires, delta, mode,
1762 CLOCK_MONOTONIC);
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001763}
1764EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1765
1766/**
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001767 * schedule_hrtimeout - sleep until timeout
1768 * @expires: timeout value (ktime_t)
1769 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1770 *
1771 * Make the current task sleep until the given expiry time has
1772 * elapsed. The routine will return immediately unless
1773 * the current task state has been set (see set_current_state()).
1774 *
1775 * You can set the task state as follows -
1776 *
1777 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
Douglas Anderson4b7e9cf2016-10-21 08:58:51 -07001778 * pass before the routine returns unless the current task is explicitly
1779 * woken up, (e.g. by wake_up_process()).
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001780 *
1781 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
Douglas Anderson4b7e9cf2016-10-21 08:58:51 -07001782 * delivered to the current task or the current task is explicitly woken
1783 * up.
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001784 *
1785 * The current task state is guaranteed to be TASK_RUNNING when this
1786 * routine returns.
1787 *
Douglas Anderson4b7e9cf2016-10-21 08:58:51 -07001788 * Returns 0 when the timer has expired. If the task was woken before the
1789 * timer expired by a signal (only possible in state TASK_INTERRUPTIBLE) or
1790 * by an explicit wakeup, it returns -EINTR.
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001791 */
1792int __sched schedule_hrtimeout(ktime_t *expires,
1793 const enum hrtimer_mode mode)
1794{
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001795 return schedule_hrtimeout_range(expires, 0, mode);
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001796}
1797EXPORT_SYMBOL_GPL(schedule_hrtimeout);