2 * include/linux/hrtimer.h
4 * hrtimers - High-resolution kernel timers
6 * Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de>
7 * Copyright(C) 2005, Red Hat, Inc., Ingo Molnar
9 * data type definitions, declarations, prototypes
11 * Started by: Thomas Gleixner and Ingo Molnar
13 * For licencing details see kernel-base/COPYING
15 #ifndef _LINUX_HRTIMER_H
16 #define _LINUX_HRTIMER_H
18 #include <linux/rbtree.h>
19 #include <linux/ktime.h>
20 #include <linux/init.h>
21 #include <linux/list.h>
22 #include <linux/wait.h>
23 #include <linux/percpu.h>
24 #include <linux/timer.h>
25 #include <linux/timerqueue.h>
27 struct hrtimer_clock_base;
28 struct hrtimer_cpu_base;
31 * Mode arguments of xxx_hrtimer functions:
34 HRTIMER_MODE_ABS = 0x0, /* Time value is absolute */
35 HRTIMER_MODE_REL = 0x1, /* Time value is relative to now */
36 HRTIMER_MODE_PINNED = 0x02, /* Timer is bound to CPU */
37 HRTIMER_MODE_ABS_PINNED = 0x02,
38 HRTIMER_MODE_REL_PINNED = 0x03,
42 * Return values for the callback function
44 enum hrtimer_restart {
45 HRTIMER_NORESTART, /* Timer is not restarted */
46 HRTIMER_RESTART, /* Timer must be restarted */
50 * Values to track state of the timer
55 * 0x01 enqueued into rbtree
56 * 0x02 timer is pinned to a cpu
58 * The callback state is not part of the timer->state because clearing it would
59 * mean touching the timer after the callback, this makes it impossible to free
60 * the timer from the callback function.
62 * Therefore we track the callback state in:
64 * timer->base->cpu_base->running == timer
66 * On SMP it is possible to have a "callback function running and enqueued"
67 * status. It happens for example when a posix timer expired and the callback
68 * queued a signal. Between dropping the lock which protects the posix timer
69 * and reacquiring the base lock of the hrtimer, another CPU can deliver the
70 * signal and rearm the timer.
72 * All state transitions are protected by cpu_base->lock.
74 #define HRTIMER_STATE_INACTIVE 0x00
75 #define HRTIMER_STATE_ENQUEUED 0x01
76 #define HRTIMER_PINNED_SHIFT 1
77 #define HRTIMER_STATE_PINNED (1 << HRTIMER_PINNED_SHIFT)
80 * struct hrtimer - the basic hrtimer structure
81 * @node: timerqueue node, which also manages node.expires,
82 * the absolute expiry time in the hrtimers internal
83 * representation. The time is related to the clock on
84 * which the timer is based. Is setup by adding
85 * slack to the _softexpires value. For non range timers
86 * identical to _softexpires.
87 * @_softexpires: the absolute earliest expiry time of the hrtimer.
88 * The time which was given as expiry time when the timer
90 * @function: timer expiry callback function
91 * @base: pointer to the timer base (per cpu and per clock)
92 * @state: state information (See bit values above)
93 * @is_rel: Set if the timer was armed relative
95 * The hrtimer structure must be initialized by hrtimer_init()
98 struct timerqueue_node node;
100 enum hrtimer_restart (*function)(struct hrtimer *);
101 struct hrtimer_clock_base *base;
107 * struct hrtimer_sleeper - simple sleeper structure
108 * @timer: embedded timer structure
109 * @task: task to wake up
111 * task is set to NULL, when the timer expires.
113 struct hrtimer_sleeper {
114 struct hrtimer timer;
115 struct task_struct *task;
119 # define HRTIMER_CLOCK_BASE_ALIGN 64
121 # define HRTIMER_CLOCK_BASE_ALIGN 32
125 * struct hrtimer_clock_base - the timer base for a specific clock
126 * @cpu_base: per cpu clock base
127 * @index: clock type index for per_cpu support when moving a
128 * timer to a base on another cpu.
129 * @clockid: clock id for per_cpu support
130 * @active: red black tree root node for the active timers
131 * @get_time: function to retrieve the current time of the clock
132 * @offset: offset of this clock to the monotonic base
134 struct hrtimer_clock_base {
135 struct hrtimer_cpu_base *cpu_base;
138 struct timerqueue_head active;
139 ktime_t (*get_time)(void);
141 } __attribute__((__aligned__(HRTIMER_CLOCK_BASE_ALIGN)));
143 enum hrtimer_base_type {
144 HRTIMER_BASE_MONOTONIC,
145 HRTIMER_BASE_REALTIME,
146 HRTIMER_BASE_BOOTTIME,
148 HRTIMER_MAX_CLOCK_BASES,
152 * struct hrtimer_cpu_base - the per cpu clock bases
153 * @lock: lock protecting the base and associated clock bases
155 * @seq: seqcount around __run_hrtimer
156 * @running: pointer to the currently running hrtimer
158 * @active_bases: Bitfield to mark bases with active timers
159 * @clock_was_set_seq: Sequence counter of clock was set events
160 * @migration_enabled: The migration of hrtimers to other cpus is enabled
161 * @nohz_active: The nohz functionality is enabled
162 * @expires_next: absolute time of the next event which was scheduled
163 * via clock_set_next_event()
164 * @next_timer: Pointer to the first expiring timer
165 * @in_hrtirq: hrtimer_interrupt() is currently executing
166 * @hres_active: State of high resolution mode
167 * @hang_detected: The last hrtimer interrupt detected a hang
168 * @nr_events: Total number of hrtimer interrupt events
169 * @nr_retries: Total number of hrtimer interrupt retries
170 * @nr_hangs: Total number of hrtimer interrupt hangs
171 * @max_hang_time: Maximum time spent in hrtimer_interrupt
172 * @clock_base: array of clock bases for this cpu
174 * Note: next_timer is just an optimization for __remove_hrtimer().
175 * Do not dereference the pointer because it is not reliable on
176 * cross cpu removals.
178 struct hrtimer_cpu_base {
181 struct hrtimer *running;
183 unsigned int active_bases;
184 unsigned int clock_was_set_seq;
185 bool migration_enabled;
187 #ifdef CONFIG_HIGH_RES_TIMERS
188 unsigned int in_hrtirq : 1,
191 ktime_t expires_next;
192 struct hrtimer *next_timer;
193 unsigned int nr_events;
194 unsigned int nr_retries;
195 unsigned int nr_hangs;
196 unsigned int max_hang_time;
198 struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES];
199 } ____cacheline_aligned;
201 static inline void hrtimer_set_expires(struct hrtimer *timer, ktime_t time)
203 BUILD_BUG_ON(sizeof(struct hrtimer_clock_base) > HRTIMER_CLOCK_BASE_ALIGN);
205 timer->node.expires = time;
206 timer->_softexpires = time;
209 static inline void hrtimer_set_expires_range(struct hrtimer *timer, ktime_t time, ktime_t delta)
211 timer->_softexpires = time;
212 timer->node.expires = ktime_add_safe(time, delta);
215 static inline void hrtimer_set_expires_range_ns(struct hrtimer *timer, ktime_t time, u64 delta)
217 timer->_softexpires = time;
218 timer->node.expires = ktime_add_safe(time, ns_to_ktime(delta));
221 static inline void hrtimer_set_expires_tv64(struct hrtimer *timer, s64 tv64)
223 timer->node.expires.tv64 = tv64;
224 timer->_softexpires.tv64 = tv64;
227 static inline void hrtimer_add_expires(struct hrtimer *timer, ktime_t time)
229 timer->node.expires = ktime_add_safe(timer->node.expires, time);
230 timer->_softexpires = ktime_add_safe(timer->_softexpires, time);
233 static inline void hrtimer_add_expires_ns(struct hrtimer *timer, u64 ns)
235 timer->node.expires = ktime_add_ns(timer->node.expires, ns);
236 timer->_softexpires = ktime_add_ns(timer->_softexpires, ns);
239 static inline ktime_t hrtimer_get_expires(const struct hrtimer *timer)
241 return timer->node.expires;
244 static inline ktime_t hrtimer_get_softexpires(const struct hrtimer *timer)
246 return timer->_softexpires;
249 static inline s64 hrtimer_get_expires_tv64(const struct hrtimer *timer)
251 return timer->node.expires.tv64;
253 static inline s64 hrtimer_get_softexpires_tv64(const struct hrtimer *timer)
255 return timer->_softexpires.tv64;
258 static inline s64 hrtimer_get_expires_ns(const struct hrtimer *timer)
260 return ktime_to_ns(timer->node.expires);
263 static inline ktime_t hrtimer_expires_remaining(const struct hrtimer *timer)
265 return ktime_sub(timer->node.expires, timer->base->get_time());
268 static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
270 return timer->base->get_time();
273 #ifdef CONFIG_HIGH_RES_TIMERS
274 struct clock_event_device;
276 extern void hrtimer_interrupt(struct clock_event_device *dev);
278 static inline int hrtimer_is_hres_active(struct hrtimer *timer)
280 return timer->base->cpu_base->hres_active;
283 extern void hrtimer_peek_ahead_timers(void);
286 * The resolution of the clocks. The resolution value is returned in
287 * the clock_getres() system call to give application programmers an
288 * idea of the (in)accuracy of timers. Timer values are rounded up to
289 * this resolution values.
291 # define HIGH_RES_NSEC 1
292 # define KTIME_HIGH_RES (ktime_t) { .tv64 = HIGH_RES_NSEC }
293 # define MONOTONIC_RES_NSEC HIGH_RES_NSEC
294 # define KTIME_MONOTONIC_RES KTIME_HIGH_RES
296 extern void clock_was_set_delayed(void);
298 extern unsigned int hrtimer_resolution;
302 # define MONOTONIC_RES_NSEC LOW_RES_NSEC
303 # define KTIME_MONOTONIC_RES KTIME_LOW_RES
305 #define hrtimer_resolution (unsigned int)LOW_RES_NSEC
307 static inline void hrtimer_peek_ahead_timers(void) { }
309 static inline int hrtimer_is_hres_active(struct hrtimer *timer)
314 static inline void clock_was_set_delayed(void) { }
318 static inline ktime_t
319 __hrtimer_expires_remaining_adjusted(const struct hrtimer *timer, ktime_t now)
321 ktime_t rem = ktime_sub(timer->node.expires, now);
324 * Adjust relative timers for the extra we added in
325 * hrtimer_start_range_ns() to prevent short timeouts.
327 if (IS_ENABLED(CONFIG_TIME_LOW_RES) && timer->is_rel)
328 rem.tv64 -= hrtimer_resolution;
332 static inline ktime_t
333 hrtimer_expires_remaining_adjusted(const struct hrtimer *timer)
335 return __hrtimer_expires_remaining_adjusted(timer,
336 timer->base->get_time());
339 extern void clock_was_set(void);
340 #ifdef CONFIG_TIMERFD
341 extern void timerfd_clock_was_set(void);
343 static inline void timerfd_clock_was_set(void) { }
345 extern void hrtimers_resume(void);
347 DECLARE_PER_CPU(struct tick_device, tick_cpu_device);
350 /* Exported timer functions: */
352 /* To be used from cpusets, only */
353 extern void hrtimer_quiesce_cpu(void *cpup);
355 /* Initialize timers: */
356 extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
357 enum hrtimer_mode mode);
359 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
360 extern void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t which_clock,
361 enum hrtimer_mode mode);
363 extern void destroy_hrtimer_on_stack(struct hrtimer *timer);
365 static inline void hrtimer_init_on_stack(struct hrtimer *timer,
366 clockid_t which_clock,
367 enum hrtimer_mode mode)
369 hrtimer_init(timer, which_clock, mode);
371 static inline void destroy_hrtimer_on_stack(struct hrtimer *timer) { }
374 /* Basic timer operations: */
375 extern void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
376 u64 range_ns, const enum hrtimer_mode mode);
379 * hrtimer_start - (re)start an hrtimer on the current CPU
380 * @timer: the timer to be added
382 * @mode: expiry mode: absolute (HRTIMER_MODE_ABS) or
383 * relative (HRTIMER_MODE_REL)
385 static inline void hrtimer_start(struct hrtimer *timer, ktime_t tim,
386 const enum hrtimer_mode mode)
388 hrtimer_start_range_ns(timer, tim, 0, mode);
391 extern int hrtimer_cancel(struct hrtimer *timer);
392 extern int hrtimer_try_to_cancel(struct hrtimer *timer);
394 static inline void hrtimer_start_expires(struct hrtimer *timer,
395 enum hrtimer_mode mode)
399 soft = hrtimer_get_softexpires(timer);
400 hard = hrtimer_get_expires(timer);
401 delta = ktime_to_ns(ktime_sub(hard, soft));
402 hrtimer_start_range_ns(timer, soft, delta, mode);
405 static inline void hrtimer_restart(struct hrtimer *timer)
407 hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
411 extern ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust);
413 static inline ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
415 return __hrtimer_get_remaining(timer, false);
418 extern u64 hrtimer_get_next_event(void);
420 extern bool hrtimer_active(const struct hrtimer *timer);
423 * Helper function to check, whether the timer is on one of the queues
425 static inline int hrtimer_is_queued(struct hrtimer *timer)
427 return timer->state & HRTIMER_STATE_ENQUEUED;
431 * Helper function to check, whether the timer is running the callback
434 static inline int hrtimer_callback_running(struct hrtimer *timer)
436 return timer->base->cpu_base->running == timer;
439 /* Forward a hrtimer so it expires after now: */
441 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);
444 * hrtimer_forward_now - forward the timer expiry so it expires after now
445 * @timer: hrtimer to forward
446 * @interval: the interval to forward
448 * Forward the timer expiry so it will expire after the current time
449 * of the hrtimer clock base. Returns the number of overruns.
451 * Can be safely called from the callback function of @timer. If
452 * called from other contexts @timer must neither be enqueued nor
453 * running the callback and the caller needs to take care of
456 * Note: This only updates the timer expiry value and does not requeue
459 static inline u64 hrtimer_forward_now(struct hrtimer *timer,
462 return hrtimer_forward(timer, timer->base->get_time(), interval);
466 extern long hrtimer_nanosleep(struct timespec *rqtp,
467 struct timespec __user *rmtp,
468 const enum hrtimer_mode mode,
469 const clockid_t clockid);
470 extern long hrtimer_nanosleep_restart(struct restart_block *restart_block);
472 extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl,
473 struct task_struct *tsk);
475 extern int schedule_hrtimeout_range(ktime_t *expires, u64 delta,
476 const enum hrtimer_mode mode);
477 extern int schedule_hrtimeout_range_clock(ktime_t *expires,
479 const enum hrtimer_mode mode,
481 extern int schedule_hrtimeout(ktime_t *expires, const enum hrtimer_mode mode);
483 /* Soft interrupt function to run the hrtimer queues: */
484 extern void hrtimer_run_queues(void);
486 /* Bootup initialization: */
487 extern void __init hrtimers_init(void);
489 /* Show pending timers: */
490 extern void sysrq_timer_list_show(void);