1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <linux/list.h>
6 #include <linux/ktime.h>
7 #include <linux/stddef.h>
8 #include <linux/debugobjects.h>
9 #include <linux/stringify.h>
10 #include <linux/timer_types.h>
14 * NB: because we have to copy the lockdep_map, setting the lockdep_map key
15 * (second argument) here is required, otherwise it could be initialised to
16 * the copy of the lockdep_map later! We use the pointer to and the string
17 * "<file>:<line>" as the key resp. the name of the lockdep_map.
19 #define __TIMER_LOCKDEP_MAP_INITIALIZER(_kn) \
20 .lockdep_map = STATIC_LOCKDEP_MAP_INIT(_kn, &_kn),
22 #define __TIMER_LOCKDEP_MAP_INITIALIZER(_kn)
26 * @TIMER_DEFERRABLE: A deferrable timer will work normally when the
27 * system is busy, but will not cause a CPU to come out of idle just
28 * to service it; instead, the timer will be serviced when the CPU
29 * eventually wakes up with a subsequent non-deferrable timer.
31 * @TIMER_IRQSAFE: An irqsafe timer is executed with IRQ disabled and
32 * it's safe to wait for the completion of the running instance from
33 * IRQ handlers, for example, by calling del_timer_sync().
35 * Note: The irq disabled callback execution is a special case for
36 * workqueue locking issues. It's not meant for executing random crap
37 * with interrupts disabled. Abuse is monitored!
39 * @TIMER_PINNED: A pinned timer will always expire on the CPU on which the
40 * timer was enqueued. When a particular CPU is required, add_timer_on()
41 * has to be used. Enqueue via mod_timer() and add_timer() is always done
44 #define TIMER_CPUMASK 0x0003FFFF
45 #define TIMER_MIGRATING 0x00040000
46 #define TIMER_BASEMASK (TIMER_CPUMASK | TIMER_MIGRATING)
47 #define TIMER_DEFERRABLE 0x00080000
48 #define TIMER_PINNED 0x00100000
49 #define TIMER_IRQSAFE 0x00200000
50 #define TIMER_INIT_FLAGS (TIMER_DEFERRABLE | TIMER_PINNED | TIMER_IRQSAFE)
51 #define TIMER_ARRAYSHIFT 22
52 #define TIMER_ARRAYMASK 0xFFC00000
54 #define TIMER_TRACE_FLAGMASK (TIMER_MIGRATING | TIMER_DEFERRABLE | TIMER_PINNED | TIMER_IRQSAFE)
56 #define __TIMER_INITIALIZER(_function, _flags) { \
57 .entry = { .next = TIMER_ENTRY_STATIC }, \
58 .function = (_function), \
60 __TIMER_LOCKDEP_MAP_INITIALIZER(FILE_LINE) \
63 #define DEFINE_TIMER(_name, _function) \
64 struct timer_list _name = \
65 __TIMER_INITIALIZER(_function, 0)
68 * LOCKDEP and DEBUG timer interfaces.
70 void init_timer_key(struct timer_list
*timer
,
71 void (*func
)(struct timer_list
*), unsigned int flags
,
72 const char *name
, struct lock_class_key
*key
);
74 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
75 extern void init_timer_on_stack_key(struct timer_list
*timer
,
76 void (*func
)(struct timer_list
*),
77 unsigned int flags
, const char *name
,
78 struct lock_class_key
*key
);
80 static inline void init_timer_on_stack_key(struct timer_list
*timer
,
81 void (*func
)(struct timer_list
*),
84 struct lock_class_key
*key
)
86 init_timer_key(timer
, func
, flags
, name
, key
);
91 #define __init_timer(_timer, _fn, _flags) \
93 static struct lock_class_key __key; \
94 init_timer_key((_timer), (_fn), (_flags), #_timer, &__key);\
97 #define __init_timer_on_stack(_timer, _fn, _flags) \
99 static struct lock_class_key __key; \
100 init_timer_on_stack_key((_timer), (_fn), (_flags), \
104 #define __init_timer(_timer, _fn, _flags) \
105 init_timer_key((_timer), (_fn), (_flags), NULL, NULL)
106 #define __init_timer_on_stack(_timer, _fn, _flags) \
107 init_timer_on_stack_key((_timer), (_fn), (_flags), NULL, NULL)
111 * timer_setup - prepare a timer for first use
112 * @timer: the timer in question
113 * @callback: the function to call when timer expires
114 * @flags: any TIMER_* flags
116 * Regular timer initialization should use either DEFINE_TIMER() above,
117 * or timer_setup(). For timers on the stack, timer_setup_on_stack() must
118 * be used and must be balanced with a call to destroy_timer_on_stack().
120 #define timer_setup(timer, callback, flags) \
121 __init_timer((timer), (callback), (flags))
123 #define timer_setup_on_stack(timer, callback, flags) \
124 __init_timer_on_stack((timer), (callback), (flags))
126 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
127 extern void destroy_timer_on_stack(struct timer_list
*timer
);
129 static inline void destroy_timer_on_stack(struct timer_list
*timer
) { }
132 #define from_timer(var, callback_timer, timer_fieldname) \
133 container_of(callback_timer, typeof(*var), timer_fieldname)
136 * timer_pending - is a timer pending?
137 * @timer: the timer in question
139 * timer_pending will tell whether a given timer is currently pending,
140 * or not. Callers must ensure serialization wrt. other operations done
141 * to this timer, eg. interrupt contexts, or other CPUs on SMP.
143 * Returns: 1 if the timer is pending, 0 if not.
145 static inline int timer_pending(const struct timer_list
* timer
)
147 return !hlist_unhashed_lockless(&timer
->entry
);
150 extern void add_timer_on(struct timer_list
*timer
, int cpu
);
151 extern int mod_timer(struct timer_list
*timer
, unsigned long expires
);
152 extern int mod_timer_pending(struct timer_list
*timer
, unsigned long expires
);
153 extern int timer_reduce(struct timer_list
*timer
, unsigned long expires
);
156 * The jiffies value which is added to now, when there is no timer
157 * in the timer wheel:
159 #define NEXT_TIMER_MAX_DELTA ((1UL << 30) - 1)
161 extern void add_timer(struct timer_list
*timer
);
162 extern void add_timer_local(struct timer_list
*timer
);
163 extern void add_timer_global(struct timer_list
*timer
);
165 extern int try_to_del_timer_sync(struct timer_list
*timer
);
166 extern int timer_delete_sync(struct timer_list
*timer
);
167 extern int timer_delete(struct timer_list
*timer
);
168 extern int timer_shutdown_sync(struct timer_list
*timer
);
169 extern int timer_shutdown(struct timer_list
*timer
);
172 * del_timer_sync - Delete a pending timer and wait for a running callback
173 * @timer: The timer to be deleted
175 * See timer_delete_sync() for detailed explanation.
177 * Do not use in new code. Use timer_delete_sync() instead.
180 * * %0 - The timer was not pending
181 * * %1 - The timer was pending and deactivated
183 static inline int del_timer_sync(struct timer_list
*timer
)
185 return timer_delete_sync(timer
);
189 * del_timer - Delete a pending timer
190 * @timer: The timer to be deleted
192 * See timer_delete() for detailed explanation.
194 * Do not use in new code. Use timer_delete() instead.
197 * * %0 - The timer was not pending
198 * * %1 - The timer was pending and deactivated
200 static inline int del_timer(struct timer_list
*timer
)
202 return timer_delete(timer
);
205 extern void init_timers(void);
207 extern enum hrtimer_restart
it_real_fn(struct hrtimer
*);
209 unsigned long __round_jiffies(unsigned long j
, int cpu
);
210 unsigned long __round_jiffies_relative(unsigned long j
, int cpu
);
211 unsigned long round_jiffies(unsigned long j
);
212 unsigned long round_jiffies_relative(unsigned long j
);
214 unsigned long __round_jiffies_up(unsigned long j
, int cpu
);
215 unsigned long __round_jiffies_up_relative(unsigned long j
, int cpu
);
216 unsigned long round_jiffies_up(unsigned long j
);
217 unsigned long round_jiffies_up_relative(unsigned long j
);
219 #ifdef CONFIG_HOTPLUG_CPU
220 int timers_prepare_cpu(unsigned int cpu
);
221 int timers_dead_cpu(unsigned int cpu
);
223 #define timers_prepare_cpu NULL
224 #define timers_dead_cpu NULL