xref: /linux/include/linux/timer.h (revision be54f8c558027a218423134dd9b8c7c46d92204a)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_TIMER_H
3 #define _LINUX_TIMER_H
4 
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>
11 
12 #ifdef CONFIG_LOCKDEP
13 /*
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.
18  */
19 #define __TIMER_LOCKDEP_MAP_INITIALIZER(_kn)				\
20 	.lockdep_map = STATIC_LOCKDEP_MAP_INIT(_kn, &_kn),
21 #else
22 #define __TIMER_LOCKDEP_MAP_INITIALIZER(_kn)
23 #endif
24 
25 /*
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.
30  *
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 timer_delete_sync().
34  *
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!
38  *
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
42  * on the local CPU.
43  */
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
53 
54 #define TIMER_TRACE_FLAGMASK	(TIMER_MIGRATING | TIMER_DEFERRABLE | TIMER_PINNED | TIMER_IRQSAFE)
55 
56 #define __TIMER_INITIALIZER(_function, _flags) {		\
57 		.entry = { .next = TIMER_ENTRY_STATIC },	\
58 		.function = (_function),			\
59 		.flags = (_flags),				\
60 		__TIMER_LOCKDEP_MAP_INITIALIZER(FILE_LINE)	\
61 	}
62 
63 #define DEFINE_TIMER(_name, _function)				\
64 	struct timer_list _name =				\
65 		__TIMER_INITIALIZER(_function, 0)
66 
67 /*
68  * LOCKDEP and DEBUG timer interfaces.
69  */
70 void timer_init_key(struct timer_list *timer,
71 		    void (*func)(struct timer_list *), unsigned int flags,
72 		    const char *name, struct lock_class_key *key);
73 
74 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
75 extern void timer_init_key_on_stack(struct timer_list *timer,
76 				    void (*func)(struct timer_list *),
77 				    unsigned int flags, const char *name,
78 				    struct lock_class_key *key);
79 #else
timer_init_key_on_stack(struct timer_list * timer,void (* func)(struct timer_list *),unsigned int flags,const char * name,struct lock_class_key * key)80 static inline void timer_init_key_on_stack(struct timer_list *timer,
81 					   void (*func)(struct timer_list *),
82 					   unsigned int flags,
83 					   const char *name,
84 					   struct lock_class_key *key)
85 {
86 	timer_init_key(timer, func, flags, name, key);
87 }
88 #endif
89 
90 #ifdef CONFIG_LOCKDEP
91 #define __timer_init(_timer, _fn, _flags)				\
92 	do {								\
93 		static struct lock_class_key __key;			\
94 		timer_init_key((_timer), (_fn), (_flags), #_timer, &__key);\
95 	} while (0)
96 
97 #define __timer_init_on_stack(_timer, _fn, _flags)			\
98 	do {								\
99 		static struct lock_class_key __key;			\
100 		timer_init_key_on_stack((_timer), (_fn), (_flags),	\
101 					#_timer, &__key);		 \
102 	} while (0)
103 #else
104 #define __timer_init(_timer, _fn, _flags)				\
105 	timer_init_key((_timer), (_fn), (_flags), NULL, NULL)
106 #define __timer_init_on_stack(_timer, _fn, _flags)			\
107 	timer_init_key_on_stack((_timer), (_fn), (_flags), NULL, NULL)
108 #endif
109 
110 /**
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
115  *
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 timer_destroy_on_stack().
119  */
120 #define timer_setup(timer, callback, flags)			\
121 	__timer_init((timer), (callback), (flags))
122 
123 #define timer_setup_on_stack(timer, callback, flags)		\
124 	__timer_init_on_stack((timer), (callback), (flags))
125 
126 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
127 extern void timer_destroy_on_stack(struct timer_list *timer);
128 #else
timer_destroy_on_stack(struct timer_list * timer)129 static inline void timer_destroy_on_stack(struct timer_list *timer) { }
130 #endif
131 
132 #define timer_container_of(var, callback_timer, timer_fieldname)	\
133 	container_of(callback_timer, typeof(*var), timer_fieldname)
134 
135 /**
136  * timer_pending - is a timer pending?
137  * @timer: the timer in question
138  *
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.
142  *
143  * Returns: 1 if the timer is pending, 0 if not.
144  */
timer_pending(const struct timer_list * timer)145 static inline int timer_pending(const struct timer_list * timer)
146 {
147 	return !hlist_unhashed_lockless(&timer->entry);
148 }
149 
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);
154 
155 /*
156  * The jiffies value which is added to now, when there is no timer
157  * in the timer wheel:
158  */
159 #define TIMER_NEXT_MAX_DELTA	((1UL << 30) - 1)
160 
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);
164 
165 extern int timer_delete_sync_try(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);
170 
171 extern void timers_init(void);
172 struct hrtimer;
173 extern enum hrtimer_restart it_real_fn(struct hrtimer *);
174 
175 unsigned long __round_jiffies_relative(unsigned long j, int cpu);
176 unsigned long round_jiffies(unsigned long j);
177 unsigned long round_jiffies_relative(unsigned long j);
178 
179 unsigned long __round_jiffies_up_relative(unsigned long j, int cpu);
180 unsigned long round_jiffies_up(unsigned long j);
181 unsigned long round_jiffies_up_relative(unsigned long j);
182 
183 #ifdef CONFIG_HOTPLUG_CPU
184 int timers_prepare_cpu(unsigned int cpu);
185 int timers_dead_cpu(unsigned int cpu);
186 #else
187 #define timers_prepare_cpu	NULL
188 #define timers_dead_cpu		NULL
189 #endif
190 
191 #endif
192