xref: /linux/kernel/time/hrtimer.c (revision 546b928da0427b0d6c663cbb992bd7bfa9ac7971)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Copyright(C) 2005-2006, Linutronix GmbH, Thomas Gleixner <tglx@kernel.org>
4  *  Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
5  *  Copyright(C) 2006-2007  Timesys Corp., Thomas Gleixner
6  *
7  *  High-resolution kernel timers
8  *
9  *  In contrast to the low-resolution timeout API, aka timer wheel,
10  *  hrtimers provide finer resolution and accuracy depending on system
11  *  configuration and capabilities.
12  *
13  *  Started by: Thomas Gleixner and Ingo Molnar
14  *
15  *  Credits:
16  *	Based on the original timer wheel code
17  *
18  *	Help, testing, suggestions, bugfixes, improvements were
19  *	provided by:
20  *
21  *	George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel
22  *	et. al.
23  */
24 
25 #include <linux/cpu.h>
26 #include <linux/export.h>
27 #include <linux/percpu.h>
28 #include <linux/hrtimer.h>
29 #include <linux/hrtimer_bases.h>
30 #include <linux/notifier.h>
31 #include <linux/syscalls.h>
32 #include <linux/interrupt.h>
33 #include <linux/tick.h>
34 #include <linux/err.h>
35 #include <linux/debugobjects.h>
36 #include <linux/sched/signal.h>
37 #include <linux/sched/sysctl.h>
38 #include <linux/sched/rt.h>
39 #include <linux/sched/deadline.h>
40 #include <linux/sched/nohz.h>
41 #include <linux/sched/debug.h>
42 #include <linux/sched/isolation.h>
43 #include <linux/timer.h>
44 #include <linux/freezer.h>
45 #include <linux/compat.h>
46 
47 #include <linux/uaccess.h>
48 
49 #include <trace/events/timer.h>
50 
51 #include "tick-internal.h"
52 
53 /*
54  * Constants to set the queued state of the timer (INACTIVE, ENQUEUED)
55  *
56  * The callback state is kept separate in the CPU base because having it in
57  * the timer would required touching the timer after the callback, which
58  * makes it impossible to free the timer from the callback function.
59  *
60  * Therefore we track the callback state in:
61  *
62  *	timer->base->cpu_base->running == timer
63  *
64  * On SMP it is possible to have a "callback function running and enqueued"
65  * status. It happens for example when a posix timer expired and the callback
66  * queued a signal. Between dropping the lock which protects the posix timer
67  * and reacquiring the base lock of the hrtimer, another CPU can deliver the
68  * signal and rearm the timer.
69  *
70  * All state transitions are protected by cpu_base->lock.
71  */
72 #define HRTIMER_STATE_INACTIVE	false
73 #define HRTIMER_STATE_ENQUEUED	true
74 
75 /*
76  * The resolution of the clocks. The resolution value is returned in
77  * the clock_getres() system call to give application programmers an
78  * idea of the (in)accuracy of timers. Timer values are rounded up to
79  * this resolution values.
80  */
81 #define HIGH_RES_NSEC		1
82 
83 /*
84  * Masks for selecting the soft and hard context timers from
85  * cpu_base->active
86  */
87 #define MASK_SHIFT		(HRTIMER_BASE_MONOTONIC_SOFT)
88 #define HRTIMER_ACTIVE_HARD	((1U << MASK_SHIFT) - 1)
89 #define HRTIMER_ACTIVE_SOFT	(HRTIMER_ACTIVE_HARD << MASK_SHIFT)
90 #define HRTIMER_ACTIVE_ALL	(HRTIMER_ACTIVE_SOFT | HRTIMER_ACTIVE_HARD)
91 
92 static void retrigger_next_event(void *arg);
93 static ktime_t __hrtimer_cb_get_time(clockid_t clock_id);
94 
95 /*
96  * The timer bases:
97  *
98  * There are more clockids than hrtimer bases. Thus, we index
99  * into the timer bases by the hrtimer_base_type enum. When trying
100  * to reach a base using a clockid, hrtimer_clockid_to_base()
101  * is used to convert from clockid to the proper hrtimer_base_type.
102  */
103 
104 #define BASE_INIT(idx, cid)			\
105 	[idx] = { .index = idx, .clockid = cid }
106 
107 DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
108 {
109 	.lock = __RAW_SPIN_LOCK_UNLOCKED(hrtimer_bases.lock),
110 	.clock_base = {
111 		BASE_INIT(HRTIMER_BASE_MONOTONIC,	CLOCK_MONOTONIC),
112 		BASE_INIT(HRTIMER_BASE_REALTIME,	CLOCK_REALTIME),
113 		BASE_INIT(HRTIMER_BASE_BOOTTIME,	CLOCK_BOOTTIME),
114 		BASE_INIT(HRTIMER_BASE_TAI,		CLOCK_TAI),
115 		BASE_INIT(HRTIMER_BASE_MONOTONIC_SOFT,	CLOCK_MONOTONIC),
116 		BASE_INIT(HRTIMER_BASE_REALTIME_SOFT,	CLOCK_REALTIME),
117 		BASE_INIT(HRTIMER_BASE_BOOTTIME_SOFT,	CLOCK_BOOTTIME),
118 		BASE_INIT(HRTIMER_BASE_TAI_SOFT,	CLOCK_TAI),
119 	},
120 	.csd = CSD_INIT(retrigger_next_event, NULL)
121 };
122 
hrtimer_base_is_online(struct hrtimer_cpu_base * base)123 static inline bool hrtimer_base_is_online(struct hrtimer_cpu_base *base)
124 {
125 	if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
126 		return true;
127 	else
128 		return likely(base->online);
129 }
130 
131 #ifdef CONFIG_HIGH_RES_TIMERS
132 DEFINE_STATIC_KEY_FALSE(hrtimer_highres_enabled_key);
133 
hrtimer_hres_workfn(struct work_struct * work)134 static void hrtimer_hres_workfn(struct work_struct *work)
135 {
136 	static_branch_enable(&hrtimer_highres_enabled_key);
137 }
138 
139 static DECLARE_WORK(hrtimer_hres_work, hrtimer_hres_workfn);
140 
hrtimer_schedule_hres_work(void)141 static inline void hrtimer_schedule_hres_work(void)
142 {
143 	if (!hrtimer_highres_enabled())
144 		schedule_work(&hrtimer_hres_work);
145 }
146 #else
hrtimer_schedule_hres_work(void)147 static inline void hrtimer_schedule_hres_work(void) { }
148 #endif
149 
150 /*
151  * Functions and macros which are different for UP/SMP systems are kept in a
152  * single place
153  */
154 #ifdef CONFIG_SMP
155 /*
156  * We require the migration_base for lock_hrtimer_base()/switch_hrtimer_base()
157  * such that hrtimer_callback_running() can unconditionally dereference
158  * timer->base->cpu_base
159  */
160 static struct hrtimer_cpu_base migration_cpu_base = {
161 	.clock_base = {
162 		[0] = {
163 			.cpu_base = &migration_cpu_base,
164 			.seq      = SEQCNT_RAW_SPINLOCK_ZERO(migration_cpu_base.seq,
165 							     &migration_cpu_base.lock),
166 		},
167 	},
168 };
169 
170 #define migration_base	migration_cpu_base.clock_base[0]
171 
172 /*
173  * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
174  * means that all timers which are tied to this base via timer->base are
175  * locked, and the base itself is locked too.
176  *
177  * So __run_timers/migrate_timers can safely modify all timers which could
178  * be found on the lists/queues.
179  *
180  * When the timer's base is locked, and the timer removed from list, it is
181  * possible to set timer->base = &migration_base and drop the lock: the timer
182  * remains locked.
183  */
lock_hrtimer_base(const struct hrtimer * timer,unsigned long * flags)184 static struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
185 						    unsigned long *flags)
186 	__acquires(&timer->base->lock)
187 {
188 	for (;;) {
189 		struct hrtimer_clock_base *base = READ_ONCE(timer->base);
190 
191 		if (likely(base != &migration_base)) {
192 			raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
193 			if (likely(base == timer->base))
194 				return base;
195 			/* The timer has migrated to another CPU: */
196 			raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
197 		}
198 		cpu_relax();
199 	}
200 }
201 
202 /*
203  * Check if the elected target is suitable considering its next
204  * event and the hotplug state of the current CPU.
205  *
206  * If the elected target is remote and its next event is after the timer
207  * to queue, then a remote reprogram is necessary. However there is no
208  * guarantee the IPI handling the operation would arrive in time to meet
209  * the high resolution deadline. In this case the local CPU becomes a
210  * preferred target, unless it is offline.
211  *
212  * High and low resolution modes are handled the same way for simplicity.
213  *
214  * Called with cpu_base->lock of target cpu held.
215  */
hrtimer_suitable_target(struct hrtimer * timer,struct hrtimer_clock_base * new_base,struct hrtimer_cpu_base * new_cpu_base,struct hrtimer_cpu_base * this_cpu_base)216 static bool hrtimer_suitable_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base,
217 				    struct hrtimer_cpu_base *new_cpu_base,
218 				    struct hrtimer_cpu_base *this_cpu_base)
219 {
220 	ktime_t expires;
221 
222 	/*
223 	 * The local CPU clockevent can be reprogrammed. Also get_target_base()
224 	 * guarantees it is online.
225 	 */
226 	if (new_cpu_base == this_cpu_base)
227 		return true;
228 
229 	/*
230 	 * The offline local CPU can't be the default target if the
231 	 * next remote target event is after this timer. Keep the
232 	 * elected new base. An IPI will be issued to reprogram
233 	 * it as a last resort.
234 	 */
235 	if (!hrtimer_base_is_online(this_cpu_base))
236 		return true;
237 
238 	expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
239 
240 	return expires >= new_base->cpu_base->expires_next;
241 }
242 
get_target_base(struct hrtimer_cpu_base * base,bool pinned)243 static inline struct hrtimer_cpu_base *get_target_base(struct hrtimer_cpu_base *base, bool pinned)
244 {
245 	if (!hrtimer_base_is_online(base)) {
246 		int cpu = cpumask_any_and(cpu_online_mask, housekeeping_cpumask(HK_TYPE_TIMER));
247 
248 		return &per_cpu(hrtimer_bases, cpu);
249 	}
250 
251 #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
252 	if (static_branch_likely(&timers_migration_enabled) && !pinned)
253 		return &per_cpu(hrtimer_bases, get_nohz_timer_target());
254 #endif
255 	return base;
256 }
257 
258 /*
259  * We switch the timer base to a power-optimized selected CPU target,
260  * if:
261  *	- NO_HZ_COMMON is enabled
262  *	- timer migration is enabled
263  *	- the timer callback is not running
264  *	- the timer is not the first expiring timer on the new target
265  *
266  * If one of the above requirements is not fulfilled we move the timer
267  * to the current CPU or leave it on the previously assigned CPU if
268  * the timer callback is currently running.
269  */
270 static inline struct hrtimer_clock_base *
switch_hrtimer_base(struct hrtimer * timer,struct hrtimer_clock_base * base,bool pinned)271 switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base, bool pinned)
272 {
273 	struct hrtimer_cpu_base *new_cpu_base, *this_cpu_base;
274 	struct hrtimer_clock_base *new_base;
275 	int basenum = base->index;
276 
277 	this_cpu_base = this_cpu_ptr(&hrtimer_bases);
278 	new_cpu_base = get_target_base(this_cpu_base, pinned);
279 again:
280 	new_base = &new_cpu_base->clock_base[basenum];
281 
282 	if (base != new_base) {
283 		/*
284 		 * We are trying to move timer to new_base. However we can't
285 		 * change timer's base while it is running, so we keep it on
286 		 * the same CPU. No hassle vs. reprogramming the event source
287 		 * in the high resolution case. The remote CPU will take care
288 		 * of this when the timer function has completed. There is no
289 		 * conflict as we hold the lock until the timer is enqueued.
290 		 */
291 		if (unlikely(hrtimer_callback_running(timer)))
292 			return base;
293 
294 		/* See the comment in lock_hrtimer_base() */
295 		WRITE_ONCE(timer->base, &migration_base);
296 		raw_spin_unlock(&base->cpu_base->lock);
297 		raw_spin_lock(&new_base->cpu_base->lock);
298 
299 		if (!hrtimer_suitable_target(timer, new_base, new_cpu_base, this_cpu_base)) {
300 			raw_spin_unlock(&new_base->cpu_base->lock);
301 			raw_spin_lock(&base->cpu_base->lock);
302 			new_cpu_base = this_cpu_base;
303 			WRITE_ONCE(timer->base, base);
304 			goto again;
305 		}
306 		WRITE_ONCE(timer->base, new_base);
307 	} else {
308 		if (!hrtimer_suitable_target(timer, new_base,  new_cpu_base, this_cpu_base)) {
309 			new_cpu_base = this_cpu_base;
310 			goto again;
311 		}
312 	}
313 	return new_base;
314 }
315 
316 #else /* CONFIG_SMP */
317 
lock_hrtimer_base(const struct hrtimer * timer,unsigned long * flags)318 static inline struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
319 							   unsigned long *flags)
320 	__acquires(&timer->base->cpu_base->lock)
321 {
322 	struct hrtimer_clock_base *base = timer->base;
323 
324 	raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
325 	return base;
326 }
327 
328 # define switch_hrtimer_base(t, b, p)	(b)
329 
330 #endif	/* !CONFIG_SMP */
331 
332 /*
333  * Functions for the union type storage format of ktime_t which are
334  * too large for inlining:
335  */
336 #if BITS_PER_LONG < 64
337 /*
338  * Divide a ktime value by a nanosecond value
339  */
__ktime_divns(const ktime_t kt,s64 div)340 s64 __ktime_divns(const ktime_t kt, s64 div)
341 {
342 	int sft = 0;
343 	s64 dclc;
344 	u64 tmp;
345 
346 	dclc = ktime_to_ns(kt);
347 	tmp = dclc < 0 ? -dclc : dclc;
348 
349 	/* Make sure the divisor is less than 2^32: */
350 	while (div >> 32) {
351 		sft++;
352 		div >>= 1;
353 	}
354 	tmp >>= sft;
355 	do_div(tmp, (u32) div);
356 	return dclc < 0 ? -tmp : tmp;
357 }
358 EXPORT_SYMBOL_GPL(__ktime_divns);
359 #endif /* BITS_PER_LONG < 64 */
360 
361 /*
362  * Add two ktime values and do a safety check for overflow:
363  */
ktime_add_safe(const ktime_t lhs,const ktime_t rhs)364 ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
365 {
366 	ktime_t res = ktime_add_unsafe(lhs, rhs);
367 
368 	/*
369 	 * We use KTIME_SEC_MAX here, the maximum timeout which we can
370 	 * return to user space in a timespec:
371 	 */
372 	if (res < 0 || res < lhs || res < rhs)
373 		res = ktime_set(KTIME_SEC_MAX, 0);
374 
375 	return res;
376 }
377 
378 EXPORT_SYMBOL_GPL(ktime_add_safe);
379 
380 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
381 
382 static const struct debug_obj_descr hrtimer_debug_descr;
383 
hrtimer_debug_hint(void * addr)384 static void *hrtimer_debug_hint(void *addr)
385 {
386 	return ACCESS_PRIVATE((struct hrtimer *)addr, function);
387 }
388 
389 /*
390  * fixup_init is called when:
391  * - an active object is initialized
392  */
hrtimer_fixup_init(void * addr,enum debug_obj_state state)393 static bool hrtimer_fixup_init(void *addr, enum debug_obj_state state)
394 {
395 	struct hrtimer *timer = addr;
396 
397 	switch (state) {
398 	case ODEBUG_STATE_ACTIVE:
399 		hrtimer_cancel(timer);
400 		debug_object_init(timer, &hrtimer_debug_descr);
401 		return true;
402 	default:
403 		return false;
404 	}
405 }
406 
407 /*
408  * fixup_activate is called when:
409  * - an active object is activated
410  * - an unknown non-static object is activated
411  */
hrtimer_fixup_activate(void * addr,enum debug_obj_state state)412 static bool hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
413 {
414 	switch (state) {
415 	case ODEBUG_STATE_ACTIVE:
416 		WARN_ON(1);
417 		fallthrough;
418 	default:
419 		return false;
420 	}
421 }
422 
423 /*
424  * fixup_free is called when:
425  * - an active object is freed
426  */
hrtimer_fixup_free(void * addr,enum debug_obj_state state)427 static bool hrtimer_fixup_free(void *addr, enum debug_obj_state state)
428 {
429 	struct hrtimer *timer = addr;
430 
431 	switch (state) {
432 	case ODEBUG_STATE_ACTIVE:
433 		hrtimer_cancel(timer);
434 		debug_object_free(timer, &hrtimer_debug_descr);
435 		return true;
436 	default:
437 		return false;
438 	}
439 }
440 
441 /* Stub timer callback for improperly used timers. */
stub_timer(struct hrtimer * unused)442 static enum hrtimer_restart stub_timer(struct hrtimer *unused)
443 {
444 	WARN_ON_ONCE(1);
445 	return HRTIMER_NORESTART;
446 }
447 
448 /*
449  * hrtimer_fixup_assert_init is called when:
450  * - an untracked/uninit-ed object is found
451  */
hrtimer_fixup_assert_init(void * addr,enum debug_obj_state state)452 static bool hrtimer_fixup_assert_init(void *addr, enum debug_obj_state state)
453 {
454 	struct hrtimer *timer = addr;
455 
456 	switch (state) {
457 	case ODEBUG_STATE_NOTAVAILABLE:
458 		hrtimer_setup(timer, stub_timer, CLOCK_MONOTONIC, 0);
459 		return true;
460 	default:
461 		return false;
462 	}
463 }
464 
465 static const struct debug_obj_descr hrtimer_debug_descr = {
466 	.name			= "hrtimer",
467 	.debug_hint		= hrtimer_debug_hint,
468 	.fixup_init		= hrtimer_fixup_init,
469 	.fixup_activate		= hrtimer_fixup_activate,
470 	.fixup_free		= hrtimer_fixup_free,
471 	.fixup_assert_init	= hrtimer_fixup_assert_init,
472 };
473 
debug_hrtimer_init(struct hrtimer * timer)474 static inline void debug_hrtimer_init(struct hrtimer *timer)
475 {
476 	debug_object_init(timer, &hrtimer_debug_descr);
477 }
478 
debug_hrtimer_init_on_stack(struct hrtimer * timer)479 static inline void debug_hrtimer_init_on_stack(struct hrtimer *timer)
480 {
481 	debug_object_init_on_stack(timer, &hrtimer_debug_descr);
482 }
483 
debug_hrtimer_activate(struct hrtimer * timer,enum hrtimer_mode mode)484 static inline void debug_hrtimer_activate(struct hrtimer *timer, enum hrtimer_mode mode)
485 {
486 	debug_object_activate(timer, &hrtimer_debug_descr);
487 }
488 
debug_hrtimer_deactivate(struct hrtimer * timer)489 static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
490 {
491 	debug_object_deactivate(timer, &hrtimer_debug_descr);
492 }
493 
debug_hrtimer_assert_init(struct hrtimer * timer)494 static inline void debug_hrtimer_assert_init(struct hrtimer *timer)
495 {
496 	debug_object_assert_init(timer, &hrtimer_debug_descr);
497 }
498 
destroy_hrtimer_on_stack(struct hrtimer * timer)499 void destroy_hrtimer_on_stack(struct hrtimer *timer)
500 {
501 	debug_object_free(timer, &hrtimer_debug_descr);
502 }
503 EXPORT_SYMBOL_GPL(destroy_hrtimer_on_stack);
504 
505 #else
506 
debug_hrtimer_init(struct hrtimer * timer)507 static inline void debug_hrtimer_init(struct hrtimer *timer) { }
debug_hrtimer_init_on_stack(struct hrtimer * timer)508 static inline void debug_hrtimer_init_on_stack(struct hrtimer *timer) { }
debug_hrtimer_activate(struct hrtimer * timer,enum hrtimer_mode mode)509 static inline void debug_hrtimer_activate(struct hrtimer *timer, enum hrtimer_mode mode) { }
debug_hrtimer_deactivate(struct hrtimer * timer)510 static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
debug_hrtimer_assert_init(struct hrtimer * timer)511 static inline void debug_hrtimer_assert_init(struct hrtimer *timer) { }
512 #endif
513 
debug_setup(struct hrtimer * timer,clockid_t clockid,enum hrtimer_mode mode)514 static inline void debug_setup(struct hrtimer *timer, clockid_t clockid, enum hrtimer_mode mode)
515 {
516 	debug_hrtimer_init(timer);
517 	trace_hrtimer_setup(timer, clockid, mode);
518 }
519 
debug_setup_on_stack(struct hrtimer * timer,clockid_t clockid,enum hrtimer_mode mode)520 static inline void debug_setup_on_stack(struct hrtimer *timer, clockid_t clockid,
521 					enum hrtimer_mode mode)
522 {
523 	debug_hrtimer_init_on_stack(timer);
524 	trace_hrtimer_setup(timer, clockid, mode);
525 }
526 
debug_activate(struct hrtimer * timer,enum hrtimer_mode mode,bool was_armed)527 static inline void debug_activate(struct hrtimer *timer, enum hrtimer_mode mode, bool was_armed)
528 {
529 	debug_hrtimer_activate(timer, mode);
530 	trace_hrtimer_start(timer, mode, was_armed);
531 }
532 
533 #define for_each_active_base(base, cpu_base, active)					\
534 	for (unsigned int idx = ffs(active); idx--; idx = ffs((active)))		\
535 		for (bool done = false; !done; active &= ~(1U << idx))			\
536 			for (base = &cpu_base->clock_base[idx]; !done; done = true)
537 
538 #define hrtimer_from_timerqueue_node(_n) container_of_const(_n, struct hrtimer, node)
539 
540 #if defined(CONFIG_NO_HZ_COMMON)
541 /*
542  * Same as hrtimer_bases_next_event() below, but skips the excluded timer and
543  * does not update cpu_base->next_timer/expires.
544  */
hrtimer_bases_next_event_without(struct hrtimer_cpu_base * cpu_base,const struct hrtimer * exclude,unsigned int active,ktime_t expires_next)545 static ktime_t hrtimer_bases_next_event_without(struct hrtimer_cpu_base *cpu_base,
546 						const struct hrtimer *exclude,
547 						unsigned int active, ktime_t expires_next)
548 {
549 	struct hrtimer_clock_base *base;
550 	ktime_t expires;
551 
552 	lockdep_assert_held(&cpu_base->lock);
553 
554 	for_each_active_base(base, cpu_base, active) {
555 		expires = ktime_sub(base->expires_next, base->offset);
556 		if (expires >= expires_next)
557 			continue;
558 
559 		/*
560 		 * If the excluded timer is the first on this base evaluate the
561 		 * next timer.
562 		 */
563 		struct timerqueue_linked_node *node = timerqueue_linked_first(&base->active);
564 
565 		if (unlikely(&exclude->node == node)) {
566 			node = timerqueue_linked_next(node);
567 			if (!node)
568 				continue;
569 			expires = ktime_sub(node->expires, base->offset);
570 			if (expires >= expires_next)
571 				continue;
572 		}
573 		expires_next = expires;
574 	}
575 	/* If base->offset changed, the result might be negative */
576 	return max(expires_next, 0);
577 }
578 #endif
579 
clock_base_next_timer(struct hrtimer_clock_base * base)580 static __always_inline struct hrtimer *clock_base_next_timer(struct hrtimer_clock_base *base)
581 {
582 	struct timerqueue_linked_node *next = timerqueue_linked_first(&base->active);
583 
584 	return hrtimer_from_timerqueue_node(next);
585 }
586 
587 /* Find the base with the earliest expiry */
hrtimer_bases_first(struct hrtimer_cpu_base * cpu_base,unsigned int active,ktime_t * expires_next,struct hrtimer ** next_timer)588 static void hrtimer_bases_first(struct hrtimer_cpu_base *cpu_base,unsigned int active,
589 				ktime_t *expires_next, struct hrtimer **next_timer)
590 {
591 	struct hrtimer_clock_base *base;
592 	ktime_t expires;
593 
594 	for_each_active_base(base, cpu_base, active) {
595 		expires = ktime_sub(base->expires_next, base->offset);
596 		if (expires < *expires_next) {
597 			*expires_next = expires;
598 			*next_timer = clock_base_next_timer(base);
599 		}
600 	}
601 }
602 
603 /*
604  * Recomputes cpu_base::*next_timer and returns the earliest expires_next
605  * but does not set cpu_base::*expires_next, that is done by
606  * hrtimer[_force]_reprogram and hrtimer_interrupt only. When updating
607  * cpu_base::*expires_next right away, reprogramming logic would no longer
608  * work.
609  *
610  * When a softirq is pending, we can ignore the HRTIMER_ACTIVE_SOFT bases,
611  * those timers will get run whenever the softirq gets handled, at the end of
612  * hrtimer_run_softirq(), hrtimer_update_softirq_timer() will re-add these bases.
613  *
614  * Therefore softirq values are those from the HRTIMER_ACTIVE_SOFT clock bases.
615  * The !softirq values are the minima across HRTIMER_ACTIVE_ALL, unless an actual
616  * softirq is pending, in which case they're the minima of HRTIMER_ACTIVE_HARD.
617  *
618  * @active_mask must be one of:
619  *  - HRTIMER_ACTIVE_ALL,
620  *  - HRTIMER_ACTIVE_SOFT, or
621  *  - HRTIMER_ACTIVE_HARD.
622  */
__hrtimer_get_next_event(struct hrtimer_cpu_base * cpu_base,unsigned int active_mask)623 static ktime_t __hrtimer_get_next_event(struct hrtimer_cpu_base *cpu_base, unsigned int active_mask)
624 {
625 	struct hrtimer *next_timer = NULL;
626 	ktime_t expires_next = KTIME_MAX;
627 	unsigned int active;
628 
629 	lockdep_assert_held(&cpu_base->lock);
630 
631 	if (!cpu_base->softirq_activated && (active_mask & HRTIMER_ACTIVE_SOFT)) {
632 		active = cpu_base->active_bases & HRTIMER_ACTIVE_SOFT;
633 		if (active)
634 			hrtimer_bases_first(cpu_base, active, &expires_next, &next_timer);
635 		cpu_base->softirq_next_timer = next_timer;
636 	}
637 
638 	if (active_mask & HRTIMER_ACTIVE_HARD) {
639 		active = cpu_base->active_bases & HRTIMER_ACTIVE_HARD;
640 		if (active)
641 			hrtimer_bases_first(cpu_base, active, &expires_next, &next_timer);
642 		cpu_base->next_timer = next_timer;
643 	}
644 	return max(expires_next, 0);
645 }
646 
hrtimer_update_next_event(struct hrtimer_cpu_base * cpu_base)647 static ktime_t hrtimer_update_next_event(struct hrtimer_cpu_base *cpu_base)
648 {
649 	ktime_t expires_next, soft = KTIME_MAX;
650 
651 	/*
652 	 * If the soft interrupt has already been activated, ignore the
653 	 * soft bases. They will be handled in the already raised soft
654 	 * interrupt.
655 	 */
656 	if (!cpu_base->softirq_activated) {
657 		soft = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_SOFT);
658 		/*
659 		 * Update the soft expiry time. clock_settime() might have
660 		 * affected it.
661 		 */
662 		cpu_base->softirq_expires_next = soft;
663 	}
664 
665 	expires_next = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_HARD);
666 	/*
667 	 * If a softirq timer is expiring first, update cpu_base->next_timer
668 	 * and program the hardware with the soft expiry time.
669 	 */
670 	if (expires_next > soft) {
671 		cpu_base->next_timer = cpu_base->softirq_next_timer;
672 		expires_next = soft;
673 	}
674 
675 	return expires_next;
676 }
677 
hrtimer_update_base(struct hrtimer_cpu_base * base)678 static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
679 {
680 	lockdep_assert_held(&base->lock);
681 
682 	ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
683 	ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset;
684 	ktime_t *offs_tai = &base->clock_base[HRTIMER_BASE_TAI].offset;
685 
686 	ktime_t now = ktime_get_update_offsets_now(&base->clock_was_set_seq, offs_real,
687 						   offs_boot, offs_tai);
688 
689 	base->clock_base[HRTIMER_BASE_REALTIME_SOFT].offset = *offs_real;
690 	base->clock_base[HRTIMER_BASE_BOOTTIME_SOFT].offset = *offs_boot;
691 	base->clock_base[HRTIMER_BASE_TAI_SOFT].offset = *offs_tai;
692 
693 	return now;
694 }
695 
696 /*
697  * Is the high resolution mode active in the CPU base. This cannot use the
698  * static key as the CPUs are switched to high resolution mode
699  * asynchronously.
700  */
hrtimer_hres_active(struct hrtimer_cpu_base * cpu_base)701 static inline int hrtimer_hres_active(struct hrtimer_cpu_base *cpu_base)
702 {
703 	return IS_ENABLED(CONFIG_HIGH_RES_TIMERS) ?
704 		cpu_base->hres_active : 0;
705 }
706 
hrtimer_rearm_event(ktime_t expires_next,bool deferred)707 static inline void hrtimer_rearm_event(ktime_t expires_next, bool deferred)
708 {
709 	trace_hrtimer_rearm(expires_next, deferred);
710 	tick_program_event(expires_next, 1);
711 }
712 
__hrtimer_reprogram(struct hrtimer_cpu_base * cpu_base,ktime_t expires_next)713 static void __hrtimer_reprogram(struct hrtimer_cpu_base *cpu_base,
714 				ktime_t expires_next)
715 {
716 	cpu_base->expires_next = expires_next;
717 
718 	/*
719 	 * If hres is not active, hardware does not have to be
720 	 * reprogrammed yet.
721 	 *
722 	 * If a hang was detected in the last timer interrupt then we
723 	 * leave the hang delay active in the hardware. We want the
724 	 * system to make progress. That also prevents the following
725 	 * scenario:
726 	 * T1 expires 50ms from now
727 	 * T2 expires 5s from now
728 	 *
729 	 * T1 is removed, so this code is called and would reprogram
730 	 * the hardware to 5s from now. Any hrtimer_start after that
731 	 * will not reprogram the hardware due to hang_detected being
732 	 * set. So we'd effectively block all timers until the T2 event
733 	 * fires.
734 	 */
735 	if (!hrtimer_hres_active(cpu_base) || cpu_base->hang_detected)
736 		return;
737 
738 	hrtimer_rearm_event(expires_next, false);
739 }
740 
741 /* Reprogram the event source with a evaluation of all clock bases */
hrtimer_force_reprogram(struct hrtimer_cpu_base * cpu_base,bool skip_equal)742 static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, bool skip_equal)
743 {
744 	ktime_t expires_next = hrtimer_update_next_event(cpu_base);
745 
746 	if (skip_equal && expires_next == cpu_base->expires_next)
747 		return;
748 
749 	__hrtimer_reprogram(cpu_base, expires_next);
750 }
751 
752 /* High resolution timer related functions */
753 #ifdef CONFIG_HIGH_RES_TIMERS
754 
755 /* High resolution timer enabled ? */
756 static bool hrtimer_hres_enabled __read_mostly  = true;
757 unsigned int hrtimer_resolution __read_mostly = LOW_RES_NSEC;
758 EXPORT_SYMBOL_GPL(hrtimer_resolution);
759 
760 /* Enable / Disable high resolution mode */
setup_hrtimer_hres(char * str)761 static int __init setup_hrtimer_hres(char *str)
762 {
763 	return (kstrtobool(str, &hrtimer_hres_enabled) == 0);
764 }
765 __setup("highres=", setup_hrtimer_hres);
766 
767 /* hrtimer_high_res_enabled - query, if the highres mode is enabled */
hrtimer_is_hres_enabled(void)768 static inline bool hrtimer_is_hres_enabled(void)
769 {
770 	return hrtimer_hres_enabled;
771 }
772 
773 /* Switch to high resolution mode */
hrtimer_switch_to_hres(void)774 static void hrtimer_switch_to_hres(void)
775 {
776 	struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases);
777 
778 	if (tick_init_highres()) {
779 		pr_warn("Could not switch to high resolution mode on CPU %u\n",	base->cpu);
780 		return;
781 	}
782 	base->hres_active = true;
783 	hrtimer_resolution = HIGH_RES_NSEC;
784 
785 	tick_setup_sched_timer(true);
786 	/* "Retrigger" the interrupt to get things going */
787 	retrigger_next_event(NULL);
788 	hrtimer_schedule_hres_work();
789 }
790 
791 #else
792 
hrtimer_is_hres_enabled(void)793 static inline bool hrtimer_is_hres_enabled(void) { return 0; }
hrtimer_switch_to_hres(void)794 static inline void hrtimer_switch_to_hres(void) { }
795 
796 #endif /* CONFIG_HIGH_RES_TIMERS */
797 
798 /*
799  * Retrigger next event is called after clock was set with interrupts
800  * disabled through an SMP function call or directly from low level
801  * resume code.
802  *
803  * This is only invoked when:
804  *	- CONFIG_HIGH_RES_TIMERS is enabled.
805  *	- CONFIG_NO_HZ_COMMON is enabled
806  *
807  * For the other cases this function is empty and because the call sites
808  * are optimized out it vanishes as well, i.e. no need for lots of
809  * #ifdeffery.
810  */
retrigger_next_event(void * arg)811 static void retrigger_next_event(void *arg)
812 {
813 	struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases);
814 
815 	/*
816 	 * When high resolution mode or nohz is active, then the offsets of
817 	 * CLOCK_REALTIME/TAI/BOOTTIME have to be updated. Otherwise the
818 	 * next tick will take care of that.
819 	 *
820 	 * If high resolution mode is active then the next expiring timer
821 	 * must be reevaluated and the clock event device reprogrammed if
822 	 * necessary.
823 	 *
824 	 * In the NOHZ case the update of the offset and the reevaluation
825 	 * of the next expiring timer is enough. The return from the SMP
826 	 * function call will take care of the reprogramming in case the
827 	 * CPU was in a NOHZ idle sleep.
828 	 *
829 	 * In periodic low resolution mode, the next softirq expiration
830 	 * must also be updated.
831 	 */
832 	guard(raw_spinlock)(&base->lock);
833 	hrtimer_update_base(base);
834 	if (hrtimer_hres_active(base))
835 		hrtimer_force_reprogram(base, /* skip_equal */ false);
836 	else
837 		hrtimer_update_next_event(base);
838 }
839 
840 /*
841  * When a timer is enqueued and expires earlier than the already enqueued
842  * timers, we have to check, whether it expires earlier than the timer for
843  * which the clock event device was armed.
844  *
845  * Called with interrupts disabled and base->cpu_base.lock held
846  */
hrtimer_reprogram(struct hrtimer * timer,bool reprogram)847 static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram)
848 {
849 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
850 	struct hrtimer_clock_base *base = timer->base;
851 	ktime_t expires = hrtimer_get_expires(timer);
852 
853 	WARN_ON_ONCE(expires < 0);
854 
855 	expires = ktime_sub(expires, base->offset);
856 	/*
857 	 * CLOCK_REALTIME timer might be requested with an absolute
858 	 * expiry time which is less than base->offset. Set it to 0.
859 	 */
860 	if (expires < 0)
861 		expires = 0;
862 
863 	if (timer->is_soft) {
864 		/*
865 		 * soft hrtimer could be started on a remote CPU. In this
866 		 * case softirq_expires_next needs to be updated on the
867 		 * remote CPU. The soft hrtimer will not expire before the
868 		 * first hard hrtimer on the remote CPU -
869 		 * hrtimer_check_target() prevents this case.
870 		 */
871 		struct hrtimer_cpu_base *timer_cpu_base = base->cpu_base;
872 
873 		if (timer_cpu_base->softirq_activated)
874 			return;
875 
876 		if (!ktime_before(expires, timer_cpu_base->softirq_expires_next))
877 			return;
878 
879 		timer_cpu_base->softirq_next_timer = timer;
880 		timer_cpu_base->softirq_expires_next = expires;
881 
882 		if (!ktime_before(expires, timer_cpu_base->expires_next) || !reprogram)
883 			return;
884 	}
885 
886 	/*
887 	 * If the timer is not on the current cpu, we cannot reprogram
888 	 * the other cpus clock event device.
889 	 */
890 	if (base->cpu_base != cpu_base)
891 		return;
892 
893 	if (expires >= cpu_base->expires_next)
894 		return;
895 
896 	/* If a deferred rearm is pending skip reprogramming the device */
897 	if (cpu_base->deferred_rearm)
898 		return;
899 
900 	cpu_base->next_timer = timer;
901 
902 	__hrtimer_reprogram(cpu_base, expires);
903 }
904 
update_needs_ipi(struct hrtimer_cpu_base * cpu_base,unsigned int active)905 static bool update_needs_ipi(struct hrtimer_cpu_base *cpu_base, unsigned int active)
906 {
907 	struct hrtimer_clock_base *base;
908 	ktime_t expires;
909 	u32 seq;
910 
911 	/*
912 	 * Update the base offsets unconditionally so the following
913 	 * checks whether the SMP function call is required works.
914 	 *
915 	 * The update is safe even when the remote CPU is in the hrtimer
916 	 * interrupt or the hrtimer soft interrupt and expiring affected
917 	 * bases. Either it will see the update before handling a base or
918 	 * it will see it when it finishes the processing and reevaluates
919 	 * the next expiring timer.
920 	 */
921 	seq = cpu_base->clock_was_set_seq;
922 	hrtimer_update_base(cpu_base);
923 
924 	/*
925 	 * If the sequence did not change over the update then the
926 	 * remote CPU already handled it.
927 	 */
928 	if (seq == cpu_base->clock_was_set_seq)
929 		return false;
930 
931 	/* If a deferred rearm is pending the remote CPU will take care of it */
932 	if (cpu_base->deferred_rearm) {
933 		cpu_base->deferred_needs_update = true;
934 		return false;
935 	}
936 
937 	/*
938 	 * Walk the affected clock bases and check whether the first expiring
939 	 * timer in a clock base is moving ahead of the first expiring timer of
940 	 * @cpu_base. If so, the IPI must be invoked because per CPU clock
941 	 * event devices cannot be remotely reprogrammed.
942 	 */
943 	active &= cpu_base->active_bases;
944 
945 	for_each_active_base(base, cpu_base, active) {
946 		struct timerqueue_linked_node *next;
947 
948 		next = timerqueue_linked_first(&base->active);
949 		expires = ktime_sub(next->expires, base->offset);
950 		if (expires < cpu_base->expires_next)
951 			return true;
952 
953 		/* Extra check for softirq clock bases */
954 		if (base->index < HRTIMER_BASE_MONOTONIC_SOFT)
955 			continue;
956 		if (cpu_base->softirq_activated)
957 			continue;
958 		if (expires < cpu_base->softirq_expires_next)
959 			return true;
960 	}
961 	return false;
962 }
963 
964 /*
965  * Clock was set. This might affect CLOCK_REALTIME, CLOCK_TAI and
966  * CLOCK_BOOTTIME (for late sleep time injection).
967  *
968  * This requires to update the offsets for these clocks
969  * vs. CLOCK_MONOTONIC. When high resolution timers are enabled, then this
970  * also requires to eventually reprogram the per CPU clock event devices
971  * when the change moves an affected timer ahead of the first expiring
972  * timer on that CPU. Obviously remote per CPU clock event devices cannot
973  * be reprogrammed. The other reason why an IPI has to be sent is when the
974  * system is in !HIGH_RES and NOHZ mode. The NOHZ mode updates the offsets
975  * in the tick, which obviously might be stopped, so this has to bring out
976  * the remote CPU which might sleep in idle to get this sorted.
977  */
clock_was_set(unsigned int bases)978 void clock_was_set(unsigned int bases)
979 {
980 	cpumask_var_t mask;
981 
982 	if (!hrtimer_highres_enabled() && !tick_nohz_is_active())
983 		goto out_timerfd;
984 
985 	if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
986 		on_each_cpu(retrigger_next_event, NULL, 1);
987 		goto out_timerfd;
988 	}
989 
990 	/* Avoid interrupting CPUs if possible */
991 	scoped_guard(cpus_read_lock) {
992 		int cpu;
993 
994 		for_each_online_cpu(cpu) {
995 			struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
996 
997 			guard(raw_spinlock_irqsave)(&cpu_base->lock);
998 			if (update_needs_ipi(cpu_base, bases))
999 				cpumask_set_cpu(cpu, mask);
1000 		}
1001 		scoped_guard(preempt)
1002 			smp_call_function_many(mask, retrigger_next_event, NULL, 1);
1003 	}
1004 	free_cpumask_var(mask);
1005 
1006 out_timerfd:
1007 	timerfd_clock_was_set();
1008 }
1009 
clock_was_set_work(struct work_struct * work)1010 static void clock_was_set_work(struct work_struct *work)
1011 {
1012 	clock_was_set(CLOCK_SET_WALL);
1013 }
1014 
1015 static DECLARE_WORK(hrtimer_work, clock_was_set_work);
1016 
1017 /*
1018  * Called from timekeeping code to reprogram the hrtimer interrupt device
1019  * on all cpus and to notify timerfd.
1020  */
clock_was_set_delayed(void)1021 void clock_was_set_delayed(void)
1022 {
1023 	schedule_work(&hrtimer_work);
1024 }
1025 
1026 /*
1027  * Called during resume either directly from via timekeeping_resume()
1028  * or in the case of s2idle from tick_unfreeze() to ensure that the
1029  * hrtimers are up to date.
1030  */
hrtimers_resume_local(void)1031 void hrtimers_resume_local(void)
1032 {
1033 	lockdep_assert_irqs_disabled();
1034 	/* Retrigger on the local CPU */
1035 	retrigger_next_event(NULL);
1036 }
1037 
1038 /* Counterpart to lock_hrtimer_base above */
unlock_hrtimer_base(const struct hrtimer * timer,unsigned long * flags)1039 static inline void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
1040 	__releases(&timer->base->cpu_base->lock)
1041 {
1042 	raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
1043 }
1044 
1045 /**
1046  * hrtimer_update_function - Update the timer's callback function
1047  * @timer:	Timer to update
1048  * @function:	New callback function
1049  *
1050  * Only safe to call if the timer is not enqueued. Can be called in the callback function if the
1051  * timer is not enqueued at the same time (see the comments above HRTIMER_STATE_ENQUEUED).
1052  */
hrtimer_update_function(struct hrtimer * timer,enum hrtimer_restart (* function)(struct hrtimer *))1053 void hrtimer_update_function(struct hrtimer *timer,
1054 			     enum hrtimer_restart (*function)(struct hrtimer *))
1055 {
1056 #ifdef CONFIG_PROVE_LOCKING
1057 	guard(raw_spinlock_irqsave)(&timer->base->cpu_base->lock);
1058 
1059 	if (WARN_ON_ONCE(hrtimer_is_queued(timer)))
1060 		return;
1061 
1062 	if (WARN_ON_ONCE(!function))
1063 		return;
1064 #endif
1065 	ACCESS_PRIVATE(timer, function) = function;
1066 }
1067 EXPORT_SYMBOL_GPL(hrtimer_update_function);
1068 
1069 /**
1070  * hrtimer_forward() - forward the timer expiry
1071  * @timer:	hrtimer to forward
1072  * @now:	forward past this time
1073  * @interval:	the interval to forward
1074  *
1075  * Forward the timer expiry so it will expire in the future.
1076  *
1077  * .. note::
1078  *  This only updates the timer expiry value and does not requeue the timer.
1079  *
1080  * There is also a variant of this function: hrtimer_forward_now().
1081  *
1082  * Context: Can be safely called from the callback function of @timer. If called
1083  *          from other contexts @timer must neither be enqueued nor running the
1084  *          callback and the caller needs to take care of serialization.
1085  *
1086  * Return: The number of overruns are returned.
1087  */
hrtimer_forward(struct hrtimer * timer,ktime_t now,ktime_t interval)1088 u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
1089 {
1090 	ktime_t delta;
1091 	u64 orun = 1;
1092 
1093 	delta = ktime_sub(now, hrtimer_get_expires(timer));
1094 
1095 	if (delta < 0)
1096 		return 0;
1097 
1098 	if (WARN_ON(timer->is_queued))
1099 		return 0;
1100 
1101 	if (interval < hrtimer_resolution)
1102 		interval = hrtimer_resolution;
1103 
1104 	if (unlikely(delta >= interval)) {
1105 		s64 incr = ktime_to_ns(interval);
1106 
1107 		orun = ktime_divns(delta, incr);
1108 		hrtimer_add_expires_ns(timer, incr * orun);
1109 		if (hrtimer_get_expires(timer) > now)
1110 			return orun;
1111 		/*
1112 		 * This (and the ktime_add() below) is the
1113 		 * correction for exact:
1114 		 */
1115 		orun++;
1116 	}
1117 	hrtimer_add_expires(timer, interval);
1118 
1119 	return orun;
1120 }
1121 EXPORT_SYMBOL_GPL(hrtimer_forward);
1122 
1123 /*
1124  * enqueue_hrtimer - internal function to (re)start a timer
1125  *
1126  * The timer is inserted in expiry order. Insertion into the
1127  * red black tree is O(log(n)).
1128  *
1129  * Returns true when the new timer is the leftmost timer in the tree.
1130  */
enqueue_hrtimer(struct hrtimer * timer,struct hrtimer_clock_base * base,enum hrtimer_mode mode,bool was_armed)1131 static bool enqueue_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base,
1132 			    enum hrtimer_mode mode, bool was_armed)
1133 {
1134 	lockdep_assert_held(&base->cpu_base->lock);
1135 
1136 	debug_activate(timer, mode, was_armed);
1137 	WARN_ON_ONCE(!base->cpu_base->online);
1138 
1139 	base->cpu_base->active_bases |= 1 << base->index;
1140 
1141 	/* Pairs with the lockless read in hrtimer_is_queued() */
1142 	WRITE_ONCE(timer->is_queued, HRTIMER_STATE_ENQUEUED);
1143 
1144 	if (!timerqueue_linked_add(&base->active, &timer->node))
1145 		return false;
1146 
1147 	base->expires_next = hrtimer_get_expires(timer);
1148 	return true;
1149 }
1150 
base_update_next_timer(struct hrtimer_clock_base * base)1151 static inline void base_update_next_timer(struct hrtimer_clock_base *base)
1152 {
1153 	struct timerqueue_linked_node *next = timerqueue_linked_first(&base->active);
1154 
1155 	base->expires_next = next ? next->expires : KTIME_MAX;
1156 }
1157 
1158 /*
1159  * __remove_hrtimer - internal function to remove a timer
1160  *
1161  * High resolution timer mode reprograms the clock event device when the
1162  * timer is the one which expires next. The caller can disable this by setting
1163  * reprogram to zero. This is useful, when the context does a reprogramming
1164  * anyway (e.g. timer interrupt)
1165  */
__remove_hrtimer(struct hrtimer * timer,struct hrtimer_clock_base * base,bool newstate,bool reprogram)1166 static void __remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base,
1167 			     bool newstate, bool reprogram)
1168 {
1169 	struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1170 	bool was_first;
1171 
1172 	lockdep_assert_held(&cpu_base->lock);
1173 
1174 	if (!timer->is_queued)
1175 		return;
1176 
1177 	/* Pairs with the lockless read in hrtimer_is_queued() */
1178 	WRITE_ONCE(timer->is_queued, newstate);
1179 
1180 	was_first = !timerqueue_linked_prev(&timer->node);
1181 
1182 	if (!timerqueue_linked_del(&base->active, &timer->node))
1183 		cpu_base->active_bases &= ~(1 << base->index);
1184 
1185 	/* Nothing to update if this was not the first timer in the base */
1186 	if (!was_first)
1187 		return;
1188 
1189 	base_update_next_timer(base);
1190 
1191 	/*
1192 	 * If reprogram is false don't update cpu_base->next_timer and do not
1193 	 * touch the clock event device.
1194 	 *
1195 	 * This happens when removing the first timer on a remote CPU, which
1196 	 * will be handled by the remote CPU's interrupt. It also happens when
1197 	 * a local timer is removed to be immediately restarted. That's handled
1198 	 * at the call site.
1199 	 */
1200 	if (!reprogram || timer != cpu_base->next_timer || timer->is_lazy)
1201 		return;
1202 
1203 	if (cpu_base->deferred_rearm)
1204 		cpu_base->deferred_needs_update = true;
1205 	else
1206 		hrtimer_force_reprogram(cpu_base, /* skip_equal */ true);
1207 }
1208 
remove_hrtimer(struct hrtimer * timer,struct hrtimer_clock_base * base,bool newstate)1209 static inline bool remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base,
1210 				  bool newstate)
1211 {
1212 	lockdep_assert_held(&base->cpu_base->lock);
1213 
1214 	if (timer->is_queued) {
1215 		bool reprogram;
1216 
1217 		debug_hrtimer_deactivate(timer);
1218 
1219 		/*
1220 		 * Remove the timer and force reprogramming when high
1221 		 * resolution mode is active and the timer is on the current
1222 		 * CPU. If we remove a timer on another CPU, reprogramming is
1223 		 * skipped. The interrupt event on this CPU is fired and
1224 		 * reprogramming happens in the interrupt handler. This is a
1225 		 * rare case and less expensive than a smp call.
1226 		 */
1227 		reprogram = base->cpu_base == this_cpu_ptr(&hrtimer_bases);
1228 
1229 		__remove_hrtimer(timer, base, newstate, reprogram);
1230 		return true;
1231 	}
1232 	return false;
1233 }
1234 
1235 /*
1236  * Update in place has to retrieve the expiry times of the neighbour nodes
1237  * if they exist. That is cache line neutral because the dequeue/enqueue
1238  * operation is going to need the same cache lines. But there is a big win
1239  * when the dequeue/enqueue can be avoided because the RB tree does not
1240  * have to be rebalanced twice.
1241  */
1242 static inline bool
hrtimer_can_update_in_place(struct hrtimer * timer,struct hrtimer_clock_base * base,ktime_t expires)1243 hrtimer_can_update_in_place(struct hrtimer *timer, struct hrtimer_clock_base *base, ktime_t expires)
1244 {
1245 	struct timerqueue_linked_node *next = timerqueue_linked_next(&timer->node);
1246 	struct timerqueue_linked_node *prev = timerqueue_linked_prev(&timer->node);
1247 
1248 	/* If the new expiry goes behind the next timer, requeue is required */
1249 	if (next && expires > next->expires)
1250 		return false;
1251 
1252 	/* If this is the first timer, update in place */
1253 	if (!prev)
1254 		return true;
1255 
1256 	/* Update in place when it does not go ahead of the previous one */
1257 	return expires >= prev->expires;
1258 }
1259 
1260 static inline bool
remove_and_enqueue_same_base(struct hrtimer * timer,struct hrtimer_clock_base * base,const enum hrtimer_mode mode,ktime_t expires,u64 delta_ns)1261 remove_and_enqueue_same_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
1262 			     const enum hrtimer_mode mode, ktime_t expires, u64 delta_ns)
1263 {
1264 	bool was_first = false;
1265 
1266 	/*
1267 	 * Updating the sort key while @timer is queued can temporarily
1268 	 * make the tree inconsistent. This is safe under cpu_base->lock:
1269 	 * no other queue operation can observe that state.
1270 	 * hrtimer_can_update_in_place() either confirms that the new expiry
1271 	 * fits between the neighbours or timerqueue_linked_del() removes the
1272 	 * timer without consulting the expiry.
1273 	 */
1274 	hrtimer_set_expires_range_ns(timer, expires, delta_ns);
1275 	expires = hrtimer_get_expires(timer);
1276 
1277 	/* Remove it from the timer queue if active */
1278 	if (timer->is_queued) {
1279 		was_first = !timerqueue_linked_prev(&timer->node);
1280 
1281 		/* Try to update in place to avoid the de/enqueue dance */
1282 		if (hrtimer_can_update_in_place(timer, base, expires)) {
1283 			trace_hrtimer_start(timer, mode, true);
1284 			if (was_first)
1285 				base->expires_next = expires;
1286 			return was_first;
1287 		}
1288 
1289 		debug_hrtimer_deactivate(timer);
1290 		timerqueue_linked_del(&base->active, &timer->node);
1291 	}
1292 
1293 	debug_activate(timer, mode, timer->is_queued);
1294 	base->cpu_base->active_bases |= 1 << base->index;
1295 
1296 	/* Pairs with the lockless read in hrtimer_is_queued() */
1297 	WRITE_ONCE(timer->is_queued, HRTIMER_STATE_ENQUEUED);
1298 
1299 	/* If it's the first expiring timer now or again, update base */
1300 	if (timerqueue_linked_add(&base->active, &timer->node)) {
1301 		base->expires_next = expires;
1302 		return true;
1303 	}
1304 
1305 	if (was_first)
1306 		base_update_next_timer(base);
1307 
1308 	return false;
1309 }
1310 
hrtimer_update_lowres(struct hrtimer * timer,ktime_t tim,const enum hrtimer_mode mode)1311 static inline ktime_t hrtimer_update_lowres(struct hrtimer *timer, ktime_t tim,
1312 					    const enum hrtimer_mode mode)
1313 {
1314 #ifdef CONFIG_TIME_LOW_RES
1315 	/*
1316 	 * CONFIG_TIME_LOW_RES indicates that the system has no way to return
1317 	 * granular time values. For relative timers we add hrtimer_resolution
1318 	 * (i.e. one jiffy) to prevent short timeouts.
1319 	 */
1320 	timer->is_rel = mode & HRTIMER_MODE_REL;
1321 	if (timer->is_rel)
1322 		tim = ktime_add_safe(tim, hrtimer_resolution);
1323 #endif
1324 	return tim;
1325 }
1326 
hrtimer_update_softirq_timer(struct hrtimer_cpu_base * cpu_base,bool reprogram)1327 static void hrtimer_update_softirq_timer(struct hrtimer_cpu_base *cpu_base, bool reprogram)
1328 {
1329 	ktime_t expires = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_SOFT);
1330 
1331 	/*
1332 	 * Reprogramming needs to be triggered, even if the next soft
1333 	 * hrtimer expires at the same time as the next hard
1334 	 * hrtimer. cpu_base->softirq_expires_next needs to be updated!
1335 	 */
1336 	if (expires == KTIME_MAX)
1337 		return;
1338 
1339 	/*
1340 	 * cpu_base->next_timer is recomputed by __hrtimer_get_next_event()
1341 	 * cpu_base->expires_next is only set by hrtimer_reprogram()
1342 	 */
1343 	hrtimer_reprogram(cpu_base->softirq_next_timer, reprogram);
1344 }
1345 
1346 #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
hrtimer_prefer_local(bool is_local,bool is_first,bool is_pinned)1347 static __always_inline bool hrtimer_prefer_local(bool is_local, bool is_first, bool is_pinned)
1348 {
1349 	if (static_branch_likely(&timers_migration_enabled)) {
1350 		/*
1351 		 * If it is local and the first expiring timer keep it on the local
1352 		 * CPU to optimize reprogramming of the clockevent device. Also
1353 		 * avoid switch_hrtimer_base() overhead when local and pinned.
1354 		 */
1355 		if (!is_local)
1356 			return false;
1357 		if (is_first || is_pinned)
1358 			return true;
1359 
1360 		/* Honour the NOHZ full restrictions */
1361 		if (!housekeeping_cpu(smp_processor_id(), HK_TYPE_KERNEL_NOISE))
1362 			return false;
1363 
1364 		/*
1365 		 * If the tick is not stopped or need_resched() is set, then
1366 		 * there is no point in moving the timer somewhere else.
1367 		 */
1368 		return !tick_nohz_tick_stopped() || need_resched();
1369 	}
1370 	return is_local;
1371 }
1372 #else
hrtimer_prefer_local(bool is_local,bool is_first,bool is_pinned)1373 static __always_inline bool hrtimer_prefer_local(bool is_local, bool is_first, bool is_pinned)
1374 {
1375 	return is_local;
1376 }
1377 #endif
1378 
hrtimer_keep_base(struct hrtimer * timer,bool is_local,bool is_first,bool is_pinned)1379 static inline bool hrtimer_keep_base(struct hrtimer *timer, bool is_local, bool is_first,
1380 				     bool is_pinned)
1381 {
1382 	/* If the timer is running the callback it has to stay on its CPU base. */
1383 	if (unlikely(timer->base->running == timer))
1384 		return true;
1385 
1386 	return hrtimer_prefer_local(is_local, is_first, is_pinned);
1387 }
1388 
1389 enum {
1390 	HRTIMER_REPROGRAM_NONE,
1391 	HRTIMER_REPROGRAM,
1392 	HRTIMER_REPROGRAM_FORCE,
1393 };
1394 
__hrtimer_start_range_ns(struct hrtimer * timer,ktime_t tim,u64 delta_ns,const enum hrtimer_mode mode,struct hrtimer_clock_base * base)1395 static int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, u64 delta_ns,
1396 				    const enum hrtimer_mode mode, struct hrtimer_clock_base *base)
1397 {
1398 	struct hrtimer_cpu_base *this_cpu_base = this_cpu_ptr(&hrtimer_bases);
1399 	bool is_pinned, first, was_first, keep_base = false;
1400 	struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1401 
1402 	was_first = cpu_base->next_timer == timer;
1403 	is_pinned = !!(mode & HRTIMER_MODE_PINNED);
1404 
1405 	/*
1406 	 * Don't keep it local if this enqueue happens on a unplugged CPU
1407 	 * after hrtimer_cpu_dying() has been invoked.
1408 	 */
1409 	if (likely(this_cpu_base->online)) {
1410 		bool is_local = cpu_base == this_cpu_base;
1411 
1412 		keep_base = hrtimer_keep_base(timer, is_local, was_first, is_pinned);
1413 	}
1414 
1415 	/* Calculate absolute expiry time for relative timers */
1416 	if (mode & HRTIMER_MODE_REL)
1417 		tim = ktime_add_safe(tim, __hrtimer_cb_get_time(base->clockid));
1418 	/* Compensate for low resolution granularity */
1419 	tim = hrtimer_update_lowres(timer, tim, mode);
1420 
1421 	/*
1422 	 * Remove an active timer from the queue. In case it is not queued
1423 	 * on the current CPU, make sure that remove_hrtimer() updates the
1424 	 * remote data correctly.
1425 	 *
1426 	 * If it's on the current CPU and the first expiring timer, then
1427 	 * skip reprogramming, keep the timer local and enforce
1428 	 * reprogramming later if it was the first expiring timer.  This
1429 	 * avoids programming the underlying clock event twice (once at
1430 	 * removal and once after enqueue).
1431 	 *
1432 	 * @keep_base is also true if the timer callback is running on a
1433 	 * remote CPU and for local pinned timers.
1434 	 */
1435 	if (likely(keep_base)) {
1436 		first = remove_and_enqueue_same_base(timer, base, mode, tim, delta_ns);
1437 	} else {
1438 		/* Keep the ENQUEUED state in case it is queued */
1439 		bool was_armed = remove_hrtimer(timer, base, HRTIMER_STATE_ENQUEUED);
1440 
1441 		hrtimer_set_expires_range_ns(timer, tim, delta_ns);
1442 
1443 		/* Switch the timer base, if necessary: */
1444 		base = switch_hrtimer_base(timer, base, is_pinned);
1445 		cpu_base = base->cpu_base;
1446 
1447 		first = enqueue_hrtimer(timer, base, mode, was_armed);
1448 	}
1449 
1450 	/* If a deferred rearm is pending skip reprogramming the device */
1451 	if (cpu_base->deferred_rearm) {
1452 		cpu_base->deferred_needs_update = true;
1453 		return HRTIMER_REPROGRAM_NONE;
1454 	}
1455 
1456 	if (!was_first || cpu_base != this_cpu_base) {
1457 		/*
1458 		 * If the current CPU base is online, then the timer is never
1459 		 * queued on a remote CPU if it would be the first expiring
1460 		 * timer there unless the timer callback is currently executed
1461 		 * on the remote CPU. In the latter case the remote CPU will
1462 		 * re-evaluate the first expiring timer after completing the
1463 		 * callbacks.
1464 		 */
1465 		if (likely(hrtimer_base_is_online(this_cpu_base)))
1466 			return first ? HRTIMER_REPROGRAM : HRTIMER_REPROGRAM_NONE;
1467 
1468 		/*
1469 		 * Timer was enqueued remote because the current base is
1470 		 * already offline. If the timer is the first to expire,
1471 		 * kick the remote CPU to reprogram the clock event.
1472 		 */
1473 		if (first)
1474 			smp_call_function_single_async(cpu_base->cpu, &cpu_base->csd);
1475 		return HRTIMER_REPROGRAM_NONE;
1476 	}
1477 
1478 	/*
1479 	 * Special case for the HRTICK timer. It is frequently rearmed and most
1480 	 * of the time moves the expiry into the future. That's expensive in
1481 	 * virtual machines and it's better to take the pointless already armed
1482 	 * interrupt than reprogramming the hardware on every context switch.
1483 	 *
1484 	 * If the new expiry is before the armed time, then reprogramming is
1485 	 * required.
1486 	 */
1487 	if (timer->is_lazy) {
1488 		if (cpu_base->expires_next <= hrtimer_get_expires(timer))
1489 			return HRTIMER_REPROGRAM_NONE;
1490 	}
1491 
1492 	/*
1493 	 * Timer was the first expiring timer and forced to stay on the
1494 	 * current CPU to avoid reprogramming on removal and enqueue. Force
1495 	 * reprogram the hardware by evaluating the new first expiring
1496 	 * timer.
1497 	 */
1498 	return HRTIMER_REPROGRAM_FORCE;
1499 }
1500 
hrtimer_start_range_ns_common(struct hrtimer * timer,ktime_t tim,u64 delta_ns,const enum hrtimer_mode mode,struct hrtimer_clock_base * base)1501 static int hrtimer_start_range_ns_common(struct hrtimer *timer, ktime_t tim,
1502 					 u64 delta_ns, const enum hrtimer_mode mode,
1503 					 struct hrtimer_clock_base *base)
1504 {
1505 	/*
1506 	 * Check whether the HRTIMER_MODE_SOFT bit and hrtimer.is_soft
1507 	 * match on CONFIG_PREEMPT_RT = n. With PREEMPT_RT check the hard
1508 	 * expiry mode because unmarked timers are moved to softirq expiry.
1509 	 */
1510 	if (!IS_ENABLED(CONFIG_PREEMPT_RT))
1511 		WARN_ON_ONCE(!(mode & HRTIMER_MODE_SOFT) ^ !timer->is_soft);
1512 	else
1513 		WARN_ON_ONCE(!(mode & HRTIMER_MODE_HARD) ^ !timer->is_hard);
1514 
1515 	return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, base);
1516 }
1517 
1518 /**
1519  * hrtimer_start_range_ns - (re)start an hrtimer
1520  * @timer:	the timer to be added
1521  * @tim:	expiry time
1522  * @delta_ns:	"slack" range for the timer
1523  * @mode:	timer mode: absolute (HRTIMER_MODE_ABS) or
1524  *		relative (HRTIMER_MODE_REL), and pinned (HRTIMER_MODE_PINNED);
1525  *		softirq based mode is considered for debug purpose only!
1526  */
hrtimer_start_range_ns(struct hrtimer * timer,ktime_t tim,u64 delta_ns,const enum hrtimer_mode mode)1527 void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, u64 delta_ns,
1528 			    const enum hrtimer_mode mode)
1529 {
1530 	struct hrtimer_clock_base *base;
1531 	unsigned long flags;
1532 
1533 	debug_hrtimer_assert_init(timer);
1534 
1535 	base = lock_hrtimer_base(timer, &flags);
1536 
1537 	switch (hrtimer_start_range_ns_common(timer, tim, delta_ns, mode, base)) {
1538 	case HRTIMER_REPROGRAM:
1539 		hrtimer_reprogram(timer, true);
1540 		break;
1541 	case HRTIMER_REPROGRAM_FORCE:
1542 		hrtimer_force_reprogram(timer->base->cpu_base, 1);
1543 		break;
1544 	case HRTIMER_REPROGRAM_NONE:
1545 		break;
1546 	}
1547 
1548 	unlock_hrtimer_base(timer, &flags);
1549 }
1550 EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
1551 
hrtimer_check_user_timer(struct hrtimer * timer)1552 static inline bool hrtimer_check_user_timer(struct hrtimer *timer)
1553 {
1554 	struct hrtimer_cpu_base *cpu_base = timer->base->cpu_base;
1555 	ktime_t expires;
1556 
1557 	/*
1558 	 * This uses soft expires because that's the user provided
1559 	 * expiry time, while expires can be further in the past
1560 	 * due to a slack value added to the user expiry time.
1561 	 */
1562 	expires = hrtimer_get_softexpires(timer);
1563 
1564 	/* Convert to monotonic */
1565 	expires = ktime_sub(expires, timer->base->offset);
1566 
1567 	/*
1568 	 * Check whether this timer will end up as the first expiring timer in
1569 	 * the CPU base. If not, no further checks required as it's then
1570 	 * guaranteed to expire in the future.
1571 	 */
1572 	if (expires >= cpu_base->expires_next)
1573 		return true;
1574 
1575 	/* Validate that the expiry time is in the future. */
1576 	if (expires > ktime_get())
1577 		return true;
1578 
1579 	debug_hrtimer_deactivate(timer);
1580 	__remove_hrtimer(timer, timer->base, HRTIMER_STATE_INACTIVE, false);
1581 	trace_hrtimer_start_expired(timer);
1582 	return false;
1583 }
1584 
1585 /**
1586  * hrtimer_start_range_ns_user - (re)start an user controlled hrtimer
1587  * @timer:	the timer to be added
1588  * @tim:	expiry time
1589  * @delta_ns:	"slack" range for the timer
1590  * @mode:	timer mode: absolute (HRTIMER_MODE_ABS) or
1591  *		relative (HRTIMER_MODE_REL), and pinned (HRTIMER_MODE_PINNED);
1592  *		softirq based mode is considered for debug purpose only!
1593  *
1594  * Returns: True when the timer was queued, false if it was already expired
1595  *
1596  * This function cannot invoke the timer callback for expired timers as it might
1597  * be called under a lock which the timer callback needs to acquire. So the
1598  * caller has to handle that case.
1599  */
hrtimer_start_range_ns_user(struct hrtimer * timer,ktime_t tim,u64 delta_ns,const enum hrtimer_mode mode)1600 bool hrtimer_start_range_ns_user(struct hrtimer *timer, ktime_t tim,
1601 				 u64 delta_ns, const enum hrtimer_mode mode)
1602 {
1603 	struct hrtimer_clock_base *base;
1604 	unsigned long flags;
1605 	bool ret = true;
1606 
1607 	debug_hrtimer_assert_init(timer);
1608 
1609 	base = lock_hrtimer_base(timer, &flags);
1610 
1611 	switch (hrtimer_start_range_ns_common(timer, tim, delta_ns, mode, base)) {
1612 	case HRTIMER_REPROGRAM:
1613 		ret = hrtimer_check_user_timer(timer);
1614 		if (ret)
1615 			hrtimer_reprogram(timer, true);
1616 		break;
1617 	case HRTIMER_REPROGRAM_FORCE:
1618 		ret = hrtimer_check_user_timer(timer);
1619 		/*
1620 		 * The base must always be reevaluated, independent of the
1621 		 * result above because the timer was the first pending timer.
1622 		 */
1623 		hrtimer_force_reprogram(timer->base->cpu_base, 1);
1624 		break;
1625 	case HRTIMER_REPROGRAM_NONE:
1626 		break;
1627 	}
1628 
1629 	unlock_hrtimer_base(timer, &flags);
1630 	return ret;
1631 }
1632 EXPORT_SYMBOL_GPL(hrtimer_start_range_ns_user);
1633 
1634 /**
1635  * hrtimer_try_to_cancel - try to deactivate a timer
1636  * @timer:	hrtimer to stop
1637  *
1638  * Returns:
1639  *
1640  *  *  0 when the timer was not active
1641  *  *  1 when the timer was active
1642  *  * -1 when the timer is currently executing the callback function and
1643  *    cannot be stopped
1644  */
hrtimer_try_to_cancel(struct hrtimer * timer)1645 int hrtimer_try_to_cancel(struct hrtimer *timer)
1646 {
1647 	struct hrtimer_clock_base *base;
1648 	unsigned long flags;
1649 	int ret = -1;
1650 
1651 	/*
1652 	 * Check lockless first. If the timer is not active (neither
1653 	 * enqueued nor running the callback, nothing to do here.  The
1654 	 * base lock does not serialize against a concurrent enqueue,
1655 	 * so we can avoid taking it.
1656 	 */
1657 	if (!hrtimer_active(timer))
1658 		return 0;
1659 
1660 	base = lock_hrtimer_base(timer, &flags);
1661 
1662 	if (!hrtimer_callback_running(timer)) {
1663 		ret = remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE);
1664 		if (ret)
1665 			trace_hrtimer_cancel(timer);
1666 	}
1667 
1668 	unlock_hrtimer_base(timer, &flags);
1669 
1670 	return ret;
1671 
1672 }
1673 EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
1674 
1675 #ifdef CONFIG_PREEMPT_RT
hrtimer_cpu_base_init_expiry_lock(struct hrtimer_cpu_base * base)1676 static void hrtimer_cpu_base_init_expiry_lock(struct hrtimer_cpu_base *base)
1677 {
1678 	spin_lock_init(&base->softirq_expiry_lock);
1679 }
1680 
hrtimer_cpu_base_lock_expiry(struct hrtimer_cpu_base * base)1681 static void hrtimer_cpu_base_lock_expiry(struct hrtimer_cpu_base *base)
1682 	__acquires(&base->softirq_expiry_lock)
1683 {
1684 	spin_lock(&base->softirq_expiry_lock);
1685 }
1686 
hrtimer_cpu_base_unlock_expiry(struct hrtimer_cpu_base * base)1687 static void hrtimer_cpu_base_unlock_expiry(struct hrtimer_cpu_base *base)
1688 	__releases(&base->softirq_expiry_lock)
1689 {
1690 	spin_unlock(&base->softirq_expiry_lock);
1691 }
1692 
1693 /*
1694  * The counterpart to hrtimer_cancel_wait_running().
1695  *
1696  * If there is a waiter for cpu_base->expiry_lock, then it was waiting for
1697  * the timer callback to finish. Drop expiry_lock and reacquire it. That
1698  * allows the waiter to acquire the lock and make progress.
1699  */
hrtimer_sync_wait_running(struct hrtimer_cpu_base * cpu_base,unsigned long flags)1700 static void hrtimer_sync_wait_running(struct hrtimer_cpu_base *cpu_base, unsigned long flags)
1701 {
1702 	if (atomic_read(&cpu_base->timer_waiters)) {
1703 		raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
1704 		spin_unlock(&cpu_base->softirq_expiry_lock);
1705 		spin_lock(&cpu_base->softirq_expiry_lock);
1706 		raw_spin_lock_irq(&cpu_base->lock);
1707 	}
1708 }
1709 
1710 #ifdef CONFIG_SMP
is_migration_base(struct hrtimer_clock_base * base)1711 static __always_inline bool is_migration_base(struct hrtimer_clock_base *base)
1712 {
1713 	return base == &migration_base;
1714 }
1715 #else
is_migration_base(struct hrtimer_clock_base * base)1716 static __always_inline bool is_migration_base(struct hrtimer_clock_base *base)
1717 {
1718 	return false;
1719 }
1720 #endif
1721 
1722 /*
1723  * This function is called on PREEMPT_RT kernels when the fast path
1724  * deletion of a timer failed because the timer callback function was
1725  * running.
1726  *
1727  * This prevents priority inversion: if the soft irq thread is preempted
1728  * in the middle of a timer callback, then calling hrtimer_cancel() can
1729  * lead to two issues:
1730  *
1731  *  - If the caller is on a remote CPU then it has to spin wait for the timer
1732  *    handler to complete. This can result in unbound priority inversion.
1733  *
1734  *  - If the caller originates from the task which preempted the timer
1735  *    handler on the same CPU, then spin waiting for the timer handler to
1736  *    complete is never going to end.
1737  */
hrtimer_cancel_wait_running(const struct hrtimer * timer)1738 void hrtimer_cancel_wait_running(const struct hrtimer *timer)
1739 {
1740 	/* Lockless read. Prevent the compiler from reloading it below */
1741 	struct hrtimer_clock_base *base = READ_ONCE(timer->base);
1742 
1743 	/*
1744 	 * Just relax if the timer expires in hard interrupt context or if
1745 	 * it is currently on the migration base.
1746 	 */
1747 	if (!timer->is_soft || is_migration_base(base)) {
1748 		cpu_relax();
1749 		return;
1750 	}
1751 
1752 	/*
1753 	 * Mark the base as contended and grab the expiry lock, which is
1754 	 * held by the softirq across the timer callback. Drop the lock
1755 	 * immediately so the softirq can expire the next timer. In theory
1756 	 * the timer could already be running again, but that's more than
1757 	 * unlikely and just causes another wait loop.
1758 	 */
1759 	atomic_inc(&base->cpu_base->timer_waiters);
1760 	spin_lock_bh(&base->cpu_base->softirq_expiry_lock);
1761 	atomic_dec(&base->cpu_base->timer_waiters);
1762 	spin_unlock_bh(&base->cpu_base->softirq_expiry_lock);
1763 }
1764 #else
hrtimer_cpu_base_init_expiry_lock(struct hrtimer_cpu_base * base)1765 static inline void hrtimer_cpu_base_init_expiry_lock(struct hrtimer_cpu_base *base) { }
hrtimer_cpu_base_lock_expiry(struct hrtimer_cpu_base * base)1766 static inline void hrtimer_cpu_base_lock_expiry(struct hrtimer_cpu_base *base) { }
hrtimer_cpu_base_unlock_expiry(struct hrtimer_cpu_base * base)1767 static inline void hrtimer_cpu_base_unlock_expiry(struct hrtimer_cpu_base *base) { }
hrtimer_sync_wait_running(struct hrtimer_cpu_base * base,unsigned long fl)1768 static inline void hrtimer_sync_wait_running(struct hrtimer_cpu_base *base, unsigned long fl) { }
1769 #endif
1770 
1771 /**
1772  * hrtimer_cancel - cancel a timer and wait for the handler to finish.
1773  * @timer:	the timer to be cancelled
1774  *
1775  * Returns:
1776  *  0 when the timer was not active
1777  *  1 when the timer was active
1778  */
hrtimer_cancel(struct hrtimer * timer)1779 int hrtimer_cancel(struct hrtimer *timer)
1780 {
1781 	int ret;
1782 
1783 	do {
1784 		ret = hrtimer_try_to_cancel(timer);
1785 
1786 		if (ret < 0)
1787 			hrtimer_cancel_wait_running(timer);
1788 	} while (ret < 0);
1789 	return ret;
1790 }
1791 EXPORT_SYMBOL_GPL(hrtimer_cancel);
1792 
1793 /**
1794  * __hrtimer_get_remaining - get remaining time for the timer
1795  * @timer:	the timer to read
1796  * @adjust:	adjust relative timers when CONFIG_TIME_LOW_RES=y
1797  */
__hrtimer_get_remaining(const struct hrtimer * timer,bool adjust)1798 ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust)
1799 {
1800 	unsigned long flags;
1801 	ktime_t rem;
1802 
1803 	lock_hrtimer_base(timer, &flags);
1804 	if (IS_ENABLED(CONFIG_TIME_LOW_RES) && adjust)
1805 		rem = hrtimer_expires_remaining_adjusted(timer);
1806 	else
1807 		rem = hrtimer_expires_remaining(timer);
1808 	unlock_hrtimer_base(timer, &flags);
1809 
1810 	return rem;
1811 }
1812 EXPORT_SYMBOL_GPL(__hrtimer_get_remaining);
1813 
1814 #ifdef CONFIG_NO_HZ_COMMON
1815 /**
1816  * hrtimer_get_next_event - get the time until next expiry event
1817  *
1818  * Returns the next expiry time or KTIME_MAX if no timer is pending.
1819  */
hrtimer_get_next_event(void)1820 ktime_t hrtimer_get_next_event(void)
1821 {
1822 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
1823 
1824 	/*
1825 	 * When HRES is active cmp_next_hrtimer_event() expects KTIME_MAX.
1826 	 *
1827 	 * cpu_base->hres_active is written only by the local CPU in
1828 	 * hrtimer_switch_to_hres() from hard interrupt context and in
1829 	 * hrtimers_cpu_starting() during CPU bring-up, and all callers reach
1830 	 * this with interrupts disabled on the same CPU, so an unlocked read is
1831 	 * stable without holding the lock.
1832 	 */
1833 	if (hrtimer_hres_active(cpu_base))
1834 		return KTIME_MAX;
1835 
1836 	guard(raw_spinlock_irqsave)(&cpu_base->lock);
1837 	return __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_ALL);
1838 }
1839 
1840 /**
1841  * hrtimer_next_event_without - time until next expiry event w/o one timer
1842  * @exclude:	timer to exclude
1843  *
1844  * Returns the next expiry time over all timers except for the @exclude one or
1845  * KTIME_MAX if none of them is pending.
1846  */
hrtimer_next_event_without(const struct hrtimer * exclude)1847 ktime_t hrtimer_next_event_without(const struct hrtimer *exclude)
1848 {
1849 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
1850 	ktime_t expires = KTIME_MAX;
1851 	unsigned int active;
1852 
1853 	guard(raw_spinlock_irqsave)(&cpu_base->lock);
1854 	if (!hrtimer_hres_active(cpu_base))
1855 		return expires;
1856 
1857 	active = cpu_base->active_bases & HRTIMER_ACTIVE_SOFT;
1858 	if (active && !cpu_base->softirq_activated)
1859 		expires = hrtimer_bases_next_event_without(cpu_base, exclude, active, KTIME_MAX);
1860 
1861 	active = cpu_base->active_bases & HRTIMER_ACTIVE_HARD;
1862 	if (!active)
1863 		return expires;
1864 	return hrtimer_bases_next_event_without(cpu_base, exclude, active, expires);
1865 }
1866 #endif
1867 
hrtimer_clockid_to_base(clockid_t clock_id)1868 static inline int hrtimer_clockid_to_base(clockid_t clock_id)
1869 {
1870 	switch (clock_id) {
1871 	case CLOCK_MONOTONIC:
1872 		return HRTIMER_BASE_MONOTONIC;
1873 	case CLOCK_REALTIME:
1874 		return HRTIMER_BASE_REALTIME;
1875 	case CLOCK_BOOTTIME:
1876 		return HRTIMER_BASE_BOOTTIME;
1877 	case CLOCK_TAI:
1878 		return HRTIMER_BASE_TAI;
1879 	default:
1880 		WARN(1, "Invalid clockid %d. Using MONOTONIC\n", clock_id);
1881 		return HRTIMER_BASE_MONOTONIC;
1882 	}
1883 }
1884 
__hrtimer_cb_get_time(clockid_t clock_id)1885 static ktime_t __hrtimer_cb_get_time(clockid_t clock_id)
1886 {
1887 	switch (clock_id) {
1888 	case CLOCK_MONOTONIC:
1889 		return ktime_get();
1890 	case CLOCK_REALTIME:
1891 		return ktime_get_real();
1892 	case CLOCK_BOOTTIME:
1893 		return ktime_get_boottime();
1894 	case CLOCK_TAI:
1895 		return ktime_get_clocktai();
1896 	default:
1897 		WARN(1, "Invalid clockid %d. Using MONOTONIC\n", clock_id);
1898 		return ktime_get();
1899 	}
1900 }
1901 
hrtimer_cb_get_time(const struct hrtimer * timer)1902 ktime_t hrtimer_cb_get_time(const struct hrtimer *timer)
1903 {
1904 	return __hrtimer_cb_get_time(timer->base->clockid);
1905 }
1906 EXPORT_SYMBOL_GPL(hrtimer_cb_get_time);
1907 
__hrtimer_setup(struct hrtimer * timer,enum hrtimer_restart (* fn)(struct hrtimer *),clockid_t clock_id,enum hrtimer_mode mode)1908 static void __hrtimer_setup(struct hrtimer *timer, enum hrtimer_restart (*fn)(struct hrtimer *),
1909 			    clockid_t clock_id, enum hrtimer_mode mode)
1910 {
1911 	bool softtimer = !!(mode & HRTIMER_MODE_SOFT);
1912 	struct hrtimer_cpu_base *cpu_base;
1913 	int base;
1914 
1915 	/*
1916 	 * On PREEMPT_RT enabled kernels hrtimers which are not explicitly
1917 	 * marked for hard interrupt expiry mode are moved into soft
1918 	 * interrupt context for latency reasons and because the callbacks
1919 	 * can invoke functions which might sleep on RT, e.g. spin_lock().
1920 	 */
1921 	if (IS_ENABLED(CONFIG_PREEMPT_RT) && !(mode & HRTIMER_MODE_HARD))
1922 		softtimer = true;
1923 
1924 	memset(timer, 0, sizeof(struct hrtimer));
1925 
1926 	cpu_base = raw_cpu_ptr(&hrtimer_bases);
1927 
1928 	/*
1929 	 * POSIX magic: Relative CLOCK_REALTIME timers are not affected by
1930 	 * clock modifications, so they needs to become CLOCK_MONOTONIC to
1931 	 * ensure POSIX compliance.
1932 	 */
1933 	if (clock_id == CLOCK_REALTIME && mode & HRTIMER_MODE_REL)
1934 		clock_id = CLOCK_MONOTONIC;
1935 
1936 	base = softtimer ? HRTIMER_MAX_CLOCK_BASES / 2 : 0;
1937 	base += hrtimer_clockid_to_base(clock_id);
1938 	timer->is_soft = softtimer;
1939 	timer->is_hard = !!(mode & HRTIMER_MODE_HARD);
1940 	timer->is_lazy = !!(mode & HRTIMER_MODE_LAZY_REARM);
1941 	timer->base = &cpu_base->clock_base[base];
1942 	timerqueue_linked_init(&timer->node);
1943 
1944 	if (WARN_ON_ONCE(!fn))
1945 		ACCESS_PRIVATE(timer, function) = hrtimer_dummy_timeout;
1946 	else
1947 		ACCESS_PRIVATE(timer, function) = fn;
1948 }
1949 
1950 /**
1951  * hrtimer_setup - initialize a timer to the given clock
1952  * @timer:	the timer to be initialized
1953  * @function:	the callback function
1954  * @clock_id:	the clock to be used
1955  * @mode:       The modes which are relevant for initialization:
1956  *              HRTIMER_MODE_ABS, HRTIMER_MODE_REL, HRTIMER_MODE_ABS_SOFT,
1957  *              HRTIMER_MODE_REL_SOFT
1958  *
1959  *              The PINNED variants of the above can be handed in,
1960  *              but the PINNED bit is ignored as pinning happens
1961  *              when the hrtimer is started
1962  */
hrtimer_setup(struct hrtimer * timer,enum hrtimer_restart (* function)(struct hrtimer *),clockid_t clock_id,enum hrtimer_mode mode)1963 void hrtimer_setup(struct hrtimer *timer, enum hrtimer_restart (*function)(struct hrtimer *),
1964 		   clockid_t clock_id, enum hrtimer_mode mode)
1965 {
1966 	debug_setup(timer, clock_id, mode);
1967 	__hrtimer_setup(timer, function, clock_id, mode);
1968 }
1969 EXPORT_SYMBOL_GPL(hrtimer_setup);
1970 
1971 /**
1972  * hrtimer_setup_on_stack - initialize a timer on stack memory
1973  * @timer:	The timer to be initialized
1974  * @function:	the callback function
1975  * @clock_id:	The clock to be used
1976  * @mode:       The timer mode
1977  *
1978  * Similar to hrtimer_setup(), except that this one must be used if struct hrtimer is in stack
1979  * memory.
1980  */
hrtimer_setup_on_stack(struct hrtimer * timer,enum hrtimer_restart (* function)(struct hrtimer *),clockid_t clock_id,enum hrtimer_mode mode)1981 void hrtimer_setup_on_stack(struct hrtimer *timer,
1982 			    enum hrtimer_restart (*function)(struct hrtimer *),
1983 			    clockid_t clock_id, enum hrtimer_mode mode)
1984 {
1985 	debug_setup_on_stack(timer, clock_id, mode);
1986 	__hrtimer_setup(timer, function, clock_id, mode);
1987 }
1988 EXPORT_SYMBOL_GPL(hrtimer_setup_on_stack);
1989 
1990 /*
1991  * A timer is active, when it is enqueued into the rbtree or the
1992  * callback function is running or it's in the state of being migrated
1993  * to another cpu.
1994  *
1995  * It is important for this function to not return a false negative.
1996  */
hrtimer_active(const struct hrtimer * timer)1997 bool hrtimer_active(const struct hrtimer *timer)
1998 {
1999 	struct hrtimer_clock_base *base;
2000 	unsigned int seq;
2001 
2002 	do {
2003 		base = READ_ONCE(timer->base);
2004 		seq = raw_read_seqcount_begin(&base->seq);
2005 
2006 		if (timer->is_queued || base->running == timer)
2007 			return true;
2008 
2009 	} while (read_seqcount_retry(&base->seq, seq) || base != READ_ONCE(timer->base));
2010 
2011 	return false;
2012 }
2013 EXPORT_SYMBOL_GPL(hrtimer_active);
2014 
2015 /*
2016  * The write_seqcount_barrier()s in __run_hrtimer() split the thing into 3
2017  * distinct sections:
2018  *
2019  *  - queued:	the timer is queued
2020  *  - callback:	the timer is being ran
2021  *  - post:	the timer is inactive or (re)queued
2022  *
2023  * On the read side we ensure we observe timer->is_queued and cpu_base->running
2024  * from the same section, if anything changed while we looked at it, we retry.
2025  * This includes timer->base changing because sequence numbers alone are
2026  * insufficient for that.
2027  *
2028  * The sequence numbers are required because otherwise we could still observe
2029  * a false negative if the read side got smeared over multiple consecutive
2030  * __run_hrtimer() invocations.
2031  */
__run_hrtimer(struct hrtimer_cpu_base * cpu_base,struct hrtimer_clock_base * base,struct hrtimer * timer,ktime_t now,unsigned long flags)2032 static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base, struct hrtimer_clock_base *base,
2033 			  struct hrtimer *timer, ktime_t now, unsigned long flags)
2034 	__must_hold(&cpu_base->lock)
2035 {
2036 	enum hrtimer_restart (*fn)(struct hrtimer *);
2037 	bool expires_in_hardirq;
2038 	int restart;
2039 
2040 	lockdep_assert_held(&cpu_base->lock);
2041 
2042 	debug_hrtimer_deactivate(timer);
2043 	base->running = timer;
2044 
2045 	/*
2046 	 * Separate the ->running assignment from the ->is_queued assignment.
2047 	 *
2048 	 * As with a regular write barrier, this ensures the read side in
2049 	 * hrtimer_active() cannot observe base->running == NULL &&
2050 	 * timer->is_queued == INACTIVE.
2051 	 */
2052 	raw_write_seqcount_barrier(&base->seq);
2053 
2054 	__remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, false);
2055 	fn = ACCESS_PRIVATE(timer, function);
2056 
2057 	/*
2058 	 * Clear the 'is relative' flag for the TIME_LOW_RES case. If the
2059 	 * timer is restarted with a period then it becomes an absolute
2060 	 * timer. If its not restarted it does not matter.
2061 	 */
2062 	if (IS_ENABLED(CONFIG_TIME_LOW_RES))
2063 		timer->is_rel = false;
2064 
2065 	/*
2066 	 * The timer is marked as running in the CPU base, so it is
2067 	 * protected against migration to a different CPU even if the lock
2068 	 * is dropped.
2069 	 */
2070 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
2071 	trace_hrtimer_expire_entry(timer, now);
2072 	expires_in_hardirq = lockdep_hrtimer_enter(timer);
2073 
2074 	restart = fn(timer);
2075 
2076 	lockdep_hrtimer_exit(expires_in_hardirq);
2077 	trace_hrtimer_expire_exit(timer);
2078 	raw_spin_lock_irq(&cpu_base->lock);
2079 
2080 	/*
2081 	 * Note: We clear the running state after enqueue_hrtimer and
2082 	 * we do not reprogram the event hardware. Happens either in
2083 	 * hrtimer_start_range_ns() or in hrtimer_interrupt()
2084 	 *
2085 	 * Note: Because we dropped the cpu_base->lock above,
2086 	 * hrtimer_start_range_ns() can have popped in and enqueued the timer
2087 	 * for us already.
2088 	 */
2089 	if (restart == HRTIMER_RESTART && !timer->is_queued)
2090 		enqueue_hrtimer(timer, base, HRTIMER_MODE_ABS, false);
2091 
2092 	/*
2093 	 * Separate the ->running assignment from the ->is_queued assignment.
2094 	 *
2095 	 * As with a regular write barrier, this ensures the read side in
2096 	 * hrtimer_active() cannot observe base->running.timer == NULL &&
2097 	 * timer->is_queued == INACTIVE.
2098 	 */
2099 	raw_write_seqcount_barrier(&base->seq);
2100 
2101 	WARN_ON_ONCE(base->running != timer);
2102 	base->running = NULL;
2103 }
2104 
__hrtimer_run_queues(struct hrtimer_cpu_base * cpu_base,ktime_t now,unsigned long flags,unsigned int active_mask)2105 static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now,
2106 				 unsigned long flags, unsigned int active_mask)
2107 {
2108 	unsigned int active = cpu_base->active_bases & active_mask;
2109 	struct hrtimer_clock_base *base;
2110 
2111 	for_each_active_base(base, cpu_base, active) {
2112 		ktime_t basenow = ktime_add(now, base->offset);
2113 		struct hrtimer *timer;
2114 
2115 		while ((timer = clock_base_next_timer(base))) {
2116 			/*
2117 			 * The immediate goal for using the softexpires is
2118 			 * minimizing wakeups, not running timers at the
2119 			 * earliest interrupt after their soft expiration.
2120 			 * This allows us to avoid using a Priority Search
2121 			 * Tree, which can answer a stabbing query for
2122 			 * overlapping intervals and instead use the simple
2123 			 * BST we already have.
2124 			 * We don't add extra wakeups by delaying timers that
2125 			 * are right-of a not yet expired timer, because that
2126 			 * timer will have to trigger a wakeup anyway.
2127 			 */
2128 			if (basenow < hrtimer_get_softexpires(timer))
2129 				break;
2130 
2131 			__run_hrtimer(cpu_base, base, timer, basenow, flags);
2132 			if (active_mask == HRTIMER_ACTIVE_SOFT)
2133 				hrtimer_sync_wait_running(cpu_base, flags);
2134 		}
2135 	}
2136 }
2137 
hrtimer_run_softirq(void)2138 static __latent_entropy void hrtimer_run_softirq(void)
2139 {
2140 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
2141 	unsigned long flags;
2142 	ktime_t now;
2143 
2144 	hrtimer_cpu_base_lock_expiry(cpu_base);
2145 	raw_spin_lock_irqsave(&cpu_base->lock, flags);
2146 
2147 	now = hrtimer_update_base(cpu_base);
2148 	__hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_SOFT);
2149 
2150 	cpu_base->softirq_activated = false;
2151 	hrtimer_update_softirq_timer(cpu_base, true);
2152 
2153 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
2154 	hrtimer_cpu_base_unlock_expiry(cpu_base);
2155 }
2156 
2157 #ifdef CONFIG_HIGH_RES_TIMERS
2158 
2159 /*
2160  * Very similar to hrtimer_force_reprogram(), except it deals with
2161  * deferred_rearm and hang_detected.
2162  */
hrtimer_rearm(struct hrtimer_cpu_base * cpu_base,ktime_t expires_next,bool deferred)2163 static void hrtimer_rearm(struct hrtimer_cpu_base *cpu_base, ktime_t expires_next, bool deferred)
2164 {
2165 	cpu_base->expires_next = expires_next;
2166 	cpu_base->deferred_rearm = false;
2167 
2168 	if (unlikely(cpu_base->hang_detected)) {
2169 		/*
2170 		 * Give the system a chance to do something else than looping
2171 		 * on hrtimer interrupts.
2172 		 */
2173 		expires_next = ktime_add_ns(ktime_get(),
2174 					    min(100 * NSEC_PER_MSEC, cpu_base->max_hang_time));
2175 	}
2176 	hrtimer_rearm_event(expires_next, deferred);
2177 }
2178 
2179 #ifdef CONFIG_HRTIMER_REARM_DEFERRED
__hrtimer_rearm_deferred(void)2180 void __hrtimer_rearm_deferred(void)
2181 {
2182 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
2183 	ktime_t expires_next;
2184 
2185 	if (!cpu_base->deferred_rearm)
2186 		return;
2187 
2188 	guard(raw_spinlock)(&cpu_base->lock);
2189 	if (cpu_base->deferred_needs_update) {
2190 		hrtimer_update_base(cpu_base);
2191 		expires_next = hrtimer_update_next_event(cpu_base);
2192 	} else {
2193 		/* No timer added/removed. Use the cached value */
2194 		expires_next = cpu_base->deferred_expires_next;
2195 	}
2196 	hrtimer_rearm(cpu_base, expires_next, true);
2197 }
2198 
2199 static __always_inline void
hrtimer_interrupt_rearm(struct hrtimer_cpu_base * cpu_base,ktime_t expires_next)2200 hrtimer_interrupt_rearm(struct hrtimer_cpu_base *cpu_base, ktime_t expires_next)
2201 {
2202 	/* hrtimer_interrupt() just re-evaluated the first expiring timer */
2203 	cpu_base->deferred_needs_update = false;
2204 	/* Cache the expiry time */
2205 	cpu_base->deferred_expires_next = expires_next;
2206 	set_thread_flag(TIF_HRTIMER_REARM);
2207 }
2208 #else  /* CONFIG_HRTIMER_REARM_DEFERRED */
2209 static __always_inline void
hrtimer_interrupt_rearm(struct hrtimer_cpu_base * cpu_base,ktime_t expires_next)2210 hrtimer_interrupt_rearm(struct hrtimer_cpu_base *cpu_base, ktime_t expires_next)
2211 {
2212 	hrtimer_rearm(cpu_base, expires_next, false);
2213 }
2214 #endif  /* !CONFIG_HRTIMER_REARM_DEFERRED */
2215 
2216 /*
2217  * High resolution timer interrupt
2218  * Called with interrupts disabled
2219  */
hrtimer_interrupt(struct clock_event_device * dev)2220 void hrtimer_interrupt(struct clock_event_device *dev)
2221 {
2222 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
2223 	ktime_t expires_next, now, entry_time, delta;
2224 	unsigned long flags;
2225 	int retries = 0;
2226 
2227 	BUG_ON(!cpu_base->hres_active);
2228 	cpu_base->nr_events++;
2229 	dev->next_event = KTIME_MAX;
2230 	dev->next_event_forced = 0;
2231 
2232 	raw_spin_lock_irqsave(&cpu_base->lock, flags);
2233 	entry_time = now = hrtimer_update_base(cpu_base);
2234 retry:
2235 	cpu_base->deferred_rearm = true;
2236 	/*
2237 	 * Set expires_next to KTIME_MAX, which prevents that remote CPUs queue
2238 	 * timers while __hrtimer_run_queues() is expiring the clock bases.
2239 	 * Timers which are re/enqueued on the local CPU are not affected by
2240 	 * this.
2241 	 */
2242 	cpu_base->expires_next = KTIME_MAX;
2243 
2244 	if (!ktime_before(now, cpu_base->softirq_expires_next)) {
2245 		cpu_base->softirq_expires_next = KTIME_MAX;
2246 		cpu_base->softirq_activated = true;
2247 		raise_timer_softirq(HRTIMER_SOFTIRQ);
2248 	}
2249 
2250 	__hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_HARD);
2251 
2252 	/*
2253 	 * The next timer was already expired due to:
2254 	 * - tracing
2255 	 * - long lasting callbacks
2256 	 * - being scheduled away when running in a VM
2257 	 *
2258 	 * We need to prevent that we loop forever in the hrtiner interrupt
2259 	 * routine. We give it 3 attempts to avoid overreacting on some
2260 	 * spurious event.
2261 	 */
2262 	now = hrtimer_update_base(cpu_base);
2263 	expires_next = hrtimer_update_next_event(cpu_base);
2264 	cpu_base->hang_detected = false;
2265 	if (expires_next < now) {
2266 		if (++retries < 3) {
2267 			cpu_base->nr_retries++;
2268 			goto retry;
2269 		}
2270 
2271 		delta = ktime_sub(now, entry_time);
2272 		cpu_base->max_hang_time = max_t(unsigned int, cpu_base->max_hang_time, delta);
2273 		cpu_base->nr_hangs++;
2274 		cpu_base->hang_detected = true;
2275 	}
2276 
2277 	hrtimer_interrupt_rearm(cpu_base, expires_next);
2278 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
2279 }
2280 
2281 #endif /* !CONFIG_HIGH_RES_TIMERS */
2282 
2283 /*
2284  * Called from run_local_timers in hardirq context every jiffy
2285  */
hrtimer_run_queues(void)2286 void hrtimer_run_queues(void)
2287 {
2288 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
2289 	unsigned long flags;
2290 	ktime_t now;
2291 
2292 	if (hrtimer_hres_active(cpu_base))
2293 		return;
2294 
2295 	/*
2296 	 * This _is_ ugly: We have to check periodically, whether we
2297 	 * can switch to highres and / or nohz mode. The clocksource
2298 	 * switch happens with xtime_lock held. Notification from
2299 	 * there only sets the check bit in the tick_oneshot code,
2300 	 * otherwise we might deadlock vs. xtime_lock.
2301 	 */
2302 	if (tick_check_oneshot_change(!hrtimer_is_hres_enabled())) {
2303 		hrtimer_switch_to_hres();
2304 		return;
2305 	}
2306 
2307 	raw_spin_lock_irqsave(&cpu_base->lock, flags);
2308 	now = hrtimer_update_base(cpu_base);
2309 
2310 	if (!ktime_before(now, cpu_base->softirq_expires_next)) {
2311 		cpu_base->softirq_expires_next = KTIME_MAX;
2312 		cpu_base->softirq_activated = true;
2313 		raise_timer_softirq(HRTIMER_SOFTIRQ);
2314 	}
2315 
2316 	__hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_HARD);
2317 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
2318 }
2319 
2320 /*
2321  * Sleep related functions:
2322  */
hrtimer_wakeup(struct hrtimer * timer)2323 static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
2324 {
2325 	struct hrtimer_sleeper *t = container_of(timer, struct hrtimer_sleeper, timer);
2326 	struct task_struct *task = t->task;
2327 
2328 	t->task = NULL;
2329 	if (task)
2330 		wake_up_process(task);
2331 
2332 	return HRTIMER_NORESTART;
2333 }
2334 
2335 /**
2336  * hrtimer_sleeper_start_expires - Start a hrtimer sleeper timer
2337  * @sl:		sleeper to be started
2338  * @mode:	timer mode abs/rel
2339  *
2340  * Wrapper around hrtimer_start_expires() for hrtimer_sleeper based timers
2341  * to allow PREEMPT_RT to tweak the delivery mode (soft/hardirq context)
2342  */
hrtimer_sleeper_start_expires(struct hrtimer_sleeper * sl,enum hrtimer_mode mode)2343 void hrtimer_sleeper_start_expires(struct hrtimer_sleeper *sl, enum hrtimer_mode mode)
2344 {
2345 	/*
2346 	 * Make the enqueue delivery mode check work on RT. If the sleeper
2347 	 * was initialized for hard interrupt delivery, force the mode bit.
2348 	 * This is a special case for hrtimer_sleepers because
2349 	 * __hrtimer_setup_sleeper() determines the delivery mode on RT so the
2350 	 * fiddling with this decision is avoided at the call sites.
2351 	 */
2352 	if (IS_ENABLED(CONFIG_PREEMPT_RT) && sl->timer.is_hard)
2353 		mode |= HRTIMER_MODE_HARD;
2354 
2355 	/* If already expired, clear the task pointer and set current state to running */
2356 	if (!hrtimer_start_expires_user(&sl->timer, mode)) {
2357 		sl->task = NULL;
2358 		__set_current_state(TASK_RUNNING);
2359 	}
2360 }
2361 EXPORT_SYMBOL_GPL(hrtimer_sleeper_start_expires);
2362 
__hrtimer_setup_sleeper(struct hrtimer_sleeper * sl,clockid_t clock_id,enum hrtimer_mode mode)2363 static void __hrtimer_setup_sleeper(struct hrtimer_sleeper *sl, clockid_t clock_id,
2364 				    enum hrtimer_mode mode)
2365 {
2366 	/*
2367 	 * On PREEMPT_RT enabled kernels hrtimers which are not explicitly
2368 	 * marked for hard interrupt expiry mode are moved into soft
2369 	 * interrupt context either for latency reasons or because the
2370 	 * hrtimer callback takes regular spinlocks or invokes other
2371 	 * functions which are not suitable for hard interrupt context on
2372 	 * PREEMPT_RT.
2373 	 *
2374 	 * The hrtimer_sleeper callback is RT compatible in hard interrupt
2375 	 * context, but there is a latency concern: Untrusted userspace can
2376 	 * spawn many threads which arm timers for the same expiry time on
2377 	 * the same CPU. That causes a latency spike due to the wakeup of
2378 	 * a gazillion threads.
2379 	 *
2380 	 * OTOH, privileged real-time user space applications rely on the
2381 	 * low latency of hard interrupt wakeups. If the current task is in
2382 	 * a real-time scheduling class, mark the mode for hard interrupt
2383 	 * expiry.
2384 	 */
2385 	if (IS_ENABLED(CONFIG_PREEMPT_RT)) {
2386 		if (rt_or_dl_task_policy(current) && !(mode & HRTIMER_MODE_SOFT))
2387 			mode |= HRTIMER_MODE_HARD;
2388 	}
2389 
2390 	__hrtimer_setup(&sl->timer, hrtimer_wakeup, clock_id, mode);
2391 	sl->task = current;
2392 }
2393 
2394 /**
2395  * hrtimer_setup_sleeper_on_stack - initialize a sleeper in stack memory
2396  * @sl:		sleeper to be initialized
2397  * @clock_id:	the clock to be used
2398  * @mode:	timer mode abs/rel
2399  */
hrtimer_setup_sleeper_on_stack(struct hrtimer_sleeper * sl,clockid_t clock_id,enum hrtimer_mode mode)2400 void hrtimer_setup_sleeper_on_stack(struct hrtimer_sleeper *sl, clockid_t clock_id,
2401 				    enum hrtimer_mode mode)
2402 {
2403 	debug_setup_on_stack(&sl->timer, clock_id, mode);
2404 	__hrtimer_setup_sleeper(sl, clock_id, mode);
2405 }
2406 EXPORT_SYMBOL_GPL(hrtimer_setup_sleeper_on_stack);
2407 
nanosleep_copyout(struct restart_block * restart,struct timespec64 * ts)2408 int nanosleep_copyout(struct restart_block *restart, struct timespec64 *ts)
2409 {
2410 	switch(restart->nanosleep.type) {
2411 #ifdef CONFIG_COMPAT_32BIT_TIME
2412 	case TT_COMPAT:
2413 		if (put_old_timespec32(ts, restart->nanosleep.compat_rmtp))
2414 			return -EFAULT;
2415 		break;
2416 #endif
2417 	case TT_NATIVE:
2418 		if (put_timespec64(ts, restart->nanosleep.rmtp))
2419 			return -EFAULT;
2420 		break;
2421 	default:
2422 		BUG();
2423 	}
2424 	return -ERESTART_RESTARTBLOCK;
2425 }
2426 
do_nanosleep(struct hrtimer_sleeper * t,enum hrtimer_mode mode)2427 static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
2428 {
2429 	struct restart_block *restart;
2430 
2431 	do {
2432 		set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
2433 		hrtimer_sleeper_start_expires(t, mode);
2434 
2435 		if (likely(t->task))
2436 			schedule();
2437 
2438 		hrtimer_cancel(&t->timer);
2439 		mode = HRTIMER_MODE_ABS;
2440 
2441 	} while (t->task && !signal_pending(current));
2442 
2443 	__set_current_state(TASK_RUNNING);
2444 
2445 	if (!t->task)
2446 		return 0;
2447 
2448 	restart = &current->restart_block;
2449 	if (restart->nanosleep.type != TT_NONE) {
2450 		ktime_t rem = hrtimer_expires_remaining(&t->timer);
2451 		struct timespec64 rmt;
2452 
2453 		if (rem <= 0)
2454 			return 0;
2455 		rmt = ktime_to_timespec64(rem);
2456 
2457 		return nanosleep_copyout(restart, &rmt);
2458 	}
2459 	return -ERESTART_RESTARTBLOCK;
2460 }
2461 
hrtimer_nanosleep_restart(struct restart_block * restart)2462 static long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
2463 {
2464 	struct hrtimer_sleeper t;
2465 	int ret;
2466 
2467 	hrtimer_setup_sleeper_on_stack(&t, restart->nanosleep.clockid, HRTIMER_MODE_ABS);
2468 	hrtimer_set_expires(&t.timer, restart->nanosleep.expires);
2469 	ret = do_nanosleep(&t, HRTIMER_MODE_ABS);
2470 	destroy_hrtimer_on_stack(&t.timer);
2471 	return ret;
2472 }
2473 
hrtimer_nanosleep(ktime_t rqtp,const enum hrtimer_mode mode,const clockid_t clockid)2474 long hrtimer_nanosleep(ktime_t rqtp, const enum hrtimer_mode mode, const clockid_t clockid)
2475 {
2476 	struct restart_block *restart;
2477 	struct hrtimer_sleeper t;
2478 	int ret;
2479 
2480 	hrtimer_setup_sleeper_on_stack(&t, clockid, mode);
2481 	hrtimer_set_expires_range_ns(&t.timer, rqtp, current->timer_slack_ns);
2482 	ret = do_nanosleep(&t, mode);
2483 	if (ret != -ERESTART_RESTARTBLOCK)
2484 		goto out;
2485 
2486 	/* Absolute timers do not update the rmtp value and restart: */
2487 	if (mode == HRTIMER_MODE_ABS) {
2488 		ret = -ERESTARTNOHAND;
2489 		goto out;
2490 	}
2491 
2492 	restart = &current->restart_block;
2493 	restart->nanosleep.clockid = t.timer.base->clockid;
2494 	restart->nanosleep.expires = hrtimer_get_expires(&t.timer);
2495 	set_restart_fn(restart, hrtimer_nanosleep_restart);
2496 out:
2497 	destroy_hrtimer_on_stack(&t.timer);
2498 	return ret;
2499 }
2500 
2501 #ifdef CONFIG_64BIT
2502 
SYSCALL_DEFINE2(nanosleep,struct __kernel_timespec __user *,rqtp,struct __kernel_timespec __user *,rmtp)2503 SYSCALL_DEFINE2(nanosleep, struct __kernel_timespec __user *, rqtp,
2504 		struct __kernel_timespec __user *, rmtp)
2505 {
2506 	struct timespec64 tu;
2507 
2508 	if (get_timespec64(&tu, rqtp))
2509 		return -EFAULT;
2510 
2511 	if (!timespec64_valid(&tu))
2512 		return -EINVAL;
2513 
2514 	current->restart_block.fn = do_no_restart_syscall;
2515 	current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
2516 	current->restart_block.nanosleep.rmtp = rmtp;
2517 	return hrtimer_nanosleep(timespec64_to_ktime(tu), HRTIMER_MODE_REL, CLOCK_MONOTONIC);
2518 }
2519 
2520 #endif
2521 
2522 #ifdef CONFIG_COMPAT_32BIT_TIME
2523 
SYSCALL_DEFINE2(nanosleep_time32,struct old_timespec32 __user *,rqtp,struct old_timespec32 __user *,rmtp)2524 SYSCALL_DEFINE2(nanosleep_time32, struct old_timespec32 __user *, rqtp,
2525 		struct old_timespec32 __user *, rmtp)
2526 {
2527 	struct timespec64 tu;
2528 
2529 	if (get_old_timespec32(&tu, rqtp))
2530 		return -EFAULT;
2531 
2532 	if (!timespec64_valid(&tu))
2533 		return -EINVAL;
2534 
2535 	current->restart_block.fn = do_no_restart_syscall;
2536 	current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
2537 	current->restart_block.nanosleep.compat_rmtp = rmtp;
2538 	return hrtimer_nanosleep(timespec64_to_ktime(tu), HRTIMER_MODE_REL, CLOCK_MONOTONIC);
2539 }
2540 #endif
2541 
2542 /*
2543  * Functions related to boot-time initialization:
2544  */
hrtimers_prepare_cpu(unsigned int cpu)2545 int hrtimers_prepare_cpu(unsigned int cpu)
2546 {
2547 	struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
2548 
2549 	for (int i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
2550 		struct hrtimer_clock_base *clock_b = &cpu_base->clock_base[i];
2551 
2552 		clock_b->cpu_base = cpu_base;
2553 		seqcount_raw_spinlock_init(&clock_b->seq, &cpu_base->lock);
2554 		timerqueue_linked_init_head(&clock_b->active);
2555 	}
2556 
2557 	cpu_base->cpu = cpu;
2558 	hrtimer_cpu_base_init_expiry_lock(cpu_base);
2559 	return 0;
2560 }
2561 
hrtimers_cpu_starting(unsigned int cpu)2562 int hrtimers_cpu_starting(unsigned int cpu)
2563 {
2564 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
2565 
2566 	/* Clear out any left over state from a CPU down operation */
2567 	cpu_base->active_bases = 0;
2568 	cpu_base->hres_active = false;
2569 	cpu_base->hang_detected = false;
2570 	cpu_base->next_timer = NULL;
2571 	cpu_base->softirq_next_timer = NULL;
2572 	cpu_base->expires_next = KTIME_MAX;
2573 	cpu_base->softirq_expires_next = KTIME_MAX;
2574 	cpu_base->softirq_activated = false;
2575 	cpu_base->online = true;
2576 	return 0;
2577 }
2578 
2579 #ifdef CONFIG_HOTPLUG_CPU
2580 
migrate_hrtimer_list(struct hrtimer_clock_base * old_base,struct hrtimer_clock_base * new_base)2581 static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
2582 				struct hrtimer_clock_base *new_base)
2583 {
2584 	struct timerqueue_linked_node *node;
2585 	struct hrtimer *timer;
2586 
2587 	while ((node = timerqueue_linked_first(&old_base->active))) {
2588 		timer = hrtimer_from_timerqueue_node(node);
2589 		BUG_ON(hrtimer_callback_running(timer));
2590 		debug_hrtimer_deactivate(timer);
2591 
2592 		/*
2593 		 * Mark it as ENQUEUED not INACTIVE otherwise the
2594 		 * timer could be seen as !active and just vanish away
2595 		 * under us on another CPU
2596 		 */
2597 		__remove_hrtimer(timer, old_base, HRTIMER_STATE_ENQUEUED, false);
2598 		timer->base = new_base;
2599 		/*
2600 		 * Enqueue the timers on the new cpu. This does not
2601 		 * reprogram the event device in case the timer
2602 		 * expires before the earliest on this CPU, but we run
2603 		 * hrtimer_interrupt after we migrated everything to
2604 		 * sort out already expired timers and reprogram the
2605 		 * event device.
2606 		 */
2607 		enqueue_hrtimer(timer, new_base, HRTIMER_MODE_ABS, true);
2608 	}
2609 }
2610 
hrtimers_cpu_dying(unsigned int dying_cpu)2611 int hrtimers_cpu_dying(unsigned int dying_cpu)
2612 {
2613 	int ncpu = cpumask_any_and(cpu_active_mask, housekeeping_cpumask(HK_TYPE_TIMER));
2614 	struct hrtimer_cpu_base *old_base, *new_base;
2615 
2616 	old_base = this_cpu_ptr(&hrtimer_bases);
2617 	new_base = &per_cpu(hrtimer_bases, ncpu);
2618 
2619 	/*
2620 	 * The caller is globally serialized and nobody else
2621 	 * takes two locks at once, deadlock is not possible.
2622 	 */
2623 	raw_spin_lock(&old_base->lock);
2624 	raw_spin_lock_nested(&new_base->lock, SINGLE_DEPTH_NESTING);
2625 
2626 	for (int i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
2627 		migrate_hrtimer_list(&old_base->clock_base[i], &new_base->clock_base[i]);
2628 
2629 	/* Tell the other CPU to retrigger the next event */
2630 	smp_call_function_single(ncpu, retrigger_next_event, NULL, 0);
2631 
2632 	raw_spin_unlock(&new_base->lock);
2633 	old_base->online = false;
2634 	raw_spin_unlock(&old_base->lock);
2635 
2636 	return 0;
2637 }
2638 
2639 #endif /* CONFIG_HOTPLUG_CPU */
2640 
hrtimers_init(void)2641 void __init hrtimers_init(void)
2642 {
2643 	hrtimers_prepare_cpu(smp_processor_id());
2644 	hrtimers_cpu_starting(smp_processor_id());
2645 	open_softirq(HRTIMER_SOFTIRQ, hrtimer_run_softirq);
2646 }
2647