• 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  * @online:		CPU is online from an hrtimers point of view
204  * @timer_waiters:	A hrtimer_cancel() invocation waits for the timer
205  *			callback to finish.
206  * @expires_next:	absolute time of the next event, is required for remote
207  *			hrtimer enqueue; it is the total first expiry time (hard
208  *			and soft hrtimer are taken into account)
209  * @next_timer:		Pointer to the first expiring timer
210  * @softirq_expires_next: Time to check, if soft queues needs also to be expired
211  * @softirq_next_timer: Pointer to the first expiring softirq based timer
212  * @clock_base:		array of clock bases for this cpu
213  *
214  * Note: next_timer is just an optimization for __remove_hrtimer().
215  *	 Do not dereference the pointer because it is not reliable on
216  *	 cross cpu removals.
217  */
218 struct hrtimer_cpu_base {
219 	raw_spinlock_t			lock;
220 	unsigned int			cpu;
221 	unsigned int			active_bases;
222 	unsigned int			clock_was_set_seq;
223 	unsigned int			hres_active		: 1,
224 					in_hrtirq		: 1,
225 					hang_detected		: 1,
226 					softirq_activated       : 1,
227 					online			: 1;
228 #ifdef CONFIG_HIGH_RES_TIMERS
229 	unsigned int			nr_events;
230 	unsigned short			nr_retries;
231 	unsigned short			nr_hangs;
232 	unsigned int			max_hang_time;
233 #endif
234 #ifdef CONFIG_PREEMPT_RT
235 	spinlock_t			softirq_expiry_lock;
236 	atomic_t			timer_waiters;
237 #endif
238 	ktime_t				expires_next;
239 	struct hrtimer			*next_timer;
240 	ktime_t				softirq_expires_next;
241 	struct hrtimer			*softirq_next_timer;
242 	struct hrtimer_clock_base	clock_base[HRTIMER_MAX_CLOCK_BASES];
243 } ____cacheline_aligned;
244 
hrtimer_set_expires(struct hrtimer * timer,ktime_t time)245 static inline void hrtimer_set_expires(struct hrtimer *timer, ktime_t time)
246 {
247 	timer->node.expires = time;
248 	timer->_softexpires = time;
249 }
250 
hrtimer_set_expires_range(struct hrtimer * timer,ktime_t time,ktime_t delta)251 static inline void hrtimer_set_expires_range(struct hrtimer *timer, ktime_t time, ktime_t delta)
252 {
253 	timer->_softexpires = time;
254 	timer->node.expires = ktime_add_safe(time, delta);
255 }
256 
hrtimer_set_expires_range_ns(struct hrtimer * timer,ktime_t time,u64 delta)257 static inline void hrtimer_set_expires_range_ns(struct hrtimer *timer, ktime_t time, u64 delta)
258 {
259 	timer->_softexpires = time;
260 	timer->node.expires = ktime_add_safe(time, ns_to_ktime(delta));
261 }
262 
hrtimer_set_expires_tv64(struct hrtimer * timer,s64 tv64)263 static inline void hrtimer_set_expires_tv64(struct hrtimer *timer, s64 tv64)
264 {
265 	timer->node.expires = tv64;
266 	timer->_softexpires = tv64;
267 }
268 
hrtimer_add_expires(struct hrtimer * timer,ktime_t time)269 static inline void hrtimer_add_expires(struct hrtimer *timer, ktime_t time)
270 {
271 	timer->node.expires = ktime_add_safe(timer->node.expires, time);
272 	timer->_softexpires = ktime_add_safe(timer->_softexpires, time);
273 }
274 
hrtimer_add_expires_ns(struct hrtimer * timer,u64 ns)275 static inline void hrtimer_add_expires_ns(struct hrtimer *timer, u64 ns)
276 {
277 	timer->node.expires = ktime_add_ns(timer->node.expires, ns);
278 	timer->_softexpires = ktime_add_ns(timer->_softexpires, ns);
279 }
280 
hrtimer_get_expires(const struct hrtimer * timer)281 static inline ktime_t hrtimer_get_expires(const struct hrtimer *timer)
282 {
283 	return timer->node.expires;
284 }
285 
hrtimer_get_softexpires(const struct hrtimer * timer)286 static inline ktime_t hrtimer_get_softexpires(const struct hrtimer *timer)
287 {
288 	return timer->_softexpires;
289 }
290 
hrtimer_get_expires_tv64(const struct hrtimer * timer)291 static inline s64 hrtimer_get_expires_tv64(const struct hrtimer *timer)
292 {
293 	return timer->node.expires;
294 }
hrtimer_get_softexpires_tv64(const struct hrtimer * timer)295 static inline s64 hrtimer_get_softexpires_tv64(const struct hrtimer *timer)
296 {
297 	return timer->_softexpires;
298 }
299 
hrtimer_get_expires_ns(const struct hrtimer * timer)300 static inline s64 hrtimer_get_expires_ns(const struct hrtimer *timer)
301 {
302 	return ktime_to_ns(timer->node.expires);
303 }
304 
hrtimer_expires_remaining(const struct hrtimer * timer)305 static inline ktime_t hrtimer_expires_remaining(const struct hrtimer *timer)
306 {
307 	return ktime_sub(timer->node.expires, timer->base->get_time());
308 }
309 
hrtimer_cb_get_time(struct hrtimer * timer)310 static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
311 {
312 	return timer->base->get_time();
313 }
314 
hrtimer_is_hres_active(struct hrtimer * timer)315 static inline int hrtimer_is_hres_active(struct hrtimer *timer)
316 {
317 	return IS_ENABLED(CONFIG_HIGH_RES_TIMERS) ?
318 		timer->base->cpu_base->hres_active : 0;
319 }
320 
321 #ifdef CONFIG_HIGH_RES_TIMERS
322 struct clock_event_device;
323 
324 extern void hrtimer_interrupt(struct clock_event_device *dev);
325 
326 extern unsigned int hrtimer_resolution;
327 
328 #else
329 
330 #define hrtimer_resolution	(unsigned int)LOW_RES_NSEC
331 
332 #endif
333 
334 static inline ktime_t
__hrtimer_expires_remaining_adjusted(const struct hrtimer * timer,ktime_t now)335 __hrtimer_expires_remaining_adjusted(const struct hrtimer *timer, ktime_t now)
336 {
337 	ktime_t rem = ktime_sub(timer->node.expires, now);
338 
339 	/*
340 	 * Adjust relative timers for the extra we added in
341 	 * hrtimer_start_range_ns() to prevent short timeouts.
342 	 */
343 	if (IS_ENABLED(CONFIG_TIME_LOW_RES) && timer->is_rel)
344 		rem -= hrtimer_resolution;
345 	return rem;
346 }
347 
348 static inline ktime_t
hrtimer_expires_remaining_adjusted(const struct hrtimer * timer)349 hrtimer_expires_remaining_adjusted(const struct hrtimer *timer)
350 {
351 	return __hrtimer_expires_remaining_adjusted(timer,
352 						    timer->base->get_time());
353 }
354 
355 #ifdef CONFIG_TIMERFD
356 extern void timerfd_clock_was_set(void);
357 extern void timerfd_resume(void);
358 #else
timerfd_clock_was_set(void)359 static inline void timerfd_clock_was_set(void) { }
timerfd_resume(void)360 static inline void timerfd_resume(void) { }
361 #endif
362 
363 DECLARE_PER_CPU(struct tick_device, tick_cpu_device);
364 
365 #ifdef CONFIG_PREEMPT_RT
366 void hrtimer_cancel_wait_running(const struct hrtimer *timer);
367 #else
hrtimer_cancel_wait_running(struct hrtimer * timer)368 static inline void hrtimer_cancel_wait_running(struct hrtimer *timer)
369 {
370 	cpu_relax();
371 }
372 #endif
373 
374 /* Exported timer functions: */
375 
376 /* Initialize timers: */
377 extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
378 			 enum hrtimer_mode mode);
379 extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, clockid_t clock_id,
380 				 enum hrtimer_mode mode);
381 
382 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
383 extern void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t which_clock,
384 				  enum hrtimer_mode mode);
385 extern void hrtimer_init_sleeper_on_stack(struct hrtimer_sleeper *sl,
386 					  clockid_t clock_id,
387 					  enum hrtimer_mode mode);
388 
389 extern void destroy_hrtimer_on_stack(struct hrtimer *timer);
390 #else
hrtimer_init_on_stack(struct hrtimer * timer,clockid_t which_clock,enum hrtimer_mode mode)391 static inline void hrtimer_init_on_stack(struct hrtimer *timer,
392 					 clockid_t which_clock,
393 					 enum hrtimer_mode mode)
394 {
395 	hrtimer_init(timer, which_clock, mode);
396 }
397 
hrtimer_init_sleeper_on_stack(struct hrtimer_sleeper * sl,clockid_t clock_id,enum hrtimer_mode mode)398 static inline void hrtimer_init_sleeper_on_stack(struct hrtimer_sleeper *sl,
399 						 clockid_t clock_id,
400 						 enum hrtimer_mode mode)
401 {
402 	hrtimer_init_sleeper(sl, clock_id, mode);
403 }
404 
destroy_hrtimer_on_stack(struct hrtimer * timer)405 static inline void destroy_hrtimer_on_stack(struct hrtimer *timer) { }
406 #endif
407 
408 /* Basic timer operations: */
409 extern void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
410 				   u64 range_ns, const enum hrtimer_mode mode);
411 
412 /**
413  * hrtimer_start - (re)start an hrtimer
414  * @timer:	the timer to be added
415  * @tim:	expiry time
416  * @mode:	timer mode: absolute (HRTIMER_MODE_ABS) or
417  *		relative (HRTIMER_MODE_REL), and pinned (HRTIMER_MODE_PINNED);
418  *		softirq based mode is considered for debug purpose only!
419  */
hrtimer_start(struct hrtimer * timer,ktime_t tim,const enum hrtimer_mode mode)420 static inline void hrtimer_start(struct hrtimer *timer, ktime_t tim,
421 				 const enum hrtimer_mode mode)
422 {
423 	hrtimer_start_range_ns(timer, tim, 0, mode);
424 }
425 
426 extern int hrtimer_cancel(struct hrtimer *timer);
427 extern int hrtimer_try_to_cancel(struct hrtimer *timer);
428 
hrtimer_start_expires(struct hrtimer * timer,enum hrtimer_mode mode)429 static inline void hrtimer_start_expires(struct hrtimer *timer,
430 					 enum hrtimer_mode mode)
431 {
432 	u64 delta;
433 	ktime_t soft, hard;
434 	soft = hrtimer_get_softexpires(timer);
435 	hard = hrtimer_get_expires(timer);
436 	delta = ktime_to_ns(ktime_sub(hard, soft));
437 	hrtimer_start_range_ns(timer, soft, delta, mode);
438 }
439 
440 void hrtimer_sleeper_start_expires(struct hrtimer_sleeper *sl,
441 				   enum hrtimer_mode mode);
442 
hrtimer_restart(struct hrtimer * timer)443 static inline void hrtimer_restart(struct hrtimer *timer)
444 {
445 	hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
446 }
447 
448 /* Query timers: */
449 extern ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust);
450 
451 /**
452  * hrtimer_get_remaining - get remaining time for the timer
453  * @timer:	the timer to read
454  */
hrtimer_get_remaining(const struct hrtimer * timer)455 static inline ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
456 {
457 	return __hrtimer_get_remaining(timer, false);
458 }
459 
460 extern u64 hrtimer_get_next_event(void);
461 extern u64 hrtimer_next_event_without(const struct hrtimer *exclude);
462 
463 extern bool hrtimer_active(const struct hrtimer *timer);
464 
465 /**
466  * hrtimer_is_queued - check, whether the timer is on one of the queues
467  * @timer:	Timer to check
468  *
469  * Returns: True if the timer is queued, false otherwise
470  *
471  * The function can be used lockless, but it gives only a current snapshot.
472  */
hrtimer_is_queued(struct hrtimer * timer)473 static inline bool hrtimer_is_queued(struct hrtimer *timer)
474 {
475 	/* The READ_ONCE pairs with the update functions of timer->state */
476 	return !!(READ_ONCE(timer->state) & HRTIMER_STATE_ENQUEUED);
477 }
478 
479 /*
480  * Helper function to check, whether the timer is running the callback
481  * function
482  */
hrtimer_callback_running(struct hrtimer * timer)483 static inline int hrtimer_callback_running(struct hrtimer *timer)
484 {
485 	return timer->base->running == timer;
486 }
487 
488 /* Forward a hrtimer so it expires after now: */
489 extern u64
490 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);
491 
492 /**
493  * hrtimer_forward_now - forward the timer expiry so it expires after now
494  * @timer:	hrtimer to forward
495  * @interval:	the interval to forward
496  *
497  * Forward the timer expiry so it will expire after the current time
498  * of the hrtimer clock base. Returns the number of overruns.
499  *
500  * Can be safely called from the callback function of @timer. If
501  * called from other contexts @timer must neither be enqueued nor
502  * running the callback and the caller needs to take care of
503  * serialization.
504  *
505  * Note: This only updates the timer expiry value and does not requeue
506  * the timer.
507  */
hrtimer_forward_now(struct hrtimer * timer,ktime_t interval)508 static inline u64 hrtimer_forward_now(struct hrtimer *timer,
509 				      ktime_t interval)
510 {
511 	return hrtimer_forward(timer, timer->base->get_time(), interval);
512 }
513 
514 /* Precise sleep: */
515 
516 extern int nanosleep_copyout(struct restart_block *, struct timespec64 *);
517 extern long hrtimer_nanosleep(ktime_t rqtp, const enum hrtimer_mode mode,
518 			      const clockid_t clockid);
519 
520 extern int schedule_hrtimeout_range(ktime_t *expires, u64 delta,
521 				    const enum hrtimer_mode mode);
522 extern int schedule_hrtimeout_range_clock(ktime_t *expires,
523 					  u64 delta,
524 					  const enum hrtimer_mode mode,
525 					  clockid_t clock_id);
526 extern int schedule_hrtimeout(ktime_t *expires, const enum hrtimer_mode mode);
527 
528 /* Soft interrupt function to run the hrtimer queues: */
529 extern void hrtimer_run_queues(void);
530 
531 /* Bootup initialization: */
532 extern void __init hrtimers_init(void);
533 
534 /* Show pending timers: */
535 extern void sysrq_timer_list_show(void);
536 
537 int hrtimers_prepare_cpu(unsigned int cpu);
538 #ifdef CONFIG_HOTPLUG_CPU
539 int hrtimers_cpu_dying(unsigned int cpu);
540 #else
541 #define hrtimers_cpu_dying	NULL
542 #endif
543 
544 #endif
545