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