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
24 struct hrtimer_clock_base;
25 struct hrtimer_cpu_base;
26
27 /*
28 * Mode arguments of xxx_hrtimer functions:
29 *
30 * HRTIMER_MODE_ABS - Time value is absolute
31 * HRTIMER_MODE_REL - Time value is relative to now
32 * HRTIMER_MODE_PINNED - Timer is bound to CPU (is only considered
33 * when starting the timer)
34 * HRTIMER_MODE_SOFT - Timer callback function will be executed in
35 * soft irq context
36 * HRTIMER_MODE_HARD - Timer callback function will be executed in
37 * hard irq context even on PREEMPT_RT.
38 */
39 enum hrtimer_mode {
40 HRTIMER_MODE_ABS = 0x00,
41 HRTIMER_MODE_REL = 0x01,
42 HRTIMER_MODE_PINNED = 0x02,
43 HRTIMER_MODE_SOFT = 0x04,
44 HRTIMER_MODE_HARD = 0x08,
45
46 HRTIMER_MODE_ABS_PINNED = HRTIMER_MODE_ABS | HRTIMER_MODE_PINNED,
47 HRTIMER_MODE_REL_PINNED = HRTIMER_MODE_REL | HRTIMER_MODE_PINNED,
48
49 HRTIMER_MODE_ABS_SOFT = HRTIMER_MODE_ABS | HRTIMER_MODE_SOFT,
50 HRTIMER_MODE_REL_SOFT = HRTIMER_MODE_REL | HRTIMER_MODE_SOFT,
51
52 HRTIMER_MODE_ABS_PINNED_SOFT = HRTIMER_MODE_ABS_PINNED | HRTIMER_MODE_SOFT,
53 HRTIMER_MODE_REL_PINNED_SOFT = HRTIMER_MODE_REL_PINNED | HRTIMER_MODE_SOFT,
54
55 HRTIMER_MODE_ABS_HARD = HRTIMER_MODE_ABS | HRTIMER_MODE_HARD,
56 HRTIMER_MODE_REL_HARD = HRTIMER_MODE_REL | HRTIMER_MODE_HARD,
57
58 HRTIMER_MODE_ABS_PINNED_HARD = HRTIMER_MODE_ABS_PINNED | HRTIMER_MODE_HARD,
59 HRTIMER_MODE_REL_PINNED_HARD = HRTIMER_MODE_REL_PINNED | HRTIMER_MODE_HARD,
60 };
61
62 /*
63 * Return values for the callback function
64 */
65 enum hrtimer_restart {
66 HRTIMER_NORESTART, /* Timer is not restarted */
67 HRTIMER_RESTART, /* Timer must be restarted */
68 };
69
70 /*
71 * Values to track state of the timer
72 *
73 * Possible states:
74 *
75 * 0x00 inactive
76 * 0x01 enqueued into rbtree
77 * 0x02 timer is pinned to a cpu
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 #define HRTIMER_PINNED_SHIFT 1
98 #define HRTIMER_STATE_PINNED (1 << HRTIMER_PINNED_SHIFT)
99
100 /**
101 * struct hrtimer - the basic hrtimer structure
102 * @node: timerqueue node, which also manages node.expires,
103 * the absolute expiry time in the hrtimers internal
104 * representation. The time is related to the clock on
105 * which the timer is based. Is setup by adding
106 * slack to the _softexpires value. For non range timers
107 * identical to _softexpires.
108 * @_softexpires: the absolute earliest expiry time of the hrtimer.
109 * The time which was given as expiry time when the timer
110 * was armed.
111 * @function: timer expiry callback function
112 * @base: pointer to the timer base (per cpu and per clock)
113 * @state: state information (See bit values above)
114 * @is_rel: Set if the timer was armed relative
115 * @is_soft: Set if hrtimer will be expired in soft interrupt context.
116 * @is_hard: Set if hrtimer will be expired in hard interrupt context
117 * even on RT.
118 *
119 * The hrtimer structure must be initialized by hrtimer_init()
120 */
121 struct hrtimer {
122 struct timerqueue_node node;
123 ktime_t _softexpires;
124 enum hrtimer_restart (*function)(struct hrtimer *);
125 struct hrtimer_clock_base *base;
126 u8 state;
127 u8 is_rel;
128 u8 is_soft;
129 u8 is_hard;
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 #ifdef CONFIG_CPU_ISOLATION_OPT
373 /* To be used from cpusets, only */
374 extern void hrtimer_quiesce_cpu(void *cpup);
375 #else
hrtimer_quiesce_cpu(void * cpup)376 static inline void hrtimer_quiesce_cpu(void *cpup) { }
377 #endif
378
379 /* Initialize timers: */
380 extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
381 enum hrtimer_mode mode);
382 extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, clockid_t clock_id,
383 enum hrtimer_mode mode);
384
385 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
386 extern void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t which_clock,
387 enum hrtimer_mode mode);
388 extern void hrtimer_init_sleeper_on_stack(struct hrtimer_sleeper *sl,
389 clockid_t clock_id,
390 enum hrtimer_mode mode);
391
392 extern void destroy_hrtimer_on_stack(struct hrtimer *timer);
393 #else
hrtimer_init_on_stack(struct hrtimer * timer,clockid_t which_clock,enum hrtimer_mode mode)394 static inline void hrtimer_init_on_stack(struct hrtimer *timer,
395 clockid_t which_clock,
396 enum hrtimer_mode mode)
397 {
398 hrtimer_init(timer, which_clock, mode);
399 }
400
hrtimer_init_sleeper_on_stack(struct hrtimer_sleeper * sl,clockid_t clock_id,enum hrtimer_mode mode)401 static inline void hrtimer_init_sleeper_on_stack(struct hrtimer_sleeper *sl,
402 clockid_t clock_id,
403 enum hrtimer_mode mode)
404 {
405 hrtimer_init_sleeper(sl, clock_id, mode);
406 }
407
destroy_hrtimer_on_stack(struct hrtimer * timer)408 static inline void destroy_hrtimer_on_stack(struct hrtimer *timer) { }
409 #endif
410
411 /* Basic timer operations: */
412 extern void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
413 u64 range_ns, const enum hrtimer_mode mode);
414
415 /**
416 * hrtimer_start - (re)start an hrtimer
417 * @timer: the timer to be added
418 * @tim: expiry time
419 * @mode: timer mode: absolute (HRTIMER_MODE_ABS) or
420 * relative (HRTIMER_MODE_REL), and pinned (HRTIMER_MODE_PINNED);
421 * softirq based mode is considered for debug purpose only!
422 */
hrtimer_start(struct hrtimer * timer,ktime_t tim,const enum hrtimer_mode mode)423 static inline void hrtimer_start(struct hrtimer *timer, ktime_t tim,
424 const enum hrtimer_mode mode)
425 {
426 hrtimer_start_range_ns(timer, tim, 0, mode);
427 }
428
429 extern int hrtimer_cancel(struct hrtimer *timer);
430 extern int hrtimer_try_to_cancel(struct hrtimer *timer);
431
hrtimer_start_expires(struct hrtimer * timer,enum hrtimer_mode mode)432 static inline void hrtimer_start_expires(struct hrtimer *timer,
433 enum hrtimer_mode mode)
434 {
435 u64 delta;
436 ktime_t soft, hard;
437 soft = hrtimer_get_softexpires(timer);
438 hard = hrtimer_get_expires(timer);
439 delta = ktime_to_ns(ktime_sub(hard, soft));
440 hrtimer_start_range_ns(timer, soft, delta, mode);
441 }
442
443 void hrtimer_sleeper_start_expires(struct hrtimer_sleeper *sl,
444 enum hrtimer_mode mode);
445
hrtimer_restart(struct hrtimer * timer)446 static inline void hrtimer_restart(struct hrtimer *timer)
447 {
448 hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
449 }
450
451 /* Query timers: */
452 extern ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust);
453
hrtimer_get_remaining(const struct hrtimer * timer)454 static inline ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
455 {
456 return __hrtimer_get_remaining(timer, false);
457 }
458
459 extern u64 hrtimer_get_next_event(void);
460 extern u64 hrtimer_next_event_without(const struct hrtimer *exclude);
461
462 extern bool hrtimer_active(const struct hrtimer *timer);
463
464 /**
465 * hrtimer_is_queued = check, whether the timer is on one of the queues
466 * @timer: Timer to check
467 *
468 * Returns: True if the timer is queued, false otherwise
469 *
470 * The function can be used lockless, but it gives only a current snapshot.
471 */
hrtimer_is_queued(struct hrtimer * timer)472 static inline bool hrtimer_is_queued(struct hrtimer *timer)
473 {
474 /* The READ_ONCE pairs with the update functions of timer->state */
475 return !!(READ_ONCE(timer->state) & HRTIMER_STATE_ENQUEUED);
476 }
477
478 /*
479 * Helper function to check, whether the timer is running the callback
480 * function
481 */
hrtimer_callback_running(struct hrtimer * timer)482 static inline int hrtimer_callback_running(struct hrtimer *timer)
483 {
484 return timer->base->running == timer;
485 }
486
487 /* Forward a hrtimer so it expires after now: */
488 extern u64
489 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);
490
491 /**
492 * hrtimer_forward_now - forward the timer expiry so it expires after now
493 * @timer: hrtimer to forward
494 * @interval: the interval to forward
495 *
496 * Forward the timer expiry so it will expire after the current time
497 * of the hrtimer clock base. Returns the number of overruns.
498 *
499 * Can be safely called from the callback function of @timer. If
500 * called from other contexts @timer must neither be enqueued nor
501 * running the callback and the caller needs to take care of
502 * serialization.
503 *
504 * Note: This only updates the timer expiry value and does not requeue
505 * the timer.
506 */
hrtimer_forward_now(struct hrtimer * timer,ktime_t interval)507 static inline u64 hrtimer_forward_now(struct hrtimer *timer,
508 ktime_t interval)
509 {
510 return hrtimer_forward(timer, timer->base->get_time(), interval);
511 }
512
513 /* Precise sleep: */
514
515 extern int nanosleep_copyout(struct restart_block *, struct timespec64 *);
516 extern long hrtimer_nanosleep(ktime_t rqtp, const enum hrtimer_mode mode,
517 const clockid_t clockid);
518
519 extern int schedule_hrtimeout_range(ktime_t *expires, u64 delta,
520 const enum hrtimer_mode mode);
521 extern int schedule_hrtimeout_range_clock(ktime_t *expires,
522 u64 delta,
523 const enum hrtimer_mode mode,
524 clockid_t clock_id);
525 extern int schedule_hrtimeout(ktime_t *expires, const enum hrtimer_mode mode);
526
527 /* Soft interrupt function to run the hrtimer queues: */
528 extern void hrtimer_run_queues(void);
529
530 /* Bootup initialization: */
531 extern void __init hrtimers_init(void);
532
533 /* Show pending timers: */
534 extern void sysrq_timer_list_show(void);
535
536 int hrtimers_prepare_cpu(unsigned int cpu);
537 #ifdef CONFIG_HOTPLUG_CPU
538 int hrtimers_dead_cpu(unsigned int cpu);
539 #else
540 #define hrtimers_dead_cpu NULL
541 #endif
542
543 #endif
544