Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * hrtimers - High-resolution kernel timers
4 *
5 * Copyright(C) 2005, Linutronix GmbH, Thomas Gleixner <tglx@kernel.org>
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/hrtimer_rearm.h>
17#include <linux/hrtimer_types.h>
18#include <linux/init.h>
19#include <linux/list.h>
20#include <linux/percpu-defs.h>
21#include <linux/rbtree.h>
22#include <linux/timer.h>
23
24/*
25 * Mode arguments of xxx_hrtimer functions:
26 *
27 * HRTIMER_MODE_ABS - Time value is absolute
28 * HRTIMER_MODE_REL - Time value is relative to now
29 * HRTIMER_MODE_PINNED - Timer is bound to CPU (is only considered
30 * when starting the timer)
31 * HRTIMER_MODE_SOFT - Timer callback function will be executed in
32 * soft irq context
33 * HRTIMER_MODE_HARD - Timer callback function will be executed in
34 * hard irq context even on PREEMPT_RT.
35 * HRTIMER_MODE_LAZY_REARM - Avoid reprogramming if the timer was the
36 * first expiring timer and is moved into the
37 * future. Special mode for the HRTICK timer to
38 * avoid extensive reprogramming of the hardware,
39 * which is expensive in virtual machines. Risks
40 * a pointless expiry, but that's better than
41 * reprogramming on every context switch,
42 */
43enum hrtimer_mode {
44 HRTIMER_MODE_ABS = 0x00,
45 HRTIMER_MODE_REL = 0x01,
46 HRTIMER_MODE_PINNED = 0x02,
47 HRTIMER_MODE_SOFT = 0x04,
48 HRTIMER_MODE_HARD = 0x08,
49 HRTIMER_MODE_LAZY_REARM = 0x10,
50
51 HRTIMER_MODE_ABS_PINNED = HRTIMER_MODE_ABS | HRTIMER_MODE_PINNED,
52 HRTIMER_MODE_REL_PINNED = HRTIMER_MODE_REL | HRTIMER_MODE_PINNED,
53
54 HRTIMER_MODE_ABS_SOFT = HRTIMER_MODE_ABS | HRTIMER_MODE_SOFT,
55 HRTIMER_MODE_REL_SOFT = HRTIMER_MODE_REL | HRTIMER_MODE_SOFT,
56
57 HRTIMER_MODE_ABS_PINNED_SOFT = HRTIMER_MODE_ABS_PINNED | HRTIMER_MODE_SOFT,
58 HRTIMER_MODE_REL_PINNED_SOFT = HRTIMER_MODE_REL_PINNED | HRTIMER_MODE_SOFT,
59
60 HRTIMER_MODE_ABS_HARD = HRTIMER_MODE_ABS | HRTIMER_MODE_HARD,
61 HRTIMER_MODE_REL_HARD = HRTIMER_MODE_REL | HRTIMER_MODE_HARD,
62
63 HRTIMER_MODE_ABS_PINNED_HARD = HRTIMER_MODE_ABS_PINNED | HRTIMER_MODE_HARD,
64 HRTIMER_MODE_REL_PINNED_HARD = HRTIMER_MODE_REL_PINNED | HRTIMER_MODE_HARD,
65};
66
67/**
68 * struct hrtimer_sleeper - simple sleeper structure
69 * @timer: embedded timer structure
70 * @task: task to wake up
71 *
72 * task is set to NULL, when the timer expires.
73 */
74struct hrtimer_sleeper {
75 struct hrtimer timer;
76 struct task_struct *task;
77};
78
79static inline void hrtimer_set_expires(struct hrtimer *timer, ktime_t time)
80{
81 timer->node.expires = time;
82 timer->_softexpires = time;
83}
84
85static inline void hrtimer_set_expires_range(struct hrtimer *timer, ktime_t time, ktime_t delta)
86{
87 timer->_softexpires = time;
88 timer->node.expires = ktime_add_safe(time, delta);
89}
90
91static inline void hrtimer_set_expires_range_ns(struct hrtimer *timer, ktime_t time, u64 delta)
92{
93 timer->_softexpires = time;
94 timer->node.expires = ktime_add_safe(time, ns_to_ktime(delta));
95}
96
97static inline void hrtimer_add_expires(struct hrtimer *timer, ktime_t time)
98{
99 timer->node.expires = ktime_add_safe(timer->node.expires, time);
100 timer->_softexpires = ktime_add_safe(timer->_softexpires, time);
101}
102
103static inline void hrtimer_add_expires_ns(struct hrtimer *timer, u64 ns)
104{
105 timer->node.expires = ktime_add_ns(timer->node.expires, ns);
106 timer->_softexpires = ktime_add_ns(timer->_softexpires, ns);
107}
108
109static inline ktime_t hrtimer_get_expires(const struct hrtimer *timer)
110{
111 return timer->node.expires;
112}
113
114static inline ktime_t hrtimer_get_softexpires(const struct hrtimer *timer)
115{
116 return timer->_softexpires;
117}
118
119ktime_t hrtimer_cb_get_time(const struct hrtimer *timer);
120
121static inline ktime_t hrtimer_expires_remaining(const struct hrtimer *timer)
122{
123 return ktime_sub(timer->node.expires, hrtimer_cb_get_time(timer));
124}
125
126#ifdef CONFIG_HIGH_RES_TIMERS
127extern unsigned int hrtimer_resolution;
128struct clock_event_device;
129
130extern void hrtimer_interrupt(struct clock_event_device *dev);
131
132extern struct static_key_false hrtimer_highres_enabled_key;
133
134static inline bool hrtimer_highres_enabled(void)
135{
136 return static_branch_likely(&hrtimer_highres_enabled_key);
137}
138
139#else /* CONFIG_HIGH_RES_TIMERS */
140#define hrtimer_resolution (unsigned int)LOW_RES_NSEC
141static inline bool hrtimer_highres_enabled(void) { return false; }
142#endif /* !CONFIG_HIGH_RES_TIMERS */
143
144static inline ktime_t
145__hrtimer_expires_remaining_adjusted(const struct hrtimer *timer, ktime_t now)
146{
147 ktime_t rem = ktime_sub(timer->node.expires, now);
148
149 /*
150 * Adjust relative timers for the extra we added in
151 * hrtimer_start_range_ns() to prevent short timeouts.
152 */
153 if (IS_ENABLED(CONFIG_TIME_LOW_RES) && timer->is_rel)
154 rem -= hrtimer_resolution;
155 return rem;
156}
157
158static inline ktime_t
159hrtimer_expires_remaining_adjusted(const struct hrtimer *timer)
160{
161 return __hrtimer_expires_remaining_adjusted(timer, hrtimer_cb_get_time(timer));
162}
163
164#ifdef CONFIG_TIMERFD
165extern void timerfd_clock_was_set(void);
166extern void timerfd_resume(void);
167#else
168static inline void timerfd_clock_was_set(void) { }
169static inline void timerfd_resume(void) { }
170#endif
171
172DECLARE_PER_CPU(struct tick_device, tick_cpu_device);
173
174#ifdef CONFIG_PREEMPT_RT
175void hrtimer_cancel_wait_running(const struct hrtimer *timer);
176#else
177static inline void hrtimer_cancel_wait_running(struct hrtimer *timer)
178{
179 cpu_relax();
180}
181#endif
182
183static inline enum hrtimer_restart hrtimer_dummy_timeout(struct hrtimer *unused)
184{
185 return HRTIMER_NORESTART;
186}
187
188/* Exported timer functions: */
189
190/* Initialize timers: */
191extern void hrtimer_setup(struct hrtimer *timer, enum hrtimer_restart (*function)(struct hrtimer *),
192 clockid_t clock_id, enum hrtimer_mode mode);
193extern void hrtimer_setup_on_stack(struct hrtimer *timer,
194 enum hrtimer_restart (*function)(struct hrtimer *),
195 clockid_t clock_id, enum hrtimer_mode mode);
196extern void hrtimer_setup_sleeper_on_stack(struct hrtimer_sleeper *sl, clockid_t clock_id,
197 enum hrtimer_mode mode);
198
199#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
200extern void destroy_hrtimer_on_stack(struct hrtimer *timer);
201#else
202static inline void destroy_hrtimer_on_stack(struct hrtimer *timer) { }
203#endif
204
205/* Basic timer operations: */
206extern void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
207 u64 range_ns, const enum hrtimer_mode mode);
208
209/**
210 * hrtimer_start - (re)start an hrtimer
211 * @timer: the timer to be added
212 * @tim: expiry time
213 * @mode: timer mode: absolute (HRTIMER_MODE_ABS) or
214 * relative (HRTIMER_MODE_REL), and pinned (HRTIMER_MODE_PINNED);
215 * softirq based mode is considered for debug purpose only!
216 */
217static inline void hrtimer_start(struct hrtimer *timer, ktime_t tim,
218 const enum hrtimer_mode mode)
219{
220 hrtimer_start_range_ns(timer, tim, 0, mode);
221}
222
223extern int hrtimer_cancel(struct hrtimer *timer);
224extern int hrtimer_try_to_cancel(struct hrtimer *timer);
225
226static inline void hrtimer_start_expires(struct hrtimer *timer,
227 enum hrtimer_mode mode)
228{
229 u64 delta;
230 ktime_t soft, hard;
231 soft = hrtimer_get_softexpires(timer);
232 hard = hrtimer_get_expires(timer);
233 delta = ktime_to_ns(ktime_sub(hard, soft));
234 hrtimer_start_range_ns(timer, soft, delta, mode);
235}
236
237void hrtimer_sleeper_start_expires(struct hrtimer_sleeper *sl,
238 enum hrtimer_mode mode);
239
240static inline void hrtimer_restart(struct hrtimer *timer)
241{
242 hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
243}
244
245/* Query timers: */
246extern ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust);
247
248/**
249 * hrtimer_get_remaining - get remaining time for the timer
250 * @timer: the timer to read
251 */
252static inline ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
253{
254 return __hrtimer_get_remaining(timer, false);
255}
256
257extern u64 hrtimer_get_next_event(void);
258extern u64 hrtimer_next_event_without(const struct hrtimer *exclude);
259
260extern bool hrtimer_active(const struct hrtimer *timer);
261
262/**
263 * hrtimer_is_queued - check, whether the timer is on one of the queues
264 * @timer: Timer to check
265 *
266 * Returns: True if the timer is queued, false otherwise
267 *
268 * The function can be used lockless, but it gives only a current snapshot.
269 */
270static inline bool hrtimer_is_queued(struct hrtimer *timer)
271{
272 /* The READ_ONCE pairs with the update functions of timer->is_queued */
273 return READ_ONCE(timer->is_queued);
274}
275
276/*
277 * Helper function to check, whether the timer is running the callback
278 * function
279 */
280static inline int hrtimer_callback_running(struct hrtimer *timer)
281{
282 return timer->base->running == timer;
283}
284
285/**
286 * hrtimer_update_function - Update the timer's callback function
287 * @timer: Timer to update
288 * @function: New callback function
289 *
290 * Only safe to call if the timer is not enqueued. Can be called in the callback function if the
291 * timer is not enqueued at the same time (see the comments above HRTIMER_STATE_ENQUEUED).
292 */
293static inline void hrtimer_update_function(struct hrtimer *timer,
294 enum hrtimer_restart (*function)(struct hrtimer *))
295{
296#ifdef CONFIG_PROVE_LOCKING
297 guard(raw_spinlock_irqsave)(&timer->base->cpu_base->lock);
298
299 if (WARN_ON_ONCE(hrtimer_is_queued(timer)))
300 return;
301
302 if (WARN_ON_ONCE(!function))
303 return;
304#endif
305 ACCESS_PRIVATE(timer, function) = function;
306}
307
308/* Forward a hrtimer so it expires after now: */
309extern u64
310hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);
311
312/**
313 * hrtimer_forward_now() - forward the timer expiry so it expires after now
314 * @timer: hrtimer to forward
315 * @interval: the interval to forward
316 *
317 * It is a variant of hrtimer_forward(). The timer will expire after the current
318 * time of the hrtimer clock base. See hrtimer_forward() for details.
319 */
320static inline u64 hrtimer_forward_now(struct hrtimer *timer,
321 ktime_t interval)
322{
323 return hrtimer_forward(timer, hrtimer_cb_get_time(timer), interval);
324}
325
326/* Precise sleep: */
327
328extern int nanosleep_copyout(struct restart_block *, struct timespec64 *);
329extern long hrtimer_nanosleep(ktime_t rqtp, const enum hrtimer_mode mode,
330 const clockid_t clockid);
331
332extern int schedule_hrtimeout_range(ktime_t *expires, u64 delta,
333 const enum hrtimer_mode mode);
334extern int schedule_hrtimeout_range_clock(ktime_t *expires,
335 u64 delta,
336 const enum hrtimer_mode mode,
337 clockid_t clock_id);
338extern int schedule_hrtimeout(ktime_t *expires, const enum hrtimer_mode mode);
339
340/* Soft interrupt function to run the hrtimer queues: */
341extern void hrtimer_run_queues(void);
342
343/* Bootup initialization: */
344extern void __init hrtimers_init(void);
345
346/* Show pending timers: */
347extern void sysrq_timer_list_show(void);
348
349int hrtimers_prepare_cpu(unsigned int cpu);
350int hrtimers_cpu_starting(unsigned int cpu);
351#ifdef CONFIG_HOTPLUG_CPU
352int hrtimers_cpu_dying(unsigned int cpu);
353#else
354#define hrtimers_cpu_dying NULL
355#endif
356
357#endif