• 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 #else
timerfd_clock_was_set(void)356 static inline void timerfd_clock_was_set(void) { }
357 #endif
358 extern void hrtimers_resume(void);
359 
360 DECLARE_PER_CPU(struct tick_device, tick_cpu_device);
361 
362 #ifdef CONFIG_PREEMPT_RT
363 void hrtimer_cancel_wait_running(const struct hrtimer *timer);
364 #else
hrtimer_cancel_wait_running(struct hrtimer * timer)365 static inline void hrtimer_cancel_wait_running(struct hrtimer *timer)
366 {
367 	cpu_relax();
368 }
369 #endif
370 
371 /* Exported timer functions: */
372 
373 /* Initialize timers: */
374 extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
375 			 enum hrtimer_mode mode);
376 extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, clockid_t clock_id,
377 				 enum hrtimer_mode mode);
378 
379 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
380 extern void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t which_clock,
381 				  enum hrtimer_mode mode);
382 extern void hrtimer_init_sleeper_on_stack(struct hrtimer_sleeper *sl,
383 					  clockid_t clock_id,
384 					  enum hrtimer_mode mode);
385 
386 extern void destroy_hrtimer_on_stack(struct hrtimer *timer);
387 #else
hrtimer_init_on_stack(struct hrtimer * timer,clockid_t which_clock,enum hrtimer_mode mode)388 static inline void hrtimer_init_on_stack(struct hrtimer *timer,
389 					 clockid_t which_clock,
390 					 enum hrtimer_mode mode)
391 {
392 	hrtimer_init(timer, which_clock, mode);
393 }
394 
hrtimer_init_sleeper_on_stack(struct hrtimer_sleeper * sl,clockid_t clock_id,enum hrtimer_mode mode)395 static inline void hrtimer_init_sleeper_on_stack(struct hrtimer_sleeper *sl,
396 						 clockid_t clock_id,
397 						 enum hrtimer_mode mode)
398 {
399 	hrtimer_init_sleeper(sl, clock_id, mode);
400 }
401 
destroy_hrtimer_on_stack(struct hrtimer * timer)402 static inline void destroy_hrtimer_on_stack(struct hrtimer *timer) { }
403 #endif
404 
405 /* Basic timer operations: */
406 extern void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
407 				   u64 range_ns, const enum hrtimer_mode mode);
408 
409 /**
410  * hrtimer_start - (re)start an hrtimer
411  * @timer:	the timer to be added
412  * @tim:	expiry time
413  * @mode:	timer mode: absolute (HRTIMER_MODE_ABS) or
414  *		relative (HRTIMER_MODE_REL), and pinned (HRTIMER_MODE_PINNED);
415  *		softirq based mode is considered for debug purpose only!
416  */
hrtimer_start(struct hrtimer * timer,ktime_t tim,const enum hrtimer_mode mode)417 static inline void hrtimer_start(struct hrtimer *timer, ktime_t tim,
418 				 const enum hrtimer_mode mode)
419 {
420 	hrtimer_start_range_ns(timer, tim, 0, mode);
421 }
422 
423 extern int hrtimer_cancel(struct hrtimer *timer);
424 extern int hrtimer_try_to_cancel(struct hrtimer *timer);
425 
hrtimer_start_expires(struct hrtimer * timer,enum hrtimer_mode mode)426 static inline void hrtimer_start_expires(struct hrtimer *timer,
427 					 enum hrtimer_mode mode)
428 {
429 	u64 delta;
430 	ktime_t soft, hard;
431 	soft = hrtimer_get_softexpires(timer);
432 	hard = hrtimer_get_expires(timer);
433 	delta = ktime_to_ns(ktime_sub(hard, soft));
434 	hrtimer_start_range_ns(timer, soft, delta, mode);
435 }
436 
437 void hrtimer_sleeper_start_expires(struct hrtimer_sleeper *sl,
438 				   enum hrtimer_mode mode);
439 
hrtimer_restart(struct hrtimer * timer)440 static inline void hrtimer_restart(struct hrtimer *timer)
441 {
442 	hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
443 }
444 
445 /* Query timers: */
446 extern ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust);
447 
hrtimer_get_remaining(const struct hrtimer * timer)448 static inline ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
449 {
450 	return __hrtimer_get_remaining(timer, false);
451 }
452 
453 extern u64 hrtimer_get_next_event(void);
454 extern u64 hrtimer_next_event_without(const struct hrtimer *exclude);
455 
456 extern bool hrtimer_active(const struct hrtimer *timer);
457 
458 /**
459  * hrtimer_is_queued = check, whether the timer is on one of the queues
460  * @timer:	Timer to check
461  *
462  * Returns: True if the timer is queued, false otherwise
463  *
464  * The function can be used lockless, but it gives only a current snapshot.
465  */
hrtimer_is_queued(struct hrtimer * timer)466 static inline bool hrtimer_is_queued(struct hrtimer *timer)
467 {
468 	/* The READ_ONCE pairs with the update functions of timer->state */
469 	return !!(READ_ONCE(timer->state) & HRTIMER_STATE_ENQUEUED);
470 }
471 
472 /*
473  * Helper function to check, whether the timer is running the callback
474  * function
475  */
hrtimer_callback_running(struct hrtimer * timer)476 static inline int hrtimer_callback_running(struct hrtimer *timer)
477 {
478 	return timer->base->running == timer;
479 }
480 
481 /* Forward a hrtimer so it expires after now: */
482 extern u64
483 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);
484 
485 /**
486  * hrtimer_forward_now - forward the timer expiry so it expires after now
487  * @timer:	hrtimer to forward
488  * @interval:	the interval to forward
489  *
490  * Forward the timer expiry so it will expire after the current time
491  * of the hrtimer clock base. Returns the number of overruns.
492  *
493  * Can be safely called from the callback function of @timer. If
494  * called from other contexts @timer must neither be enqueued nor
495  * running the callback and the caller needs to take care of
496  * serialization.
497  *
498  * Note: This only updates the timer expiry value and does not requeue
499  * the timer.
500  */
hrtimer_forward_now(struct hrtimer * timer,ktime_t interval)501 static inline u64 hrtimer_forward_now(struct hrtimer *timer,
502 				      ktime_t interval)
503 {
504 	return hrtimer_forward(timer, timer->base->get_time(), interval);
505 }
506 
507 /* Precise sleep: */
508 
509 extern int nanosleep_copyout(struct restart_block *, struct timespec64 *);
510 extern long hrtimer_nanosleep(ktime_t rqtp, const enum hrtimer_mode mode,
511 			      const clockid_t clockid);
512 
513 extern int schedule_hrtimeout_range(ktime_t *expires, u64 delta,
514 				    const enum hrtimer_mode mode);
515 extern int schedule_hrtimeout_range_clock(ktime_t *expires,
516 					  u64 delta,
517 					  const enum hrtimer_mode mode,
518 					  clockid_t clock_id);
519 extern int schedule_hrtimeout(ktime_t *expires, const enum hrtimer_mode mode);
520 
521 /* Soft interrupt function to run the hrtimer queues: */
522 extern void hrtimer_run_queues(void);
523 
524 /* Bootup initialization: */
525 extern void __init hrtimers_init(void);
526 
527 /* Show pending timers: */
528 extern void sysrq_timer_list_show(void);
529 
530 int hrtimers_prepare_cpu(unsigned int cpu);
531 #ifdef CONFIG_HOTPLUG_CPU
532 int hrtimers_dead_cpu(unsigned int cpu);
533 #else
534 #define hrtimers_dead_cpu	NULL
535 #endif
536 
537 #endif
538