blob: f2de328bb8d52f1b233e1fd047964a74488b6b1a [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
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000181static inline
182struct hrtimer_cpu_base *get_target_base(struct hrtimer_cpu_base *base,
183 int pinned)
184{
Thomas Gleixnerae67bad2018-01-14 23:30:51 +0100185#if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
186 if (static_branch_likely(&timers_migration_enabled) && !pinned)
187 return &per_cpu(hrtimer_bases, get_nohz_timer_target());
188#endif
Frederic Weisbecker662b3e12015-08-18 16:18:28 +0200189 return base;
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000190}
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000191
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800192/*
Frederic Weisbeckerb48362d2015-08-18 16:18:29 +0200193 * We switch the timer base to a power-optimized selected CPU target,
194 * if:
195 * - NO_HZ_COMMON is enabled
196 * - timer migration is enabled
197 * - the timer callback is not running
198 * - the timer is not the first expiring timer on the new target
199 *
200 * If one of the above requirements is not fulfilled we move the timer
201 * to the current CPU or leave it on the previously assigned CPU if
202 * the timer callback is currently running.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800203 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800204static inline struct hrtimer_clock_base *
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530205switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
206 int pinned)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800207{
Frederic Weisbeckerb48362d2015-08-18 16:18:29 +0200208 struct hrtimer_cpu_base *new_cpu_base, *this_cpu_base;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800209 struct hrtimer_clock_base *new_base;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200210 int basenum = base->index;
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530211
Frederic Weisbeckerb48362d2015-08-18 16:18:29 +0200212 this_cpu_base = this_cpu_ptr(&hrtimer_bases);
213 new_cpu_base = get_target_base(this_cpu_base, pinned);
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530214again:
John Stultze06383d2010-12-14 19:37:07 -0800215 new_base = &new_cpu_base->clock_base[basenum];
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800216
217 if (base != new_base) {
218 /*
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200219 * We are trying to move timer to new_base.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800220 * However we can't change timer's base while it is running,
221 * so we keep it on the same CPU. No hassle vs. reprogramming
222 * the event source in the high resolution case. The softirq
223 * code will take care of this when the timer function has
224 * completed. There is no conflict as we hold the lock until
225 * the timer is enqueued.
226 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800227 if (unlikely(hrtimer_callback_running(timer)))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800228 return base;
229
Peter Zijlstra887d9dc2015-06-11 14:46:48 +0200230 /* See the comment in lock_hrtimer_base() */
231 timer->base = &migration_base;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100232 raw_spin_unlock(&base->cpu_base->lock);
233 raw_spin_lock(&new_base->cpu_base->lock);
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530234
Frederic Weisbeckerb48362d2015-08-18 16:18:29 +0200235 if (new_cpu_base != this_cpu_base &&
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000236 hrtimer_check_target(timer, new_base)) {
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100237 raw_spin_unlock(&new_base->cpu_base->lock);
238 raw_spin_lock(&base->cpu_base->lock);
Frederic Weisbeckerb48362d2015-08-18 16:18:29 +0200239 new_cpu_base = this_cpu_base;
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200240 timer->base = base;
241 goto again;
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530242 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800243 timer->base = new_base;
Leon Ma012a45e2014-04-30 16:43:10 +0800244 } else {
Frederic Weisbeckerb48362d2015-08-18 16:18:29 +0200245 if (new_cpu_base != this_cpu_base &&
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000246 hrtimer_check_target(timer, new_base)) {
Frederic Weisbeckerb48362d2015-08-18 16:18:29 +0200247 new_cpu_base = this_cpu_base;
Leon Ma012a45e2014-04-30 16:43:10 +0800248 goto again;
249 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800250 }
251 return new_base;
252}
253
254#else /* CONFIG_SMP */
255
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800256static inline struct hrtimer_clock_base *
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800257lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
258{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800259 struct hrtimer_clock_base *base = timer->base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800260
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100261 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800262
263 return base;
264}
265
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530266# define switch_hrtimer_base(t, b, p) (b)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800267
268#endif /* !CONFIG_SMP */
269
270/*
271 * Functions for the union type storage format of ktime_t which are
272 * too large for inlining:
273 */
274#if BITS_PER_LONG < 64
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800275/*
276 * Divide a ktime value by a nanosecond value
277 */
John Stultzf7bcb702015-05-08 13:47:23 -0700278s64 __ktime_divns(const ktime_t kt, s64 div)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800279{
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800280 int sft = 0;
John Stultzf7bcb702015-05-08 13:47:23 -0700281 s64 dclc;
282 u64 tmp;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800283
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300284 dclc = ktime_to_ns(kt);
John Stultzf7bcb702015-05-08 13:47:23 -0700285 tmp = dclc < 0 ? -dclc : dclc;
286
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800287 /* Make sure the divisor is less than 2^32: */
288 while (div >> 32) {
289 sft++;
290 div >>= 1;
291 }
John Stultzf7bcb702015-05-08 13:47:23 -0700292 tmp >>= sft;
293 do_div(tmp, (unsigned long) div);
294 return dclc < 0 ? -tmp : tmp;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800295}
Nicolas Pitre8b618622014-12-03 14:43:06 -0500296EXPORT_SYMBOL_GPL(__ktime_divns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800297#endif /* BITS_PER_LONG >= 64 */
298
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100299/*
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100300 * Add two ktime values and do a safety check for overflow:
301 */
302ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
303{
Vegard Nossum979515c2016-08-13 01:37:04 +0200304 ktime_t res = ktime_add_unsafe(lhs, rhs);
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100305
306 /*
307 * We use KTIME_SEC_MAX here, the maximum timeout which we can
308 * return to user space in a timespec:
309 */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100310 if (res < 0 || res < lhs || res < rhs)
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100311 res = ktime_set(KTIME_SEC_MAX, 0);
312
313 return res;
314}
315
Artem Bityutskiy8daa21e2009-05-28 16:21:24 +0300316EXPORT_SYMBOL_GPL(ktime_add_safe);
317
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700318#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
319
320static struct debug_obj_descr hrtimer_debug_descr;
321
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100322static void *hrtimer_debug_hint(void *addr)
323{
324 return ((struct hrtimer *) addr)->function;
325}
326
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700327/*
328 * fixup_init is called when:
329 * - an active object is initialized
330 */
Du, Changbine3252462016-05-19 17:09:29 -0700331static bool hrtimer_fixup_init(void *addr, enum debug_obj_state state)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700332{
333 struct hrtimer *timer = addr;
334
335 switch (state) {
336 case ODEBUG_STATE_ACTIVE:
337 hrtimer_cancel(timer);
338 debug_object_init(timer, &hrtimer_debug_descr);
Du, Changbine3252462016-05-19 17:09:29 -0700339 return true;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700340 default:
Du, Changbine3252462016-05-19 17:09:29 -0700341 return false;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700342 }
343}
344
345/*
346 * fixup_activate is called when:
347 * - an active object is activated
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700348 * - an unknown non-static object is activated
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700349 */
Du, Changbine3252462016-05-19 17:09:29 -0700350static bool hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700351{
352 switch (state) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700353 case ODEBUG_STATE_ACTIVE:
354 WARN_ON(1);
355
356 default:
Du, Changbine3252462016-05-19 17:09:29 -0700357 return false;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700358 }
359}
360
361/*
362 * fixup_free is called when:
363 * - an active object is freed
364 */
Du, Changbine3252462016-05-19 17:09:29 -0700365static bool hrtimer_fixup_free(void *addr, enum debug_obj_state state)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700366{
367 struct hrtimer *timer = addr;
368
369 switch (state) {
370 case ODEBUG_STATE_ACTIVE:
371 hrtimer_cancel(timer);
372 debug_object_free(timer, &hrtimer_debug_descr);
Du, Changbine3252462016-05-19 17:09:29 -0700373 return true;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700374 default:
Du, Changbine3252462016-05-19 17:09:29 -0700375 return false;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700376 }
377}
378
379static struct debug_obj_descr hrtimer_debug_descr = {
380 .name = "hrtimer",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100381 .debug_hint = hrtimer_debug_hint,
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700382 .fixup_init = hrtimer_fixup_init,
383 .fixup_activate = hrtimer_fixup_activate,
384 .fixup_free = hrtimer_fixup_free,
385};
386
387static inline void debug_hrtimer_init(struct hrtimer *timer)
388{
389 debug_object_init(timer, &hrtimer_debug_descr);
390}
391
392static inline void debug_hrtimer_activate(struct hrtimer *timer)
393{
394 debug_object_activate(timer, &hrtimer_debug_descr);
395}
396
397static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
398{
399 debug_object_deactivate(timer, &hrtimer_debug_descr);
400}
401
402static inline void debug_hrtimer_free(struct hrtimer *timer)
403{
404 debug_object_free(timer, &hrtimer_debug_descr);
405}
406
407static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
408 enum hrtimer_mode mode);
409
410void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
411 enum hrtimer_mode mode)
412{
413 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
414 __hrtimer_init(timer, clock_id, mode);
415}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -0700416EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700417
418void destroy_hrtimer_on_stack(struct hrtimer *timer)
419{
420 debug_object_free(timer, &hrtimer_debug_descr);
421}
Guenter Roeckc08376a2016-05-26 17:21:05 -0700422EXPORT_SYMBOL_GPL(destroy_hrtimer_on_stack);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700423
424#else
425static inline void debug_hrtimer_init(struct hrtimer *timer) { }
426static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
427static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
428#endif
429
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800430static inline void
431debug_init(struct hrtimer *timer, clockid_t clockid,
432 enum hrtimer_mode mode)
433{
434 debug_hrtimer_init(timer);
435 trace_hrtimer_init(timer, clockid, mode);
436}
437
438static inline void debug_activate(struct hrtimer *timer)
439{
440 debug_hrtimer_activate(timer);
441 trace_hrtimer_start(timer);
442}
443
444static inline void debug_deactivate(struct hrtimer *timer)
445{
446 debug_hrtimer_deactivate(timer);
447 trace_hrtimer_cancel(timer);
448}
449
Thomas Gleixner9bc749192015-01-20 21:24:10 +0100450#if defined(CONFIG_NO_HZ_COMMON) || defined(CONFIG_HIGH_RES_TIMERS)
Thomas Gleixner895bdfa2015-04-14 21:08:49 +0000451static inline void hrtimer_update_next_timer(struct hrtimer_cpu_base *cpu_base,
452 struct hrtimer *timer)
453{
454#ifdef CONFIG_HIGH_RES_TIMERS
455 cpu_base->next_timer = timer;
456#endif
457}
458
kbuild test robot4ebbda52015-01-23 20:12:06 +0800459static ktime_t __hrtimer_get_next_event(struct hrtimer_cpu_base *cpu_base)
Thomas Gleixner9bc749192015-01-20 21:24:10 +0100460{
461 struct hrtimer_clock_base *base = cpu_base->clock_base;
Thomas Gleixner34aee882015-04-14 21:08:41 +0000462 unsigned int active = cpu_base->active_bases;
Thomas Gleixner2456e852016-12-25 11:38:40 +0100463 ktime_t expires, expires_next = KTIME_MAX;
Thomas Gleixner9bc749192015-01-20 21:24:10 +0100464
Thomas Gleixner895bdfa2015-04-14 21:08:49 +0000465 hrtimer_update_next_timer(cpu_base, NULL);
Thomas Gleixner34aee882015-04-14 21:08:41 +0000466 for (; active; base++, active >>= 1) {
Thomas Gleixner9bc749192015-01-20 21:24:10 +0100467 struct timerqueue_node *next;
468 struct hrtimer *timer;
469
Thomas Gleixner34aee882015-04-14 21:08:41 +0000470 if (!(active & 0x01))
Thomas Gleixner9bc749192015-01-20 21:24:10 +0100471 continue;
472
Thomas Gleixner34aee882015-04-14 21:08:41 +0000473 next = timerqueue_getnext(&base->active);
Thomas Gleixner9bc749192015-01-20 21:24:10 +0100474 timer = container_of(next, struct hrtimer, node);
475 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixner2456e852016-12-25 11:38:40 +0100476 if (expires < expires_next) {
Thomas Gleixner9bc749192015-01-20 21:24:10 +0100477 expires_next = expires;
Thomas Gleixner895bdfa2015-04-14 21:08:49 +0000478 hrtimer_update_next_timer(cpu_base, timer);
479 }
Thomas Gleixner9bc749192015-01-20 21:24:10 +0100480 }
481 /*
482 * clock_was_set() might have changed base->offset of any of
483 * the clock bases so the result might be negative. Fix it up
484 * to prevent a false positive in clockevents_program_event().
485 */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100486 if (expires_next < 0)
487 expires_next = 0;
Thomas Gleixner9bc749192015-01-20 21:24:10 +0100488 return expires_next;
489}
490#endif
491
Thomas Gleixner21d6d522015-04-14 21:08:35 +0000492static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
493{
494 ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
495 ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset;
496 ktime_t *offs_tai = &base->clock_base[HRTIMER_BASE_TAI].offset;
497
Thomas Gleixner868a3e92015-04-14 21:08:37 +0000498 return ktime_get_update_offsets_now(&base->clock_was_set_seq,
499 offs_real, offs_boot, offs_tai);
Thomas Gleixner21d6d522015-04-14 21:08:35 +0000500}
501
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800502/* High resolution timer related functions */
503#ifdef CONFIG_HIGH_RES_TIMERS
504
505/*
506 * High resolution timer enabled ?
507 */
Kees Cook4cc7ecb72016-03-17 14:23:00 -0700508static bool hrtimer_hres_enabled __read_mostly = true;
Thomas Gleixner398ca172015-04-14 21:08:27 +0000509unsigned int hrtimer_resolution __read_mostly = LOW_RES_NSEC;
510EXPORT_SYMBOL_GPL(hrtimer_resolution);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800511
512/*
513 * Enable / Disable high resolution mode
514 */
515static int __init setup_hrtimer_hres(char *str)
516{
Kees Cook4cc7ecb72016-03-17 14:23:00 -0700517 return (kstrtobool(str, &hrtimer_hres_enabled) == 0);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800518}
519
520__setup("highres=", setup_hrtimer_hres);
521
522/*
523 * hrtimer_high_res_enabled - query, if the highres mode is enabled
524 */
525static inline int hrtimer_is_hres_enabled(void)
526{
527 return hrtimer_hres_enabled;
528}
529
530/*
531 * Is the high resolution mode active ?
532 */
Thomas Gleixnere19ffe82015-04-14 21:08:39 +0000533static inline int __hrtimer_hres_active(struct hrtimer_cpu_base *cpu_base)
534{
535 return cpu_base->hres_active;
536}
537
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800538static inline int hrtimer_hres_active(void)
539{
Thomas Gleixnere19ffe82015-04-14 21:08:39 +0000540 return __hrtimer_hres_active(this_cpu_ptr(&hrtimer_bases));
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800541}
542
543/*
544 * Reprogram the event source with checking both queues for the
545 * next event
546 * Called with interrupts disabled and base->lock held
547 */
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400548static void
549hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800550{
Thomas Gleixner21d6d522015-04-14 21:08:35 +0000551 ktime_t expires_next;
552
553 if (!cpu_base->hres_active)
554 return;
555
556 expires_next = __hrtimer_get_next_event(cpu_base);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800557
Thomas Gleixner2456e852016-12-25 11:38:40 +0100558 if (skip_equal && expires_next == cpu_base->expires_next)
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400559 return;
560
Thomas Gleixner2456e852016-12-25 11:38:40 +0100561 cpu_base->expires_next = expires_next;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400562
Stuart Hayes6c6c0d52014-04-29 17:55:02 -0500563 /*
564 * If a hang was detected in the last timer interrupt then we
565 * leave the hang delay active in the hardware. We want the
566 * system to make progress. That also prevents the following
567 * scenario:
568 * T1 expires 50ms from now
569 * T2 expires 5s from now
570 *
571 * T1 is removed, so this code is called and would reprogram
572 * the hardware to 5s from now. Any hrtimer_start after that
573 * will not reprogram the hardware due to hang_detected being
574 * set. So we'd effectivly block all timers until the T2 event
575 * fires.
576 */
577 if (cpu_base->hang_detected)
578 return;
579
Viresh Kumard25408752015-04-03 09:04:05 +0530580 tick_program_event(cpu_base->expires_next, 1);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800581}
582
583/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800584 * When a timer is enqueued and expires earlier than the already enqueued
585 * timers, we have to check, whether it expires earlier than the timer for
586 * which the clock event device was armed.
587 *
588 * Called with interrupts disabled and base->cpu_base.lock held
589 */
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +0000590static void hrtimer_reprogram(struct hrtimer *timer,
591 struct hrtimer_clock_base *base)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800592{
Christoph Lameterdc5df732014-08-17 12:30:26 -0500593 struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700594 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800595
Arjan van de Vencc584b22008-09-01 15:02:30 -0700596 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
Thomas Gleixner63070a72008-02-14 00:58:36 +0100597
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800598 /*
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +0000599 * If the timer is not on the current cpu, we cannot reprogram
600 * the other cpus clock event device.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800601 */
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +0000602 if (base->cpu_base != cpu_base)
603 return;
604
605 /*
606 * If the hrtimer interrupt is running, then it will
607 * reevaluate the clock bases and reprogram the clock event
608 * device. The callbacks are always executed in hard interrupt
609 * context so we don't need an extra check for a running
610 * callback.
611 */
612 if (cpu_base->in_hrtirq)
613 return;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800614
Thomas Gleixner63070a72008-02-14 00:58:36 +0100615 /*
616 * CLOCK_REALTIME timer might be requested with an absolute
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +0000617 * expiry time which is less than base->offset. Set it to 0.
Thomas Gleixner63070a72008-02-14 00:58:36 +0100618 */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100619 if (expires < 0)
620 expires = 0;
Thomas Gleixner63070a72008-02-14 00:58:36 +0100621
Thomas Gleixner2456e852016-12-25 11:38:40 +0100622 if (expires >= cpu_base->expires_next)
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +0000623 return;
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100624
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +0000625 /* Update the pointer to the next expiring timer */
Thomas Gleixner895bdfa2015-04-14 21:08:49 +0000626 cpu_base->next_timer = timer;
Thomas Gleixner9bc749192015-01-20 21:24:10 +0100627
628 /*
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100629 * If a hang was detected in the last timer interrupt then we
630 * do not schedule a timer which is earlier than the expiry
631 * which we enforced in the hang detection. We want the system
632 * to make progress.
633 */
634 if (cpu_base->hang_detected)
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +0000635 return;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800636
637 /*
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +0000638 * Program the timer hardware. We enforce the expiry for
639 * events which are already in the past.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800640 */
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +0000641 cpu_base->expires_next = expires;
642 tick_program_event(expires, 1);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800643}
644
Ingo Molnar995f0542007-04-07 12:05:00 +0200645/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800646 * Initialize the high resolution related parts of cpu_base
647 */
648static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
649{
Thomas Gleixner2456e852016-12-25 11:38:40 +0100650 base->expires_next = KTIME_MAX;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800651 base->hres_active = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800652}
653
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200654/*
655 * Retrigger next event is called after clock was set
656 *
657 * Called with interrupts disabled via on_each_cpu()
658 */
659static void retrigger_next_event(void *arg)
660{
Christoph Lameterdc5df732014-08-17 12:30:26 -0500661 struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200662
Thomas Gleixnere19ffe82015-04-14 21:08:39 +0000663 if (!base->hres_active)
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200664 return;
665
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200666 raw_spin_lock(&base->lock);
John Stultz5baefd62012-07-10 18:43:25 -0400667 hrtimer_update_base(base);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200668 hrtimer_force_reprogram(base, 0);
669 raw_spin_unlock(&base->lock);
670}
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200671
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800672/*
673 * Switch to high resolution mode
674 */
Luiz Capitulino75e3b372015-08-11 16:40:43 -0400675static void hrtimer_switch_to_hres(void)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800676{
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +0000677 struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800678
679 if (tick_init_highres()) {
Ingo Molnar820de5c2007-07-21 04:37:36 -0700680 printk(KERN_WARNING "Could not switch to high resolution "
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +0000681 "mode on CPU %d\n", base->cpu);
Guenter Roeck85e1cd62015-08-22 01:10:47 -0700682 return;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800683 }
684 base->hres_active = 1;
Thomas Gleixner398ca172015-04-14 21:08:27 +0000685 hrtimer_resolution = HIGH_RES_NSEC;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800686
687 tick_setup_sched_timer();
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800688 /* "Retrigger" the interrupt to get things going */
689 retrigger_next_event(NULL);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800690}
691
Thomas Gleixner5ec24812013-07-05 12:09:18 +0200692static void clock_was_set_work(struct work_struct *work)
693{
694 clock_was_set();
695}
696
697static DECLARE_WORK(hrtimer_work, clock_was_set_work);
698
John Stultzf55a6fa2012-07-10 18:43:19 -0400699/*
Pratyush Patelb4d90e92016-06-23 20:50:37 +0200700 * Called from timekeeping and resume code to reprogram the hrtimer
Thomas Gleixner5ec24812013-07-05 12:09:18 +0200701 * interrupt device on all cpus.
John Stultzf55a6fa2012-07-10 18:43:19 -0400702 */
703void clock_was_set_delayed(void)
704{
Thomas Gleixner5ec24812013-07-05 12:09:18 +0200705 schedule_work(&hrtimer_work);
John Stultzf55a6fa2012-07-10 18:43:19 -0400706}
707
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800708#else
709
Thomas Gleixnere19ffe82015-04-14 21:08:39 +0000710static inline int __hrtimer_hres_active(struct hrtimer_cpu_base *b) { return 0; }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800711static inline int hrtimer_hres_active(void) { return 0; }
712static inline int hrtimer_is_hres_enabled(void) { return 0; }
Luiz Capitulino75e3b372015-08-11 16:40:43 -0400713static inline void hrtimer_switch_to_hres(void) { }
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400714static inline void
715hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
Viresh Kumar9e1e01d2014-06-22 01:29:17 +0200716static inline int hrtimer_reprogram(struct hrtimer *timer,
717 struct hrtimer_clock_base *base)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800718{
719 return 0;
720}
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800721static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200722static inline void retrigger_next_event(void *arg) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800723
724#endif /* CONFIG_HIGH_RES_TIMERS */
725
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200726/*
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200727 * Clock realtime was set
728 *
729 * Change the offset of the realtime clock vs. the monotonic
730 * clock.
731 *
732 * We might have to reprogram the high resolution timer interrupt. On
733 * SMP we call the architecture specific code to retrigger _all_ high
734 * resolution timer interrupts. On UP we just disable interrupts and
735 * call the high resolution interrupt code.
736 */
737void clock_was_set(void)
738{
Thomas Gleixner90ff1f32011-05-25 23:08:17 +0200739#ifdef CONFIG_HIGH_RES_TIMERS
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200740 /* Retrigger the CPU local events everywhere */
741 on_each_cpu(retrigger_next_event, NULL, 1);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200742#endif
743 timerfd_clock_was_set();
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200744}
745
746/*
747 * During resume we might have to reprogram the high resolution timer
David Vrabel7c4c3a02013-06-27 11:35:44 +0100748 * interrupt on all online CPUs. However, all other CPUs will be
749 * stopped with IRQs interrupts disabled so the clock_was_set() call
Thomas Gleixner5ec24812013-07-05 12:09:18 +0200750 * must be deferred.
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200751 */
752void hrtimers_resume(void)
753{
Frederic Weisbecker53bef3f2017-11-06 16:01:21 +0100754 lockdep_assert_irqs_disabled();
Thomas Gleixner5ec24812013-07-05 12:09:18 +0200755 /* Retrigger on the local CPU */
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200756 retrigger_next_event(NULL);
Thomas Gleixner5ec24812013-07-05 12:09:18 +0200757 /* And schedule a retrigger for all others */
758 clock_was_set_delayed();
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200759}
760
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800761/*
Uwe Kleine-König6506f2a2007-10-20 01:56:53 +0200762 * Counterpart to lock_hrtimer_base above:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800763 */
764static inline
765void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
766{
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100767 raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800768}
769
770/**
771 * hrtimer_forward - forward the timer expiry
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800772 * @timer: hrtimer to forward
Roman Zippel44f21472006-03-26 01:38:06 -0800773 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800774 * @interval: the interval to forward
775 *
776 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700777 * Returns the number of overruns.
Thomas Gleixner91e5a2172015-04-13 21:02:22 +0000778 *
779 * Can be safely called from the callback function of @timer. If
780 * called from other contexts @timer must neither be enqueued nor
781 * running the callback and the caller needs to take care of
782 * serialization.
783 *
784 * Note: This only updates the timer expiry value and does not requeue
785 * the timer.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800786 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800787u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800788{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800789 u64 orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800790 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800791
Arjan van de Vencc584b22008-09-01 15:02:30 -0700792 delta = ktime_sub(now, hrtimer_get_expires(timer));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800793
Thomas Gleixner2456e852016-12-25 11:38:40 +0100794 if (delta < 0)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800795 return 0;
796
Peter Zijlstra5de27552014-05-20 15:49:48 +0200797 if (WARN_ON(timer->state & HRTIMER_STATE_ENQUEUED))
798 return 0;
799
Thomas Gleixner2456e852016-12-25 11:38:40 +0100800 if (interval < hrtimer_resolution)
801 interval = hrtimer_resolution;
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100802
Thomas Gleixner2456e852016-12-25 11:38:40 +0100803 if (unlikely(delta >= interval)) {
Roman Zippeldf869b62006-03-26 01:38:11 -0800804 s64 incr = ktime_to_ns(interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800805
806 orun = ktime_divns(delta, incr);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700807 hrtimer_add_expires_ns(timer, incr * orun);
Thomas Gleixner2456e852016-12-25 11:38:40 +0100808 if (hrtimer_get_expires_tv64(timer) > now)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800809 return orun;
810 /*
811 * This (and the ktime_add() below) is the
812 * correction for exact:
813 */
814 orun++;
815 }
Arjan van de Vencc584b22008-09-01 15:02:30 -0700816 hrtimer_add_expires(timer, interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800817
818 return orun;
819}
Stas Sergeev6bdb6b62007-05-08 00:31:58 -0700820EXPORT_SYMBOL_GPL(hrtimer_forward);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800821
822/*
823 * enqueue_hrtimer - internal function to (re)start a timer
824 *
825 * The timer is inserted in expiry order. Insertion into the
826 * red black tree is O(log(n)). Must hold the base lock.
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100827 *
828 * Returns 1 when the new timer is the leftmost timer in the tree.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800829 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100830static int enqueue_hrtimer(struct hrtimer *timer,
831 struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800832{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800833 debug_activate(timer);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700834
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200835 base->cpu_base->active_bases |= 1 << base->index;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800836
Peter Zijlstra887d9dc2015-06-11 14:46:48 +0200837 timer->state = HRTIMER_STATE_ENQUEUED;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100838
Thomas Gleixnerb97f44c2015-04-14 21:08:47 +0000839 return timerqueue_add(&base->active, &timer->node);
Thomas Gleixner288867e2006-01-12 11:25:54 +0100840}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800841
842/*
843 * __remove_hrtimer - internal function to remove a timer
844 *
845 * Caller must hold the base lock.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800846 *
847 * High resolution timer mode reprograms the clock event device when the
848 * timer is the one which expires next. The caller can disable this by setting
849 * reprogram to zero. This is useful, when the context does a reprogramming
850 * anyway (e.g. timer interrupt)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800851 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800852static void __remove_hrtimer(struct hrtimer *timer,
Thomas Gleixner303e9672007-02-16 01:27:51 -0800853 struct hrtimer_clock_base *base,
Thomas Gleixner203cbf772016-01-14 16:54:46 +0000854 u8 newstate, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800855{
Thomas Gleixnere19ffe82015-04-14 21:08:39 +0000856 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
Thomas Gleixner203cbf772016-01-14 16:54:46 +0000857 u8 state = timer->state;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400858
Thomas Gleixner303e9672007-02-16 01:27:51 -0800859 timer->state = newstate;
Thomas Gleixner895bdfa2015-04-14 21:08:49 +0000860 if (!(state & HRTIMER_STATE_ENQUEUED))
861 return;
David Woodhouseed198cb2006-04-22 02:38:50 +0100862
Thomas Gleixnerb97f44c2015-04-14 21:08:47 +0000863 if (!timerqueue_del(&base->active, &timer->node))
Thomas Gleixnere19ffe82015-04-14 21:08:39 +0000864 cpu_base->active_bases &= ~(1 << base->index);
Viresh Kumard9f0acd2015-04-14 21:08:25 +0000865
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800866#ifdef CONFIG_HIGH_RES_TIMERS
Thomas Gleixner895bdfa2015-04-14 21:08:49 +0000867 /*
868 * Note: If reprogram is false we do not update
869 * cpu_base->next_timer. This happens when we remove the first
870 * timer on a remote cpu. No harm as we never dereference
871 * cpu_base->next_timer. So the worst thing what can happen is
872 * an superflous call to hrtimer_force_reprogram() on the
873 * remote cpu later on if the same timer gets enqueued again.
874 */
875 if (reprogram && timer == cpu_base->next_timer)
876 hrtimer_force_reprogram(cpu_base, 1);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800877#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800878}
879
880/*
881 * remove hrtimer, called with base lock held
882 */
883static inline int
Peter Zijlstra8edfb032015-06-11 14:46:45 +0200884remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base, bool restart)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800885{
Thomas Gleixner303e9672007-02-16 01:27:51 -0800886 if (hrtimer_is_queued(timer)) {
Thomas Gleixner203cbf772016-01-14 16:54:46 +0000887 u8 state = timer->state;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800888 int reprogram;
889
890 /*
891 * Remove the timer and force reprogramming when high
892 * resolution mode is active and the timer is on the current
893 * CPU. If we remove a timer on another CPU, reprogramming is
894 * skipped. The interrupt event on this CPU is fired and
895 * reprogramming happens in the interrupt handler. This is a
896 * rare case and less expensive than a smp call.
897 */
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800898 debug_deactivate(timer);
Christoph Lameterdc5df732014-08-17 12:30:26 -0500899 reprogram = base->cpu_base == this_cpu_ptr(&hrtimer_bases);
Peter Zijlstra8edfb032015-06-11 14:46:45 +0200900
Peter Zijlstra887d9dc2015-06-11 14:46:48 +0200901 if (!restart)
902 state = HRTIMER_STATE_INACTIVE;
903
Salman Qazif13d4f92010-10-12 07:25:19 -0700904 __remove_hrtimer(timer, base, state, reprogram);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800905 return 1;
906 }
907 return 0;
908}
909
Thomas Gleixner203cbf772016-01-14 16:54:46 +0000910static inline ktime_t hrtimer_update_lowres(struct hrtimer *timer, ktime_t tim,
911 const enum hrtimer_mode mode)
912{
913#ifdef CONFIG_TIME_LOW_RES
914 /*
915 * CONFIG_TIME_LOW_RES indicates that the system has no way to return
916 * granular time values. For relative timers we add hrtimer_resolution
917 * (i.e. one jiffie) to prevent short timeouts.
918 */
919 timer->is_rel = mode & HRTIMER_MODE_REL;
920 if (timer->is_rel)
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100921 tim = ktime_add_safe(tim, hrtimer_resolution);
Thomas Gleixner203cbf772016-01-14 16:54:46 +0000922#endif
923 return tim;
924}
925
Thomas Gleixner58f1f802015-04-14 21:09:08 +0000926/**
927 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
928 * @timer: the timer to be added
929 * @tim: expiry time
930 * @delta_ns: "slack" range for the timer
931 * @mode: expiry mode: absolute (HRTIMER_MODE_ABS) or
932 * relative (HRTIMER_MODE_REL)
Thomas Gleixner58f1f802015-04-14 21:09:08 +0000933 */
Thomas Gleixner61699e132015-04-14 21:09:23 +0000934void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
John Stultzda8b44d2016-03-17 14:20:51 -0700935 u64 delta_ns, const enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800936{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800937 struct hrtimer_clock_base *base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800938 unsigned long flags;
Thomas Gleixner61699e132015-04-14 21:09:23 +0000939 int leftmost;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800940
941 base = lock_hrtimer_base(timer, &flags);
942
943 /* Remove an active timer from the queue: */
Peter Zijlstra8edfb032015-06-11 14:46:45 +0200944 remove_hrtimer(timer, base, true);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800945
Thomas Gleixner203cbf772016-01-14 16:54:46 +0000946 if (mode & HRTIMER_MODE_REL)
Viresh Kumar84ea7fe2014-05-12 13:42:29 +0530947 tim = ktime_add_safe(tim, base->get_time());
Thomas Gleixner203cbf772016-01-14 16:54:46 +0000948
949 tim = hrtimer_update_lowres(timer, tim, mode);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700950
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700951 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800952
Viresh Kumar84ea7fe2014-05-12 13:42:29 +0530953 /* Switch the timer base, if necessary: */
954 new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
955
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100956 leftmost = enqueue_hrtimer(timer, new_base);
Thomas Gleixner61699e132015-04-14 21:09:23 +0000957 if (!leftmost)
958 goto unlock;
Viresh Kumar49a2a072014-06-23 13:39:37 +0530959
960 if (!hrtimer_is_hres_active(timer)) {
961 /*
962 * Kick to reschedule the next tick to handle the new timer
963 * on dynticks target.
964 */
Thomas Gleixnerae67bad2018-01-14 23:30:51 +0100965 if (is_timers_nohz_active())
Thomas Gleixner683be132015-05-26 22:50:35 +0000966 wake_up_nohz_cpu(new_base->cpu_base->cpu);
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +0000967 } else {
968 hrtimer_reprogram(timer, new_base);
Leonid Shatzb22affe2013-02-04 14:33:37 +0200969 }
Thomas Gleixner61699e132015-04-14 21:09:23 +0000970unlock:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800971 unlock_hrtimer_base(timer, &flags);
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100972}
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700973EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
974
975/**
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800976 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800977 * @timer: hrtimer to stop
978 *
979 * Returns:
980 * 0 when the timer was not active
981 * 1 when the timer was active
Masanari Iida0ba42a52017-03-07 20:48:02 +0900982 * -1 when the timer is currently executing the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -0700983 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800984 */
985int hrtimer_try_to_cancel(struct hrtimer *timer)
986{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800987 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800988 unsigned long flags;
989 int ret = -1;
990
Thomas Gleixner19d9f422015-04-14 21:09:25 +0000991 /*
992 * Check lockless first. If the timer is not active (neither
993 * enqueued nor running the callback, nothing to do here. The
994 * base lock does not serialize against a concurrent enqueue,
995 * so we can avoid taking it.
996 */
997 if (!hrtimer_active(timer))
998 return 0;
999
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001000 base = lock_hrtimer_base(timer, &flags);
1001
Thomas Gleixner303e9672007-02-16 01:27:51 -08001002 if (!hrtimer_callback_running(timer))
Peter Zijlstra8edfb032015-06-11 14:46:45 +02001003 ret = remove_hrtimer(timer, base, false);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001004
1005 unlock_hrtimer_base(timer, &flags);
1006
1007 return ret;
1008
1009}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001010EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001011
1012/**
1013 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001014 * @timer: the timer to be cancelled
1015 *
1016 * Returns:
1017 * 0 when the timer was not active
1018 * 1 when the timer was active
1019 */
1020int hrtimer_cancel(struct hrtimer *timer)
1021{
1022 for (;;) {
1023 int ret = hrtimer_try_to_cancel(timer);
1024
1025 if (ret >= 0)
1026 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -07001027 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001028 }
1029}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001030EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001031
1032/**
1033 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001034 * @timer: the timer to read
Thomas Gleixner203cbf772016-01-14 16:54:46 +00001035 * @adjust: adjust relative timers when CONFIG_TIME_LOW_RES=y
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001036 */
Thomas Gleixner203cbf772016-01-14 16:54:46 +00001037ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001038{
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001039 unsigned long flags;
1040 ktime_t rem;
1041
Andi Kleenb3bd3de2010-08-10 14:17:51 -07001042 lock_hrtimer_base(timer, &flags);
Thomas Gleixner203cbf772016-01-14 16:54:46 +00001043 if (IS_ENABLED(CONFIG_TIME_LOW_RES) && adjust)
1044 rem = hrtimer_expires_remaining_adjusted(timer);
1045 else
1046 rem = hrtimer_expires_remaining(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001047 unlock_hrtimer_base(timer, &flags);
1048
1049 return rem;
1050}
Thomas Gleixner203cbf772016-01-14 16:54:46 +00001051EXPORT_SYMBOL_GPL(__hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001052
Frederic Weisbecker3451d022011-08-10 23:21:01 +02001053#ifdef CONFIG_NO_HZ_COMMON
Tony Lindgren69239742006-03-06 15:42:45 -08001054/**
1055 * hrtimer_get_next_event - get the time until next expiry event
1056 *
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +00001057 * Returns the next expiry time or KTIME_MAX if no timer is pending.
Tony Lindgren69239742006-03-06 15:42:45 -08001058 */
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +00001059u64 hrtimer_get_next_event(void)
Tony Lindgren69239742006-03-06 15:42:45 -08001060{
Christoph Lameterdc5df732014-08-17 12:30:26 -05001061 struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +00001062 u64 expires = KTIME_MAX;
Tony Lindgren69239742006-03-06 15:42:45 -08001063 unsigned long flags;
Tony Lindgren69239742006-03-06 15:42:45 -08001064
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001065 raw_spin_lock_irqsave(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001066
Thomas Gleixnere19ffe82015-04-14 21:08:39 +00001067 if (!__hrtimer_hres_active(cpu_base))
Thomas Gleixner2456e852016-12-25 11:38:40 +01001068 expires = __hrtimer_get_next_event(cpu_base);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001069
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001070 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001071
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +00001072 return expires;
Tony Lindgren69239742006-03-06 15:42:45 -08001073}
1074#endif
1075
Marc Zyngier336a9cd2016-01-15 17:41:09 +00001076static inline int hrtimer_clockid_to_base(clockid_t clock_id)
1077{
1078 if (likely(clock_id < MAX_CLOCKS)) {
1079 int base = hrtimer_clock_to_base_table[clock_id];
1080
1081 if (likely(base != HRTIMER_MAX_CLOCK_BASES))
1082 return base;
1083 }
1084 WARN(1, "Invalid clockid %d. Using MONOTONIC\n", clock_id);
1085 return HRTIMER_BASE_MONOTONIC;
1086}
1087
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001088static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1089 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001090{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001091 struct hrtimer_cpu_base *cpu_base;
John Stultze06383d2010-12-14 19:37:07 -08001092 int base;
George Anzinger7978672c2006-02-01 03:05:11 -08001093
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001094 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger7978672c2006-02-01 03:05:11 -08001095
Christoph Lameter22127e92014-08-17 12:30:25 -05001096 cpu_base = raw_cpu_ptr(&hrtimer_bases);
George Anzinger7978672c2006-02-01 03:05:11 -08001097
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001098 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger7978672c2006-02-01 03:05:11 -08001099 clock_id = CLOCK_MONOTONIC;
1100
John Stultze06383d2010-12-14 19:37:07 -08001101 base = hrtimer_clockid_to_base(clock_id);
1102 timer->base = &cpu_base->clock_base[base];
John Stultz998adc32010-09-20 19:19:17 -07001103 timerqueue_init(&timer->node);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001104}
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001105
1106/**
1107 * hrtimer_init - initialize a timer to the given clock
1108 * @timer: the timer to be initialized
1109 * @clock_id: the clock to be used
1110 * @mode: timer mode abs/rel
1111 */
1112void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1113 enum hrtimer_mode mode)
1114{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001115 debug_init(timer, clock_id, mode);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001116 __hrtimer_init(timer, clock_id, mode);
1117}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001118EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001119
Peter Zijlstra887d9dc2015-06-11 14:46:48 +02001120/*
1121 * A timer is active, when it is enqueued into the rbtree or the
1122 * callback function is running or it's in the state of being migrated
1123 * to another cpu.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001124 *
Peter Zijlstra887d9dc2015-06-11 14:46:48 +02001125 * It is important for this function to not return a false negative.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001126 */
Peter Zijlstra887d9dc2015-06-11 14:46:48 +02001127bool hrtimer_active(const struct hrtimer *timer)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001128{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001129 struct hrtimer_cpu_base *cpu_base;
Peter Zijlstra887d9dc2015-06-11 14:46:48 +02001130 unsigned int seq;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001131
Peter Zijlstra887d9dc2015-06-11 14:46:48 +02001132 do {
1133 cpu_base = READ_ONCE(timer->base->cpu_base);
1134 seq = raw_read_seqcount_begin(&cpu_base->seq);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001135
Peter Zijlstra887d9dc2015-06-11 14:46:48 +02001136 if (timer->state != HRTIMER_STATE_INACTIVE ||
1137 cpu_base->running == timer)
1138 return true;
1139
1140 } while (read_seqcount_retry(&cpu_base->seq, seq) ||
1141 cpu_base != READ_ONCE(timer->base->cpu_base));
1142
1143 return false;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001144}
Peter Zijlstra887d9dc2015-06-11 14:46:48 +02001145EXPORT_SYMBOL_GPL(hrtimer_active);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001146
Peter Zijlstra887d9dc2015-06-11 14:46:48 +02001147/*
1148 * The write_seqcount_barrier()s in __run_hrtimer() split the thing into 3
1149 * distinct sections:
1150 *
1151 * - queued: the timer is queued
1152 * - callback: the timer is being ran
1153 * - post: the timer is inactive or (re)queued
1154 *
1155 * On the read side we ensure we observe timer->state and cpu_base->running
1156 * from the same section, if anything changed while we looked at it, we retry.
1157 * This includes timer->base changing because sequence numbers alone are
1158 * insufficient for that.
1159 *
1160 * The sequence numbers are required because otherwise we could still observe
1161 * a false negative if the read side got smeared over multiple consequtive
1162 * __run_hrtimer() invocations.
1163 */
1164
Thomas Gleixner21d6d522015-04-14 21:08:35 +00001165static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base,
1166 struct hrtimer_clock_base *base,
1167 struct hrtimer *timer, ktime_t *now)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001168{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001169 enum hrtimer_restart (*fn)(struct hrtimer *);
1170 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001171
Peter Zijlstra887d9dc2015-06-11 14:46:48 +02001172 lockdep_assert_held(&cpu_base->lock);
Peter Zijlstraca109492008-11-25 12:43:51 +01001173
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001174 debug_deactivate(timer);
Peter Zijlstra887d9dc2015-06-11 14:46:48 +02001175 cpu_base->running = timer;
1176
1177 /*
1178 * Separate the ->running assignment from the ->state assignment.
1179 *
1180 * As with a regular write barrier, this ensures the read side in
1181 * hrtimer_active() cannot observe cpu_base->running == NULL &&
1182 * timer->state == INACTIVE.
1183 */
1184 raw_write_seqcount_barrier(&cpu_base->seq);
1185
1186 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 0);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001187 fn = timer->function;
Peter Zijlstraca109492008-11-25 12:43:51 +01001188
1189 /*
Thomas Gleixner203cbf772016-01-14 16:54:46 +00001190 * Clear the 'is relative' flag for the TIME_LOW_RES case. If the
1191 * timer is restarted with a period then it becomes an absolute
1192 * timer. If its not restarted it does not matter.
1193 */
1194 if (IS_ENABLED(CONFIG_TIME_LOW_RES))
1195 timer->is_rel = false;
1196
1197 /*
Thomas Gleixnerd05ca132017-12-21 11:41:31 +01001198 * The timer is marked as running in the CPU base, so it is
1199 * protected against migration to a different CPU even if the lock
1200 * is dropped.
Peter Zijlstraca109492008-11-25 12:43:51 +01001201 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001202 raw_spin_unlock(&cpu_base->lock);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001203 trace_hrtimer_expire_entry(timer, now);
Peter Zijlstraca109492008-11-25 12:43:51 +01001204 restart = fn(timer);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001205 trace_hrtimer_expire_exit(timer);
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001206 raw_spin_lock(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001207
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001208 /*
Peter Zijlstra887d9dc2015-06-11 14:46:48 +02001209 * Note: We clear the running state after enqueue_hrtimer and
Pratyush Patelb4d90e92016-06-23 20:50:37 +02001210 * we do not reprogram the event hardware. Happens either in
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001211 * hrtimer_start_range_ns() or in hrtimer_interrupt()
Peter Zijlstra5de27552014-05-20 15:49:48 +02001212 *
1213 * Note: Because we dropped the cpu_base->lock above,
1214 * hrtimer_start_range_ns() can have popped in and enqueued the timer
1215 * for us already.
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001216 */
Peter Zijlstra5de27552014-05-20 15:49:48 +02001217 if (restart != HRTIMER_NORESTART &&
1218 !(timer->state & HRTIMER_STATE_ENQUEUED))
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001219 enqueue_hrtimer(timer, base);
Salman Qazif13d4f92010-10-12 07:25:19 -07001220
Peter Zijlstra887d9dc2015-06-11 14:46:48 +02001221 /*
1222 * Separate the ->running assignment from the ->state assignment.
1223 *
1224 * As with a regular write barrier, this ensures the read side in
1225 * hrtimer_active() cannot observe cpu_base->running == NULL &&
1226 * timer->state == INACTIVE.
1227 */
1228 raw_write_seqcount_barrier(&cpu_base->seq);
Salman Qazif13d4f92010-10-12 07:25:19 -07001229
Peter Zijlstra887d9dc2015-06-11 14:46:48 +02001230 WARN_ON_ONCE(cpu_base->running != timer);
1231 cpu_base->running = NULL;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001232}
1233
Thomas Gleixner21d6d522015-04-14 21:08:35 +00001234static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now)
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001235{
Thomas Gleixner34aee882015-04-14 21:08:41 +00001236 struct hrtimer_clock_base *base = cpu_base->clock_base;
1237 unsigned int active = cpu_base->active_bases;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001238
Thomas Gleixner34aee882015-04-14 21:08:41 +00001239 for (; active; base++, active >>= 1) {
John Stultz998adc32010-09-20 19:19:17 -07001240 struct timerqueue_node *node;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001241 ktime_t basenow;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001242
Thomas Gleixner34aee882015-04-14 21:08:41 +00001243 if (!(active & 0x01))
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001244 continue;
1245
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001246 basenow = ktime_add(now, base->offset);
1247
John Stultz998adc32010-09-20 19:19:17 -07001248 while ((node = timerqueue_getnext(&base->active))) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001249 struct hrtimer *timer;
1250
John Stultz998adc32010-09-20 19:19:17 -07001251 timer = container_of(node, struct hrtimer, node);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001252
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001253 /*
1254 * The immediate goal for using the softexpires is
1255 * minimizing wakeups, not running timers at the
1256 * earliest interrupt after their soft expiration.
1257 * This allows us to avoid using a Priority Search
1258 * Tree, which can answer a stabbing querry for
1259 * overlapping intervals and instead use the simple
1260 * BST we already have.
1261 * We don't add extra wakeups by delaying timers that
1262 * are right-of a not yet expired timer, because that
1263 * timer will have to trigger a wakeup anyway.
1264 */
Thomas Gleixner2456e852016-12-25 11:38:40 +01001265 if (basenow < hrtimer_get_softexpires_tv64(timer))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001266 break;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001267
Thomas Gleixner21d6d522015-04-14 21:08:35 +00001268 __run_hrtimer(cpu_base, base, timer, &basenow);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001269 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001270 }
Thomas Gleixner21d6d522015-04-14 21:08:35 +00001271}
1272
1273#ifdef CONFIG_HIGH_RES_TIMERS
1274
1275/*
1276 * High resolution timer interrupt
1277 * Called with interrupts disabled
1278 */
1279void hrtimer_interrupt(struct clock_event_device *dev)
1280{
1281 struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
1282 ktime_t expires_next, now, entry_time, delta;
1283 int retries = 0;
1284
1285 BUG_ON(!cpu_base->hres_active);
1286 cpu_base->nr_events++;
Thomas Gleixner2456e852016-12-25 11:38:40 +01001287 dev->next_event = KTIME_MAX;
Thomas Gleixner21d6d522015-04-14 21:08:35 +00001288
1289 raw_spin_lock(&cpu_base->lock);
1290 entry_time = now = hrtimer_update_base(cpu_base);
1291retry:
1292 cpu_base->in_hrtirq = 1;
1293 /*
1294 * We set expires_next to KTIME_MAX here with cpu_base->lock
1295 * held to prevent that a timer is enqueued in our queue via
1296 * the migration code. This does not affect enqueueing of
1297 * timers which run their callback and need to be requeued on
1298 * this CPU.
1299 */
Thomas Gleixner2456e852016-12-25 11:38:40 +01001300 cpu_base->expires_next = KTIME_MAX;
Thomas Gleixner21d6d522015-04-14 21:08:35 +00001301
1302 __hrtimer_run_queues(cpu_base, now);
1303
Thomas Gleixner9bc749192015-01-20 21:24:10 +01001304 /* Reevaluate the clock bases for the next expiry */
1305 expires_next = __hrtimer_get_next_event(cpu_base);
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001306 /*
1307 * Store the new expiry value so the migration code can verify
1308 * against it.
1309 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001310 cpu_base->expires_next = expires_next;
Thomas Gleixner9bc749192015-01-20 21:24:10 +01001311 cpu_base->in_hrtirq = 0;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001312 raw_spin_unlock(&cpu_base->lock);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001313
1314 /* Reprogramming necessary ? */
Viresh Kumard25408752015-04-03 09:04:05 +05301315 if (!tick_program_event(expires_next, 0)) {
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001316 cpu_base->hang_detected = 0;
1317 return;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001318 }
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001319
1320 /*
1321 * The next timer was already expired due to:
1322 * - tracing
1323 * - long lasting callbacks
1324 * - being scheduled away when running in a VM
1325 *
1326 * We need to prevent that we loop forever in the hrtimer
1327 * interrupt routine. We give it 3 attempts to avoid
1328 * overreacting on some spurious event.
John Stultz5baefd62012-07-10 18:43:25 -04001329 *
1330 * Acquire base lock for updating the offsets and retrieving
1331 * the current time.
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001332 */
Thomas Gleixner196951e2012-07-10 18:43:23 -04001333 raw_spin_lock(&cpu_base->lock);
John Stultz5baefd62012-07-10 18:43:25 -04001334 now = hrtimer_update_base(cpu_base);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001335 cpu_base->nr_retries++;
1336 if (++retries < 3)
1337 goto retry;
1338 /*
1339 * Give the system a chance to do something else than looping
1340 * here. We stored the entry time, so we know exactly how long
1341 * we spent here. We schedule the next event this amount of
1342 * time away.
1343 */
1344 cpu_base->nr_hangs++;
1345 cpu_base->hang_detected = 1;
Thomas Gleixner196951e2012-07-10 18:43:23 -04001346 raw_spin_unlock(&cpu_base->lock);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001347 delta = ktime_sub(now, entry_time);
Thomas Gleixner2456e852016-12-25 11:38:40 +01001348 if ((unsigned int)delta > cpu_base->max_hang_time)
1349 cpu_base->max_hang_time = (unsigned int) delta;
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001350 /*
1351 * Limit it to a sensible value as we enforce a longer
1352 * delay. Give the CPU at least 100ms to catch up.
1353 */
Thomas Gleixner2456e852016-12-25 11:38:40 +01001354 if (delta > 100 * NSEC_PER_MSEC)
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001355 expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
1356 else
1357 expires_next = ktime_add(now, delta);
1358 tick_program_event(expires_next, 1);
1359 printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
1360 ktime_to_ns(delta));
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001361}
1362
Stephen Boyd016da202017-03-16 18:08:13 -07001363/* called with interrupts disabled */
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +00001364static inline void __hrtimer_peek_ahead_timers(void)
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001365{
1366 struct tick_device *td;
1367
1368 if (!hrtimer_hres_active())
1369 return;
1370
Christoph Lameter22127e92014-08-17 12:30:25 -05001371 td = this_cpu_ptr(&tick_cpu_device);
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001372 if (td && td->evtdev)
1373 hrtimer_interrupt(td->evtdev);
1374}
1375
Ingo Molnar82c5b7b2009-01-05 14:11:10 +01001376#else /* CONFIG_HIGH_RES_TIMERS */
1377
1378static inline void __hrtimer_peek_ahead_timers(void) { }
1379
1380#endif /* !CONFIG_HIGH_RES_TIMERS */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001381
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001382/*
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +00001383 * Called from run_local_timers in hardirq context every jiffy
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001384 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001385void hrtimer_run_queues(void)
1386{
Christoph Lameterdc5df732014-08-17 12:30:26 -05001387 struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
Thomas Gleixner21d6d522015-04-14 21:08:35 +00001388 ktime_t now;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001389
Thomas Gleixnere19ffe82015-04-14 21:08:39 +00001390 if (__hrtimer_hres_active(cpu_base))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001391 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001392
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +00001393 /*
1394 * This _is_ ugly: We have to check periodically, whether we
1395 * can switch to highres and / or nohz mode. The clocksource
1396 * switch happens with xtime_lock held. Notification from
1397 * there only sets the check bit in the tick_oneshot code,
1398 * otherwise we might deadlock vs. xtime_lock.
1399 */
1400 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled())) {
1401 hrtimer_switch_to_hres();
1402 return;
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001403 }
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +00001404
Thomas Gleixner21d6d522015-04-14 21:08:35 +00001405 raw_spin_lock(&cpu_base->lock);
1406 now = hrtimer_update_base(cpu_base);
1407 __hrtimer_run_queues(cpu_base, now);
1408 raw_spin_unlock(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001409}
1410
1411/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001412 * Sleep related functions:
1413 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001414static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001415{
1416 struct hrtimer_sleeper *t =
1417 container_of(timer, struct hrtimer_sleeper, timer);
1418 struct task_struct *task = t->task;
1419
1420 t->task = NULL;
1421 if (task)
1422 wake_up_process(task);
1423
1424 return HRTIMER_NORESTART;
1425}
1426
Ingo Molnar36c8b582006-07-03 00:25:41 -07001427void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001428{
1429 sl->timer.function = hrtimer_wakeup;
1430 sl->task = task;
1431}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07001432EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
Thomas Gleixner00362e32006-03-31 02:31:17 -08001433
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001434int nanosleep_copyout(struct restart_block *restart, struct timespec64 *ts)
Al Viroce41aaf2017-06-07 09:42:32 +01001435{
1436 switch(restart->nanosleep.type) {
1437#ifdef CONFIG_COMPAT
1438 case TT_COMPAT:
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001439 if (compat_put_timespec64(ts, restart->nanosleep.compat_rmtp))
Al Viroce41aaf2017-06-07 09:42:32 +01001440 return -EFAULT;
1441 break;
1442#endif
1443 case TT_NATIVE:
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001444 if (put_timespec64(ts, restart->nanosleep.rmtp))
Al Viroce41aaf2017-06-07 09:42:32 +01001445 return -EFAULT;
1446 break;
1447 default:
1448 BUG();
1449 }
1450 return -ERESTART_RESTARTBLOCK;
1451}
1452
Thomas Gleixner669d7862006-03-31 02:31:19 -08001453static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001454{
Al Viroedbeda42017-06-07 09:42:31 +01001455 struct restart_block *restart;
1456
Thomas Gleixner669d7862006-03-31 02:31:19 -08001457 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001458
Roman Zippel432569b2006-03-26 01:38:08 -08001459 do {
1460 set_current_state(TASK_INTERRUPTIBLE);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001461 hrtimer_start_expires(&t->timer, mode);
Roman Zippel432569b2006-03-26 01:38:08 -08001462
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001463 if (likely(t->task))
Colin Crossb0f8c442013-05-06 23:50:19 +00001464 freezable_schedule();
Roman Zippel432569b2006-03-26 01:38:08 -08001465
Thomas Gleixner669d7862006-03-31 02:31:19 -08001466 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001467 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -08001468
Thomas Gleixner669d7862006-03-31 02:31:19 -08001469 } while (t->task && !signal_pending(current));
1470
Peter Zijlstra3588a082008-02-01 17:45:13 +01001471 __set_current_state(TASK_RUNNING);
1472
Al Viroa7602682017-06-07 09:42:29 +01001473 if (!t->task)
Oleg Nesterov080344b2008-02-01 17:29:05 +03001474 return 0;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001475
Al Viroedbeda42017-06-07 09:42:31 +01001476 restart = &current->restart_block;
1477 if (restart->nanosleep.type != TT_NONE) {
Al Viroa7602682017-06-07 09:42:29 +01001478 ktime_t rem = hrtimer_expires_remaining(&t->timer);
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001479 struct timespec64 rmt;
Al Viroedbeda42017-06-07 09:42:31 +01001480
Al Viroa7602682017-06-07 09:42:29 +01001481 if (rem <= 0)
1482 return 0;
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001483 rmt = ktime_to_timespec64(rem);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001484
Al Viroce41aaf2017-06-07 09:42:32 +01001485 return nanosleep_copyout(restart, &rmt);
Al Viroa7602682017-06-07 09:42:29 +01001486 }
1487 return -ERESTART_RESTARTBLOCK;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001488}
1489
Al Virofb923c42017-06-07 09:42:33 +01001490static long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001491{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001492 struct hrtimer_sleeper t;
Al Viroa7602682017-06-07 09:42:29 +01001493 int ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001494
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001495 hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001496 HRTIMER_MODE_ABS);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001497 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001498
Al Viroa7602682017-06-07 09:42:29 +01001499 ret = do_nanosleep(&t, HRTIMER_MODE_ABS);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001500 destroy_hrtimer_on_stack(&t.timer);
1501 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001502}
1503
Thomas Gleixner938e7cf2017-06-13 23:34:33 +02001504long hrtimer_nanosleep(const struct timespec64 *rqtp,
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001505 const enum hrtimer_mode mode, const clockid_t clockid)
1506{
Al Viroa7602682017-06-07 09:42:29 +01001507 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -08001508 struct hrtimer_sleeper t;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001509 int ret = 0;
John Stultzda8b44d2016-03-17 14:20:51 -07001510 u64 slack;
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001511
1512 slack = current->timer_slack_ns;
Dario Faggioliaab03e02013-11-28 11:14:43 +01001513 if (dl_task(current) || rt_task(current))
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001514 slack = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001515
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001516 hrtimer_init_on_stack(&t.timer, clockid, mode);
Deepa Dinamaniad196382017-03-26 12:04:18 -07001517 hrtimer_set_expires_range_ns(&t.timer, timespec64_to_ktime(*rqtp), slack);
Al Viroa7602682017-06-07 09:42:29 +01001518 ret = do_nanosleep(&t, mode);
1519 if (ret != -ERESTART_RESTARTBLOCK)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001520 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001521
George Anzinger7978672c2006-02-01 03:05:11 -08001522 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001523 if (mode == HRTIMER_MODE_ABS) {
1524 ret = -ERESTARTNOHAND;
1525 goto out;
1526 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001527
Al Viroa7602682017-06-07 09:42:29 +01001528 restart = &current->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -07001529 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001530 restart->nanosleep.clockid = t.timer.base->clockid;
Arjan van de Vencc584b22008-09-01 15:02:30 -07001531 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001532out:
1533 destroy_hrtimer_on_stack(&t.timer);
1534 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001535}
1536
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001537SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1538 struct timespec __user *, rmtp)
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001539{
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001540 struct timespec64 tu;
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001541
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001542 if (get_timespec64(&tu, rqtp))
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001543 return -EFAULT;
1544
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001545 if (!timespec64_valid(&tu))
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001546 return -EINVAL;
1547
Al Viroedbeda42017-06-07 09:42:31 +01001548 current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
Al Viro192a82f2017-06-07 09:42:28 +01001549 current->restart_block.nanosleep.rmtp = rmtp;
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001550 return hrtimer_nanosleep(&tu, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001551}
1552
Al Viroedbeda42017-06-07 09:42:31 +01001553#ifdef CONFIG_COMPAT
1554
1555COMPAT_SYSCALL_DEFINE2(nanosleep, struct compat_timespec __user *, rqtp,
1556 struct compat_timespec __user *, rmtp)
1557{
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001558 struct timespec64 tu;
Al Viroedbeda42017-06-07 09:42:31 +01001559
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001560 if (compat_get_timespec64(&tu, rqtp))
Al Viroedbeda42017-06-07 09:42:31 +01001561 return -EFAULT;
1562
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001563 if (!timespec64_valid(&tu))
Al Viroedbeda42017-06-07 09:42:31 +01001564 return -EINVAL;
1565
1566 current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
1567 current->restart_block.nanosleep.compat_rmtp = rmtp;
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001568 return hrtimer_nanosleep(&tu, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Al Viroedbeda42017-06-07 09:42:31 +01001569}
1570#endif
1571
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001572/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001573 * Functions related to boot-time initialization:
1574 */
Thomas Gleixner27590dc2016-07-15 10:41:04 +02001575int hrtimers_prepare_cpu(unsigned int cpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001576{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001577 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001578 int i;
1579
John Stultz998adc32010-09-20 19:19:17 -07001580 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001581 cpu_base->clock_base[i].cpu_base = cpu_base;
John Stultz998adc32010-09-20 19:19:17 -07001582 timerqueue_init_head(&cpu_base->clock_base[i].active);
1583 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001584
Viresh Kumarcddd0242014-06-22 01:29:15 +02001585 cpu_base->cpu = cpu;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001586 hrtimer_init_hres(cpu_base);
Thomas Gleixner27590dc2016-07-15 10:41:04 +02001587 return 0;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001588}
1589
1590#ifdef CONFIG_HOTPLUG_CPU
1591
Peter Zijlstraca109492008-11-25 12:43:51 +01001592static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
Peter Zijlstra37810652008-12-04 11:17:10 +01001593 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001594{
1595 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001596 struct timerqueue_node *node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001597
John Stultz998adc32010-09-20 19:19:17 -07001598 while ((node = timerqueue_getnext(&old_base->active))) {
1599 timer = container_of(node, struct hrtimer, node);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001600 BUG_ON(hrtimer_callback_running(timer));
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001601 debug_deactivate(timer);
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001602
1603 /*
Oleg Nesterovc04dca02015-06-11 14:46:44 +02001604 * Mark it as ENQUEUED not INACTIVE otherwise the
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001605 * timer could be seen as !active and just vanish away
1606 * under us on another CPU
1607 */
Oleg Nesterovc04dca02015-06-11 14:46:44 +02001608 __remove_hrtimer(timer, old_base, HRTIMER_STATE_ENQUEUED, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001609 timer->base = new_base;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001610 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001611 * Enqueue the timers on the new cpu. This does not
1612 * reprogram the event device in case the timer
1613 * expires before the earliest on this CPU, but we run
1614 * hrtimer_interrupt after we migrated everything to
1615 * sort out already expired timers and reprogram the
1616 * event device.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001617 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001618 enqueue_hrtimer(timer, new_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001619 }
1620}
1621
Thomas Gleixner27590dc2016-07-15 10:41:04 +02001622int hrtimers_dead_cpu(unsigned int scpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001623{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001624 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001625 int i;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001626
Peter Zijlstra37810652008-12-04 11:17:10 +01001627 BUG_ON(cpu_online(scpu));
Peter Zijlstra37810652008-12-04 11:17:10 +01001628 tick_cancel_sched_timer(scpu);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001629
1630 local_irq_disable();
1631 old_base = &per_cpu(hrtimer_bases, scpu);
Christoph Lameterdc5df732014-08-17 12:30:26 -05001632 new_base = this_cpu_ptr(&hrtimer_bases);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001633 /*
1634 * The caller is globally serialized and nobody else
1635 * takes two locks at once, deadlock is not possible.
1636 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001637 raw_spin_lock(&new_base->lock);
1638 raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001639
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001640 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Peter Zijlstraca109492008-11-25 12:43:51 +01001641 migrate_hrtimer_list(&old_base->clock_base[i],
Peter Zijlstra37810652008-12-04 11:17:10 +01001642 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001643 }
1644
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001645 raw_spin_unlock(&old_base->lock);
1646 raw_spin_unlock(&new_base->lock);
Peter Zijlstra37810652008-12-04 11:17:10 +01001647
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001648 /* Check, if we got expired work to do */
1649 __hrtimer_peek_ahead_timers();
1650 local_irq_enable();
Thomas Gleixner27590dc2016-07-15 10:41:04 +02001651 return 0;
Peter Zijlstra37810652008-12-04 11:17:10 +01001652}
1653
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001654#endif /* CONFIG_HOTPLUG_CPU */
1655
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001656void __init hrtimers_init(void)
1657{
Thomas Gleixner27590dc2016-07-15 10:41:04 +02001658 hrtimers_prepare_cpu(smp_processor_id());
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001659}
1660
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001661/**
Carsten Emde351b3f72010-04-02 22:40:19 +02001662 * schedule_hrtimeout_range_clock - sleep until timeout
1663 * @expires: timeout value (ktime_t)
1664 * @delta: slack in expires timeout (ktime_t)
Anna-Maria Gleixner90777712017-12-21 11:41:33 +01001665 * @mode: timer mode
1666 * @clock_id: timer clock to be used
Carsten Emde351b3f72010-04-02 22:40:19 +02001667 */
1668int __sched
John Stultzda8b44d2016-03-17 14:20:51 -07001669schedule_hrtimeout_range_clock(ktime_t *expires, u64 delta,
Anna-Maria Gleixner90777712017-12-21 11:41:33 +01001670 const enum hrtimer_mode mode, clockid_t clock_id)
Carsten Emde351b3f72010-04-02 22:40:19 +02001671{
1672 struct hrtimer_sleeper t;
1673
1674 /*
1675 * Optimize when a zero timeout value is given. It does not
1676 * matter whether this is an absolute or a relative time.
1677 */
Thomas Gleixner2456e852016-12-25 11:38:40 +01001678 if (expires && *expires == 0) {
Carsten Emde351b3f72010-04-02 22:40:19 +02001679 __set_current_state(TASK_RUNNING);
1680 return 0;
1681 }
1682
1683 /*
Namhyung Kim43b21012010-12-22 19:01:47 +01001684 * A NULL parameter means "infinite"
Carsten Emde351b3f72010-04-02 22:40:19 +02001685 */
1686 if (!expires) {
1687 schedule();
Carsten Emde351b3f72010-04-02 22:40:19 +02001688 return -EINTR;
1689 }
1690
Anna-Maria Gleixner90777712017-12-21 11:41:33 +01001691 hrtimer_init_on_stack(&t.timer, clock_id, mode);
Carsten Emde351b3f72010-04-02 22:40:19 +02001692 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1693
1694 hrtimer_init_sleeper(&t, current);
1695
1696 hrtimer_start_expires(&t.timer, mode);
Carsten Emde351b3f72010-04-02 22:40:19 +02001697
1698 if (likely(t.task))
1699 schedule();
1700
1701 hrtimer_cancel(&t.timer);
1702 destroy_hrtimer_on_stack(&t.timer);
1703
1704 __set_current_state(TASK_RUNNING);
1705
1706 return !t.task ? 0 : -EINTR;
1707}
1708
1709/**
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001710 * schedule_hrtimeout_range - sleep until timeout
1711 * @expires: timeout value (ktime_t)
1712 * @delta: slack in expires timeout (ktime_t)
Anna-Maria Gleixner90777712017-12-21 11:41:33 +01001713 * @mode: timer mode
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001714 *
1715 * Make the current task sleep until the given expiry time has
1716 * elapsed. The routine will return immediately unless
1717 * the current task state has been set (see set_current_state()).
1718 *
1719 * The @delta argument gives the kernel the freedom to schedule the
1720 * actual wakeup to a time that is both power and performance friendly.
1721 * The kernel give the normal best effort behavior for "@expires+@delta",
1722 * but may decide to fire the timer earlier, but no earlier than @expires.
1723 *
1724 * You can set the task state as follows -
1725 *
1726 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
Douglas Anderson4b7e9cf2016-10-21 08:58:51 -07001727 * pass before the routine returns unless the current task is explicitly
1728 * woken up, (e.g. by wake_up_process()).
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001729 *
1730 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
Douglas Anderson4b7e9cf2016-10-21 08:58:51 -07001731 * delivered to the current task or the current task is explicitly woken
1732 * up.
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001733 *
1734 * The current task state is guaranteed to be TASK_RUNNING when this
1735 * routine returns.
1736 *
Douglas Anderson4b7e9cf2016-10-21 08:58:51 -07001737 * Returns 0 when the timer has expired. If the task was woken before the
1738 * timer expired by a signal (only possible in state TASK_INTERRUPTIBLE) or
1739 * by an explicit wakeup, it returns -EINTR.
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001740 */
John Stultzda8b44d2016-03-17 14:20:51 -07001741int __sched schedule_hrtimeout_range(ktime_t *expires, u64 delta,
Carsten Emde351b3f72010-04-02 22:40:19 +02001742 const enum hrtimer_mode mode)
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001743{
Carsten Emde351b3f72010-04-02 22:40:19 +02001744 return schedule_hrtimeout_range_clock(expires, delta, mode,
1745 CLOCK_MONOTONIC);
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001746}
1747EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1748
1749/**
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001750 * schedule_hrtimeout - sleep until timeout
1751 * @expires: timeout value (ktime_t)
Anna-Maria Gleixner90777712017-12-21 11:41:33 +01001752 * @mode: timer mode
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001753 *
1754 * Make the current task sleep until the given expiry time has
1755 * elapsed. The routine will return immediately unless
1756 * the current task state has been set (see set_current_state()).
1757 *
1758 * You can set the task state as follows -
1759 *
1760 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
Douglas Anderson4b7e9cf2016-10-21 08:58:51 -07001761 * pass before the routine returns unless the current task is explicitly
1762 * woken up, (e.g. by wake_up_process()).
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001763 *
1764 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
Douglas Anderson4b7e9cf2016-10-21 08:58:51 -07001765 * delivered to the current task or the current task is explicitly woken
1766 * up.
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001767 *
1768 * The current task state is guaranteed to be TASK_RUNNING when this
1769 * routine returns.
1770 *
Douglas Anderson4b7e9cf2016-10-21 08:58:51 -07001771 * Returns 0 when the timer has expired. If the task was woken before the
1772 * timer expired by a signal (only possible in state TASK_INTERRUPTIBLE) or
1773 * by an explicit wakeup, it returns -EINTR.
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001774 */
1775int __sched schedule_hrtimeout(ktime_t *expires,
1776 const enum hrtimer_mode mode)
1777{
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001778 return schedule_hrtimeout_range(expires, 0, mode);
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001779}
1780EXPORT_SYMBOL_GPL(schedule_hrtimeout);