xref: /openbmc/linux/include/linux/workqueue.h (revision 6741dd3f)
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 
54bdb0a654SLai 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.
61bdb0a654SLai 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,
71bbb68dfaSTejun Heo 
72715b06b8STejun Heo 	/*
73715b06b8STejun Heo 	 * When a work item is off queue, its high bits point to the last
747c3eed5cSTejun Heo 	 * pool it was on.  Cap at 31 bits and use the highest number to
757c3eed5cSTejun Heo 	 * indicate that no pool is associated.
76715b06b8STejun Heo 	 */
77bbb68dfaSTejun Heo 	WORK_OFFQ_FLAG_BITS	= 1,
787c3eed5cSTejun Heo 	WORK_OFFQ_POOL_SHIFT	= WORK_OFFQ_FLAG_BASE + WORK_OFFQ_FLAG_BITS,
797c3eed5cSTejun Heo 	WORK_OFFQ_LEFT		= BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT,
807c3eed5cSTejun Heo 	WORK_OFFQ_POOL_BITS	= WORK_OFFQ_LEFT <= 31 ? WORK_OFFQ_LEFT : 31,
81dcd989cbSTejun Heo 
82dcd989cbSTejun Heo 	/* bit mask for work_busy() return values */
83dcd989cbSTejun Heo 	WORK_BUSY_PENDING	= 1 << 0,
84dcd989cbSTejun Heo 	WORK_BUSY_RUNNING	= 1 << 1,
853d1cb205STejun Heo 
863d1cb205STejun Heo 	/* maximum string length for set_worker_desc() */
873d1cb205STejun Heo 	WORKER_DESC_LEN		= 24,
8822df02bbSTejun Heo };
8922df02bbSTejun Heo 
90afa4bb77SLinus Torvalds /* Convenience constants - of type 'unsigned long', not 'enum'! */
91afa4bb77SLinus Torvalds #define WORK_OFFQ_CANCELING	(1ul << __WORK_OFFQ_CANCELING)
92afa4bb77SLinus Torvalds #define WORK_OFFQ_POOL_NONE	((1ul << WORK_OFFQ_POOL_BITS) - 1)
93afa4bb77SLinus Torvalds #define WORK_STRUCT_NO_POOL	(WORK_OFFQ_POOL_NONE << WORK_OFFQ_POOL_SHIFT)
94afa4bb77SLinus Torvalds 
95afa4bb77SLinus Torvalds #define WORK_STRUCT_FLAG_MASK    ((1ul << WORK_STRUCT_FLAG_BITS) - 1)
96afa4bb77SLinus Torvalds #define WORK_STRUCT_WQ_DATA_MASK (~WORK_STRUCT_FLAG_MASK)
97afa4bb77SLinus Torvalds 
981da177e4SLinus Torvalds struct work_struct {
99a08727baSLinus Torvalds 	atomic_long_t data;
1001da177e4SLinus Torvalds 	struct list_head entry;
1016bb49e59SDavid Howells 	work_func_t func;
1024e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
1034e6045f1SJohannes Berg 	struct lockdep_map lockdep_map;
1044e6045f1SJohannes Berg #endif
10552bad64dSDavid Howells };
10652bad64dSDavid Howells 
107a45463cbSArnd Bergmann #define WORK_DATA_INIT()	ATOMIC_LONG_INIT((unsigned long)WORK_STRUCT_NO_POOL)
1087a22ad75STejun Heo #define WORK_DATA_STATIC_INIT()	\
109a45463cbSArnd Bergmann 	ATOMIC_LONG_INIT((unsigned long)(WORK_STRUCT_NO_POOL | WORK_STRUCT_STATIC))
110a08727baSLinus Torvalds 
11152bad64dSDavid Howells struct delayed_work {
11252bad64dSDavid Howells 	struct work_struct work;
1131da177e4SLinus Torvalds 	struct timer_list timer;
11460c057bcSLai Jiangshan 
11560c057bcSLai Jiangshan 	/* target workqueue and CPU ->timer uses to queue ->work */
11660c057bcSLai Jiangshan 	struct workqueue_struct *wq;
1171265057fSTejun Heo 	int cpu;
1181da177e4SLinus Torvalds };
1191da177e4SLinus Torvalds 
12005f0fe6bSTejun Heo struct rcu_work {
12105f0fe6bSTejun Heo 	struct work_struct work;
12205f0fe6bSTejun Heo 	struct rcu_head rcu;
12305f0fe6bSTejun Heo 
12405f0fe6bSTejun Heo 	/* target workqueue ->rcu uses to queue ->work */
12505f0fe6bSTejun Heo 	struct workqueue_struct *wq;
12605f0fe6bSTejun Heo };
12705f0fe6bSTejun Heo 
12884193c07STejun Heo enum wq_affn_scope {
129523a301eSTejun Heo 	WQ_AFFN_DFL,			/* use system default */
13063c5484eSTejun Heo 	WQ_AFFN_CPU,			/* one pod per CPU */
13163c5484eSTejun Heo 	WQ_AFFN_SMT,			/* one pod poer SMT */
13263c5484eSTejun Heo 	WQ_AFFN_CACHE,			/* one pod per LLC */
13384193c07STejun Heo 	WQ_AFFN_NUMA,			/* one pod per NUMA node */
13484193c07STejun Heo 	WQ_AFFN_SYSTEM,			/* one pod across the whole system */
13584193c07STejun Heo 
13684193c07STejun Heo 	WQ_AFFN_NR_TYPES,
13784193c07STejun Heo };
13884193c07STejun Heo 
13942412c3aSSilvio Fricke /**
14042412c3aSSilvio Fricke  * struct workqueue_attrs - A struct for workqueue attributes.
141d55262c4STejun Heo  *
14242412c3aSSilvio Fricke  * This can be used to change attributes of an unbound workqueue.
1437a4e344cSTejun Heo  */
1447a4e344cSTejun Heo struct workqueue_attrs {
14542412c3aSSilvio Fricke 	/**
14642412c3aSSilvio Fricke 	 * @nice: nice level
14742412c3aSSilvio Fricke 	 */
14842412c3aSSilvio Fricke 	int nice;
14942412c3aSSilvio Fricke 
15042412c3aSSilvio Fricke 	/**
15142412c3aSSilvio Fricke 	 * @cpumask: allowed CPUs
1529546b29eSTejun Heo 	 *
1539546b29eSTejun Heo 	 * Work items in this workqueue are affine to these CPUs and not allowed
1549546b29eSTejun Heo 	 * to execute on other CPUs. A pool serving a workqueue must have the
1559546b29eSTejun Heo 	 * same @cpumask.
15642412c3aSSilvio Fricke 	 */
15742412c3aSSilvio Fricke 	cpumask_var_t cpumask;
15842412c3aSSilvio Fricke 
1599546b29eSTejun Heo 	/**
1609546b29eSTejun Heo 	 * @__pod_cpumask: internal attribute used to create per-pod pools
1619546b29eSTejun Heo 	 *
1629546b29eSTejun Heo 	 * Internal use only.
1639546b29eSTejun Heo 	 *
1649546b29eSTejun Heo 	 * Per-pod unbound worker pools are used to improve locality. Always a
1659546b29eSTejun Heo 	 * subset of ->cpumask. A workqueue can be associated with multiple
1669546b29eSTejun Heo 	 * worker pools with disjoint @__pod_cpumask's. Whether the enforcement
1679546b29eSTejun Heo 	 * of a pool's @__pod_cpumask is strict depends on @affn_strict.
1689546b29eSTejun Heo 	 */
1699546b29eSTejun Heo 	cpumask_var_t __pod_cpumask;
1709546b29eSTejun Heo 
1718639ecebSTejun Heo 	/**
1728639ecebSTejun Heo 	 * @affn_strict: affinity scope is strict
1738639ecebSTejun Heo 	 *
1748639ecebSTejun Heo 	 * If clear, workqueue will make a best-effort attempt at starting the
1758639ecebSTejun Heo 	 * worker inside @__pod_cpumask but the scheduler is free to migrate it
1768639ecebSTejun Heo 	 * outside.
1778639ecebSTejun Heo 	 *
1788639ecebSTejun Heo 	 * If set, workers are only allowed to run inside @__pod_cpumask.
1798639ecebSTejun Heo 	 */
1808639ecebSTejun Heo 	bool affn_strict;
1818639ecebSTejun Heo 
18284193c07STejun Heo 	/*
18384193c07STejun Heo 	 * Below fields aren't properties of a worker_pool. They only modify how
18484193c07STejun Heo 	 * :c:func:`apply_workqueue_attrs` select pools and thus don't
18584193c07STejun Heo 	 * participate in pool hash calculations or equality comparisons.
18684193c07STejun Heo 	 */
18784193c07STejun Heo 
18884193c07STejun Heo 	/**
18984193c07STejun Heo 	 * @affn_scope: unbound CPU affinity scope
19084193c07STejun Heo 	 *
19184193c07STejun Heo 	 * CPU pods are used to improve execution locality of unbound work
19284193c07STejun Heo 	 * items. There are multiple pod types, one for each wq_affn_scope, and
19384193c07STejun Heo 	 * every CPU in the system belongs to one pod in every pod type. CPUs
19484193c07STejun Heo 	 * that belong to the same pod share the worker pool. For example,
19584193c07STejun Heo 	 * selecting %WQ_AFFN_NUMA makes the workqueue use a separate worker
19684193c07STejun Heo 	 * pool for each NUMA node.
19784193c07STejun Heo 	 */
19884193c07STejun Heo 	enum wq_affn_scope affn_scope;
19984193c07STejun Heo 
20042412c3aSSilvio Fricke 	/**
201af73f5c9STejun Heo 	 * @ordered: work items must be executed one by one in queueing order
20242412c3aSSilvio Fricke 	 */
203af73f5c9STejun Heo 	bool ordered;
2047a4e344cSTejun Heo };
2057a4e344cSTejun Heo 
to_delayed_work(struct work_struct * work)206bf6aede7SJean Delvare static inline struct delayed_work *to_delayed_work(struct work_struct *work)
207bf6aede7SJean Delvare {
208bf6aede7SJean Delvare 	return container_of(work, struct delayed_work, work);
209bf6aede7SJean Delvare }
210bf6aede7SJean Delvare 
to_rcu_work(struct work_struct * work)21105f0fe6bSTejun Heo static inline struct rcu_work *to_rcu_work(struct work_struct *work)
21205f0fe6bSTejun Heo {
21305f0fe6bSTejun Heo 	return container_of(work, struct rcu_work, work);
21405f0fe6bSTejun Heo }
21505f0fe6bSTejun Heo 
2161fa44ecaSJames Bottomley struct execute_work {
2171fa44ecaSJames Bottomley 	struct work_struct work;
2181fa44ecaSJames Bottomley };
2191fa44ecaSJames Bottomley 
2204e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
2214e6045f1SJohannes Berg /*
2224e6045f1SJohannes Berg  * NB: because we have to copy the lockdep_map, setting _key
2234e6045f1SJohannes Berg  * here is required, otherwise it could get initialised to the
2244e6045f1SJohannes Berg  * copy of the lockdep_map!
2254e6045f1SJohannes Berg  */
2264e6045f1SJohannes Berg #define __WORK_INIT_LOCKDEP_MAP(n, k) \
2274e6045f1SJohannes Berg 	.lockdep_map = STATIC_LOCKDEP_MAP_INIT(n, k),
2284e6045f1SJohannes Berg #else
2294e6045f1SJohannes Berg #define __WORK_INIT_LOCKDEP_MAP(n, k)
2304e6045f1SJohannes Berg #endif
2314e6045f1SJohannes Berg 
23265f27f38SDavid Howells #define __WORK_INITIALIZER(n, f) {					\
233dc186ad7SThomas Gleixner 	.data = WORK_DATA_STATIC_INIT(),				\
23465f27f38SDavid Howells 	.entry	= { &(n).entry, &(n).entry },				\
23565f27f38SDavid Howells 	.func = (f),							\
2364e6045f1SJohannes Berg 	__WORK_INIT_LOCKDEP_MAP(#n, &(n))				\
23765f27f38SDavid Howells 	}
23865f27f38SDavid Howells 
239f991b318STejun Heo #define __DELAYED_WORK_INITIALIZER(n, f, tflags) {			\
24065f27f38SDavid Howells 	.work = __WORK_INITIALIZER((n).work, (f)),			\
241841b86f3SKees Cook 	.timer = __TIMER_INITIALIZER(delayed_work_timer_fn,\
242e0aecdd8STejun Heo 				     (tflags) | TIMER_IRQSAFE),		\
243dd6414b5SPhil Carmody 	}
244dd6414b5SPhil Carmody 
24565f27f38SDavid Howells #define DECLARE_WORK(n, f)						\
24665f27f38SDavid Howells 	struct work_struct n = __WORK_INITIALIZER(n, f)
24765f27f38SDavid Howells 
24865f27f38SDavid Howells #define DECLARE_DELAYED_WORK(n, f)					\
249f991b318STejun Heo 	struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f, 0)
25065f27f38SDavid Howells 
251203b42f7STejun Heo #define DECLARE_DEFERRABLE_WORK(n, f)					\
252f991b318STejun Heo 	struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f, TIMER_DEFERRABLE)
253dd6414b5SPhil Carmody 
254dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK
255dc186ad7SThomas Gleixner extern void __init_work(struct work_struct *work, int onstack);
256dc186ad7SThomas Gleixner extern void destroy_work_on_stack(struct work_struct *work);
257ea2e64f2SThomas Gleixner extern void destroy_delayed_work_on_stack(struct delayed_work *work);
work_static(struct work_struct * work)2584690c4abSTejun Heo static inline unsigned int work_static(struct work_struct *work)
2594690c4abSTejun Heo {
26022df02bbSTejun Heo 	return *work_data_bits(work) & WORK_STRUCT_STATIC;
2614690c4abSTejun Heo }
262dc186ad7SThomas Gleixner #else
__init_work(struct work_struct * work,int onstack)263dc186ad7SThomas Gleixner static inline void __init_work(struct work_struct *work, int onstack) { }
destroy_work_on_stack(struct work_struct * work)264dc186ad7SThomas Gleixner static inline void destroy_work_on_stack(struct work_struct *work) { }
destroy_delayed_work_on_stack(struct delayed_work * work)265ea2e64f2SThomas Gleixner static inline void destroy_delayed_work_on_stack(struct delayed_work *work) { }
work_static(struct work_struct * work)2664690c4abSTejun Heo static inline unsigned int work_static(struct work_struct *work) { return 0; }
267dc186ad7SThomas Gleixner #endif
268dc186ad7SThomas Gleixner 
2691da177e4SLinus Torvalds /*
27052bad64dSDavid Howells  * initialize all of a work item in one go
271a08727baSLinus Torvalds  *
272b9049df5SDmitri Vorobiev  * NOTE! No point in using "atomic_long_set()": using a direct
273a08727baSLinus Torvalds  * assignment of the work data initializer allows the compiler
274a08727baSLinus Torvalds  * to generate better code.
2751da177e4SLinus Torvalds  */
2764e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
277be2355b7SFrederic Weisbecker #define __INIT_WORK_KEY(_work, _func, _onstack, _key)			\
2784e6045f1SJohannes Berg 	do {								\
279dc186ad7SThomas Gleixner 		__init_work((_work), _onstack);				\
2804e6045f1SJohannes Berg 		(_work)->data = (atomic_long_t) WORK_DATA_INIT();	\
281be2355b7SFrederic Weisbecker 		lockdep_init_map(&(_work)->lockdep_map, "(work_completion)"#_work, (_key), 0); \
2824e6045f1SJohannes Berg 		INIT_LIST_HEAD(&(_work)->entry);			\
283f073f922STejun Heo 		(_work)->func = (_func);				\
2844e6045f1SJohannes Berg 	} while (0)
2854e6045f1SJohannes Berg #else
286be2355b7SFrederic Weisbecker #define __INIT_WORK_KEY(_work, _func, _onstack, _key)			\
2871da177e4SLinus Torvalds 	do {								\
288dc186ad7SThomas Gleixner 		__init_work((_work), _onstack);				\
28923b2e599SOleg Nesterov 		(_work)->data = (atomic_long_t) WORK_DATA_INIT();	\
29065f27f38SDavid Howells 		INIT_LIST_HEAD(&(_work)->entry);			\
291f073f922STejun Heo 		(_work)->func = (_func);				\
29265f27f38SDavid Howells 	} while (0)
2934e6045f1SJohannes Berg #endif
29465f27f38SDavid Howells 
295be2355b7SFrederic Weisbecker #define __INIT_WORK(_work, _func, _onstack)				\
296be2355b7SFrederic Weisbecker 	do {								\
297be2355b7SFrederic Weisbecker 		static __maybe_unused struct lock_class_key __key;	\
298be2355b7SFrederic Weisbecker 									\
299be2355b7SFrederic Weisbecker 		__INIT_WORK_KEY(_work, _func, _onstack, &__key);	\
300be2355b7SFrederic Weisbecker 	} while (0)
301be2355b7SFrederic Weisbecker 
302dc186ad7SThomas Gleixner #define INIT_WORK(_work, _func)						\
3039da7dae9SValentin Rothberg 	__INIT_WORK((_work), (_func), 0)
304dc186ad7SThomas Gleixner 
305ca1cab37SAndrew Morton #define INIT_WORK_ONSTACK(_work, _func)					\
3069da7dae9SValentin Rothberg 	__INIT_WORK((_work), (_func), 1)
307dc186ad7SThomas Gleixner 
308be2355b7SFrederic Weisbecker #define INIT_WORK_ONSTACK_KEY(_work, _func, _key)			\
309be2355b7SFrederic Weisbecker 	__INIT_WORK_KEY((_work), (_func), 1, _key)
310be2355b7SFrederic Weisbecker 
311f991b318STejun Heo #define __INIT_DELAYED_WORK(_work, _func, _tflags)			\
31265f27f38SDavid Howells 	do {								\
31365f27f38SDavid Howells 		INIT_WORK(&(_work)->work, (_func));			\
314919b250fSKees Cook 		__init_timer(&(_work)->timer,				\
315919b250fSKees Cook 			     delayed_work_timer_fn,			\
316e0aecdd8STejun Heo 			     (_tflags) | TIMER_IRQSAFE);		\
31765f27f38SDavid Howells 	} while (0)
31865f27f38SDavid Howells 
319f991b318STejun Heo #define __INIT_DELAYED_WORK_ONSTACK(_work, _func, _tflags)		\
3206d612b0fSPeter Zijlstra 	do {								\
321ca1cab37SAndrew Morton 		INIT_WORK_ONSTACK(&(_work)->work, (_func));		\
322919b250fSKees Cook 		__init_timer_on_stack(&(_work)->timer,			\
323919b250fSKees Cook 				      delayed_work_timer_fn,		\
324e0aecdd8STejun Heo 				      (_tflags) | TIMER_IRQSAFE);	\
3256d612b0fSPeter Zijlstra 	} while (0)
3266d612b0fSPeter Zijlstra 
327f991b318STejun Heo #define INIT_DELAYED_WORK(_work, _func)					\
328f991b318STejun Heo 	__INIT_DELAYED_WORK(_work, _func, 0)
329f991b318STejun Heo 
330f991b318STejun Heo #define INIT_DELAYED_WORK_ONSTACK(_work, _func)				\
331f991b318STejun Heo 	__INIT_DELAYED_WORK_ONSTACK(_work, _func, 0)
332f991b318STejun Heo 
333203b42f7STejun Heo #define INIT_DEFERRABLE_WORK(_work, _func)				\
334f991b318STejun Heo 	__INIT_DELAYED_WORK(_work, _func, TIMER_DEFERRABLE)
335f991b318STejun Heo 
336f991b318STejun Heo #define INIT_DEFERRABLE_WORK_ONSTACK(_work, _func)			\
337f991b318STejun Heo 	__INIT_DELAYED_WORK_ONSTACK(_work, _func, TIMER_DEFERRABLE)
33828287033SVenki Pallipadi 
33905f0fe6bSTejun Heo #define INIT_RCU_WORK(_work, _func)					\
34005f0fe6bSTejun Heo 	INIT_WORK(&(_work)->work, (_func))
34105f0fe6bSTejun Heo 
34205f0fe6bSTejun Heo #define INIT_RCU_WORK_ONSTACK(_work, _func)				\
34305f0fe6bSTejun Heo 	INIT_WORK_ONSTACK(&(_work)->work, (_func))
34405f0fe6bSTejun Heo 
345365970a1SDavid Howells /**
346365970a1SDavid Howells  * work_pending - Find out whether a work item is currently pending
347365970a1SDavid Howells  * @work: The work item in question
348365970a1SDavid Howells  */
349365970a1SDavid Howells #define work_pending(work) \
35022df02bbSTejun Heo 	test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))
351365970a1SDavid Howells 
352365970a1SDavid Howells /**
353365970a1SDavid Howells  * delayed_work_pending - Find out whether a delayable work item is currently
354365970a1SDavid Howells  * pending
355355c0663SJonathan Corbet  * @w: The work item in question
356365970a1SDavid Howells  */
3570221872aSLinus Torvalds #define delayed_work_pending(w) \
3580221872aSLinus Torvalds 	work_pending(&(w)->work)
359365970a1SDavid Howells 
360c54fce6eSTejun Heo /*
361c54fce6eSTejun Heo  * Workqueue flags and constants.  For details, please refer to
36242412c3aSSilvio Fricke  * Documentation/core-api/workqueue.rst.
363c54fce6eSTejun Heo  */
36497e37d7bSTejun Heo enum {
365c7fc77f7STejun Heo 	WQ_UNBOUND		= 1 << 1, /* not bound to any cpu */
36658a69cb4STejun Heo 	WQ_FREEZABLE		= 1 << 2, /* freeze during suspend */
3676370a6adSTejun Heo 	WQ_MEM_RECLAIM		= 1 << 3, /* may be used for memory reclaim */
368649027d7STejun Heo 	WQ_HIGHPRI		= 1 << 4, /* high priority */
36941f50094SGeert Uytterhoeven 	WQ_CPU_INTENSIVE	= 1 << 5, /* cpu intensive workqueue */
37093e86295SMenglong Dong 	WQ_SYSFS		= 1 << 6, /* visible in sysfs, see workqueue_sysfs_register() */
371b71ab8c2STejun Heo 
372cee22a15SViresh Kumar 	/*
373cee22a15SViresh Kumar 	 * Per-cpu workqueues are generally preferred because they tend to
374cee22a15SViresh Kumar 	 * show better performance thanks to cache locality.  Per-cpu
375cee22a15SViresh Kumar 	 * workqueues exclude the scheduler from choosing the CPU to
376cee22a15SViresh Kumar 	 * execute the worker threads, which has an unfortunate side effect
377cee22a15SViresh Kumar 	 * of increasing power consumption.
378cee22a15SViresh Kumar 	 *
379cee22a15SViresh Kumar 	 * The scheduler considers a CPU idle if it doesn't have any task
380cee22a15SViresh Kumar 	 * to execute and tries to keep idle cores idle to conserve power;
381cee22a15SViresh Kumar 	 * however, for example, a per-cpu work item scheduled from an
382cee22a15SViresh Kumar 	 * interrupt handler on an idle CPU will force the scheduler to
38367dc8325SCai Huoqing 	 * execute the work item on that CPU breaking the idleness, which in
384cee22a15SViresh Kumar 	 * turn may lead to more scheduling choices which are sub-optimal
385cee22a15SViresh Kumar 	 * in terms of power consumption.
386cee22a15SViresh Kumar 	 *
387cee22a15SViresh Kumar 	 * Workqueues marked with WQ_POWER_EFFICIENT are per-cpu by default
388cee22a15SViresh Kumar 	 * but become unbound if workqueue.power_efficient kernel param is
389cee22a15SViresh Kumar 	 * specified.  Per-cpu workqueues which are identified to
390cee22a15SViresh Kumar 	 * contribute significantly to power-consumption are identified and
391cee22a15SViresh Kumar 	 * marked with this flag and enabling the power_efficient mode
392cee22a15SViresh Kumar 	 * leads to noticeable power saving at the cost of small
393cee22a15SViresh Kumar 	 * performance disadvantage.
394cee22a15SViresh Kumar 	 *
395cee22a15SViresh Kumar 	 * http://thread.gmane.org/gmane.linux.kernel/1480396
396cee22a15SViresh Kumar 	 */
397cee22a15SViresh Kumar 	WQ_POWER_EFFICIENT	= 1 << 7,
398cee22a15SViresh Kumar 
39933e3f0a3SRichard Clark 	__WQ_DESTROYING		= 1 << 15, /* internal: workqueue is destroying */
400618b01ebSTejun Heo 	__WQ_DRAINING		= 1 << 16, /* internal: workqueue is draining */
4018719dceaSTejun Heo 	__WQ_ORDERED		= 1 << 17, /* internal: workqueue is ordered */
40223d11a58STejun Heo 	__WQ_LEGACY		= 1 << 18, /* internal: create*_workqueue() */
403fbf1c41fSBen Hutchings 	__WQ_ORDERED_EXPLICIT	= 1 << 19, /* internal: alloc_ordered_workqueue() */
404e41e704bSTejun Heo 
405b71ab8c2STejun Heo 	WQ_MAX_ACTIVE		= 512,	  /* I like 512, better ideas? */
406636b927eSTejun Heo 	WQ_UNBOUND_MAX_ACTIVE	= WQ_MAX_ACTIVE,
407b71ab8c2STejun Heo 	WQ_DFL_ACTIVE		= WQ_MAX_ACTIVE / 2,
40897e37d7bSTejun Heo };
40952bad64dSDavid Howells 
410d320c038STejun Heo /*
411d320c038STejun Heo  * System-wide workqueues which are always present.
412d320c038STejun Heo  *
413d320c038STejun Heo  * system_wq is the one used by schedule[_delayed]_work[_on]().
414d320c038STejun Heo  * Multi-CPU multi-threaded.  There are users which expect relatively
415d320c038STejun Heo  * short queue flush time.  Don't queue works which can run for too
416d320c038STejun Heo  * long.
417d320c038STejun Heo  *
41873e43544SLai Jiangshan  * system_highpri_wq is similar to system_wq but for work items which
41973e43544SLai Jiangshan  * require WQ_HIGHPRI.
42073e43544SLai Jiangshan  *
421d320c038STejun Heo  * system_long_wq is similar to system_wq but may host long running
422d320c038STejun Heo  * works.  Queue flushing might take relatively long.
423d320c038STejun Heo  *
424f3421797STejun Heo  * system_unbound_wq is unbound workqueue.  Workers are not bound to
425f3421797STejun Heo  * any specific CPU, not concurrency managed, and all queued works are
426f3421797STejun Heo  * executed immediately as long as max_active limit is not reached and
427f3421797STejun Heo  * resources are available.
4284149efb2STejun Heo  *
42924d51addSTejun Heo  * system_freezable_wq is equivalent to system_wq except that it's
43024d51addSTejun Heo  * freezable.
4310668106cSViresh Kumar  *
4320668106cSViresh Kumar  * *_power_efficient_wq are inclined towards saving power and converted
4330668106cSViresh Kumar  * into WQ_UNBOUND variants if 'wq_power_efficient' is enabled; otherwise,
4340668106cSViresh Kumar  * they are same as their non-power-efficient counterparts - e.g.
4350668106cSViresh Kumar  * system_power_efficient_wq is identical to system_wq if
4360668106cSViresh Kumar  * 'wq_power_efficient' is disabled.  See WQ_POWER_EFFICIENT for more info.
437d320c038STejun Heo  */
438d320c038STejun Heo extern struct workqueue_struct *system_wq;
43973e43544SLai Jiangshan extern struct workqueue_struct *system_highpri_wq;
440d320c038STejun Heo extern struct workqueue_struct *system_long_wq;
441f3421797STejun Heo extern struct workqueue_struct *system_unbound_wq;
44224d51addSTejun Heo extern struct workqueue_struct *system_freezable_wq;
4430668106cSViresh Kumar extern struct workqueue_struct *system_power_efficient_wq;
4440668106cSViresh Kumar extern struct workqueue_struct *system_freezable_power_efficient_wq;
445ae930e0fSTejun Heo 
446b196be89STejun Heo /**
447b196be89STejun Heo  * alloc_workqueue - allocate a workqueue
448b196be89STejun Heo  * @fmt: printf format for the name of the workqueue
449b196be89STejun Heo  * @flags: WQ_* flags
4506741dd3fSGreg Kroah-Hartman  * @max_active: max in-flight work items per CPU, 0 for default
451669de8bdSBart Van Assche  * remaining args: args for @fmt
452b196be89STejun Heo  *
4536741dd3fSGreg Kroah-Hartman  * Allocate a workqueue with the specified parameters.  For detailed
4546741dd3fSGreg Kroah-Hartman  * information on WQ_* flags, please refer to
45542412c3aSSilvio Fricke  * Documentation/core-api/workqueue.rst.
456b196be89STejun Heo  *
457b196be89STejun Heo  * RETURNS:
458b196be89STejun Heo  * Pointer to the allocated workqueue on success, %NULL on failure.
459b196be89STejun Heo  */
46080f0a1f9SRolf Eike Beer __printf(1, 4) struct workqueue_struct *
46180f0a1f9SRolf Eike Beer alloc_workqueue(const char *fmt, unsigned int flags, int max_active, ...);
4624e6045f1SJohannes Berg 
46381dcaf65STejun Heo /**
46481dcaf65STejun Heo  * alloc_ordered_workqueue - allocate an ordered workqueue
465b196be89STejun Heo  * @fmt: printf format for the name of the workqueue
46658a69cb4STejun Heo  * @flags: WQ_* flags (only WQ_FREEZABLE and WQ_MEM_RECLAIM are meaningful)
4678bee9dd9SJonathan Neuschäfer  * @args: args for @fmt
46881dcaf65STejun Heo  *
46981dcaf65STejun Heo  * Allocate an ordered workqueue.  An ordered workqueue executes at
47081dcaf65STejun Heo  * most one work item at any given time in the queued order.  They are
47181dcaf65STejun Heo  * implemented as unbound workqueues with @max_active of one.
47281dcaf65STejun Heo  *
47381dcaf65STejun Heo  * RETURNS:
47481dcaf65STejun Heo  * Pointer to the allocated workqueue on success, %NULL on failure.
47581dcaf65STejun Heo  */
476b196be89STejun Heo #define alloc_ordered_workqueue(fmt, flags, args...)			\
4770a94efb5STejun Heo 	alloc_workqueue(fmt, WQ_UNBOUND | __WQ_ORDERED |		\
4780a94efb5STejun Heo 			__WQ_ORDERED_EXPLICIT | (flags), 1, ##args)
47981dcaf65STejun Heo 
48097e37d7bSTejun Heo #define create_workqueue(name)						\
48123d11a58STejun Heo 	alloc_workqueue("%s", __WQ_LEGACY | WQ_MEM_RECLAIM, 1, (name))
48258a69cb4STejun Heo #define create_freezable_workqueue(name)				\
48323d11a58STejun Heo 	alloc_workqueue("%s", __WQ_LEGACY | WQ_FREEZABLE | WQ_UNBOUND |	\
48423d11a58STejun Heo 			WQ_MEM_RECLAIM, 1, (name))
48597e37d7bSTejun Heo #define create_singlethread_workqueue(name)				\
48623d11a58STejun Heo 	alloc_ordered_workqueue("%s", __WQ_LEGACY | WQ_MEM_RECLAIM, name)
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds extern void destroy_workqueue(struct workqueue_struct *wq);
4891da177e4SLinus Torvalds 
490513c98d0SDaniel Jordan struct workqueue_attrs *alloc_workqueue_attrs(void);
491513c98d0SDaniel Jordan void free_workqueue_attrs(struct workqueue_attrs *attrs);
492513c98d0SDaniel Jordan int apply_workqueue_attrs(struct workqueue_struct *wq,
493513c98d0SDaniel Jordan 			  const struct workqueue_attrs *attrs);
494042f7df1SLai Jiangshan int workqueue_set_unbound_cpumask(cpumask_var_t cpumask);
4957a4e344cSTejun Heo 
496d4283e93STejun Heo extern bool queue_work_on(int cpu, struct workqueue_struct *wq,
497c1a220e7SZhang Rui 			struct work_struct *work);
4988204e0c1SAlexander Duyck extern bool queue_work_node(int node, struct workqueue_struct *wq,
4998204e0c1SAlexander Duyck 			    struct work_struct *work);
500d4283e93STejun Heo extern bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
50152bad64dSDavid Howells 			struct delayed_work *work, unsigned long delay);
5028376fe22STejun Heo extern bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
5038376fe22STejun Heo 			struct delayed_work *dwork, unsigned long delay);
50405f0fe6bSTejun Heo extern bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork);
50528e53bddSOleg Nesterov 
506c4f135d6STetsuo Handa extern void __flush_workqueue(struct workqueue_struct *wq);
5079c5a2ba7STejun Heo extern void drain_workqueue(struct workqueue_struct *wq);
5081da177e4SLinus Torvalds 
50965f27f38SDavid Howells extern int schedule_on_each_cpu(work_func_t func);
5101da177e4SLinus Torvalds 
51165f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *);
5121da177e4SLinus Torvalds 
513401a8d04STejun Heo extern bool flush_work(struct work_struct *work);
51473b4b532SAndrey Grodzovsky extern bool cancel_work(struct work_struct *work);
515401a8d04STejun Heo extern bool cancel_work_sync(struct work_struct *work);
516401a8d04STejun Heo 
517401a8d04STejun Heo extern bool flush_delayed_work(struct delayed_work *dwork);
51857b30ae7STejun Heo extern bool cancel_delayed_work(struct delayed_work *dwork);
519401a8d04STejun Heo extern bool cancel_delayed_work_sync(struct delayed_work *dwork);
52028e53bddSOleg Nesterov 
52105f0fe6bSTejun Heo extern bool flush_rcu_work(struct rcu_work *rwork);
52205f0fe6bSTejun Heo 
523dcd989cbSTejun Heo extern void workqueue_set_max_active(struct workqueue_struct *wq,
524dcd989cbSTejun Heo 				     int max_active);
52527d4ee03SLukas Wunner extern struct work_struct *current_work(void);
526e6267616STejun Heo extern bool current_is_workqueue_rescuer(void);
527d84ff051STejun Heo extern bool workqueue_congested(int cpu, struct workqueue_struct *wq);
528dcd989cbSTejun Heo extern unsigned int work_busy(struct work_struct *work);
5293d1cb205STejun Heo extern __printf(1, 2) void set_worker_desc(const char *fmt, ...);
5303d1cb205STejun Heo extern void print_worker_info(const char *log_lvl, struct task_struct *task);
53155df0933SImran Khan extern void show_all_workqueues(void);
532704bc669SJungseung Lee extern void show_freezable_workqueues(void);
53355df0933SImran Khan extern void show_one_workqueue(struct workqueue_struct *wq);
5346b59808bSTejun Heo extern void wq_worker_comm(char *buf, size_t size, struct task_struct *task);
535dcd989cbSTejun Heo 
5368425e3d5STejun Heo /**
5378425e3d5STejun Heo  * queue_work - queue work on a workqueue
5388425e3d5STejun Heo  * @wq: workqueue to use
5398425e3d5STejun Heo  * @work: work to queue
5408425e3d5STejun Heo  *
5418425e3d5STejun Heo  * Returns %false if @work was already on a queue, %true otherwise.
5428425e3d5STejun Heo  *
5438425e3d5STejun Heo  * We queue the work to the CPU on which it was submitted, but if the CPU dies
5448425e3d5STejun Heo  * it can be processed by another CPU.
545dbb92f88SAndrea Parri  *
546dbb92f88SAndrea Parri  * Memory-ordering properties:  If it returns %true, guarantees that all stores
547dbb92f88SAndrea Parri  * preceding the call to queue_work() in the program order will be visible from
548dbb92f88SAndrea Parri  * the CPU which will execute @work by the time such work executes, e.g.,
549dbb92f88SAndrea Parri  *
550dbb92f88SAndrea Parri  * { x is initially 0 }
551dbb92f88SAndrea Parri  *
552dbb92f88SAndrea Parri  *   CPU0				CPU1
553dbb92f88SAndrea Parri  *
554dbb92f88SAndrea Parri  *   WRITE_ONCE(x, 1);			[ @work is being executed ]
555dbb92f88SAndrea Parri  *   r0 = queue_work(wq, work);		  r1 = READ_ONCE(x);
556dbb92f88SAndrea Parri  *
557dbb92f88SAndrea Parri  * Forbids: r0 == true && r1 == 0
5588425e3d5STejun Heo  */
queue_work(struct workqueue_struct * wq,struct work_struct * work)5598425e3d5STejun Heo static inline bool queue_work(struct workqueue_struct *wq,
5608425e3d5STejun Heo 			      struct work_struct *work)
5618425e3d5STejun Heo {
5628425e3d5STejun Heo 	return queue_work_on(WORK_CPU_UNBOUND, wq, work);
5638425e3d5STejun Heo }
5648425e3d5STejun Heo 
5658425e3d5STejun Heo /**
5668425e3d5STejun Heo  * queue_delayed_work - queue work on a workqueue after delay
5678425e3d5STejun Heo  * @wq: workqueue to use
5688425e3d5STejun Heo  * @dwork: delayable work to queue
5698425e3d5STejun Heo  * @delay: number of jiffies to wait before queueing
5708425e3d5STejun Heo  *
5718425e3d5STejun Heo  * Equivalent to queue_delayed_work_on() but tries to use the local CPU.
5728425e3d5STejun Heo  */
queue_delayed_work(struct workqueue_struct * wq,struct delayed_work * dwork,unsigned long delay)5738425e3d5STejun Heo static inline bool queue_delayed_work(struct workqueue_struct *wq,
5748425e3d5STejun Heo 				      struct delayed_work *dwork,
5758425e3d5STejun Heo 				      unsigned long delay)
5768425e3d5STejun Heo {
5778425e3d5STejun Heo 	return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
5788425e3d5STejun Heo }
5798425e3d5STejun Heo 
5808425e3d5STejun Heo /**
5818425e3d5STejun Heo  * mod_delayed_work - modify delay of or queue a delayed work
5828425e3d5STejun Heo  * @wq: workqueue to use
5838425e3d5STejun Heo  * @dwork: work to queue
5848425e3d5STejun Heo  * @delay: number of jiffies to wait before queueing
5858425e3d5STejun Heo  *
5868425e3d5STejun Heo  * mod_delayed_work_on() on local CPU.
5878425e3d5STejun Heo  */
mod_delayed_work(struct workqueue_struct * wq,struct delayed_work * dwork,unsigned long delay)5888425e3d5STejun Heo static inline bool mod_delayed_work(struct workqueue_struct *wq,
5898425e3d5STejun Heo 				    struct delayed_work *dwork,
5908425e3d5STejun Heo 				    unsigned long delay)
5918425e3d5STejun Heo {
5928425e3d5STejun Heo 	return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
5938425e3d5STejun Heo }
5948425e3d5STejun Heo 
5958425e3d5STejun Heo /**
5968425e3d5STejun Heo  * schedule_work_on - put work task on a specific cpu
5978425e3d5STejun Heo  * @cpu: cpu to put the work task on
5988425e3d5STejun Heo  * @work: job to be done
5998425e3d5STejun Heo  *
6008425e3d5STejun Heo  * This puts a job on a specific cpu
6018425e3d5STejun Heo  */
schedule_work_on(int cpu,struct work_struct * work)6028425e3d5STejun Heo static inline bool schedule_work_on(int cpu, struct work_struct *work)
6038425e3d5STejun Heo {
6048425e3d5STejun Heo 	return queue_work_on(cpu, system_wq, work);
6058425e3d5STejun Heo }
6068425e3d5STejun Heo 
6078425e3d5STejun Heo /**
6088425e3d5STejun Heo  * schedule_work - put work task in global workqueue
6098425e3d5STejun Heo  * @work: job to be done
6108425e3d5STejun Heo  *
6118425e3d5STejun Heo  * Returns %false if @work was already on the kernel-global workqueue and
6128425e3d5STejun Heo  * %true otherwise.
6138425e3d5STejun Heo  *
6148425e3d5STejun Heo  * This puts a job in the kernel-global workqueue if it was not already
6158425e3d5STejun Heo  * queued and leaves it in the same position on the kernel-global
6168425e3d5STejun Heo  * workqueue otherwise.
617dbb92f88SAndrea Parri  *
618dbb92f88SAndrea Parri  * Shares the same memory-ordering properties of queue_work(), cf. the
619dbb92f88SAndrea Parri  * DocBook header of queue_work().
6208425e3d5STejun Heo  */
schedule_work(struct work_struct * work)6218425e3d5STejun Heo static inline bool schedule_work(struct work_struct *work)
6228425e3d5STejun Heo {
6238425e3d5STejun Heo 	return queue_work(system_wq, work);
6248425e3d5STejun Heo }
6258425e3d5STejun Heo 
626c4f135d6STetsuo Handa /*
627c4f135d6STetsuo Handa  * Detect attempt to flush system-wide workqueues at compile time when possible.
62820bdedafSTetsuo Handa  * Warn attempt to flush system-wide workqueues at runtime.
629c4f135d6STetsuo Handa  *
630c4f135d6STetsuo Handa  * See https://lkml.kernel.org/r/49925af7-78a8-a3dd-bce6-cfc02e1a9236@I-love.SAKURA.ne.jp
631c4f135d6STetsuo Handa  * for reasons and steps for converting system-wide workqueues into local workqueues.
632c4f135d6STetsuo Handa  */
633c4f135d6STetsuo Handa extern void __warn_flushing_systemwide_wq(void)
634c4f135d6STetsuo Handa 	__compiletime_warning("Please avoid flushing system-wide workqueues.");
635c4f135d6STetsuo Handa 
63620bdedafSTetsuo Handa /* Please stop using this function, for this function will be removed in near future. */
637c4f135d6STetsuo Handa #define flush_scheduled_work()						\
638c4f135d6STetsuo Handa ({									\
639c4f135d6STetsuo Handa 	__warn_flushing_systemwide_wq();				\
640c4f135d6STetsuo Handa 	__flush_workqueue(system_wq);					\
641c4f135d6STetsuo Handa })
642c4f135d6STetsuo Handa 
643c4f135d6STetsuo Handa #define flush_workqueue(wq)						\
644c4f135d6STetsuo Handa ({									\
645c4f135d6STetsuo Handa 	struct workqueue_struct *_wq = (wq);				\
646c4f135d6STetsuo Handa 									\
647c4f135d6STetsuo Handa 	if ((__builtin_constant_p(_wq == system_wq) &&			\
648c4f135d6STetsuo Handa 	     _wq == system_wq) ||					\
649c4f135d6STetsuo Handa 	    (__builtin_constant_p(_wq == system_highpri_wq) &&		\
650c4f135d6STetsuo Handa 	     _wq == system_highpri_wq) ||				\
651c4f135d6STetsuo Handa 	    (__builtin_constant_p(_wq == system_long_wq) &&		\
652c4f135d6STetsuo Handa 	     _wq == system_long_wq) ||					\
653c4f135d6STetsuo Handa 	    (__builtin_constant_p(_wq == system_unbound_wq) &&		\
654c4f135d6STetsuo Handa 	     _wq == system_unbound_wq) ||				\
655c4f135d6STetsuo Handa 	    (__builtin_constant_p(_wq == system_freezable_wq) &&	\
656c4f135d6STetsuo Handa 	     _wq == system_freezable_wq) ||				\
657c4f135d6STetsuo Handa 	    (__builtin_constant_p(_wq == system_power_efficient_wq) &&	\
658c4f135d6STetsuo Handa 	     _wq == system_power_efficient_wq) ||			\
659c4f135d6STetsuo Handa 	    (__builtin_constant_p(_wq == system_freezable_power_efficient_wq) && \
660c4f135d6STetsuo Handa 	     _wq == system_freezable_power_efficient_wq))		\
661c4f135d6STetsuo Handa 		__warn_flushing_systemwide_wq();			\
662c4f135d6STetsuo Handa 	__flush_workqueue(_wq);						\
663c4f135d6STetsuo Handa })
66437b1ef31SLai Jiangshan 
66537b1ef31SLai Jiangshan /**
6668425e3d5STejun Heo  * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
6678425e3d5STejun Heo  * @cpu: cpu to use
6688425e3d5STejun Heo  * @dwork: job to be done
6698425e3d5STejun Heo  * @delay: number of jiffies to wait
6708425e3d5STejun Heo  *
6718425e3d5STejun Heo  * After waiting for a given time this puts a job in the kernel-global
6728425e3d5STejun Heo  * workqueue on the specified CPU.
6738425e3d5STejun Heo  */
schedule_delayed_work_on(int cpu,struct delayed_work * dwork,unsigned long delay)6748425e3d5STejun Heo static inline bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
6758425e3d5STejun Heo 					    unsigned long delay)
6768425e3d5STejun Heo {
6778425e3d5STejun Heo 	return queue_delayed_work_on(cpu, system_wq, dwork, delay);
6788425e3d5STejun Heo }
6798425e3d5STejun Heo 
6808425e3d5STejun Heo /**
6818425e3d5STejun Heo  * schedule_delayed_work - put work task in global workqueue after delay
6828425e3d5STejun Heo  * @dwork: job to be done
6838425e3d5STejun Heo  * @delay: number of jiffies to wait or 0 for immediate execution
6848425e3d5STejun Heo  *
6858425e3d5STejun Heo  * After waiting for a given time this puts a job in the kernel-global
6868425e3d5STejun Heo  * workqueue.
6878425e3d5STejun Heo  */
schedule_delayed_work(struct delayed_work * dwork,unsigned long delay)6888425e3d5STejun Heo static inline bool schedule_delayed_work(struct delayed_work *dwork,
6898425e3d5STejun Heo 					 unsigned long delay)
6908425e3d5STejun Heo {
6918425e3d5STejun Heo 	return queue_delayed_work(system_wq, dwork, delay);
6928425e3d5STejun Heo }
6938425e3d5STejun Heo 
6942d3854a3SRusty Russell #ifndef CONFIG_SMP
work_on_cpu(int cpu,long (* fn)(void *),void * arg)695d84ff051STejun Heo static inline long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
6962d3854a3SRusty Russell {
6972d3854a3SRusty Russell 	return fn(arg);
6982d3854a3SRusty Russell }
work_on_cpu_safe(int cpu,long (* fn)(void *),void * arg)6990e8d6a93SThomas Gleixner static inline long work_on_cpu_safe(int cpu, long (*fn)(void *), void *arg)
7000e8d6a93SThomas Gleixner {
7010e8d6a93SThomas Gleixner 	return fn(arg);
7020e8d6a93SThomas Gleixner }
7032d3854a3SRusty Russell #else
704be2355b7SFrederic Weisbecker long work_on_cpu_key(int cpu, long (*fn)(void *),
705be2355b7SFrederic Weisbecker 		     void *arg, struct lock_class_key *key);
706be2355b7SFrederic Weisbecker /*
707be2355b7SFrederic Weisbecker  * A new key is defined for each caller to make sure the work
708be2355b7SFrederic Weisbecker  * associated with the function doesn't share its locking class.
709be2355b7SFrederic Weisbecker  */
710be2355b7SFrederic Weisbecker #define work_on_cpu(_cpu, _fn, _arg)			\
711be2355b7SFrederic Weisbecker ({							\
712be2355b7SFrederic Weisbecker 	static struct lock_class_key __key;		\
713be2355b7SFrederic Weisbecker 							\
714be2355b7SFrederic Weisbecker 	work_on_cpu_key(_cpu, _fn, _arg, &__key);	\
715be2355b7SFrederic Weisbecker })
716be2355b7SFrederic Weisbecker 
717be2355b7SFrederic Weisbecker long work_on_cpu_safe_key(int cpu, long (*fn)(void *),
718be2355b7SFrederic Weisbecker 			  void *arg, struct lock_class_key *key);
719be2355b7SFrederic Weisbecker 
720be2355b7SFrederic Weisbecker /*
721be2355b7SFrederic Weisbecker  * A new key is defined for each caller to make sure the work
722be2355b7SFrederic Weisbecker  * associated with the function doesn't share its locking class.
723be2355b7SFrederic Weisbecker  */
724be2355b7SFrederic Weisbecker #define work_on_cpu_safe(_cpu, _fn, _arg)		\
725be2355b7SFrederic Weisbecker ({							\
726be2355b7SFrederic Weisbecker 	static struct lock_class_key __key;		\
727be2355b7SFrederic Weisbecker 							\
728be2355b7SFrederic Weisbecker 	work_on_cpu_safe_key(_cpu, _fn, _arg, &__key);	\
729be2355b7SFrederic Weisbecker })
7302d3854a3SRusty Russell #endif /* CONFIG_SMP */
731a25909a4SPaul E. McKenney 
732a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER
733a0a1a5fdSTejun Heo extern void freeze_workqueues_begin(void);
734a0a1a5fdSTejun Heo extern bool freeze_workqueues_busy(void);
735a0a1a5fdSTejun Heo extern void thaw_workqueues(void);
736a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */
737a0a1a5fdSTejun Heo 
738226223abSTejun Heo #ifdef CONFIG_SYSFS
739226223abSTejun Heo int workqueue_sysfs_register(struct workqueue_struct *wq);
740226223abSTejun Heo #else	/* CONFIG_SYSFS */
workqueue_sysfs_register(struct workqueue_struct * wq)741226223abSTejun Heo static inline int workqueue_sysfs_register(struct workqueue_struct *wq)
742226223abSTejun Heo { return 0; }
743226223abSTejun Heo #endif	/* CONFIG_SYSFS */
744226223abSTejun Heo 
74582607adcSTejun Heo #ifdef CONFIG_WQ_WATCHDOG
74682607adcSTejun Heo void wq_watchdog_touch(int cpu);
74782607adcSTejun Heo #else	/* CONFIG_WQ_WATCHDOG */
wq_watchdog_touch(int cpu)74882607adcSTejun Heo static inline void wq_watchdog_touch(int cpu) { }
74982607adcSTejun Heo #endif	/* CONFIG_WQ_WATCHDOG */
75082607adcSTejun Heo 
7517ee681b2SThomas Gleixner #ifdef CONFIG_SMP
7527ee681b2SThomas Gleixner int workqueue_prepare_cpu(unsigned int cpu);
7537ee681b2SThomas Gleixner int workqueue_online_cpu(unsigned int cpu);
7547ee681b2SThomas Gleixner int workqueue_offline_cpu(unsigned int cpu);
7557ee681b2SThomas Gleixner #endif
7567ee681b2SThomas Gleixner 
7572333e829SYu Chen void __init workqueue_init_early(void);
7582333e829SYu Chen void __init workqueue_init(void);
7592930155bSTejun Heo void __init workqueue_init_topology(void);
7603347fa09STejun Heo 
7611da177e4SLinus Torvalds #endif
762