xref: /openbmc/linux/kernel/workqueue.c (revision 7fb98ea7)
11da177e4SLinus Torvalds /*
2c54fce6eSTejun Heo  * kernel/workqueue.c - generic async execution with shared worker pool
31da177e4SLinus Torvalds  *
4c54fce6eSTejun Heo  * Copyright (C) 2002		Ingo Molnar
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *   Derived from the taskqueue/keventd code by:
71da177e4SLinus Torvalds  *     David Woodhouse <dwmw2@infradead.org>
8e1f8e874SFrancois Cami  *     Andrew Morton
91da177e4SLinus Torvalds  *     Kai Petzke <wpp@marie.physik.tu-berlin.de>
101da177e4SLinus Torvalds  *     Theodore Ts'o <tytso@mit.edu>
1189ada679SChristoph Lameter  *
12cde53535SChristoph Lameter  * Made to use alloc_percpu by Christoph Lameter.
13c54fce6eSTejun Heo  *
14c54fce6eSTejun Heo  * Copyright (C) 2010		SUSE Linux Products GmbH
15c54fce6eSTejun Heo  * Copyright (C) 2010		Tejun Heo <tj@kernel.org>
16c54fce6eSTejun Heo  *
17c54fce6eSTejun Heo  * This is the generic async execution mechanism.  Work items as are
18c54fce6eSTejun Heo  * executed in process context.  The worker pool is shared and
19c54fce6eSTejun Heo  * automatically managed.  There is one worker pool for each CPU and
20c54fce6eSTejun Heo  * one extra for works which are better served by workers which are
21c54fce6eSTejun Heo  * not bound to any specific CPU.
22c54fce6eSTejun Heo  *
23c54fce6eSTejun Heo  * Please read Documentation/workqueue.txt for details.
241da177e4SLinus Torvalds  */
251da177e4SLinus Torvalds 
269984de1aSPaul Gortmaker #include <linux/export.h>
271da177e4SLinus Torvalds #include <linux/kernel.h>
281da177e4SLinus Torvalds #include <linux/sched.h>
291da177e4SLinus Torvalds #include <linux/init.h>
301da177e4SLinus Torvalds #include <linux/signal.h>
311da177e4SLinus Torvalds #include <linux/completion.h>
321da177e4SLinus Torvalds #include <linux/workqueue.h>
331da177e4SLinus Torvalds #include <linux/slab.h>
341da177e4SLinus Torvalds #include <linux/cpu.h>
351da177e4SLinus Torvalds #include <linux/notifier.h>
361da177e4SLinus Torvalds #include <linux/kthread.h>
371fa44ecaSJames Bottomley #include <linux/hardirq.h>
3846934023SChristoph Lameter #include <linux/mempolicy.h>
39341a5958SRafael J. Wysocki #include <linux/freezer.h>
40d5abe669SPeter Zijlstra #include <linux/kallsyms.h>
41d5abe669SPeter Zijlstra #include <linux/debug_locks.h>
424e6045f1SJohannes Berg #include <linux/lockdep.h>
43c34056a3STejun Heo #include <linux/idr.h>
4442f8570fSSasha Levin #include <linux/hashtable.h>
45e22bee78STejun Heo 
46ea138446STejun Heo #include "workqueue_internal.h"
471da177e4SLinus Torvalds 
48c8e55f36STejun Heo enum {
49bc2ae0f5STejun Heo 	/*
5024647570STejun Heo 	 * worker_pool flags
51bc2ae0f5STejun Heo 	 *
5224647570STejun Heo 	 * A bound pool is either associated or disassociated with its CPU.
53bc2ae0f5STejun Heo 	 * While associated (!DISASSOCIATED), all workers are bound to the
54bc2ae0f5STejun Heo 	 * CPU and none has %WORKER_UNBOUND set and concurrency management
55bc2ae0f5STejun Heo 	 * is in effect.
56bc2ae0f5STejun Heo 	 *
57bc2ae0f5STejun Heo 	 * While DISASSOCIATED, the cpu may be offline and all workers have
58bc2ae0f5STejun Heo 	 * %WORKER_UNBOUND set and concurrency management disabled, and may
5924647570STejun Heo 	 * be executing on any CPU.  The pool behaves as an unbound one.
60bc2ae0f5STejun Heo 	 *
61bc2ae0f5STejun Heo 	 * Note that DISASSOCIATED can be flipped only while holding
6224647570STejun Heo 	 * assoc_mutex to avoid changing binding state while
6324647570STejun Heo 	 * create_worker() is in progress.
64bc2ae0f5STejun Heo 	 */
6511ebea50STejun Heo 	POOL_MANAGE_WORKERS	= 1 << 0,	/* need to manage workers */
66552a37e9SLai Jiangshan 	POOL_MANAGING_WORKERS   = 1 << 1,       /* managing workers */
6724647570STejun Heo 	POOL_DISASSOCIATED	= 1 << 2,	/* cpu can't serve workers */
6835b6bb63STejun Heo 	POOL_FREEZING		= 1 << 3,	/* freeze in progress */
69db7bccf4STejun Heo 
70c8e55f36STejun Heo 	/* worker flags */
71c8e55f36STejun Heo 	WORKER_STARTED		= 1 << 0,	/* started */
72c8e55f36STejun Heo 	WORKER_DIE		= 1 << 1,	/* die die die */
73c8e55f36STejun Heo 	WORKER_IDLE		= 1 << 2,	/* is idle */
74e22bee78STejun Heo 	WORKER_PREP		= 1 << 3,	/* preparing to run works */
75fb0e7bebSTejun Heo 	WORKER_CPU_INTENSIVE	= 1 << 6,	/* cpu intensive */
76f3421797STejun Heo 	WORKER_UNBOUND		= 1 << 7,	/* worker is unbound */
77e22bee78STejun Heo 
785f7dabfdSLai Jiangshan 	WORKER_NOT_RUNNING	= WORKER_PREP | WORKER_UNBOUND |
79403c821dSTejun Heo 				  WORKER_CPU_INTENSIVE,
80db7bccf4STejun Heo 
81e34cdddbSTejun Heo 	NR_STD_WORKER_POOLS	= 2,		/* # standard pools per cpu */
824ce62e9eSTejun Heo 
83c8e55f36STejun Heo 	BUSY_WORKER_HASH_ORDER	= 6,		/* 64 pointers */
84db7bccf4STejun Heo 
85e22bee78STejun Heo 	MAX_IDLE_WORKERS_RATIO	= 4,		/* 1/4 of busy can be idle */
86e22bee78STejun Heo 	IDLE_WORKER_TIMEOUT	= 300 * HZ,	/* keep idle ones for 5 mins */
87e22bee78STejun Heo 
883233cdbdSTejun Heo 	MAYDAY_INITIAL_TIMEOUT  = HZ / 100 >= 2 ? HZ / 100 : 2,
893233cdbdSTejun Heo 						/* call for help after 10ms
903233cdbdSTejun Heo 						   (min two ticks) */
91e22bee78STejun Heo 	MAYDAY_INTERVAL		= HZ / 10,	/* and then every 100ms */
92e22bee78STejun Heo 	CREATE_COOLDOWN		= HZ,		/* time to breath after fail */
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds 	/*
95e22bee78STejun Heo 	 * Rescue workers are used only on emergencies and shared by
96e22bee78STejun Heo 	 * all cpus.  Give -20.
97e22bee78STejun Heo 	 */
98e22bee78STejun Heo 	RESCUER_NICE_LEVEL	= -20,
993270476aSTejun Heo 	HIGHPRI_NICE_LEVEL	= -20,
100c8e55f36STejun Heo };
101c8e55f36STejun Heo 
1021da177e4SLinus Torvalds /*
1034690c4abSTejun Heo  * Structure fields follow one of the following exclusion rules.
1044690c4abSTejun Heo  *
105e41e704bSTejun Heo  * I: Modifiable by initialization/destruction paths and read-only for
106e41e704bSTejun Heo  *    everyone else.
1074690c4abSTejun Heo  *
108e22bee78STejun Heo  * P: Preemption protected.  Disabling preemption is enough and should
109e22bee78STejun Heo  *    only be modified and accessed from the local cpu.
110e22bee78STejun Heo  *
111d565ed63STejun Heo  * L: pool->lock protected.  Access with pool->lock held.
1124690c4abSTejun Heo  *
113d565ed63STejun Heo  * X: During normal operation, modification requires pool->lock and should
114d565ed63STejun Heo  *    be done only from local cpu.  Either disabling preemption on local
115d565ed63STejun Heo  *    cpu or grabbing pool->lock is enough for read access.  If
116d565ed63STejun Heo  *    POOL_DISASSOCIATED is set, it's identical to L.
117e22bee78STejun Heo  *
11873f53c4aSTejun Heo  * F: wq->flush_mutex protected.
11973f53c4aSTejun Heo  *
1204690c4abSTejun Heo  * W: workqueue_lock protected.
1214690c4abSTejun Heo  */
1224690c4abSTejun Heo 
1232eaebdb3STejun Heo /* struct worker is defined in workqueue_internal.h */
124c34056a3STejun Heo 
125bd7bdd43STejun Heo struct worker_pool {
126d565ed63STejun Heo 	spinlock_t		lock;		/* the pool lock */
127d84ff051STejun Heo 	int			cpu;		/* I: the associated cpu */
1289daf9e67STejun Heo 	int			id;		/* I: pool ID */
12911ebea50STejun Heo 	unsigned int		flags;		/* X: flags */
130bd7bdd43STejun Heo 
131bd7bdd43STejun Heo 	struct list_head	worklist;	/* L: list of pending works */
132bd7bdd43STejun Heo 	int			nr_workers;	/* L: total number of workers */
133ea1abd61SLai Jiangshan 
134ea1abd61SLai Jiangshan 	/* nr_idle includes the ones off idle_list for rebinding */
135bd7bdd43STejun Heo 	int			nr_idle;	/* L: currently idle ones */
136bd7bdd43STejun Heo 
137bd7bdd43STejun Heo 	struct list_head	idle_list;	/* X: list of idle workers */
138bd7bdd43STejun Heo 	struct timer_list	idle_timer;	/* L: worker idle timeout */
139bd7bdd43STejun Heo 	struct timer_list	mayday_timer;	/* L: SOS timer for workers */
140bd7bdd43STejun Heo 
141c9e7cf27STejun Heo 	/* workers are chained either in busy_hash or idle_list */
142c9e7cf27STejun Heo 	DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
143c9e7cf27STejun Heo 						/* L: hash of busy workers */
144c9e7cf27STejun Heo 
14524647570STejun Heo 	struct mutex		assoc_mutex;	/* protect POOL_DISASSOCIATED */
146bd7bdd43STejun Heo 	struct ida		worker_ida;	/* L: for worker IDs */
147e19e397aSTejun Heo 
148e19e397aSTejun Heo 	/*
149e19e397aSTejun Heo 	 * The current concurrency level.  As it's likely to be accessed
150e19e397aSTejun Heo 	 * from other CPUs during try_to_wake_up(), put it in a separate
151e19e397aSTejun Heo 	 * cacheline.
152e19e397aSTejun Heo 	 */
153e19e397aSTejun Heo 	atomic_t		nr_running ____cacheline_aligned_in_smp;
1548b03ae3cSTejun Heo } ____cacheline_aligned_in_smp;
1558b03ae3cSTejun Heo 
1568b03ae3cSTejun Heo /*
157112202d9STejun Heo  * The per-pool workqueue.  While queued, the lower WORK_STRUCT_FLAG_BITS
158112202d9STejun Heo  * of work_struct->data are used for flags and the remaining high bits
159112202d9STejun Heo  * point to the pwq; thus, pwqs need to be aligned at two's power of the
160112202d9STejun Heo  * number of flag bits.
1611da177e4SLinus Torvalds  */
162112202d9STejun Heo struct pool_workqueue {
163bd7bdd43STejun Heo 	struct worker_pool	*pool;		/* I: the associated pool */
1644690c4abSTejun Heo 	struct workqueue_struct *wq;		/* I: the owning workqueue */
16573f53c4aSTejun Heo 	int			work_color;	/* L: current color */
16673f53c4aSTejun Heo 	int			flush_color;	/* L: flushing color */
16773f53c4aSTejun Heo 	int			nr_in_flight[WORK_NR_COLORS];
16873f53c4aSTejun Heo 						/* L: nr of in_flight works */
1691e19ffc6STejun Heo 	int			nr_active;	/* L: nr of active works */
170a0a1a5fdSTejun Heo 	int			max_active;	/* L: max active works */
1711e19ffc6STejun Heo 	struct list_head	delayed_works;	/* L: delayed works */
17230cdf249STejun Heo 	struct list_head	pwqs_node;	/* I: node on wq->pwqs */
173493a1724STejun Heo 	struct list_head	mayday_node;	/* W: node on wq->maydays */
174e904e6c2STejun Heo } __aligned(1 << WORK_STRUCT_FLAG_BITS);
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds /*
17773f53c4aSTejun Heo  * Structure used to wait for workqueue flush.
17873f53c4aSTejun Heo  */
17973f53c4aSTejun Heo struct wq_flusher {
18073f53c4aSTejun Heo 	struct list_head	list;		/* F: list of flushers */
18173f53c4aSTejun Heo 	int			flush_color;	/* F: flush color waiting for */
18273f53c4aSTejun Heo 	struct completion	done;		/* flush completion */
18373f53c4aSTejun Heo };
1841da177e4SLinus Torvalds 
18573f53c4aSTejun Heo /*
1861da177e4SLinus Torvalds  * The externally visible workqueue abstraction is an array of
1871da177e4SLinus Torvalds  * per-CPU workqueues:
1881da177e4SLinus Torvalds  */
1891da177e4SLinus Torvalds struct workqueue_struct {
1909c5a2ba7STejun Heo 	unsigned int		flags;		/* W: WQ_* flags */
191420c0ddbSTejun Heo 	struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwq's */
19230cdf249STejun Heo 	struct list_head	pwqs;		/* I: all pwqs of this wq */
1934690c4abSTejun Heo 	struct list_head	list;		/* W: list of all workqueues */
19473f53c4aSTejun Heo 
19573f53c4aSTejun Heo 	struct mutex		flush_mutex;	/* protects wq flushing */
19673f53c4aSTejun Heo 	int			work_color;	/* F: current work color */
19773f53c4aSTejun Heo 	int			flush_color;	/* F: current flush color */
198112202d9STejun Heo 	atomic_t		nr_pwqs_to_flush; /* flush in progress */
19973f53c4aSTejun Heo 	struct wq_flusher	*first_flusher;	/* F: first flusher */
20073f53c4aSTejun Heo 	struct list_head	flusher_queue;	/* F: flush waiters */
20173f53c4aSTejun Heo 	struct list_head	flusher_overflow; /* F: flush overflow list */
20273f53c4aSTejun Heo 
203493a1724STejun Heo 	struct list_head	maydays;	/* W: pwqs requesting rescue */
204e22bee78STejun Heo 	struct worker		*rescuer;	/* I: rescue worker */
205e22bee78STejun Heo 
2069c5a2ba7STejun Heo 	int			nr_drainers;	/* W: drain in progress */
207112202d9STejun Heo 	int			saved_max_active; /* W: saved pwq max_active */
2084e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
2094e6045f1SJohannes Berg 	struct lockdep_map	lockdep_map;
2104e6045f1SJohannes Berg #endif
211b196be89STejun Heo 	char			name[];		/* I: workqueue name */
2121da177e4SLinus Torvalds };
2131da177e4SLinus Torvalds 
214e904e6c2STejun Heo static struct kmem_cache *pwq_cache;
215e904e6c2STejun Heo 
216d320c038STejun Heo struct workqueue_struct *system_wq __read_mostly;
217d320c038STejun Heo EXPORT_SYMBOL_GPL(system_wq);
218044c782cSValentin Ilie struct workqueue_struct *system_highpri_wq __read_mostly;
2191aabe902SJoonsoo Kim EXPORT_SYMBOL_GPL(system_highpri_wq);
220044c782cSValentin Ilie struct workqueue_struct *system_long_wq __read_mostly;
221d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq);
222044c782cSValentin Ilie struct workqueue_struct *system_unbound_wq __read_mostly;
223f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq);
224044c782cSValentin Ilie struct workqueue_struct *system_freezable_wq __read_mostly;
22524d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq);
226d320c038STejun Heo 
22797bd2347STejun Heo #define CREATE_TRACE_POINTS
22897bd2347STejun Heo #include <trace/events/workqueue.h>
22997bd2347STejun Heo 
23038db41d9STejun Heo #define for_each_std_worker_pool(pool, cpu)				\
231a60dc39cSTejun Heo 	for ((pool) = &std_worker_pools(cpu)[0];			\
232a60dc39cSTejun Heo 	     (pool) < &std_worker_pools(cpu)[NR_STD_WORKER_POOLS]; (pool)++)
2334ce62e9eSTejun Heo 
234b67bfe0dSSasha Levin #define for_each_busy_worker(worker, i, pool)				\
235b67bfe0dSSasha Levin 	hash_for_each(pool->busy_hash, i, worker, hentry)
236db7bccf4STejun Heo 
237706026c2STejun Heo static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
238f3421797STejun Heo 				unsigned int sw)
239f3421797STejun Heo {
240f3421797STejun Heo 	if (cpu < nr_cpu_ids) {
241f3421797STejun Heo 		if (sw & 1) {
242f3421797STejun Heo 			cpu = cpumask_next(cpu, mask);
243f3421797STejun Heo 			if (cpu < nr_cpu_ids)
244f3421797STejun Heo 				return cpu;
245f3421797STejun Heo 		}
246f3421797STejun Heo 		if (sw & 2)
247f3421797STejun Heo 			return WORK_CPU_UNBOUND;
248f3421797STejun Heo 	}
2496be19588SLai Jiangshan 	return WORK_CPU_END;
250f3421797STejun Heo }
251f3421797STejun Heo 
25209884951STejun Heo /*
25309884951STejun Heo  * CPU iterators
25409884951STejun Heo  *
255706026c2STejun Heo  * An extra cpu number is defined using an invalid cpu number
25609884951STejun Heo  * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
257706026c2STejun Heo  * specific CPU.  The following iterators are similar to for_each_*_cpu()
258706026c2STejun Heo  * iterators but also considers the unbound CPU.
25909884951STejun Heo  *
260706026c2STejun Heo  * for_each_wq_cpu()		: possible CPUs + WORK_CPU_UNBOUND
261706026c2STejun Heo  * for_each_online_wq_cpu()	: online CPUs + WORK_CPU_UNBOUND
26209884951STejun Heo  */
263706026c2STejun Heo #define for_each_wq_cpu(cpu)						\
264706026c2STejun Heo 	for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, 3);		\
2656be19588SLai Jiangshan 	     (cpu) < WORK_CPU_END;					\
266706026c2STejun Heo 	     (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, 3))
267f3421797STejun Heo 
268706026c2STejun Heo #define for_each_online_wq_cpu(cpu)					\
269706026c2STejun Heo 	for ((cpu) = __next_wq_cpu(-1, cpu_online_mask, 3);		\
2706be19588SLai Jiangshan 	     (cpu) < WORK_CPU_END;					\
271706026c2STejun Heo 	     (cpu) = __next_wq_cpu((cpu), cpu_online_mask, 3))
272f3421797STejun Heo 
27349e3cf44STejun Heo /**
27417116969STejun Heo  * for_each_pool - iterate through all worker_pools in the system
27517116969STejun Heo  * @pool: iteration cursor
27617116969STejun Heo  * @id: integer used for iteration
27717116969STejun Heo  */
27817116969STejun Heo #define for_each_pool(pool, id)						\
27917116969STejun Heo 	idr_for_each_entry(&worker_pool_idr, pool, id)
28017116969STejun Heo 
28117116969STejun Heo /**
28249e3cf44STejun Heo  * for_each_pwq - iterate through all pool_workqueues of the specified workqueue
28349e3cf44STejun Heo  * @pwq: iteration cursor
28449e3cf44STejun Heo  * @wq: the target workqueue
28549e3cf44STejun Heo  */
28649e3cf44STejun Heo #define for_each_pwq(pwq, wq)						\
28749e3cf44STejun Heo 	list_for_each_entry((pwq), &(wq)->pwqs, pwqs_node)
288f3421797STejun Heo 
289dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK
290dc186ad7SThomas Gleixner 
291dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr;
292dc186ad7SThomas Gleixner 
29399777288SStanislaw Gruszka static void *work_debug_hint(void *addr)
29499777288SStanislaw Gruszka {
29599777288SStanislaw Gruszka 	return ((struct work_struct *) addr)->func;
29699777288SStanislaw Gruszka }
29799777288SStanislaw Gruszka 
298dc186ad7SThomas Gleixner /*
299dc186ad7SThomas Gleixner  * fixup_init is called when:
300dc186ad7SThomas Gleixner  * - an active object is initialized
301dc186ad7SThomas Gleixner  */
302dc186ad7SThomas Gleixner static int work_fixup_init(void *addr, enum debug_obj_state state)
303dc186ad7SThomas Gleixner {
304dc186ad7SThomas Gleixner 	struct work_struct *work = addr;
305dc186ad7SThomas Gleixner 
306dc186ad7SThomas Gleixner 	switch (state) {
307dc186ad7SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
308dc186ad7SThomas Gleixner 		cancel_work_sync(work);
309dc186ad7SThomas Gleixner 		debug_object_init(work, &work_debug_descr);
310dc186ad7SThomas Gleixner 		return 1;
311dc186ad7SThomas Gleixner 	default:
312dc186ad7SThomas Gleixner 		return 0;
313dc186ad7SThomas Gleixner 	}
314dc186ad7SThomas Gleixner }
315dc186ad7SThomas Gleixner 
316dc186ad7SThomas Gleixner /*
317dc186ad7SThomas Gleixner  * fixup_activate is called when:
318dc186ad7SThomas Gleixner  * - an active object is activated
319dc186ad7SThomas Gleixner  * - an unknown object is activated (might be a statically initialized object)
320dc186ad7SThomas Gleixner  */
321dc186ad7SThomas Gleixner static int work_fixup_activate(void *addr, enum debug_obj_state state)
322dc186ad7SThomas Gleixner {
323dc186ad7SThomas Gleixner 	struct work_struct *work = addr;
324dc186ad7SThomas Gleixner 
325dc186ad7SThomas Gleixner 	switch (state) {
326dc186ad7SThomas Gleixner 
327dc186ad7SThomas Gleixner 	case ODEBUG_STATE_NOTAVAILABLE:
328dc186ad7SThomas Gleixner 		/*
329dc186ad7SThomas Gleixner 		 * This is not really a fixup. The work struct was
330dc186ad7SThomas Gleixner 		 * statically initialized. We just make sure that it
331dc186ad7SThomas Gleixner 		 * is tracked in the object tracker.
332dc186ad7SThomas Gleixner 		 */
33322df02bbSTejun Heo 		if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
334dc186ad7SThomas Gleixner 			debug_object_init(work, &work_debug_descr);
335dc186ad7SThomas Gleixner 			debug_object_activate(work, &work_debug_descr);
336dc186ad7SThomas Gleixner 			return 0;
337dc186ad7SThomas Gleixner 		}
338dc186ad7SThomas Gleixner 		WARN_ON_ONCE(1);
339dc186ad7SThomas Gleixner 		return 0;
340dc186ad7SThomas Gleixner 
341dc186ad7SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
342dc186ad7SThomas Gleixner 		WARN_ON(1);
343dc186ad7SThomas Gleixner 
344dc186ad7SThomas Gleixner 	default:
345dc186ad7SThomas Gleixner 		return 0;
346dc186ad7SThomas Gleixner 	}
347dc186ad7SThomas Gleixner }
348dc186ad7SThomas Gleixner 
349dc186ad7SThomas Gleixner /*
350dc186ad7SThomas Gleixner  * fixup_free is called when:
351dc186ad7SThomas Gleixner  * - an active object is freed
352dc186ad7SThomas Gleixner  */
353dc186ad7SThomas Gleixner static int work_fixup_free(void *addr, enum debug_obj_state state)
354dc186ad7SThomas Gleixner {
355dc186ad7SThomas Gleixner 	struct work_struct *work = addr;
356dc186ad7SThomas Gleixner 
357dc186ad7SThomas Gleixner 	switch (state) {
358dc186ad7SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
359dc186ad7SThomas Gleixner 		cancel_work_sync(work);
360dc186ad7SThomas Gleixner 		debug_object_free(work, &work_debug_descr);
361dc186ad7SThomas Gleixner 		return 1;
362dc186ad7SThomas Gleixner 	default:
363dc186ad7SThomas Gleixner 		return 0;
364dc186ad7SThomas Gleixner 	}
365dc186ad7SThomas Gleixner }
366dc186ad7SThomas Gleixner 
367dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr = {
368dc186ad7SThomas Gleixner 	.name		= "work_struct",
36999777288SStanislaw Gruszka 	.debug_hint	= work_debug_hint,
370dc186ad7SThomas Gleixner 	.fixup_init	= work_fixup_init,
371dc186ad7SThomas Gleixner 	.fixup_activate	= work_fixup_activate,
372dc186ad7SThomas Gleixner 	.fixup_free	= work_fixup_free,
373dc186ad7SThomas Gleixner };
374dc186ad7SThomas Gleixner 
375dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work)
376dc186ad7SThomas Gleixner {
377dc186ad7SThomas Gleixner 	debug_object_activate(work, &work_debug_descr);
378dc186ad7SThomas Gleixner }
379dc186ad7SThomas Gleixner 
380dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work)
381dc186ad7SThomas Gleixner {
382dc186ad7SThomas Gleixner 	debug_object_deactivate(work, &work_debug_descr);
383dc186ad7SThomas Gleixner }
384dc186ad7SThomas Gleixner 
385dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack)
386dc186ad7SThomas Gleixner {
387dc186ad7SThomas Gleixner 	if (onstack)
388dc186ad7SThomas Gleixner 		debug_object_init_on_stack(work, &work_debug_descr);
389dc186ad7SThomas Gleixner 	else
390dc186ad7SThomas Gleixner 		debug_object_init(work, &work_debug_descr);
391dc186ad7SThomas Gleixner }
392dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work);
393dc186ad7SThomas Gleixner 
394dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work)
395dc186ad7SThomas Gleixner {
396dc186ad7SThomas Gleixner 	debug_object_free(work, &work_debug_descr);
397dc186ad7SThomas Gleixner }
398dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack);
399dc186ad7SThomas Gleixner 
400dc186ad7SThomas Gleixner #else
401dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { }
402dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { }
403dc186ad7SThomas Gleixner #endif
404dc186ad7SThomas Gleixner 
40595402b38SGautham R Shenoy /* Serializes the accesses to the list of workqueues. */
40695402b38SGautham R Shenoy static DEFINE_SPINLOCK(workqueue_lock);
4071da177e4SLinus Torvalds static LIST_HEAD(workqueues);
408a0a1a5fdSTejun Heo static bool workqueue_freezing;		/* W: have wqs started freezing? */
4091da177e4SLinus Torvalds 
41014441960SOleg Nesterov /*
411e19e397aSTejun Heo  * The CPU and unbound standard worker pools.  The unbound ones have
412e19e397aSTejun Heo  * POOL_DISASSOCIATED set, and their workers have WORKER_UNBOUND set.
41314441960SOleg Nesterov  */
414e19e397aSTejun Heo static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
415a60dc39cSTejun Heo 				     cpu_std_worker_pools);
416a60dc39cSTejun Heo static struct worker_pool unbound_std_worker_pools[NR_STD_WORKER_POOLS];
417f3421797STejun Heo 
4189daf9e67STejun Heo /* idr of all pools */
4199daf9e67STejun Heo static DEFINE_MUTEX(worker_pool_idr_mutex);
4209daf9e67STejun Heo static DEFINE_IDR(worker_pool_idr);
4219daf9e67STejun Heo 
422c34056a3STejun Heo static int worker_thread(void *__worker);
4231da177e4SLinus Torvalds 
424a60dc39cSTejun Heo static struct worker_pool *std_worker_pools(int cpu)
4251da177e4SLinus Torvalds {
426f3421797STejun Heo 	if (cpu != WORK_CPU_UNBOUND)
427a60dc39cSTejun Heo 		return per_cpu(cpu_std_worker_pools, cpu);
428f3421797STejun Heo 	else
429a60dc39cSTejun Heo 		return unbound_std_worker_pools;
4301da177e4SLinus Torvalds }
4311da177e4SLinus Torvalds 
4324e8f0a60STejun Heo static int std_worker_pool_pri(struct worker_pool *pool)
4334e8f0a60STejun Heo {
434a60dc39cSTejun Heo 	return pool - std_worker_pools(pool->cpu);
4354e8f0a60STejun Heo }
4364e8f0a60STejun Heo 
4379daf9e67STejun Heo /* allocate ID and assign it to @pool */
4389daf9e67STejun Heo static int worker_pool_assign_id(struct worker_pool *pool)
4399daf9e67STejun Heo {
4409daf9e67STejun Heo 	int ret;
4419daf9e67STejun Heo 
4429daf9e67STejun Heo 	mutex_lock(&worker_pool_idr_mutex);
4439daf9e67STejun Heo 	idr_pre_get(&worker_pool_idr, GFP_KERNEL);
4449daf9e67STejun Heo 	ret = idr_get_new(&worker_pool_idr, pool, &pool->id);
4459daf9e67STejun Heo 	mutex_unlock(&worker_pool_idr_mutex);
4469daf9e67STejun Heo 
4479daf9e67STejun Heo 	return ret;
4489daf9e67STejun Heo }
4499daf9e67STejun Heo 
4507c3eed5cSTejun Heo /*
4517c3eed5cSTejun Heo  * Lookup worker_pool by id.  The idr currently is built during boot and
4527c3eed5cSTejun Heo  * never modified.  Don't worry about locking for now.
4537c3eed5cSTejun Heo  */
4547c3eed5cSTejun Heo static struct worker_pool *worker_pool_by_id(int pool_id)
4557c3eed5cSTejun Heo {
4567c3eed5cSTejun Heo 	return idr_find(&worker_pool_idr, pool_id);
4577c3eed5cSTejun Heo }
4587c3eed5cSTejun Heo 
459d565ed63STejun Heo static struct worker_pool *get_std_worker_pool(int cpu, bool highpri)
460d565ed63STejun Heo {
461a60dc39cSTejun Heo 	struct worker_pool *pools = std_worker_pools(cpu);
462d565ed63STejun Heo 
463a60dc39cSTejun Heo 	return &pools[highpri];
464d565ed63STejun Heo }
465d565ed63STejun Heo 
4667fb98ea7STejun Heo static struct pool_workqueue *first_pwq(struct workqueue_struct *wq)
467a848e3b6SOleg Nesterov {
4687fb98ea7STejun Heo 	return list_first_entry(&wq->pwqs, struct pool_workqueue, pwqs_node);
469f3421797STejun Heo }
470a848e3b6SOleg Nesterov 
47173f53c4aSTejun Heo static unsigned int work_color_to_flags(int color)
47273f53c4aSTejun Heo {
47373f53c4aSTejun Heo 	return color << WORK_STRUCT_COLOR_SHIFT;
47473f53c4aSTejun Heo }
47573f53c4aSTejun Heo 
47673f53c4aSTejun Heo static int get_work_color(struct work_struct *work)
47773f53c4aSTejun Heo {
47873f53c4aSTejun Heo 	return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
47973f53c4aSTejun Heo 		((1 << WORK_STRUCT_COLOR_BITS) - 1);
48073f53c4aSTejun Heo }
48173f53c4aSTejun Heo 
48273f53c4aSTejun Heo static int work_next_color(int color)
48373f53c4aSTejun Heo {
48473f53c4aSTejun Heo 	return (color + 1) % WORK_NR_COLORS;
485b1f4ec17SOleg Nesterov }
486b1f4ec17SOleg Nesterov 
4874594bf15SDavid Howells /*
488112202d9STejun Heo  * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data
489112202d9STejun Heo  * contain the pointer to the queued pwq.  Once execution starts, the flag
4907c3eed5cSTejun Heo  * is cleared and the high bits contain OFFQ flags and pool ID.
4917a22ad75STejun Heo  *
492112202d9STejun Heo  * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
493112202d9STejun Heo  * and clear_work_data() can be used to set the pwq, pool or clear
494bbb68dfaSTejun Heo  * work->data.  These functions should only be called while the work is
495bbb68dfaSTejun Heo  * owned - ie. while the PENDING bit is set.
4967a22ad75STejun Heo  *
497112202d9STejun Heo  * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq
4987c3eed5cSTejun Heo  * corresponding to a work.  Pool is available once the work has been
499112202d9STejun Heo  * queued anywhere after initialization until it is sync canceled.  pwq is
5007c3eed5cSTejun Heo  * available only while the work item is queued.
501bbb68dfaSTejun Heo  *
502bbb68dfaSTejun Heo  * %WORK_OFFQ_CANCELING is used to mark a work item which is being
503bbb68dfaSTejun Heo  * canceled.  While being canceled, a work item may have its PENDING set
504bbb68dfaSTejun Heo  * but stay off timer and worklist for arbitrarily long and nobody should
505bbb68dfaSTejun Heo  * try to steal the PENDING bit.
5064594bf15SDavid Howells  */
5077a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data,
5087a22ad75STejun Heo 				 unsigned long flags)
5097a22ad75STejun Heo {
5106183c009STejun Heo 	WARN_ON_ONCE(!work_pending(work));
5117a22ad75STejun Heo 	atomic_long_set(&work->data, data | flags | work_static(work));
5127a22ad75STejun Heo }
5137a22ad75STejun Heo 
514112202d9STejun Heo static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq,
5154690c4abSTejun Heo 			 unsigned long extra_flags)
516365970a1SDavid Howells {
517112202d9STejun Heo 	set_work_data(work, (unsigned long)pwq,
518112202d9STejun Heo 		      WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags);
519365970a1SDavid Howells }
520365970a1SDavid Howells 
5214468a00fSLai Jiangshan static void set_work_pool_and_keep_pending(struct work_struct *work,
5224468a00fSLai Jiangshan 					   int pool_id)
5234468a00fSLai Jiangshan {
5244468a00fSLai Jiangshan 	set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT,
5254468a00fSLai Jiangshan 		      WORK_STRUCT_PENDING);
5264468a00fSLai Jiangshan }
5274468a00fSLai Jiangshan 
5287c3eed5cSTejun Heo static void set_work_pool_and_clear_pending(struct work_struct *work,
5297c3eed5cSTejun Heo 					    int pool_id)
5304d707b9fSOleg Nesterov {
53123657bb1STejun Heo 	/*
53223657bb1STejun Heo 	 * The following wmb is paired with the implied mb in
53323657bb1STejun Heo 	 * test_and_set_bit(PENDING) and ensures all updates to @work made
53423657bb1STejun Heo 	 * here are visible to and precede any updates by the next PENDING
53523657bb1STejun Heo 	 * owner.
53623657bb1STejun Heo 	 */
53723657bb1STejun Heo 	smp_wmb();
5387c3eed5cSTejun Heo 	set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
5394d707b9fSOleg Nesterov }
5404d707b9fSOleg Nesterov 
5417a22ad75STejun Heo static void clear_work_data(struct work_struct *work)
542365970a1SDavid Howells {
5437c3eed5cSTejun Heo 	smp_wmb();	/* see set_work_pool_and_clear_pending() */
5447c3eed5cSTejun Heo 	set_work_data(work, WORK_STRUCT_NO_POOL, 0);
5457a22ad75STejun Heo }
5467a22ad75STejun Heo 
547112202d9STejun Heo static struct pool_workqueue *get_work_pwq(struct work_struct *work)
5487a22ad75STejun Heo {
549e120153dSTejun Heo 	unsigned long data = atomic_long_read(&work->data);
5507a22ad75STejun Heo 
551112202d9STejun Heo 	if (data & WORK_STRUCT_PWQ)
552e120153dSTejun Heo 		return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
553e120153dSTejun Heo 	else
554e120153dSTejun Heo 		return NULL;
5557a22ad75STejun Heo }
5567a22ad75STejun Heo 
5577c3eed5cSTejun Heo /**
5587c3eed5cSTejun Heo  * get_work_pool - return the worker_pool a given work was associated with
5597c3eed5cSTejun Heo  * @work: the work item of interest
5607c3eed5cSTejun Heo  *
5617c3eed5cSTejun Heo  * Return the worker_pool @work was last associated with.  %NULL if none.
5627c3eed5cSTejun Heo  */
5637c3eed5cSTejun Heo static struct worker_pool *get_work_pool(struct work_struct *work)
5647a22ad75STejun Heo {
565e120153dSTejun Heo 	unsigned long data = atomic_long_read(&work->data);
5667c3eed5cSTejun Heo 	struct worker_pool *pool;
5677c3eed5cSTejun Heo 	int pool_id;
5687a22ad75STejun Heo 
569112202d9STejun Heo 	if (data & WORK_STRUCT_PWQ)
570112202d9STejun Heo 		return ((struct pool_workqueue *)
5717c3eed5cSTejun Heo 			(data & WORK_STRUCT_WQ_DATA_MASK))->pool;
5727a22ad75STejun Heo 
5737c3eed5cSTejun Heo 	pool_id = data >> WORK_OFFQ_POOL_SHIFT;
5747c3eed5cSTejun Heo 	if (pool_id == WORK_OFFQ_POOL_NONE)
5757a22ad75STejun Heo 		return NULL;
5767a22ad75STejun Heo 
5777c3eed5cSTejun Heo 	pool = worker_pool_by_id(pool_id);
5787c3eed5cSTejun Heo 	WARN_ON_ONCE(!pool);
5797c3eed5cSTejun Heo 	return pool;
5807c3eed5cSTejun Heo }
5817c3eed5cSTejun Heo 
5827c3eed5cSTejun Heo /**
5837c3eed5cSTejun Heo  * get_work_pool_id - return the worker pool ID a given work is associated with
5847c3eed5cSTejun Heo  * @work: the work item of interest
5857c3eed5cSTejun Heo  *
5867c3eed5cSTejun Heo  * Return the worker_pool ID @work was last associated with.
5877c3eed5cSTejun Heo  * %WORK_OFFQ_POOL_NONE if none.
5887c3eed5cSTejun Heo  */
5897c3eed5cSTejun Heo static int get_work_pool_id(struct work_struct *work)
5907c3eed5cSTejun Heo {
59154d5b7d0SLai Jiangshan 	unsigned long data = atomic_long_read(&work->data);
5927c3eed5cSTejun Heo 
593112202d9STejun Heo 	if (data & WORK_STRUCT_PWQ)
594112202d9STejun Heo 		return ((struct pool_workqueue *)
59554d5b7d0SLai Jiangshan 			(data & WORK_STRUCT_WQ_DATA_MASK))->pool->id;
59654d5b7d0SLai Jiangshan 
59754d5b7d0SLai Jiangshan 	return data >> WORK_OFFQ_POOL_SHIFT;
5987c3eed5cSTejun Heo }
5997c3eed5cSTejun Heo 
600bbb68dfaSTejun Heo static void mark_work_canceling(struct work_struct *work)
601bbb68dfaSTejun Heo {
6027c3eed5cSTejun Heo 	unsigned long pool_id = get_work_pool_id(work);
603bbb68dfaSTejun Heo 
6047c3eed5cSTejun Heo 	pool_id <<= WORK_OFFQ_POOL_SHIFT;
6057c3eed5cSTejun Heo 	set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
606bbb68dfaSTejun Heo }
607bbb68dfaSTejun Heo 
608bbb68dfaSTejun Heo static bool work_is_canceling(struct work_struct *work)
609bbb68dfaSTejun Heo {
610bbb68dfaSTejun Heo 	unsigned long data = atomic_long_read(&work->data);
611bbb68dfaSTejun Heo 
612112202d9STejun Heo 	return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING);
613bbb68dfaSTejun Heo }
614bbb68dfaSTejun Heo 
615e22bee78STejun Heo /*
6163270476aSTejun Heo  * Policy functions.  These define the policies on how the global worker
6173270476aSTejun Heo  * pools are managed.  Unless noted otherwise, these functions assume that
618d565ed63STejun Heo  * they're being called with pool->lock held.
619e22bee78STejun Heo  */
620e22bee78STejun Heo 
62163d95a91STejun Heo static bool __need_more_worker(struct worker_pool *pool)
622649027d7STejun Heo {
623e19e397aSTejun Heo 	return !atomic_read(&pool->nr_running);
624649027d7STejun Heo }
625649027d7STejun Heo 
626e22bee78STejun Heo /*
627e22bee78STejun Heo  * Need to wake up a worker?  Called from anything but currently
628e22bee78STejun Heo  * running workers.
629974271c4STejun Heo  *
630974271c4STejun Heo  * Note that, because unbound workers never contribute to nr_running, this
631706026c2STejun Heo  * function will always return %true for unbound pools as long as the
632974271c4STejun Heo  * worklist isn't empty.
633e22bee78STejun Heo  */
63463d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool)
635e22bee78STejun Heo {
63663d95a91STejun Heo 	return !list_empty(&pool->worklist) && __need_more_worker(pool);
637e22bee78STejun Heo }
638e22bee78STejun Heo 
639e22bee78STejun Heo /* Can I start working?  Called from busy but !running workers. */
64063d95a91STejun Heo static bool may_start_working(struct worker_pool *pool)
641e22bee78STejun Heo {
64263d95a91STejun Heo 	return pool->nr_idle;
643e22bee78STejun Heo }
644e22bee78STejun Heo 
645e22bee78STejun Heo /* Do I need to keep working?  Called from currently running workers. */
64663d95a91STejun Heo static bool keep_working(struct worker_pool *pool)
647e22bee78STejun Heo {
648e19e397aSTejun Heo 	return !list_empty(&pool->worklist) &&
649e19e397aSTejun Heo 		atomic_read(&pool->nr_running) <= 1;
650e22bee78STejun Heo }
651e22bee78STejun Heo 
652e22bee78STejun Heo /* Do we need a new worker?  Called from manager. */
65363d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool)
654e22bee78STejun Heo {
65563d95a91STejun Heo 	return need_more_worker(pool) && !may_start_working(pool);
656e22bee78STejun Heo }
657e22bee78STejun Heo 
658e22bee78STejun Heo /* Do I need to be the manager? */
65963d95a91STejun Heo static bool need_to_manage_workers(struct worker_pool *pool)
660e22bee78STejun Heo {
66163d95a91STejun Heo 	return need_to_create_worker(pool) ||
66211ebea50STejun Heo 		(pool->flags & POOL_MANAGE_WORKERS);
663e22bee78STejun Heo }
664e22bee78STejun Heo 
665e22bee78STejun Heo /* Do we have too many workers and should some go away? */
66663d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool)
667e22bee78STejun Heo {
668552a37e9SLai Jiangshan 	bool managing = pool->flags & POOL_MANAGING_WORKERS;
66963d95a91STejun Heo 	int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
67063d95a91STejun Heo 	int nr_busy = pool->nr_workers - nr_idle;
671e22bee78STejun Heo 
672ea1abd61SLai Jiangshan 	/*
673ea1abd61SLai Jiangshan 	 * nr_idle and idle_list may disagree if idle rebinding is in
674ea1abd61SLai Jiangshan 	 * progress.  Never return %true if idle_list is empty.
675ea1abd61SLai Jiangshan 	 */
676ea1abd61SLai Jiangshan 	if (list_empty(&pool->idle_list))
677ea1abd61SLai Jiangshan 		return false;
678ea1abd61SLai Jiangshan 
679e22bee78STejun Heo 	return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
680e22bee78STejun Heo }
681e22bee78STejun Heo 
682e22bee78STejun Heo /*
683e22bee78STejun Heo  * Wake up functions.
684e22bee78STejun Heo  */
685e22bee78STejun Heo 
6867e11629dSTejun Heo /* Return the first worker.  Safe with preemption disabled */
68763d95a91STejun Heo static struct worker *first_worker(struct worker_pool *pool)
6887e11629dSTejun Heo {
68963d95a91STejun Heo 	if (unlikely(list_empty(&pool->idle_list)))
6907e11629dSTejun Heo 		return NULL;
6917e11629dSTejun Heo 
69263d95a91STejun Heo 	return list_first_entry(&pool->idle_list, struct worker, entry);
6937e11629dSTejun Heo }
6947e11629dSTejun Heo 
6957e11629dSTejun Heo /**
6967e11629dSTejun Heo  * wake_up_worker - wake up an idle worker
69763d95a91STejun Heo  * @pool: worker pool to wake worker from
6987e11629dSTejun Heo  *
69963d95a91STejun Heo  * Wake up the first idle worker of @pool.
7007e11629dSTejun Heo  *
7017e11629dSTejun Heo  * CONTEXT:
702d565ed63STejun Heo  * spin_lock_irq(pool->lock).
7037e11629dSTejun Heo  */
70463d95a91STejun Heo static void wake_up_worker(struct worker_pool *pool)
7057e11629dSTejun Heo {
70663d95a91STejun Heo 	struct worker *worker = first_worker(pool);
7077e11629dSTejun Heo 
7087e11629dSTejun Heo 	if (likely(worker))
7097e11629dSTejun Heo 		wake_up_process(worker->task);
7107e11629dSTejun Heo }
7117e11629dSTejun Heo 
7124690c4abSTejun Heo /**
713e22bee78STejun Heo  * wq_worker_waking_up - a worker is waking up
714e22bee78STejun Heo  * @task: task waking up
715e22bee78STejun Heo  * @cpu: CPU @task is waking up to
716e22bee78STejun Heo  *
717e22bee78STejun Heo  * This function is called during try_to_wake_up() when a worker is
718e22bee78STejun Heo  * being awoken.
719e22bee78STejun Heo  *
720e22bee78STejun Heo  * CONTEXT:
721e22bee78STejun Heo  * spin_lock_irq(rq->lock)
722e22bee78STejun Heo  */
723d84ff051STejun Heo void wq_worker_waking_up(struct task_struct *task, int cpu)
724e22bee78STejun Heo {
725e22bee78STejun Heo 	struct worker *worker = kthread_data(task);
726e22bee78STejun Heo 
72736576000SJoonsoo Kim 	if (!(worker->flags & WORKER_NOT_RUNNING)) {
728ec22ca5eSTejun Heo 		WARN_ON_ONCE(worker->pool->cpu != cpu);
729e19e397aSTejun Heo 		atomic_inc(&worker->pool->nr_running);
730e22bee78STejun Heo 	}
73136576000SJoonsoo Kim }
732e22bee78STejun Heo 
733e22bee78STejun Heo /**
734e22bee78STejun Heo  * wq_worker_sleeping - a worker is going to sleep
735e22bee78STejun Heo  * @task: task going to sleep
736e22bee78STejun Heo  * @cpu: CPU in question, must be the current CPU number
737e22bee78STejun Heo  *
738e22bee78STejun Heo  * This function is called during schedule() when a busy worker is
739e22bee78STejun Heo  * going to sleep.  Worker on the same cpu can be woken up by
740e22bee78STejun Heo  * returning pointer to its task.
741e22bee78STejun Heo  *
742e22bee78STejun Heo  * CONTEXT:
743e22bee78STejun Heo  * spin_lock_irq(rq->lock)
744e22bee78STejun Heo  *
745e22bee78STejun Heo  * RETURNS:
746e22bee78STejun Heo  * Worker task on @cpu to wake up, %NULL if none.
747e22bee78STejun Heo  */
748d84ff051STejun Heo struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
749e22bee78STejun Heo {
750e22bee78STejun Heo 	struct worker *worker = kthread_data(task), *to_wakeup = NULL;
751111c225aSTejun Heo 	struct worker_pool *pool;
752e22bee78STejun Heo 
753111c225aSTejun Heo 	/*
754111c225aSTejun Heo 	 * Rescuers, which may not have all the fields set up like normal
755111c225aSTejun Heo 	 * workers, also reach here, let's not access anything before
756111c225aSTejun Heo 	 * checking NOT_RUNNING.
757111c225aSTejun Heo 	 */
7582d64672eSSteven Rostedt 	if (worker->flags & WORKER_NOT_RUNNING)
759e22bee78STejun Heo 		return NULL;
760e22bee78STejun Heo 
761111c225aSTejun Heo 	pool = worker->pool;
762111c225aSTejun Heo 
763e22bee78STejun Heo 	/* this can only happen on the local cpu */
7646183c009STejun Heo 	if (WARN_ON_ONCE(cpu != raw_smp_processor_id()))
7656183c009STejun Heo 		return NULL;
766e22bee78STejun Heo 
767e22bee78STejun Heo 	/*
768e22bee78STejun Heo 	 * The counterpart of the following dec_and_test, implied mb,
769e22bee78STejun Heo 	 * worklist not empty test sequence is in insert_work().
770e22bee78STejun Heo 	 * Please read comment there.
771e22bee78STejun Heo 	 *
772628c78e7STejun Heo 	 * NOT_RUNNING is clear.  This means that we're bound to and
773628c78e7STejun Heo 	 * running on the local cpu w/ rq lock held and preemption
774628c78e7STejun Heo 	 * disabled, which in turn means that none else could be
775d565ed63STejun Heo 	 * manipulating idle_list, so dereferencing idle_list without pool
776628c78e7STejun Heo 	 * lock is safe.
777e22bee78STejun Heo 	 */
778e19e397aSTejun Heo 	if (atomic_dec_and_test(&pool->nr_running) &&
779e19e397aSTejun Heo 	    !list_empty(&pool->worklist))
78063d95a91STejun Heo 		to_wakeup = first_worker(pool);
781e22bee78STejun Heo 	return to_wakeup ? to_wakeup->task : NULL;
782e22bee78STejun Heo }
783e22bee78STejun Heo 
784e22bee78STejun Heo /**
785e22bee78STejun Heo  * worker_set_flags - set worker flags and adjust nr_running accordingly
786cb444766STejun Heo  * @worker: self
787d302f017STejun Heo  * @flags: flags to set
788d302f017STejun Heo  * @wakeup: wakeup an idle worker if necessary
789d302f017STejun Heo  *
790e22bee78STejun Heo  * Set @flags in @worker->flags and adjust nr_running accordingly.  If
791e22bee78STejun Heo  * nr_running becomes zero and @wakeup is %true, an idle worker is
792e22bee78STejun Heo  * woken up.
793d302f017STejun Heo  *
794cb444766STejun Heo  * CONTEXT:
795d565ed63STejun Heo  * spin_lock_irq(pool->lock)
796d302f017STejun Heo  */
797d302f017STejun Heo static inline void worker_set_flags(struct worker *worker, unsigned int flags,
798d302f017STejun Heo 				    bool wakeup)
799d302f017STejun Heo {
800bd7bdd43STejun Heo 	struct worker_pool *pool = worker->pool;
801e22bee78STejun Heo 
802cb444766STejun Heo 	WARN_ON_ONCE(worker->task != current);
803cb444766STejun Heo 
804e22bee78STejun Heo 	/*
805e22bee78STejun Heo 	 * If transitioning into NOT_RUNNING, adjust nr_running and
806e22bee78STejun Heo 	 * wake up an idle worker as necessary if requested by
807e22bee78STejun Heo 	 * @wakeup.
808e22bee78STejun Heo 	 */
809e22bee78STejun Heo 	if ((flags & WORKER_NOT_RUNNING) &&
810e22bee78STejun Heo 	    !(worker->flags & WORKER_NOT_RUNNING)) {
811e22bee78STejun Heo 		if (wakeup) {
812e19e397aSTejun Heo 			if (atomic_dec_and_test(&pool->nr_running) &&
813bd7bdd43STejun Heo 			    !list_empty(&pool->worklist))
81463d95a91STejun Heo 				wake_up_worker(pool);
815e22bee78STejun Heo 		} else
816e19e397aSTejun Heo 			atomic_dec(&pool->nr_running);
817e22bee78STejun Heo 	}
818e22bee78STejun Heo 
819d302f017STejun Heo 	worker->flags |= flags;
820d302f017STejun Heo }
821d302f017STejun Heo 
822d302f017STejun Heo /**
823e22bee78STejun Heo  * worker_clr_flags - clear worker flags and adjust nr_running accordingly
824cb444766STejun Heo  * @worker: self
825d302f017STejun Heo  * @flags: flags to clear
826d302f017STejun Heo  *
827e22bee78STejun Heo  * Clear @flags in @worker->flags and adjust nr_running accordingly.
828d302f017STejun Heo  *
829cb444766STejun Heo  * CONTEXT:
830d565ed63STejun Heo  * spin_lock_irq(pool->lock)
831d302f017STejun Heo  */
832d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
833d302f017STejun Heo {
83463d95a91STejun Heo 	struct worker_pool *pool = worker->pool;
835e22bee78STejun Heo 	unsigned int oflags = worker->flags;
836e22bee78STejun Heo 
837cb444766STejun Heo 	WARN_ON_ONCE(worker->task != current);
838cb444766STejun Heo 
839d302f017STejun Heo 	worker->flags &= ~flags;
840e22bee78STejun Heo 
84142c025f3STejun Heo 	/*
84242c025f3STejun Heo 	 * If transitioning out of NOT_RUNNING, increment nr_running.  Note
84342c025f3STejun Heo 	 * that the nested NOT_RUNNING is not a noop.  NOT_RUNNING is mask
84442c025f3STejun Heo 	 * of multiple flags, not a single flag.
84542c025f3STejun Heo 	 */
846e22bee78STejun Heo 	if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
847e22bee78STejun Heo 		if (!(worker->flags & WORKER_NOT_RUNNING))
848e19e397aSTejun Heo 			atomic_inc(&pool->nr_running);
849d302f017STejun Heo }
850d302f017STejun Heo 
851d302f017STejun Heo /**
8528cca0eeaSTejun Heo  * find_worker_executing_work - find worker which is executing a work
853c9e7cf27STejun Heo  * @pool: pool of interest
8548cca0eeaSTejun Heo  * @work: work to find worker for
8558cca0eeaSTejun Heo  *
856c9e7cf27STejun Heo  * Find a worker which is executing @work on @pool by searching
857c9e7cf27STejun Heo  * @pool->busy_hash which is keyed by the address of @work.  For a worker
858a2c1c57bSTejun Heo  * to match, its current execution should match the address of @work and
859a2c1c57bSTejun Heo  * its work function.  This is to avoid unwanted dependency between
860a2c1c57bSTejun Heo  * unrelated work executions through a work item being recycled while still
861a2c1c57bSTejun Heo  * being executed.
862a2c1c57bSTejun Heo  *
863a2c1c57bSTejun Heo  * This is a bit tricky.  A work item may be freed once its execution
864a2c1c57bSTejun Heo  * starts and nothing prevents the freed area from being recycled for
865a2c1c57bSTejun Heo  * another work item.  If the same work item address ends up being reused
866a2c1c57bSTejun Heo  * before the original execution finishes, workqueue will identify the
867a2c1c57bSTejun Heo  * recycled work item as currently executing and make it wait until the
868a2c1c57bSTejun Heo  * current execution finishes, introducing an unwanted dependency.
869a2c1c57bSTejun Heo  *
870a2c1c57bSTejun Heo  * This function checks the work item address, work function and workqueue
871a2c1c57bSTejun Heo  * to avoid false positives.  Note that this isn't complete as one may
872a2c1c57bSTejun Heo  * construct a work function which can introduce dependency onto itself
873a2c1c57bSTejun Heo  * through a recycled work item.  Well, if somebody wants to shoot oneself
874a2c1c57bSTejun Heo  * in the foot that badly, there's only so much we can do, and if such
875a2c1c57bSTejun Heo  * deadlock actually occurs, it should be easy to locate the culprit work
876a2c1c57bSTejun Heo  * function.
8778cca0eeaSTejun Heo  *
8788cca0eeaSTejun Heo  * CONTEXT:
879d565ed63STejun Heo  * spin_lock_irq(pool->lock).
8808cca0eeaSTejun Heo  *
8818cca0eeaSTejun Heo  * RETURNS:
8828cca0eeaSTejun Heo  * Pointer to worker which is executing @work if found, NULL
8838cca0eeaSTejun Heo  * otherwise.
8848cca0eeaSTejun Heo  */
885c9e7cf27STejun Heo static struct worker *find_worker_executing_work(struct worker_pool *pool,
8868cca0eeaSTejun Heo 						 struct work_struct *work)
8878cca0eeaSTejun Heo {
88842f8570fSSasha Levin 	struct worker *worker;
88942f8570fSSasha Levin 
890b67bfe0dSSasha Levin 	hash_for_each_possible(pool->busy_hash, worker, hentry,
891a2c1c57bSTejun Heo 			       (unsigned long)work)
892a2c1c57bSTejun Heo 		if (worker->current_work == work &&
893a2c1c57bSTejun Heo 		    worker->current_func == work->func)
89442f8570fSSasha Levin 			return worker;
89542f8570fSSasha Levin 
89642f8570fSSasha Levin 	return NULL;
8978cca0eeaSTejun Heo }
8988cca0eeaSTejun Heo 
8998cca0eeaSTejun Heo /**
900bf4ede01STejun Heo  * move_linked_works - move linked works to a list
901bf4ede01STejun Heo  * @work: start of series of works to be scheduled
902bf4ede01STejun Heo  * @head: target list to append @work to
903bf4ede01STejun Heo  * @nextp: out paramter for nested worklist walking
904bf4ede01STejun Heo  *
905bf4ede01STejun Heo  * Schedule linked works starting from @work to @head.  Work series to
906bf4ede01STejun Heo  * be scheduled starts at @work and includes any consecutive work with
907bf4ede01STejun Heo  * WORK_STRUCT_LINKED set in its predecessor.
908bf4ede01STejun Heo  *
909bf4ede01STejun Heo  * If @nextp is not NULL, it's updated to point to the next work of
910bf4ede01STejun Heo  * the last scheduled work.  This allows move_linked_works() to be
911bf4ede01STejun Heo  * nested inside outer list_for_each_entry_safe().
912bf4ede01STejun Heo  *
913bf4ede01STejun Heo  * CONTEXT:
914d565ed63STejun Heo  * spin_lock_irq(pool->lock).
915bf4ede01STejun Heo  */
916bf4ede01STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head,
917bf4ede01STejun Heo 			      struct work_struct **nextp)
918bf4ede01STejun Heo {
919bf4ede01STejun Heo 	struct work_struct *n;
920bf4ede01STejun Heo 
921bf4ede01STejun Heo 	/*
922bf4ede01STejun Heo 	 * Linked worklist will always end before the end of the list,
923bf4ede01STejun Heo 	 * use NULL for list head.
924bf4ede01STejun Heo 	 */
925bf4ede01STejun Heo 	list_for_each_entry_safe_from(work, n, NULL, entry) {
926bf4ede01STejun Heo 		list_move_tail(&work->entry, head);
927bf4ede01STejun Heo 		if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
928bf4ede01STejun Heo 			break;
929bf4ede01STejun Heo 	}
930bf4ede01STejun Heo 
931bf4ede01STejun Heo 	/*
932bf4ede01STejun Heo 	 * If we're already inside safe list traversal and have moved
933bf4ede01STejun Heo 	 * multiple works to the scheduled queue, the next position
934bf4ede01STejun Heo 	 * needs to be updated.
935bf4ede01STejun Heo 	 */
936bf4ede01STejun Heo 	if (nextp)
937bf4ede01STejun Heo 		*nextp = n;
938bf4ede01STejun Heo }
939bf4ede01STejun Heo 
940112202d9STejun Heo static void pwq_activate_delayed_work(struct work_struct *work)
941bf4ede01STejun Heo {
942112202d9STejun Heo 	struct pool_workqueue *pwq = get_work_pwq(work);
943bf4ede01STejun Heo 
944bf4ede01STejun Heo 	trace_workqueue_activate_work(work);
945112202d9STejun Heo 	move_linked_works(work, &pwq->pool->worklist, NULL);
946bf4ede01STejun Heo 	__clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
947112202d9STejun Heo 	pwq->nr_active++;
948bf4ede01STejun Heo }
949bf4ede01STejun Heo 
950112202d9STejun Heo static void pwq_activate_first_delayed(struct pool_workqueue *pwq)
9513aa62497SLai Jiangshan {
952112202d9STejun Heo 	struct work_struct *work = list_first_entry(&pwq->delayed_works,
9533aa62497SLai Jiangshan 						    struct work_struct, entry);
9543aa62497SLai Jiangshan 
955112202d9STejun Heo 	pwq_activate_delayed_work(work);
9563aa62497SLai Jiangshan }
9573aa62497SLai Jiangshan 
958bf4ede01STejun Heo /**
959112202d9STejun Heo  * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight
960112202d9STejun Heo  * @pwq: pwq of interest
961bf4ede01STejun Heo  * @color: color of work which left the queue
962bf4ede01STejun Heo  *
963bf4ede01STejun Heo  * A work either has completed or is removed from pending queue,
964112202d9STejun Heo  * decrement nr_in_flight of its pwq and handle workqueue flushing.
965bf4ede01STejun Heo  *
966bf4ede01STejun Heo  * CONTEXT:
967d565ed63STejun Heo  * spin_lock_irq(pool->lock).
968bf4ede01STejun Heo  */
969112202d9STejun Heo static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color)
970bf4ede01STejun Heo {
971bf4ede01STejun Heo 	/* ignore uncolored works */
972bf4ede01STejun Heo 	if (color == WORK_NO_COLOR)
973bf4ede01STejun Heo 		return;
974bf4ede01STejun Heo 
975112202d9STejun Heo 	pwq->nr_in_flight[color]--;
976bf4ede01STejun Heo 
977112202d9STejun Heo 	pwq->nr_active--;
978112202d9STejun Heo 	if (!list_empty(&pwq->delayed_works)) {
979bf4ede01STejun Heo 		/* one down, submit a delayed one */
980112202d9STejun Heo 		if (pwq->nr_active < pwq->max_active)
981112202d9STejun Heo 			pwq_activate_first_delayed(pwq);
982bf4ede01STejun Heo 	}
983bf4ede01STejun Heo 
984bf4ede01STejun Heo 	/* is flush in progress and are we at the flushing tip? */
985112202d9STejun Heo 	if (likely(pwq->flush_color != color))
986bf4ede01STejun Heo 		return;
987bf4ede01STejun Heo 
988bf4ede01STejun Heo 	/* are there still in-flight works? */
989112202d9STejun Heo 	if (pwq->nr_in_flight[color])
990bf4ede01STejun Heo 		return;
991bf4ede01STejun Heo 
992112202d9STejun Heo 	/* this pwq is done, clear flush_color */
993112202d9STejun Heo 	pwq->flush_color = -1;
994bf4ede01STejun Heo 
995bf4ede01STejun Heo 	/*
996112202d9STejun Heo 	 * If this was the last pwq, wake up the first flusher.  It
997bf4ede01STejun Heo 	 * will handle the rest.
998bf4ede01STejun Heo 	 */
999112202d9STejun Heo 	if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))
1000112202d9STejun Heo 		complete(&pwq->wq->first_flusher->done);
1001bf4ede01STejun Heo }
1002bf4ede01STejun Heo 
100336e227d2STejun Heo /**
1004bbb68dfaSTejun Heo  * try_to_grab_pending - steal work item from worklist and disable irq
100536e227d2STejun Heo  * @work: work item to steal
100636e227d2STejun Heo  * @is_dwork: @work is a delayed_work
1007bbb68dfaSTejun Heo  * @flags: place to store irq state
100836e227d2STejun Heo  *
100936e227d2STejun Heo  * Try to grab PENDING bit of @work.  This function can handle @work in any
101036e227d2STejun Heo  * stable state - idle, on timer or on worklist.  Return values are
101136e227d2STejun Heo  *
101236e227d2STejun Heo  *  1		if @work was pending and we successfully stole PENDING
101336e227d2STejun Heo  *  0		if @work was idle and we claimed PENDING
101436e227d2STejun Heo  *  -EAGAIN	if PENDING couldn't be grabbed at the moment, safe to busy-retry
1015bbb68dfaSTejun Heo  *  -ENOENT	if someone else is canceling @work, this state may persist
1016bbb68dfaSTejun Heo  *		for arbitrarily long
101736e227d2STejun Heo  *
1018bbb68dfaSTejun Heo  * On >= 0 return, the caller owns @work's PENDING bit.  To avoid getting
1019e0aecdd8STejun Heo  * interrupted while holding PENDING and @work off queue, irq must be
1020e0aecdd8STejun Heo  * disabled on entry.  This, combined with delayed_work->timer being
1021e0aecdd8STejun Heo  * irqsafe, ensures that we return -EAGAIN for finite short period of time.
1022bbb68dfaSTejun Heo  *
1023bbb68dfaSTejun Heo  * On successful return, >= 0, irq is disabled and the caller is
1024bbb68dfaSTejun Heo  * responsible for releasing it using local_irq_restore(*@flags).
1025bbb68dfaSTejun Heo  *
1026e0aecdd8STejun Heo  * This function is safe to call from any context including IRQ handler.
1027bf4ede01STejun Heo  */
1028bbb68dfaSTejun Heo static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1029bbb68dfaSTejun Heo 			       unsigned long *flags)
1030bf4ede01STejun Heo {
1031d565ed63STejun Heo 	struct worker_pool *pool;
1032112202d9STejun Heo 	struct pool_workqueue *pwq;
1033bf4ede01STejun Heo 
1034bbb68dfaSTejun Heo 	local_irq_save(*flags);
1035bbb68dfaSTejun Heo 
103636e227d2STejun Heo 	/* try to steal the timer if it exists */
103736e227d2STejun Heo 	if (is_dwork) {
103836e227d2STejun Heo 		struct delayed_work *dwork = to_delayed_work(work);
103936e227d2STejun Heo 
1040e0aecdd8STejun Heo 		/*
1041e0aecdd8STejun Heo 		 * dwork->timer is irqsafe.  If del_timer() fails, it's
1042e0aecdd8STejun Heo 		 * guaranteed that the timer is not queued anywhere and not
1043e0aecdd8STejun Heo 		 * running on the local CPU.
1044e0aecdd8STejun Heo 		 */
104536e227d2STejun Heo 		if (likely(del_timer(&dwork->timer)))
104636e227d2STejun Heo 			return 1;
104736e227d2STejun Heo 	}
104836e227d2STejun Heo 
104936e227d2STejun Heo 	/* try to claim PENDING the normal way */
1050bf4ede01STejun Heo 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1051bf4ede01STejun Heo 		return 0;
1052bf4ede01STejun Heo 
1053bf4ede01STejun Heo 	/*
1054bf4ede01STejun Heo 	 * The queueing is in progress, or it is already queued. Try to
1055bf4ede01STejun Heo 	 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1056bf4ede01STejun Heo 	 */
1057d565ed63STejun Heo 	pool = get_work_pool(work);
1058d565ed63STejun Heo 	if (!pool)
1059bbb68dfaSTejun Heo 		goto fail;
1060bf4ede01STejun Heo 
1061d565ed63STejun Heo 	spin_lock(&pool->lock);
1062bf4ede01STejun Heo 	/*
1063112202d9STejun Heo 	 * work->data is guaranteed to point to pwq only while the work
1064112202d9STejun Heo 	 * item is queued on pwq->wq, and both updating work->data to point
1065112202d9STejun Heo 	 * to pwq on queueing and to pool on dequeueing are done under
1066112202d9STejun Heo 	 * pwq->pool->lock.  This in turn guarantees that, if work->data
1067112202d9STejun Heo 	 * points to pwq which is associated with a locked pool, the work
10680b3dae68SLai Jiangshan 	 * item is currently queued on that pool.
1069bf4ede01STejun Heo 	 */
1070112202d9STejun Heo 	pwq = get_work_pwq(work);
1071112202d9STejun Heo 	if (pwq && pwq->pool == pool) {
1072bf4ede01STejun Heo 		debug_work_deactivate(work);
10733aa62497SLai Jiangshan 
10743aa62497SLai Jiangshan 		/*
107516062836STejun Heo 		 * A delayed work item cannot be grabbed directly because
107616062836STejun Heo 		 * it might have linked NO_COLOR work items which, if left
1077112202d9STejun Heo 		 * on the delayed_list, will confuse pwq->nr_active
107816062836STejun Heo 		 * management later on and cause stall.  Make sure the work
107916062836STejun Heo 		 * item is activated before grabbing.
10803aa62497SLai Jiangshan 		 */
10813aa62497SLai Jiangshan 		if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
1082112202d9STejun Heo 			pwq_activate_delayed_work(work);
10833aa62497SLai Jiangshan 
1084bf4ede01STejun Heo 		list_del_init(&work->entry);
1085112202d9STejun Heo 		pwq_dec_nr_in_flight(get_work_pwq(work), get_work_color(work));
108636e227d2STejun Heo 
1087112202d9STejun Heo 		/* work->data points to pwq iff queued, point to pool */
10884468a00fSLai Jiangshan 		set_work_pool_and_keep_pending(work, pool->id);
10894468a00fSLai Jiangshan 
1090d565ed63STejun Heo 		spin_unlock(&pool->lock);
109136e227d2STejun Heo 		return 1;
1092bf4ede01STejun Heo 	}
1093d565ed63STejun Heo 	spin_unlock(&pool->lock);
1094bbb68dfaSTejun Heo fail:
1095bbb68dfaSTejun Heo 	local_irq_restore(*flags);
1096bbb68dfaSTejun Heo 	if (work_is_canceling(work))
1097bbb68dfaSTejun Heo 		return -ENOENT;
1098bbb68dfaSTejun Heo 	cpu_relax();
109936e227d2STejun Heo 	return -EAGAIN;
1100bf4ede01STejun Heo }
1101bf4ede01STejun Heo 
1102bf4ede01STejun Heo /**
1103706026c2STejun Heo  * insert_work - insert a work into a pool
1104112202d9STejun Heo  * @pwq: pwq @work belongs to
11054690c4abSTejun Heo  * @work: work to insert
11064690c4abSTejun Heo  * @head: insertion point
11074690c4abSTejun Heo  * @extra_flags: extra WORK_STRUCT_* flags to set
11084690c4abSTejun Heo  *
1109112202d9STejun Heo  * Insert @work which belongs to @pwq after @head.  @extra_flags is or'd to
1110706026c2STejun Heo  * work_struct flags.
11114690c4abSTejun Heo  *
11124690c4abSTejun Heo  * CONTEXT:
1113d565ed63STejun Heo  * spin_lock_irq(pool->lock).
1114365970a1SDavid Howells  */
1115112202d9STejun Heo static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
1116112202d9STejun Heo 			struct list_head *head, unsigned int extra_flags)
1117b89deed3SOleg Nesterov {
1118112202d9STejun Heo 	struct worker_pool *pool = pwq->pool;
1119e1d8aa9fSFrederic Weisbecker 
11204690c4abSTejun Heo 	/* we own @work, set data and link */
1121112202d9STejun Heo 	set_work_pwq(work, pwq, extra_flags);
11221a4d9b0aSOleg Nesterov 	list_add_tail(&work->entry, head);
1123e22bee78STejun Heo 
1124e22bee78STejun Heo 	/*
1125e22bee78STejun Heo 	 * Ensure either worker_sched_deactivated() sees the above
1126e22bee78STejun Heo 	 * list_add_tail() or we see zero nr_running to avoid workers
1127e22bee78STejun Heo 	 * lying around lazily while there are works to be processed.
1128e22bee78STejun Heo 	 */
1129e22bee78STejun Heo 	smp_mb();
1130e22bee78STejun Heo 
113163d95a91STejun Heo 	if (__need_more_worker(pool))
113263d95a91STejun Heo 		wake_up_worker(pool);
1133b89deed3SOleg Nesterov }
1134b89deed3SOleg Nesterov 
1135c8efcc25STejun Heo /*
1136c8efcc25STejun Heo  * Test whether @work is being queued from another work executing on the
11378d03ecfeSTejun Heo  * same workqueue.
1138c8efcc25STejun Heo  */
1139c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq)
1140c8efcc25STejun Heo {
1141c8efcc25STejun Heo 	struct worker *worker;
1142c8efcc25STejun Heo 
11438d03ecfeSTejun Heo 	worker = current_wq_worker();
1144c8efcc25STejun Heo 	/*
11458d03ecfeSTejun Heo 	 * Return %true iff I'm a worker execuing a work item on @wq.  If
11468d03ecfeSTejun Heo 	 * I'm @worker, it's safe to dereference it without locking.
1147c8efcc25STejun Heo 	 */
1148112202d9STejun Heo 	return worker && worker->current_pwq->wq == wq;
1149c8efcc25STejun Heo }
1150c8efcc25STejun Heo 
1151d84ff051STejun Heo static void __queue_work(int cpu, struct workqueue_struct *wq,
11521da177e4SLinus Torvalds 			 struct work_struct *work)
11531da177e4SLinus Torvalds {
1154112202d9STejun Heo 	struct pool_workqueue *pwq;
11551e19ffc6STejun Heo 	struct list_head *worklist;
11568a2e8e5dSTejun Heo 	unsigned int work_flags;
1157b75cac93SJoonsoo Kim 	unsigned int req_cpu = cpu;
11588930cabaSTejun Heo 
11598930cabaSTejun Heo 	/*
11608930cabaSTejun Heo 	 * While a work item is PENDING && off queue, a task trying to
11618930cabaSTejun Heo 	 * steal the PENDING will busy-loop waiting for it to either get
11628930cabaSTejun Heo 	 * queued or lose PENDING.  Grabbing PENDING and queueing should
11638930cabaSTejun Heo 	 * happen with IRQ disabled.
11648930cabaSTejun Heo 	 */
11658930cabaSTejun Heo 	WARN_ON_ONCE(!irqs_disabled());
11661da177e4SLinus Torvalds 
1167dc186ad7SThomas Gleixner 	debug_work_activate(work);
11681e19ffc6STejun Heo 
1169c8efcc25STejun Heo 	/* if dying, only works from the same workqueue are allowed */
11709c5a2ba7STejun Heo 	if (unlikely(wq->flags & WQ_DRAINING) &&
1171c8efcc25STejun Heo 	    WARN_ON_ONCE(!is_chained_work(wq)))
1172e41e704bSTejun Heo 		return;
1173e41e704bSTejun Heo 
1174112202d9STejun Heo 	/* determine the pwq to use */
1175c7fc77f7STejun Heo 	if (!(wq->flags & WQ_UNBOUND)) {
1176c9e7cf27STejun Heo 		struct worker_pool *last_pool;
1177c7fc77f7STejun Heo 
117857469821STejun Heo 		if (cpu == WORK_CPU_UNBOUND)
1179f3421797STejun Heo 			cpu = raw_smp_processor_id();
1180f3421797STejun Heo 
118118aa9effSTejun Heo 		/*
1182dbf2576eSTejun Heo 		 * It's multi cpu.  If @work was previously on a different
1183dbf2576eSTejun Heo 		 * cpu, it might still be running there, in which case the
1184dbf2576eSTejun Heo 		 * work needs to be queued on that cpu to guarantee
1185dbf2576eSTejun Heo 		 * non-reentrancy.
118618aa9effSTejun Heo 		 */
11877fb98ea7STejun Heo 		pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
1188c9e7cf27STejun Heo 		last_pool = get_work_pool(work);
1189dbf2576eSTejun Heo 
1190112202d9STejun Heo 		if (last_pool && last_pool != pwq->pool) {
119118aa9effSTejun Heo 			struct worker *worker;
119218aa9effSTejun Heo 
1193d565ed63STejun Heo 			spin_lock(&last_pool->lock);
119418aa9effSTejun Heo 
1195c9e7cf27STejun Heo 			worker = find_worker_executing_work(last_pool, work);
119618aa9effSTejun Heo 
1197112202d9STejun Heo 			if (worker && worker->current_pwq->wq == wq) {
11987fb98ea7STejun Heo 				pwq = per_cpu_ptr(wq->cpu_pwqs, last_pool->cpu);
11998594fadeSLai Jiangshan 			} else {
120018aa9effSTejun Heo 				/* meh... not running there, queue here */
1201d565ed63STejun Heo 				spin_unlock(&last_pool->lock);
1202112202d9STejun Heo 				spin_lock(&pwq->pool->lock);
120318aa9effSTejun Heo 			}
12048930cabaSTejun Heo 		} else {
1205112202d9STejun Heo 			spin_lock(&pwq->pool->lock);
12068930cabaSTejun Heo 		}
1207f3421797STejun Heo 	} else {
12087fb98ea7STejun Heo 		pwq = first_pwq(wq);
1209112202d9STejun Heo 		spin_lock(&pwq->pool->lock);
1210502ca9d8STejun Heo 	}
1211502ca9d8STejun Heo 
1212112202d9STejun Heo 	/* pwq determined, queue */
1213112202d9STejun Heo 	trace_workqueue_queue_work(req_cpu, pwq, work);
1214502ca9d8STejun Heo 
1215f5b2552bSDan Carpenter 	if (WARN_ON(!list_empty(&work->entry))) {
1216112202d9STejun Heo 		spin_unlock(&pwq->pool->lock);
1217f5b2552bSDan Carpenter 		return;
1218f5b2552bSDan Carpenter 	}
12191e19ffc6STejun Heo 
1220112202d9STejun Heo 	pwq->nr_in_flight[pwq->work_color]++;
1221112202d9STejun Heo 	work_flags = work_color_to_flags(pwq->work_color);
12221e19ffc6STejun Heo 
1223112202d9STejun Heo 	if (likely(pwq->nr_active < pwq->max_active)) {
1224cdadf009STejun Heo 		trace_workqueue_activate_work(work);
1225112202d9STejun Heo 		pwq->nr_active++;
1226112202d9STejun Heo 		worklist = &pwq->pool->worklist;
12278a2e8e5dSTejun Heo 	} else {
12288a2e8e5dSTejun Heo 		work_flags |= WORK_STRUCT_DELAYED;
1229112202d9STejun Heo 		worklist = &pwq->delayed_works;
12308a2e8e5dSTejun Heo 	}
12311e19ffc6STejun Heo 
1232112202d9STejun Heo 	insert_work(pwq, work, worklist, work_flags);
12331e19ffc6STejun Heo 
1234112202d9STejun Heo 	spin_unlock(&pwq->pool->lock);
12351da177e4SLinus Torvalds }
12361da177e4SLinus Torvalds 
12370fcb78c2SRolf Eike Beer /**
1238c1a220e7SZhang Rui  * queue_work_on - queue work on specific cpu
1239c1a220e7SZhang Rui  * @cpu: CPU number to execute work on
1240c1a220e7SZhang Rui  * @wq: workqueue to use
1241c1a220e7SZhang Rui  * @work: work to queue
1242c1a220e7SZhang Rui  *
1243d4283e93STejun Heo  * Returns %false if @work was already on a queue, %true otherwise.
1244c1a220e7SZhang Rui  *
1245c1a220e7SZhang Rui  * We queue the work to a specific CPU, the caller must ensure it
1246c1a220e7SZhang Rui  * can't go away.
1247c1a220e7SZhang Rui  */
1248d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq,
1249d4283e93STejun Heo 		   struct work_struct *work)
1250c1a220e7SZhang Rui {
1251d4283e93STejun Heo 	bool ret = false;
12528930cabaSTejun Heo 	unsigned long flags;
12538930cabaSTejun Heo 
12548930cabaSTejun Heo 	local_irq_save(flags);
1255c1a220e7SZhang Rui 
125622df02bbSTejun Heo 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
12574690c4abSTejun Heo 		__queue_work(cpu, wq, work);
1258d4283e93STejun Heo 		ret = true;
1259c1a220e7SZhang Rui 	}
12608930cabaSTejun Heo 
12618930cabaSTejun Heo 	local_irq_restore(flags);
1262c1a220e7SZhang Rui 	return ret;
1263c1a220e7SZhang Rui }
1264c1a220e7SZhang Rui EXPORT_SYMBOL_GPL(queue_work_on);
1265c1a220e7SZhang Rui 
12660a13c00eSTejun Heo /**
12670a13c00eSTejun Heo  * queue_work - queue work on a workqueue
12680a13c00eSTejun Heo  * @wq: workqueue to use
12690a13c00eSTejun Heo  * @work: work to queue
12700a13c00eSTejun Heo  *
1271d4283e93STejun Heo  * Returns %false if @work was already on a queue, %true otherwise.
12720a13c00eSTejun Heo  *
12730a13c00eSTejun Heo  * We queue the work to the CPU on which it was submitted, but if the CPU dies
12740a13c00eSTejun Heo  * it can be processed by another CPU.
12750a13c00eSTejun Heo  */
1276d4283e93STejun Heo bool queue_work(struct workqueue_struct *wq, struct work_struct *work)
12770a13c00eSTejun Heo {
127857469821STejun Heo 	return queue_work_on(WORK_CPU_UNBOUND, wq, work);
12790a13c00eSTejun Heo }
12800a13c00eSTejun Heo EXPORT_SYMBOL_GPL(queue_work);
12810a13c00eSTejun Heo 
1282d8e794dfSTejun Heo void delayed_work_timer_fn(unsigned long __data)
12831da177e4SLinus Torvalds {
128452bad64dSDavid Howells 	struct delayed_work *dwork = (struct delayed_work *)__data;
12851da177e4SLinus Torvalds 
1286e0aecdd8STejun Heo 	/* should have been called from irqsafe timer with irq already off */
128760c057bcSLai Jiangshan 	__queue_work(dwork->cpu, dwork->wq, &dwork->work);
12881da177e4SLinus Torvalds }
12891438ade5SKonstantin Khlebnikov EXPORT_SYMBOL(delayed_work_timer_fn);
12901da177e4SLinus Torvalds 
12917beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
129252bad64dSDavid Howells 				struct delayed_work *dwork, unsigned long delay)
12931da177e4SLinus Torvalds {
12947beb2edfSTejun Heo 	struct timer_list *timer = &dwork->timer;
12957beb2edfSTejun Heo 	struct work_struct *work = &dwork->work;
12961da177e4SLinus Torvalds 
12977beb2edfSTejun Heo 	WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
12987beb2edfSTejun Heo 		     timer->data != (unsigned long)dwork);
1299fc4b514fSTejun Heo 	WARN_ON_ONCE(timer_pending(timer));
1300fc4b514fSTejun Heo 	WARN_ON_ONCE(!list_empty(&work->entry));
13017beb2edfSTejun Heo 
13028852aac2STejun Heo 	/*
13038852aac2STejun Heo 	 * If @delay is 0, queue @dwork->work immediately.  This is for
13048852aac2STejun Heo 	 * both optimization and correctness.  The earliest @timer can
13058852aac2STejun Heo 	 * expire is on the closest next tick and delayed_work users depend
13068852aac2STejun Heo 	 * on that there's no such delay when @delay is 0.
13078852aac2STejun Heo 	 */
13088852aac2STejun Heo 	if (!delay) {
13098852aac2STejun Heo 		__queue_work(cpu, wq, &dwork->work);
13108852aac2STejun Heo 		return;
13118852aac2STejun Heo 	}
13128852aac2STejun Heo 
13137beb2edfSTejun Heo 	timer_stats_timer_set_start_info(&dwork->timer);
13147beb2edfSTejun Heo 
131560c057bcSLai Jiangshan 	dwork->wq = wq;
13161265057fSTejun Heo 	dwork->cpu = cpu;
13177beb2edfSTejun Heo 	timer->expires = jiffies + delay;
13187beb2edfSTejun Heo 
13197beb2edfSTejun Heo 	if (unlikely(cpu != WORK_CPU_UNBOUND))
13207beb2edfSTejun Heo 		add_timer_on(timer, cpu);
13217beb2edfSTejun Heo 	else
13227beb2edfSTejun Heo 		add_timer(timer);
13237beb2edfSTejun Heo }
13241da177e4SLinus Torvalds 
13250fcb78c2SRolf Eike Beer /**
13260fcb78c2SRolf Eike Beer  * queue_delayed_work_on - queue work on specific CPU after delay
13270fcb78c2SRolf Eike Beer  * @cpu: CPU number to execute work on
13280fcb78c2SRolf Eike Beer  * @wq: workqueue to use
1329af9997e4SRandy Dunlap  * @dwork: work to queue
13300fcb78c2SRolf Eike Beer  * @delay: number of jiffies to wait before queueing
13310fcb78c2SRolf Eike Beer  *
1332715f1300STejun Heo  * Returns %false if @work was already on a queue, %true otherwise.  If
1333715f1300STejun Heo  * @delay is zero and @dwork is idle, it will be scheduled for immediate
1334715f1300STejun Heo  * execution.
13350fcb78c2SRolf Eike Beer  */
1336d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
133752bad64dSDavid Howells 			   struct delayed_work *dwork, unsigned long delay)
13387a6bc1cdSVenkatesh Pallipadi {
133952bad64dSDavid Howells 	struct work_struct *work = &dwork->work;
1340d4283e93STejun Heo 	bool ret = false;
13418930cabaSTejun Heo 	unsigned long flags;
13428930cabaSTejun Heo 
13438930cabaSTejun Heo 	/* read the comment in __queue_work() */
13448930cabaSTejun Heo 	local_irq_save(flags);
13457a6bc1cdSVenkatesh Pallipadi 
134622df02bbSTejun Heo 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
13477beb2edfSTejun Heo 		__queue_delayed_work(cpu, wq, dwork, delay);
1348d4283e93STejun Heo 		ret = true;
13497a6bc1cdSVenkatesh Pallipadi 	}
13508930cabaSTejun Heo 
13518930cabaSTejun Heo 	local_irq_restore(flags);
13527a6bc1cdSVenkatesh Pallipadi 	return ret;
13537a6bc1cdSVenkatesh Pallipadi }
1354ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work_on);
13551da177e4SLinus Torvalds 
1356c8e55f36STejun Heo /**
13570a13c00eSTejun Heo  * queue_delayed_work - queue work on a workqueue after delay
13580a13c00eSTejun Heo  * @wq: workqueue to use
13590a13c00eSTejun Heo  * @dwork: delayable work to queue
13600a13c00eSTejun Heo  * @delay: number of jiffies to wait before queueing
13610a13c00eSTejun Heo  *
1362715f1300STejun Heo  * Equivalent to queue_delayed_work_on() but tries to use the local CPU.
13630a13c00eSTejun Heo  */
1364d4283e93STejun Heo bool queue_delayed_work(struct workqueue_struct *wq,
13650a13c00eSTejun Heo 			struct delayed_work *dwork, unsigned long delay)
13660a13c00eSTejun Heo {
136757469821STejun Heo 	return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
13680a13c00eSTejun Heo }
13690a13c00eSTejun Heo EXPORT_SYMBOL_GPL(queue_delayed_work);
13700a13c00eSTejun Heo 
13710a13c00eSTejun Heo /**
13728376fe22STejun Heo  * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
13738376fe22STejun Heo  * @cpu: CPU number to execute work on
13748376fe22STejun Heo  * @wq: workqueue to use
13758376fe22STejun Heo  * @dwork: work to queue
13768376fe22STejun Heo  * @delay: number of jiffies to wait before queueing
13778376fe22STejun Heo  *
13788376fe22STejun Heo  * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
13798376fe22STejun Heo  * modify @dwork's timer so that it expires after @delay.  If @delay is
13808376fe22STejun Heo  * zero, @work is guaranteed to be scheduled immediately regardless of its
13818376fe22STejun Heo  * current state.
13828376fe22STejun Heo  *
13838376fe22STejun Heo  * Returns %false if @dwork was idle and queued, %true if @dwork was
13848376fe22STejun Heo  * pending and its timer was modified.
13858376fe22STejun Heo  *
1386e0aecdd8STejun Heo  * This function is safe to call from any context including IRQ handler.
13878376fe22STejun Heo  * See try_to_grab_pending() for details.
13888376fe22STejun Heo  */
13898376fe22STejun Heo bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
13908376fe22STejun Heo 			 struct delayed_work *dwork, unsigned long delay)
13918376fe22STejun Heo {
13928376fe22STejun Heo 	unsigned long flags;
13938376fe22STejun Heo 	int ret;
13948376fe22STejun Heo 
13958376fe22STejun Heo 	do {
13968376fe22STejun Heo 		ret = try_to_grab_pending(&dwork->work, true, &flags);
13978376fe22STejun Heo 	} while (unlikely(ret == -EAGAIN));
13988376fe22STejun Heo 
13998376fe22STejun Heo 	if (likely(ret >= 0)) {
14008376fe22STejun Heo 		__queue_delayed_work(cpu, wq, dwork, delay);
14018376fe22STejun Heo 		local_irq_restore(flags);
14028376fe22STejun Heo 	}
14038376fe22STejun Heo 
14048376fe22STejun Heo 	/* -ENOENT from try_to_grab_pending() becomes %true */
14058376fe22STejun Heo 	return ret;
14068376fe22STejun Heo }
14078376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work_on);
14088376fe22STejun Heo 
14098376fe22STejun Heo /**
14108376fe22STejun Heo  * mod_delayed_work - modify delay of or queue a delayed work
14118376fe22STejun Heo  * @wq: workqueue to use
14128376fe22STejun Heo  * @dwork: work to queue
14138376fe22STejun Heo  * @delay: number of jiffies to wait before queueing
14148376fe22STejun Heo  *
14158376fe22STejun Heo  * mod_delayed_work_on() on local CPU.
14168376fe22STejun Heo  */
14178376fe22STejun Heo bool mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dwork,
14188376fe22STejun Heo 		      unsigned long delay)
14198376fe22STejun Heo {
14208376fe22STejun Heo 	return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
14218376fe22STejun Heo }
14228376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work);
14238376fe22STejun Heo 
14248376fe22STejun Heo /**
1425c8e55f36STejun Heo  * worker_enter_idle - enter idle state
1426c8e55f36STejun Heo  * @worker: worker which is entering idle state
1427c8e55f36STejun Heo  *
1428c8e55f36STejun Heo  * @worker is entering idle state.  Update stats and idle timer if
1429c8e55f36STejun Heo  * necessary.
1430c8e55f36STejun Heo  *
1431c8e55f36STejun Heo  * LOCKING:
1432d565ed63STejun Heo  * spin_lock_irq(pool->lock).
1433c8e55f36STejun Heo  */
1434c8e55f36STejun Heo static void worker_enter_idle(struct worker *worker)
14351da177e4SLinus Torvalds {
1436bd7bdd43STejun Heo 	struct worker_pool *pool = worker->pool;
1437c8e55f36STejun Heo 
14386183c009STejun Heo 	if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||
14396183c009STejun Heo 	    WARN_ON_ONCE(!list_empty(&worker->entry) &&
14406183c009STejun Heo 			 (worker->hentry.next || worker->hentry.pprev)))
14416183c009STejun Heo 		return;
1442c8e55f36STejun Heo 
1443cb444766STejun Heo 	/* can't use worker_set_flags(), also called from start_worker() */
1444cb444766STejun Heo 	worker->flags |= WORKER_IDLE;
1445bd7bdd43STejun Heo 	pool->nr_idle++;
1446e22bee78STejun Heo 	worker->last_active = jiffies;
1447c8e55f36STejun Heo 
1448c8e55f36STejun Heo 	/* idle_list is LIFO */
1449bd7bdd43STejun Heo 	list_add(&worker->entry, &pool->idle_list);
1450db7bccf4STejun Heo 
145163d95a91STejun Heo 	if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1452628c78e7STejun Heo 		mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
1453cb444766STejun Heo 
1454544ecf31STejun Heo 	/*
1455706026c2STejun Heo 	 * Sanity check nr_running.  Because wq_unbind_fn() releases
1456d565ed63STejun Heo 	 * pool->lock between setting %WORKER_UNBOUND and zapping
1457628c78e7STejun Heo 	 * nr_running, the warning may trigger spuriously.  Check iff
1458628c78e7STejun Heo 	 * unbind is not in progress.
1459544ecf31STejun Heo 	 */
146024647570STejun Heo 	WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
1461bd7bdd43STejun Heo 		     pool->nr_workers == pool->nr_idle &&
1462e19e397aSTejun Heo 		     atomic_read(&pool->nr_running));
1463c8e55f36STejun Heo }
1464c8e55f36STejun Heo 
1465c8e55f36STejun Heo /**
1466c8e55f36STejun Heo  * worker_leave_idle - leave idle state
1467c8e55f36STejun Heo  * @worker: worker which is leaving idle state
1468c8e55f36STejun Heo  *
1469c8e55f36STejun Heo  * @worker is leaving idle state.  Update stats.
1470c8e55f36STejun Heo  *
1471c8e55f36STejun Heo  * LOCKING:
1472d565ed63STejun Heo  * spin_lock_irq(pool->lock).
1473c8e55f36STejun Heo  */
1474c8e55f36STejun Heo static void worker_leave_idle(struct worker *worker)
1475c8e55f36STejun Heo {
1476bd7bdd43STejun Heo 	struct worker_pool *pool = worker->pool;
1477c8e55f36STejun Heo 
14786183c009STejun Heo 	if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))
14796183c009STejun Heo 		return;
1480d302f017STejun Heo 	worker_clr_flags(worker, WORKER_IDLE);
1481bd7bdd43STejun Heo 	pool->nr_idle--;
1482c8e55f36STejun Heo 	list_del_init(&worker->entry);
1483c8e55f36STejun Heo }
1484c8e55f36STejun Heo 
1485e22bee78STejun Heo /**
1486f36dc67bSLai Jiangshan  * worker_maybe_bind_and_lock - try to bind %current to worker_pool and lock it
1487f36dc67bSLai Jiangshan  * @pool: target worker_pool
1488f36dc67bSLai Jiangshan  *
1489f36dc67bSLai Jiangshan  * Bind %current to the cpu of @pool if it is associated and lock @pool.
1490e22bee78STejun Heo  *
1491e22bee78STejun Heo  * Works which are scheduled while the cpu is online must at least be
1492e22bee78STejun Heo  * scheduled to a worker which is bound to the cpu so that if they are
1493e22bee78STejun Heo  * flushed from cpu callbacks while cpu is going down, they are
1494e22bee78STejun Heo  * guaranteed to execute on the cpu.
1495e22bee78STejun Heo  *
1496f5faa077SLai Jiangshan  * This function is to be used by unbound workers and rescuers to bind
1497e22bee78STejun Heo  * themselves to the target cpu and may race with cpu going down or
1498e22bee78STejun Heo  * coming online.  kthread_bind() can't be used because it may put the
1499e22bee78STejun Heo  * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1500706026c2STejun Heo  * verbatim as it's best effort and blocking and pool may be
1501e22bee78STejun Heo  * [dis]associated in the meantime.
1502e22bee78STejun Heo  *
1503706026c2STejun Heo  * This function tries set_cpus_allowed() and locks pool and verifies the
150424647570STejun Heo  * binding against %POOL_DISASSOCIATED which is set during
1505f2d5a0eeSTejun Heo  * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1506f2d5a0eeSTejun Heo  * enters idle state or fetches works without dropping lock, it can
1507f2d5a0eeSTejun Heo  * guarantee the scheduling requirement described in the first paragraph.
1508e22bee78STejun Heo  *
1509e22bee78STejun Heo  * CONTEXT:
1510d565ed63STejun Heo  * Might sleep.  Called without any lock but returns with pool->lock
1511e22bee78STejun Heo  * held.
1512e22bee78STejun Heo  *
1513e22bee78STejun Heo  * RETURNS:
1514706026c2STejun Heo  * %true if the associated pool is online (@worker is successfully
1515e22bee78STejun Heo  * bound), %false if offline.
1516e22bee78STejun Heo  */
1517f36dc67bSLai Jiangshan static bool worker_maybe_bind_and_lock(struct worker_pool *pool)
1518d565ed63STejun Heo __acquires(&pool->lock)
1519e22bee78STejun Heo {
1520e22bee78STejun Heo 	while (true) {
1521e22bee78STejun Heo 		/*
1522e22bee78STejun Heo 		 * The following call may fail, succeed or succeed
1523e22bee78STejun Heo 		 * without actually migrating the task to the cpu if
1524e22bee78STejun Heo 		 * it races with cpu hotunplug operation.  Verify
152524647570STejun Heo 		 * against POOL_DISASSOCIATED.
1526e22bee78STejun Heo 		 */
152724647570STejun Heo 		if (!(pool->flags & POOL_DISASSOCIATED))
1528f5faa077SLai Jiangshan 			set_cpus_allowed_ptr(current, get_cpu_mask(pool->cpu));
1529e22bee78STejun Heo 
1530d565ed63STejun Heo 		spin_lock_irq(&pool->lock);
153124647570STejun Heo 		if (pool->flags & POOL_DISASSOCIATED)
1532e22bee78STejun Heo 			return false;
1533f5faa077SLai Jiangshan 		if (task_cpu(current) == pool->cpu &&
1534e22bee78STejun Heo 		    cpumask_equal(&current->cpus_allowed,
1535ec22ca5eSTejun Heo 				  get_cpu_mask(pool->cpu)))
1536e22bee78STejun Heo 			return true;
1537d565ed63STejun Heo 		spin_unlock_irq(&pool->lock);
1538e22bee78STejun Heo 
15395035b20fSTejun Heo 		/*
15405035b20fSTejun Heo 		 * We've raced with CPU hot[un]plug.  Give it a breather
15415035b20fSTejun Heo 		 * and retry migration.  cond_resched() is required here;
15425035b20fSTejun Heo 		 * otherwise, we might deadlock against cpu_stop trying to
15435035b20fSTejun Heo 		 * bring down the CPU on non-preemptive kernel.
15445035b20fSTejun Heo 		 */
1545e22bee78STejun Heo 		cpu_relax();
15465035b20fSTejun Heo 		cond_resched();
1547e22bee78STejun Heo 	}
1548e22bee78STejun Heo }
1549e22bee78STejun Heo 
1550e22bee78STejun Heo /*
1551ea1abd61SLai Jiangshan  * Rebind an idle @worker to its CPU.  worker_thread() will test
15525f7dabfdSLai Jiangshan  * list_empty(@worker->entry) before leaving idle and call this function.
155325511a47STejun Heo  */
155425511a47STejun Heo static void idle_worker_rebind(struct worker *worker)
155525511a47STejun Heo {
15565f7dabfdSLai Jiangshan 	/* CPU may go down again inbetween, clear UNBOUND only on success */
1557f36dc67bSLai Jiangshan 	if (worker_maybe_bind_and_lock(worker->pool))
15585f7dabfdSLai Jiangshan 		worker_clr_flags(worker, WORKER_UNBOUND);
155925511a47STejun Heo 
1560ea1abd61SLai Jiangshan 	/* rebind complete, become available again */
1561ea1abd61SLai Jiangshan 	list_add(&worker->entry, &worker->pool->idle_list);
1562d565ed63STejun Heo 	spin_unlock_irq(&worker->pool->lock);
156325511a47STejun Heo }
156425511a47STejun Heo 
156525511a47STejun Heo /*
156625511a47STejun Heo  * Function for @worker->rebind.work used to rebind unbound busy workers to
1567403c821dSTejun Heo  * the associated cpu which is coming back online.  This is scheduled by
1568403c821dSTejun Heo  * cpu up but can race with other cpu hotplug operations and may be
1569403c821dSTejun Heo  * executed twice without intervening cpu down.
1570e22bee78STejun Heo  */
157125511a47STejun Heo static void busy_worker_rebind_fn(struct work_struct *work)
1572e22bee78STejun Heo {
1573e22bee78STejun Heo 	struct worker *worker = container_of(work, struct worker, rebind_work);
1574e22bee78STejun Heo 
1575f36dc67bSLai Jiangshan 	if (worker_maybe_bind_and_lock(worker->pool))
1576eab6d828SLai Jiangshan 		worker_clr_flags(worker, WORKER_UNBOUND);
1577e22bee78STejun Heo 
1578d565ed63STejun Heo 	spin_unlock_irq(&worker->pool->lock);
1579e22bee78STejun Heo }
1580e22bee78STejun Heo 
158125511a47STejun Heo /**
158294cf58bbSTejun Heo  * rebind_workers - rebind all workers of a pool to the associated CPU
158394cf58bbSTejun Heo  * @pool: pool of interest
158425511a47STejun Heo  *
158594cf58bbSTejun Heo  * @pool->cpu is coming online.  Rebind all workers to the CPU.  Rebinding
158625511a47STejun Heo  * is different for idle and busy ones.
158725511a47STejun Heo  *
1588ea1abd61SLai Jiangshan  * Idle ones will be removed from the idle_list and woken up.  They will
1589ea1abd61SLai Jiangshan  * add themselves back after completing rebind.  This ensures that the
1590ea1abd61SLai Jiangshan  * idle_list doesn't contain any unbound workers when re-bound busy workers
1591ea1abd61SLai Jiangshan  * try to perform local wake-ups for concurrency management.
159225511a47STejun Heo  *
1593ea1abd61SLai Jiangshan  * Busy workers can rebind after they finish their current work items.
1594ea1abd61SLai Jiangshan  * Queueing the rebind work item at the head of the scheduled list is
1595ea1abd61SLai Jiangshan  * enough.  Note that nr_running will be properly bumped as busy workers
1596ea1abd61SLai Jiangshan  * rebind.
159725511a47STejun Heo  *
1598ea1abd61SLai Jiangshan  * On return, all non-manager workers are scheduled for rebind - see
1599ea1abd61SLai Jiangshan  * manage_workers() for the manager special case.  Any idle worker
1600ea1abd61SLai Jiangshan  * including the manager will not appear on @idle_list until rebind is
1601ea1abd61SLai Jiangshan  * complete, making local wake-ups safe.
160225511a47STejun Heo  */
160394cf58bbSTejun Heo static void rebind_workers(struct worker_pool *pool)
160425511a47STejun Heo {
1605ea1abd61SLai Jiangshan 	struct worker *worker, *n;
160625511a47STejun Heo 	int i;
160725511a47STejun Heo 
1608b2eb83d1SLai Jiangshan 	lockdep_assert_held(&pool->assoc_mutex);
1609d565ed63STejun Heo 	lockdep_assert_held(&pool->lock);
161025511a47STejun Heo 
16115f7dabfdSLai Jiangshan 	/* dequeue and kick idle ones */
1612ea1abd61SLai Jiangshan 	list_for_each_entry_safe(worker, n, &pool->idle_list, entry) {
1613ea1abd61SLai Jiangshan 		/*
161494cf58bbSTejun Heo 		 * idle workers should be off @pool->idle_list until rebind
161594cf58bbSTejun Heo 		 * is complete to avoid receiving premature local wake-ups.
1616ea1abd61SLai Jiangshan 		 */
1617ea1abd61SLai Jiangshan 		list_del_init(&worker->entry);
161825511a47STejun Heo 
161925511a47STejun Heo 		/*
162094cf58bbSTejun Heo 		 * worker_thread() will see the above dequeuing and call
162194cf58bbSTejun Heo 		 * idle_worker_rebind().
162225511a47STejun Heo 		 */
162325511a47STejun Heo 		wake_up_process(worker->task);
162425511a47STejun Heo 	}
162525511a47STejun Heo 
1626ea1abd61SLai Jiangshan 	/* rebind busy workers */
1627b67bfe0dSSasha Levin 	for_each_busy_worker(worker, i, pool) {
162825511a47STejun Heo 		struct work_struct *rebind_work = &worker->rebind_work;
1629e2b6a6d5SJoonsoo Kim 		struct workqueue_struct *wq;
163025511a47STejun Heo 
163125511a47STejun Heo 		if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
163225511a47STejun Heo 				     work_data_bits(rebind_work)))
163325511a47STejun Heo 			continue;
163425511a47STejun Heo 
163525511a47STejun Heo 		debug_work_activate(rebind_work);
163690beca5dSTejun Heo 
163790beca5dSTejun Heo 		/*
163894cf58bbSTejun Heo 		 * wq doesn't really matter but let's keep @worker->pool
1639112202d9STejun Heo 		 * and @pwq->pool consistent for sanity.
164090beca5dSTejun Heo 		 */
1641e34cdddbSTejun Heo 		if (std_worker_pool_pri(worker->pool))
1642e2b6a6d5SJoonsoo Kim 			wq = system_highpri_wq;
1643e2b6a6d5SJoonsoo Kim 		else
1644e2b6a6d5SJoonsoo Kim 			wq = system_wq;
1645ec58815aSTejun Heo 
16467fb98ea7STejun Heo 		insert_work(per_cpu_ptr(wq->cpu_pwqs, pool->cpu), rebind_work,
164725511a47STejun Heo 			    worker->scheduled.next,
164825511a47STejun Heo 			    work_color_to_flags(WORK_NO_COLOR));
1649ec58815aSTejun Heo 	}
165025511a47STejun Heo }
165125511a47STejun Heo 
1652c34056a3STejun Heo static struct worker *alloc_worker(void)
1653c34056a3STejun Heo {
1654c34056a3STejun Heo 	struct worker *worker;
1655c34056a3STejun Heo 
1656c34056a3STejun Heo 	worker = kzalloc(sizeof(*worker), GFP_KERNEL);
1657c8e55f36STejun Heo 	if (worker) {
1658c8e55f36STejun Heo 		INIT_LIST_HEAD(&worker->entry);
1659affee4b2STejun Heo 		INIT_LIST_HEAD(&worker->scheduled);
166025511a47STejun Heo 		INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn);
1661e22bee78STejun Heo 		/* on creation a worker is in !idle && prep state */
1662e22bee78STejun Heo 		worker->flags = WORKER_PREP;
1663c8e55f36STejun Heo 	}
1664c34056a3STejun Heo 	return worker;
1665c34056a3STejun Heo }
1666c34056a3STejun Heo 
1667c34056a3STejun Heo /**
1668c34056a3STejun Heo  * create_worker - create a new workqueue worker
166963d95a91STejun Heo  * @pool: pool the new worker will belong to
1670c34056a3STejun Heo  *
167163d95a91STejun Heo  * Create a new worker which is bound to @pool.  The returned worker
1672c34056a3STejun Heo  * can be started by calling start_worker() or destroyed using
1673c34056a3STejun Heo  * destroy_worker().
1674c34056a3STejun Heo  *
1675c34056a3STejun Heo  * CONTEXT:
1676c34056a3STejun Heo  * Might sleep.  Does GFP_KERNEL allocations.
1677c34056a3STejun Heo  *
1678c34056a3STejun Heo  * RETURNS:
1679c34056a3STejun Heo  * Pointer to the newly created worker.
1680c34056a3STejun Heo  */
1681bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool)
1682c34056a3STejun Heo {
1683e34cdddbSTejun Heo 	const char *pri = std_worker_pool_pri(pool) ? "H" : "";
1684c34056a3STejun Heo 	struct worker *worker = NULL;
1685f3421797STejun Heo 	int id = -1;
1686c34056a3STejun Heo 
1687d565ed63STejun Heo 	spin_lock_irq(&pool->lock);
1688bd7bdd43STejun Heo 	while (ida_get_new(&pool->worker_ida, &id)) {
1689d565ed63STejun Heo 		spin_unlock_irq(&pool->lock);
1690bd7bdd43STejun Heo 		if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
1691c34056a3STejun Heo 			goto fail;
1692d565ed63STejun Heo 		spin_lock_irq(&pool->lock);
1693c34056a3STejun Heo 	}
1694d565ed63STejun Heo 	spin_unlock_irq(&pool->lock);
1695c34056a3STejun Heo 
1696c34056a3STejun Heo 	worker = alloc_worker();
1697c34056a3STejun Heo 	if (!worker)
1698c34056a3STejun Heo 		goto fail;
1699c34056a3STejun Heo 
1700bd7bdd43STejun Heo 	worker->pool = pool;
1701c34056a3STejun Heo 	worker->id = id;
1702c34056a3STejun Heo 
1703ec22ca5eSTejun Heo 	if (pool->cpu != WORK_CPU_UNBOUND)
170494dcf29aSEric Dumazet 		worker->task = kthread_create_on_node(worker_thread,
1705ec22ca5eSTejun Heo 					worker, cpu_to_node(pool->cpu),
1706d84ff051STejun Heo 					"kworker/%d:%d%s", pool->cpu, id, pri);
1707f3421797STejun Heo 	else
1708f3421797STejun Heo 		worker->task = kthread_create(worker_thread, worker,
17093270476aSTejun Heo 					      "kworker/u:%d%s", id, pri);
1710c34056a3STejun Heo 	if (IS_ERR(worker->task))
1711c34056a3STejun Heo 		goto fail;
1712c34056a3STejun Heo 
1713e34cdddbSTejun Heo 	if (std_worker_pool_pri(pool))
17143270476aSTejun Heo 		set_user_nice(worker->task, HIGHPRI_NICE_LEVEL);
17153270476aSTejun Heo 
1716db7bccf4STejun Heo 	/*
1717bc2ae0f5STejun Heo 	 * Determine CPU binding of the new worker depending on
171824647570STejun Heo 	 * %POOL_DISASSOCIATED.  The caller is responsible for ensuring the
1719bc2ae0f5STejun Heo 	 * flag remains stable across this function.  See the comments
1720bc2ae0f5STejun Heo 	 * above the flag definition for details.
1721bc2ae0f5STejun Heo 	 *
1722bc2ae0f5STejun Heo 	 * As an unbound worker may later become a regular one if CPU comes
1723bc2ae0f5STejun Heo 	 * online, make sure every worker has %PF_THREAD_BOUND set.
1724db7bccf4STejun Heo 	 */
172524647570STejun Heo 	if (!(pool->flags & POOL_DISASSOCIATED)) {
1726ec22ca5eSTejun Heo 		kthread_bind(worker->task, pool->cpu);
1727bc2ae0f5STejun Heo 	} else {
1728db7bccf4STejun Heo 		worker->task->flags |= PF_THREAD_BOUND;
1729f3421797STejun Heo 		worker->flags |= WORKER_UNBOUND;
1730f3421797STejun Heo 	}
1731c34056a3STejun Heo 
1732c34056a3STejun Heo 	return worker;
1733c34056a3STejun Heo fail:
1734c34056a3STejun Heo 	if (id >= 0) {
1735d565ed63STejun Heo 		spin_lock_irq(&pool->lock);
1736bd7bdd43STejun Heo 		ida_remove(&pool->worker_ida, id);
1737d565ed63STejun Heo 		spin_unlock_irq(&pool->lock);
1738c34056a3STejun Heo 	}
1739c34056a3STejun Heo 	kfree(worker);
1740c34056a3STejun Heo 	return NULL;
1741c34056a3STejun Heo }
1742c34056a3STejun Heo 
1743c34056a3STejun Heo /**
1744c34056a3STejun Heo  * start_worker - start a newly created worker
1745c34056a3STejun Heo  * @worker: worker to start
1746c34056a3STejun Heo  *
1747706026c2STejun Heo  * Make the pool aware of @worker and start it.
1748c34056a3STejun Heo  *
1749c34056a3STejun Heo  * CONTEXT:
1750d565ed63STejun Heo  * spin_lock_irq(pool->lock).
1751c34056a3STejun Heo  */
1752c34056a3STejun Heo static void start_worker(struct worker *worker)
1753c34056a3STejun Heo {
1754cb444766STejun Heo 	worker->flags |= WORKER_STARTED;
1755bd7bdd43STejun Heo 	worker->pool->nr_workers++;
1756c8e55f36STejun Heo 	worker_enter_idle(worker);
1757c34056a3STejun Heo 	wake_up_process(worker->task);
1758c34056a3STejun Heo }
1759c34056a3STejun Heo 
1760c34056a3STejun Heo /**
1761c34056a3STejun Heo  * destroy_worker - destroy a workqueue worker
1762c34056a3STejun Heo  * @worker: worker to be destroyed
1763c34056a3STejun Heo  *
1764706026c2STejun Heo  * Destroy @worker and adjust @pool stats accordingly.
1765c8e55f36STejun Heo  *
1766c8e55f36STejun Heo  * CONTEXT:
1767d565ed63STejun Heo  * spin_lock_irq(pool->lock) which is released and regrabbed.
1768c34056a3STejun Heo  */
1769c34056a3STejun Heo static void destroy_worker(struct worker *worker)
1770c34056a3STejun Heo {
1771bd7bdd43STejun Heo 	struct worker_pool *pool = worker->pool;
1772c34056a3STejun Heo 	int id = worker->id;
1773c34056a3STejun Heo 
1774c34056a3STejun Heo 	/* sanity check frenzy */
17756183c009STejun Heo 	if (WARN_ON(worker->current_work) ||
17766183c009STejun Heo 	    WARN_ON(!list_empty(&worker->scheduled)))
17776183c009STejun Heo 		return;
1778c34056a3STejun Heo 
1779c8e55f36STejun Heo 	if (worker->flags & WORKER_STARTED)
1780bd7bdd43STejun Heo 		pool->nr_workers--;
1781c8e55f36STejun Heo 	if (worker->flags & WORKER_IDLE)
1782bd7bdd43STejun Heo 		pool->nr_idle--;
1783c8e55f36STejun Heo 
1784c8e55f36STejun Heo 	list_del_init(&worker->entry);
1785cb444766STejun Heo 	worker->flags |= WORKER_DIE;
1786c8e55f36STejun Heo 
1787d565ed63STejun Heo 	spin_unlock_irq(&pool->lock);
1788c8e55f36STejun Heo 
1789c34056a3STejun Heo 	kthread_stop(worker->task);
1790c34056a3STejun Heo 	kfree(worker);
1791c34056a3STejun Heo 
1792d565ed63STejun Heo 	spin_lock_irq(&pool->lock);
1793bd7bdd43STejun Heo 	ida_remove(&pool->worker_ida, id);
1794c34056a3STejun Heo }
1795c34056a3STejun Heo 
179663d95a91STejun Heo static void idle_worker_timeout(unsigned long __pool)
1797e22bee78STejun Heo {
179863d95a91STejun Heo 	struct worker_pool *pool = (void *)__pool;
1799e22bee78STejun Heo 
1800d565ed63STejun Heo 	spin_lock_irq(&pool->lock);
1801e22bee78STejun Heo 
180263d95a91STejun Heo 	if (too_many_workers(pool)) {
1803e22bee78STejun Heo 		struct worker *worker;
1804e22bee78STejun Heo 		unsigned long expires;
1805e22bee78STejun Heo 
1806e22bee78STejun Heo 		/* idle_list is kept in LIFO order, check the last one */
180763d95a91STejun Heo 		worker = list_entry(pool->idle_list.prev, struct worker, entry);
1808e22bee78STejun Heo 		expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1809e22bee78STejun Heo 
1810e22bee78STejun Heo 		if (time_before(jiffies, expires))
181163d95a91STejun Heo 			mod_timer(&pool->idle_timer, expires);
1812e22bee78STejun Heo 		else {
1813e22bee78STejun Heo 			/* it's been idle for too long, wake up manager */
181411ebea50STejun Heo 			pool->flags |= POOL_MANAGE_WORKERS;
181563d95a91STejun Heo 			wake_up_worker(pool);
1816e22bee78STejun Heo 		}
1817e22bee78STejun Heo 	}
1818e22bee78STejun Heo 
1819d565ed63STejun Heo 	spin_unlock_irq(&pool->lock);
1820e22bee78STejun Heo }
1821e22bee78STejun Heo 
1822493a1724STejun Heo static void send_mayday(struct work_struct *work)
1823e22bee78STejun Heo {
1824112202d9STejun Heo 	struct pool_workqueue *pwq = get_work_pwq(work);
1825112202d9STejun Heo 	struct workqueue_struct *wq = pwq->wq;
1826493a1724STejun Heo 
1827493a1724STejun Heo 	lockdep_assert_held(&workqueue_lock);
1828e22bee78STejun Heo 
1829e22bee78STejun Heo 	if (!(wq->flags & WQ_RESCUER))
1830493a1724STejun Heo 		return;
1831e22bee78STejun Heo 
1832e22bee78STejun Heo 	/* mayday mayday mayday */
1833493a1724STejun Heo 	if (list_empty(&pwq->mayday_node)) {
1834493a1724STejun Heo 		list_add_tail(&pwq->mayday_node, &wq->maydays);
1835e22bee78STejun Heo 		wake_up_process(wq->rescuer->task);
1836493a1724STejun Heo 	}
1837e22bee78STejun Heo }
1838e22bee78STejun Heo 
1839706026c2STejun Heo static void pool_mayday_timeout(unsigned long __pool)
1840e22bee78STejun Heo {
184163d95a91STejun Heo 	struct worker_pool *pool = (void *)__pool;
1842e22bee78STejun Heo 	struct work_struct *work;
1843e22bee78STejun Heo 
1844493a1724STejun Heo 	spin_lock_irq(&workqueue_lock);		/* for wq->maydays */
1845493a1724STejun Heo 	spin_lock(&pool->lock);
1846e22bee78STejun Heo 
184763d95a91STejun Heo 	if (need_to_create_worker(pool)) {
1848e22bee78STejun Heo 		/*
1849e22bee78STejun Heo 		 * We've been trying to create a new worker but
1850e22bee78STejun Heo 		 * haven't been successful.  We might be hitting an
1851e22bee78STejun Heo 		 * allocation deadlock.  Send distress signals to
1852e22bee78STejun Heo 		 * rescuers.
1853e22bee78STejun Heo 		 */
185463d95a91STejun Heo 		list_for_each_entry(work, &pool->worklist, entry)
1855e22bee78STejun Heo 			send_mayday(work);
1856e22bee78STejun Heo 	}
1857e22bee78STejun Heo 
1858493a1724STejun Heo 	spin_unlock(&pool->lock);
1859493a1724STejun Heo 	spin_unlock_irq(&workqueue_lock);
1860e22bee78STejun Heo 
186163d95a91STejun Heo 	mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
1862e22bee78STejun Heo }
1863e22bee78STejun Heo 
1864e22bee78STejun Heo /**
1865e22bee78STejun Heo  * maybe_create_worker - create a new worker if necessary
186663d95a91STejun Heo  * @pool: pool to create a new worker for
1867e22bee78STejun Heo  *
186863d95a91STejun Heo  * Create a new worker for @pool if necessary.  @pool is guaranteed to
1869e22bee78STejun Heo  * have at least one idle worker on return from this function.  If
1870e22bee78STejun Heo  * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
187163d95a91STejun Heo  * sent to all rescuers with works scheduled on @pool to resolve
1872e22bee78STejun Heo  * possible allocation deadlock.
1873e22bee78STejun Heo  *
1874e22bee78STejun Heo  * On return, need_to_create_worker() is guaranteed to be false and
1875e22bee78STejun Heo  * may_start_working() true.
1876e22bee78STejun Heo  *
1877e22bee78STejun Heo  * LOCKING:
1878d565ed63STejun Heo  * spin_lock_irq(pool->lock) which may be released and regrabbed
1879e22bee78STejun Heo  * multiple times.  Does GFP_KERNEL allocations.  Called only from
1880e22bee78STejun Heo  * manager.
1881e22bee78STejun Heo  *
1882e22bee78STejun Heo  * RETURNS:
1883d565ed63STejun Heo  * false if no action was taken and pool->lock stayed locked, true
1884e22bee78STejun Heo  * otherwise.
1885e22bee78STejun Heo  */
188663d95a91STejun Heo static bool maybe_create_worker(struct worker_pool *pool)
1887d565ed63STejun Heo __releases(&pool->lock)
1888d565ed63STejun Heo __acquires(&pool->lock)
1889e22bee78STejun Heo {
189063d95a91STejun Heo 	if (!need_to_create_worker(pool))
1891e22bee78STejun Heo 		return false;
1892e22bee78STejun Heo restart:
1893d565ed63STejun Heo 	spin_unlock_irq(&pool->lock);
18949f9c2364STejun Heo 
1895e22bee78STejun Heo 	/* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
189663d95a91STejun Heo 	mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
1897e22bee78STejun Heo 
1898e22bee78STejun Heo 	while (true) {
1899e22bee78STejun Heo 		struct worker *worker;
1900e22bee78STejun Heo 
1901bc2ae0f5STejun Heo 		worker = create_worker(pool);
1902e22bee78STejun Heo 		if (worker) {
190363d95a91STejun Heo 			del_timer_sync(&pool->mayday_timer);
1904d565ed63STejun Heo 			spin_lock_irq(&pool->lock);
1905e22bee78STejun Heo 			start_worker(worker);
19066183c009STejun Heo 			if (WARN_ON_ONCE(need_to_create_worker(pool)))
19076183c009STejun Heo 				goto restart;
1908e22bee78STejun Heo 			return true;
1909e22bee78STejun Heo 		}
1910e22bee78STejun Heo 
191163d95a91STejun Heo 		if (!need_to_create_worker(pool))
1912e22bee78STejun Heo 			break;
1913e22bee78STejun Heo 
1914e22bee78STejun Heo 		__set_current_state(TASK_INTERRUPTIBLE);
1915e22bee78STejun Heo 		schedule_timeout(CREATE_COOLDOWN);
19169f9c2364STejun Heo 
191763d95a91STejun Heo 		if (!need_to_create_worker(pool))
1918e22bee78STejun Heo 			break;
1919e22bee78STejun Heo 	}
1920e22bee78STejun Heo 
192163d95a91STejun Heo 	del_timer_sync(&pool->mayday_timer);
1922d565ed63STejun Heo 	spin_lock_irq(&pool->lock);
192363d95a91STejun Heo 	if (need_to_create_worker(pool))
1924e22bee78STejun Heo 		goto restart;
1925e22bee78STejun Heo 	return true;
1926e22bee78STejun Heo }
1927e22bee78STejun Heo 
1928e22bee78STejun Heo /**
1929e22bee78STejun Heo  * maybe_destroy_worker - destroy workers which have been idle for a while
193063d95a91STejun Heo  * @pool: pool to destroy workers for
1931e22bee78STejun Heo  *
193263d95a91STejun Heo  * Destroy @pool workers which have been idle for longer than
1933e22bee78STejun Heo  * IDLE_WORKER_TIMEOUT.
1934e22bee78STejun Heo  *
1935e22bee78STejun Heo  * LOCKING:
1936d565ed63STejun Heo  * spin_lock_irq(pool->lock) which may be released and regrabbed
1937e22bee78STejun Heo  * multiple times.  Called only from manager.
1938e22bee78STejun Heo  *
1939e22bee78STejun Heo  * RETURNS:
1940d565ed63STejun Heo  * false if no action was taken and pool->lock stayed locked, true
1941e22bee78STejun Heo  * otherwise.
1942e22bee78STejun Heo  */
194363d95a91STejun Heo static bool maybe_destroy_workers(struct worker_pool *pool)
1944e22bee78STejun Heo {
1945e22bee78STejun Heo 	bool ret = false;
1946e22bee78STejun Heo 
194763d95a91STejun Heo 	while (too_many_workers(pool)) {
1948e22bee78STejun Heo 		struct worker *worker;
1949e22bee78STejun Heo 		unsigned long expires;
1950e22bee78STejun Heo 
195163d95a91STejun Heo 		worker = list_entry(pool->idle_list.prev, struct worker, entry);
1952e22bee78STejun Heo 		expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1953e22bee78STejun Heo 
1954e22bee78STejun Heo 		if (time_before(jiffies, expires)) {
195563d95a91STejun Heo 			mod_timer(&pool->idle_timer, expires);
1956e22bee78STejun Heo 			break;
1957e22bee78STejun Heo 		}
1958e22bee78STejun Heo 
1959e22bee78STejun Heo 		destroy_worker(worker);
1960e22bee78STejun Heo 		ret = true;
1961e22bee78STejun Heo 	}
1962e22bee78STejun Heo 
1963e22bee78STejun Heo 	return ret;
1964e22bee78STejun Heo }
1965e22bee78STejun Heo 
1966e22bee78STejun Heo /**
1967e22bee78STejun Heo  * manage_workers - manage worker pool
1968e22bee78STejun Heo  * @worker: self
1969e22bee78STejun Heo  *
1970706026c2STejun Heo  * Assume the manager role and manage the worker pool @worker belongs
1971e22bee78STejun Heo  * to.  At any given time, there can be only zero or one manager per
1972706026c2STejun Heo  * pool.  The exclusion is handled automatically by this function.
1973e22bee78STejun Heo  *
1974e22bee78STejun Heo  * The caller can safely start processing works on false return.  On
1975e22bee78STejun Heo  * true return, it's guaranteed that need_to_create_worker() is false
1976e22bee78STejun Heo  * and may_start_working() is true.
1977e22bee78STejun Heo  *
1978e22bee78STejun Heo  * CONTEXT:
1979d565ed63STejun Heo  * spin_lock_irq(pool->lock) which may be released and regrabbed
1980e22bee78STejun Heo  * multiple times.  Does GFP_KERNEL allocations.
1981e22bee78STejun Heo  *
1982e22bee78STejun Heo  * RETURNS:
1983d565ed63STejun Heo  * spin_lock_irq(pool->lock) which may be released and regrabbed
1984d565ed63STejun Heo  * multiple times.  Does GFP_KERNEL allocations.
1985e22bee78STejun Heo  */
1986e22bee78STejun Heo static bool manage_workers(struct worker *worker)
1987e22bee78STejun Heo {
198863d95a91STejun Heo 	struct worker_pool *pool = worker->pool;
1989e22bee78STejun Heo 	bool ret = false;
1990e22bee78STejun Heo 
1991ee378aa4SLai Jiangshan 	if (pool->flags & POOL_MANAGING_WORKERS)
1992e22bee78STejun Heo 		return ret;
1993e22bee78STejun Heo 
1994552a37e9SLai Jiangshan 	pool->flags |= POOL_MANAGING_WORKERS;
1995ee378aa4SLai Jiangshan 
1996ee378aa4SLai Jiangshan 	/*
1997ee378aa4SLai Jiangshan 	 * To simplify both worker management and CPU hotplug, hold off
1998ee378aa4SLai Jiangshan 	 * management while hotplug is in progress.  CPU hotplug path can't
1999ee378aa4SLai Jiangshan 	 * grab %POOL_MANAGING_WORKERS to achieve this because that can
2000ee378aa4SLai Jiangshan 	 * lead to idle worker depletion (all become busy thinking someone
2001ee378aa4SLai Jiangshan 	 * else is managing) which in turn can result in deadlock under
2002b2eb83d1SLai Jiangshan 	 * extreme circumstances.  Use @pool->assoc_mutex to synchronize
2003ee378aa4SLai Jiangshan 	 * manager against CPU hotplug.
2004ee378aa4SLai Jiangshan 	 *
2005b2eb83d1SLai Jiangshan 	 * assoc_mutex would always be free unless CPU hotplug is in
2006d565ed63STejun Heo 	 * progress.  trylock first without dropping @pool->lock.
2007ee378aa4SLai Jiangshan 	 */
2008b2eb83d1SLai Jiangshan 	if (unlikely(!mutex_trylock(&pool->assoc_mutex))) {
2009d565ed63STejun Heo 		spin_unlock_irq(&pool->lock);
2010b2eb83d1SLai Jiangshan 		mutex_lock(&pool->assoc_mutex);
2011ee378aa4SLai Jiangshan 		/*
2012ee378aa4SLai Jiangshan 		 * CPU hotplug could have happened while we were waiting
2013b2eb83d1SLai Jiangshan 		 * for assoc_mutex.  Hotplug itself can't handle us
2014ee378aa4SLai Jiangshan 		 * because manager isn't either on idle or busy list, and
2015706026c2STejun Heo 		 * @pool's state and ours could have deviated.
2016ee378aa4SLai Jiangshan 		 *
2017b2eb83d1SLai Jiangshan 		 * As hotplug is now excluded via assoc_mutex, we can
2018ee378aa4SLai Jiangshan 		 * simply try to bind.  It will succeed or fail depending
2019706026c2STejun Heo 		 * on @pool's current state.  Try it and adjust
2020ee378aa4SLai Jiangshan 		 * %WORKER_UNBOUND accordingly.
2021ee378aa4SLai Jiangshan 		 */
2022f36dc67bSLai Jiangshan 		if (worker_maybe_bind_and_lock(pool))
2023ee378aa4SLai Jiangshan 			worker->flags &= ~WORKER_UNBOUND;
2024ee378aa4SLai Jiangshan 		else
2025ee378aa4SLai Jiangshan 			worker->flags |= WORKER_UNBOUND;
2026ee378aa4SLai Jiangshan 
2027ee378aa4SLai Jiangshan 		ret = true;
2028ee378aa4SLai Jiangshan 	}
2029ee378aa4SLai Jiangshan 
203011ebea50STejun Heo 	pool->flags &= ~POOL_MANAGE_WORKERS;
2031e22bee78STejun Heo 
2032e22bee78STejun Heo 	/*
2033e22bee78STejun Heo 	 * Destroy and then create so that may_start_working() is true
2034e22bee78STejun Heo 	 * on return.
2035e22bee78STejun Heo 	 */
203663d95a91STejun Heo 	ret |= maybe_destroy_workers(pool);
203763d95a91STejun Heo 	ret |= maybe_create_worker(pool);
2038e22bee78STejun Heo 
2039552a37e9SLai Jiangshan 	pool->flags &= ~POOL_MANAGING_WORKERS;
2040b2eb83d1SLai Jiangshan 	mutex_unlock(&pool->assoc_mutex);
2041e22bee78STejun Heo 	return ret;
2042e22bee78STejun Heo }
2043e22bee78STejun Heo 
2044a62428c0STejun Heo /**
2045a62428c0STejun Heo  * process_one_work - process single work
2046c34056a3STejun Heo  * @worker: self
2047a62428c0STejun Heo  * @work: work to process
2048a62428c0STejun Heo  *
2049a62428c0STejun Heo  * Process @work.  This function contains all the logics necessary to
2050a62428c0STejun Heo  * process a single work including synchronization against and
2051a62428c0STejun Heo  * interaction with other workers on the same cpu, queueing and
2052a62428c0STejun Heo  * flushing.  As long as context requirement is met, any worker can
2053a62428c0STejun Heo  * call this function to process a work.
2054a62428c0STejun Heo  *
2055a62428c0STejun Heo  * CONTEXT:
2056d565ed63STejun Heo  * spin_lock_irq(pool->lock) which is released and regrabbed.
2057a62428c0STejun Heo  */
2058c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work)
2059d565ed63STejun Heo __releases(&pool->lock)
2060d565ed63STejun Heo __acquires(&pool->lock)
20611da177e4SLinus Torvalds {
2062112202d9STejun Heo 	struct pool_workqueue *pwq = get_work_pwq(work);
2063bd7bdd43STejun Heo 	struct worker_pool *pool = worker->pool;
2064112202d9STejun Heo 	bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE;
206573f53c4aSTejun Heo 	int work_color;
20667e11629dSTejun Heo 	struct worker *collision;
20674e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
20684e6045f1SJohannes Berg 	/*
2069a62428c0STejun Heo 	 * It is permissible to free the struct work_struct from
2070a62428c0STejun Heo 	 * inside the function that is called from it, this we need to
2071a62428c0STejun Heo 	 * take into account for lockdep too.  To avoid bogus "held
2072a62428c0STejun Heo 	 * lock freed" warnings as well as problems when looking into
2073a62428c0STejun Heo 	 * work->lockdep_map, make a copy and use that here.
20744e6045f1SJohannes Berg 	 */
20754d82a1deSPeter Zijlstra 	struct lockdep_map lockdep_map;
20764d82a1deSPeter Zijlstra 
20774d82a1deSPeter Zijlstra 	lockdep_copy_map(&lockdep_map, &work->lockdep_map);
20784e6045f1SJohannes Berg #endif
20796fec10a1STejun Heo 	/*
20806fec10a1STejun Heo 	 * Ensure we're on the correct CPU.  DISASSOCIATED test is
20816fec10a1STejun Heo 	 * necessary to avoid spurious warnings from rescuers servicing the
208224647570STejun Heo 	 * unbound or a disassociated pool.
20836fec10a1STejun Heo 	 */
20845f7dabfdSLai Jiangshan 	WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) &&
208524647570STejun Heo 		     !(pool->flags & POOL_DISASSOCIATED) &&
2086ec22ca5eSTejun Heo 		     raw_smp_processor_id() != pool->cpu);
208725511a47STejun Heo 
20887e11629dSTejun Heo 	/*
20897e11629dSTejun Heo 	 * A single work shouldn't be executed concurrently by
20907e11629dSTejun Heo 	 * multiple workers on a single cpu.  Check whether anyone is
20917e11629dSTejun Heo 	 * already processing the work.  If so, defer the work to the
20927e11629dSTejun Heo 	 * currently executing one.
20937e11629dSTejun Heo 	 */
2094c9e7cf27STejun Heo 	collision = find_worker_executing_work(pool, work);
20957e11629dSTejun Heo 	if (unlikely(collision)) {
20967e11629dSTejun Heo 		move_linked_works(work, &collision->scheduled, NULL);
20977e11629dSTejun Heo 		return;
20987e11629dSTejun Heo 	}
20991da177e4SLinus Torvalds 
21008930cabaSTejun Heo 	/* claim and dequeue */
21011da177e4SLinus Torvalds 	debug_work_deactivate(work);
2102c9e7cf27STejun Heo 	hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
2103c34056a3STejun Heo 	worker->current_work = work;
2104a2c1c57bSTejun Heo 	worker->current_func = work->func;
2105112202d9STejun Heo 	worker->current_pwq = pwq;
210673f53c4aSTejun Heo 	work_color = get_work_color(work);
21077a22ad75STejun Heo 
2108a62428c0STejun Heo 	list_del_init(&work->entry);
2109a62428c0STejun Heo 
2110649027d7STejun Heo 	/*
2111fb0e7bebSTejun Heo 	 * CPU intensive works don't participate in concurrency
2112fb0e7bebSTejun Heo 	 * management.  They're the scheduler's responsibility.
2113fb0e7bebSTejun Heo 	 */
2114fb0e7bebSTejun Heo 	if (unlikely(cpu_intensive))
2115fb0e7bebSTejun Heo 		worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2116fb0e7bebSTejun Heo 
2117974271c4STejun Heo 	/*
2118d565ed63STejun Heo 	 * Unbound pool isn't concurrency managed and work items should be
2119974271c4STejun Heo 	 * executed ASAP.  Wake up another worker if necessary.
2120974271c4STejun Heo 	 */
212163d95a91STejun Heo 	if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
212263d95a91STejun Heo 		wake_up_worker(pool);
2123974271c4STejun Heo 
21248930cabaSTejun Heo 	/*
21257c3eed5cSTejun Heo 	 * Record the last pool and clear PENDING which should be the last
2126d565ed63STejun Heo 	 * update to @work.  Also, do this inside @pool->lock so that
212723657bb1STejun Heo 	 * PENDING and queued state changes happen together while IRQ is
212823657bb1STejun Heo 	 * disabled.
21298930cabaSTejun Heo 	 */
21307c3eed5cSTejun Heo 	set_work_pool_and_clear_pending(work, pool->id);
21311da177e4SLinus Torvalds 
2132d565ed63STejun Heo 	spin_unlock_irq(&pool->lock);
2133365970a1SDavid Howells 
2134112202d9STejun Heo 	lock_map_acquire_read(&pwq->wq->lockdep_map);
21353295f0efSIngo Molnar 	lock_map_acquire(&lockdep_map);
2136e36c886aSArjan van de Ven 	trace_workqueue_execute_start(work);
2137a2c1c57bSTejun Heo 	worker->current_func(work);
2138e36c886aSArjan van de Ven 	/*
2139e36c886aSArjan van de Ven 	 * While we must be careful to not use "work" after this, the trace
2140e36c886aSArjan van de Ven 	 * point will only record its address.
2141e36c886aSArjan van de Ven 	 */
2142e36c886aSArjan van de Ven 	trace_workqueue_execute_end(work);
21433295f0efSIngo Molnar 	lock_map_release(&lockdep_map);
2144112202d9STejun Heo 	lock_map_release(&pwq->wq->lockdep_map);
21451da177e4SLinus Torvalds 
2146d5abe669SPeter Zijlstra 	if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
2147044c782cSValentin Ilie 		pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2148044c782cSValentin Ilie 		       "     last function: %pf\n",
2149a2c1c57bSTejun Heo 		       current->comm, preempt_count(), task_pid_nr(current),
2150a2c1c57bSTejun Heo 		       worker->current_func);
2151d5abe669SPeter Zijlstra 		debug_show_held_locks(current);
2152d5abe669SPeter Zijlstra 		dump_stack();
2153d5abe669SPeter Zijlstra 	}
2154d5abe669SPeter Zijlstra 
2155d565ed63STejun Heo 	spin_lock_irq(&pool->lock);
2156a62428c0STejun Heo 
2157fb0e7bebSTejun Heo 	/* clear cpu intensive status */
2158fb0e7bebSTejun Heo 	if (unlikely(cpu_intensive))
2159fb0e7bebSTejun Heo 		worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2160fb0e7bebSTejun Heo 
2161a62428c0STejun Heo 	/* we're done with it, release */
216242f8570fSSasha Levin 	hash_del(&worker->hentry);
2163c34056a3STejun Heo 	worker->current_work = NULL;
2164a2c1c57bSTejun Heo 	worker->current_func = NULL;
2165112202d9STejun Heo 	worker->current_pwq = NULL;
2166112202d9STejun Heo 	pwq_dec_nr_in_flight(pwq, work_color);
21671da177e4SLinus Torvalds }
21681da177e4SLinus Torvalds 
2169affee4b2STejun Heo /**
2170affee4b2STejun Heo  * process_scheduled_works - process scheduled works
2171affee4b2STejun Heo  * @worker: self
2172affee4b2STejun Heo  *
2173affee4b2STejun Heo  * Process all scheduled works.  Please note that the scheduled list
2174affee4b2STejun Heo  * may change while processing a work, so this function repeatedly
2175affee4b2STejun Heo  * fetches a work from the top and executes it.
2176affee4b2STejun Heo  *
2177affee4b2STejun Heo  * CONTEXT:
2178d565ed63STejun Heo  * spin_lock_irq(pool->lock) which may be released and regrabbed
2179affee4b2STejun Heo  * multiple times.
2180affee4b2STejun Heo  */
2181affee4b2STejun Heo static void process_scheduled_works(struct worker *worker)
21821da177e4SLinus Torvalds {
2183affee4b2STejun Heo 	while (!list_empty(&worker->scheduled)) {
2184affee4b2STejun Heo 		struct work_struct *work = list_first_entry(&worker->scheduled,
2185a62428c0STejun Heo 						struct work_struct, entry);
2186c34056a3STejun Heo 		process_one_work(worker, work);
2187a62428c0STejun Heo 	}
21881da177e4SLinus Torvalds }
21891da177e4SLinus Torvalds 
21904690c4abSTejun Heo /**
21914690c4abSTejun Heo  * worker_thread - the worker thread function
2192c34056a3STejun Heo  * @__worker: self
21934690c4abSTejun Heo  *
2194706026c2STejun Heo  * The worker thread function.  There are NR_CPU_WORKER_POOLS dynamic pools
2195706026c2STejun Heo  * of these per each cpu.  These workers process all works regardless of
2196e22bee78STejun Heo  * their specific target workqueue.  The only exception is works which
2197e22bee78STejun Heo  * belong to workqueues with a rescuer which will be explained in
2198e22bee78STejun Heo  * rescuer_thread().
21994690c4abSTejun Heo  */
2200c34056a3STejun Heo static int worker_thread(void *__worker)
22011da177e4SLinus Torvalds {
2202c34056a3STejun Heo 	struct worker *worker = __worker;
2203bd7bdd43STejun Heo 	struct worker_pool *pool = worker->pool;
22041da177e4SLinus Torvalds 
2205e22bee78STejun Heo 	/* tell the scheduler that this is a workqueue worker */
2206e22bee78STejun Heo 	worker->task->flags |= PF_WQ_WORKER;
2207c8e55f36STejun Heo woke_up:
2208d565ed63STejun Heo 	spin_lock_irq(&pool->lock);
2209affee4b2STejun Heo 
22105f7dabfdSLai Jiangshan 	/* we are off idle list if destruction or rebind is requested */
22115f7dabfdSLai Jiangshan 	if (unlikely(list_empty(&worker->entry))) {
2212d565ed63STejun Heo 		spin_unlock_irq(&pool->lock);
221325511a47STejun Heo 
22145f7dabfdSLai Jiangshan 		/* if DIE is set, destruction is requested */
221525511a47STejun Heo 		if (worker->flags & WORKER_DIE) {
2216e22bee78STejun Heo 			worker->task->flags &= ~PF_WQ_WORKER;
2217c8e55f36STejun Heo 			return 0;
2218c8e55f36STejun Heo 		}
2219c8e55f36STejun Heo 
22205f7dabfdSLai Jiangshan 		/* otherwise, rebind */
222125511a47STejun Heo 		idle_worker_rebind(worker);
222225511a47STejun Heo 		goto woke_up;
222325511a47STejun Heo 	}
222425511a47STejun Heo 
2225c8e55f36STejun Heo 	worker_leave_idle(worker);
2226db7bccf4STejun Heo recheck:
2227e22bee78STejun Heo 	/* no more worker necessary? */
222863d95a91STejun Heo 	if (!need_more_worker(pool))
2229e22bee78STejun Heo 		goto sleep;
2230e22bee78STejun Heo 
2231e22bee78STejun Heo 	/* do we need to manage? */
223263d95a91STejun Heo 	if (unlikely(!may_start_working(pool)) && manage_workers(worker))
2233e22bee78STejun Heo 		goto recheck;
2234e22bee78STejun Heo 
2235c8e55f36STejun Heo 	/*
2236c8e55f36STejun Heo 	 * ->scheduled list can only be filled while a worker is
2237c8e55f36STejun Heo 	 * preparing to process a work or actually processing it.
2238c8e55f36STejun Heo 	 * Make sure nobody diddled with it while I was sleeping.
2239c8e55f36STejun Heo 	 */
22406183c009STejun Heo 	WARN_ON_ONCE(!list_empty(&worker->scheduled));
2241c8e55f36STejun Heo 
2242e22bee78STejun Heo 	/*
2243e22bee78STejun Heo 	 * When control reaches this point, we're guaranteed to have
2244e22bee78STejun Heo 	 * at least one idle worker or that someone else has already
2245e22bee78STejun Heo 	 * assumed the manager role.
2246e22bee78STejun Heo 	 */
2247e22bee78STejun Heo 	worker_clr_flags(worker, WORKER_PREP);
2248e22bee78STejun Heo 
2249e22bee78STejun Heo 	do {
2250affee4b2STejun Heo 		struct work_struct *work =
2251bd7bdd43STejun Heo 			list_first_entry(&pool->worklist,
2252affee4b2STejun Heo 					 struct work_struct, entry);
2253affee4b2STejun Heo 
2254c8e55f36STejun Heo 		if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2255affee4b2STejun Heo 			/* optimization path, not strictly necessary */
2256affee4b2STejun Heo 			process_one_work(worker, work);
2257affee4b2STejun Heo 			if (unlikely(!list_empty(&worker->scheduled)))
2258affee4b2STejun Heo 				process_scheduled_works(worker);
2259affee4b2STejun Heo 		} else {
2260c8e55f36STejun Heo 			move_linked_works(work, &worker->scheduled, NULL);
2261affee4b2STejun Heo 			process_scheduled_works(worker);
2262affee4b2STejun Heo 		}
226363d95a91STejun Heo 	} while (keep_working(pool));
2264affee4b2STejun Heo 
2265e22bee78STejun Heo 	worker_set_flags(worker, WORKER_PREP, false);
2266d313dd85STejun Heo sleep:
226763d95a91STejun Heo 	if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
2268e22bee78STejun Heo 		goto recheck;
2269d313dd85STejun Heo 
2270c8e55f36STejun Heo 	/*
2271d565ed63STejun Heo 	 * pool->lock is held and there's no work to process and no need to
2272d565ed63STejun Heo 	 * manage, sleep.  Workers are woken up only while holding
2273d565ed63STejun Heo 	 * pool->lock or from local cpu, so setting the current state
2274d565ed63STejun Heo 	 * before releasing pool->lock is enough to prevent losing any
2275d565ed63STejun Heo 	 * event.
2276c8e55f36STejun Heo 	 */
2277c8e55f36STejun Heo 	worker_enter_idle(worker);
2278c8e55f36STejun Heo 	__set_current_state(TASK_INTERRUPTIBLE);
2279d565ed63STejun Heo 	spin_unlock_irq(&pool->lock);
22801da177e4SLinus Torvalds 	schedule();
2281c8e55f36STejun Heo 	goto woke_up;
22821da177e4SLinus Torvalds }
22831da177e4SLinus Torvalds 
2284e22bee78STejun Heo /**
2285e22bee78STejun Heo  * rescuer_thread - the rescuer thread function
2286111c225aSTejun Heo  * @__rescuer: self
2287e22bee78STejun Heo  *
2288e22bee78STejun Heo  * Workqueue rescuer thread function.  There's one rescuer for each
2289e22bee78STejun Heo  * workqueue which has WQ_RESCUER set.
2290e22bee78STejun Heo  *
2291706026c2STejun Heo  * Regular work processing on a pool may block trying to create a new
2292e22bee78STejun Heo  * worker which uses GFP_KERNEL allocation which has slight chance of
2293e22bee78STejun Heo  * developing into deadlock if some works currently on the same queue
2294e22bee78STejun Heo  * need to be processed to satisfy the GFP_KERNEL allocation.  This is
2295e22bee78STejun Heo  * the problem rescuer solves.
2296e22bee78STejun Heo  *
2297706026c2STejun Heo  * When such condition is possible, the pool summons rescuers of all
2298706026c2STejun Heo  * workqueues which have works queued on the pool and let them process
2299e22bee78STejun Heo  * those works so that forward progress can be guaranteed.
2300e22bee78STejun Heo  *
2301e22bee78STejun Heo  * This should happen rarely.
2302e22bee78STejun Heo  */
2303111c225aSTejun Heo static int rescuer_thread(void *__rescuer)
2304e22bee78STejun Heo {
2305111c225aSTejun Heo 	struct worker *rescuer = __rescuer;
2306111c225aSTejun Heo 	struct workqueue_struct *wq = rescuer->rescue_wq;
2307e22bee78STejun Heo 	struct list_head *scheduled = &rescuer->scheduled;
2308e22bee78STejun Heo 
2309e22bee78STejun Heo 	set_user_nice(current, RESCUER_NICE_LEVEL);
2310111c225aSTejun Heo 
2311111c225aSTejun Heo 	/*
2312111c225aSTejun Heo 	 * Mark rescuer as worker too.  As WORKER_PREP is never cleared, it
2313111c225aSTejun Heo 	 * doesn't participate in concurrency management.
2314111c225aSTejun Heo 	 */
2315111c225aSTejun Heo 	rescuer->task->flags |= PF_WQ_WORKER;
2316e22bee78STejun Heo repeat:
2317e22bee78STejun Heo 	set_current_state(TASK_INTERRUPTIBLE);
23181da177e4SLinus Torvalds 
2319412d32e6SMike Galbraith 	if (kthread_should_stop()) {
2320412d32e6SMike Galbraith 		__set_current_state(TASK_RUNNING);
2321111c225aSTejun Heo 		rescuer->task->flags &= ~PF_WQ_WORKER;
2322e22bee78STejun Heo 		return 0;
2323412d32e6SMike Galbraith 	}
23241da177e4SLinus Torvalds 
2325493a1724STejun Heo 	/* see whether any pwq is asking for help */
2326493a1724STejun Heo 	spin_lock_irq(&workqueue_lock);
2327493a1724STejun Heo 
2328493a1724STejun Heo 	while (!list_empty(&wq->maydays)) {
2329493a1724STejun Heo 		struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
2330493a1724STejun Heo 					struct pool_workqueue, mayday_node);
2331112202d9STejun Heo 		struct worker_pool *pool = pwq->pool;
2332e22bee78STejun Heo 		struct work_struct *work, *n;
2333e22bee78STejun Heo 
2334e22bee78STejun Heo 		__set_current_state(TASK_RUNNING);
2335493a1724STejun Heo 		list_del_init(&pwq->mayday_node);
2336493a1724STejun Heo 
2337493a1724STejun Heo 		spin_unlock_irq(&workqueue_lock);
2338e22bee78STejun Heo 
2339e22bee78STejun Heo 		/* migrate to the target cpu if possible */
2340f36dc67bSLai Jiangshan 		worker_maybe_bind_and_lock(pool);
2341b3104104SLai Jiangshan 		rescuer->pool = pool;
2342e22bee78STejun Heo 
2343e22bee78STejun Heo 		/*
2344e22bee78STejun Heo 		 * Slurp in all works issued via this workqueue and
2345e22bee78STejun Heo 		 * process'em.
2346e22bee78STejun Heo 		 */
23476183c009STejun Heo 		WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
2348bd7bdd43STejun Heo 		list_for_each_entry_safe(work, n, &pool->worklist, entry)
2349112202d9STejun Heo 			if (get_work_pwq(work) == pwq)
2350e22bee78STejun Heo 				move_linked_works(work, scheduled, &n);
2351e22bee78STejun Heo 
2352e22bee78STejun Heo 		process_scheduled_works(rescuer);
23537576958aSTejun Heo 
23547576958aSTejun Heo 		/*
2355d565ed63STejun Heo 		 * Leave this pool.  If keep_working() is %true, notify a
23567576958aSTejun Heo 		 * regular worker; otherwise, we end up with 0 concurrency
23577576958aSTejun Heo 		 * and stalling the execution.
23587576958aSTejun Heo 		 */
235963d95a91STejun Heo 		if (keep_working(pool))
236063d95a91STejun Heo 			wake_up_worker(pool);
23617576958aSTejun Heo 
2362b3104104SLai Jiangshan 		rescuer->pool = NULL;
2363493a1724STejun Heo 		spin_unlock(&pool->lock);
2364493a1724STejun Heo 		spin_lock(&workqueue_lock);
23651da177e4SLinus Torvalds 	}
23661da177e4SLinus Torvalds 
2367493a1724STejun Heo 	spin_unlock_irq(&workqueue_lock);
2368493a1724STejun Heo 
2369111c225aSTejun Heo 	/* rescuers should never participate in concurrency management */
2370111c225aSTejun Heo 	WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
2371e22bee78STejun Heo 	schedule();
2372e22bee78STejun Heo 	goto repeat;
23731da177e4SLinus Torvalds }
23741da177e4SLinus Torvalds 
2375fc2e4d70SOleg Nesterov struct wq_barrier {
2376fc2e4d70SOleg Nesterov 	struct work_struct	work;
2377fc2e4d70SOleg Nesterov 	struct completion	done;
2378fc2e4d70SOleg Nesterov };
2379fc2e4d70SOleg Nesterov 
2380fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work)
2381fc2e4d70SOleg Nesterov {
2382fc2e4d70SOleg Nesterov 	struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2383fc2e4d70SOleg Nesterov 	complete(&barr->done);
2384fc2e4d70SOleg Nesterov }
2385fc2e4d70SOleg Nesterov 
23864690c4abSTejun Heo /**
23874690c4abSTejun Heo  * insert_wq_barrier - insert a barrier work
2388112202d9STejun Heo  * @pwq: pwq to insert barrier into
23894690c4abSTejun Heo  * @barr: wq_barrier to insert
2390affee4b2STejun Heo  * @target: target work to attach @barr to
2391affee4b2STejun Heo  * @worker: worker currently executing @target, NULL if @target is not executing
23924690c4abSTejun Heo  *
2393affee4b2STejun Heo  * @barr is linked to @target such that @barr is completed only after
2394affee4b2STejun Heo  * @target finishes execution.  Please note that the ordering
2395affee4b2STejun Heo  * guarantee is observed only with respect to @target and on the local
2396affee4b2STejun Heo  * cpu.
2397affee4b2STejun Heo  *
2398affee4b2STejun Heo  * Currently, a queued barrier can't be canceled.  This is because
2399affee4b2STejun Heo  * try_to_grab_pending() can't determine whether the work to be
2400affee4b2STejun Heo  * grabbed is at the head of the queue and thus can't clear LINKED
2401affee4b2STejun Heo  * flag of the previous work while there must be a valid next work
2402affee4b2STejun Heo  * after a work with LINKED flag set.
2403affee4b2STejun Heo  *
2404affee4b2STejun Heo  * Note that when @worker is non-NULL, @target may be modified
2405112202d9STejun Heo  * underneath us, so we can't reliably determine pwq from @target.
24064690c4abSTejun Heo  *
24074690c4abSTejun Heo  * CONTEXT:
2408d565ed63STejun Heo  * spin_lock_irq(pool->lock).
24094690c4abSTejun Heo  */
2410112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq,
2411affee4b2STejun Heo 			      struct wq_barrier *barr,
2412affee4b2STejun Heo 			      struct work_struct *target, struct worker *worker)
2413fc2e4d70SOleg Nesterov {
2414affee4b2STejun Heo 	struct list_head *head;
2415affee4b2STejun Heo 	unsigned int linked = 0;
2416affee4b2STejun Heo 
2417dc186ad7SThomas Gleixner 	/*
2418d565ed63STejun Heo 	 * debugobject calls are safe here even with pool->lock locked
2419dc186ad7SThomas Gleixner 	 * as we know for sure that this will not trigger any of the
2420dc186ad7SThomas Gleixner 	 * checks and call back into the fixup functions where we
2421dc186ad7SThomas Gleixner 	 * might deadlock.
2422dc186ad7SThomas Gleixner 	 */
2423ca1cab37SAndrew Morton 	INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
242422df02bbSTejun Heo 	__set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
2425fc2e4d70SOleg Nesterov 	init_completion(&barr->done);
242683c22520SOleg Nesterov 
2427affee4b2STejun Heo 	/*
2428affee4b2STejun Heo 	 * If @target is currently being executed, schedule the
2429affee4b2STejun Heo 	 * barrier to the worker; otherwise, put it after @target.
2430affee4b2STejun Heo 	 */
2431affee4b2STejun Heo 	if (worker)
2432affee4b2STejun Heo 		head = worker->scheduled.next;
2433affee4b2STejun Heo 	else {
2434affee4b2STejun Heo 		unsigned long *bits = work_data_bits(target);
2435affee4b2STejun Heo 
2436affee4b2STejun Heo 		head = target->entry.next;
2437affee4b2STejun Heo 		/* there can already be other linked works, inherit and set */
2438affee4b2STejun Heo 		linked = *bits & WORK_STRUCT_LINKED;
2439affee4b2STejun Heo 		__set_bit(WORK_STRUCT_LINKED_BIT, bits);
2440affee4b2STejun Heo 	}
2441affee4b2STejun Heo 
2442dc186ad7SThomas Gleixner 	debug_work_activate(&barr->work);
2443112202d9STejun Heo 	insert_work(pwq, &barr->work, head,
2444affee4b2STejun Heo 		    work_color_to_flags(WORK_NO_COLOR) | linked);
2445fc2e4d70SOleg Nesterov }
2446fc2e4d70SOleg Nesterov 
244773f53c4aSTejun Heo /**
2448112202d9STejun Heo  * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing
244973f53c4aSTejun Heo  * @wq: workqueue being flushed
245073f53c4aSTejun Heo  * @flush_color: new flush color, < 0 for no-op
245173f53c4aSTejun Heo  * @work_color: new work color, < 0 for no-op
245273f53c4aSTejun Heo  *
2453112202d9STejun Heo  * Prepare pwqs for workqueue flushing.
245473f53c4aSTejun Heo  *
2455112202d9STejun Heo  * If @flush_color is non-negative, flush_color on all pwqs should be
2456112202d9STejun Heo  * -1.  If no pwq has in-flight commands at the specified color, all
2457112202d9STejun Heo  * pwq->flush_color's stay at -1 and %false is returned.  If any pwq
2458112202d9STejun Heo  * has in flight commands, its pwq->flush_color is set to
2459112202d9STejun Heo  * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq
246073f53c4aSTejun Heo  * wakeup logic is armed and %true is returned.
246173f53c4aSTejun Heo  *
246273f53c4aSTejun Heo  * The caller should have initialized @wq->first_flusher prior to
246373f53c4aSTejun Heo  * calling this function with non-negative @flush_color.  If
246473f53c4aSTejun Heo  * @flush_color is negative, no flush color update is done and %false
246573f53c4aSTejun Heo  * is returned.
246673f53c4aSTejun Heo  *
2467112202d9STejun Heo  * If @work_color is non-negative, all pwqs should have the same
246873f53c4aSTejun Heo  * work_color which is previous to @work_color and all will be
246973f53c4aSTejun Heo  * advanced to @work_color.
247073f53c4aSTejun Heo  *
247173f53c4aSTejun Heo  * CONTEXT:
247273f53c4aSTejun Heo  * mutex_lock(wq->flush_mutex).
247373f53c4aSTejun Heo  *
247473f53c4aSTejun Heo  * RETURNS:
247573f53c4aSTejun Heo  * %true if @flush_color >= 0 and there's something to flush.  %false
247673f53c4aSTejun Heo  * otherwise.
247773f53c4aSTejun Heo  */
2478112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
247973f53c4aSTejun Heo 				      int flush_color, int work_color)
24801da177e4SLinus Torvalds {
248173f53c4aSTejun Heo 	bool wait = false;
248249e3cf44STejun Heo 	struct pool_workqueue *pwq;
24831da177e4SLinus Torvalds 
248473f53c4aSTejun Heo 	if (flush_color >= 0) {
24856183c009STejun Heo 		WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
2486112202d9STejun Heo 		atomic_set(&wq->nr_pwqs_to_flush, 1);
2487dc186ad7SThomas Gleixner 	}
248814441960SOleg Nesterov 
248949e3cf44STejun Heo 	for_each_pwq(pwq, wq) {
2490112202d9STejun Heo 		struct worker_pool *pool = pwq->pool;
24911da177e4SLinus Torvalds 
2492d565ed63STejun Heo 		spin_lock_irq(&pool->lock);
249373f53c4aSTejun Heo 
249473f53c4aSTejun Heo 		if (flush_color >= 0) {
24956183c009STejun Heo 			WARN_ON_ONCE(pwq->flush_color != -1);
249673f53c4aSTejun Heo 
2497112202d9STejun Heo 			if (pwq->nr_in_flight[flush_color]) {
2498112202d9STejun Heo 				pwq->flush_color = flush_color;
2499112202d9STejun Heo 				atomic_inc(&wq->nr_pwqs_to_flush);
250073f53c4aSTejun Heo 				wait = true;
25011da177e4SLinus Torvalds 			}
250273f53c4aSTejun Heo 		}
250373f53c4aSTejun Heo 
250473f53c4aSTejun Heo 		if (work_color >= 0) {
25056183c009STejun Heo 			WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
2506112202d9STejun Heo 			pwq->work_color = work_color;
250773f53c4aSTejun Heo 		}
250873f53c4aSTejun Heo 
2509d565ed63STejun Heo 		spin_unlock_irq(&pool->lock);
25101da177e4SLinus Torvalds 	}
25111da177e4SLinus Torvalds 
2512112202d9STejun Heo 	if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush))
251373f53c4aSTejun Heo 		complete(&wq->first_flusher->done);
251473f53c4aSTejun Heo 
251573f53c4aSTejun Heo 	return wait;
251683c22520SOleg Nesterov }
25171da177e4SLinus Torvalds 
25180fcb78c2SRolf Eike Beer /**
25191da177e4SLinus Torvalds  * flush_workqueue - ensure that any scheduled work has run to completion.
25200fcb78c2SRolf Eike Beer  * @wq: workqueue to flush
25211da177e4SLinus Torvalds  *
25221da177e4SLinus Torvalds  * Forces execution of the workqueue and blocks until its completion.
25231da177e4SLinus Torvalds  * This is typically used in driver shutdown handlers.
25241da177e4SLinus Torvalds  *
2525fc2e4d70SOleg Nesterov  * We sleep until all works which were queued on entry have been handled,
2526fc2e4d70SOleg Nesterov  * but we are not livelocked by new incoming ones.
25271da177e4SLinus Torvalds  */
25287ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq)
25291da177e4SLinus Torvalds {
253073f53c4aSTejun Heo 	struct wq_flusher this_flusher = {
253173f53c4aSTejun Heo 		.list = LIST_HEAD_INIT(this_flusher.list),
253273f53c4aSTejun Heo 		.flush_color = -1,
253373f53c4aSTejun Heo 		.done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
253473f53c4aSTejun Heo 	};
253573f53c4aSTejun Heo 	int next_color;
2536b1f4ec17SOleg Nesterov 
25373295f0efSIngo Molnar 	lock_map_acquire(&wq->lockdep_map);
25383295f0efSIngo Molnar 	lock_map_release(&wq->lockdep_map);
253973f53c4aSTejun Heo 
254073f53c4aSTejun Heo 	mutex_lock(&wq->flush_mutex);
254173f53c4aSTejun Heo 
254273f53c4aSTejun Heo 	/*
254373f53c4aSTejun Heo 	 * Start-to-wait phase
254473f53c4aSTejun Heo 	 */
254573f53c4aSTejun Heo 	next_color = work_next_color(wq->work_color);
254673f53c4aSTejun Heo 
254773f53c4aSTejun Heo 	if (next_color != wq->flush_color) {
254873f53c4aSTejun Heo 		/*
254973f53c4aSTejun Heo 		 * Color space is not full.  The current work_color
255073f53c4aSTejun Heo 		 * becomes our flush_color and work_color is advanced
255173f53c4aSTejun Heo 		 * by one.
255273f53c4aSTejun Heo 		 */
25536183c009STejun Heo 		WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
255473f53c4aSTejun Heo 		this_flusher.flush_color = wq->work_color;
255573f53c4aSTejun Heo 		wq->work_color = next_color;
255673f53c4aSTejun Heo 
255773f53c4aSTejun Heo 		if (!wq->first_flusher) {
255873f53c4aSTejun Heo 			/* no flush in progress, become the first flusher */
25596183c009STejun Heo 			WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
256073f53c4aSTejun Heo 
256173f53c4aSTejun Heo 			wq->first_flusher = &this_flusher;
256273f53c4aSTejun Heo 
2563112202d9STejun Heo 			if (!flush_workqueue_prep_pwqs(wq, wq->flush_color,
256473f53c4aSTejun Heo 						       wq->work_color)) {
256573f53c4aSTejun Heo 				/* nothing to flush, done */
256673f53c4aSTejun Heo 				wq->flush_color = next_color;
256773f53c4aSTejun Heo 				wq->first_flusher = NULL;
256873f53c4aSTejun Heo 				goto out_unlock;
256973f53c4aSTejun Heo 			}
257073f53c4aSTejun Heo 		} else {
257173f53c4aSTejun Heo 			/* wait in queue */
25726183c009STejun Heo 			WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
257373f53c4aSTejun Heo 			list_add_tail(&this_flusher.list, &wq->flusher_queue);
2574112202d9STejun Heo 			flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
257573f53c4aSTejun Heo 		}
257673f53c4aSTejun Heo 	} else {
257773f53c4aSTejun Heo 		/*
257873f53c4aSTejun Heo 		 * Oops, color space is full, wait on overflow queue.
257973f53c4aSTejun Heo 		 * The next flush completion will assign us
258073f53c4aSTejun Heo 		 * flush_color and transfer to flusher_queue.
258173f53c4aSTejun Heo 		 */
258273f53c4aSTejun Heo 		list_add_tail(&this_flusher.list, &wq->flusher_overflow);
258373f53c4aSTejun Heo 	}
258473f53c4aSTejun Heo 
258573f53c4aSTejun Heo 	mutex_unlock(&wq->flush_mutex);
258673f53c4aSTejun Heo 
258773f53c4aSTejun Heo 	wait_for_completion(&this_flusher.done);
258873f53c4aSTejun Heo 
258973f53c4aSTejun Heo 	/*
259073f53c4aSTejun Heo 	 * Wake-up-and-cascade phase
259173f53c4aSTejun Heo 	 *
259273f53c4aSTejun Heo 	 * First flushers are responsible for cascading flushes and
259373f53c4aSTejun Heo 	 * handling overflow.  Non-first flushers can simply return.
259473f53c4aSTejun Heo 	 */
259573f53c4aSTejun Heo 	if (wq->first_flusher != &this_flusher)
259673f53c4aSTejun Heo 		return;
259773f53c4aSTejun Heo 
259873f53c4aSTejun Heo 	mutex_lock(&wq->flush_mutex);
259973f53c4aSTejun Heo 
26004ce48b37STejun Heo 	/* we might have raced, check again with mutex held */
26014ce48b37STejun Heo 	if (wq->first_flusher != &this_flusher)
26024ce48b37STejun Heo 		goto out_unlock;
26034ce48b37STejun Heo 
260473f53c4aSTejun Heo 	wq->first_flusher = NULL;
260573f53c4aSTejun Heo 
26066183c009STejun Heo 	WARN_ON_ONCE(!list_empty(&this_flusher.list));
26076183c009STejun Heo 	WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
260873f53c4aSTejun Heo 
260973f53c4aSTejun Heo 	while (true) {
261073f53c4aSTejun Heo 		struct wq_flusher *next, *tmp;
261173f53c4aSTejun Heo 
261273f53c4aSTejun Heo 		/* complete all the flushers sharing the current flush color */
261373f53c4aSTejun Heo 		list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
261473f53c4aSTejun Heo 			if (next->flush_color != wq->flush_color)
261573f53c4aSTejun Heo 				break;
261673f53c4aSTejun Heo 			list_del_init(&next->list);
261773f53c4aSTejun Heo 			complete(&next->done);
261873f53c4aSTejun Heo 		}
261973f53c4aSTejun Heo 
26206183c009STejun Heo 		WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
262173f53c4aSTejun Heo 			     wq->flush_color != work_next_color(wq->work_color));
262273f53c4aSTejun Heo 
262373f53c4aSTejun Heo 		/* this flush_color is finished, advance by one */
262473f53c4aSTejun Heo 		wq->flush_color = work_next_color(wq->flush_color);
262573f53c4aSTejun Heo 
262673f53c4aSTejun Heo 		/* one color has been freed, handle overflow queue */
262773f53c4aSTejun Heo 		if (!list_empty(&wq->flusher_overflow)) {
262873f53c4aSTejun Heo 			/*
262973f53c4aSTejun Heo 			 * Assign the same color to all overflowed
263073f53c4aSTejun Heo 			 * flushers, advance work_color and append to
263173f53c4aSTejun Heo 			 * flusher_queue.  This is the start-to-wait
263273f53c4aSTejun Heo 			 * phase for these overflowed flushers.
263373f53c4aSTejun Heo 			 */
263473f53c4aSTejun Heo 			list_for_each_entry(tmp, &wq->flusher_overflow, list)
263573f53c4aSTejun Heo 				tmp->flush_color = wq->work_color;
263673f53c4aSTejun Heo 
263773f53c4aSTejun Heo 			wq->work_color = work_next_color(wq->work_color);
263873f53c4aSTejun Heo 
263973f53c4aSTejun Heo 			list_splice_tail_init(&wq->flusher_overflow,
264073f53c4aSTejun Heo 					      &wq->flusher_queue);
2641112202d9STejun Heo 			flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
264273f53c4aSTejun Heo 		}
264373f53c4aSTejun Heo 
264473f53c4aSTejun Heo 		if (list_empty(&wq->flusher_queue)) {
26456183c009STejun Heo 			WARN_ON_ONCE(wq->flush_color != wq->work_color);
264673f53c4aSTejun Heo 			break;
264773f53c4aSTejun Heo 		}
264873f53c4aSTejun Heo 
264973f53c4aSTejun Heo 		/*
265073f53c4aSTejun Heo 		 * Need to flush more colors.  Make the next flusher
2651112202d9STejun Heo 		 * the new first flusher and arm pwqs.
265273f53c4aSTejun Heo 		 */
26536183c009STejun Heo 		WARN_ON_ONCE(wq->flush_color == wq->work_color);
26546183c009STejun Heo 		WARN_ON_ONCE(wq->flush_color != next->flush_color);
265573f53c4aSTejun Heo 
265673f53c4aSTejun Heo 		list_del_init(&next->list);
265773f53c4aSTejun Heo 		wq->first_flusher = next;
265873f53c4aSTejun Heo 
2659112202d9STejun Heo 		if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1))
266073f53c4aSTejun Heo 			break;
266173f53c4aSTejun Heo 
266273f53c4aSTejun Heo 		/*
266373f53c4aSTejun Heo 		 * Meh... this color is already done, clear first
266473f53c4aSTejun Heo 		 * flusher and repeat cascading.
266573f53c4aSTejun Heo 		 */
266673f53c4aSTejun Heo 		wq->first_flusher = NULL;
266773f53c4aSTejun Heo 	}
266873f53c4aSTejun Heo 
266973f53c4aSTejun Heo out_unlock:
267073f53c4aSTejun Heo 	mutex_unlock(&wq->flush_mutex);
26711da177e4SLinus Torvalds }
2672ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(flush_workqueue);
26731da177e4SLinus Torvalds 
26749c5a2ba7STejun Heo /**
26759c5a2ba7STejun Heo  * drain_workqueue - drain a workqueue
26769c5a2ba7STejun Heo  * @wq: workqueue to drain
26779c5a2ba7STejun Heo  *
26789c5a2ba7STejun Heo  * Wait until the workqueue becomes empty.  While draining is in progress,
26799c5a2ba7STejun Heo  * only chain queueing is allowed.  IOW, only currently pending or running
26809c5a2ba7STejun Heo  * work items on @wq can queue further work items on it.  @wq is flushed
26819c5a2ba7STejun Heo  * repeatedly until it becomes empty.  The number of flushing is detemined
26829c5a2ba7STejun Heo  * by the depth of chaining and should be relatively short.  Whine if it
26839c5a2ba7STejun Heo  * takes too long.
26849c5a2ba7STejun Heo  */
26859c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq)
26869c5a2ba7STejun Heo {
26879c5a2ba7STejun Heo 	unsigned int flush_cnt = 0;
268849e3cf44STejun Heo 	struct pool_workqueue *pwq;
26899c5a2ba7STejun Heo 
26909c5a2ba7STejun Heo 	/*
26919c5a2ba7STejun Heo 	 * __queue_work() needs to test whether there are drainers, is much
26929c5a2ba7STejun Heo 	 * hotter than drain_workqueue() and already looks at @wq->flags.
26939c5a2ba7STejun Heo 	 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
26949c5a2ba7STejun Heo 	 */
2695e98d5b16STejun Heo 	spin_lock_irq(&workqueue_lock);
26969c5a2ba7STejun Heo 	if (!wq->nr_drainers++)
26979c5a2ba7STejun Heo 		wq->flags |= WQ_DRAINING;
2698e98d5b16STejun Heo 	spin_unlock_irq(&workqueue_lock);
26999c5a2ba7STejun Heo reflush:
27009c5a2ba7STejun Heo 	flush_workqueue(wq);
27019c5a2ba7STejun Heo 
270249e3cf44STejun Heo 	for_each_pwq(pwq, wq) {
2703fa2563e4SThomas Tuttle 		bool drained;
27049c5a2ba7STejun Heo 
2705112202d9STejun Heo 		spin_lock_irq(&pwq->pool->lock);
2706112202d9STejun Heo 		drained = !pwq->nr_active && list_empty(&pwq->delayed_works);
2707112202d9STejun Heo 		spin_unlock_irq(&pwq->pool->lock);
2708fa2563e4SThomas Tuttle 
2709fa2563e4SThomas Tuttle 		if (drained)
27109c5a2ba7STejun Heo 			continue;
27119c5a2ba7STejun Heo 
27129c5a2ba7STejun Heo 		if (++flush_cnt == 10 ||
27139c5a2ba7STejun Heo 		    (flush_cnt % 100 == 0 && flush_cnt <= 1000))
2714044c782cSValentin Ilie 			pr_warn("workqueue %s: flush on destruction isn't complete after %u tries\n",
27159c5a2ba7STejun Heo 				wq->name, flush_cnt);
27169c5a2ba7STejun Heo 		goto reflush;
27179c5a2ba7STejun Heo 	}
27189c5a2ba7STejun Heo 
2719e98d5b16STejun Heo 	spin_lock_irq(&workqueue_lock);
27209c5a2ba7STejun Heo 	if (!--wq->nr_drainers)
27219c5a2ba7STejun Heo 		wq->flags &= ~WQ_DRAINING;
2722e98d5b16STejun Heo 	spin_unlock_irq(&workqueue_lock);
27239c5a2ba7STejun Heo }
27249c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue);
27259c5a2ba7STejun Heo 
2726606a5020STejun Heo static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
2727baf59022STejun Heo {
2728baf59022STejun Heo 	struct worker *worker = NULL;
2729c9e7cf27STejun Heo 	struct worker_pool *pool;
2730112202d9STejun Heo 	struct pool_workqueue *pwq;
2731baf59022STejun Heo 
2732baf59022STejun Heo 	might_sleep();
2733c9e7cf27STejun Heo 	pool = get_work_pool(work);
2734c9e7cf27STejun Heo 	if (!pool)
2735baf59022STejun Heo 		return false;
2736baf59022STejun Heo 
2737d565ed63STejun Heo 	spin_lock_irq(&pool->lock);
27380b3dae68SLai Jiangshan 	/* see the comment in try_to_grab_pending() with the same code */
2739112202d9STejun Heo 	pwq = get_work_pwq(work);
2740112202d9STejun Heo 	if (pwq) {
2741112202d9STejun Heo 		if (unlikely(pwq->pool != pool))
2742baf59022STejun Heo 			goto already_gone;
2743606a5020STejun Heo 	} else {
2744c9e7cf27STejun Heo 		worker = find_worker_executing_work(pool, work);
2745baf59022STejun Heo 		if (!worker)
2746baf59022STejun Heo 			goto already_gone;
2747112202d9STejun Heo 		pwq = worker->current_pwq;
2748606a5020STejun Heo 	}
2749baf59022STejun Heo 
2750112202d9STejun Heo 	insert_wq_barrier(pwq, barr, work, worker);
2751d565ed63STejun Heo 	spin_unlock_irq(&pool->lock);
2752baf59022STejun Heo 
2753e159489bSTejun Heo 	/*
2754e159489bSTejun Heo 	 * If @max_active is 1 or rescuer is in use, flushing another work
2755e159489bSTejun Heo 	 * item on the same workqueue may lead to deadlock.  Make sure the
2756e159489bSTejun Heo 	 * flusher is not running on the same workqueue by verifying write
2757e159489bSTejun Heo 	 * access.
2758e159489bSTejun Heo 	 */
2759112202d9STejun Heo 	if (pwq->wq->saved_max_active == 1 || pwq->wq->flags & WQ_RESCUER)
2760112202d9STejun Heo 		lock_map_acquire(&pwq->wq->lockdep_map);
2761e159489bSTejun Heo 	else
2762112202d9STejun Heo 		lock_map_acquire_read(&pwq->wq->lockdep_map);
2763112202d9STejun Heo 	lock_map_release(&pwq->wq->lockdep_map);
2764e159489bSTejun Heo 
2765baf59022STejun Heo 	return true;
2766baf59022STejun Heo already_gone:
2767d565ed63STejun Heo 	spin_unlock_irq(&pool->lock);
2768baf59022STejun Heo 	return false;
2769baf59022STejun Heo }
2770baf59022STejun Heo 
2771db700897SOleg Nesterov /**
2772401a8d04STejun Heo  * flush_work - wait for a work to finish executing the last queueing instance
2773401a8d04STejun Heo  * @work: the work to flush
2774db700897SOleg Nesterov  *
2775606a5020STejun Heo  * Wait until @work has finished execution.  @work is guaranteed to be idle
2776606a5020STejun Heo  * on return if it hasn't been requeued since flush started.
2777401a8d04STejun Heo  *
2778401a8d04STejun Heo  * RETURNS:
2779401a8d04STejun Heo  * %true if flush_work() waited for the work to finish execution,
2780401a8d04STejun Heo  * %false if it was already idle.
2781db700897SOleg Nesterov  */
2782401a8d04STejun Heo bool flush_work(struct work_struct *work)
2783db700897SOleg Nesterov {
2784db700897SOleg Nesterov 	struct wq_barrier barr;
2785db700897SOleg Nesterov 
27860976dfc1SStephen Boyd 	lock_map_acquire(&work->lockdep_map);
27870976dfc1SStephen Boyd 	lock_map_release(&work->lockdep_map);
27880976dfc1SStephen Boyd 
2789606a5020STejun Heo 	if (start_flush_work(work, &barr)) {
2790db700897SOleg Nesterov 		wait_for_completion(&barr.done);
2791dc186ad7SThomas Gleixner 		destroy_work_on_stack(&barr.work);
2792401a8d04STejun Heo 		return true;
2793606a5020STejun Heo 	} else {
2794401a8d04STejun Heo 		return false;
2795db700897SOleg Nesterov 	}
2796606a5020STejun Heo }
2797db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work);
2798db700897SOleg Nesterov 
279936e227d2STejun Heo static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
2800401a8d04STejun Heo {
2801bbb68dfaSTejun Heo 	unsigned long flags;
28021f1f642eSOleg Nesterov 	int ret;
28031f1f642eSOleg Nesterov 
28041f1f642eSOleg Nesterov 	do {
2805bbb68dfaSTejun Heo 		ret = try_to_grab_pending(work, is_dwork, &flags);
2806bbb68dfaSTejun Heo 		/*
2807bbb68dfaSTejun Heo 		 * If someone else is canceling, wait for the same event it
2808bbb68dfaSTejun Heo 		 * would be waiting for before retrying.
2809bbb68dfaSTejun Heo 		 */
2810bbb68dfaSTejun Heo 		if (unlikely(ret == -ENOENT))
2811606a5020STejun Heo 			flush_work(work);
28121f1f642eSOleg Nesterov 	} while (unlikely(ret < 0));
28131f1f642eSOleg Nesterov 
2814bbb68dfaSTejun Heo 	/* tell other tasks trying to grab @work to back off */
2815bbb68dfaSTejun Heo 	mark_work_canceling(work);
2816bbb68dfaSTejun Heo 	local_irq_restore(flags);
2817bbb68dfaSTejun Heo 
2818606a5020STejun Heo 	flush_work(work);
28197a22ad75STejun Heo 	clear_work_data(work);
28201f1f642eSOleg Nesterov 	return ret;
28211f1f642eSOleg Nesterov }
28221f1f642eSOleg Nesterov 
28236e84d644SOleg Nesterov /**
2824401a8d04STejun Heo  * cancel_work_sync - cancel a work and wait for it to finish
2825401a8d04STejun Heo  * @work: the work to cancel
28266e84d644SOleg Nesterov  *
2827401a8d04STejun Heo  * Cancel @work and wait for its execution to finish.  This function
2828401a8d04STejun Heo  * can be used even if the work re-queues itself or migrates to
2829401a8d04STejun Heo  * another workqueue.  On return from this function, @work is
2830401a8d04STejun Heo  * guaranteed to be not pending or executing on any CPU.
28311f1f642eSOleg Nesterov  *
2832401a8d04STejun Heo  * cancel_work_sync(&delayed_work->work) must not be used for
2833401a8d04STejun Heo  * delayed_work's.  Use cancel_delayed_work_sync() instead.
28346e84d644SOleg Nesterov  *
2835401a8d04STejun Heo  * The caller must ensure that the workqueue on which @work was last
28366e84d644SOleg Nesterov  * queued can't be destroyed before this function returns.
2837401a8d04STejun Heo  *
2838401a8d04STejun Heo  * RETURNS:
2839401a8d04STejun Heo  * %true if @work was pending, %false otherwise.
28406e84d644SOleg Nesterov  */
2841401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work)
28426e84d644SOleg Nesterov {
284336e227d2STejun Heo 	return __cancel_work_timer(work, false);
2844b89deed3SOleg Nesterov }
284528e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync);
2846b89deed3SOleg Nesterov 
28476e84d644SOleg Nesterov /**
2848401a8d04STejun Heo  * flush_delayed_work - wait for a dwork to finish executing the last queueing
2849401a8d04STejun Heo  * @dwork: the delayed work to flush
28506e84d644SOleg Nesterov  *
2851401a8d04STejun Heo  * Delayed timer is cancelled and the pending work is queued for
2852401a8d04STejun Heo  * immediate execution.  Like flush_work(), this function only
2853401a8d04STejun Heo  * considers the last queueing instance of @dwork.
28541f1f642eSOleg Nesterov  *
2855401a8d04STejun Heo  * RETURNS:
2856401a8d04STejun Heo  * %true if flush_work() waited for the work to finish execution,
2857401a8d04STejun Heo  * %false if it was already idle.
28586e84d644SOleg Nesterov  */
2859401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork)
2860401a8d04STejun Heo {
28618930cabaSTejun Heo 	local_irq_disable();
2862401a8d04STejun Heo 	if (del_timer_sync(&dwork->timer))
286360c057bcSLai Jiangshan 		__queue_work(dwork->cpu, dwork->wq, &dwork->work);
28648930cabaSTejun Heo 	local_irq_enable();
2865401a8d04STejun Heo 	return flush_work(&dwork->work);
2866401a8d04STejun Heo }
2867401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work);
2868401a8d04STejun Heo 
2869401a8d04STejun Heo /**
287057b30ae7STejun Heo  * cancel_delayed_work - cancel a delayed work
287157b30ae7STejun Heo  * @dwork: delayed_work to cancel
287209383498STejun Heo  *
287357b30ae7STejun Heo  * Kill off a pending delayed_work.  Returns %true if @dwork was pending
287457b30ae7STejun Heo  * and canceled; %false if wasn't pending.  Note that the work callback
287557b30ae7STejun Heo  * function may still be running on return, unless it returns %true and the
287657b30ae7STejun Heo  * work doesn't re-arm itself.  Explicitly flush or use
287757b30ae7STejun Heo  * cancel_delayed_work_sync() to wait on it.
287809383498STejun Heo  *
287957b30ae7STejun Heo  * This function is safe to call from any context including IRQ handler.
288009383498STejun Heo  */
288157b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork)
288209383498STejun Heo {
288357b30ae7STejun Heo 	unsigned long flags;
288457b30ae7STejun Heo 	int ret;
288557b30ae7STejun Heo 
288657b30ae7STejun Heo 	do {
288757b30ae7STejun Heo 		ret = try_to_grab_pending(&dwork->work, true, &flags);
288857b30ae7STejun Heo 	} while (unlikely(ret == -EAGAIN));
288957b30ae7STejun Heo 
289057b30ae7STejun Heo 	if (unlikely(ret < 0))
289157b30ae7STejun Heo 		return false;
289257b30ae7STejun Heo 
28937c3eed5cSTejun Heo 	set_work_pool_and_clear_pending(&dwork->work,
28947c3eed5cSTejun Heo 					get_work_pool_id(&dwork->work));
289557b30ae7STejun Heo 	local_irq_restore(flags);
2896c0158ca6SDan Magenheimer 	return ret;
289709383498STejun Heo }
289857b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work);
289909383498STejun Heo 
290009383498STejun Heo /**
2901401a8d04STejun Heo  * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2902401a8d04STejun Heo  * @dwork: the delayed work cancel
2903401a8d04STejun Heo  *
2904401a8d04STejun Heo  * This is cancel_work_sync() for delayed works.
2905401a8d04STejun Heo  *
2906401a8d04STejun Heo  * RETURNS:
2907401a8d04STejun Heo  * %true if @dwork was pending, %false otherwise.
2908401a8d04STejun Heo  */
2909401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork)
29106e84d644SOleg Nesterov {
291136e227d2STejun Heo 	return __cancel_work_timer(&dwork->work, true);
29126e84d644SOleg Nesterov }
2913f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync);
29141da177e4SLinus Torvalds 
29150fcb78c2SRolf Eike Beer /**
2916c1a220e7SZhang Rui  * schedule_work_on - put work task on a specific cpu
2917c1a220e7SZhang Rui  * @cpu: cpu to put the work task on
2918c1a220e7SZhang Rui  * @work: job to be done
2919c1a220e7SZhang Rui  *
2920c1a220e7SZhang Rui  * This puts a job on a specific cpu
2921c1a220e7SZhang Rui  */
2922d4283e93STejun Heo bool schedule_work_on(int cpu, struct work_struct *work)
2923c1a220e7SZhang Rui {
2924d320c038STejun Heo 	return queue_work_on(cpu, system_wq, work);
2925c1a220e7SZhang Rui }
2926c1a220e7SZhang Rui EXPORT_SYMBOL(schedule_work_on);
2927c1a220e7SZhang Rui 
29280fcb78c2SRolf Eike Beer /**
2929ae90dd5dSDave Jones  * schedule_work - put work task in global workqueue
29301da177e4SLinus Torvalds  * @work: job to be done
29311da177e4SLinus Torvalds  *
2932d4283e93STejun Heo  * Returns %false if @work was already on the kernel-global workqueue and
2933d4283e93STejun Heo  * %true otherwise.
293452bad64dSDavid Howells  *
29350fcb78c2SRolf Eike Beer  * This puts a job in the kernel-global workqueue if it was not already
29360fcb78c2SRolf Eike Beer  * queued and leaves it in the same position on the kernel-global
29370fcb78c2SRolf Eike Beer  * workqueue otherwise.
29381da177e4SLinus Torvalds  */
2939d4283e93STejun Heo bool schedule_work(struct work_struct *work)
29401da177e4SLinus Torvalds {
29410fcb78c2SRolf Eike Beer 	return queue_work(system_wq, work);
29421da177e4SLinus Torvalds }
29430fcb78c2SRolf Eike Beer EXPORT_SYMBOL(schedule_work);
29441da177e4SLinus Torvalds 
29450fcb78c2SRolf Eike Beer /**
29460fcb78c2SRolf Eike Beer  * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
29470fcb78c2SRolf Eike Beer  * @cpu: cpu to use
29480fcb78c2SRolf Eike Beer  * @dwork: job to be done
29490fcb78c2SRolf Eike Beer  * @delay: number of jiffies to wait
29500fcb78c2SRolf Eike Beer  *
29510fcb78c2SRolf Eike Beer  * After waiting for a given time this puts a job in the kernel-global
29520fcb78c2SRolf Eike Beer  * workqueue on the specified CPU.
29530fcb78c2SRolf Eike Beer  */
2954d4283e93STejun Heo bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
2955d4283e93STejun Heo 			      unsigned long delay)
29561da177e4SLinus Torvalds {
2957d320c038STejun Heo 	return queue_delayed_work_on(cpu, system_wq, dwork, delay);
29581da177e4SLinus Torvalds }
2959ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work_on);
29601da177e4SLinus Torvalds 
2961b6136773SAndrew Morton /**
29620a13c00eSTejun Heo  * schedule_delayed_work - put work task in global workqueue after delay
29630a13c00eSTejun Heo  * @dwork: job to be done
29640a13c00eSTejun Heo  * @delay: number of jiffies to wait or 0 for immediate execution
29650a13c00eSTejun Heo  *
29660a13c00eSTejun Heo  * After waiting for a given time this puts a job in the kernel-global
29670a13c00eSTejun Heo  * workqueue.
29680a13c00eSTejun Heo  */
2969d4283e93STejun Heo bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay)
29700a13c00eSTejun Heo {
29710a13c00eSTejun Heo 	return queue_delayed_work(system_wq, dwork, delay);
29720a13c00eSTejun Heo }
29730a13c00eSTejun Heo EXPORT_SYMBOL(schedule_delayed_work);
29740a13c00eSTejun Heo 
29750a13c00eSTejun Heo /**
297631ddd871STejun Heo  * schedule_on_each_cpu - execute a function synchronously on each online CPU
2977b6136773SAndrew Morton  * @func: the function to call
2978b6136773SAndrew Morton  *
297931ddd871STejun Heo  * schedule_on_each_cpu() executes @func on each online CPU using the
298031ddd871STejun Heo  * system workqueue and blocks until all CPUs have completed.
2981b6136773SAndrew Morton  * schedule_on_each_cpu() is very slow.
298231ddd871STejun Heo  *
298331ddd871STejun Heo  * RETURNS:
298431ddd871STejun Heo  * 0 on success, -errno on failure.
2985b6136773SAndrew Morton  */
298665f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func)
298715316ba8SChristoph Lameter {
298815316ba8SChristoph Lameter 	int cpu;
298938f51568SNamhyung Kim 	struct work_struct __percpu *works;
299015316ba8SChristoph Lameter 
2991b6136773SAndrew Morton 	works = alloc_percpu(struct work_struct);
2992b6136773SAndrew Morton 	if (!works)
299315316ba8SChristoph Lameter 		return -ENOMEM;
2994b6136773SAndrew Morton 
299595402b38SGautham R Shenoy 	get_online_cpus();
299693981800STejun Heo 
299715316ba8SChristoph Lameter 	for_each_online_cpu(cpu) {
29989bfb1839SIngo Molnar 		struct work_struct *work = per_cpu_ptr(works, cpu);
29999bfb1839SIngo Molnar 
30009bfb1839SIngo Molnar 		INIT_WORK(work, func);
30018de6d308SOleg Nesterov 		schedule_work_on(cpu, work);
300215316ba8SChristoph Lameter 	}
300393981800STejun Heo 
300493981800STejun Heo 	for_each_online_cpu(cpu)
30058616a89aSOleg Nesterov 		flush_work(per_cpu_ptr(works, cpu));
300693981800STejun Heo 
300795402b38SGautham R Shenoy 	put_online_cpus();
3008b6136773SAndrew Morton 	free_percpu(works);
300915316ba8SChristoph Lameter 	return 0;
301015316ba8SChristoph Lameter }
301115316ba8SChristoph Lameter 
3012eef6a7d5SAlan Stern /**
3013eef6a7d5SAlan Stern  * flush_scheduled_work - ensure that any scheduled work has run to completion.
3014eef6a7d5SAlan Stern  *
3015eef6a7d5SAlan Stern  * Forces execution of the kernel-global workqueue and blocks until its
3016eef6a7d5SAlan Stern  * completion.
3017eef6a7d5SAlan Stern  *
3018eef6a7d5SAlan Stern  * Think twice before calling this function!  It's very easy to get into
3019eef6a7d5SAlan Stern  * trouble if you don't take great care.  Either of the following situations
3020eef6a7d5SAlan Stern  * will lead to deadlock:
3021eef6a7d5SAlan Stern  *
3022eef6a7d5SAlan Stern  *	One of the work items currently on the workqueue needs to acquire
3023eef6a7d5SAlan Stern  *	a lock held by your code or its caller.
3024eef6a7d5SAlan Stern  *
3025eef6a7d5SAlan Stern  *	Your code is running in the context of a work routine.
3026eef6a7d5SAlan Stern  *
3027eef6a7d5SAlan Stern  * They will be detected by lockdep when they occur, but the first might not
3028eef6a7d5SAlan Stern  * occur very often.  It depends on what work items are on the workqueue and
3029eef6a7d5SAlan Stern  * what locks they need, which you have no control over.
3030eef6a7d5SAlan Stern  *
3031eef6a7d5SAlan Stern  * In most situations flushing the entire workqueue is overkill; you merely
3032eef6a7d5SAlan Stern  * need to know that a particular work item isn't queued and isn't running.
3033eef6a7d5SAlan Stern  * In such cases you should use cancel_delayed_work_sync() or
3034eef6a7d5SAlan Stern  * cancel_work_sync() instead.
3035eef6a7d5SAlan Stern  */
30361da177e4SLinus Torvalds void flush_scheduled_work(void)
30371da177e4SLinus Torvalds {
3038d320c038STejun Heo 	flush_workqueue(system_wq);
30391da177e4SLinus Torvalds }
3040ae90dd5dSDave Jones EXPORT_SYMBOL(flush_scheduled_work);
30411da177e4SLinus Torvalds 
30421da177e4SLinus Torvalds /**
30431fa44ecaSJames Bottomley  * execute_in_process_context - reliably execute the routine with user context
30441fa44ecaSJames Bottomley  * @fn:		the function to execute
30451fa44ecaSJames Bottomley  * @ew:		guaranteed storage for the execute work structure (must
30461fa44ecaSJames Bottomley  *		be available when the work executes)
30471fa44ecaSJames Bottomley  *
30481fa44ecaSJames Bottomley  * Executes the function immediately if process context is available,
30491fa44ecaSJames Bottomley  * otherwise schedules the function for delayed execution.
30501fa44ecaSJames Bottomley  *
30511fa44ecaSJames Bottomley  * Returns:	0 - function was executed
30521fa44ecaSJames Bottomley  *		1 - function was scheduled for execution
30531fa44ecaSJames Bottomley  */
305465f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew)
30551fa44ecaSJames Bottomley {
30561fa44ecaSJames Bottomley 	if (!in_interrupt()) {
305765f27f38SDavid Howells 		fn(&ew->work);
30581fa44ecaSJames Bottomley 		return 0;
30591fa44ecaSJames Bottomley 	}
30601fa44ecaSJames Bottomley 
306165f27f38SDavid Howells 	INIT_WORK(&ew->work, fn);
30621fa44ecaSJames Bottomley 	schedule_work(&ew->work);
30631fa44ecaSJames Bottomley 
30641fa44ecaSJames Bottomley 	return 1;
30651fa44ecaSJames Bottomley }
30661fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context);
30671fa44ecaSJames Bottomley 
30681da177e4SLinus Torvalds int keventd_up(void)
30691da177e4SLinus Torvalds {
3070d320c038STejun Heo 	return system_wq != NULL;
30711da177e4SLinus Torvalds }
30721da177e4SLinus Torvalds 
307330cdf249STejun Heo static int alloc_and_link_pwqs(struct workqueue_struct *wq)
30741da177e4SLinus Torvalds {
307549e3cf44STejun Heo 	bool highpri = wq->flags & WQ_HIGHPRI;
307630cdf249STejun Heo 	int cpu;
3077e1d8aa9fSFrederic Weisbecker 
307830cdf249STejun Heo 	if (!(wq->flags & WQ_UNBOUND)) {
3079420c0ddbSTejun Heo 		wq->cpu_pwqs = alloc_percpu(struct pool_workqueue);
3080420c0ddbSTejun Heo 		if (!wq->cpu_pwqs)
308130cdf249STejun Heo 			return -ENOMEM;
308230cdf249STejun Heo 
308330cdf249STejun Heo 		for_each_possible_cpu(cpu) {
30847fb98ea7STejun Heo 			struct pool_workqueue *pwq =
30857fb98ea7STejun Heo 				per_cpu_ptr(wq->cpu_pwqs, cpu);
308630cdf249STejun Heo 
308749e3cf44STejun Heo 			pwq->pool = get_std_worker_pool(cpu, highpri);
308830cdf249STejun Heo 			list_add_tail(&pwq->pwqs_node, &wq->pwqs);
308930cdf249STejun Heo 		}
309030cdf249STejun Heo 	} else {
309130cdf249STejun Heo 		struct pool_workqueue *pwq;
309230cdf249STejun Heo 
309330cdf249STejun Heo 		pwq = kmem_cache_zalloc(pwq_cache, GFP_KERNEL);
309430cdf249STejun Heo 		if (!pwq)
309530cdf249STejun Heo 			return -ENOMEM;
309630cdf249STejun Heo 
309749e3cf44STejun Heo 		pwq->pool = get_std_worker_pool(WORK_CPU_UNBOUND, highpri);
309830cdf249STejun Heo 		list_add_tail(&pwq->pwqs_node, &wq->pwqs);
309930cdf249STejun Heo 	}
310030cdf249STejun Heo 
310130cdf249STejun Heo 	return 0;
31020f900049STejun Heo }
31030f900049STejun Heo 
3104112202d9STejun Heo static void free_pwqs(struct workqueue_struct *wq)
310506ba38a9SOleg Nesterov {
3106e06ffa1eSLai Jiangshan 	if (!(wq->flags & WQ_UNBOUND))
3107420c0ddbSTejun Heo 		free_percpu(wq->cpu_pwqs);
3108420c0ddbSTejun Heo 	else if (!list_empty(&wq->pwqs))
3109420c0ddbSTejun Heo 		kmem_cache_free(pwq_cache, list_first_entry(&wq->pwqs,
3110420c0ddbSTejun Heo 					struct pool_workqueue, pwqs_node));
311106ba38a9SOleg Nesterov }
311206ba38a9SOleg Nesterov 
3113f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags,
3114f3421797STejun Heo 			       const char *name)
3115b71ab8c2STejun Heo {
3116f3421797STejun Heo 	int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3117f3421797STejun Heo 
3118f3421797STejun Heo 	if (max_active < 1 || max_active > lim)
3119044c782cSValentin Ilie 		pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
3120f3421797STejun Heo 			max_active, name, 1, lim);
3121b71ab8c2STejun Heo 
3122f3421797STejun Heo 	return clamp_val(max_active, 1, lim);
3123b71ab8c2STejun Heo }
3124b71ab8c2STejun Heo 
3125b196be89STejun Heo struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
312697e37d7bSTejun Heo 					       unsigned int flags,
31271e19ffc6STejun Heo 					       int max_active,
3128eb13ba87SJohannes Berg 					       struct lock_class_key *key,
3129b196be89STejun Heo 					       const char *lock_name, ...)
31303af24433SOleg Nesterov {
3131b196be89STejun Heo 	va_list args, args1;
31323af24433SOleg Nesterov 	struct workqueue_struct *wq;
313349e3cf44STejun Heo 	struct pool_workqueue *pwq;
3134b196be89STejun Heo 	size_t namelen;
3135b196be89STejun Heo 
3136b196be89STejun Heo 	/* determine namelen, allocate wq and format name */
3137b196be89STejun Heo 	va_start(args, lock_name);
3138b196be89STejun Heo 	va_copy(args1, args);
3139b196be89STejun Heo 	namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3140b196be89STejun Heo 
3141b196be89STejun Heo 	wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3142b196be89STejun Heo 	if (!wq)
3143b196be89STejun Heo 		goto err;
3144b196be89STejun Heo 
3145b196be89STejun Heo 	vsnprintf(wq->name, namelen, fmt, args1);
3146b196be89STejun Heo 	va_end(args);
3147b196be89STejun Heo 	va_end(args1);
31483af24433SOleg Nesterov 
3149f3421797STejun Heo 	/*
31506370a6adSTejun Heo 	 * Workqueues which may be used during memory reclaim should
31516370a6adSTejun Heo 	 * have a rescuer to guarantee forward progress.
31526370a6adSTejun Heo 	 */
31536370a6adSTejun Heo 	if (flags & WQ_MEM_RECLAIM)
31546370a6adSTejun Heo 		flags |= WQ_RESCUER;
31556370a6adSTejun Heo 
3156d320c038STejun Heo 	max_active = max_active ?: WQ_DFL_ACTIVE;
3157b196be89STejun Heo 	max_active = wq_clamp_max_active(max_active, flags, wq->name);
31583af24433SOleg Nesterov 
3159b196be89STejun Heo 	/* init wq */
316097e37d7bSTejun Heo 	wq->flags = flags;
3161a0a1a5fdSTejun Heo 	wq->saved_max_active = max_active;
316273f53c4aSTejun Heo 	mutex_init(&wq->flush_mutex);
3163112202d9STejun Heo 	atomic_set(&wq->nr_pwqs_to_flush, 0);
316430cdf249STejun Heo 	INIT_LIST_HEAD(&wq->pwqs);
316573f53c4aSTejun Heo 	INIT_LIST_HEAD(&wq->flusher_queue);
316673f53c4aSTejun Heo 	INIT_LIST_HEAD(&wq->flusher_overflow);
3167493a1724STejun Heo 	INIT_LIST_HEAD(&wq->maydays);
31683af24433SOleg Nesterov 
3169eb13ba87SJohannes Berg 	lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
3170cce1a165SOleg Nesterov 	INIT_LIST_HEAD(&wq->list);
31713af24433SOleg Nesterov 
317230cdf249STejun Heo 	if (alloc_and_link_pwqs(wq) < 0)
3173bdbc5dd7STejun Heo 		goto err;
3174bdbc5dd7STejun Heo 
317549e3cf44STejun Heo 	for_each_pwq(pwq, wq) {
3176112202d9STejun Heo 		BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
3177112202d9STejun Heo 		pwq->wq = wq;
3178112202d9STejun Heo 		pwq->flush_color = -1;
3179112202d9STejun Heo 		pwq->max_active = max_active;
3180112202d9STejun Heo 		INIT_LIST_HEAD(&pwq->delayed_works);
3181493a1724STejun Heo 		INIT_LIST_HEAD(&pwq->mayday_node);
3182e22bee78STejun Heo 	}
31831537663fSTejun Heo 
3184e22bee78STejun Heo 	if (flags & WQ_RESCUER) {
3185e22bee78STejun Heo 		struct worker *rescuer;
3186e22bee78STejun Heo 
3187e22bee78STejun Heo 		wq->rescuer = rescuer = alloc_worker();
3188e22bee78STejun Heo 		if (!rescuer)
3189e22bee78STejun Heo 			goto err;
3190e22bee78STejun Heo 
3191111c225aSTejun Heo 		rescuer->rescue_wq = wq;
3192111c225aSTejun Heo 		rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
3193b196be89STejun Heo 					       wq->name);
3194e22bee78STejun Heo 		if (IS_ERR(rescuer->task))
3195e22bee78STejun Heo 			goto err;
3196e22bee78STejun Heo 
3197e22bee78STejun Heo 		rescuer->task->flags |= PF_THREAD_BOUND;
3198e22bee78STejun Heo 		wake_up_process(rescuer->task);
31993af24433SOleg Nesterov 	}
32001537663fSTejun Heo 
32013af24433SOleg Nesterov 	/*
3202a0a1a5fdSTejun Heo 	 * workqueue_lock protects global freeze state and workqueues
3203a0a1a5fdSTejun Heo 	 * list.  Grab it, set max_active accordingly and add the new
3204a0a1a5fdSTejun Heo 	 * workqueue to workqueues list.
32053af24433SOleg Nesterov 	 */
3206e98d5b16STejun Heo 	spin_lock_irq(&workqueue_lock);
3207a0a1a5fdSTejun Heo 
320858a69cb4STejun Heo 	if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
320949e3cf44STejun Heo 		for_each_pwq(pwq, wq)
321049e3cf44STejun Heo 			pwq->max_active = 0;
3211a0a1a5fdSTejun Heo 
32123af24433SOleg Nesterov 	list_add(&wq->list, &workqueues);
3213a0a1a5fdSTejun Heo 
3214e98d5b16STejun Heo 	spin_unlock_irq(&workqueue_lock);
32153af24433SOleg Nesterov 
32163af24433SOleg Nesterov 	return wq;
32174690c4abSTejun Heo err:
32184690c4abSTejun Heo 	if (wq) {
3219112202d9STejun Heo 		free_pwqs(wq);
3220e22bee78STejun Heo 		kfree(wq->rescuer);
32214690c4abSTejun Heo 		kfree(wq);
32223af24433SOleg Nesterov 	}
32234690c4abSTejun Heo 	return NULL;
32241da177e4SLinus Torvalds }
3225d320c038STejun Heo EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
32261da177e4SLinus Torvalds 
32273af24433SOleg Nesterov /**
32283af24433SOleg Nesterov  * destroy_workqueue - safely terminate a workqueue
32293af24433SOleg Nesterov  * @wq: target workqueue
32303af24433SOleg Nesterov  *
32313af24433SOleg Nesterov  * Safely destroy a workqueue. All work currently pending will be done first.
32323af24433SOleg Nesterov  */
32333af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq)
32343af24433SOleg Nesterov {
323549e3cf44STejun Heo 	struct pool_workqueue *pwq;
32363af24433SOleg Nesterov 
32379c5a2ba7STejun Heo 	/* drain it before proceeding with destruction */
32389c5a2ba7STejun Heo 	drain_workqueue(wq);
3239c8efcc25STejun Heo 
32406183c009STejun Heo 	/* sanity checks */
324149e3cf44STejun Heo 	for_each_pwq(pwq, wq) {
32426183c009STejun Heo 		int i;
32436183c009STejun Heo 
32446183c009STejun Heo 		for (i = 0; i < WORK_NR_COLORS; i++)
32456183c009STejun Heo 			if (WARN_ON(pwq->nr_in_flight[i]))
32466183c009STejun Heo 				return;
32476183c009STejun Heo 		if (WARN_ON(pwq->nr_active) ||
32486183c009STejun Heo 		    WARN_ON(!list_empty(&pwq->delayed_works)))
32496183c009STejun Heo 			return;
32506183c009STejun Heo 	}
32516183c009STejun Heo 
3252a0a1a5fdSTejun Heo 	/*
3253a0a1a5fdSTejun Heo 	 * wq list is used to freeze wq, remove from list after
3254a0a1a5fdSTejun Heo 	 * flushing is complete in case freeze races us.
3255a0a1a5fdSTejun Heo 	 */
3256e98d5b16STejun Heo 	spin_lock_irq(&workqueue_lock);
32573af24433SOleg Nesterov 	list_del(&wq->list);
3258e98d5b16STejun Heo 	spin_unlock_irq(&workqueue_lock);
32593af24433SOleg Nesterov 
3260e22bee78STejun Heo 	if (wq->flags & WQ_RESCUER) {
3261e22bee78STejun Heo 		kthread_stop(wq->rescuer->task);
32628d9df9f0SXiaotian Feng 		kfree(wq->rescuer);
3263e22bee78STejun Heo 	}
3264e22bee78STejun Heo 
3265112202d9STejun Heo 	free_pwqs(wq);
32663af24433SOleg Nesterov 	kfree(wq);
32673af24433SOleg Nesterov }
32683af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue);
32693af24433SOleg Nesterov 
3270dcd989cbSTejun Heo /**
3271112202d9STejun Heo  * pwq_set_max_active - adjust max_active of a pwq
3272112202d9STejun Heo  * @pwq: target pool_workqueue
32739f4bd4cdSLai Jiangshan  * @max_active: new max_active value.
32749f4bd4cdSLai Jiangshan  *
3275112202d9STejun Heo  * Set @pwq->max_active to @max_active and activate delayed works if
32769f4bd4cdSLai Jiangshan  * increased.
32779f4bd4cdSLai Jiangshan  *
32789f4bd4cdSLai Jiangshan  * CONTEXT:
3279d565ed63STejun Heo  * spin_lock_irq(pool->lock).
32809f4bd4cdSLai Jiangshan  */
3281112202d9STejun Heo static void pwq_set_max_active(struct pool_workqueue *pwq, int max_active)
32829f4bd4cdSLai Jiangshan {
3283112202d9STejun Heo 	pwq->max_active = max_active;
32849f4bd4cdSLai Jiangshan 
3285112202d9STejun Heo 	while (!list_empty(&pwq->delayed_works) &&
3286112202d9STejun Heo 	       pwq->nr_active < pwq->max_active)
3287112202d9STejun Heo 		pwq_activate_first_delayed(pwq);
32889f4bd4cdSLai Jiangshan }
32899f4bd4cdSLai Jiangshan 
32909f4bd4cdSLai Jiangshan /**
3291dcd989cbSTejun Heo  * workqueue_set_max_active - adjust max_active of a workqueue
3292dcd989cbSTejun Heo  * @wq: target workqueue
3293dcd989cbSTejun Heo  * @max_active: new max_active value.
3294dcd989cbSTejun Heo  *
3295dcd989cbSTejun Heo  * Set max_active of @wq to @max_active.
3296dcd989cbSTejun Heo  *
3297dcd989cbSTejun Heo  * CONTEXT:
3298dcd989cbSTejun Heo  * Don't call from IRQ context.
3299dcd989cbSTejun Heo  */
3300dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3301dcd989cbSTejun Heo {
330249e3cf44STejun Heo 	struct pool_workqueue *pwq;
3303dcd989cbSTejun Heo 
3304f3421797STejun Heo 	max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
3305dcd989cbSTejun Heo 
3306e98d5b16STejun Heo 	spin_lock_irq(&workqueue_lock);
3307dcd989cbSTejun Heo 
3308dcd989cbSTejun Heo 	wq->saved_max_active = max_active;
3309dcd989cbSTejun Heo 
331049e3cf44STejun Heo 	for_each_pwq(pwq, wq) {
3311112202d9STejun Heo 		struct worker_pool *pool = pwq->pool;
3312dcd989cbSTejun Heo 
3313e98d5b16STejun Heo 		spin_lock(&pool->lock);
3314dcd989cbSTejun Heo 
331558a69cb4STejun Heo 		if (!(wq->flags & WQ_FREEZABLE) ||
331635b6bb63STejun Heo 		    !(pool->flags & POOL_FREEZING))
3317112202d9STejun Heo 			pwq_set_max_active(pwq, max_active);
3318dcd989cbSTejun Heo 
3319e98d5b16STejun Heo 		spin_unlock(&pool->lock);
3320dcd989cbSTejun Heo 	}
3321dcd989cbSTejun Heo 
3322e98d5b16STejun Heo 	spin_unlock_irq(&workqueue_lock);
3323dcd989cbSTejun Heo }
3324dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3325dcd989cbSTejun Heo 
3326dcd989cbSTejun Heo /**
3327dcd989cbSTejun Heo  * workqueue_congested - test whether a workqueue is congested
3328dcd989cbSTejun Heo  * @cpu: CPU in question
3329dcd989cbSTejun Heo  * @wq: target workqueue
3330dcd989cbSTejun Heo  *
3331dcd989cbSTejun Heo  * Test whether @wq's cpu workqueue for @cpu is congested.  There is
3332dcd989cbSTejun Heo  * no synchronization around this function and the test result is
3333dcd989cbSTejun Heo  * unreliable and only useful as advisory hints or for debugging.
3334dcd989cbSTejun Heo  *
3335dcd989cbSTejun Heo  * RETURNS:
3336dcd989cbSTejun Heo  * %true if congested, %false otherwise.
3337dcd989cbSTejun Heo  */
3338d84ff051STejun Heo bool workqueue_congested(int cpu, struct workqueue_struct *wq)
3339dcd989cbSTejun Heo {
33407fb98ea7STejun Heo 	struct pool_workqueue *pwq;
33417fb98ea7STejun Heo 
33427fb98ea7STejun Heo 	if (!(wq->flags & WQ_UNBOUND))
33437fb98ea7STejun Heo 		pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
33447fb98ea7STejun Heo 	else
33457fb98ea7STejun Heo 		pwq = first_pwq(wq);
3346dcd989cbSTejun Heo 
3347112202d9STejun Heo 	return !list_empty(&pwq->delayed_works);
3348dcd989cbSTejun Heo }
3349dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested);
3350dcd989cbSTejun Heo 
3351dcd989cbSTejun Heo /**
3352dcd989cbSTejun Heo  * work_busy - test whether a work is currently pending or running
3353dcd989cbSTejun Heo  * @work: the work to be tested
3354dcd989cbSTejun Heo  *
3355dcd989cbSTejun Heo  * Test whether @work is currently pending or running.  There is no
3356dcd989cbSTejun Heo  * synchronization around this function and the test result is
3357dcd989cbSTejun Heo  * unreliable and only useful as advisory hints or for debugging.
3358dcd989cbSTejun Heo  *
3359dcd989cbSTejun Heo  * RETURNS:
3360dcd989cbSTejun Heo  * OR'd bitmask of WORK_BUSY_* bits.
3361dcd989cbSTejun Heo  */
3362dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work)
3363dcd989cbSTejun Heo {
3364c9e7cf27STejun Heo 	struct worker_pool *pool = get_work_pool(work);
3365dcd989cbSTejun Heo 	unsigned long flags;
3366dcd989cbSTejun Heo 	unsigned int ret = 0;
3367dcd989cbSTejun Heo 
3368dcd989cbSTejun Heo 	if (work_pending(work))
3369dcd989cbSTejun Heo 		ret |= WORK_BUSY_PENDING;
3370038366c5SLai Jiangshan 
3371038366c5SLai Jiangshan 	if (pool) {
3372038366c5SLai Jiangshan 		spin_lock_irqsave(&pool->lock, flags);
3373c9e7cf27STejun Heo 		if (find_worker_executing_work(pool, work))
3374dcd989cbSTejun Heo 			ret |= WORK_BUSY_RUNNING;
3375d565ed63STejun Heo 		spin_unlock_irqrestore(&pool->lock, flags);
3376038366c5SLai Jiangshan 	}
3377dcd989cbSTejun Heo 
3378dcd989cbSTejun Heo 	return ret;
3379dcd989cbSTejun Heo }
3380dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy);
3381dcd989cbSTejun Heo 
3382db7bccf4STejun Heo /*
3383db7bccf4STejun Heo  * CPU hotplug.
3384db7bccf4STejun Heo  *
3385e22bee78STejun Heo  * There are two challenges in supporting CPU hotplug.  Firstly, there
3386112202d9STejun Heo  * are a lot of assumptions on strong associations among work, pwq and
3387706026c2STejun Heo  * pool which make migrating pending and scheduled works very
3388e22bee78STejun Heo  * difficult to implement without impacting hot paths.  Secondly,
338994cf58bbSTejun Heo  * worker pools serve mix of short, long and very long running works making
3390e22bee78STejun Heo  * blocked draining impractical.
3391e22bee78STejun Heo  *
339224647570STejun Heo  * This is solved by allowing the pools to be disassociated from the CPU
3393628c78e7STejun Heo  * running as an unbound one and allowing it to be reattached later if the
3394628c78e7STejun Heo  * cpu comes back online.
3395db7bccf4STejun Heo  */
3396db7bccf4STejun Heo 
3397706026c2STejun Heo static void wq_unbind_fn(struct work_struct *work)
3398db7bccf4STejun Heo {
339938db41d9STejun Heo 	int cpu = smp_processor_id();
34004ce62e9eSTejun Heo 	struct worker_pool *pool;
3401db7bccf4STejun Heo 	struct worker *worker;
3402db7bccf4STejun Heo 	int i;
3403db7bccf4STejun Heo 
340438db41d9STejun Heo 	for_each_std_worker_pool(pool, cpu) {
34056183c009STejun Heo 		WARN_ON_ONCE(cpu != smp_processor_id());
3406db7bccf4STejun Heo 
340794cf58bbSTejun Heo 		mutex_lock(&pool->assoc_mutex);
340894cf58bbSTejun Heo 		spin_lock_irq(&pool->lock);
3409e22bee78STejun Heo 
3410f2d5a0eeSTejun Heo 		/*
341194cf58bbSTejun Heo 		 * We've claimed all manager positions.  Make all workers
341294cf58bbSTejun Heo 		 * unbound and set DISASSOCIATED.  Before this, all workers
341394cf58bbSTejun Heo 		 * except for the ones which are still executing works from
341494cf58bbSTejun Heo 		 * before the last CPU down must be on the cpu.  After
341594cf58bbSTejun Heo 		 * this, they may become diasporas.
3416f2d5a0eeSTejun Heo 		 */
34174ce62e9eSTejun Heo 		list_for_each_entry(worker, &pool->idle_list, entry)
3418403c821dSTejun Heo 			worker->flags |= WORKER_UNBOUND;
3419db7bccf4STejun Heo 
3420b67bfe0dSSasha Levin 		for_each_busy_worker(worker, i, pool)
3421403c821dSTejun Heo 			worker->flags |= WORKER_UNBOUND;
3422db7bccf4STejun Heo 
342324647570STejun Heo 		pool->flags |= POOL_DISASSOCIATED;
3424f2d5a0eeSTejun Heo 
342594cf58bbSTejun Heo 		spin_unlock_irq(&pool->lock);
342694cf58bbSTejun Heo 		mutex_unlock(&pool->assoc_mutex);
342794cf58bbSTejun Heo 	}
3428e22bee78STejun Heo 
3429e22bee78STejun Heo 	/*
3430628c78e7STejun Heo 	 * Call schedule() so that we cross rq->lock and thus can guarantee
3431628c78e7STejun Heo 	 * sched callbacks see the %WORKER_UNBOUND flag.  This is necessary
3432628c78e7STejun Heo 	 * as scheduler callbacks may be invoked from other cpus.
3433628c78e7STejun Heo 	 */
3434628c78e7STejun Heo 	schedule();
3435628c78e7STejun Heo 
3436628c78e7STejun Heo 	/*
3437628c78e7STejun Heo 	 * Sched callbacks are disabled now.  Zap nr_running.  After this,
3438628c78e7STejun Heo 	 * nr_running stays zero and need_more_worker() and keep_working()
343938db41d9STejun Heo 	 * are always true as long as the worklist is not empty.  Pools on
344038db41d9STejun Heo 	 * @cpu now behave as unbound (in terms of concurrency management)
344138db41d9STejun Heo 	 * pools which are served by workers tied to the CPU.
3442628c78e7STejun Heo 	 *
3443628c78e7STejun Heo 	 * On return from this function, the current worker would trigger
3444628c78e7STejun Heo 	 * unbound chain execution of pending work items if other workers
3445628c78e7STejun Heo 	 * didn't already.
3446e22bee78STejun Heo 	 */
344738db41d9STejun Heo 	for_each_std_worker_pool(pool, cpu)
3448e19e397aSTejun Heo 		atomic_set(&pool->nr_running, 0);
3449db7bccf4STejun Heo }
3450db7bccf4STejun Heo 
34518db25e78STejun Heo /*
34528db25e78STejun Heo  * Workqueues should be brought up before normal priority CPU notifiers.
34538db25e78STejun Heo  * This will be registered high priority CPU notifier.
34548db25e78STejun Heo  */
34559fdf9b73SLai Jiangshan static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb,
34561da177e4SLinus Torvalds 					       unsigned long action,
34571da177e4SLinus Torvalds 					       void *hcpu)
34581da177e4SLinus Torvalds {
3459d84ff051STejun Heo 	int cpu = (unsigned long)hcpu;
34604ce62e9eSTejun Heo 	struct worker_pool *pool;
34611da177e4SLinus Torvalds 
34628db25e78STejun Heo 	switch (action & ~CPU_TASKS_FROZEN) {
34633af24433SOleg Nesterov 	case CPU_UP_PREPARE:
346438db41d9STejun Heo 		for_each_std_worker_pool(pool, cpu) {
34653ce63377STejun Heo 			struct worker *worker;
34663ce63377STejun Heo 
34673ce63377STejun Heo 			if (pool->nr_workers)
34683ce63377STejun Heo 				continue;
34693ce63377STejun Heo 
34703ce63377STejun Heo 			worker = create_worker(pool);
34713ce63377STejun Heo 			if (!worker)
34723ce63377STejun Heo 				return NOTIFY_BAD;
34733ce63377STejun Heo 
3474d565ed63STejun Heo 			spin_lock_irq(&pool->lock);
34753ce63377STejun Heo 			start_worker(worker);
3476d565ed63STejun Heo 			spin_unlock_irq(&pool->lock);
34773af24433SOleg Nesterov 		}
34781da177e4SLinus Torvalds 		break;
34791da177e4SLinus Torvalds 
348065758202STejun Heo 	case CPU_DOWN_FAILED:
348165758202STejun Heo 	case CPU_ONLINE:
348238db41d9STejun Heo 		for_each_std_worker_pool(pool, cpu) {
348394cf58bbSTejun Heo 			mutex_lock(&pool->assoc_mutex);
348494cf58bbSTejun Heo 			spin_lock_irq(&pool->lock);
348594cf58bbSTejun Heo 
348624647570STejun Heo 			pool->flags &= ~POOL_DISASSOCIATED;
348794cf58bbSTejun Heo 			rebind_workers(pool);
348894cf58bbSTejun Heo 
348994cf58bbSTejun Heo 			spin_unlock_irq(&pool->lock);
349094cf58bbSTejun Heo 			mutex_unlock(&pool->assoc_mutex);
349194cf58bbSTejun Heo 		}
34928db25e78STejun Heo 		break;
349365758202STejun Heo 	}
349465758202STejun Heo 	return NOTIFY_OK;
349565758202STejun Heo }
349665758202STejun Heo 
349765758202STejun Heo /*
349865758202STejun Heo  * Workqueues should be brought down after normal priority CPU notifiers.
349965758202STejun Heo  * This will be registered as low priority CPU notifier.
350065758202STejun Heo  */
35019fdf9b73SLai Jiangshan static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb,
350265758202STejun Heo 						 unsigned long action,
350365758202STejun Heo 						 void *hcpu)
350465758202STejun Heo {
3505d84ff051STejun Heo 	int cpu = (unsigned long)hcpu;
35068db25e78STejun Heo 	struct work_struct unbind_work;
35078db25e78STejun Heo 
350865758202STejun Heo 	switch (action & ~CPU_TASKS_FROZEN) {
350965758202STejun Heo 	case CPU_DOWN_PREPARE:
35108db25e78STejun Heo 		/* unbinding should happen on the local CPU */
3511706026c2STejun Heo 		INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
35127635d2fdSJoonsoo Kim 		queue_work_on(cpu, system_highpri_wq, &unbind_work);
35138db25e78STejun Heo 		flush_work(&unbind_work);
35148db25e78STejun Heo 		break;
351565758202STejun Heo 	}
351665758202STejun Heo 	return NOTIFY_OK;
351765758202STejun Heo }
351865758202STejun Heo 
35192d3854a3SRusty Russell #ifdef CONFIG_SMP
35208ccad40dSRusty Russell 
35212d3854a3SRusty Russell struct work_for_cpu {
3522ed48ece2STejun Heo 	struct work_struct work;
35232d3854a3SRusty Russell 	long (*fn)(void *);
35242d3854a3SRusty Russell 	void *arg;
35252d3854a3SRusty Russell 	long ret;
35262d3854a3SRusty Russell };
35272d3854a3SRusty Russell 
3528ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work)
35292d3854a3SRusty Russell {
3530ed48ece2STejun Heo 	struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
3531ed48ece2STejun Heo 
35322d3854a3SRusty Russell 	wfc->ret = wfc->fn(wfc->arg);
35332d3854a3SRusty Russell }
35342d3854a3SRusty Russell 
35352d3854a3SRusty Russell /**
35362d3854a3SRusty Russell  * work_on_cpu - run a function in user context on a particular cpu
35372d3854a3SRusty Russell  * @cpu: the cpu to run on
35382d3854a3SRusty Russell  * @fn: the function to run
35392d3854a3SRusty Russell  * @arg: the function arg
35402d3854a3SRusty Russell  *
354131ad9081SRusty Russell  * This will return the value @fn returns.
354231ad9081SRusty Russell  * It is up to the caller to ensure that the cpu doesn't go offline.
35436b44003eSAndrew Morton  * The caller must not hold any locks which would prevent @fn from completing.
35442d3854a3SRusty Russell  */
3545d84ff051STejun Heo long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
35462d3854a3SRusty Russell {
3547ed48ece2STejun Heo 	struct work_for_cpu wfc = { .fn = fn, .arg = arg };
35482d3854a3SRusty Russell 
3549ed48ece2STejun Heo 	INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
3550ed48ece2STejun Heo 	schedule_work_on(cpu, &wfc.work);
3551ed48ece2STejun Heo 	flush_work(&wfc.work);
35522d3854a3SRusty Russell 	return wfc.ret;
35532d3854a3SRusty Russell }
35542d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu);
35552d3854a3SRusty Russell #endif /* CONFIG_SMP */
35562d3854a3SRusty Russell 
3557a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER
3558e7577c50SRusty Russell 
3559a0a1a5fdSTejun Heo /**
3560a0a1a5fdSTejun Heo  * freeze_workqueues_begin - begin freezing workqueues
3561a0a1a5fdSTejun Heo  *
356258a69cb4STejun Heo  * Start freezing workqueues.  After this function returns, all freezable
356358a69cb4STejun Heo  * workqueues will queue new works to their frozen_works list instead of
3564706026c2STejun Heo  * pool->worklist.
3565a0a1a5fdSTejun Heo  *
3566a0a1a5fdSTejun Heo  * CONTEXT:
3567d565ed63STejun Heo  * Grabs and releases workqueue_lock and pool->lock's.
3568a0a1a5fdSTejun Heo  */
3569a0a1a5fdSTejun Heo void freeze_workqueues_begin(void)
3570a0a1a5fdSTejun Heo {
357117116969STejun Heo 	struct worker_pool *pool;
357224b8a847STejun Heo 	struct workqueue_struct *wq;
357324b8a847STejun Heo 	struct pool_workqueue *pwq;
357417116969STejun Heo 	int id;
3575a0a1a5fdSTejun Heo 
3576e98d5b16STejun Heo 	spin_lock_irq(&workqueue_lock);
3577a0a1a5fdSTejun Heo 
35786183c009STejun Heo 	WARN_ON_ONCE(workqueue_freezing);
3579a0a1a5fdSTejun Heo 	workqueue_freezing = true;
3580a0a1a5fdSTejun Heo 
358124b8a847STejun Heo 	/* set FREEZING */
358217116969STejun Heo 	for_each_pool(pool, id) {
3583e98d5b16STejun Heo 		spin_lock(&pool->lock);
358435b6bb63STejun Heo 		WARN_ON_ONCE(pool->flags & POOL_FREEZING);
358535b6bb63STejun Heo 		pool->flags |= POOL_FREEZING;
358624b8a847STejun Heo 		spin_unlock(&pool->lock);
35871da177e4SLinus Torvalds 	}
35888b03ae3cSTejun Heo 
358924b8a847STejun Heo 	/* suppress further executions by setting max_active to zero */
359024b8a847STejun Heo 	list_for_each_entry(wq, &workqueues, list) {
359124b8a847STejun Heo 		if (!(wq->flags & WQ_FREEZABLE))
359224b8a847STejun Heo 			continue;
359324b8a847STejun Heo 
359424b8a847STejun Heo 		for_each_pwq(pwq, wq) {
359524b8a847STejun Heo 			spin_lock(&pwq->pool->lock);
359624b8a847STejun Heo 			pwq->max_active = 0;
359724b8a847STejun Heo 			spin_unlock(&pwq->pool->lock);
359824b8a847STejun Heo 		}
3599a1056305STejun Heo 	}
3600a0a1a5fdSTejun Heo 
3601e98d5b16STejun Heo 	spin_unlock_irq(&workqueue_lock);
3602a0a1a5fdSTejun Heo }
3603a0a1a5fdSTejun Heo 
3604a0a1a5fdSTejun Heo /**
360558a69cb4STejun Heo  * freeze_workqueues_busy - are freezable workqueues still busy?
3606a0a1a5fdSTejun Heo  *
3607a0a1a5fdSTejun Heo  * Check whether freezing is complete.  This function must be called
3608a0a1a5fdSTejun Heo  * between freeze_workqueues_begin() and thaw_workqueues().
3609a0a1a5fdSTejun Heo  *
3610a0a1a5fdSTejun Heo  * CONTEXT:
3611a0a1a5fdSTejun Heo  * Grabs and releases workqueue_lock.
3612a0a1a5fdSTejun Heo  *
3613a0a1a5fdSTejun Heo  * RETURNS:
361458a69cb4STejun Heo  * %true if some freezable workqueues are still busy.  %false if freezing
361558a69cb4STejun Heo  * is complete.
3616a0a1a5fdSTejun Heo  */
3617a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void)
3618a0a1a5fdSTejun Heo {
3619a0a1a5fdSTejun Heo 	bool busy = false;
362024b8a847STejun Heo 	struct workqueue_struct *wq;
362124b8a847STejun Heo 	struct pool_workqueue *pwq;
3622a0a1a5fdSTejun Heo 
3623e98d5b16STejun Heo 	spin_lock_irq(&workqueue_lock);
3624a0a1a5fdSTejun Heo 
36256183c009STejun Heo 	WARN_ON_ONCE(!workqueue_freezing);
3626a0a1a5fdSTejun Heo 
362724b8a847STejun Heo 	list_for_each_entry(wq, &workqueues, list) {
362824b8a847STejun Heo 		if (!(wq->flags & WQ_FREEZABLE))
362924b8a847STejun Heo 			continue;
3630a0a1a5fdSTejun Heo 		/*
3631a0a1a5fdSTejun Heo 		 * nr_active is monotonically decreasing.  It's safe
3632a0a1a5fdSTejun Heo 		 * to peek without lock.
3633a0a1a5fdSTejun Heo 		 */
363424b8a847STejun Heo 		for_each_pwq(pwq, wq) {
36356183c009STejun Heo 			WARN_ON_ONCE(pwq->nr_active < 0);
3636112202d9STejun Heo 			if (pwq->nr_active) {
3637a0a1a5fdSTejun Heo 				busy = true;
3638a0a1a5fdSTejun Heo 				goto out_unlock;
3639a0a1a5fdSTejun Heo 			}
3640a0a1a5fdSTejun Heo 		}
3641a0a1a5fdSTejun Heo 	}
3642a0a1a5fdSTejun Heo out_unlock:
3643e98d5b16STejun Heo 	spin_unlock_irq(&workqueue_lock);
3644a0a1a5fdSTejun Heo 	return busy;
3645a0a1a5fdSTejun Heo }
3646a0a1a5fdSTejun Heo 
3647a0a1a5fdSTejun Heo /**
3648a0a1a5fdSTejun Heo  * thaw_workqueues - thaw workqueues
3649a0a1a5fdSTejun Heo  *
3650a0a1a5fdSTejun Heo  * Thaw workqueues.  Normal queueing is restored and all collected
3651706026c2STejun Heo  * frozen works are transferred to their respective pool worklists.
3652a0a1a5fdSTejun Heo  *
3653a0a1a5fdSTejun Heo  * CONTEXT:
3654d565ed63STejun Heo  * Grabs and releases workqueue_lock and pool->lock's.
3655a0a1a5fdSTejun Heo  */
3656a0a1a5fdSTejun Heo void thaw_workqueues(void)
3657a0a1a5fdSTejun Heo {
365824b8a847STejun Heo 	struct workqueue_struct *wq;
365924b8a847STejun Heo 	struct pool_workqueue *pwq;
366024b8a847STejun Heo 	struct worker_pool *pool;
366124b8a847STejun Heo 	int id;
3662a0a1a5fdSTejun Heo 
3663e98d5b16STejun Heo 	spin_lock_irq(&workqueue_lock);
3664a0a1a5fdSTejun Heo 
3665a0a1a5fdSTejun Heo 	if (!workqueue_freezing)
3666a0a1a5fdSTejun Heo 		goto out_unlock;
3667a0a1a5fdSTejun Heo 
366824b8a847STejun Heo 	/* clear FREEZING */
366924b8a847STejun Heo 	for_each_pool(pool, id) {
3670e98d5b16STejun Heo 		spin_lock(&pool->lock);
367135b6bb63STejun Heo 		WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
367235b6bb63STejun Heo 		pool->flags &= ~POOL_FREEZING;
3673e98d5b16STejun Heo 		spin_unlock(&pool->lock);
3674d565ed63STejun Heo 	}
367524b8a847STejun Heo 
367624b8a847STejun Heo 	/* restore max_active and repopulate worklist */
367724b8a847STejun Heo 	list_for_each_entry(wq, &workqueues, list) {
367824b8a847STejun Heo 		if (!(wq->flags & WQ_FREEZABLE))
367924b8a847STejun Heo 			continue;
368024b8a847STejun Heo 
368124b8a847STejun Heo 		for_each_pwq(pwq, wq) {
368224b8a847STejun Heo 			spin_lock(&pwq->pool->lock);
368324b8a847STejun Heo 			pwq_set_max_active(pwq, wq->saved_max_active);
368424b8a847STejun Heo 			spin_unlock(&pwq->pool->lock);
368524b8a847STejun Heo 		}
368624b8a847STejun Heo 	}
368724b8a847STejun Heo 
368824b8a847STejun Heo 	/* kick workers */
368924b8a847STejun Heo 	for_each_pool(pool, id) {
369024b8a847STejun Heo 		spin_lock(&pool->lock);
369124b8a847STejun Heo 		wake_up_worker(pool);
369224b8a847STejun Heo 		spin_unlock(&pool->lock);
3693a0a1a5fdSTejun Heo 	}
3694a0a1a5fdSTejun Heo 
3695a0a1a5fdSTejun Heo 	workqueue_freezing = false;
3696a0a1a5fdSTejun Heo out_unlock:
3697e98d5b16STejun Heo 	spin_unlock_irq(&workqueue_lock);
3698a0a1a5fdSTejun Heo }
3699a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */
3700a0a1a5fdSTejun Heo 
37016ee0578bSSuresh Siddha static int __init init_workqueues(void)
37021da177e4SLinus Torvalds {
3703d84ff051STejun Heo 	int cpu;
3704c34056a3STejun Heo 
37057c3eed5cSTejun Heo 	/* make sure we have enough bits for OFFQ pool ID */
37067c3eed5cSTejun Heo 	BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) <
37076be19588SLai Jiangshan 		     WORK_CPU_END * NR_STD_WORKER_POOLS);
3708b5490077STejun Heo 
3709e904e6c2STejun Heo 	WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
3710e904e6c2STejun Heo 
3711e904e6c2STejun Heo 	pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
3712e904e6c2STejun Heo 
371365758202STejun Heo 	cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
3714a5b4e57dSLai Jiangshan 	hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
37158b03ae3cSTejun Heo 
3716706026c2STejun Heo 	/* initialize CPU pools */
3717706026c2STejun Heo 	for_each_wq_cpu(cpu) {
37184ce62e9eSTejun Heo 		struct worker_pool *pool;
37198b03ae3cSTejun Heo 
372038db41d9STejun Heo 		for_each_std_worker_pool(pool, cpu) {
3721d565ed63STejun Heo 			spin_lock_init(&pool->lock);
3722ec22ca5eSTejun Heo 			pool->cpu = cpu;
372324647570STejun Heo 			pool->flags |= POOL_DISASSOCIATED;
37244ce62e9eSTejun Heo 			INIT_LIST_HEAD(&pool->worklist);
37254ce62e9eSTejun Heo 			INIT_LIST_HEAD(&pool->idle_list);
3726c9e7cf27STejun Heo 			hash_init(pool->busy_hash);
3727e22bee78STejun Heo 
37284ce62e9eSTejun Heo 			init_timer_deferrable(&pool->idle_timer);
37294ce62e9eSTejun Heo 			pool->idle_timer.function = idle_worker_timeout;
37304ce62e9eSTejun Heo 			pool->idle_timer.data = (unsigned long)pool;
3731e22bee78STejun Heo 
3732706026c2STejun Heo 			setup_timer(&pool->mayday_timer, pool_mayday_timeout,
37334ce62e9eSTejun Heo 				    (unsigned long)pool);
37344ce62e9eSTejun Heo 
3735b2eb83d1SLai Jiangshan 			mutex_init(&pool->assoc_mutex);
37364ce62e9eSTejun Heo 			ida_init(&pool->worker_ida);
37379daf9e67STejun Heo 
37389daf9e67STejun Heo 			/* alloc pool ID */
37399daf9e67STejun Heo 			BUG_ON(worker_pool_assign_id(pool));
37404ce62e9eSTejun Heo 		}
37418b03ae3cSTejun Heo 	}
37428b03ae3cSTejun Heo 
3743e22bee78STejun Heo 	/* create the initial worker */
3744706026c2STejun Heo 	for_each_online_wq_cpu(cpu) {
37454ce62e9eSTejun Heo 		struct worker_pool *pool;
3746e22bee78STejun Heo 
374738db41d9STejun Heo 		for_each_std_worker_pool(pool, cpu) {
37484ce62e9eSTejun Heo 			struct worker *worker;
37494ce62e9eSTejun Heo 
375024647570STejun Heo 			if (cpu != WORK_CPU_UNBOUND)
375124647570STejun Heo 				pool->flags &= ~POOL_DISASSOCIATED;
375224647570STejun Heo 
3753bc2ae0f5STejun Heo 			worker = create_worker(pool);
3754e22bee78STejun Heo 			BUG_ON(!worker);
3755d565ed63STejun Heo 			spin_lock_irq(&pool->lock);
3756e22bee78STejun Heo 			start_worker(worker);
3757d565ed63STejun Heo 			spin_unlock_irq(&pool->lock);
3758e22bee78STejun Heo 		}
37594ce62e9eSTejun Heo 	}
3760e22bee78STejun Heo 
3761d320c038STejun Heo 	system_wq = alloc_workqueue("events", 0, 0);
37621aabe902SJoonsoo Kim 	system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
3763d320c038STejun Heo 	system_long_wq = alloc_workqueue("events_long", 0, 0);
3764f3421797STejun Heo 	system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3765f3421797STejun Heo 					    WQ_UNBOUND_MAX_ACTIVE);
376624d51addSTejun Heo 	system_freezable_wq = alloc_workqueue("events_freezable",
376724d51addSTejun Heo 					      WQ_FREEZABLE, 0);
37681aabe902SJoonsoo Kim 	BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
3769ae930e0fSTejun Heo 	       !system_unbound_wq || !system_freezable_wq);
37706ee0578bSSuresh Siddha 	return 0;
37711da177e4SLinus Torvalds }
37726ee0578bSSuresh Siddha early_initcall(init_workqueues);
3773