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