blob: d10bc7e73b41eff9a20900ae63f0c1538b71078b [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef _LINUX_TIMER_H
3#define _LINUX_TIMER_H
4
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/list.h>
Ingo Molnar82f67cd2007-02-16 01:28:13 -08006#include <linux/ktime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/stddef.h>
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -07008#include <linux/debugobjects.h>
Johannes Berg6f2b9b92009-01-29 16:03:20 +01009#include <linux/stringify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
Linus Torvalds1da177e2005-04-16 15:20:36 -070011struct timer_list {
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -080012 /*
13 * All fields that change during normal runtime grouped to the
14 * same cacheline
15 */
Thomas Gleixner0eeda712015-05-26 22:50:29 +000016 struct hlist_node entry;
17 unsigned long expires;
Kees Cook354b46b2017-10-22 19:15:40 -070018 void (*function)(struct timer_list *);
Thomas Gleixner0eeda712015-05-26 22:50:29 +000019 u32 flags;
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -080020
Johannes Berg6f2b9b92009-01-29 16:03:20 +010021#ifdef CONFIG_LOCKDEP
Thomas Gleixner0eeda712015-05-26 22:50:29 +000022 struct lockdep_map lockdep_map;
Johannes Berg6f2b9b92009-01-29 16:03:20 +010023#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070024};
25
Johannes Berg6f2b9b92009-01-29 16:03:20 +010026#ifdef CONFIG_LOCKDEP
27/*
28 * NB: because we have to copy the lockdep_map, setting the lockdep_map key
29 * (second argument) here is required, otherwise it could be initialised to
30 * the copy of the lockdep_map later! We use the pointer to and the string
31 * "<file>:<line>" as the key resp. the name of the lockdep_map.
32 */
33#define __TIMER_LOCKDEP_MAP_INITIALIZER(_kn) \
34 .lockdep_map = STATIC_LOCKDEP_MAP_INIT(_kn, &_kn),
35#else
36#define __TIMER_LOCKDEP_MAP_INITIALIZER(_kn)
37#endif
38
Peter Xu28ef2db2019-06-28 18:59:42 +080039/**
40 * @TIMER_DEFERRABLE: A deferrable timer will work normally when the
41 * system is busy, but will not cause a CPU to come out of idle just
42 * to service it; instead, the timer will be serviced when the CPU
43 * eventually wakes up with a subsequent non-deferrable timer.
Tejun Heoc5f66e92012-08-08 11:10:28 -070044 *
Peter Xu28ef2db2019-06-28 18:59:42 +080045 * @TIMER_IRQSAFE: An irqsafe timer is executed with IRQ disabled and
46 * it's safe to wait for the completion of the running instance from
47 * IRQ handlers, for example, by calling del_timer_sync().
Tejun Heoc5f66e92012-08-08 11:10:28 -070048 *
49 * Note: The irq disabled callback execution is a special case for
50 * workqueue locking issues. It's not meant for executing random crap
51 * with interrupts disabled. Abuse is monitored!
Peter Xu28ef2db2019-06-28 18:59:42 +080052 *
53 * @TIMER_PINNED: A pinned timer will not be affected by any timer
54 * placement heuristics (like, NOHZ) and will always expire on the CPU
55 * on which the timer was enqueued.
56 *
57 * Note: Because enqueuing of timers can migrate the timer from one
58 * CPU to another, pinned timers are not guaranteed to stay on the
59 * initialy selected CPU. They move to the CPU on which the enqueue
60 * function is invoked via mod_timer() or add_timer(). If the timer
61 * should be placed on a particular CPU, then add_timer_on() has to be
62 * used.
Phil Carmodydd6414b2010-10-20 15:57:33 -070063 */
Thomas Gleixnerb0d6e2d2016-07-04 09:50:29 +000064#define TIMER_CPUMASK 0x0003FFFF
65#define TIMER_MIGRATING 0x00040000
Thomas Gleixner0eeda712015-05-26 22:50:29 +000066#define TIMER_BASEMASK (TIMER_CPUMASK | TIMER_MIGRATING)
Thomas Gleixnerb0d6e2d2016-07-04 09:50:29 +000067#define TIMER_DEFERRABLE 0x00080000
68#define TIMER_PINNED 0x00100000
69#define TIMER_IRQSAFE 0x00200000
Qianli Zhaob952caf2020-08-13 23:03:14 +080070#define TIMER_INIT_FLAGS (TIMER_DEFERRABLE | TIMER_PINNED | TIMER_IRQSAFE)
Thomas Gleixner500462a2016-07-04 09:50:30 +000071#define TIMER_ARRAYSHIFT 22
72#define TIMER_ARRAYMASK 0xFFC00000
Phil Carmodydd6414b2010-10-20 15:57:33 -070073
Thomas Gleixner8a58a342017-02-10 16:41:15 +010074#define TIMER_TRACE_FLAGMASK (TIMER_MIGRATING | TIMER_DEFERRABLE | TIMER_PINNED | TIMER_IRQSAFE)
75
Kees Cook1fe66ba2017-10-22 18:22:50 -070076#define __TIMER_INITIALIZER(_function, _flags) { \
Thomas Gleixner1dabbce2015-05-26 22:50:28 +000077 .entry = { .next = TIMER_ENTRY_STATIC }, \
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 .function = (_function), \
Thomas Gleixner0eeda712015-05-26 22:50:29 +000079 .flags = (_flags), \
Johannes Berg6f2b9b92009-01-29 16:03:20 +010080 __TIMER_LOCKDEP_MAP_INITIALIZER( \
81 __FILE__ ":" __stringify(__LINE__)) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
83
Kees Cook1d27e3e2017-10-04 16:27:04 -070084#define DEFINE_TIMER(_name, _function) \
Ingo Molnar8d06afa2005-09-09 13:10:40 -070085 struct timer_list _name = \
Kees Cook841b86f2017-10-23 09:40:42 +020086 __TIMER_INITIALIZER(_function, 0)
Ingo Molnar8d06afa2005-09-09 13:10:40 -070087
Kees Cook919b2502017-10-22 18:48:43 -070088/*
89 * LOCKDEP and DEBUG timer interfaces.
90 */
Kees Cook188665b2017-10-22 18:14:46 -070091void init_timer_key(struct timer_list *timer,
92 void (*func)(struct timer_list *), unsigned int flags,
Tejun Heofc683992012-08-08 11:10:27 -070093 const char *name, struct lock_class_key *key);
Johannes Berg6f2b9b92009-01-29 16:03:20 +010094
Tejun Heo5a9af382012-08-08 11:10:26 -070095#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
96extern void init_timer_on_stack_key(struct timer_list *timer,
Kees Cook188665b2017-10-22 18:14:46 -070097 void (*func)(struct timer_list *),
Tejun Heofc683992012-08-08 11:10:27 -070098 unsigned int flags, const char *name,
Tejun Heo5a9af382012-08-08 11:10:26 -070099 struct lock_class_key *key);
Tejun Heo5a9af382012-08-08 11:10:26 -0700100#else
Tejun Heo5a9af382012-08-08 11:10:26 -0700101static inline void init_timer_on_stack_key(struct timer_list *timer,
Kees Cook188665b2017-10-22 18:14:46 -0700102 void (*func)(struct timer_list *),
103 unsigned int flags,
104 const char *name,
Tejun Heo5a9af382012-08-08 11:10:26 -0700105 struct lock_class_key *key)
106{
Kees Cook188665b2017-10-22 18:14:46 -0700107 init_timer_key(timer, func, flags, name, key);
Tejun Heo5a9af382012-08-08 11:10:26 -0700108}
109#endif
110
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100111#ifdef CONFIG_LOCKDEP
Kees Cook188665b2017-10-22 18:14:46 -0700112#define __init_timer(_timer, _fn, _flags) \
Tejun Heofc683992012-08-08 11:10:27 -0700113 do { \
114 static struct lock_class_key __key; \
Kees Cook188665b2017-10-22 18:14:46 -0700115 init_timer_key((_timer), (_fn), (_flags), #_timer, &__key);\
Tejun Heofc683992012-08-08 11:10:27 -0700116 } while (0)
117
Kees Cook188665b2017-10-22 18:14:46 -0700118#define __init_timer_on_stack(_timer, _fn, _flags) \
Tejun Heofc683992012-08-08 11:10:27 -0700119 do { \
120 static struct lock_class_key __key; \
Kees Cook188665b2017-10-22 18:14:46 -0700121 init_timer_on_stack_key((_timer), (_fn), (_flags), \
122 #_timer, &__key); \
Tejun Heofc683992012-08-08 11:10:27 -0700123 } while (0)
124#else
Kees Cook188665b2017-10-22 18:14:46 -0700125#define __init_timer(_timer, _fn, _flags) \
126 init_timer_key((_timer), (_fn), (_flags), NULL, NULL)
127#define __init_timer_on_stack(_timer, _fn, _flags) \
128 init_timer_on_stack_key((_timer), (_fn), (_flags), NULL, NULL)
Tejun Heofc683992012-08-08 11:10:27 -0700129#endif
130
Kees Cook919b2502017-10-22 18:48:43 -0700131/**
132 * timer_setup - prepare a timer for first use
133 * @timer: the timer in question
134 * @callback: the function to call when timer expires
135 * @flags: any TIMER_* flags
136 *
137 * Regular timer initialization should use either DEFINE_TIMER() above,
138 * or timer_setup(). For timers on the stack, timer_setup_on_stack() must
139 * be used and must be balanced with a call to destroy_timer_on_stack().
Kees Cook52f737c2017-10-19 13:28:38 -0700140 */
Kees Cook919b2502017-10-22 18:48:43 -0700141#define timer_setup(timer, callback, flags) \
142 __init_timer((timer), (callback), (flags))
143
144#define timer_setup_on_stack(timer, callback, flags) \
145 __init_timer_on_stack((timer), (callback), (flags))
146
147#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
148extern void destroy_timer_on_stack(struct timer_list *timer);
149#else
150static inline void destroy_timer_on_stack(struct timer_list *timer) { }
Kees Cook52f737c2017-10-19 13:28:38 -0700151#endif
Kees Cook58e11772017-10-04 16:26:55 -0700152
Kees Cook686fef92017-09-28 06:38:17 -0700153#define from_timer(var, callback_timer, timer_fieldname) \
154 container_of(callback_timer, typeof(*var), timer_fieldname)
155
Robert P. J. Day45f8bde2007-01-26 00:57:09 -0800156/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 * timer_pending - is a timer pending?
158 * @timer: the timer in question
159 *
160 * timer_pending will tell whether a given timer is currently pending,
161 * or not. Callers must ensure serialization wrt. other operations done
162 * to this timer, eg. interrupt contexts, or other CPUs on SMP.
163 *
164 * return value: 1 if the timer is pending, 0 if not.
165 */
166static inline int timer_pending(const struct timer_list * timer)
167{
Eric Dumazet90c01892019-11-07 11:37:38 -0800168 return !hlist_unhashed_lockless(&timer->entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
171extern void add_timer_on(struct timer_list *timer, int cpu);
172extern int del_timer(struct timer_list * timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173extern int mod_timer(struct timer_list *timer, unsigned long expires);
Ingo Molnar74019222009-02-18 12:23:29 +0100174extern int mod_timer_pending(struct timer_list *timer, unsigned long expires);
David Howellsb24591e2017-11-09 12:35:07 +0000175extern int timer_reduce(struct timer_list *timer, unsigned long expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Thomas Gleixnerfd064b92007-02-16 01:27:47 -0800177/*
Thomas Gleixnereaad0842007-05-29 23:47:39 +0200178 * The jiffies value which is added to now, when there is no timer
179 * in the timer wheel:
180 */
181#define NEXT_TIMER_MAX_DELTA ((1UL << 30) - 1)
182
Ingo Molnar74019222009-02-18 12:23:29 +0100183extern void add_timer(struct timer_list *timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Yong Zhang6f1bc452010-10-20 15:57:31 -0700185extern int try_to_del_timer_sync(struct timer_list *timer);
186
Anna-Maria Gleixner030dcdd2019-07-26 20:31:00 +0200187#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 extern int del_timer_sync(struct timer_list *timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189#else
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700190# define del_timer_sync(t) del_timer(t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191#endif
192
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700193#define del_singleshot_timer_sync(t) del_timer_sync(t)
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195extern void init_timers(void);
196extern void run_local_timers(void);
Roman Zippel05cfb612006-03-26 01:38:12 -0800197struct hrtimer;
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800198extern enum hrtimer_restart it_real_fn(struct hrtimer *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000200#if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
Ingo Molnarb2d5bfe2017-02-06 09:56:40 +0100201struct ctl_table;
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000202
203extern unsigned int sysctl_timer_migration;
204int timer_migration_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +0200205 void *buffer, size_t *lenp, loff_t *ppos);
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000206#endif
207
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800208unsigned long __round_jiffies(unsigned long j, int cpu);
209unsigned long __round_jiffies_relative(unsigned long j, int cpu);
210unsigned long round_jiffies(unsigned long j);
211unsigned long round_jiffies_relative(unsigned long j);
212
Alan Stern9c133c42008-11-06 08:42:48 +0100213unsigned long __round_jiffies_up(unsigned long j, int cpu);
214unsigned long __round_jiffies_up_relative(unsigned long j, int cpu);
215unsigned long round_jiffies_up(unsigned long j);
216unsigned long round_jiffies_up_relative(unsigned long j);
217
Richard Cochran24f73b92016-07-13 17:16:59 +0000218#ifdef CONFIG_HOTPLUG_CPU
Thomas Gleixner26456f82017-12-27 21:37:25 +0100219int timers_prepare_cpu(unsigned int cpu);
Richard Cochran24f73b92016-07-13 17:16:59 +0000220int timers_dead_cpu(unsigned int cpu);
221#else
Thomas Gleixner26456f82017-12-27 21:37:25 +0100222#define timers_prepare_cpu NULL
223#define timers_dead_cpu NULL
Richard Cochran24f73b92016-07-13 17:16:59 +0000224#endif
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226#endif