• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/android_kabi.h>
11 
12 struct timer_list {
13 	/*
14 	 * All fields that change during normal runtime grouped to the
15 	 * same cacheline
16 	 */
17 	struct hlist_node	entry;
18 	unsigned long		expires;
19 	void			(*function)(struct timer_list *);
20 	u32			flags;
21 
22 #ifdef CONFIG_LOCKDEP
23 	struct lockdep_map	lockdep_map;
24 #endif
25 
26 	ANDROID_KABI_RESERVE(1);
27 	ANDROID_KABI_RESERVE(2);
28 };
29 
30 #ifdef CONFIG_LOCKDEP
31 /*
32  * NB: because we have to copy the lockdep_map, setting the lockdep_map key
33  * (second argument) here is required, otherwise it could be initialised to
34  * the copy of the lockdep_map later! We use the pointer to and the string
35  * "<file>:<line>" as the key resp. the name of the lockdep_map.
36  */
37 #define __TIMER_LOCKDEP_MAP_INITIALIZER(_kn)				\
38 	.lockdep_map = STATIC_LOCKDEP_MAP_INIT(_kn, &_kn),
39 #else
40 #define __TIMER_LOCKDEP_MAP_INITIALIZER(_kn)
41 #endif
42 
43 /**
44  * @TIMER_DEFERRABLE: A deferrable timer will work normally when the
45  * system is busy, but will not cause a CPU to come out of idle just
46  * to service it; instead, the timer will be serviced when the CPU
47  * eventually wakes up with a subsequent non-deferrable timer.
48  *
49  * @TIMER_IRQSAFE: An irqsafe timer is executed with IRQ disabled and
50  * it's safe to wait for the completion of the running instance from
51  * IRQ handlers, for example, by calling del_timer_sync().
52  *
53  * Note: The irq disabled callback execution is a special case for
54  * workqueue locking issues. It's not meant for executing random crap
55  * with interrupts disabled. Abuse is monitored!
56  *
57  * @TIMER_PINNED: A pinned timer will not be affected by any timer
58  * placement heuristics (like, NOHZ) and will always expire on the CPU
59  * on which the timer was enqueued.
60  *
61  * Note: Because enqueuing of timers can migrate the timer from one
62  * CPU to another, pinned timers are not guaranteed to stay on the
63  * initialy selected CPU.  They move to the CPU on which the enqueue
64  * function is invoked via mod_timer() or add_timer().  If the timer
65  * should be placed on a particular CPU, then add_timer_on() has to be
66  * used.
67  */
68 #define TIMER_CPUMASK		0x0003FFFF
69 #define TIMER_MIGRATING		0x00040000
70 #define TIMER_BASEMASK		(TIMER_CPUMASK | TIMER_MIGRATING)
71 #define TIMER_DEFERRABLE	0x00080000
72 #define TIMER_PINNED		0x00100000
73 #define TIMER_IRQSAFE		0x00200000
74 #define TIMER_INIT_FLAGS	(TIMER_DEFERRABLE | TIMER_PINNED | TIMER_IRQSAFE)
75 #define TIMER_ARRAYSHIFT	22
76 #define TIMER_ARRAYMASK		0xFFC00000
77 
78 #define TIMER_TRACE_FLAGMASK	(TIMER_MIGRATING | TIMER_DEFERRABLE | TIMER_PINNED | TIMER_IRQSAFE)
79 
80 #define __TIMER_INITIALIZER(_function, _flags) {		\
81 		.entry = { .next = TIMER_ENTRY_STATIC },	\
82 		.function = (_function),			\
83 		.flags = (_flags),				\
84 		__TIMER_LOCKDEP_MAP_INITIALIZER(		\
85 			__FILE__ ":" __stringify(__LINE__))	\
86 	}
87 
88 #define DEFINE_TIMER(_name, _function)				\
89 	struct timer_list _name =				\
90 		__TIMER_INITIALIZER(_function, 0)
91 
92 /*
93  * LOCKDEP and DEBUG timer interfaces.
94  */
95 void init_timer_key(struct timer_list *timer,
96 		    void (*func)(struct timer_list *), unsigned int flags,
97 		    const char *name, struct lock_class_key *key);
98 
99 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
100 extern void init_timer_on_stack_key(struct timer_list *timer,
101 				    void (*func)(struct timer_list *),
102 				    unsigned int flags, const char *name,
103 				    struct lock_class_key *key);
104 #else
init_timer_on_stack_key(struct timer_list * timer,void (* func)(struct timer_list *),unsigned int flags,const char * name,struct lock_class_key * key)105 static inline void init_timer_on_stack_key(struct timer_list *timer,
106 					   void (*func)(struct timer_list *),
107 					   unsigned int flags,
108 					   const char *name,
109 					   struct lock_class_key *key)
110 {
111 	init_timer_key(timer, func, flags, name, key);
112 }
113 #endif
114 
115 #ifdef CONFIG_LOCKDEP
116 #define __init_timer(_timer, _fn, _flags)				\
117 	do {								\
118 		static struct lock_class_key __key;			\
119 		init_timer_key((_timer), (_fn), (_flags), #_timer, &__key);\
120 	} while (0)
121 
122 #define __init_timer_on_stack(_timer, _fn, _flags)			\
123 	do {								\
124 		static struct lock_class_key __key;			\
125 		init_timer_on_stack_key((_timer), (_fn), (_flags),	\
126 					#_timer, &__key);		 \
127 	} while (0)
128 #else
129 #define __init_timer(_timer, _fn, _flags)				\
130 	init_timer_key((_timer), (_fn), (_flags), NULL, NULL)
131 #define __init_timer_on_stack(_timer, _fn, _flags)			\
132 	init_timer_on_stack_key((_timer), (_fn), (_flags), NULL, NULL)
133 #endif
134 
135 /**
136  * timer_setup - prepare a timer for first use
137  * @timer: the timer in question
138  * @callback: the function to call when timer expires
139  * @flags: any TIMER_* flags
140  *
141  * Regular timer initialization should use either DEFINE_TIMER() above,
142  * or timer_setup(). For timers on the stack, timer_setup_on_stack() must
143  * be used and must be balanced with a call to destroy_timer_on_stack().
144  */
145 #define timer_setup(timer, callback, flags)			\
146 	__init_timer((timer), (callback), (flags))
147 
148 #define timer_setup_on_stack(timer, callback, flags)		\
149 	__init_timer_on_stack((timer), (callback), (flags))
150 
151 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
152 extern void destroy_timer_on_stack(struct timer_list *timer);
153 #else
destroy_timer_on_stack(struct timer_list * timer)154 static inline void destroy_timer_on_stack(struct timer_list *timer) { }
155 #endif
156 
157 #define from_timer(var, callback_timer, timer_fieldname) \
158 	container_of(callback_timer, typeof(*var), timer_fieldname)
159 
160 /**
161  * timer_pending - is a timer pending?
162  * @timer: the timer in question
163  *
164  * timer_pending will tell whether a given timer is currently pending,
165  * or not. Callers must ensure serialization wrt. other operations done
166  * to this timer, eg. interrupt contexts, or other CPUs on SMP.
167  *
168  * return value: 1 if the timer is pending, 0 if not.
169  */
timer_pending(const struct timer_list * timer)170 static inline int timer_pending(const struct timer_list * timer)
171 {
172 	return !hlist_unhashed_lockless(&timer->entry);
173 }
174 
175 extern void add_timer_on(struct timer_list *timer, int cpu);
176 extern int del_timer(struct timer_list * timer);
177 extern int mod_timer(struct timer_list *timer, unsigned long expires);
178 extern int mod_timer_pending(struct timer_list *timer, unsigned long expires);
179 extern int timer_reduce(struct timer_list *timer, unsigned long expires);
180 
181 /*
182  * The jiffies value which is added to now, when there is no timer
183  * in the timer wheel:
184  */
185 #define NEXT_TIMER_MAX_DELTA	((1UL << 30) - 1)
186 
187 extern void add_timer(struct timer_list *timer);
188 
189 extern int try_to_del_timer_sync(struct timer_list *timer);
190 
191 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT)
192   extern int del_timer_sync(struct timer_list *timer);
193 #else
194 # define del_timer_sync(t)		del_timer(t)
195 #endif
196 
197 #define del_singleshot_timer_sync(t) del_timer_sync(t)
198 
199 extern void init_timers(void);
200 struct hrtimer;
201 extern enum hrtimer_restart it_real_fn(struct hrtimer *);
202 
203 #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
204 struct ctl_table;
205 
206 extern unsigned int sysctl_timer_migration;
207 int timer_migration_handler(struct ctl_table *table, int write,
208 			    void *buffer, size_t *lenp, loff_t *ppos);
209 #endif
210 
211 unsigned long __round_jiffies(unsigned long j, int cpu);
212 unsigned long __round_jiffies_relative(unsigned long j, int cpu);
213 unsigned long round_jiffies(unsigned long j);
214 unsigned long round_jiffies_relative(unsigned long j);
215 
216 unsigned long __round_jiffies_up(unsigned long j, int cpu);
217 unsigned long __round_jiffies_up_relative(unsigned long j, int cpu);
218 unsigned long round_jiffies_up(unsigned long j);
219 unsigned long round_jiffies_up_relative(unsigned long j);
220 
221 #ifdef CONFIG_HOTPLUG_CPU
222 int timers_prepare_cpu(unsigned int cpu);
223 int timers_dead_cpu(unsigned int cpu);
224 #else
225 #define timers_prepare_cpu	NULL
226 #define timers_dead_cpu		NULL
227 #endif
228 
229 #endif
230