xref: /openbmc/linux/include/linux/workqueue.h (revision dbb92f88)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * workqueue.h --- work queue handling for Linux.
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds #ifndef _LINUX_WORKQUEUE_H
71da177e4SLinus Torvalds #define _LINUX_WORKQUEUE_H
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/timer.h>
101da177e4SLinus Torvalds #include <linux/linkage.h>
111da177e4SLinus Torvalds #include <linux/bitops.h>
124e6045f1SJohannes Berg #include <linux/lockdep.h>
137a22ad75STejun Heo #include <linux/threads.h>
1460063497SArun Sharma #include <linux/atomic.h>
157a4e344cSTejun Heo #include <linux/cpumask.h>
1605f0fe6bSTejun Heo #include <linux/rcupdate.h>
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds struct workqueue_struct;
191da177e4SLinus Torvalds 
2065f27f38SDavid Howells struct work_struct;
2165f27f38SDavid Howells typedef void (*work_func_t)(struct work_struct *work);
228c20feb6SKees Cook void delayed_work_timer_fn(struct timer_list *t);
236bb49e59SDavid Howells 
24a08727baSLinus Torvalds /*
25a08727baSLinus Torvalds  * The first word is the work queue pointer and the flags rolled into
26a08727baSLinus Torvalds  * one
27a08727baSLinus Torvalds  */
28a08727baSLinus Torvalds #define work_data_bits(work) ((unsigned long *)(&(work)->data))
29a08727baSLinus Torvalds 
3022df02bbSTejun Heo enum {
3122df02bbSTejun Heo 	WORK_STRUCT_PENDING_BIT	= 0,	/* work item is pending execution */
328a2e8e5dSTejun Heo 	WORK_STRUCT_DELAYED_BIT	= 1,	/* work item is delayed */
33112202d9STejun Heo 	WORK_STRUCT_PWQ_BIT	= 2,	/* data points to pwq */
348a2e8e5dSTejun Heo 	WORK_STRUCT_LINKED_BIT	= 3,	/* next work is linked to this one */
3522df02bbSTejun Heo #ifdef CONFIG_DEBUG_OBJECTS_WORK
368a2e8e5dSTejun Heo 	WORK_STRUCT_STATIC_BIT	= 4,	/* static initializer (debugobjects) */
378a2e8e5dSTejun Heo 	WORK_STRUCT_COLOR_SHIFT	= 5,	/* color for workqueue flushing */
380f900049STejun Heo #else
398a2e8e5dSTejun Heo 	WORK_STRUCT_COLOR_SHIFT	= 4,	/* color for workqueue flushing */
4022df02bbSTejun Heo #endif
4122df02bbSTejun Heo 
4273f53c4aSTejun Heo 	WORK_STRUCT_COLOR_BITS	= 4,
4373f53c4aSTejun Heo 
4422df02bbSTejun Heo 	WORK_STRUCT_PENDING	= 1 << WORK_STRUCT_PENDING_BIT,
458a2e8e5dSTejun Heo 	WORK_STRUCT_DELAYED	= 1 << WORK_STRUCT_DELAYED_BIT,
46112202d9STejun Heo 	WORK_STRUCT_PWQ		= 1 << WORK_STRUCT_PWQ_BIT,
47affee4b2STejun Heo 	WORK_STRUCT_LINKED	= 1 << WORK_STRUCT_LINKED_BIT,
4822df02bbSTejun Heo #ifdef CONFIG_DEBUG_OBJECTS_WORK
4922df02bbSTejun Heo 	WORK_STRUCT_STATIC	= 1 << WORK_STRUCT_STATIC_BIT,
5022df02bbSTejun Heo #else
5122df02bbSTejun Heo 	WORK_STRUCT_STATIC	= 0,
5222df02bbSTejun Heo #endif
5322df02bbSTejun Heo 
5473f53c4aSTejun Heo 	/*
5573f53c4aSTejun Heo 	 * The last color is no color used for works which don't
5673f53c4aSTejun Heo 	 * participate in workqueue flushing.
5773f53c4aSTejun Heo 	 */
5873f53c4aSTejun Heo 	WORK_NR_COLORS		= (1 << WORK_STRUCT_COLOR_BITS) - 1,
5973f53c4aSTejun Heo 	WORK_NO_COLOR		= WORK_NR_COLORS,
6073f53c4aSTejun Heo 
6179bc251fSLai Jiangshan 	/* not bound to any CPU, prefer the local CPU */
62f3421797STejun Heo 	WORK_CPU_UNBOUND	= NR_CPUS,
63bdbc5dd7STejun Heo 
6473f53c4aSTejun Heo 	/*
65112202d9STejun Heo 	 * Reserve 7 bits off of pwq pointer w/ debugobjects turned off.
66112202d9STejun Heo 	 * This makes pwqs aligned to 256 bytes and allows 15 workqueue
67112202d9STejun Heo 	 * flush colors.
6873f53c4aSTejun Heo 	 */
6973f53c4aSTejun Heo 	WORK_STRUCT_FLAG_BITS	= WORK_STRUCT_COLOR_SHIFT +
7073f53c4aSTejun Heo 				  WORK_STRUCT_COLOR_BITS,
7173f53c4aSTejun Heo 
72112202d9STejun Heo 	/* data contains off-queue information when !WORK_STRUCT_PWQ */
7345d9550aSLai Jiangshan 	WORK_OFFQ_FLAG_BASE	= WORK_STRUCT_COLOR_SHIFT,
74bbb68dfaSTejun Heo 
758603e1b3STejun Heo 	__WORK_OFFQ_CANCELING	= WORK_OFFQ_FLAG_BASE,
768603e1b3STejun Heo 	WORK_OFFQ_CANCELING	= (1 << __WORK_OFFQ_CANCELING),
77bbb68dfaSTejun Heo 
78715b06b8STejun Heo 	/*
79715b06b8STejun Heo 	 * When a work item is off queue, its high bits point to the last
807c3eed5cSTejun Heo 	 * pool it was on.  Cap at 31 bits and use the highest number to
817c3eed5cSTejun Heo 	 * indicate that no pool is associated.
82715b06b8STejun Heo 	 */
83bbb68dfaSTejun Heo 	WORK_OFFQ_FLAG_BITS	= 1,
847c3eed5cSTejun Heo 	WORK_OFFQ_POOL_SHIFT	= WORK_OFFQ_FLAG_BASE + WORK_OFFQ_FLAG_BITS,
857c3eed5cSTejun Heo 	WORK_OFFQ_LEFT		= BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT,
867c3eed5cSTejun Heo 	WORK_OFFQ_POOL_BITS	= WORK_OFFQ_LEFT <= 31 ? WORK_OFFQ_LEFT : 31,
877c3eed5cSTejun Heo 	WORK_OFFQ_POOL_NONE	= (1LU << WORK_OFFQ_POOL_BITS) - 1,
88b5490077STejun Heo 
89b5490077STejun Heo 	/* convenience constants */
900f900049STejun Heo 	WORK_STRUCT_FLAG_MASK	= (1UL << WORK_STRUCT_FLAG_BITS) - 1,
9122df02bbSTejun Heo 	WORK_STRUCT_WQ_DATA_MASK = ~WORK_STRUCT_FLAG_MASK,
927c3eed5cSTejun Heo 	WORK_STRUCT_NO_POOL	= (unsigned long)WORK_OFFQ_POOL_NONE << WORK_OFFQ_POOL_SHIFT,
93dcd989cbSTejun Heo 
94dcd989cbSTejun Heo 	/* bit mask for work_busy() return values */
95dcd989cbSTejun Heo 	WORK_BUSY_PENDING	= 1 << 0,
96dcd989cbSTejun Heo 	WORK_BUSY_RUNNING	= 1 << 1,
973d1cb205STejun Heo 
983d1cb205STejun Heo 	/* maximum string length for set_worker_desc() */
993d1cb205STejun Heo 	WORKER_DESC_LEN		= 24,
10022df02bbSTejun Heo };
10122df02bbSTejun Heo 
1021da177e4SLinus Torvalds struct work_struct {
103a08727baSLinus Torvalds 	atomic_long_t data;
1041da177e4SLinus Torvalds 	struct list_head entry;
1056bb49e59SDavid Howells 	work_func_t func;
1064e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
1074e6045f1SJohannes Berg 	struct lockdep_map lockdep_map;
1084e6045f1SJohannes Berg #endif
10952bad64dSDavid Howells };
11052bad64dSDavid Howells 
111a45463cbSArnd Bergmann #define WORK_DATA_INIT()	ATOMIC_LONG_INIT((unsigned long)WORK_STRUCT_NO_POOL)
1127a22ad75STejun Heo #define WORK_DATA_STATIC_INIT()	\
113a45463cbSArnd Bergmann 	ATOMIC_LONG_INIT((unsigned long)(WORK_STRUCT_NO_POOL | WORK_STRUCT_STATIC))
114a08727baSLinus Torvalds 
11552bad64dSDavid Howells struct delayed_work {
11652bad64dSDavid Howells 	struct work_struct work;
1171da177e4SLinus Torvalds 	struct timer_list timer;
11860c057bcSLai Jiangshan 
11960c057bcSLai Jiangshan 	/* target workqueue and CPU ->timer uses to queue ->work */
12060c057bcSLai Jiangshan 	struct workqueue_struct *wq;
1211265057fSTejun Heo 	int cpu;
1221da177e4SLinus Torvalds };
1231da177e4SLinus Torvalds 
12405f0fe6bSTejun Heo struct rcu_work {
12505f0fe6bSTejun Heo 	struct work_struct work;
12605f0fe6bSTejun Heo 	struct rcu_head rcu;
12705f0fe6bSTejun Heo 
12805f0fe6bSTejun Heo 	/* target workqueue ->rcu uses to queue ->work */
12905f0fe6bSTejun Heo 	struct workqueue_struct *wq;
13005f0fe6bSTejun Heo };
13105f0fe6bSTejun Heo 
13242412c3aSSilvio Fricke /**
13342412c3aSSilvio Fricke  * struct workqueue_attrs - A struct for workqueue attributes.
134d55262c4STejun Heo  *
13542412c3aSSilvio Fricke  * This can be used to change attributes of an unbound workqueue.
1367a4e344cSTejun Heo  */
1377a4e344cSTejun Heo struct workqueue_attrs {
13842412c3aSSilvio Fricke 	/**
13942412c3aSSilvio Fricke 	 * @nice: nice level
14042412c3aSSilvio Fricke 	 */
14142412c3aSSilvio Fricke 	int nice;
14242412c3aSSilvio Fricke 
14342412c3aSSilvio Fricke 	/**
14442412c3aSSilvio Fricke 	 * @cpumask: allowed CPUs
14542412c3aSSilvio Fricke 	 */
14642412c3aSSilvio Fricke 	cpumask_var_t cpumask;
14742412c3aSSilvio Fricke 
14842412c3aSSilvio Fricke 	/**
14942412c3aSSilvio Fricke 	 * @no_numa: disable NUMA affinity
15042412c3aSSilvio Fricke 	 *
15142412c3aSSilvio Fricke 	 * Unlike other fields, ``no_numa`` isn't a property of a worker_pool. It
15242412c3aSSilvio Fricke 	 * only modifies how :c:func:`apply_workqueue_attrs` select pools and thus
15342412c3aSSilvio Fricke 	 * doesn't participate in pool hash calculations or equality comparisons.
15442412c3aSSilvio Fricke 	 */
15542412c3aSSilvio Fricke 	bool no_numa;
1567a4e344cSTejun Heo };
1577a4e344cSTejun Heo 
158bf6aede7SJean Delvare static inline struct delayed_work *to_delayed_work(struct work_struct *work)
159bf6aede7SJean Delvare {
160bf6aede7SJean Delvare 	return container_of(work, struct delayed_work, work);
161bf6aede7SJean Delvare }
162bf6aede7SJean Delvare 
16305f0fe6bSTejun Heo static inline struct rcu_work *to_rcu_work(struct work_struct *work)
16405f0fe6bSTejun Heo {
16505f0fe6bSTejun Heo 	return container_of(work, struct rcu_work, work);
16605f0fe6bSTejun Heo }
16705f0fe6bSTejun Heo 
1681fa44ecaSJames Bottomley struct execute_work {
1691fa44ecaSJames Bottomley 	struct work_struct work;
1701fa44ecaSJames Bottomley };
1711fa44ecaSJames Bottomley 
1724e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
1734e6045f1SJohannes Berg /*
1744e6045f1SJohannes Berg  * NB: because we have to copy the lockdep_map, setting _key
1754e6045f1SJohannes Berg  * here is required, otherwise it could get initialised to the
1764e6045f1SJohannes Berg  * copy of the lockdep_map!
1774e6045f1SJohannes Berg  */
1784e6045f1SJohannes Berg #define __WORK_INIT_LOCKDEP_MAP(n, k) \
1794e6045f1SJohannes Berg 	.lockdep_map = STATIC_LOCKDEP_MAP_INIT(n, k),
1804e6045f1SJohannes Berg #else
1814e6045f1SJohannes Berg #define __WORK_INIT_LOCKDEP_MAP(n, k)
1824e6045f1SJohannes Berg #endif
1834e6045f1SJohannes Berg 
18465f27f38SDavid Howells #define __WORK_INITIALIZER(n, f) {					\
185dc186ad7SThomas Gleixner 	.data = WORK_DATA_STATIC_INIT(),				\
18665f27f38SDavid Howells 	.entry	= { &(n).entry, &(n).entry },				\
18765f27f38SDavid Howells 	.func = (f),							\
1884e6045f1SJohannes Berg 	__WORK_INIT_LOCKDEP_MAP(#n, &(n))				\
18965f27f38SDavid Howells 	}
19065f27f38SDavid Howells 
191f991b318STejun Heo #define __DELAYED_WORK_INITIALIZER(n, f, tflags) {			\
19265f27f38SDavid Howells 	.work = __WORK_INITIALIZER((n).work, (f)),			\
193841b86f3SKees Cook 	.timer = __TIMER_INITIALIZER(delayed_work_timer_fn,\
194e0aecdd8STejun Heo 				     (tflags) | TIMER_IRQSAFE),		\
195dd6414b5SPhil Carmody 	}
196dd6414b5SPhil Carmody 
19765f27f38SDavid Howells #define DECLARE_WORK(n, f)						\
19865f27f38SDavid Howells 	struct work_struct n = __WORK_INITIALIZER(n, f)
19965f27f38SDavid Howells 
20065f27f38SDavid Howells #define DECLARE_DELAYED_WORK(n, f)					\
201f991b318STejun Heo 	struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f, 0)
20265f27f38SDavid Howells 
203203b42f7STejun Heo #define DECLARE_DEFERRABLE_WORK(n, f)					\
204f991b318STejun Heo 	struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f, TIMER_DEFERRABLE)
205dd6414b5SPhil Carmody 
206dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK
207dc186ad7SThomas Gleixner extern void __init_work(struct work_struct *work, int onstack);
208dc186ad7SThomas Gleixner extern void destroy_work_on_stack(struct work_struct *work);
209ea2e64f2SThomas Gleixner extern void destroy_delayed_work_on_stack(struct delayed_work *work);
2104690c4abSTejun Heo static inline unsigned int work_static(struct work_struct *work)
2114690c4abSTejun Heo {
21222df02bbSTejun Heo 	return *work_data_bits(work) & WORK_STRUCT_STATIC;
2134690c4abSTejun Heo }
214dc186ad7SThomas Gleixner #else
215dc186ad7SThomas Gleixner static inline void __init_work(struct work_struct *work, int onstack) { }
216dc186ad7SThomas Gleixner static inline void destroy_work_on_stack(struct work_struct *work) { }
217ea2e64f2SThomas Gleixner static inline void destroy_delayed_work_on_stack(struct delayed_work *work) { }
2184690c4abSTejun Heo static inline unsigned int work_static(struct work_struct *work) { return 0; }
219dc186ad7SThomas Gleixner #endif
220dc186ad7SThomas Gleixner 
2211da177e4SLinus Torvalds /*
22252bad64dSDavid Howells  * initialize all of a work item in one go
223a08727baSLinus Torvalds  *
224b9049df5SDmitri Vorobiev  * NOTE! No point in using "atomic_long_set()": using a direct
225a08727baSLinus Torvalds  * assignment of the work data initializer allows the compiler
226a08727baSLinus Torvalds  * to generate better code.
2271da177e4SLinus Torvalds  */
2284e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
229dc186ad7SThomas Gleixner #define __INIT_WORK(_work, _func, _onstack)				\
2304e6045f1SJohannes Berg 	do {								\
2314e6045f1SJohannes Berg 		static struct lock_class_key __key;			\
2324e6045f1SJohannes Berg 									\
233dc186ad7SThomas Gleixner 		__init_work((_work), _onstack);				\
2344e6045f1SJohannes Berg 		(_work)->data = (atomic_long_t) WORK_DATA_INIT();	\
235fd1a5b04SByungchul Park 		lockdep_init_map(&(_work)->lockdep_map, "(work_completion)"#_work, &__key, 0); \
2364e6045f1SJohannes Berg 		INIT_LIST_HEAD(&(_work)->entry);			\
237f073f922STejun Heo 		(_work)->func = (_func);				\
2384e6045f1SJohannes Berg 	} while (0)
2394e6045f1SJohannes Berg #else
240dc186ad7SThomas Gleixner #define __INIT_WORK(_work, _func, _onstack)				\
2411da177e4SLinus Torvalds 	do {								\
242dc186ad7SThomas Gleixner 		__init_work((_work), _onstack);				\
24323b2e599SOleg Nesterov 		(_work)->data = (atomic_long_t) WORK_DATA_INIT();	\
24465f27f38SDavid Howells 		INIT_LIST_HEAD(&(_work)->entry);			\
245f073f922STejun Heo 		(_work)->func = (_func);				\
24665f27f38SDavid Howells 	} while (0)
2474e6045f1SJohannes Berg #endif
24865f27f38SDavid Howells 
249dc186ad7SThomas Gleixner #define INIT_WORK(_work, _func)						\
2509da7dae9SValentin Rothberg 	__INIT_WORK((_work), (_func), 0)
251dc186ad7SThomas Gleixner 
252ca1cab37SAndrew Morton #define INIT_WORK_ONSTACK(_work, _func)					\
2539da7dae9SValentin Rothberg 	__INIT_WORK((_work), (_func), 1)
254dc186ad7SThomas Gleixner 
255f991b318STejun Heo #define __INIT_DELAYED_WORK(_work, _func, _tflags)			\
25665f27f38SDavid Howells 	do {								\
25765f27f38SDavid Howells 		INIT_WORK(&(_work)->work, (_func));			\
258919b250fSKees Cook 		__init_timer(&(_work)->timer,				\
259919b250fSKees Cook 			     delayed_work_timer_fn,			\
260e0aecdd8STejun Heo 			     (_tflags) | TIMER_IRQSAFE);		\
26165f27f38SDavid Howells 	} while (0)
26265f27f38SDavid Howells 
263f991b318STejun Heo #define __INIT_DELAYED_WORK_ONSTACK(_work, _func, _tflags)		\
2646d612b0fSPeter Zijlstra 	do {								\
265ca1cab37SAndrew Morton 		INIT_WORK_ONSTACK(&(_work)->work, (_func));		\
266919b250fSKees Cook 		__init_timer_on_stack(&(_work)->timer,			\
267919b250fSKees Cook 				      delayed_work_timer_fn,		\
268e0aecdd8STejun Heo 				      (_tflags) | TIMER_IRQSAFE);	\
2696d612b0fSPeter Zijlstra 	} while (0)
2706d612b0fSPeter Zijlstra 
271f991b318STejun Heo #define INIT_DELAYED_WORK(_work, _func)					\
272f991b318STejun Heo 	__INIT_DELAYED_WORK(_work, _func, 0)
273f991b318STejun Heo 
274f991b318STejun Heo #define INIT_DELAYED_WORK_ONSTACK(_work, _func)				\
275f991b318STejun Heo 	__INIT_DELAYED_WORK_ONSTACK(_work, _func, 0)
276f991b318STejun Heo 
277203b42f7STejun Heo #define INIT_DEFERRABLE_WORK(_work, _func)				\
278f991b318STejun Heo 	__INIT_DELAYED_WORK(_work, _func, TIMER_DEFERRABLE)
279f991b318STejun Heo 
280f991b318STejun Heo #define INIT_DEFERRABLE_WORK_ONSTACK(_work, _func)			\
281f991b318STejun Heo 	__INIT_DELAYED_WORK_ONSTACK(_work, _func, TIMER_DEFERRABLE)
28228287033SVenki Pallipadi 
28305f0fe6bSTejun Heo #define INIT_RCU_WORK(_work, _func)					\
28405f0fe6bSTejun Heo 	INIT_WORK(&(_work)->work, (_func))
28505f0fe6bSTejun Heo 
28605f0fe6bSTejun Heo #define INIT_RCU_WORK_ONSTACK(_work, _func)				\
28705f0fe6bSTejun Heo 	INIT_WORK_ONSTACK(&(_work)->work, (_func))
28805f0fe6bSTejun Heo 
289365970a1SDavid Howells /**
290365970a1SDavid Howells  * work_pending - Find out whether a work item is currently pending
291365970a1SDavid Howells  * @work: The work item in question
292365970a1SDavid Howells  */
293365970a1SDavid Howells #define work_pending(work) \
29422df02bbSTejun Heo 	test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))
295365970a1SDavid Howells 
296365970a1SDavid Howells /**
297365970a1SDavid Howells  * delayed_work_pending - Find out whether a delayable work item is currently
298365970a1SDavid Howells  * pending
299355c0663SJonathan Corbet  * @w: The work item in question
300365970a1SDavid Howells  */
3010221872aSLinus Torvalds #define delayed_work_pending(w) \
3020221872aSLinus Torvalds 	work_pending(&(w)->work)
303365970a1SDavid Howells 
304c54fce6eSTejun Heo /*
305c54fce6eSTejun Heo  * Workqueue flags and constants.  For details, please refer to
30642412c3aSSilvio Fricke  * Documentation/core-api/workqueue.rst.
307c54fce6eSTejun Heo  */
30897e37d7bSTejun Heo enum {
309c7fc77f7STejun Heo 	WQ_UNBOUND		= 1 << 1, /* not bound to any cpu */
31058a69cb4STejun Heo 	WQ_FREEZABLE		= 1 << 2, /* freeze during suspend */
3116370a6adSTejun Heo 	WQ_MEM_RECLAIM		= 1 << 3, /* may be used for memory reclaim */
312649027d7STejun Heo 	WQ_HIGHPRI		= 1 << 4, /* high priority */
31341f50094SGeert Uytterhoeven 	WQ_CPU_INTENSIVE	= 1 << 5, /* cpu intensive workqueue */
314226223abSTejun Heo 	WQ_SYSFS		= 1 << 6, /* visible in sysfs, see wq_sysfs_register() */
315b71ab8c2STejun Heo 
316cee22a15SViresh Kumar 	/*
317cee22a15SViresh Kumar 	 * Per-cpu workqueues are generally preferred because they tend to
318cee22a15SViresh Kumar 	 * show better performance thanks to cache locality.  Per-cpu
319cee22a15SViresh Kumar 	 * workqueues exclude the scheduler from choosing the CPU to
320cee22a15SViresh Kumar 	 * execute the worker threads, which has an unfortunate side effect
321cee22a15SViresh Kumar 	 * of increasing power consumption.
322cee22a15SViresh Kumar 	 *
323cee22a15SViresh Kumar 	 * The scheduler considers a CPU idle if it doesn't have any task
324cee22a15SViresh Kumar 	 * to execute and tries to keep idle cores idle to conserve power;
325cee22a15SViresh Kumar 	 * however, for example, a per-cpu work item scheduled from an
326cee22a15SViresh Kumar 	 * interrupt handler on an idle CPU will force the scheduler to
327cee22a15SViresh Kumar 	 * excute the work item on that CPU breaking the idleness, which in
328cee22a15SViresh Kumar 	 * turn may lead to more scheduling choices which are sub-optimal
329cee22a15SViresh Kumar 	 * in terms of power consumption.
330cee22a15SViresh Kumar 	 *
331cee22a15SViresh Kumar 	 * Workqueues marked with WQ_POWER_EFFICIENT are per-cpu by default
332cee22a15SViresh Kumar 	 * but become unbound if workqueue.power_efficient kernel param is
333cee22a15SViresh Kumar 	 * specified.  Per-cpu workqueues which are identified to
334cee22a15SViresh Kumar 	 * contribute significantly to power-consumption are identified and
335cee22a15SViresh Kumar 	 * marked with this flag and enabling the power_efficient mode
336cee22a15SViresh Kumar 	 * leads to noticeable power saving at the cost of small
337cee22a15SViresh Kumar 	 * performance disadvantage.
338cee22a15SViresh Kumar 	 *
339cee22a15SViresh Kumar 	 * http://thread.gmane.org/gmane.linux.kernel/1480396
340cee22a15SViresh Kumar 	 */
341cee22a15SViresh Kumar 	WQ_POWER_EFFICIENT	= 1 << 7,
342cee22a15SViresh Kumar 
343618b01ebSTejun Heo 	__WQ_DRAINING		= 1 << 16, /* internal: workqueue is draining */
3448719dceaSTejun Heo 	__WQ_ORDERED		= 1 << 17, /* internal: workqueue is ordered */
34523d11a58STejun Heo 	__WQ_LEGACY		= 1 << 18, /* internal: create*_workqueue() */
346fbf1c41fSBen Hutchings 	__WQ_ORDERED_EXPLICIT	= 1 << 19, /* internal: alloc_ordered_workqueue() */
347e41e704bSTejun Heo 
348b71ab8c2STejun Heo 	WQ_MAX_ACTIVE		= 512,	  /* I like 512, better ideas? */
349f3421797STejun Heo 	WQ_MAX_UNBOUND_PER_CPU	= 4,	  /* 4 * #cpus for unbound wq */
350b71ab8c2STejun Heo 	WQ_DFL_ACTIVE		= WQ_MAX_ACTIVE / 2,
35197e37d7bSTejun Heo };
35252bad64dSDavid Howells 
353f3421797STejun Heo /* unbound wq's aren't per-cpu, scale max_active according to #cpus */
354f3421797STejun Heo #define WQ_UNBOUND_MAX_ACTIVE	\
355f3421797STejun Heo 	max_t(int, WQ_MAX_ACTIVE, num_possible_cpus() * WQ_MAX_UNBOUND_PER_CPU)
356f3421797STejun Heo 
357d320c038STejun Heo /*
358d320c038STejun Heo  * System-wide workqueues which are always present.
359d320c038STejun Heo  *
360d320c038STejun Heo  * system_wq is the one used by schedule[_delayed]_work[_on]().
361d320c038STejun Heo  * Multi-CPU multi-threaded.  There are users which expect relatively
362d320c038STejun Heo  * short queue flush time.  Don't queue works which can run for too
363d320c038STejun Heo  * long.
364d320c038STejun Heo  *
36573e43544SLai Jiangshan  * system_highpri_wq is similar to system_wq but for work items which
36673e43544SLai Jiangshan  * require WQ_HIGHPRI.
36773e43544SLai Jiangshan  *
368d320c038STejun Heo  * system_long_wq is similar to system_wq but may host long running
369d320c038STejun Heo  * works.  Queue flushing might take relatively long.
370d320c038STejun Heo  *
371f3421797STejun Heo  * system_unbound_wq is unbound workqueue.  Workers are not bound to
372f3421797STejun Heo  * any specific CPU, not concurrency managed, and all queued works are
373f3421797STejun Heo  * executed immediately as long as max_active limit is not reached and
374f3421797STejun Heo  * resources are available.
3754149efb2STejun Heo  *
37624d51addSTejun Heo  * system_freezable_wq is equivalent to system_wq except that it's
37724d51addSTejun Heo  * freezable.
3780668106cSViresh Kumar  *
3790668106cSViresh Kumar  * *_power_efficient_wq are inclined towards saving power and converted
3800668106cSViresh Kumar  * into WQ_UNBOUND variants if 'wq_power_efficient' is enabled; otherwise,
3810668106cSViresh Kumar  * they are same as their non-power-efficient counterparts - e.g.
3820668106cSViresh Kumar  * system_power_efficient_wq is identical to system_wq if
3830668106cSViresh Kumar  * 'wq_power_efficient' is disabled.  See WQ_POWER_EFFICIENT for more info.
384d320c038STejun Heo  */
385d320c038STejun Heo extern struct workqueue_struct *system_wq;
38673e43544SLai Jiangshan extern struct workqueue_struct *system_highpri_wq;
387d320c038STejun Heo extern struct workqueue_struct *system_long_wq;
388f3421797STejun Heo extern struct workqueue_struct *system_unbound_wq;
38924d51addSTejun Heo extern struct workqueue_struct *system_freezable_wq;
3900668106cSViresh Kumar extern struct workqueue_struct *system_power_efficient_wq;
3910668106cSViresh Kumar extern struct workqueue_struct *system_freezable_power_efficient_wq;
392ae930e0fSTejun Heo 
393b196be89STejun Heo /**
394b196be89STejun Heo  * alloc_workqueue - allocate a workqueue
395b196be89STejun Heo  * @fmt: printf format for the name of the workqueue
396b196be89STejun Heo  * @flags: WQ_* flags
397b196be89STejun Heo  * @max_active: max in-flight work items, 0 for default
398669de8bdSBart Van Assche  * remaining args: args for @fmt
399b196be89STejun Heo  *
400b196be89STejun Heo  * Allocate a workqueue with the specified parameters.  For detailed
40142412c3aSSilvio Fricke  * information on WQ_* flags, please refer to
40242412c3aSSilvio Fricke  * Documentation/core-api/workqueue.rst.
403b196be89STejun Heo  *
404b196be89STejun Heo  * RETURNS:
405b196be89STejun Heo  * Pointer to the allocated workqueue on success, %NULL on failure.
406b196be89STejun Heo  */
407669de8bdSBart Van Assche struct workqueue_struct *alloc_workqueue(const char *fmt,
408669de8bdSBart Van Assche 					 unsigned int flags,
409669de8bdSBart Van Assche 					 int max_active, ...);
4104e6045f1SJohannes Berg 
41181dcaf65STejun Heo /**
41281dcaf65STejun Heo  * alloc_ordered_workqueue - allocate an ordered workqueue
413b196be89STejun Heo  * @fmt: printf format for the name of the workqueue
41458a69cb4STejun Heo  * @flags: WQ_* flags (only WQ_FREEZABLE and WQ_MEM_RECLAIM are meaningful)
415355c0663SJonathan Corbet  * @args...: args for @fmt
41681dcaf65STejun Heo  *
41781dcaf65STejun Heo  * Allocate an ordered workqueue.  An ordered workqueue executes at
41881dcaf65STejun Heo  * most one work item at any given time in the queued order.  They are
41981dcaf65STejun Heo  * implemented as unbound workqueues with @max_active of one.
42081dcaf65STejun Heo  *
42181dcaf65STejun Heo  * RETURNS:
42281dcaf65STejun Heo  * Pointer to the allocated workqueue on success, %NULL on failure.
42381dcaf65STejun Heo  */
424b196be89STejun Heo #define alloc_ordered_workqueue(fmt, flags, args...)			\
4250a94efb5STejun Heo 	alloc_workqueue(fmt, WQ_UNBOUND | __WQ_ORDERED |		\
4260a94efb5STejun Heo 			__WQ_ORDERED_EXPLICIT | (flags), 1, ##args)
42781dcaf65STejun Heo 
42897e37d7bSTejun Heo #define create_workqueue(name)						\
42923d11a58STejun Heo 	alloc_workqueue("%s", __WQ_LEGACY | WQ_MEM_RECLAIM, 1, (name))
43058a69cb4STejun Heo #define create_freezable_workqueue(name)				\
43123d11a58STejun Heo 	alloc_workqueue("%s", __WQ_LEGACY | WQ_FREEZABLE | WQ_UNBOUND |	\
43223d11a58STejun Heo 			WQ_MEM_RECLAIM, 1, (name))
43397e37d7bSTejun Heo #define create_singlethread_workqueue(name)				\
43423d11a58STejun Heo 	alloc_ordered_workqueue("%s", __WQ_LEGACY | WQ_MEM_RECLAIM, name)
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds extern void destroy_workqueue(struct workqueue_struct *wq);
4371da177e4SLinus Torvalds 
438513c98d0SDaniel Jordan struct workqueue_attrs *alloc_workqueue_attrs(void);
439513c98d0SDaniel Jordan void free_workqueue_attrs(struct workqueue_attrs *attrs);
440513c98d0SDaniel Jordan int apply_workqueue_attrs(struct workqueue_struct *wq,
441513c98d0SDaniel Jordan 			  const struct workqueue_attrs *attrs);
442042f7df1SLai Jiangshan int workqueue_set_unbound_cpumask(cpumask_var_t cpumask);
4437a4e344cSTejun Heo 
444d4283e93STejun Heo extern bool queue_work_on(int cpu, struct workqueue_struct *wq,
445c1a220e7SZhang Rui 			struct work_struct *work);
4468204e0c1SAlexander Duyck extern bool queue_work_node(int node, struct workqueue_struct *wq,
4478204e0c1SAlexander Duyck 			    struct work_struct *work);
448d4283e93STejun Heo extern bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
44952bad64dSDavid Howells 			struct delayed_work *work, unsigned long delay);
4508376fe22STejun Heo extern bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
4518376fe22STejun Heo 			struct delayed_work *dwork, unsigned long delay);
45205f0fe6bSTejun Heo extern bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork);
45328e53bddSOleg Nesterov 
454b3c97528SHarvey Harrison extern void flush_workqueue(struct workqueue_struct *wq);
4559c5a2ba7STejun Heo extern void drain_workqueue(struct workqueue_struct *wq);
4561da177e4SLinus Torvalds 
45765f27f38SDavid Howells extern int schedule_on_each_cpu(work_func_t func);
4581da177e4SLinus Torvalds 
45965f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *);
4601da177e4SLinus Torvalds 
461401a8d04STejun Heo extern bool flush_work(struct work_struct *work);
462401a8d04STejun Heo extern bool cancel_work_sync(struct work_struct *work);
463401a8d04STejun Heo 
464401a8d04STejun Heo extern bool flush_delayed_work(struct delayed_work *dwork);
46557b30ae7STejun Heo extern bool cancel_delayed_work(struct delayed_work *dwork);
466401a8d04STejun Heo extern bool cancel_delayed_work_sync(struct delayed_work *dwork);
46728e53bddSOleg Nesterov 
46805f0fe6bSTejun Heo extern bool flush_rcu_work(struct rcu_work *rwork);
46905f0fe6bSTejun Heo 
470dcd989cbSTejun Heo extern void workqueue_set_max_active(struct workqueue_struct *wq,
471dcd989cbSTejun Heo 				     int max_active);
47227d4ee03SLukas Wunner extern struct work_struct *current_work(void);
473e6267616STejun Heo extern bool current_is_workqueue_rescuer(void);
474d84ff051STejun Heo extern bool workqueue_congested(int cpu, struct workqueue_struct *wq);
475dcd989cbSTejun Heo extern unsigned int work_busy(struct work_struct *work);
4763d1cb205STejun Heo extern __printf(1, 2) void set_worker_desc(const char *fmt, ...);
4773d1cb205STejun Heo extern void print_worker_info(const char *log_lvl, struct task_struct *task);
4783494fc30STejun Heo extern void show_workqueue_state(void);
4796b59808bSTejun Heo extern void wq_worker_comm(char *buf, size_t size, struct task_struct *task);
480dcd989cbSTejun Heo 
4818425e3d5STejun Heo /**
4828425e3d5STejun Heo  * queue_work - queue work on a workqueue
4838425e3d5STejun Heo  * @wq: workqueue to use
4848425e3d5STejun Heo  * @work: work to queue
4858425e3d5STejun Heo  *
4868425e3d5STejun Heo  * Returns %false if @work was already on a queue, %true otherwise.
4878425e3d5STejun Heo  *
4888425e3d5STejun Heo  * We queue the work to the CPU on which it was submitted, but if the CPU dies
4898425e3d5STejun Heo  * it can be processed by another CPU.
490dbb92f88SAndrea Parri  *
491dbb92f88SAndrea Parri  * Memory-ordering properties:  If it returns %true, guarantees that all stores
492dbb92f88SAndrea Parri  * preceding the call to queue_work() in the program order will be visible from
493dbb92f88SAndrea Parri  * the CPU which will execute @work by the time such work executes, e.g.,
494dbb92f88SAndrea Parri  *
495dbb92f88SAndrea Parri  * { x is initially 0 }
496dbb92f88SAndrea Parri  *
497dbb92f88SAndrea Parri  *   CPU0				CPU1
498dbb92f88SAndrea Parri  *
499dbb92f88SAndrea Parri  *   WRITE_ONCE(x, 1);			[ @work is being executed ]
500dbb92f88SAndrea Parri  *   r0 = queue_work(wq, work);		  r1 = READ_ONCE(x);
501dbb92f88SAndrea Parri  *
502dbb92f88SAndrea Parri  * Forbids: r0 == true && r1 == 0
5038425e3d5STejun Heo  */
5048425e3d5STejun Heo static inline bool queue_work(struct workqueue_struct *wq,
5058425e3d5STejun Heo 			      struct work_struct *work)
5068425e3d5STejun Heo {
5078425e3d5STejun Heo 	return queue_work_on(WORK_CPU_UNBOUND, wq, work);
5088425e3d5STejun Heo }
5098425e3d5STejun Heo 
5108425e3d5STejun Heo /**
5118425e3d5STejun Heo  * queue_delayed_work - queue work on a workqueue after delay
5128425e3d5STejun Heo  * @wq: workqueue to use
5138425e3d5STejun Heo  * @dwork: delayable work to queue
5148425e3d5STejun Heo  * @delay: number of jiffies to wait before queueing
5158425e3d5STejun Heo  *
5168425e3d5STejun Heo  * Equivalent to queue_delayed_work_on() but tries to use the local CPU.
5178425e3d5STejun Heo  */
5188425e3d5STejun Heo static inline bool queue_delayed_work(struct workqueue_struct *wq,
5198425e3d5STejun Heo 				      struct delayed_work *dwork,
5208425e3d5STejun Heo 				      unsigned long delay)
5218425e3d5STejun Heo {
5228425e3d5STejun Heo 	return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
5238425e3d5STejun Heo }
5248425e3d5STejun Heo 
5258425e3d5STejun Heo /**
5268425e3d5STejun Heo  * mod_delayed_work - modify delay of or queue a delayed work
5278425e3d5STejun Heo  * @wq: workqueue to use
5288425e3d5STejun Heo  * @dwork: work to queue
5298425e3d5STejun Heo  * @delay: number of jiffies to wait before queueing
5308425e3d5STejun Heo  *
5318425e3d5STejun Heo  * mod_delayed_work_on() on local CPU.
5328425e3d5STejun Heo  */
5338425e3d5STejun Heo static inline bool mod_delayed_work(struct workqueue_struct *wq,
5348425e3d5STejun Heo 				    struct delayed_work *dwork,
5358425e3d5STejun Heo 				    unsigned long delay)
5368425e3d5STejun Heo {
5378425e3d5STejun Heo 	return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
5388425e3d5STejun Heo }
5398425e3d5STejun Heo 
5408425e3d5STejun Heo /**
5418425e3d5STejun Heo  * schedule_work_on - put work task on a specific cpu
5428425e3d5STejun Heo  * @cpu: cpu to put the work task on
5438425e3d5STejun Heo  * @work: job to be done
5448425e3d5STejun Heo  *
5458425e3d5STejun Heo  * This puts a job on a specific cpu
5468425e3d5STejun Heo  */
5478425e3d5STejun Heo static inline bool schedule_work_on(int cpu, struct work_struct *work)
5488425e3d5STejun Heo {
5498425e3d5STejun Heo 	return queue_work_on(cpu, system_wq, work);
5508425e3d5STejun Heo }
5518425e3d5STejun Heo 
5528425e3d5STejun Heo /**
5538425e3d5STejun Heo  * schedule_work - put work task in global workqueue
5548425e3d5STejun Heo  * @work: job to be done
5558425e3d5STejun Heo  *
5568425e3d5STejun Heo  * Returns %false if @work was already on the kernel-global workqueue and
5578425e3d5STejun Heo  * %true otherwise.
5588425e3d5STejun Heo  *
5598425e3d5STejun Heo  * This puts a job in the kernel-global workqueue if it was not already
5608425e3d5STejun Heo  * queued and leaves it in the same position on the kernel-global
5618425e3d5STejun Heo  * workqueue otherwise.
562dbb92f88SAndrea Parri  *
563dbb92f88SAndrea Parri  * Shares the same memory-ordering properties of queue_work(), cf. the
564dbb92f88SAndrea Parri  * DocBook header of queue_work().
5658425e3d5STejun Heo  */
5668425e3d5STejun Heo static inline bool schedule_work(struct work_struct *work)
5678425e3d5STejun Heo {
5688425e3d5STejun Heo 	return queue_work(system_wq, work);
5698425e3d5STejun Heo }
5708425e3d5STejun Heo 
5718425e3d5STejun Heo /**
57237b1ef31SLai Jiangshan  * flush_scheduled_work - ensure that any scheduled work has run to completion.
57337b1ef31SLai Jiangshan  *
57437b1ef31SLai Jiangshan  * Forces execution of the kernel-global workqueue and blocks until its
57537b1ef31SLai Jiangshan  * completion.
57637b1ef31SLai Jiangshan  *
57737b1ef31SLai Jiangshan  * Think twice before calling this function!  It's very easy to get into
57837b1ef31SLai Jiangshan  * trouble if you don't take great care.  Either of the following situations
57937b1ef31SLai Jiangshan  * will lead to deadlock:
58037b1ef31SLai Jiangshan  *
58137b1ef31SLai Jiangshan  *	One of the work items currently on the workqueue needs to acquire
58237b1ef31SLai Jiangshan  *	a lock held by your code or its caller.
58337b1ef31SLai Jiangshan  *
58437b1ef31SLai Jiangshan  *	Your code is running in the context of a work routine.
58537b1ef31SLai Jiangshan  *
58637b1ef31SLai Jiangshan  * They will be detected by lockdep when they occur, but the first might not
58737b1ef31SLai Jiangshan  * occur very often.  It depends on what work items are on the workqueue and
58837b1ef31SLai Jiangshan  * what locks they need, which you have no control over.
58937b1ef31SLai Jiangshan  *
59037b1ef31SLai Jiangshan  * In most situations flushing the entire workqueue is overkill; you merely
59137b1ef31SLai Jiangshan  * need to know that a particular work item isn't queued and isn't running.
59237b1ef31SLai Jiangshan  * In such cases you should use cancel_delayed_work_sync() or
59337b1ef31SLai Jiangshan  * cancel_work_sync() instead.
59437b1ef31SLai Jiangshan  */
59537b1ef31SLai Jiangshan static inline void flush_scheduled_work(void)
59637b1ef31SLai Jiangshan {
59737b1ef31SLai Jiangshan 	flush_workqueue(system_wq);
59837b1ef31SLai Jiangshan }
59937b1ef31SLai Jiangshan 
60037b1ef31SLai Jiangshan /**
6018425e3d5STejun Heo  * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
6028425e3d5STejun Heo  * @cpu: cpu to use
6038425e3d5STejun Heo  * @dwork: job to be done
6048425e3d5STejun Heo  * @delay: number of jiffies to wait
6058425e3d5STejun Heo  *
6068425e3d5STejun Heo  * After waiting for a given time this puts a job in the kernel-global
6078425e3d5STejun Heo  * workqueue on the specified CPU.
6088425e3d5STejun Heo  */
6098425e3d5STejun Heo static inline bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
6108425e3d5STejun Heo 					    unsigned long delay)
6118425e3d5STejun Heo {
6128425e3d5STejun Heo 	return queue_delayed_work_on(cpu, system_wq, dwork, delay);
6138425e3d5STejun Heo }
6148425e3d5STejun Heo 
6158425e3d5STejun Heo /**
6168425e3d5STejun Heo  * schedule_delayed_work - put work task in global workqueue after delay
6178425e3d5STejun Heo  * @dwork: job to be done
6188425e3d5STejun Heo  * @delay: number of jiffies to wait or 0 for immediate execution
6198425e3d5STejun Heo  *
6208425e3d5STejun Heo  * After waiting for a given time this puts a job in the kernel-global
6218425e3d5STejun Heo  * workqueue.
6228425e3d5STejun Heo  */
6238425e3d5STejun Heo static inline bool schedule_delayed_work(struct delayed_work *dwork,
6248425e3d5STejun Heo 					 unsigned long delay)
6258425e3d5STejun Heo {
6268425e3d5STejun Heo 	return queue_delayed_work(system_wq, dwork, delay);
6278425e3d5STejun Heo }
6288425e3d5STejun Heo 
6292d3854a3SRusty Russell #ifndef CONFIG_SMP
630d84ff051STejun Heo static inline long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
6312d3854a3SRusty Russell {
6322d3854a3SRusty Russell 	return fn(arg);
6332d3854a3SRusty Russell }
6340e8d6a93SThomas Gleixner static inline long work_on_cpu_safe(int cpu, long (*fn)(void *), void *arg)
6350e8d6a93SThomas Gleixner {
6360e8d6a93SThomas Gleixner 	return fn(arg);
6370e8d6a93SThomas Gleixner }
6382d3854a3SRusty Russell #else
639d84ff051STejun Heo long work_on_cpu(int cpu, long (*fn)(void *), void *arg);
6400e8d6a93SThomas Gleixner long work_on_cpu_safe(int cpu, long (*fn)(void *), void *arg);
6412d3854a3SRusty Russell #endif /* CONFIG_SMP */
642a25909a4SPaul E. McKenney 
643a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER
644a0a1a5fdSTejun Heo extern void freeze_workqueues_begin(void);
645a0a1a5fdSTejun Heo extern bool freeze_workqueues_busy(void);
646a0a1a5fdSTejun Heo extern void thaw_workqueues(void);
647a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */
648a0a1a5fdSTejun Heo 
649226223abSTejun Heo #ifdef CONFIG_SYSFS
650226223abSTejun Heo int workqueue_sysfs_register(struct workqueue_struct *wq);
651226223abSTejun Heo #else	/* CONFIG_SYSFS */
652226223abSTejun Heo static inline int workqueue_sysfs_register(struct workqueue_struct *wq)
653226223abSTejun Heo { return 0; }
654226223abSTejun Heo #endif	/* CONFIG_SYSFS */
655226223abSTejun Heo 
65682607adcSTejun Heo #ifdef CONFIG_WQ_WATCHDOG
65782607adcSTejun Heo void wq_watchdog_touch(int cpu);
65882607adcSTejun Heo #else	/* CONFIG_WQ_WATCHDOG */
65982607adcSTejun Heo static inline void wq_watchdog_touch(int cpu) { }
66082607adcSTejun Heo #endif	/* CONFIG_WQ_WATCHDOG */
66182607adcSTejun Heo 
6627ee681b2SThomas Gleixner #ifdef CONFIG_SMP
6637ee681b2SThomas Gleixner int workqueue_prepare_cpu(unsigned int cpu);
6647ee681b2SThomas Gleixner int workqueue_online_cpu(unsigned int cpu);
6657ee681b2SThomas Gleixner int workqueue_offline_cpu(unsigned int cpu);
6667ee681b2SThomas Gleixner #endif
6677ee681b2SThomas Gleixner 
6683347fa09STejun Heo int __init workqueue_init_early(void);
6693347fa09STejun Heo int __init workqueue_init(void);
6703347fa09STejun Heo 
6711da177e4SLinus Torvalds #endif
672