• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  hrtimers - High-resolution kernel timers
4  *
5  *   Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de>
6  *   Copyright(C) 2005, Red Hat, Inc., Ingo Molnar
7  *
8  *  data type definitions, declarations, prototypes
9  *
10  *  Started by: Thomas Gleixner and Ingo Molnar
11  */
12 #ifndef _LINUX_HRTIMER_H
13 #define _LINUX_HRTIMER_H
14 
15 #include <linux/hrtimer_defs.h>
16 #include <linux/rbtree.h>
17 #include <linux/init.h>
18 #include <linux/list.h>
19 #include <linux/percpu.h>
20 #include <linux/seqlock.h>
21 #include <linux/timer.h>
22 #include <linux/timerqueue.h>
23 #include <linux/android_kabi.h>
24 
25 struct hrtimer_clock_base;
26 struct hrtimer_cpu_base;
27 
28 /*
29  * Mode arguments of xxx_hrtimer functions:
30  *
31  * HRTIMER_MODE_ABS		- Time value is absolute
32  * HRTIMER_MODE_REL		- Time value is relative to now
33  * HRTIMER_MODE_PINNED		- Timer is bound to CPU (is only considered
34  *				  when starting the timer)
35  * HRTIMER_MODE_SOFT		- Timer callback function will be executed in
36  *				  soft irq context
37  * HRTIMER_MODE_HARD		- Timer callback function will be executed in
38  *				  hard irq context even on PREEMPT_RT.
39  */
40 enum hrtimer_mode {
41 	HRTIMER_MODE_ABS	= 0x00,
42 	HRTIMER_MODE_REL	= 0x01,
43 	HRTIMER_MODE_PINNED	= 0x02,
44 	HRTIMER_MODE_SOFT	= 0x04,
45 	HRTIMER_MODE_HARD	= 0x08,
46 
47 	HRTIMER_MODE_ABS_PINNED = HRTIMER_MODE_ABS | HRTIMER_MODE_PINNED,
48 	HRTIMER_MODE_REL_PINNED = HRTIMER_MODE_REL | HRTIMER_MODE_PINNED,
49 
50 	HRTIMER_MODE_ABS_SOFT	= HRTIMER_MODE_ABS | HRTIMER_MODE_SOFT,
51 	HRTIMER_MODE_REL_SOFT	= HRTIMER_MODE_REL | HRTIMER_MODE_SOFT,
52 
53 	HRTIMER_MODE_ABS_PINNED_SOFT = HRTIMER_MODE_ABS_PINNED | HRTIMER_MODE_SOFT,
54 	HRTIMER_MODE_REL_PINNED_SOFT = HRTIMER_MODE_REL_PINNED | HRTIMER_MODE_SOFT,
55 
56 	HRTIMER_MODE_ABS_HARD	= HRTIMER_MODE_ABS | HRTIMER_MODE_HARD,
57 	HRTIMER_MODE_REL_HARD	= HRTIMER_MODE_REL | HRTIMER_MODE_HARD,
58 
59 	HRTIMER_MODE_ABS_PINNED_HARD = HRTIMER_MODE_ABS_PINNED | HRTIMER_MODE_HARD,
60 	HRTIMER_MODE_REL_PINNED_HARD = HRTIMER_MODE_REL_PINNED | HRTIMER_MODE_HARD,
61 };
62 
63 /*
64  * Return values for the callback function
65  */
66 enum hrtimer_restart {
67 	HRTIMER_NORESTART,	/* Timer is not restarted */
68 	HRTIMER_RESTART,	/* Timer must be restarted */
69 };
70 
71 /*
72  * Values to track state of the timer
73  *
74  * Possible states:
75  *
76  * 0x00		inactive
77  * 0x01		enqueued into rbtree
78  *
79  * The callback state is not part of the timer->state because clearing it would
80  * mean touching the timer after the callback, this makes it impossible to free
81  * the timer from the callback function.
82  *
83  * Therefore we track the callback state in:
84  *
85  *	timer->base->cpu_base->running == timer
86  *
87  * On SMP it is possible to have a "callback function running and enqueued"
88  * status. It happens for example when a posix timer expired and the callback
89  * queued a signal. Between dropping the lock which protects the posix timer
90  * and reacquiring the base lock of the hrtimer, another CPU can deliver the
91  * signal and rearm the timer.
92  *
93  * All state transitions are protected by cpu_base->lock.
94  */
95 #define HRTIMER_STATE_INACTIVE	0x00
96 #define HRTIMER_STATE_ENQUEUED	0x01
97 
98 /**
99  * struct hrtimer - the basic hrtimer structure
100  * @node:	timerqueue node, which also manages node.expires,
101  *		the absolute expiry time in the hrtimers internal
102  *		representation. The time is related to the clock on
103  *		which the timer is based. Is setup by adding
104  *		slack to the _softexpires value. For non range timers
105  *		identical to _softexpires.
106  * @_softexpires: the absolute earliest expiry time of the hrtimer.
107  *		The time which was given as expiry time when the timer
108  *		was armed.
109  * @function:	timer expiry callback function
110  * @base:	pointer to the timer base (per cpu and per clock)
111  * @state:	state information (See bit values above)
112  * @is_rel:	Set if the timer was armed relative
113  * @is_soft:	Set if hrtimer will be expired in soft interrupt context.
114  * @is_hard:	Set if hrtimer will be expired in hard interrupt context
115  *		even on RT.
116  *
117  * The hrtimer structure must be initialized by hrtimer_init()
118  */
119 struct hrtimer {
120 	struct timerqueue_node		node;
121 	ktime_t				_softexpires;
122 	enum hrtimer_restart		(*function)(struct hrtimer *);
123 	struct hrtimer_clock_base	*base;
124 	u8				state;
125 	u8				is_rel;
126 	u8				is_soft;
127 	u8				is_hard;
128 
129 	ANDROID_KABI_RESERVE(1);
130 };
131 
132 /**
133  * struct hrtimer_sleeper - simple sleeper structure
134  * @timer:	embedded timer structure
135  * @task:	task to wake up
136  *
137  * task is set to NULL, when the timer expires.
138  */
139 struct hrtimer_sleeper {
140 	struct hrtimer timer;
141 	struct task_struct *task;
142 };
143 
144 #ifdef CONFIG_64BIT
145 # define __hrtimer_clock_base_align	____cacheline_aligned
146 #else
147 # define __hrtimer_clock_base_align
148 #endif
149 
150 /**
151  * struct hrtimer_clock_base - the timer base for a specific clock
152  * @cpu_base:		per cpu clock base
153  * @index:		clock type index for per_cpu support when moving a
154  *			timer to a base on another cpu.
155  * @clockid:		clock id for per_cpu support
156  * @seq:		seqcount around __run_hrtimer
157  * @running:		pointer to the currently running hrtimer
158  * @active:		red black tree root node for the active timers
159  * @get_time:		function to retrieve the current time of the clock
160  * @offset:		offset of this clock to the monotonic base
161  */
162 struct hrtimer_clock_base {
163 	struct hrtimer_cpu_base	*cpu_base;
164 	unsigned int		index;
165 	clockid_t		clockid;
166 	seqcount_raw_spinlock_t	seq;
167 	struct hrtimer		*running;
168 	struct timerqueue_head	active;
169 	ktime_t			(*get_time)(void);
170 	ktime_t			offset;
171 } __hrtimer_clock_base_align;
172 
173 enum  hrtimer_base_type {
174 	HRTIMER_BASE_MONOTONIC,
175 	HRTIMER_BASE_REALTIME,
176 	HRTIMER_BASE_BOOTTIME,
177 	HRTIMER_BASE_TAI,
178 	HRTIMER_BASE_MONOTONIC_SOFT,
179 	HRTIMER_BASE_REALTIME_SOFT,
180 	HRTIMER_BASE_BOOTTIME_SOFT,
181 	HRTIMER_BASE_TAI_SOFT,
182 	HRTIMER_MAX_CLOCK_BASES,
183 };
184 
185 /**
186  * struct hrtimer_cpu_base - the per cpu clock bases
187  * @lock:		lock protecting the base and associated clock bases
188  *			and timers
189  * @cpu:		cpu number
190  * @active_bases:	Bitfield to mark bases with active timers
191  * @clock_was_set_seq:	Sequence counter of clock was set events
192  * @hres_active:	State of high resolution mode
193  * @in_hrtirq:		hrtimer_interrupt() is currently executing
194  * @hang_detected:	The last hrtimer interrupt detected a hang
195  * @softirq_activated:	displays, if the softirq is raised - update of softirq
196  *			related settings is not required then.
197  * @nr_events:		Total number of hrtimer interrupt events
198  * @nr_retries:		Total number of hrtimer interrupt retries
199  * @nr_hangs:		Total number of hrtimer interrupt hangs
200  * @max_hang_time:	Maximum time spent in hrtimer_interrupt
201  * @softirq_expiry_lock: Lock which is taken while softirq based hrtimer are
202  *			 expired
203  * @timer_waiters:	A hrtimer_cancel() invocation waits for the timer
204  *			callback to finish.
205  * @expires_next:	absolute time of the next event, is required for remote
206  *			hrtimer enqueue; it is the total first expiry time (hard
207  *			and soft hrtimer are taken into account)
208  * @next_timer:		Pointer to the first expiring timer
209  * @softirq_expires_next: Time to check, if soft queues needs also to be expired
210  * @softirq_next_timer: Pointer to the first expiring softirq based timer
211  * @clock_base:		array of clock bases for this cpu
212  *
213  * Note: next_timer is just an optimization for __remove_hrtimer().
214  *	 Do not dereference the pointer because it is not reliable on
215  *	 cross cpu removals.
216  */
217 struct hrtimer_cpu_base {
218 	raw_spinlock_t			lock;
219 	unsigned int			cpu;
220 	unsigned int			active_bases;
221 	unsigned int			clock_was_set_seq;
222 	unsigned int			hres_active		: 1,
223 					in_hrtirq		: 1,
224 					hang_detected		: 1,
225 					softirq_activated       : 1;
226 #ifdef CONFIG_HIGH_RES_TIMERS
227 	unsigned int			nr_events;
228 	unsigned short			nr_retries;
229 	unsigned short			nr_hangs;
230 	unsigned int			max_hang_time;
231 #endif
232 #ifdef CONFIG_PREEMPT_RT
233 	spinlock_t			softirq_expiry_lock;
234 	atomic_t			timer_waiters;
235 #endif
236 	ktime_t				expires_next;
237 	struct hrtimer			*next_timer;
238 	ktime_t				softirq_expires_next;
239 	struct hrtimer			*softirq_next_timer;
240 	struct hrtimer_clock_base	clock_base[HRTIMER_MAX_CLOCK_BASES];
241 } ____cacheline_aligned;
242 
hrtimer_set_expires(struct hrtimer * timer,ktime_t time)243 static inline void hrtimer_set_expires(struct hrtimer *timer, ktime_t time)
244 {
245 	timer->node.expires = time;
246 	timer->_softexpires = time;
247 }
248 
hrtimer_set_expires_range(struct hrtimer * timer,ktime_t time,ktime_t delta)249 static inline void hrtimer_set_expires_range(struct hrtimer *timer, ktime_t time, ktime_t delta)
250 {
251 	timer->_softexpires = time;
252 	timer->node.expires = ktime_add_safe(time, delta);
253 }
254 
hrtimer_set_expires_range_ns(struct hrtimer * timer,ktime_t time,u64 delta)255 static inline void hrtimer_set_expires_range_ns(struct hrtimer *timer, ktime_t time, u64 delta)
256 {
257 	timer->_softexpires = time;
258 	timer->node.expires = ktime_add_safe(time, ns_to_ktime(delta));
259 }
260 
hrtimer_set_expires_tv64(struct hrtimer * timer,s64 tv64)261 static inline void hrtimer_set_expires_tv64(struct hrtimer *timer, s64 tv64)
262 {
263 	timer->node.expires = tv64;
264 	timer->_softexpires = tv64;
265 }
266 
hrtimer_add_expires(struct hrtimer * timer,ktime_t time)267 static inline void hrtimer_add_expires(struct hrtimer *timer, ktime_t time)
268 {
269 	timer->node.expires = ktime_add_safe(timer->node.expires, time);
270 	timer->_softexpires = ktime_add_safe(timer->_softexpires, time);
271 }
272 
hrtimer_add_expires_ns(struct hrtimer * timer,u64 ns)273 static inline void hrtimer_add_expires_ns(struct hrtimer *timer, u64 ns)
274 {
275 	timer->node.expires = ktime_add_ns(timer->node.expires, ns);
276 	timer->_softexpires = ktime_add_ns(timer->_softexpires, ns);
277 }
278 
hrtimer_get_expires(const struct hrtimer * timer)279 static inline ktime_t hrtimer_get_expires(const struct hrtimer *timer)
280 {
281 	return timer->node.expires;
282 }
283 
hrtimer_get_softexpires(const struct hrtimer * timer)284 static inline ktime_t hrtimer_get_softexpires(const struct hrtimer *timer)
285 {
286 	return timer->_softexpires;
287 }
288 
hrtimer_get_expires_tv64(const struct hrtimer * timer)289 static inline s64 hrtimer_get_expires_tv64(const struct hrtimer *timer)
290 {
291 	return timer->node.expires;
292 }
hrtimer_get_softexpires_tv64(const struct hrtimer * timer)293 static inline s64 hrtimer_get_softexpires_tv64(const struct hrtimer *timer)
294 {
295 	return timer->_softexpires;
296 }
297 
hrtimer_get_expires_ns(const struct hrtimer * timer)298 static inline s64 hrtimer_get_expires_ns(const struct hrtimer *timer)
299 {
300 	return ktime_to_ns(timer->node.expires);
301 }
302 
hrtimer_expires_remaining(const struct hrtimer * timer)303 static inline ktime_t hrtimer_expires_remaining(const struct hrtimer *timer)
304 {
305 	return ktime_sub(timer->node.expires, timer->base->get_time());
306 }
307 
hrtimer_cb_get_time(struct hrtimer * timer)308 static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
309 {
310 	return timer->base->get_time();
311 }
312 
hrtimer_is_hres_active(struct hrtimer * timer)313 static inline int hrtimer_is_hres_active(struct hrtimer *timer)
314 {
315 	return IS_ENABLED(CONFIG_HIGH_RES_TIMERS) ?
316 		timer->base->cpu_base->hres_active : 0;
317 }
318 
319 #ifdef CONFIG_HIGH_RES_TIMERS
320 struct clock_event_device;
321 
322 extern void hrtimer_interrupt(struct clock_event_device *dev);
323 
324 extern unsigned int hrtimer_resolution;
325 
326 #else
327 
328 #define hrtimer_resolution	(unsigned int)LOW_RES_NSEC
329 
330 #endif
331 
332 static inline ktime_t
__hrtimer_expires_remaining_adjusted(const struct hrtimer * timer,ktime_t now)333 __hrtimer_expires_remaining_adjusted(const struct hrtimer *timer, ktime_t now)
334 {
335 	ktime_t rem = ktime_sub(timer->node.expires, now);
336 
337 	/*
338 	 * Adjust relative timers for the extra we added in
339 	 * hrtimer_start_range_ns() to prevent short timeouts.
340 	 */
341 	if (IS_ENABLED(CONFIG_TIME_LOW_RES) && timer->is_rel)
342 		rem -= hrtimer_resolution;
343 	return rem;
344 }
345 
346 static inline ktime_t
hrtimer_expires_remaining_adjusted(const struct hrtimer * timer)347 hrtimer_expires_remaining_adjusted(const struct hrtimer *timer)
348 {
349 	return __hrtimer_expires_remaining_adjusted(timer,
350 						    timer->base->get_time());
351 }
352 
353 #ifdef CONFIG_TIMERFD
354 extern void timerfd_clock_was_set(void);
355 extern void timerfd_resume(void);
356 #else
timerfd_clock_was_set(void)357 static inline void timerfd_clock_was_set(void) { }
timerfd_resume(void)358 static inline void timerfd_resume(void) { }
359 #endif
360 
361 DECLARE_PER_CPU(struct tick_device, tick_cpu_device);
362 
363 #ifdef CONFIG_PREEMPT_RT
364 void hrtimer_cancel_wait_running(const struct hrtimer *timer);
365 #else
hrtimer_cancel_wait_running(struct hrtimer * timer)366 static inline void hrtimer_cancel_wait_running(struct hrtimer *timer)
367 {
368 	cpu_relax();
369 }
370 #endif
371 
372 /* Exported timer functions: */
373 
374 /* Initialize timers: */
375 extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
376 			 enum hrtimer_mode mode);
377 extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, clockid_t clock_id,
378 				 enum hrtimer_mode mode);
379 
380 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
381 extern void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t which_clock,
382 				  enum hrtimer_mode mode);
383 extern void hrtimer_init_sleeper_on_stack(struct hrtimer_sleeper *sl,
384 					  clockid_t clock_id,
385 					  enum hrtimer_mode mode);
386 
387 extern void destroy_hrtimer_on_stack(struct hrtimer *timer);
388 #else
hrtimer_init_on_stack(struct hrtimer * timer,clockid_t which_clock,enum hrtimer_mode mode)389 static inline void hrtimer_init_on_stack(struct hrtimer *timer,
390 					 clockid_t which_clock,
391 					 enum hrtimer_mode mode)
392 {
393 	hrtimer_init(timer, which_clock, mode);
394 }
395 
hrtimer_init_sleeper_on_stack(struct hrtimer_sleeper * sl,clockid_t clock_id,enum hrtimer_mode mode)396 static inline void hrtimer_init_sleeper_on_stack(struct hrtimer_sleeper *sl,
397 						 clockid_t clock_id,
398 						 enum hrtimer_mode mode)
399 {
400 	hrtimer_init_sleeper(sl, clock_id, mode);
401 }
402 
destroy_hrtimer_on_stack(struct hrtimer * timer)403 static inline void destroy_hrtimer_on_stack(struct hrtimer *timer) { }
404 #endif
405 
406 /* Basic timer operations: */
407 extern void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
408 				   u64 range_ns, const enum hrtimer_mode mode);
409 
410 /**
411  * hrtimer_start - (re)start an hrtimer
412  * @timer:	the timer to be added
413  * @tim:	expiry time
414  * @mode:	timer mode: absolute (HRTIMER_MODE_ABS) or
415  *		relative (HRTIMER_MODE_REL), and pinned (HRTIMER_MODE_PINNED);
416  *		softirq based mode is considered for debug purpose only!
417  */
hrtimer_start(struct hrtimer * timer,ktime_t tim,const enum hrtimer_mode mode)418 static inline void hrtimer_start(struct hrtimer *timer, ktime_t tim,
419 				 const enum hrtimer_mode mode)
420 {
421 	hrtimer_start_range_ns(timer, tim, 0, mode);
422 }
423 
424 extern int hrtimer_cancel(struct hrtimer *timer);
425 extern int hrtimer_try_to_cancel(struct hrtimer *timer);
426 
hrtimer_start_expires(struct hrtimer * timer,enum hrtimer_mode mode)427 static inline void hrtimer_start_expires(struct hrtimer *timer,
428 					 enum hrtimer_mode mode)
429 {
430 	u64 delta;
431 	ktime_t soft, hard;
432 	soft = hrtimer_get_softexpires(timer);
433 	hard = hrtimer_get_expires(timer);
434 	delta = ktime_to_ns(ktime_sub(hard, soft));
435 	hrtimer_start_range_ns(timer, soft, delta, mode);
436 }
437 
438 void hrtimer_sleeper_start_expires(struct hrtimer_sleeper *sl,
439 				   enum hrtimer_mode mode);
440 
hrtimer_restart(struct hrtimer * timer)441 static inline void hrtimer_restart(struct hrtimer *timer)
442 {
443 	hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
444 }
445 
446 /* Query timers: */
447 extern ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust);
448 
449 /**
450  * hrtimer_get_remaining - get remaining time for the timer
451  * @timer:	the timer to read
452  */
hrtimer_get_remaining(const struct hrtimer * timer)453 static inline ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
454 {
455 	return __hrtimer_get_remaining(timer, false);
456 }
457 
458 extern u64 hrtimer_get_next_event(void);
459 extern u64 hrtimer_next_event_without(const struct hrtimer *exclude);
460 
461 extern bool hrtimer_active(const struct hrtimer *timer);
462 
463 /**
464  * hrtimer_is_queued - check, whether the timer is on one of the queues
465  * @timer:	Timer to check
466  *
467  * Returns: True if the timer is queued, false otherwise
468  *
469  * The function can be used lockless, but it gives only a current snapshot.
470  */
hrtimer_is_queued(struct hrtimer * timer)471 static inline bool hrtimer_is_queued(struct hrtimer *timer)
472 {
473 	/* The READ_ONCE pairs with the update functions of timer->state */
474 	return !!(READ_ONCE(timer->state) & HRTIMER_STATE_ENQUEUED);
475 }
476 
477 /*
478  * Helper function to check, whether the timer is running the callback
479  * function
480  */
hrtimer_callback_running(struct hrtimer * timer)481 static inline int hrtimer_callback_running(struct hrtimer *timer)
482 {
483 	return timer->base->running == timer;
484 }
485 
486 /* Forward a hrtimer so it expires after now: */
487 extern u64
488 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);
489 
490 /**
491  * hrtimer_forward_now - forward the timer expiry so it expires after now
492  * @timer:	hrtimer to forward
493  * @interval:	the interval to forward
494  *
495  * Forward the timer expiry so it will expire after the current time
496  * of the hrtimer clock base. Returns the number of overruns.
497  *
498  * Can be safely called from the callback function of @timer. If
499  * called from other contexts @timer must neither be enqueued nor
500  * running the callback and the caller needs to take care of
501  * serialization.
502  *
503  * Note: This only updates the timer expiry value and does not requeue
504  * the timer.
505  */
hrtimer_forward_now(struct hrtimer * timer,ktime_t interval)506 static inline u64 hrtimer_forward_now(struct hrtimer *timer,
507 				      ktime_t interval)
508 {
509 	return hrtimer_forward(timer, timer->base->get_time(), interval);
510 }
511 
512 /* Precise sleep: */
513 
514 extern int nanosleep_copyout(struct restart_block *, struct timespec64 *);
515 extern long hrtimer_nanosleep(ktime_t rqtp, const enum hrtimer_mode mode,
516 			      const clockid_t clockid);
517 
518 extern int schedule_hrtimeout_range(ktime_t *expires, u64 delta,
519 				    const enum hrtimer_mode mode);
520 extern int schedule_hrtimeout_range_clock(ktime_t *expires,
521 					  u64 delta,
522 					  const enum hrtimer_mode mode,
523 					  clockid_t clock_id);
524 extern int schedule_hrtimeout(ktime_t *expires, const enum hrtimer_mode mode);
525 
526 /* Soft interrupt function to run the hrtimer queues: */
527 extern void hrtimer_run_queues(void);
528 
529 /* Bootup initialization: */
530 extern void __init hrtimers_init(void);
531 
532 /* Show pending timers: */
533 extern void sysrq_timer_list_show(void);
534 
535 int hrtimers_prepare_cpu(unsigned int cpu);
536 #ifdef CONFIG_HOTPLUG_CPU
537 int hrtimers_dead_cpu(unsigned int cpu);
538 #else
539 #define hrtimers_dead_cpu	NULL
540 #endif
541 
542 #endif
543