xref: /openbmc/linux/include/linux/workqueue.h (revision 1265057f)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * workqueue.h --- work queue handling for Linux.
31da177e4SLinus Torvalds  */
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds #ifndef _LINUX_WORKQUEUE_H
61da177e4SLinus Torvalds #define _LINUX_WORKQUEUE_H
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #include <linux/timer.h>
91da177e4SLinus Torvalds #include <linux/linkage.h>
101da177e4SLinus Torvalds #include <linux/bitops.h>
114e6045f1SJohannes Berg #include <linux/lockdep.h>
127a22ad75STejun Heo #include <linux/threads.h>
1360063497SArun Sharma #include <linux/atomic.h>
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds struct workqueue_struct;
161da177e4SLinus Torvalds 
1765f27f38SDavid Howells struct work_struct;
1865f27f38SDavid Howells typedef void (*work_func_t)(struct work_struct *work);
19d8e794dfSTejun Heo void delayed_work_timer_fn(unsigned long __data);
206bb49e59SDavid Howells 
21a08727baSLinus Torvalds /*
22a08727baSLinus Torvalds  * The first word is the work queue pointer and the flags rolled into
23a08727baSLinus Torvalds  * one
24a08727baSLinus Torvalds  */
25a08727baSLinus Torvalds #define work_data_bits(work) ((unsigned long *)(&(work)->data))
26a08727baSLinus Torvalds 
2722df02bbSTejun Heo enum {
2822df02bbSTejun Heo 	WORK_STRUCT_PENDING_BIT	= 0,	/* work item is pending execution */
298a2e8e5dSTejun Heo 	WORK_STRUCT_DELAYED_BIT	= 1,	/* work item is delayed */
308a2e8e5dSTejun Heo 	WORK_STRUCT_CWQ_BIT	= 2,	/* data points to cwq */
318a2e8e5dSTejun Heo 	WORK_STRUCT_LINKED_BIT	= 3,	/* next work is linked to this one */
3222df02bbSTejun Heo #ifdef CONFIG_DEBUG_OBJECTS_WORK
338a2e8e5dSTejun Heo 	WORK_STRUCT_STATIC_BIT	= 4,	/* static initializer (debugobjects) */
348a2e8e5dSTejun Heo 	WORK_STRUCT_COLOR_SHIFT	= 5,	/* color for workqueue flushing */
350f900049STejun Heo #else
368a2e8e5dSTejun Heo 	WORK_STRUCT_COLOR_SHIFT	= 4,	/* color for workqueue flushing */
3722df02bbSTejun Heo #endif
3822df02bbSTejun Heo 
3973f53c4aSTejun Heo 	WORK_STRUCT_COLOR_BITS	= 4,
4073f53c4aSTejun Heo 
4122df02bbSTejun Heo 	WORK_STRUCT_PENDING	= 1 << WORK_STRUCT_PENDING_BIT,
428a2e8e5dSTejun Heo 	WORK_STRUCT_DELAYED	= 1 << WORK_STRUCT_DELAYED_BIT,
43e120153dSTejun Heo 	WORK_STRUCT_CWQ		= 1 << WORK_STRUCT_CWQ_BIT,
44affee4b2STejun Heo 	WORK_STRUCT_LINKED	= 1 << WORK_STRUCT_LINKED_BIT,
4522df02bbSTejun Heo #ifdef CONFIG_DEBUG_OBJECTS_WORK
4622df02bbSTejun Heo 	WORK_STRUCT_STATIC	= 1 << WORK_STRUCT_STATIC_BIT,
4722df02bbSTejun Heo #else
4822df02bbSTejun Heo 	WORK_STRUCT_STATIC	= 0,
4922df02bbSTejun Heo #endif
5022df02bbSTejun Heo 
5173f53c4aSTejun Heo 	/*
5273f53c4aSTejun Heo 	 * The last color is no color used for works which don't
5373f53c4aSTejun Heo 	 * participate in workqueue flushing.
5473f53c4aSTejun Heo 	 */
5573f53c4aSTejun Heo 	WORK_NR_COLORS		= (1 << WORK_STRUCT_COLOR_BITS) - 1,
5673f53c4aSTejun Heo 	WORK_NO_COLOR		= WORK_NR_COLORS,
5773f53c4aSTejun Heo 
58bdbc5dd7STejun Heo 	/* special cpu IDs */
59f3421797STejun Heo 	WORK_CPU_UNBOUND	= NR_CPUS,
60f3421797STejun Heo 	WORK_CPU_NONE		= NR_CPUS + 1,
61bdbc5dd7STejun Heo 	WORK_CPU_LAST		= WORK_CPU_NONE,
62bdbc5dd7STejun Heo 
6373f53c4aSTejun Heo 	/*
64e120153dSTejun Heo 	 * Reserve 7 bits off of cwq pointer w/ debugobjects turned
658a2e8e5dSTejun Heo 	 * off.  This makes cwqs aligned to 256 bytes and allows 15
668a2e8e5dSTejun Heo 	 * workqueue flush colors.
6773f53c4aSTejun Heo 	 */
6873f53c4aSTejun Heo 	WORK_STRUCT_FLAG_BITS	= WORK_STRUCT_COLOR_SHIFT +
6973f53c4aSTejun Heo 				  WORK_STRUCT_COLOR_BITS,
7073f53c4aSTejun Heo 
71b5490077STejun Heo 	/* data contains off-queue information when !WORK_STRUCT_CWQ */
72b5490077STejun Heo 	WORK_OFFQ_FLAG_BASE	= WORK_STRUCT_FLAG_BITS,
73bbb68dfaSTejun Heo 
74bbb68dfaSTejun Heo 	WORK_OFFQ_CANCELING	= (1 << WORK_OFFQ_FLAG_BASE),
75bbb68dfaSTejun Heo 
76bbb68dfaSTejun Heo 	WORK_OFFQ_FLAG_BITS	= 1,
77b5490077STejun Heo 	WORK_OFFQ_CPU_SHIFT	= WORK_OFFQ_FLAG_BASE + WORK_OFFQ_FLAG_BITS,
78b5490077STejun Heo 
79b5490077STejun Heo 	/* convenience constants */
800f900049STejun Heo 	WORK_STRUCT_FLAG_MASK	= (1UL << WORK_STRUCT_FLAG_BITS) - 1,
8122df02bbSTejun Heo 	WORK_STRUCT_WQ_DATA_MASK = ~WORK_STRUCT_FLAG_MASK,
82b5490077STejun Heo 	WORK_STRUCT_NO_CPU	= (unsigned long)WORK_CPU_NONE << WORK_OFFQ_CPU_SHIFT,
83dcd989cbSTejun Heo 
84dcd989cbSTejun Heo 	/* bit mask for work_busy() return values */
85dcd989cbSTejun Heo 	WORK_BUSY_PENDING	= 1 << 0,
86dcd989cbSTejun Heo 	WORK_BUSY_RUNNING	= 1 << 1,
8722df02bbSTejun Heo };
8822df02bbSTejun Heo 
891da177e4SLinus Torvalds struct work_struct {
90a08727baSLinus Torvalds 	atomic_long_t data;
911da177e4SLinus Torvalds 	struct list_head entry;
926bb49e59SDavid Howells 	work_func_t func;
934e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
944e6045f1SJohannes Berg 	struct lockdep_map lockdep_map;
954e6045f1SJohannes Berg #endif
9652bad64dSDavid Howells };
9752bad64dSDavid Howells 
987a22ad75STejun Heo #define WORK_DATA_INIT()	ATOMIC_LONG_INIT(WORK_STRUCT_NO_CPU)
997a22ad75STejun Heo #define WORK_DATA_STATIC_INIT()	\
1007a22ad75STejun Heo 	ATOMIC_LONG_INIT(WORK_STRUCT_NO_CPU | WORK_STRUCT_STATIC)
101a08727baSLinus Torvalds 
10252bad64dSDavid Howells struct delayed_work {
10352bad64dSDavid Howells 	struct work_struct work;
1041da177e4SLinus Torvalds 	struct timer_list timer;
1051265057fSTejun Heo 	int cpu;
1061da177e4SLinus Torvalds };
1071da177e4SLinus Torvalds 
108bf6aede7SJean Delvare static inline struct delayed_work *to_delayed_work(struct work_struct *work)
109bf6aede7SJean Delvare {
110bf6aede7SJean Delvare 	return container_of(work, struct delayed_work, work);
111bf6aede7SJean Delvare }
112bf6aede7SJean Delvare 
1131fa44ecaSJames Bottomley struct execute_work {
1141fa44ecaSJames Bottomley 	struct work_struct work;
1151fa44ecaSJames Bottomley };
1161fa44ecaSJames Bottomley 
1174e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
1184e6045f1SJohannes Berg /*
1194e6045f1SJohannes Berg  * NB: because we have to copy the lockdep_map, setting _key
1204e6045f1SJohannes Berg  * here is required, otherwise it could get initialised to the
1214e6045f1SJohannes Berg  * copy of the lockdep_map!
1224e6045f1SJohannes Berg  */
1234e6045f1SJohannes Berg #define __WORK_INIT_LOCKDEP_MAP(n, k) \
1244e6045f1SJohannes Berg 	.lockdep_map = STATIC_LOCKDEP_MAP_INIT(n, k),
1254e6045f1SJohannes Berg #else
1264e6045f1SJohannes Berg #define __WORK_INIT_LOCKDEP_MAP(n, k)
1274e6045f1SJohannes Berg #endif
1284e6045f1SJohannes Berg 
12965f27f38SDavid Howells #define __WORK_INITIALIZER(n, f) {				\
130dc186ad7SThomas Gleixner 	.data = WORK_DATA_STATIC_INIT(),			\
13165f27f38SDavid Howells 	.entry	= { &(n).entry, &(n).entry },			\
13265f27f38SDavid Howells 	.func = (f),						\
1334e6045f1SJohannes Berg 	__WORK_INIT_LOCKDEP_MAP(#n, &(n))			\
13465f27f38SDavid Howells 	}
13565f27f38SDavid Howells 
13665f27f38SDavid Howells #define __DELAYED_WORK_INITIALIZER(n, f) {			\
13765f27f38SDavid Howells 	.work = __WORK_INITIALIZER((n).work, (f)),		\
138d8e794dfSTejun Heo 	.timer = TIMER_INITIALIZER(delayed_work_timer_fn,	\
139d8e794dfSTejun Heo 				0, (unsigned long)&(n)),	\
1401da177e4SLinus Torvalds 	}
1411da177e4SLinus Torvalds 
142dd6414b5SPhil Carmody #define __DEFERRED_WORK_INITIALIZER(n, f) {			\
143dd6414b5SPhil Carmody 	.work = __WORK_INITIALIZER((n).work, (f)),		\
144d8e794dfSTejun Heo 	.timer = TIMER_DEFERRED_INITIALIZER(delayed_work_timer_fn, \
145d8e794dfSTejun Heo 				0, (unsigned long)&(n)),	\
146dd6414b5SPhil Carmody 	}
147dd6414b5SPhil Carmody 
14865f27f38SDavid Howells #define DECLARE_WORK(n, f)					\
14965f27f38SDavid Howells 	struct work_struct n = __WORK_INITIALIZER(n, f)
15065f27f38SDavid Howells 
15165f27f38SDavid Howells #define DECLARE_DELAYED_WORK(n, f)				\
15265f27f38SDavid Howells 	struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f)
15365f27f38SDavid Howells 
154dd6414b5SPhil Carmody #define DECLARE_DEFERRED_WORK(n, f)				\
155dd6414b5SPhil Carmody 	struct delayed_work n = __DEFERRED_WORK_INITIALIZER(n, f)
156dd6414b5SPhil Carmody 
1571da177e4SLinus Torvalds /*
15865f27f38SDavid Howells  * initialize a work item's function pointer
1591da177e4SLinus Torvalds  */
16065f27f38SDavid Howells #define PREPARE_WORK(_work, _func)				\
1611da177e4SLinus Torvalds 	do {							\
16252bad64dSDavid Howells 		(_work)->func = (_func);			\
1631da177e4SLinus Torvalds 	} while (0)
1641da177e4SLinus Torvalds 
16565f27f38SDavid Howells #define PREPARE_DELAYED_WORK(_work, _func)			\
16665f27f38SDavid Howells 	PREPARE_WORK(&(_work)->work, (_func))
16752bad64dSDavid Howells 
168dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK
169dc186ad7SThomas Gleixner extern void __init_work(struct work_struct *work, int onstack);
170dc186ad7SThomas Gleixner extern void destroy_work_on_stack(struct work_struct *work);
1714690c4abSTejun Heo static inline unsigned int work_static(struct work_struct *work)
1724690c4abSTejun Heo {
17322df02bbSTejun Heo 	return *work_data_bits(work) & WORK_STRUCT_STATIC;
1744690c4abSTejun Heo }
175dc186ad7SThomas Gleixner #else
176dc186ad7SThomas Gleixner static inline void __init_work(struct work_struct *work, int onstack) { }
177dc186ad7SThomas Gleixner static inline void destroy_work_on_stack(struct work_struct *work) { }
1784690c4abSTejun Heo static inline unsigned int work_static(struct work_struct *work) { return 0; }
179dc186ad7SThomas Gleixner #endif
180dc186ad7SThomas Gleixner 
1811da177e4SLinus Torvalds /*
18252bad64dSDavid Howells  * initialize all of a work item in one go
183a08727baSLinus Torvalds  *
184b9049df5SDmitri Vorobiev  * NOTE! No point in using "atomic_long_set()": using a direct
185a08727baSLinus Torvalds  * assignment of the work data initializer allows the compiler
186a08727baSLinus Torvalds  * to generate better code.
1871da177e4SLinus Torvalds  */
1884e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
189dc186ad7SThomas Gleixner #define __INIT_WORK(_work, _func, _onstack)				\
1904e6045f1SJohannes Berg 	do {								\
1914e6045f1SJohannes Berg 		static struct lock_class_key __key;			\
1924e6045f1SJohannes Berg 									\
193dc186ad7SThomas Gleixner 		__init_work((_work), _onstack);				\
1944e6045f1SJohannes Berg 		(_work)->data = (atomic_long_t) WORK_DATA_INIT();	\
1954e6045f1SJohannes Berg 		lockdep_init_map(&(_work)->lockdep_map, #_work, &__key, 0);\
1964e6045f1SJohannes Berg 		INIT_LIST_HEAD(&(_work)->entry);			\
1974e6045f1SJohannes Berg 		PREPARE_WORK((_work), (_func));				\
1984e6045f1SJohannes Berg 	} while (0)
1994e6045f1SJohannes Berg #else
200dc186ad7SThomas Gleixner #define __INIT_WORK(_work, _func, _onstack)				\
2011da177e4SLinus Torvalds 	do {								\
202dc186ad7SThomas Gleixner 		__init_work((_work), _onstack);				\
20323b2e599SOleg Nesterov 		(_work)->data = (atomic_long_t) WORK_DATA_INIT();	\
20465f27f38SDavid Howells 		INIT_LIST_HEAD(&(_work)->entry);			\
20565f27f38SDavid Howells 		PREPARE_WORK((_work), (_func));				\
20665f27f38SDavid Howells 	} while (0)
2074e6045f1SJohannes Berg #endif
20865f27f38SDavid Howells 
209dc186ad7SThomas Gleixner #define INIT_WORK(_work, _func)					\
210dc186ad7SThomas Gleixner 	do {							\
211dc186ad7SThomas Gleixner 		__INIT_WORK((_work), (_func), 0);		\
212dc186ad7SThomas Gleixner 	} while (0)
213dc186ad7SThomas Gleixner 
214ca1cab37SAndrew Morton #define INIT_WORK_ONSTACK(_work, _func)				\
215dc186ad7SThomas Gleixner 	do {							\
216dc186ad7SThomas Gleixner 		__INIT_WORK((_work), (_func), 1);		\
217dc186ad7SThomas Gleixner 	} while (0)
218dc186ad7SThomas Gleixner 
21965f27f38SDavid Howells #define INIT_DELAYED_WORK(_work, _func)				\
22065f27f38SDavid Howells 	do {							\
22165f27f38SDavid Howells 		INIT_WORK(&(_work)->work, (_func));		\
22265f27f38SDavid Howells 		init_timer(&(_work)->timer);			\
223d8e794dfSTejun Heo 		(_work)->timer.function = delayed_work_timer_fn;\
224d8e794dfSTejun Heo 		(_work)->timer.data = (unsigned long)(_work);	\
22565f27f38SDavid Howells 	} while (0)
22665f27f38SDavid Howells 
227ca1cab37SAndrew Morton #define INIT_DELAYED_WORK_ONSTACK(_work, _func)			\
2286d612b0fSPeter Zijlstra 	do {							\
229ca1cab37SAndrew Morton 		INIT_WORK_ONSTACK(&(_work)->work, (_func));	\
2306d612b0fSPeter Zijlstra 		init_timer_on_stack(&(_work)->timer);		\
231d8e794dfSTejun Heo 		(_work)->timer.function = delayed_work_timer_fn;\
232d8e794dfSTejun Heo 		(_work)->timer.data = (unsigned long)(_work);	\
2336d612b0fSPeter Zijlstra 	} while (0)
2346d612b0fSPeter Zijlstra 
23528287033SVenki Pallipadi #define INIT_DELAYED_WORK_DEFERRABLE(_work, _func)		\
23628287033SVenki Pallipadi 	do {							\
23728287033SVenki Pallipadi 		INIT_WORK(&(_work)->work, (_func));		\
23828287033SVenki Pallipadi 		init_timer_deferrable(&(_work)->timer);		\
239d8e794dfSTejun Heo 		(_work)->timer.function = delayed_work_timer_fn;\
240d8e794dfSTejun Heo 		(_work)->timer.data = (unsigned long)(_work);	\
24128287033SVenki Pallipadi 	} while (0)
24228287033SVenki Pallipadi 
243365970a1SDavid Howells /**
244365970a1SDavid Howells  * work_pending - Find out whether a work item is currently pending
245365970a1SDavid Howells  * @work: The work item in question
246365970a1SDavid Howells  */
247365970a1SDavid Howells #define work_pending(work) \
24822df02bbSTejun Heo 	test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))
249365970a1SDavid Howells 
250365970a1SDavid Howells /**
251365970a1SDavid Howells  * delayed_work_pending - Find out whether a delayable work item is currently
252365970a1SDavid Howells  * pending
253365970a1SDavid Howells  * @work: The work item in question
254365970a1SDavid Howells  */
2550221872aSLinus Torvalds #define delayed_work_pending(w) \
2560221872aSLinus Torvalds 	work_pending(&(w)->work)
257365970a1SDavid Howells 
25865f27f38SDavid Howells /**
25923b2e599SOleg Nesterov  * work_clear_pending - for internal use only, mark a work item as not pending
26023b2e599SOleg Nesterov  * @work: The work item in question
26165f27f38SDavid Howells  */
26223b2e599SOleg Nesterov #define work_clear_pending(work) \
26322df02bbSTejun Heo 	clear_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))
26465f27f38SDavid Howells 
265c54fce6eSTejun Heo /*
266c54fce6eSTejun Heo  * Workqueue flags and constants.  For details, please refer to
267c54fce6eSTejun Heo  * Documentation/workqueue.txt.
268c54fce6eSTejun Heo  */
26997e37d7bSTejun Heo enum {
270bdbc5dd7STejun Heo 	WQ_NON_REENTRANT	= 1 << 0, /* guarantee non-reentrance */
271c7fc77f7STejun Heo 	WQ_UNBOUND		= 1 << 1, /* not bound to any cpu */
27258a69cb4STejun Heo 	WQ_FREEZABLE		= 1 << 2, /* freeze during suspend */
2736370a6adSTejun Heo 	WQ_MEM_RECLAIM		= 1 << 3, /* may be used for memory reclaim */
274649027d7STejun Heo 	WQ_HIGHPRI		= 1 << 4, /* high priority */
275fb0e7bebSTejun Heo 	WQ_CPU_INTENSIVE	= 1 << 5, /* cpu instensive workqueue */
276b71ab8c2STejun Heo 
2779c5a2ba7STejun Heo 	WQ_DRAINING		= 1 << 6, /* internal: workqueue is draining */
2786370a6adSTejun Heo 	WQ_RESCUER		= 1 << 7, /* internal: workqueue has rescuer */
279e41e704bSTejun Heo 
280b71ab8c2STejun Heo 	WQ_MAX_ACTIVE		= 512,	  /* I like 512, better ideas? */
281f3421797STejun Heo 	WQ_MAX_UNBOUND_PER_CPU	= 4,	  /* 4 * #cpus for unbound wq */
282b71ab8c2STejun Heo 	WQ_DFL_ACTIVE		= WQ_MAX_ACTIVE / 2,
28397e37d7bSTejun Heo };
28452bad64dSDavid Howells 
285f3421797STejun Heo /* unbound wq's aren't per-cpu, scale max_active according to #cpus */
286f3421797STejun Heo #define WQ_UNBOUND_MAX_ACTIVE	\
287f3421797STejun Heo 	max_t(int, WQ_MAX_ACTIVE, num_possible_cpus() * WQ_MAX_UNBOUND_PER_CPU)
288f3421797STejun Heo 
289d320c038STejun Heo /*
290d320c038STejun Heo  * System-wide workqueues which are always present.
291d320c038STejun Heo  *
292d320c038STejun Heo  * system_wq is the one used by schedule[_delayed]_work[_on]().
293d320c038STejun Heo  * Multi-CPU multi-threaded.  There are users which expect relatively
294d320c038STejun Heo  * short queue flush time.  Don't queue works which can run for too
295d320c038STejun Heo  * long.
296d320c038STejun Heo  *
297d320c038STejun Heo  * system_long_wq is similar to system_wq but may host long running
298d320c038STejun Heo  * works.  Queue flushing might take relatively long.
299d320c038STejun Heo  *
300d320c038STejun Heo  * system_nrt_wq is non-reentrant and guarantees that any given work
301d320c038STejun Heo  * item is never executed in parallel by multiple CPUs.  Queue
302d320c038STejun Heo  * flushing might take relatively long.
303f3421797STejun Heo  *
304f3421797STejun Heo  * system_unbound_wq is unbound workqueue.  Workers are not bound to
305f3421797STejun Heo  * any specific CPU, not concurrency managed, and all queued works are
306f3421797STejun Heo  * executed immediately as long as max_active limit is not reached and
307f3421797STejun Heo  * resources are available.
3084149efb2STejun Heo  *
30924d51addSTejun Heo  * system_freezable_wq is equivalent to system_wq except that it's
31024d51addSTejun Heo  * freezable.
31162d3c543SAlan Stern  *
31262d3c543SAlan Stern  * system_nrt_freezable_wq is equivalent to system_nrt_wq except that
31362d3c543SAlan Stern  * it's freezable.
314d320c038STejun Heo  */
315d320c038STejun Heo extern struct workqueue_struct *system_wq;
316d320c038STejun Heo extern struct workqueue_struct *system_long_wq;
317d320c038STejun Heo extern struct workqueue_struct *system_nrt_wq;
318f3421797STejun Heo extern struct workqueue_struct *system_unbound_wq;
31924d51addSTejun Heo extern struct workqueue_struct *system_freezable_wq;
32062d3c543SAlan Stern extern struct workqueue_struct *system_nrt_freezable_wq;
3211da177e4SLinus Torvalds 
3224e6045f1SJohannes Berg extern struct workqueue_struct *
323b196be89STejun Heo __alloc_workqueue_key(const char *fmt, unsigned int flags, int max_active,
324b196be89STejun Heo 	struct lock_class_key *key, const char *lock_name, ...) __printf(1, 6);
3254e6045f1SJohannes Berg 
326b196be89STejun Heo /**
327b196be89STejun Heo  * alloc_workqueue - allocate a workqueue
328b196be89STejun Heo  * @fmt: printf format for the name of the workqueue
329b196be89STejun Heo  * @flags: WQ_* flags
330b196be89STejun Heo  * @max_active: max in-flight work items, 0 for default
331b196be89STejun Heo  * @args: args for @fmt
332b196be89STejun Heo  *
333b196be89STejun Heo  * Allocate a workqueue with the specified parameters.  For detailed
334b196be89STejun Heo  * information on WQ_* flags, please refer to Documentation/workqueue.txt.
335b196be89STejun Heo  *
336b196be89STejun Heo  * The __lock_name macro dance is to guarantee that single lock_class_key
337b196be89STejun Heo  * doesn't end up with different namesm, which isn't allowed by lockdep.
338b196be89STejun Heo  *
339b196be89STejun Heo  * RETURNS:
340b196be89STejun Heo  * Pointer to the allocated workqueue on success, %NULL on failure.
341b196be89STejun Heo  */
3424e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
343b196be89STejun Heo #define alloc_workqueue(fmt, flags, max_active, args...)	\
3444e6045f1SJohannes Berg ({								\
3454e6045f1SJohannes Berg 	static struct lock_class_key __key;			\
346eb13ba87SJohannes Berg 	const char *__lock_name;				\
347eb13ba87SJohannes Berg 								\
348b196be89STejun Heo 	if (__builtin_constant_p(fmt))				\
349b196be89STejun Heo 		__lock_name = (fmt);				\
350eb13ba87SJohannes Berg 	else							\
351b196be89STejun Heo 		__lock_name = #fmt;				\
3524e6045f1SJohannes Berg 								\
353b196be89STejun Heo 	__alloc_workqueue_key((fmt), (flags), (max_active),	\
354b196be89STejun Heo 			      &__key, __lock_name, ##args);	\
3554e6045f1SJohannes Berg })
3564e6045f1SJohannes Berg #else
357b196be89STejun Heo #define alloc_workqueue(fmt, flags, max_active, args...)	\
358b196be89STejun Heo 	__alloc_workqueue_key((fmt), (flags), (max_active),	\
359b196be89STejun Heo 			      NULL, NULL, ##args)
3604e6045f1SJohannes Berg #endif
3614e6045f1SJohannes Berg 
36281dcaf65STejun Heo /**
36381dcaf65STejun Heo  * alloc_ordered_workqueue - allocate an ordered workqueue
364b196be89STejun Heo  * @fmt: printf format for the name of the workqueue
36558a69cb4STejun Heo  * @flags: WQ_* flags (only WQ_FREEZABLE and WQ_MEM_RECLAIM are meaningful)
366b196be89STejun Heo  * @args: args for @fmt
36781dcaf65STejun Heo  *
36881dcaf65STejun Heo  * Allocate an ordered workqueue.  An ordered workqueue executes at
36981dcaf65STejun Heo  * most one work item at any given time in the queued order.  They are
37081dcaf65STejun Heo  * implemented as unbound workqueues with @max_active of one.
37181dcaf65STejun Heo  *
37281dcaf65STejun Heo  * RETURNS:
37381dcaf65STejun Heo  * Pointer to the allocated workqueue on success, %NULL on failure.
37481dcaf65STejun Heo  */
375b196be89STejun Heo #define alloc_ordered_workqueue(fmt, flags, args...)		\
376b196be89STejun Heo 	alloc_workqueue(fmt, WQ_UNBOUND | (flags), 1, ##args)
37781dcaf65STejun Heo 
37897e37d7bSTejun Heo #define create_workqueue(name)					\
3796370a6adSTejun Heo 	alloc_workqueue((name), WQ_MEM_RECLAIM, 1)
38058a69cb4STejun Heo #define create_freezable_workqueue(name)			\
38158a69cb4STejun Heo 	alloc_workqueue((name), WQ_FREEZABLE | WQ_UNBOUND | WQ_MEM_RECLAIM, 1)
38297e37d7bSTejun Heo #define create_singlethread_workqueue(name)			\
3836370a6adSTejun Heo 	alloc_workqueue((name), WQ_UNBOUND | WQ_MEM_RECLAIM, 1)
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds extern void destroy_workqueue(struct workqueue_struct *wq);
3861da177e4SLinus Torvalds 
387d4283e93STejun Heo extern bool queue_work_on(int cpu, struct workqueue_struct *wq,
388c1a220e7SZhang Rui 			struct work_struct *work);
389d4283e93STejun Heo extern bool queue_work(struct workqueue_struct *wq, struct work_struct *work);
390d4283e93STejun Heo extern bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
39152bad64dSDavid Howells 			struct delayed_work *work, unsigned long delay);
392d4283e93STejun Heo extern bool queue_delayed_work(struct workqueue_struct *wq,
3930a13c00eSTejun Heo 			struct delayed_work *work, unsigned long delay);
3948376fe22STejun Heo extern bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
3958376fe22STejun Heo 			struct delayed_work *dwork, unsigned long delay);
3968376fe22STejun Heo extern bool mod_delayed_work(struct workqueue_struct *wq,
3978376fe22STejun Heo 			struct delayed_work *dwork, unsigned long delay);
39828e53bddSOleg Nesterov 
399b3c97528SHarvey Harrison extern void flush_workqueue(struct workqueue_struct *wq);
4009c5a2ba7STejun Heo extern void drain_workqueue(struct workqueue_struct *wq);
40128e53bddSOleg Nesterov extern void flush_scheduled_work(void);
4021da177e4SLinus Torvalds 
403d4283e93STejun Heo extern bool schedule_work_on(int cpu, struct work_struct *work);
404d4283e93STejun Heo extern bool schedule_work(struct work_struct *work);
405d4283e93STejun Heo extern bool schedule_delayed_work_on(int cpu, struct delayed_work *work,
40628e53bddSOleg Nesterov 				     unsigned long delay);
407d4283e93STejun Heo extern bool schedule_delayed_work(struct delayed_work *work,
4080a13c00eSTejun Heo 				  unsigned long delay);
40965f27f38SDavid Howells extern int schedule_on_each_cpu(work_func_t func);
4101da177e4SLinus Torvalds extern int keventd_up(void);
4111da177e4SLinus Torvalds 
41265f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *);
4131da177e4SLinus Torvalds 
414401a8d04STejun Heo extern bool flush_work(struct work_struct *work);
41509383498STejun Heo extern bool flush_work_sync(struct work_struct *work);
416401a8d04STejun Heo extern bool cancel_work_sync(struct work_struct *work);
417401a8d04STejun Heo 
418401a8d04STejun Heo extern bool flush_delayed_work(struct delayed_work *dwork);
41909383498STejun Heo extern bool flush_delayed_work_sync(struct delayed_work *work);
420401a8d04STejun Heo extern bool cancel_delayed_work_sync(struct delayed_work *dwork);
42128e53bddSOleg Nesterov 
422dcd989cbSTejun Heo extern void workqueue_set_max_active(struct workqueue_struct *wq,
423dcd989cbSTejun Heo 				     int max_active);
424dcd989cbSTejun Heo extern bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq);
425dcd989cbSTejun Heo extern unsigned int work_cpu(struct work_struct *work);
426dcd989cbSTejun Heo extern unsigned int work_busy(struct work_struct *work);
427dcd989cbSTejun Heo 
4281da177e4SLinus Torvalds /*
4291da177e4SLinus Torvalds  * Kill off a pending schedule_delayed_work().  Note that the work callback
430071b6386SOleg Nesterov  * function may still be running on return from cancel_delayed_work(), unless
431071b6386SOleg Nesterov  * it returns 1 and the work doesn't re-arm itself. Run flush_workqueue() or
43228e53bddSOleg Nesterov  * cancel_work_sync() to wait on it.
4331da177e4SLinus Torvalds  */
434401a8d04STejun Heo static inline bool cancel_delayed_work(struct delayed_work *work)
4351da177e4SLinus Torvalds {
436401a8d04STejun Heo 	bool ret;
4371da177e4SLinus Torvalds 
438223a10a9SOleg Nesterov 	ret = del_timer_sync(&work->timer);
4391da177e4SLinus Torvalds 	if (ret)
44023b2e599SOleg Nesterov 		work_clear_pending(&work->work);
4411da177e4SLinus Torvalds 	return ret;
4421da177e4SLinus Torvalds }
4431da177e4SLinus Torvalds 
4444e49627bSOleg Nesterov /*
4454e49627bSOleg Nesterov  * Like above, but uses del_timer() instead of del_timer_sync(). This means,
4464e49627bSOleg Nesterov  * if it returns 0 the timer function may be running and the queueing is in
4474e49627bSOleg Nesterov  * progress.
4484e49627bSOleg Nesterov  */
449401a8d04STejun Heo static inline bool __cancel_delayed_work(struct delayed_work *work)
4504e49627bSOleg Nesterov {
451401a8d04STejun Heo 	bool ret;
4524e49627bSOleg Nesterov 
4534e49627bSOleg Nesterov 	ret = del_timer(&work->timer);
4544e49627bSOleg Nesterov 	if (ret)
4554e49627bSOleg Nesterov 		work_clear_pending(&work->work);
4564e49627bSOleg Nesterov 	return ret;
4574e49627bSOleg Nesterov }
4584e49627bSOleg Nesterov 
4592d3854a3SRusty Russell #ifndef CONFIG_SMP
4602d3854a3SRusty Russell static inline long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
4612d3854a3SRusty Russell {
4622d3854a3SRusty Russell 	return fn(arg);
4632d3854a3SRusty Russell }
4642d3854a3SRusty Russell #else
4652d3854a3SRusty Russell long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg);
4662d3854a3SRusty Russell #endif /* CONFIG_SMP */
467a25909a4SPaul E. McKenney 
468a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER
469a0a1a5fdSTejun Heo extern void freeze_workqueues_begin(void);
470a0a1a5fdSTejun Heo extern bool freeze_workqueues_busy(void);
471a0a1a5fdSTejun Heo extern void thaw_workqueues(void);
472a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */
473a0a1a5fdSTejun Heo 
4741da177e4SLinus Torvalds #endif
475