xref: /openbmc/linux/include/linux/workqueue.h (revision bdb0a654)
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 */
32f97a4a1aSLai Jiangshan 	WORK_STRUCT_INACTIVE_BIT= 1,	/* work item is inactive */
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,
45f97a4a1aSLai Jiangshan 	WORK_STRUCT_INACTIVE	= 1 << WORK_STRUCT_INACTIVE_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 
54*bdb0a654SLai Jiangshan 	WORK_NR_COLORS		= (1 << WORK_STRUCT_COLOR_BITS),
5573f53c4aSTejun Heo 
5679bc251fSLai Jiangshan 	/* not bound to any CPU, prefer the local CPU */
57f3421797STejun Heo 	WORK_CPU_UNBOUND	= NR_CPUS,
58bdbc5dd7STejun Heo 
5973f53c4aSTejun Heo 	/*
60c39ba6b3SLai Jiangshan 	 * Reserve 8 bits off of pwq pointer w/ debugobjects turned off.
61*bdb0a654SLai Jiangshan 	 * This makes pwqs aligned to 256 bytes and allows 16 workqueue
62112202d9STejun Heo 	 * flush colors.
6373f53c4aSTejun Heo 	 */
6473f53c4aSTejun Heo 	WORK_STRUCT_FLAG_BITS	= WORK_STRUCT_COLOR_SHIFT +
6573f53c4aSTejun Heo 				  WORK_STRUCT_COLOR_BITS,
6673f53c4aSTejun Heo 
67112202d9STejun Heo 	/* data contains off-queue information when !WORK_STRUCT_PWQ */
6845d9550aSLai Jiangshan 	WORK_OFFQ_FLAG_BASE	= WORK_STRUCT_COLOR_SHIFT,
69bbb68dfaSTejun Heo 
708603e1b3STejun Heo 	__WORK_OFFQ_CANCELING	= WORK_OFFQ_FLAG_BASE,
718603e1b3STejun Heo 	WORK_OFFQ_CANCELING	= (1 << __WORK_OFFQ_CANCELING),
72bbb68dfaSTejun Heo 
73715b06b8STejun Heo 	/*
74715b06b8STejun Heo 	 * When a work item is off queue, its high bits point to the last
757c3eed5cSTejun Heo 	 * pool it was on.  Cap at 31 bits and use the highest number to
767c3eed5cSTejun Heo 	 * indicate that no pool is associated.
77715b06b8STejun Heo 	 */
78bbb68dfaSTejun Heo 	WORK_OFFQ_FLAG_BITS	= 1,
797c3eed5cSTejun Heo 	WORK_OFFQ_POOL_SHIFT	= WORK_OFFQ_FLAG_BASE + WORK_OFFQ_FLAG_BITS,
807c3eed5cSTejun Heo 	WORK_OFFQ_LEFT		= BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT,
817c3eed5cSTejun Heo 	WORK_OFFQ_POOL_BITS	= WORK_OFFQ_LEFT <= 31 ? WORK_OFFQ_LEFT : 31,
827c3eed5cSTejun Heo 	WORK_OFFQ_POOL_NONE	= (1LU << WORK_OFFQ_POOL_BITS) - 1,
83b5490077STejun Heo 
84b5490077STejun Heo 	/* convenience constants */
850f900049STejun Heo 	WORK_STRUCT_FLAG_MASK	= (1UL << WORK_STRUCT_FLAG_BITS) - 1,
8622df02bbSTejun Heo 	WORK_STRUCT_WQ_DATA_MASK = ~WORK_STRUCT_FLAG_MASK,
877c3eed5cSTejun Heo 	WORK_STRUCT_NO_POOL	= (unsigned long)WORK_OFFQ_POOL_NONE << WORK_OFFQ_POOL_SHIFT,
88dcd989cbSTejun Heo 
89dcd989cbSTejun Heo 	/* bit mask for work_busy() return values */
90dcd989cbSTejun Heo 	WORK_BUSY_PENDING	= 1 << 0,
91dcd989cbSTejun Heo 	WORK_BUSY_RUNNING	= 1 << 1,
923d1cb205STejun Heo 
933d1cb205STejun Heo 	/* maximum string length for set_worker_desc() */
943d1cb205STejun Heo 	WORKER_DESC_LEN		= 24,
9522df02bbSTejun Heo };
9622df02bbSTejun Heo 
971da177e4SLinus Torvalds struct work_struct {
98a08727baSLinus Torvalds 	atomic_long_t data;
991da177e4SLinus Torvalds 	struct list_head entry;
1006bb49e59SDavid Howells 	work_func_t func;
1014e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
1024e6045f1SJohannes Berg 	struct lockdep_map lockdep_map;
1034e6045f1SJohannes Berg #endif
10452bad64dSDavid Howells };
10552bad64dSDavid Howells 
106a45463cbSArnd Bergmann #define WORK_DATA_INIT()	ATOMIC_LONG_INIT((unsigned long)WORK_STRUCT_NO_POOL)
1077a22ad75STejun Heo #define WORK_DATA_STATIC_INIT()	\
108a45463cbSArnd Bergmann 	ATOMIC_LONG_INIT((unsigned long)(WORK_STRUCT_NO_POOL | WORK_STRUCT_STATIC))
109a08727baSLinus Torvalds 
11052bad64dSDavid Howells struct delayed_work {
11152bad64dSDavid Howells 	struct work_struct work;
1121da177e4SLinus Torvalds 	struct timer_list timer;
11360c057bcSLai Jiangshan 
11460c057bcSLai Jiangshan 	/* target workqueue and CPU ->timer uses to queue ->work */
11560c057bcSLai Jiangshan 	struct workqueue_struct *wq;
1161265057fSTejun Heo 	int cpu;
1171da177e4SLinus Torvalds };
1181da177e4SLinus Torvalds 
11905f0fe6bSTejun Heo struct rcu_work {
12005f0fe6bSTejun Heo 	struct work_struct work;
12105f0fe6bSTejun Heo 	struct rcu_head rcu;
12205f0fe6bSTejun Heo 
12305f0fe6bSTejun Heo 	/* target workqueue ->rcu uses to queue ->work */
12405f0fe6bSTejun Heo 	struct workqueue_struct *wq;
12505f0fe6bSTejun Heo };
12605f0fe6bSTejun Heo 
12742412c3aSSilvio Fricke /**
12842412c3aSSilvio Fricke  * struct workqueue_attrs - A struct for workqueue attributes.
129d55262c4STejun Heo  *
13042412c3aSSilvio Fricke  * This can be used to change attributes of an unbound workqueue.
1317a4e344cSTejun Heo  */
1327a4e344cSTejun Heo struct workqueue_attrs {
13342412c3aSSilvio Fricke 	/**
13442412c3aSSilvio Fricke 	 * @nice: nice level
13542412c3aSSilvio Fricke 	 */
13642412c3aSSilvio Fricke 	int nice;
13742412c3aSSilvio Fricke 
13842412c3aSSilvio Fricke 	/**
13942412c3aSSilvio Fricke 	 * @cpumask: allowed CPUs
14042412c3aSSilvio Fricke 	 */
14142412c3aSSilvio Fricke 	cpumask_var_t cpumask;
14242412c3aSSilvio Fricke 
14342412c3aSSilvio Fricke 	/**
14442412c3aSSilvio Fricke 	 * @no_numa: disable NUMA affinity
14542412c3aSSilvio Fricke 	 *
14642412c3aSSilvio Fricke 	 * Unlike other fields, ``no_numa`` isn't a property of a worker_pool. It
14742412c3aSSilvio Fricke 	 * only modifies how :c:func:`apply_workqueue_attrs` select pools and thus
14842412c3aSSilvio Fricke 	 * doesn't participate in pool hash calculations or equality comparisons.
14942412c3aSSilvio Fricke 	 */
15042412c3aSSilvio Fricke 	bool no_numa;
1517a4e344cSTejun Heo };
1527a4e344cSTejun Heo 
153bf6aede7SJean Delvare static inline struct delayed_work *to_delayed_work(struct work_struct *work)
154bf6aede7SJean Delvare {
155bf6aede7SJean Delvare 	return container_of(work, struct delayed_work, work);
156bf6aede7SJean Delvare }
157bf6aede7SJean Delvare 
15805f0fe6bSTejun Heo static inline struct rcu_work *to_rcu_work(struct work_struct *work)
15905f0fe6bSTejun Heo {
16005f0fe6bSTejun Heo 	return container_of(work, struct rcu_work, work);
16105f0fe6bSTejun Heo }
16205f0fe6bSTejun Heo 
1631fa44ecaSJames Bottomley struct execute_work {
1641fa44ecaSJames Bottomley 	struct work_struct work;
1651fa44ecaSJames Bottomley };
1661fa44ecaSJames Bottomley 
1674e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
1684e6045f1SJohannes Berg /*
1694e6045f1SJohannes Berg  * NB: because we have to copy the lockdep_map, setting _key
1704e6045f1SJohannes Berg  * here is required, otherwise it could get initialised to the
1714e6045f1SJohannes Berg  * copy of the lockdep_map!
1724e6045f1SJohannes Berg  */
1734e6045f1SJohannes Berg #define __WORK_INIT_LOCKDEP_MAP(n, k) \
1744e6045f1SJohannes Berg 	.lockdep_map = STATIC_LOCKDEP_MAP_INIT(n, k),
1754e6045f1SJohannes Berg #else
1764e6045f1SJohannes Berg #define __WORK_INIT_LOCKDEP_MAP(n, k)
1774e6045f1SJohannes Berg #endif
1784e6045f1SJohannes Berg 
17965f27f38SDavid Howells #define __WORK_INITIALIZER(n, f) {					\
180dc186ad7SThomas Gleixner 	.data = WORK_DATA_STATIC_INIT(),				\
18165f27f38SDavid Howells 	.entry	= { &(n).entry, &(n).entry },				\
18265f27f38SDavid Howells 	.func = (f),							\
1834e6045f1SJohannes Berg 	__WORK_INIT_LOCKDEP_MAP(#n, &(n))				\
18465f27f38SDavid Howells 	}
18565f27f38SDavid Howells 
186f991b318STejun Heo #define __DELAYED_WORK_INITIALIZER(n, f, tflags) {			\
18765f27f38SDavid Howells 	.work = __WORK_INITIALIZER((n).work, (f)),			\
188841b86f3SKees Cook 	.timer = __TIMER_INITIALIZER(delayed_work_timer_fn,\
189e0aecdd8STejun Heo 				     (tflags) | TIMER_IRQSAFE),		\
190dd6414b5SPhil Carmody 	}
191dd6414b5SPhil Carmody 
19265f27f38SDavid Howells #define DECLARE_WORK(n, f)						\
19365f27f38SDavid Howells 	struct work_struct n = __WORK_INITIALIZER(n, f)
19465f27f38SDavid Howells 
19565f27f38SDavid Howells #define DECLARE_DELAYED_WORK(n, f)					\
196f991b318STejun Heo 	struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f, 0)
19765f27f38SDavid Howells 
198203b42f7STejun Heo #define DECLARE_DEFERRABLE_WORK(n, f)					\
199f991b318STejun Heo 	struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f, TIMER_DEFERRABLE)
200dd6414b5SPhil Carmody 
201dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK
202dc186ad7SThomas Gleixner extern void __init_work(struct work_struct *work, int onstack);
203dc186ad7SThomas Gleixner extern void destroy_work_on_stack(struct work_struct *work);
204ea2e64f2SThomas Gleixner extern void destroy_delayed_work_on_stack(struct delayed_work *work);
2054690c4abSTejun Heo static inline unsigned int work_static(struct work_struct *work)
2064690c4abSTejun Heo {
20722df02bbSTejun Heo 	return *work_data_bits(work) & WORK_STRUCT_STATIC;
2084690c4abSTejun Heo }
209dc186ad7SThomas Gleixner #else
210dc186ad7SThomas Gleixner static inline void __init_work(struct work_struct *work, int onstack) { }
211dc186ad7SThomas Gleixner static inline void destroy_work_on_stack(struct work_struct *work) { }
212ea2e64f2SThomas Gleixner static inline void destroy_delayed_work_on_stack(struct delayed_work *work) { }
2134690c4abSTejun Heo static inline unsigned int work_static(struct work_struct *work) { return 0; }
214dc186ad7SThomas Gleixner #endif
215dc186ad7SThomas Gleixner 
2161da177e4SLinus Torvalds /*
21752bad64dSDavid Howells  * initialize all of a work item in one go
218a08727baSLinus Torvalds  *
219b9049df5SDmitri Vorobiev  * NOTE! No point in using "atomic_long_set()": using a direct
220a08727baSLinus Torvalds  * assignment of the work data initializer allows the compiler
221a08727baSLinus Torvalds  * to generate better code.
2221da177e4SLinus Torvalds  */
2234e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
224dc186ad7SThomas Gleixner #define __INIT_WORK(_work, _func, _onstack)				\
2254e6045f1SJohannes Berg 	do {								\
2264e6045f1SJohannes Berg 		static struct lock_class_key __key;			\
2274e6045f1SJohannes Berg 									\
228dc186ad7SThomas Gleixner 		__init_work((_work), _onstack);				\
2294e6045f1SJohannes Berg 		(_work)->data = (atomic_long_t) WORK_DATA_INIT();	\
230fd1a5b04SByungchul Park 		lockdep_init_map(&(_work)->lockdep_map, "(work_completion)"#_work, &__key, 0); \
2314e6045f1SJohannes Berg 		INIT_LIST_HEAD(&(_work)->entry);			\
232f073f922STejun Heo 		(_work)->func = (_func);				\
2334e6045f1SJohannes Berg 	} while (0)
2344e6045f1SJohannes Berg #else
235dc186ad7SThomas Gleixner #define __INIT_WORK(_work, _func, _onstack)				\
2361da177e4SLinus Torvalds 	do {								\
237dc186ad7SThomas Gleixner 		__init_work((_work), _onstack);				\
23823b2e599SOleg Nesterov 		(_work)->data = (atomic_long_t) WORK_DATA_INIT();	\
23965f27f38SDavid Howells 		INIT_LIST_HEAD(&(_work)->entry);			\
240f073f922STejun Heo 		(_work)->func = (_func);				\
24165f27f38SDavid Howells 	} while (0)
2424e6045f1SJohannes Berg #endif
24365f27f38SDavid Howells 
244dc186ad7SThomas Gleixner #define INIT_WORK(_work, _func)						\
2459da7dae9SValentin Rothberg 	__INIT_WORK((_work), (_func), 0)
246dc186ad7SThomas Gleixner 
247ca1cab37SAndrew Morton #define INIT_WORK_ONSTACK(_work, _func)					\
2489da7dae9SValentin Rothberg 	__INIT_WORK((_work), (_func), 1)
249dc186ad7SThomas Gleixner 
250f991b318STejun Heo #define __INIT_DELAYED_WORK(_work, _func, _tflags)			\
25165f27f38SDavid Howells 	do {								\
25265f27f38SDavid Howells 		INIT_WORK(&(_work)->work, (_func));			\
253919b250fSKees Cook 		__init_timer(&(_work)->timer,				\
254919b250fSKees Cook 			     delayed_work_timer_fn,			\
255e0aecdd8STejun Heo 			     (_tflags) | TIMER_IRQSAFE);		\
25665f27f38SDavid Howells 	} while (0)
25765f27f38SDavid Howells 
258f991b318STejun Heo #define __INIT_DELAYED_WORK_ONSTACK(_work, _func, _tflags)		\
2596d612b0fSPeter Zijlstra 	do {								\
260ca1cab37SAndrew Morton 		INIT_WORK_ONSTACK(&(_work)->work, (_func));		\
261919b250fSKees Cook 		__init_timer_on_stack(&(_work)->timer,			\
262919b250fSKees Cook 				      delayed_work_timer_fn,		\
263e0aecdd8STejun Heo 				      (_tflags) | TIMER_IRQSAFE);	\
2646d612b0fSPeter Zijlstra 	} while (0)
2656d612b0fSPeter Zijlstra 
266f991b318STejun Heo #define INIT_DELAYED_WORK(_work, _func)					\
267f991b318STejun Heo 	__INIT_DELAYED_WORK(_work, _func, 0)
268f991b318STejun Heo 
269f991b318STejun Heo #define INIT_DELAYED_WORK_ONSTACK(_work, _func)				\
270f991b318STejun Heo 	__INIT_DELAYED_WORK_ONSTACK(_work, _func, 0)
271f991b318STejun Heo 
272203b42f7STejun Heo #define INIT_DEFERRABLE_WORK(_work, _func)				\
273f991b318STejun Heo 	__INIT_DELAYED_WORK(_work, _func, TIMER_DEFERRABLE)
274f991b318STejun Heo 
275f991b318STejun Heo #define INIT_DEFERRABLE_WORK_ONSTACK(_work, _func)			\
276f991b318STejun Heo 	__INIT_DELAYED_WORK_ONSTACK(_work, _func, TIMER_DEFERRABLE)
27728287033SVenki Pallipadi 
27805f0fe6bSTejun Heo #define INIT_RCU_WORK(_work, _func)					\
27905f0fe6bSTejun Heo 	INIT_WORK(&(_work)->work, (_func))
28005f0fe6bSTejun Heo 
28105f0fe6bSTejun Heo #define INIT_RCU_WORK_ONSTACK(_work, _func)				\
28205f0fe6bSTejun Heo 	INIT_WORK_ONSTACK(&(_work)->work, (_func))
28305f0fe6bSTejun Heo 
284365970a1SDavid Howells /**
285365970a1SDavid Howells  * work_pending - Find out whether a work item is currently pending
286365970a1SDavid Howells  * @work: The work item in question
287365970a1SDavid Howells  */
288365970a1SDavid Howells #define work_pending(work) \
28922df02bbSTejun Heo 	test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))
290365970a1SDavid Howells 
291365970a1SDavid Howells /**
292365970a1SDavid Howells  * delayed_work_pending - Find out whether a delayable work item is currently
293365970a1SDavid Howells  * pending
294355c0663SJonathan Corbet  * @w: The work item in question
295365970a1SDavid Howells  */
2960221872aSLinus Torvalds #define delayed_work_pending(w) \
2970221872aSLinus Torvalds 	work_pending(&(w)->work)
298365970a1SDavid Howells 
299c54fce6eSTejun Heo /*
300c54fce6eSTejun Heo  * Workqueue flags and constants.  For details, please refer to
30142412c3aSSilvio Fricke  * Documentation/core-api/workqueue.rst.
302c54fce6eSTejun Heo  */
30397e37d7bSTejun Heo enum {
304c7fc77f7STejun Heo 	WQ_UNBOUND		= 1 << 1, /* not bound to any cpu */
30558a69cb4STejun Heo 	WQ_FREEZABLE		= 1 << 2, /* freeze during suspend */
3066370a6adSTejun Heo 	WQ_MEM_RECLAIM		= 1 << 3, /* may be used for memory reclaim */
307649027d7STejun Heo 	WQ_HIGHPRI		= 1 << 4, /* high priority */
30841f50094SGeert Uytterhoeven 	WQ_CPU_INTENSIVE	= 1 << 5, /* cpu intensive workqueue */
30993e86295SMenglong Dong 	WQ_SYSFS		= 1 << 6, /* visible in sysfs, see workqueue_sysfs_register() */
310b71ab8c2STejun Heo 
311cee22a15SViresh Kumar 	/*
312cee22a15SViresh Kumar 	 * Per-cpu workqueues are generally preferred because they tend to
313cee22a15SViresh Kumar 	 * show better performance thanks to cache locality.  Per-cpu
314cee22a15SViresh Kumar 	 * workqueues exclude the scheduler from choosing the CPU to
315cee22a15SViresh Kumar 	 * execute the worker threads, which has an unfortunate side effect
316cee22a15SViresh Kumar 	 * of increasing power consumption.
317cee22a15SViresh Kumar 	 *
318cee22a15SViresh Kumar 	 * The scheduler considers a CPU idle if it doesn't have any task
319cee22a15SViresh Kumar 	 * to execute and tries to keep idle cores idle to conserve power;
320cee22a15SViresh Kumar 	 * however, for example, a per-cpu work item scheduled from an
321cee22a15SViresh Kumar 	 * interrupt handler on an idle CPU will force the scheduler to
32267dc8325SCai Huoqing 	 * execute the work item on that CPU breaking the idleness, which in
323cee22a15SViresh Kumar 	 * turn may lead to more scheduling choices which are sub-optimal
324cee22a15SViresh Kumar 	 * in terms of power consumption.
325cee22a15SViresh Kumar 	 *
326cee22a15SViresh Kumar 	 * Workqueues marked with WQ_POWER_EFFICIENT are per-cpu by default
327cee22a15SViresh Kumar 	 * but become unbound if workqueue.power_efficient kernel param is
328cee22a15SViresh Kumar 	 * specified.  Per-cpu workqueues which are identified to
329cee22a15SViresh Kumar 	 * contribute significantly to power-consumption are identified and
330cee22a15SViresh Kumar 	 * marked with this flag and enabling the power_efficient mode
331cee22a15SViresh Kumar 	 * leads to noticeable power saving at the cost of small
332cee22a15SViresh Kumar 	 * performance disadvantage.
333cee22a15SViresh Kumar 	 *
334cee22a15SViresh Kumar 	 * http://thread.gmane.org/gmane.linux.kernel/1480396
335cee22a15SViresh Kumar 	 */
336cee22a15SViresh Kumar 	WQ_POWER_EFFICIENT	= 1 << 7,
337cee22a15SViresh Kumar 
338618b01ebSTejun Heo 	__WQ_DRAINING		= 1 << 16, /* internal: workqueue is draining */
3398719dceaSTejun Heo 	__WQ_ORDERED		= 1 << 17, /* internal: workqueue is ordered */
34023d11a58STejun Heo 	__WQ_LEGACY		= 1 << 18, /* internal: create*_workqueue() */
341fbf1c41fSBen Hutchings 	__WQ_ORDERED_EXPLICIT	= 1 << 19, /* internal: alloc_ordered_workqueue() */
342e41e704bSTejun Heo 
343b71ab8c2STejun Heo 	WQ_MAX_ACTIVE		= 512,	  /* I like 512, better ideas? */
344f3421797STejun Heo 	WQ_MAX_UNBOUND_PER_CPU	= 4,	  /* 4 * #cpus for unbound wq */
345b71ab8c2STejun Heo 	WQ_DFL_ACTIVE		= WQ_MAX_ACTIVE / 2,
34697e37d7bSTejun Heo };
34752bad64dSDavid Howells 
348f3421797STejun Heo /* unbound wq's aren't per-cpu, scale max_active according to #cpus */
349f3421797STejun Heo #define WQ_UNBOUND_MAX_ACTIVE	\
350f3421797STejun Heo 	max_t(int, WQ_MAX_ACTIVE, num_possible_cpus() * WQ_MAX_UNBOUND_PER_CPU)
351f3421797STejun Heo 
352d320c038STejun Heo /*
353d320c038STejun Heo  * System-wide workqueues which are always present.
354d320c038STejun Heo  *
355d320c038STejun Heo  * system_wq is the one used by schedule[_delayed]_work[_on]().
356d320c038STejun Heo  * Multi-CPU multi-threaded.  There are users which expect relatively
357d320c038STejun Heo  * short queue flush time.  Don't queue works which can run for too
358d320c038STejun Heo  * long.
359d320c038STejun Heo  *
36073e43544SLai Jiangshan  * system_highpri_wq is similar to system_wq but for work items which
36173e43544SLai Jiangshan  * require WQ_HIGHPRI.
36273e43544SLai Jiangshan  *
363d320c038STejun Heo  * system_long_wq is similar to system_wq but may host long running
364d320c038STejun Heo  * works.  Queue flushing might take relatively long.
365d320c038STejun Heo  *
366f3421797STejun Heo  * system_unbound_wq is unbound workqueue.  Workers are not bound to
367f3421797STejun Heo  * any specific CPU, not concurrency managed, and all queued works are
368f3421797STejun Heo  * executed immediately as long as max_active limit is not reached and
369f3421797STejun Heo  * resources are available.
3704149efb2STejun Heo  *
37124d51addSTejun Heo  * system_freezable_wq is equivalent to system_wq except that it's
37224d51addSTejun Heo  * freezable.
3730668106cSViresh Kumar  *
3740668106cSViresh Kumar  * *_power_efficient_wq are inclined towards saving power and converted
3750668106cSViresh Kumar  * into WQ_UNBOUND variants if 'wq_power_efficient' is enabled; otherwise,
3760668106cSViresh Kumar  * they are same as their non-power-efficient counterparts - e.g.
3770668106cSViresh Kumar  * system_power_efficient_wq is identical to system_wq if
3780668106cSViresh Kumar  * 'wq_power_efficient' is disabled.  See WQ_POWER_EFFICIENT for more info.
379d320c038STejun Heo  */
380d320c038STejun Heo extern struct workqueue_struct *system_wq;
38173e43544SLai Jiangshan extern struct workqueue_struct *system_highpri_wq;
382d320c038STejun Heo extern struct workqueue_struct *system_long_wq;
383f3421797STejun Heo extern struct workqueue_struct *system_unbound_wq;
38424d51addSTejun Heo extern struct workqueue_struct *system_freezable_wq;
3850668106cSViresh Kumar extern struct workqueue_struct *system_power_efficient_wq;
3860668106cSViresh Kumar extern struct workqueue_struct *system_freezable_power_efficient_wq;
387ae930e0fSTejun Heo 
388b196be89STejun Heo /**
389b196be89STejun Heo  * alloc_workqueue - allocate a workqueue
390b196be89STejun Heo  * @fmt: printf format for the name of the workqueue
391b196be89STejun Heo  * @flags: WQ_* flags
392b196be89STejun Heo  * @max_active: max in-flight work items, 0 for default
393669de8bdSBart Van Assche  * remaining args: args for @fmt
394b196be89STejun Heo  *
395b196be89STejun Heo  * Allocate a workqueue with the specified parameters.  For detailed
39642412c3aSSilvio Fricke  * information on WQ_* flags, please refer to
39742412c3aSSilvio Fricke  * Documentation/core-api/workqueue.rst.
398b196be89STejun Heo  *
399b196be89STejun Heo  * RETURNS:
400b196be89STejun Heo  * Pointer to the allocated workqueue on success, %NULL on failure.
401b196be89STejun Heo  */
402669de8bdSBart Van Assche struct workqueue_struct *alloc_workqueue(const char *fmt,
403669de8bdSBart Van Assche 					 unsigned int flags,
404669de8bdSBart Van Assche 					 int max_active, ...);
4054e6045f1SJohannes Berg 
40681dcaf65STejun Heo /**
40781dcaf65STejun Heo  * alloc_ordered_workqueue - allocate an ordered workqueue
408b196be89STejun Heo  * @fmt: printf format for the name of the workqueue
40958a69cb4STejun Heo  * @flags: WQ_* flags (only WQ_FREEZABLE and WQ_MEM_RECLAIM are meaningful)
410355c0663SJonathan Corbet  * @args...: args for @fmt
41181dcaf65STejun Heo  *
41281dcaf65STejun Heo  * Allocate an ordered workqueue.  An ordered workqueue executes at
41381dcaf65STejun Heo  * most one work item at any given time in the queued order.  They are
41481dcaf65STejun Heo  * implemented as unbound workqueues with @max_active of one.
41581dcaf65STejun Heo  *
41681dcaf65STejun Heo  * RETURNS:
41781dcaf65STejun Heo  * Pointer to the allocated workqueue on success, %NULL on failure.
41881dcaf65STejun Heo  */
419b196be89STejun Heo #define alloc_ordered_workqueue(fmt, flags, args...)			\
4200a94efb5STejun Heo 	alloc_workqueue(fmt, WQ_UNBOUND | __WQ_ORDERED |		\
4210a94efb5STejun Heo 			__WQ_ORDERED_EXPLICIT | (flags), 1, ##args)
42281dcaf65STejun Heo 
42397e37d7bSTejun Heo #define create_workqueue(name)						\
42423d11a58STejun Heo 	alloc_workqueue("%s", __WQ_LEGACY | WQ_MEM_RECLAIM, 1, (name))
42558a69cb4STejun Heo #define create_freezable_workqueue(name)				\
42623d11a58STejun Heo 	alloc_workqueue("%s", __WQ_LEGACY | WQ_FREEZABLE | WQ_UNBOUND |	\
42723d11a58STejun Heo 			WQ_MEM_RECLAIM, 1, (name))
42897e37d7bSTejun Heo #define create_singlethread_workqueue(name)				\
42923d11a58STejun Heo 	alloc_ordered_workqueue("%s", __WQ_LEGACY | WQ_MEM_RECLAIM, name)
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds extern void destroy_workqueue(struct workqueue_struct *wq);
4321da177e4SLinus Torvalds 
433513c98d0SDaniel Jordan struct workqueue_attrs *alloc_workqueue_attrs(void);
434513c98d0SDaniel Jordan void free_workqueue_attrs(struct workqueue_attrs *attrs);
435513c98d0SDaniel Jordan int apply_workqueue_attrs(struct workqueue_struct *wq,
436513c98d0SDaniel Jordan 			  const struct workqueue_attrs *attrs);
437042f7df1SLai Jiangshan int workqueue_set_unbound_cpumask(cpumask_var_t cpumask);
4387a4e344cSTejun Heo 
439d4283e93STejun Heo extern bool queue_work_on(int cpu, struct workqueue_struct *wq,
440c1a220e7SZhang Rui 			struct work_struct *work);
4418204e0c1SAlexander Duyck extern bool queue_work_node(int node, struct workqueue_struct *wq,
4428204e0c1SAlexander Duyck 			    struct work_struct *work);
443d4283e93STejun Heo extern bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
44452bad64dSDavid Howells 			struct delayed_work *work, unsigned long delay);
4458376fe22STejun Heo extern bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
4468376fe22STejun Heo 			struct delayed_work *dwork, unsigned long delay);
44705f0fe6bSTejun Heo extern bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork);
44828e53bddSOleg Nesterov 
449b3c97528SHarvey Harrison extern void flush_workqueue(struct workqueue_struct *wq);
4509c5a2ba7STejun Heo extern void drain_workqueue(struct workqueue_struct *wq);
4511da177e4SLinus Torvalds 
45265f27f38SDavid Howells extern int schedule_on_each_cpu(work_func_t func);
4531da177e4SLinus Torvalds 
45465f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *);
4551da177e4SLinus Torvalds 
456401a8d04STejun Heo extern bool flush_work(struct work_struct *work);
457401a8d04STejun Heo extern bool cancel_work_sync(struct work_struct *work);
458401a8d04STejun Heo 
459401a8d04STejun Heo extern bool flush_delayed_work(struct delayed_work *dwork);
46057b30ae7STejun Heo extern bool cancel_delayed_work(struct delayed_work *dwork);
461401a8d04STejun Heo extern bool cancel_delayed_work_sync(struct delayed_work *dwork);
46228e53bddSOleg Nesterov 
46305f0fe6bSTejun Heo extern bool flush_rcu_work(struct rcu_work *rwork);
46405f0fe6bSTejun Heo 
465dcd989cbSTejun Heo extern void workqueue_set_max_active(struct workqueue_struct *wq,
466dcd989cbSTejun Heo 				     int max_active);
46727d4ee03SLukas Wunner extern struct work_struct *current_work(void);
468e6267616STejun Heo extern bool current_is_workqueue_rescuer(void);
469d84ff051STejun Heo extern bool workqueue_congested(int cpu, struct workqueue_struct *wq);
470dcd989cbSTejun Heo extern unsigned int work_busy(struct work_struct *work);
4713d1cb205STejun Heo extern __printf(1, 2) void set_worker_desc(const char *fmt, ...);
4723d1cb205STejun Heo extern void print_worker_info(const char *log_lvl, struct task_struct *task);
4733494fc30STejun Heo extern void show_workqueue_state(void);
4746b59808bSTejun Heo extern void wq_worker_comm(char *buf, size_t size, struct task_struct *task);
475dcd989cbSTejun Heo 
4768425e3d5STejun Heo /**
4778425e3d5STejun Heo  * queue_work - queue work on a workqueue
4788425e3d5STejun Heo  * @wq: workqueue to use
4798425e3d5STejun Heo  * @work: work to queue
4808425e3d5STejun Heo  *
4818425e3d5STejun Heo  * Returns %false if @work was already on a queue, %true otherwise.
4828425e3d5STejun Heo  *
4838425e3d5STejun Heo  * We queue the work to the CPU on which it was submitted, but if the CPU dies
4848425e3d5STejun Heo  * it can be processed by another CPU.
485dbb92f88SAndrea Parri  *
486dbb92f88SAndrea Parri  * Memory-ordering properties:  If it returns %true, guarantees that all stores
487dbb92f88SAndrea Parri  * preceding the call to queue_work() in the program order will be visible from
488dbb92f88SAndrea Parri  * the CPU which will execute @work by the time such work executes, e.g.,
489dbb92f88SAndrea Parri  *
490dbb92f88SAndrea Parri  * { x is initially 0 }
491dbb92f88SAndrea Parri  *
492dbb92f88SAndrea Parri  *   CPU0				CPU1
493dbb92f88SAndrea Parri  *
494dbb92f88SAndrea Parri  *   WRITE_ONCE(x, 1);			[ @work is being executed ]
495dbb92f88SAndrea Parri  *   r0 = queue_work(wq, work);		  r1 = READ_ONCE(x);
496dbb92f88SAndrea Parri  *
497dbb92f88SAndrea Parri  * Forbids: r0 == true && r1 == 0
4988425e3d5STejun Heo  */
4998425e3d5STejun Heo static inline bool queue_work(struct workqueue_struct *wq,
5008425e3d5STejun Heo 			      struct work_struct *work)
5018425e3d5STejun Heo {
5028425e3d5STejun Heo 	return queue_work_on(WORK_CPU_UNBOUND, wq, work);
5038425e3d5STejun Heo }
5048425e3d5STejun Heo 
5058425e3d5STejun Heo /**
5068425e3d5STejun Heo  * queue_delayed_work - queue work on a workqueue after delay
5078425e3d5STejun Heo  * @wq: workqueue to use
5088425e3d5STejun Heo  * @dwork: delayable work to queue
5098425e3d5STejun Heo  * @delay: number of jiffies to wait before queueing
5108425e3d5STejun Heo  *
5118425e3d5STejun Heo  * Equivalent to queue_delayed_work_on() but tries to use the local CPU.
5128425e3d5STejun Heo  */
5138425e3d5STejun Heo static inline bool queue_delayed_work(struct workqueue_struct *wq,
5148425e3d5STejun Heo 				      struct delayed_work *dwork,
5158425e3d5STejun Heo 				      unsigned long delay)
5168425e3d5STejun Heo {
5178425e3d5STejun Heo 	return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
5188425e3d5STejun Heo }
5198425e3d5STejun Heo 
5208425e3d5STejun Heo /**
5218425e3d5STejun Heo  * mod_delayed_work - modify delay of or queue a delayed work
5228425e3d5STejun Heo  * @wq: workqueue to use
5238425e3d5STejun Heo  * @dwork: work to queue
5248425e3d5STejun Heo  * @delay: number of jiffies to wait before queueing
5258425e3d5STejun Heo  *
5268425e3d5STejun Heo  * mod_delayed_work_on() on local CPU.
5278425e3d5STejun Heo  */
5288425e3d5STejun Heo static inline bool mod_delayed_work(struct workqueue_struct *wq,
5298425e3d5STejun Heo 				    struct delayed_work *dwork,
5308425e3d5STejun Heo 				    unsigned long delay)
5318425e3d5STejun Heo {
5328425e3d5STejun Heo 	return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
5338425e3d5STejun Heo }
5348425e3d5STejun Heo 
5358425e3d5STejun Heo /**
5368425e3d5STejun Heo  * schedule_work_on - put work task on a specific cpu
5378425e3d5STejun Heo  * @cpu: cpu to put the work task on
5388425e3d5STejun Heo  * @work: job to be done
5398425e3d5STejun Heo  *
5408425e3d5STejun Heo  * This puts a job on a specific cpu
5418425e3d5STejun Heo  */
5428425e3d5STejun Heo static inline bool schedule_work_on(int cpu, struct work_struct *work)
5438425e3d5STejun Heo {
5448425e3d5STejun Heo 	return queue_work_on(cpu, system_wq, work);
5458425e3d5STejun Heo }
5468425e3d5STejun Heo 
5478425e3d5STejun Heo /**
5488425e3d5STejun Heo  * schedule_work - put work task in global workqueue
5498425e3d5STejun Heo  * @work: job to be done
5508425e3d5STejun Heo  *
5518425e3d5STejun Heo  * Returns %false if @work was already on the kernel-global workqueue and
5528425e3d5STejun Heo  * %true otherwise.
5538425e3d5STejun Heo  *
5548425e3d5STejun Heo  * This puts a job in the kernel-global workqueue if it was not already
5558425e3d5STejun Heo  * queued and leaves it in the same position on the kernel-global
5568425e3d5STejun Heo  * workqueue otherwise.
557dbb92f88SAndrea Parri  *
558dbb92f88SAndrea Parri  * Shares the same memory-ordering properties of queue_work(), cf. the
559dbb92f88SAndrea Parri  * DocBook header of queue_work().
5608425e3d5STejun Heo  */
5618425e3d5STejun Heo static inline bool schedule_work(struct work_struct *work)
5628425e3d5STejun Heo {
5638425e3d5STejun Heo 	return queue_work(system_wq, work);
5648425e3d5STejun Heo }
5658425e3d5STejun Heo 
5668425e3d5STejun Heo /**
56737b1ef31SLai Jiangshan  * flush_scheduled_work - ensure that any scheduled work has run to completion.
56837b1ef31SLai Jiangshan  *
56937b1ef31SLai Jiangshan  * Forces execution of the kernel-global workqueue and blocks until its
57037b1ef31SLai Jiangshan  * completion.
57137b1ef31SLai Jiangshan  *
57237b1ef31SLai Jiangshan  * Think twice before calling this function!  It's very easy to get into
57337b1ef31SLai Jiangshan  * trouble if you don't take great care.  Either of the following situations
57437b1ef31SLai Jiangshan  * will lead to deadlock:
57537b1ef31SLai Jiangshan  *
57637b1ef31SLai Jiangshan  *	One of the work items currently on the workqueue needs to acquire
57737b1ef31SLai Jiangshan  *	a lock held by your code or its caller.
57837b1ef31SLai Jiangshan  *
57937b1ef31SLai Jiangshan  *	Your code is running in the context of a work routine.
58037b1ef31SLai Jiangshan  *
58137b1ef31SLai Jiangshan  * They will be detected by lockdep when they occur, but the first might not
58237b1ef31SLai Jiangshan  * occur very often.  It depends on what work items are on the workqueue and
58337b1ef31SLai Jiangshan  * what locks they need, which you have no control over.
58437b1ef31SLai Jiangshan  *
58537b1ef31SLai Jiangshan  * In most situations flushing the entire workqueue is overkill; you merely
58637b1ef31SLai Jiangshan  * need to know that a particular work item isn't queued and isn't running.
58737b1ef31SLai Jiangshan  * In such cases you should use cancel_delayed_work_sync() or
58837b1ef31SLai Jiangshan  * cancel_work_sync() instead.
58937b1ef31SLai Jiangshan  */
59037b1ef31SLai Jiangshan static inline void flush_scheduled_work(void)
59137b1ef31SLai Jiangshan {
59237b1ef31SLai Jiangshan 	flush_workqueue(system_wq);
59337b1ef31SLai Jiangshan }
59437b1ef31SLai Jiangshan 
59537b1ef31SLai Jiangshan /**
5968425e3d5STejun Heo  * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
5978425e3d5STejun Heo  * @cpu: cpu to use
5988425e3d5STejun Heo  * @dwork: job to be done
5998425e3d5STejun Heo  * @delay: number of jiffies to wait
6008425e3d5STejun Heo  *
6018425e3d5STejun Heo  * After waiting for a given time this puts a job in the kernel-global
6028425e3d5STejun Heo  * workqueue on the specified CPU.
6038425e3d5STejun Heo  */
6048425e3d5STejun Heo static inline bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
6058425e3d5STejun Heo 					    unsigned long delay)
6068425e3d5STejun Heo {
6078425e3d5STejun Heo 	return queue_delayed_work_on(cpu, system_wq, dwork, delay);
6088425e3d5STejun Heo }
6098425e3d5STejun Heo 
6108425e3d5STejun Heo /**
6118425e3d5STejun Heo  * schedule_delayed_work - put work task in global workqueue after delay
6128425e3d5STejun Heo  * @dwork: job to be done
6138425e3d5STejun Heo  * @delay: number of jiffies to wait or 0 for immediate execution
6148425e3d5STejun Heo  *
6158425e3d5STejun Heo  * After waiting for a given time this puts a job in the kernel-global
6168425e3d5STejun Heo  * workqueue.
6178425e3d5STejun Heo  */
6188425e3d5STejun Heo static inline bool schedule_delayed_work(struct delayed_work *dwork,
6198425e3d5STejun Heo 					 unsigned long delay)
6208425e3d5STejun Heo {
6218425e3d5STejun Heo 	return queue_delayed_work(system_wq, dwork, delay);
6228425e3d5STejun Heo }
6238425e3d5STejun Heo 
6242d3854a3SRusty Russell #ifndef CONFIG_SMP
625d84ff051STejun Heo static inline long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
6262d3854a3SRusty Russell {
6272d3854a3SRusty Russell 	return fn(arg);
6282d3854a3SRusty Russell }
6290e8d6a93SThomas Gleixner static inline long work_on_cpu_safe(int cpu, long (*fn)(void *), void *arg)
6300e8d6a93SThomas Gleixner {
6310e8d6a93SThomas Gleixner 	return fn(arg);
6320e8d6a93SThomas Gleixner }
6332d3854a3SRusty Russell #else
634d84ff051STejun Heo long work_on_cpu(int cpu, long (*fn)(void *), void *arg);
6350e8d6a93SThomas Gleixner long work_on_cpu_safe(int cpu, long (*fn)(void *), void *arg);
6362d3854a3SRusty Russell #endif /* CONFIG_SMP */
637a25909a4SPaul E. McKenney 
638a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER
639a0a1a5fdSTejun Heo extern void freeze_workqueues_begin(void);
640a0a1a5fdSTejun Heo extern bool freeze_workqueues_busy(void);
641a0a1a5fdSTejun Heo extern void thaw_workqueues(void);
642a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */
643a0a1a5fdSTejun Heo 
644226223abSTejun Heo #ifdef CONFIG_SYSFS
645226223abSTejun Heo int workqueue_sysfs_register(struct workqueue_struct *wq);
646226223abSTejun Heo #else	/* CONFIG_SYSFS */
647226223abSTejun Heo static inline int workqueue_sysfs_register(struct workqueue_struct *wq)
648226223abSTejun Heo { return 0; }
649226223abSTejun Heo #endif	/* CONFIG_SYSFS */
650226223abSTejun Heo 
65182607adcSTejun Heo #ifdef CONFIG_WQ_WATCHDOG
65282607adcSTejun Heo void wq_watchdog_touch(int cpu);
65382607adcSTejun Heo #else	/* CONFIG_WQ_WATCHDOG */
65482607adcSTejun Heo static inline void wq_watchdog_touch(int cpu) { }
65582607adcSTejun Heo #endif	/* CONFIG_WQ_WATCHDOG */
65682607adcSTejun Heo 
6577ee681b2SThomas Gleixner #ifdef CONFIG_SMP
6587ee681b2SThomas Gleixner int workqueue_prepare_cpu(unsigned int cpu);
6597ee681b2SThomas Gleixner int workqueue_online_cpu(unsigned int cpu);
6607ee681b2SThomas Gleixner int workqueue_offline_cpu(unsigned int cpu);
6617ee681b2SThomas Gleixner #endif
6627ee681b2SThomas Gleixner 
6632333e829SYu Chen void __init workqueue_init_early(void);
6642333e829SYu Chen void __init workqueue_init(void);
6653347fa09STejun Heo 
6661da177e4SLinus Torvalds #endif
667