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