xref: /openbmc/linux/kernel/workqueue.c (revision d4283e93)
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>
44e22bee78STejun Heo 
45e22bee78STejun Heo #include "workqueue_sched.h"
461da177e4SLinus Torvalds 
47c8e55f36STejun Heo enum {
48bc2ae0f5STejun Heo 	/*
49bc2ae0f5STejun Heo 	 * global_cwq flags
50bc2ae0f5STejun Heo 	 *
51bc2ae0f5STejun Heo 	 * A bound gcwq is either associated or disassociated with its CPU.
52bc2ae0f5STejun Heo 	 * While associated (!DISASSOCIATED), all workers are bound to the
53bc2ae0f5STejun Heo 	 * CPU and none has %WORKER_UNBOUND set and concurrency management
54bc2ae0f5STejun Heo 	 * is in effect.
55bc2ae0f5STejun Heo 	 *
56bc2ae0f5STejun Heo 	 * While DISASSOCIATED, the cpu may be offline and all workers have
57bc2ae0f5STejun Heo 	 * %WORKER_UNBOUND set and concurrency management disabled, and may
58bc2ae0f5STejun Heo 	 * be executing on any CPU.  The gcwq behaves as an unbound one.
59bc2ae0f5STejun Heo 	 *
60bc2ae0f5STejun Heo 	 * Note that DISASSOCIATED can be flipped only while holding
61bc2ae0f5STejun Heo 	 * managership of all pools on the gcwq to avoid changing binding
62bc2ae0f5STejun Heo 	 * state while create_worker() is in progress.
63bc2ae0f5STejun Heo 	 */
6411ebea50STejun Heo 	GCWQ_DISASSOCIATED	= 1 << 0,	/* cpu can't serve workers */
6511ebea50STejun Heo 	GCWQ_FREEZING		= 1 << 1,	/* freeze in progress */
6611ebea50STejun Heo 
6711ebea50STejun Heo 	/* pool flags */
6811ebea50STejun Heo 	POOL_MANAGE_WORKERS	= 1 << 0,	/* need to manage workers */
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 */
75e22bee78STejun Heo 	WORKER_REBIND		= 1 << 5,	/* mom is home, come back */
76fb0e7bebSTejun Heo 	WORKER_CPU_INTENSIVE	= 1 << 6,	/* cpu intensive */
77f3421797STejun Heo 	WORKER_UNBOUND		= 1 << 7,	/* worker is unbound */
78e22bee78STejun Heo 
79403c821dSTejun Heo 	WORKER_NOT_RUNNING	= WORKER_PREP | WORKER_REBIND | WORKER_UNBOUND |
80403c821dSTejun Heo 				  WORKER_CPU_INTENSIVE,
81db7bccf4STejun Heo 
823270476aSTejun Heo 	NR_WORKER_POOLS		= 2,		/* # worker pools per gcwq */
834ce62e9eSTejun Heo 
84c8e55f36STejun Heo 	BUSY_WORKER_HASH_ORDER	= 6,		/* 64 pointers */
85c8e55f36STejun Heo 	BUSY_WORKER_HASH_SIZE	= 1 << BUSY_WORKER_HASH_ORDER,
86c8e55f36STejun Heo 	BUSY_WORKER_HASH_MASK	= BUSY_WORKER_HASH_SIZE - 1,
87db7bccf4STejun Heo 
88e22bee78STejun Heo 	MAX_IDLE_WORKERS_RATIO	= 4,		/* 1/4 of busy can be idle */
89e22bee78STejun Heo 	IDLE_WORKER_TIMEOUT	= 300 * HZ,	/* keep idle ones for 5 mins */
90e22bee78STejun Heo 
913233cdbdSTejun Heo 	MAYDAY_INITIAL_TIMEOUT  = HZ / 100 >= 2 ? HZ / 100 : 2,
923233cdbdSTejun Heo 						/* call for help after 10ms
933233cdbdSTejun Heo 						   (min two ticks) */
94e22bee78STejun Heo 	MAYDAY_INTERVAL		= HZ / 10,	/* and then every 100ms */
95e22bee78STejun Heo 	CREATE_COOLDOWN		= HZ,		/* time to breath after fail */
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	/*
98e22bee78STejun Heo 	 * Rescue workers are used only on emergencies and shared by
99e22bee78STejun Heo 	 * all cpus.  Give -20.
100e22bee78STejun Heo 	 */
101e22bee78STejun Heo 	RESCUER_NICE_LEVEL	= -20,
1023270476aSTejun Heo 	HIGHPRI_NICE_LEVEL	= -20,
103c8e55f36STejun Heo };
104c8e55f36STejun Heo 
1051da177e4SLinus Torvalds /*
1064690c4abSTejun Heo  * Structure fields follow one of the following exclusion rules.
1074690c4abSTejun Heo  *
108e41e704bSTejun Heo  * I: Modifiable by initialization/destruction paths and read-only for
109e41e704bSTejun Heo  *    everyone else.
1104690c4abSTejun Heo  *
111e22bee78STejun Heo  * P: Preemption protected.  Disabling preemption is enough and should
112e22bee78STejun Heo  *    only be modified and accessed from the local cpu.
113e22bee78STejun Heo  *
1148b03ae3cSTejun Heo  * L: gcwq->lock protected.  Access with gcwq->lock held.
1154690c4abSTejun Heo  *
116e22bee78STejun Heo  * X: During normal operation, modification requires gcwq->lock and
117e22bee78STejun Heo  *    should be done only from local cpu.  Either disabling preemption
118e22bee78STejun Heo  *    on local cpu or grabbing gcwq->lock is enough for read access.
119f3421797STejun Heo  *    If GCWQ_DISASSOCIATED is set, it's identical to L.
120e22bee78STejun Heo  *
12173f53c4aSTejun Heo  * F: wq->flush_mutex protected.
12273f53c4aSTejun Heo  *
1234690c4abSTejun Heo  * W: workqueue_lock protected.
1244690c4abSTejun Heo  */
1254690c4abSTejun Heo 
1268b03ae3cSTejun Heo struct global_cwq;
127bd7bdd43STejun Heo struct worker_pool;
12825511a47STejun Heo struct idle_rebind;
129c34056a3STejun Heo 
130e22bee78STejun Heo /*
131e22bee78STejun Heo  * The poor guys doing the actual heavy lifting.  All on-duty workers
132e22bee78STejun Heo  * are either serving the manager role, on idle list or on busy hash.
133e22bee78STejun Heo  */
134c34056a3STejun Heo struct worker {
135c8e55f36STejun Heo 	/* on idle list while idle, on busy hash table while busy */
136c8e55f36STejun Heo 	union {
137c8e55f36STejun Heo 		struct list_head	entry;	/* L: while idle */
138c8e55f36STejun Heo 		struct hlist_node	hentry;	/* L: while busy */
139c8e55f36STejun Heo 	};
140c8e55f36STejun Heo 
141c34056a3STejun Heo 	struct work_struct	*current_work;	/* L: work being processed */
1428cca0eeaSTejun Heo 	struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */
143affee4b2STejun Heo 	struct list_head	scheduled;	/* L: scheduled works */
144c34056a3STejun Heo 	struct task_struct	*task;		/* I: worker task */
145bd7bdd43STejun Heo 	struct worker_pool	*pool;		/* I: the associated pool */
146e22bee78STejun Heo 	/* 64 bytes boundary on 64bit, 32 on 32bit */
147e22bee78STejun Heo 	unsigned long		last_active;	/* L: last active timestamp */
148e22bee78STejun Heo 	unsigned int		flags;		/* X: flags */
149c34056a3STejun Heo 	int			id;		/* I: worker id */
15025511a47STejun Heo 
15125511a47STejun Heo 	/* for rebinding worker to CPU */
15225511a47STejun Heo 	struct idle_rebind	*idle_rebind;	/* L: for idle worker */
15325511a47STejun Heo 	struct work_struct	rebind_work;	/* L: for busy worker */
154c34056a3STejun Heo };
155c34056a3STejun Heo 
156bd7bdd43STejun Heo struct worker_pool {
157bd7bdd43STejun Heo 	struct global_cwq	*gcwq;		/* I: the owning gcwq */
15811ebea50STejun Heo 	unsigned int		flags;		/* X: flags */
159bd7bdd43STejun Heo 
160bd7bdd43STejun Heo 	struct list_head	worklist;	/* L: list of pending works */
161bd7bdd43STejun Heo 	int			nr_workers;	/* L: total number of workers */
162bd7bdd43STejun Heo 	int			nr_idle;	/* L: currently idle ones */
163bd7bdd43STejun Heo 
164bd7bdd43STejun Heo 	struct list_head	idle_list;	/* X: list of idle workers */
165bd7bdd43STejun Heo 	struct timer_list	idle_timer;	/* L: worker idle timeout */
166bd7bdd43STejun Heo 	struct timer_list	mayday_timer;	/* L: SOS timer for workers */
167bd7bdd43STejun Heo 
16860373152STejun Heo 	struct mutex		manager_mutex;	/* mutex manager should hold */
169bd7bdd43STejun Heo 	struct ida		worker_ida;	/* L: for worker IDs */
170bd7bdd43STejun Heo };
171bd7bdd43STejun Heo 
1724690c4abSTejun Heo /*
173e22bee78STejun Heo  * Global per-cpu workqueue.  There's one and only one for each cpu
174e22bee78STejun Heo  * and all works are queued and processed here regardless of their
175e22bee78STejun Heo  * target workqueues.
1768b03ae3cSTejun Heo  */
1778b03ae3cSTejun Heo struct global_cwq {
1788b03ae3cSTejun Heo 	spinlock_t		lock;		/* the gcwq lock */
1798b03ae3cSTejun Heo 	unsigned int		cpu;		/* I: the associated cpu */
180db7bccf4STejun Heo 	unsigned int		flags;		/* L: GCWQ_* flags */
181c8e55f36STejun Heo 
182bd7bdd43STejun Heo 	/* workers are chained either in busy_hash or pool idle_list */
183c8e55f36STejun Heo 	struct hlist_head	busy_hash[BUSY_WORKER_HASH_SIZE];
184c8e55f36STejun Heo 						/* L: hash of busy workers */
185c8e55f36STejun Heo 
1863270476aSTejun Heo 	struct worker_pool	pools[2];	/* normal and highpri pools */
187db7bccf4STejun Heo 
18825511a47STejun Heo 	wait_queue_head_t	rebind_hold;	/* rebind hold wait */
1898b03ae3cSTejun Heo } ____cacheline_aligned_in_smp;
1908b03ae3cSTejun Heo 
1918b03ae3cSTejun Heo /*
192502ca9d8STejun Heo  * The per-CPU workqueue.  The lower WORK_STRUCT_FLAG_BITS of
1930f900049STejun Heo  * work_struct->data are used for flags and thus cwqs need to be
1940f900049STejun Heo  * aligned at two's power of the number of flag bits.
1951da177e4SLinus Torvalds  */
1961da177e4SLinus Torvalds struct cpu_workqueue_struct {
197bd7bdd43STejun Heo 	struct worker_pool	*pool;		/* I: the associated pool */
1984690c4abSTejun Heo 	struct workqueue_struct *wq;		/* I: the owning workqueue */
19973f53c4aSTejun Heo 	int			work_color;	/* L: current color */
20073f53c4aSTejun Heo 	int			flush_color;	/* L: flushing color */
20173f53c4aSTejun Heo 	int			nr_in_flight[WORK_NR_COLORS];
20273f53c4aSTejun Heo 						/* L: nr of in_flight works */
2031e19ffc6STejun Heo 	int			nr_active;	/* L: nr of active works */
204a0a1a5fdSTejun Heo 	int			max_active;	/* L: max active works */
2051e19ffc6STejun Heo 	struct list_head	delayed_works;	/* L: delayed works */
2060f900049STejun Heo };
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds /*
20973f53c4aSTejun Heo  * Structure used to wait for workqueue flush.
21073f53c4aSTejun Heo  */
21173f53c4aSTejun Heo struct wq_flusher {
21273f53c4aSTejun Heo 	struct list_head	list;		/* F: list of flushers */
21373f53c4aSTejun Heo 	int			flush_color;	/* F: flush color waiting for */
21473f53c4aSTejun Heo 	struct completion	done;		/* flush completion */
21573f53c4aSTejun Heo };
2161da177e4SLinus Torvalds 
21773f53c4aSTejun Heo /*
218f2e005aaSTejun Heo  * All cpumasks are assumed to be always set on UP and thus can't be
219f2e005aaSTejun Heo  * used to determine whether there's something to be done.
220f2e005aaSTejun Heo  */
221f2e005aaSTejun Heo #ifdef CONFIG_SMP
222f2e005aaSTejun Heo typedef cpumask_var_t mayday_mask_t;
223f2e005aaSTejun Heo #define mayday_test_and_set_cpu(cpu, mask)	\
224f2e005aaSTejun Heo 	cpumask_test_and_set_cpu((cpu), (mask))
225f2e005aaSTejun Heo #define mayday_clear_cpu(cpu, mask)		cpumask_clear_cpu((cpu), (mask))
226f2e005aaSTejun Heo #define for_each_mayday_cpu(cpu, mask)		for_each_cpu((cpu), (mask))
2279c37547aSTejun Heo #define alloc_mayday_mask(maskp, gfp)		zalloc_cpumask_var((maskp), (gfp))
228f2e005aaSTejun Heo #define free_mayday_mask(mask)			free_cpumask_var((mask))
229f2e005aaSTejun Heo #else
230f2e005aaSTejun Heo typedef unsigned long mayday_mask_t;
231f2e005aaSTejun Heo #define mayday_test_and_set_cpu(cpu, mask)	test_and_set_bit(0, &(mask))
232f2e005aaSTejun Heo #define mayday_clear_cpu(cpu, mask)		clear_bit(0, &(mask))
233f2e005aaSTejun Heo #define for_each_mayday_cpu(cpu, mask)		if ((cpu) = 0, (mask))
234f2e005aaSTejun Heo #define alloc_mayday_mask(maskp, gfp)		true
235f2e005aaSTejun Heo #define free_mayday_mask(mask)			do { } while (0)
236f2e005aaSTejun Heo #endif
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds /*
2391da177e4SLinus Torvalds  * The externally visible workqueue abstraction is an array of
2401da177e4SLinus Torvalds  * per-CPU workqueues:
2411da177e4SLinus Torvalds  */
2421da177e4SLinus Torvalds struct workqueue_struct {
2439c5a2ba7STejun Heo 	unsigned int		flags;		/* W: WQ_* flags */
244bdbc5dd7STejun Heo 	union {
245bdbc5dd7STejun Heo 		struct cpu_workqueue_struct __percpu	*pcpu;
246bdbc5dd7STejun Heo 		struct cpu_workqueue_struct		*single;
247bdbc5dd7STejun Heo 		unsigned long				v;
248bdbc5dd7STejun Heo 	} cpu_wq;				/* I: cwq's */
2494690c4abSTejun Heo 	struct list_head	list;		/* W: list of all workqueues */
25073f53c4aSTejun Heo 
25173f53c4aSTejun Heo 	struct mutex		flush_mutex;	/* protects wq flushing */
25273f53c4aSTejun Heo 	int			work_color;	/* F: current work color */
25373f53c4aSTejun Heo 	int			flush_color;	/* F: current flush color */
25473f53c4aSTejun Heo 	atomic_t		nr_cwqs_to_flush; /* flush in progress */
25573f53c4aSTejun Heo 	struct wq_flusher	*first_flusher;	/* F: first flusher */
25673f53c4aSTejun Heo 	struct list_head	flusher_queue;	/* F: flush waiters */
25773f53c4aSTejun Heo 	struct list_head	flusher_overflow; /* F: flush overflow list */
25873f53c4aSTejun Heo 
259f2e005aaSTejun Heo 	mayday_mask_t		mayday_mask;	/* cpus requesting rescue */
260e22bee78STejun Heo 	struct worker		*rescuer;	/* I: rescue worker */
261e22bee78STejun Heo 
2629c5a2ba7STejun Heo 	int			nr_drainers;	/* W: drain in progress */
263dcd989cbSTejun Heo 	int			saved_max_active; /* W: saved cwq max_active */
2644e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
2654e6045f1SJohannes Berg 	struct lockdep_map	lockdep_map;
2664e6045f1SJohannes Berg #endif
267b196be89STejun Heo 	char			name[];		/* I: workqueue name */
2681da177e4SLinus Torvalds };
2691da177e4SLinus Torvalds 
270d320c038STejun Heo struct workqueue_struct *system_wq __read_mostly;
271d320c038STejun Heo struct workqueue_struct *system_long_wq __read_mostly;
272d320c038STejun Heo struct workqueue_struct *system_nrt_wq __read_mostly;
273f3421797STejun Heo struct workqueue_struct *system_unbound_wq __read_mostly;
27424d51addSTejun Heo struct workqueue_struct *system_freezable_wq __read_mostly;
27562d3c543SAlan Stern struct workqueue_struct *system_nrt_freezable_wq __read_mostly;
276d320c038STejun Heo EXPORT_SYMBOL_GPL(system_wq);
277d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq);
278d320c038STejun Heo EXPORT_SYMBOL_GPL(system_nrt_wq);
279f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq);
28024d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq);
28162d3c543SAlan Stern EXPORT_SYMBOL_GPL(system_nrt_freezable_wq);
282d320c038STejun Heo 
28397bd2347STejun Heo #define CREATE_TRACE_POINTS
28497bd2347STejun Heo #include <trace/events/workqueue.h>
28597bd2347STejun Heo 
2864ce62e9eSTejun Heo #define for_each_worker_pool(pool, gcwq)				\
2873270476aSTejun Heo 	for ((pool) = &(gcwq)->pools[0];				\
2883270476aSTejun Heo 	     (pool) < &(gcwq)->pools[NR_WORKER_POOLS]; (pool)++)
2894ce62e9eSTejun Heo 
290db7bccf4STejun Heo #define for_each_busy_worker(worker, i, pos, gcwq)			\
291db7bccf4STejun Heo 	for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)			\
292db7bccf4STejun Heo 		hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
293db7bccf4STejun Heo 
294f3421797STejun Heo static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
295f3421797STejun Heo 				  unsigned int sw)
296f3421797STejun Heo {
297f3421797STejun Heo 	if (cpu < nr_cpu_ids) {
298f3421797STejun Heo 		if (sw & 1) {
299f3421797STejun Heo 			cpu = cpumask_next(cpu, mask);
300f3421797STejun Heo 			if (cpu < nr_cpu_ids)
301f3421797STejun Heo 				return cpu;
302f3421797STejun Heo 		}
303f3421797STejun Heo 		if (sw & 2)
304f3421797STejun Heo 			return WORK_CPU_UNBOUND;
305f3421797STejun Heo 	}
306f3421797STejun Heo 	return WORK_CPU_NONE;
307f3421797STejun Heo }
308f3421797STejun Heo 
309f3421797STejun Heo static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
310f3421797STejun Heo 				struct workqueue_struct *wq)
311f3421797STejun Heo {
312f3421797STejun Heo 	return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
313f3421797STejun Heo }
314f3421797STejun Heo 
31509884951STejun Heo /*
31609884951STejun Heo  * CPU iterators
31709884951STejun Heo  *
31809884951STejun Heo  * An extra gcwq is defined for an invalid cpu number
31909884951STejun Heo  * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
32009884951STejun Heo  * specific CPU.  The following iterators are similar to
32109884951STejun Heo  * for_each_*_cpu() iterators but also considers the unbound gcwq.
32209884951STejun Heo  *
32309884951STejun Heo  * for_each_gcwq_cpu()		: possible CPUs + WORK_CPU_UNBOUND
32409884951STejun Heo  * for_each_online_gcwq_cpu()	: online CPUs + WORK_CPU_UNBOUND
32509884951STejun Heo  * for_each_cwq_cpu()		: possible CPUs for bound workqueues,
32609884951STejun Heo  *				  WORK_CPU_UNBOUND for unbound workqueues
32709884951STejun Heo  */
328f3421797STejun Heo #define for_each_gcwq_cpu(cpu)						\
329f3421797STejun Heo 	for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3);		\
330f3421797STejun Heo 	     (cpu) < WORK_CPU_NONE;					\
331f3421797STejun Heo 	     (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
332f3421797STejun Heo 
333f3421797STejun Heo #define for_each_online_gcwq_cpu(cpu)					\
334f3421797STejun Heo 	for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3);		\
335f3421797STejun Heo 	     (cpu) < WORK_CPU_NONE;					\
336f3421797STejun Heo 	     (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
337f3421797STejun Heo 
338f3421797STejun Heo #define for_each_cwq_cpu(cpu, wq)					\
339f3421797STejun Heo 	for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq));	\
340f3421797STejun Heo 	     (cpu) < WORK_CPU_NONE;					\
341f3421797STejun Heo 	     (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
342f3421797STejun Heo 
343dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK
344dc186ad7SThomas Gleixner 
345dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr;
346dc186ad7SThomas Gleixner 
34799777288SStanislaw Gruszka static void *work_debug_hint(void *addr)
34899777288SStanislaw Gruszka {
34999777288SStanislaw Gruszka 	return ((struct work_struct *) addr)->func;
35099777288SStanislaw Gruszka }
35199777288SStanislaw Gruszka 
352dc186ad7SThomas Gleixner /*
353dc186ad7SThomas Gleixner  * fixup_init is called when:
354dc186ad7SThomas Gleixner  * - an active object is initialized
355dc186ad7SThomas Gleixner  */
356dc186ad7SThomas Gleixner static int work_fixup_init(void *addr, enum debug_obj_state state)
357dc186ad7SThomas Gleixner {
358dc186ad7SThomas Gleixner 	struct work_struct *work = addr;
359dc186ad7SThomas Gleixner 
360dc186ad7SThomas Gleixner 	switch (state) {
361dc186ad7SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
362dc186ad7SThomas Gleixner 		cancel_work_sync(work);
363dc186ad7SThomas Gleixner 		debug_object_init(work, &work_debug_descr);
364dc186ad7SThomas Gleixner 		return 1;
365dc186ad7SThomas Gleixner 	default:
366dc186ad7SThomas Gleixner 		return 0;
367dc186ad7SThomas Gleixner 	}
368dc186ad7SThomas Gleixner }
369dc186ad7SThomas Gleixner 
370dc186ad7SThomas Gleixner /*
371dc186ad7SThomas Gleixner  * fixup_activate is called when:
372dc186ad7SThomas Gleixner  * - an active object is activated
373dc186ad7SThomas Gleixner  * - an unknown object is activated (might be a statically initialized object)
374dc186ad7SThomas Gleixner  */
375dc186ad7SThomas Gleixner static int work_fixup_activate(void *addr, enum debug_obj_state state)
376dc186ad7SThomas Gleixner {
377dc186ad7SThomas Gleixner 	struct work_struct *work = addr;
378dc186ad7SThomas Gleixner 
379dc186ad7SThomas Gleixner 	switch (state) {
380dc186ad7SThomas Gleixner 
381dc186ad7SThomas Gleixner 	case ODEBUG_STATE_NOTAVAILABLE:
382dc186ad7SThomas Gleixner 		/*
383dc186ad7SThomas Gleixner 		 * This is not really a fixup. The work struct was
384dc186ad7SThomas Gleixner 		 * statically initialized. We just make sure that it
385dc186ad7SThomas Gleixner 		 * is tracked in the object tracker.
386dc186ad7SThomas Gleixner 		 */
38722df02bbSTejun Heo 		if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
388dc186ad7SThomas Gleixner 			debug_object_init(work, &work_debug_descr);
389dc186ad7SThomas Gleixner 			debug_object_activate(work, &work_debug_descr);
390dc186ad7SThomas Gleixner 			return 0;
391dc186ad7SThomas Gleixner 		}
392dc186ad7SThomas Gleixner 		WARN_ON_ONCE(1);
393dc186ad7SThomas Gleixner 		return 0;
394dc186ad7SThomas Gleixner 
395dc186ad7SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
396dc186ad7SThomas Gleixner 		WARN_ON(1);
397dc186ad7SThomas Gleixner 
398dc186ad7SThomas Gleixner 	default:
399dc186ad7SThomas Gleixner 		return 0;
400dc186ad7SThomas Gleixner 	}
401dc186ad7SThomas Gleixner }
402dc186ad7SThomas Gleixner 
403dc186ad7SThomas Gleixner /*
404dc186ad7SThomas Gleixner  * fixup_free is called when:
405dc186ad7SThomas Gleixner  * - an active object is freed
406dc186ad7SThomas Gleixner  */
407dc186ad7SThomas Gleixner static int work_fixup_free(void *addr, enum debug_obj_state state)
408dc186ad7SThomas Gleixner {
409dc186ad7SThomas Gleixner 	struct work_struct *work = addr;
410dc186ad7SThomas Gleixner 
411dc186ad7SThomas Gleixner 	switch (state) {
412dc186ad7SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
413dc186ad7SThomas Gleixner 		cancel_work_sync(work);
414dc186ad7SThomas Gleixner 		debug_object_free(work, &work_debug_descr);
415dc186ad7SThomas Gleixner 		return 1;
416dc186ad7SThomas Gleixner 	default:
417dc186ad7SThomas Gleixner 		return 0;
418dc186ad7SThomas Gleixner 	}
419dc186ad7SThomas Gleixner }
420dc186ad7SThomas Gleixner 
421dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr = {
422dc186ad7SThomas Gleixner 	.name		= "work_struct",
42399777288SStanislaw Gruszka 	.debug_hint	= work_debug_hint,
424dc186ad7SThomas Gleixner 	.fixup_init	= work_fixup_init,
425dc186ad7SThomas Gleixner 	.fixup_activate	= work_fixup_activate,
426dc186ad7SThomas Gleixner 	.fixup_free	= work_fixup_free,
427dc186ad7SThomas Gleixner };
428dc186ad7SThomas Gleixner 
429dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work)
430dc186ad7SThomas Gleixner {
431dc186ad7SThomas Gleixner 	debug_object_activate(work, &work_debug_descr);
432dc186ad7SThomas Gleixner }
433dc186ad7SThomas Gleixner 
434dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work)
435dc186ad7SThomas Gleixner {
436dc186ad7SThomas Gleixner 	debug_object_deactivate(work, &work_debug_descr);
437dc186ad7SThomas Gleixner }
438dc186ad7SThomas Gleixner 
439dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack)
440dc186ad7SThomas Gleixner {
441dc186ad7SThomas Gleixner 	if (onstack)
442dc186ad7SThomas Gleixner 		debug_object_init_on_stack(work, &work_debug_descr);
443dc186ad7SThomas Gleixner 	else
444dc186ad7SThomas Gleixner 		debug_object_init(work, &work_debug_descr);
445dc186ad7SThomas Gleixner }
446dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work);
447dc186ad7SThomas Gleixner 
448dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work)
449dc186ad7SThomas Gleixner {
450dc186ad7SThomas Gleixner 	debug_object_free(work, &work_debug_descr);
451dc186ad7SThomas Gleixner }
452dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack);
453dc186ad7SThomas Gleixner 
454dc186ad7SThomas Gleixner #else
455dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { }
456dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { }
457dc186ad7SThomas Gleixner #endif
458dc186ad7SThomas Gleixner 
45995402b38SGautham R Shenoy /* Serializes the accesses to the list of workqueues. */
46095402b38SGautham R Shenoy static DEFINE_SPINLOCK(workqueue_lock);
4611da177e4SLinus Torvalds static LIST_HEAD(workqueues);
462a0a1a5fdSTejun Heo static bool workqueue_freezing;		/* W: have wqs started freezing? */
4631da177e4SLinus Torvalds 
46414441960SOleg Nesterov /*
465e22bee78STejun Heo  * The almighty global cpu workqueues.  nr_running is the only field
466e22bee78STejun Heo  * which is expected to be used frequently by other cpus via
467e22bee78STejun Heo  * try_to_wake_up().  Put it in a separate cacheline.
46814441960SOleg Nesterov  */
4698b03ae3cSTejun Heo static DEFINE_PER_CPU(struct global_cwq, global_cwq);
4704ce62e9eSTejun Heo static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, pool_nr_running[NR_WORKER_POOLS]);
471f756d5e2SNathan Lynch 
472f3421797STejun Heo /*
473f3421797STejun Heo  * Global cpu workqueue and nr_running counter for unbound gcwq.  The
474f3421797STejun Heo  * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its
475f3421797STejun Heo  * workers have WORKER_UNBOUND set.
476f3421797STejun Heo  */
477f3421797STejun Heo static struct global_cwq unbound_global_cwq;
4784ce62e9eSTejun Heo static atomic_t unbound_pool_nr_running[NR_WORKER_POOLS] = {
4794ce62e9eSTejun Heo 	[0 ... NR_WORKER_POOLS - 1]	= ATOMIC_INIT(0),	/* always 0 */
4804ce62e9eSTejun Heo };
481f3421797STejun Heo 
482c34056a3STejun Heo static int worker_thread(void *__worker);
4831da177e4SLinus Torvalds 
4843270476aSTejun Heo static int worker_pool_pri(struct worker_pool *pool)
4853270476aSTejun Heo {
4863270476aSTejun Heo 	return pool - pool->gcwq->pools;
4873270476aSTejun Heo }
4883270476aSTejun Heo 
4898b03ae3cSTejun Heo static struct global_cwq *get_gcwq(unsigned int cpu)
4901da177e4SLinus Torvalds {
491f3421797STejun Heo 	if (cpu != WORK_CPU_UNBOUND)
4928b03ae3cSTejun Heo 		return &per_cpu(global_cwq, cpu);
493f3421797STejun Heo 	else
494f3421797STejun Heo 		return &unbound_global_cwq;
4951da177e4SLinus Torvalds }
4961da177e4SLinus Torvalds 
49763d95a91STejun Heo static atomic_t *get_pool_nr_running(struct worker_pool *pool)
498b1f4ec17SOleg Nesterov {
49963d95a91STejun Heo 	int cpu = pool->gcwq->cpu;
5003270476aSTejun Heo 	int idx = worker_pool_pri(pool);
50163d95a91STejun Heo 
502f3421797STejun Heo 	if (cpu != WORK_CPU_UNBOUND)
5034ce62e9eSTejun Heo 		return &per_cpu(pool_nr_running, cpu)[idx];
504f3421797STejun Heo 	else
5054ce62e9eSTejun Heo 		return &unbound_pool_nr_running[idx];
506b1f4ec17SOleg Nesterov }
507b1f4ec17SOleg Nesterov 
5084690c4abSTejun Heo static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
5094690c4abSTejun Heo 					    struct workqueue_struct *wq)
510a848e3b6SOleg Nesterov {
511f3421797STejun Heo 	if (!(wq->flags & WQ_UNBOUND)) {
512e06ffa1eSLai Jiangshan 		if (likely(cpu < nr_cpu_ids))
513bdbc5dd7STejun Heo 			return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
514f3421797STejun Heo 	} else if (likely(cpu == WORK_CPU_UNBOUND))
515f3421797STejun Heo 		return wq->cpu_wq.single;
516f3421797STejun Heo 	return NULL;
517f3421797STejun Heo }
518a848e3b6SOleg Nesterov 
51973f53c4aSTejun Heo static unsigned int work_color_to_flags(int color)
52073f53c4aSTejun Heo {
52173f53c4aSTejun Heo 	return color << WORK_STRUCT_COLOR_SHIFT;
52273f53c4aSTejun Heo }
52373f53c4aSTejun Heo 
52473f53c4aSTejun Heo static int get_work_color(struct work_struct *work)
52573f53c4aSTejun Heo {
52673f53c4aSTejun Heo 	return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
52773f53c4aSTejun Heo 		((1 << WORK_STRUCT_COLOR_BITS) - 1);
52873f53c4aSTejun Heo }
52973f53c4aSTejun Heo 
53073f53c4aSTejun Heo static int work_next_color(int color)
53173f53c4aSTejun Heo {
53273f53c4aSTejun Heo 	return (color + 1) % WORK_NR_COLORS;
5331da177e4SLinus Torvalds }
5341da177e4SLinus Torvalds 
5354594bf15SDavid Howells /*
536e120153dSTejun Heo  * A work's data points to the cwq with WORK_STRUCT_CWQ set while the
537e120153dSTejun Heo  * work is on queue.  Once execution starts, WORK_STRUCT_CWQ is
538e120153dSTejun Heo  * cleared and the work data contains the cpu number it was last on.
5397a22ad75STejun Heo  *
5407a22ad75STejun Heo  * set_work_{cwq|cpu}() and clear_work_data() can be used to set the
5417a22ad75STejun Heo  * cwq, cpu or clear work->data.  These functions should only be
5427a22ad75STejun Heo  * called while the work is owned - ie. while the PENDING bit is set.
5437a22ad75STejun Heo  *
5447a22ad75STejun Heo  * get_work_[g]cwq() can be used to obtain the gcwq or cwq
5457a22ad75STejun Heo  * corresponding to a work.  gcwq is available once the work has been
5467a22ad75STejun Heo  * queued anywhere after initialization.  cwq is available only from
5477a22ad75STejun Heo  * queueing until execution starts.
5484594bf15SDavid Howells  */
5497a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data,
5507a22ad75STejun Heo 				 unsigned long flags)
5517a22ad75STejun Heo {
5527a22ad75STejun Heo 	BUG_ON(!work_pending(work));
5537a22ad75STejun Heo 	atomic_long_set(&work->data, data | flags | work_static(work));
5547a22ad75STejun Heo }
5557a22ad75STejun Heo 
5567a22ad75STejun Heo static void set_work_cwq(struct work_struct *work,
5574690c4abSTejun Heo 			 struct cpu_workqueue_struct *cwq,
5584690c4abSTejun Heo 			 unsigned long extra_flags)
559365970a1SDavid Howells {
5607a22ad75STejun Heo 	set_work_data(work, (unsigned long)cwq,
561e120153dSTejun Heo 		      WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
562365970a1SDavid Howells }
563365970a1SDavid Howells 
5647a22ad75STejun Heo static void set_work_cpu(struct work_struct *work, unsigned int cpu)
5654d707b9fSOleg Nesterov {
5667a22ad75STejun Heo 	set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING);
5674d707b9fSOleg Nesterov }
5684d707b9fSOleg Nesterov 
5697a22ad75STejun Heo static void clear_work_data(struct work_struct *work)
570365970a1SDavid Howells {
5717a22ad75STejun Heo 	set_work_data(work, WORK_STRUCT_NO_CPU, 0);
5727a22ad75STejun Heo }
5737a22ad75STejun Heo 
5747a22ad75STejun Heo static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
5757a22ad75STejun Heo {
576e120153dSTejun Heo 	unsigned long data = atomic_long_read(&work->data);
5777a22ad75STejun Heo 
578e120153dSTejun Heo 	if (data & WORK_STRUCT_CWQ)
579e120153dSTejun Heo 		return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
580e120153dSTejun Heo 	else
581e120153dSTejun Heo 		return NULL;
5827a22ad75STejun Heo }
5837a22ad75STejun Heo 
5847a22ad75STejun Heo static struct global_cwq *get_work_gcwq(struct work_struct *work)
5857a22ad75STejun Heo {
586e120153dSTejun Heo 	unsigned long data = atomic_long_read(&work->data);
5877a22ad75STejun Heo 	unsigned int cpu;
5887a22ad75STejun Heo 
589e120153dSTejun Heo 	if (data & WORK_STRUCT_CWQ)
590e120153dSTejun Heo 		return ((struct cpu_workqueue_struct *)
591bd7bdd43STejun Heo 			(data & WORK_STRUCT_WQ_DATA_MASK))->pool->gcwq;
5927a22ad75STejun Heo 
5937a22ad75STejun Heo 	cpu = data >> WORK_STRUCT_FLAG_BITS;
594bdbc5dd7STejun Heo 	if (cpu == WORK_CPU_NONE)
5957a22ad75STejun Heo 		return NULL;
5967a22ad75STejun Heo 
597f3421797STejun Heo 	BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
5987a22ad75STejun Heo 	return get_gcwq(cpu);
599365970a1SDavid Howells }
600365970a1SDavid Howells 
601e22bee78STejun Heo /*
6023270476aSTejun Heo  * Policy functions.  These define the policies on how the global worker
6033270476aSTejun Heo  * pools are managed.  Unless noted otherwise, these functions assume that
6043270476aSTejun Heo  * they're being called with gcwq->lock held.
605e22bee78STejun Heo  */
606e22bee78STejun Heo 
60763d95a91STejun Heo static bool __need_more_worker(struct worker_pool *pool)
608649027d7STejun Heo {
6093270476aSTejun Heo 	return !atomic_read(get_pool_nr_running(pool));
610649027d7STejun Heo }
611649027d7STejun Heo 
612e22bee78STejun Heo /*
613e22bee78STejun Heo  * Need to wake up a worker?  Called from anything but currently
614e22bee78STejun Heo  * running workers.
615974271c4STejun Heo  *
616974271c4STejun Heo  * Note that, because unbound workers never contribute to nr_running, this
617974271c4STejun Heo  * function will always return %true for unbound gcwq as long as the
618974271c4STejun Heo  * worklist isn't empty.
619e22bee78STejun Heo  */
62063d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool)
621e22bee78STejun Heo {
62263d95a91STejun Heo 	return !list_empty(&pool->worklist) && __need_more_worker(pool);
623e22bee78STejun Heo }
624e22bee78STejun Heo 
625e22bee78STejun Heo /* Can I start working?  Called from busy but !running workers. */
62663d95a91STejun Heo static bool may_start_working(struct worker_pool *pool)
627e22bee78STejun Heo {
62863d95a91STejun Heo 	return pool->nr_idle;
629e22bee78STejun Heo }
630e22bee78STejun Heo 
631e22bee78STejun Heo /* Do I need to keep working?  Called from currently running workers. */
63263d95a91STejun Heo static bool keep_working(struct worker_pool *pool)
633e22bee78STejun Heo {
63463d95a91STejun Heo 	atomic_t *nr_running = get_pool_nr_running(pool);
635e22bee78STejun Heo 
6363270476aSTejun Heo 	return !list_empty(&pool->worklist) && atomic_read(nr_running) <= 1;
637e22bee78STejun Heo }
638e22bee78STejun Heo 
639e22bee78STejun Heo /* Do we need a new worker?  Called from manager. */
64063d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool)
641e22bee78STejun Heo {
64263d95a91STejun Heo 	return need_more_worker(pool) && !may_start_working(pool);
643e22bee78STejun Heo }
644e22bee78STejun Heo 
645e22bee78STejun Heo /* Do I need to be the manager? */
64663d95a91STejun Heo static bool need_to_manage_workers(struct worker_pool *pool)
647e22bee78STejun Heo {
64863d95a91STejun Heo 	return need_to_create_worker(pool) ||
64911ebea50STejun Heo 		(pool->flags & POOL_MANAGE_WORKERS);
650e22bee78STejun Heo }
651e22bee78STejun Heo 
652e22bee78STejun Heo /* Do we have too many workers and should some go away? */
65363d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool)
654e22bee78STejun Heo {
65560373152STejun Heo 	bool managing = mutex_is_locked(&pool->manager_mutex);
65663d95a91STejun Heo 	int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
65763d95a91STejun Heo 	int nr_busy = pool->nr_workers - nr_idle;
658e22bee78STejun Heo 
659e22bee78STejun Heo 	return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
660e22bee78STejun Heo }
661e22bee78STejun Heo 
662e22bee78STejun Heo /*
663e22bee78STejun Heo  * Wake up functions.
664e22bee78STejun Heo  */
665e22bee78STejun Heo 
6667e11629dSTejun Heo /* Return the first worker.  Safe with preemption disabled */
66763d95a91STejun Heo static struct worker *first_worker(struct worker_pool *pool)
6687e11629dSTejun Heo {
66963d95a91STejun Heo 	if (unlikely(list_empty(&pool->idle_list)))
6707e11629dSTejun Heo 		return NULL;
6717e11629dSTejun Heo 
67263d95a91STejun Heo 	return list_first_entry(&pool->idle_list, struct worker, entry);
6737e11629dSTejun Heo }
6747e11629dSTejun Heo 
6757e11629dSTejun Heo /**
6767e11629dSTejun Heo  * wake_up_worker - wake up an idle worker
67763d95a91STejun Heo  * @pool: worker pool to wake worker from
6787e11629dSTejun Heo  *
67963d95a91STejun Heo  * Wake up the first idle worker of @pool.
6807e11629dSTejun Heo  *
6817e11629dSTejun Heo  * CONTEXT:
6827e11629dSTejun Heo  * spin_lock_irq(gcwq->lock).
6837e11629dSTejun Heo  */
68463d95a91STejun Heo static void wake_up_worker(struct worker_pool *pool)
6857e11629dSTejun Heo {
68663d95a91STejun Heo 	struct worker *worker = first_worker(pool);
6877e11629dSTejun Heo 
6887e11629dSTejun Heo 	if (likely(worker))
6897e11629dSTejun Heo 		wake_up_process(worker->task);
6907e11629dSTejun Heo }
6917e11629dSTejun Heo 
6924690c4abSTejun Heo /**
693e22bee78STejun Heo  * wq_worker_waking_up - a worker is waking up
694e22bee78STejun Heo  * @task: task waking up
695e22bee78STejun Heo  * @cpu: CPU @task is waking up to
696e22bee78STejun Heo  *
697e22bee78STejun Heo  * This function is called during try_to_wake_up() when a worker is
698e22bee78STejun Heo  * being awoken.
699e22bee78STejun Heo  *
700e22bee78STejun Heo  * CONTEXT:
701e22bee78STejun Heo  * spin_lock_irq(rq->lock)
702e22bee78STejun Heo  */
703e22bee78STejun Heo void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
704e22bee78STejun Heo {
705e22bee78STejun Heo 	struct worker *worker = kthread_data(task);
706e22bee78STejun Heo 
7072d64672eSSteven Rostedt 	if (!(worker->flags & WORKER_NOT_RUNNING))
70863d95a91STejun Heo 		atomic_inc(get_pool_nr_running(worker->pool));
709e22bee78STejun Heo }
710e22bee78STejun Heo 
711e22bee78STejun Heo /**
712e22bee78STejun Heo  * wq_worker_sleeping - a worker is going to sleep
713e22bee78STejun Heo  * @task: task going to sleep
714e22bee78STejun Heo  * @cpu: CPU in question, must be the current CPU number
715e22bee78STejun Heo  *
716e22bee78STejun Heo  * This function is called during schedule() when a busy worker is
717e22bee78STejun Heo  * going to sleep.  Worker on the same cpu can be woken up by
718e22bee78STejun Heo  * returning pointer to its task.
719e22bee78STejun Heo  *
720e22bee78STejun Heo  * CONTEXT:
721e22bee78STejun Heo  * spin_lock_irq(rq->lock)
722e22bee78STejun Heo  *
723e22bee78STejun Heo  * RETURNS:
724e22bee78STejun Heo  * Worker task on @cpu to wake up, %NULL if none.
725e22bee78STejun Heo  */
726e22bee78STejun Heo struct task_struct *wq_worker_sleeping(struct task_struct *task,
727e22bee78STejun Heo 				       unsigned int cpu)
728e22bee78STejun Heo {
729e22bee78STejun Heo 	struct worker *worker = kthread_data(task), *to_wakeup = NULL;
730bd7bdd43STejun Heo 	struct worker_pool *pool = worker->pool;
73163d95a91STejun Heo 	atomic_t *nr_running = get_pool_nr_running(pool);
732e22bee78STejun Heo 
7332d64672eSSteven Rostedt 	if (worker->flags & WORKER_NOT_RUNNING)
734e22bee78STejun Heo 		return NULL;
735e22bee78STejun Heo 
736e22bee78STejun Heo 	/* this can only happen on the local cpu */
737e22bee78STejun Heo 	BUG_ON(cpu != raw_smp_processor_id());
738e22bee78STejun Heo 
739e22bee78STejun Heo 	/*
740e22bee78STejun Heo 	 * The counterpart of the following dec_and_test, implied mb,
741e22bee78STejun Heo 	 * worklist not empty test sequence is in insert_work().
742e22bee78STejun Heo 	 * Please read comment there.
743e22bee78STejun Heo 	 *
744628c78e7STejun Heo 	 * NOT_RUNNING is clear.  This means that we're bound to and
745628c78e7STejun Heo 	 * running on the local cpu w/ rq lock held and preemption
746628c78e7STejun Heo 	 * disabled, which in turn means that none else could be
747628c78e7STejun Heo 	 * manipulating idle_list, so dereferencing idle_list without gcwq
748628c78e7STejun Heo 	 * lock is safe.
749e22bee78STejun Heo 	 */
750bd7bdd43STejun Heo 	if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist))
75163d95a91STejun Heo 		to_wakeup = first_worker(pool);
752e22bee78STejun Heo 	return to_wakeup ? to_wakeup->task : NULL;
753e22bee78STejun Heo }
754e22bee78STejun Heo 
755e22bee78STejun Heo /**
756e22bee78STejun Heo  * worker_set_flags - set worker flags and adjust nr_running accordingly
757cb444766STejun Heo  * @worker: self
758d302f017STejun Heo  * @flags: flags to set
759d302f017STejun Heo  * @wakeup: wakeup an idle worker if necessary
760d302f017STejun Heo  *
761e22bee78STejun Heo  * Set @flags in @worker->flags and adjust nr_running accordingly.  If
762e22bee78STejun Heo  * nr_running becomes zero and @wakeup is %true, an idle worker is
763e22bee78STejun Heo  * woken up.
764d302f017STejun Heo  *
765cb444766STejun Heo  * CONTEXT:
766cb444766STejun Heo  * spin_lock_irq(gcwq->lock)
767d302f017STejun Heo  */
768d302f017STejun Heo static inline void worker_set_flags(struct worker *worker, unsigned int flags,
769d302f017STejun Heo 				    bool wakeup)
770d302f017STejun Heo {
771bd7bdd43STejun Heo 	struct worker_pool *pool = worker->pool;
772e22bee78STejun Heo 
773cb444766STejun Heo 	WARN_ON_ONCE(worker->task != current);
774cb444766STejun Heo 
775e22bee78STejun Heo 	/*
776e22bee78STejun Heo 	 * If transitioning into NOT_RUNNING, adjust nr_running and
777e22bee78STejun Heo 	 * wake up an idle worker as necessary if requested by
778e22bee78STejun Heo 	 * @wakeup.
779e22bee78STejun Heo 	 */
780e22bee78STejun Heo 	if ((flags & WORKER_NOT_RUNNING) &&
781e22bee78STejun Heo 	    !(worker->flags & WORKER_NOT_RUNNING)) {
78263d95a91STejun Heo 		atomic_t *nr_running = get_pool_nr_running(pool);
783e22bee78STejun Heo 
784e22bee78STejun Heo 		if (wakeup) {
785e22bee78STejun Heo 			if (atomic_dec_and_test(nr_running) &&
786bd7bdd43STejun Heo 			    !list_empty(&pool->worklist))
78763d95a91STejun Heo 				wake_up_worker(pool);
788e22bee78STejun Heo 		} else
789e22bee78STejun Heo 			atomic_dec(nr_running);
790e22bee78STejun Heo 	}
791e22bee78STejun Heo 
792d302f017STejun Heo 	worker->flags |= flags;
793d302f017STejun Heo }
794d302f017STejun Heo 
795d302f017STejun Heo /**
796e22bee78STejun Heo  * worker_clr_flags - clear worker flags and adjust nr_running accordingly
797cb444766STejun Heo  * @worker: self
798d302f017STejun Heo  * @flags: flags to clear
799d302f017STejun Heo  *
800e22bee78STejun Heo  * Clear @flags in @worker->flags and adjust nr_running accordingly.
801d302f017STejun Heo  *
802cb444766STejun Heo  * CONTEXT:
803cb444766STejun Heo  * spin_lock_irq(gcwq->lock)
804d302f017STejun Heo  */
805d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
806d302f017STejun Heo {
80763d95a91STejun Heo 	struct worker_pool *pool = worker->pool;
808e22bee78STejun Heo 	unsigned int oflags = worker->flags;
809e22bee78STejun Heo 
810cb444766STejun Heo 	WARN_ON_ONCE(worker->task != current);
811cb444766STejun Heo 
812d302f017STejun Heo 	worker->flags &= ~flags;
813e22bee78STejun Heo 
81442c025f3STejun Heo 	/*
81542c025f3STejun Heo 	 * If transitioning out of NOT_RUNNING, increment nr_running.  Note
81642c025f3STejun Heo 	 * that the nested NOT_RUNNING is not a noop.  NOT_RUNNING is mask
81742c025f3STejun Heo 	 * of multiple flags, not a single flag.
81842c025f3STejun Heo 	 */
819e22bee78STejun Heo 	if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
820e22bee78STejun Heo 		if (!(worker->flags & WORKER_NOT_RUNNING))
82163d95a91STejun Heo 			atomic_inc(get_pool_nr_running(pool));
822d302f017STejun Heo }
823d302f017STejun Heo 
824d302f017STejun Heo /**
825c8e55f36STejun Heo  * busy_worker_head - return the busy hash head for a work
826c8e55f36STejun Heo  * @gcwq: gcwq of interest
827c8e55f36STejun Heo  * @work: work to be hashed
828c8e55f36STejun Heo  *
829c8e55f36STejun Heo  * Return hash head of @gcwq for @work.
830c8e55f36STejun Heo  *
831c8e55f36STejun Heo  * CONTEXT:
832c8e55f36STejun Heo  * spin_lock_irq(gcwq->lock).
833c8e55f36STejun Heo  *
834c8e55f36STejun Heo  * RETURNS:
835c8e55f36STejun Heo  * Pointer to the hash head.
836c8e55f36STejun Heo  */
837c8e55f36STejun Heo static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
838c8e55f36STejun Heo 					   struct work_struct *work)
839c8e55f36STejun Heo {
840c8e55f36STejun Heo 	const int base_shift = ilog2(sizeof(struct work_struct));
841c8e55f36STejun Heo 	unsigned long v = (unsigned long)work;
842c8e55f36STejun Heo 
843c8e55f36STejun Heo 	/* simple shift and fold hash, do we need something better? */
844c8e55f36STejun Heo 	v >>= base_shift;
845c8e55f36STejun Heo 	v += v >> BUSY_WORKER_HASH_ORDER;
846c8e55f36STejun Heo 	v &= BUSY_WORKER_HASH_MASK;
847c8e55f36STejun Heo 
848c8e55f36STejun Heo 	return &gcwq->busy_hash[v];
849c8e55f36STejun Heo }
850c8e55f36STejun Heo 
851c8e55f36STejun Heo /**
8528cca0eeaSTejun Heo  * __find_worker_executing_work - find worker which is executing a work
8538cca0eeaSTejun Heo  * @gcwq: gcwq of interest
8548cca0eeaSTejun Heo  * @bwh: hash head as returned by busy_worker_head()
8558cca0eeaSTejun Heo  * @work: work to find worker for
8568cca0eeaSTejun Heo  *
8578cca0eeaSTejun Heo  * Find a worker which is executing @work on @gcwq.  @bwh should be
8588cca0eeaSTejun Heo  * the hash head obtained by calling busy_worker_head() with the same
8598cca0eeaSTejun Heo  * work.
8608cca0eeaSTejun Heo  *
8618cca0eeaSTejun Heo  * CONTEXT:
8628cca0eeaSTejun Heo  * spin_lock_irq(gcwq->lock).
8638cca0eeaSTejun Heo  *
8648cca0eeaSTejun Heo  * RETURNS:
8658cca0eeaSTejun Heo  * Pointer to worker which is executing @work if found, NULL
8668cca0eeaSTejun Heo  * otherwise.
8678cca0eeaSTejun Heo  */
8688cca0eeaSTejun Heo static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
8698cca0eeaSTejun Heo 						   struct hlist_head *bwh,
8708cca0eeaSTejun Heo 						   struct work_struct *work)
8718cca0eeaSTejun Heo {
8728cca0eeaSTejun Heo 	struct worker *worker;
8738cca0eeaSTejun Heo 	struct hlist_node *tmp;
8748cca0eeaSTejun Heo 
8758cca0eeaSTejun Heo 	hlist_for_each_entry(worker, tmp, bwh, hentry)
8768cca0eeaSTejun Heo 		if (worker->current_work == work)
8778cca0eeaSTejun Heo 			return worker;
8788cca0eeaSTejun Heo 	return NULL;
8798cca0eeaSTejun Heo }
8808cca0eeaSTejun Heo 
8818cca0eeaSTejun Heo /**
8828cca0eeaSTejun Heo  * find_worker_executing_work - find worker which is executing a work
8838cca0eeaSTejun Heo  * @gcwq: gcwq of interest
8848cca0eeaSTejun Heo  * @work: work to find worker for
8858cca0eeaSTejun Heo  *
8868cca0eeaSTejun Heo  * Find a worker which is executing @work on @gcwq.  This function is
8878cca0eeaSTejun Heo  * identical to __find_worker_executing_work() except that this
8888cca0eeaSTejun Heo  * function calculates @bwh itself.
8898cca0eeaSTejun Heo  *
8908cca0eeaSTejun Heo  * CONTEXT:
8918cca0eeaSTejun Heo  * spin_lock_irq(gcwq->lock).
8928cca0eeaSTejun Heo  *
8938cca0eeaSTejun Heo  * RETURNS:
8948cca0eeaSTejun Heo  * Pointer to worker which is executing @work if found, NULL
8958cca0eeaSTejun Heo  * otherwise.
8968cca0eeaSTejun Heo  */
8978cca0eeaSTejun Heo static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
8988cca0eeaSTejun Heo 						 struct work_struct *work)
8998cca0eeaSTejun Heo {
9008cca0eeaSTejun Heo 	return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
9018cca0eeaSTejun Heo 					    work);
9028cca0eeaSTejun Heo }
9038cca0eeaSTejun Heo 
9048cca0eeaSTejun Heo /**
9057e11629dSTejun Heo  * insert_work - insert a work into gcwq
9064690c4abSTejun Heo  * @cwq: cwq @work belongs to
9074690c4abSTejun Heo  * @work: work to insert
9084690c4abSTejun Heo  * @head: insertion point
9094690c4abSTejun Heo  * @extra_flags: extra WORK_STRUCT_* flags to set
9104690c4abSTejun Heo  *
9117e11629dSTejun Heo  * Insert @work which belongs to @cwq into @gcwq after @head.
9127e11629dSTejun Heo  * @extra_flags is or'd to work_struct flags.
9134690c4abSTejun Heo  *
9144690c4abSTejun Heo  * CONTEXT:
9158b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock).
9161da177e4SLinus Torvalds  */
917b89deed3SOleg Nesterov static void insert_work(struct cpu_workqueue_struct *cwq,
9184690c4abSTejun Heo 			struct work_struct *work, struct list_head *head,
9194690c4abSTejun Heo 			unsigned int extra_flags)
920b89deed3SOleg Nesterov {
92163d95a91STejun Heo 	struct worker_pool *pool = cwq->pool;
922e1d8aa9fSFrederic Weisbecker 
9234690c4abSTejun Heo 	/* we own @work, set data and link */
9247a22ad75STejun Heo 	set_work_cwq(work, cwq, extra_flags);
9254690c4abSTejun Heo 
9266e84d644SOleg Nesterov 	/*
9276e84d644SOleg Nesterov 	 * Ensure that we get the right work->data if we see the
9286e84d644SOleg Nesterov 	 * result of list_add() below, see try_to_grab_pending().
9296e84d644SOleg Nesterov 	 */
9306e84d644SOleg Nesterov 	smp_wmb();
9314690c4abSTejun Heo 
9321a4d9b0aSOleg Nesterov 	list_add_tail(&work->entry, head);
933e22bee78STejun Heo 
934e22bee78STejun Heo 	/*
935e22bee78STejun Heo 	 * Ensure either worker_sched_deactivated() sees the above
936e22bee78STejun Heo 	 * list_add_tail() or we see zero nr_running to avoid workers
937e22bee78STejun Heo 	 * lying around lazily while there are works to be processed.
938e22bee78STejun Heo 	 */
939e22bee78STejun Heo 	smp_mb();
940e22bee78STejun Heo 
94163d95a91STejun Heo 	if (__need_more_worker(pool))
94263d95a91STejun Heo 		wake_up_worker(pool);
943b89deed3SOleg Nesterov }
944b89deed3SOleg Nesterov 
945c8efcc25STejun Heo /*
946c8efcc25STejun Heo  * Test whether @work is being queued from another work executing on the
947c8efcc25STejun Heo  * same workqueue.  This is rather expensive and should only be used from
948c8efcc25STejun Heo  * cold paths.
949c8efcc25STejun Heo  */
950c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq)
951c8efcc25STejun Heo {
952c8efcc25STejun Heo 	unsigned long flags;
953c8efcc25STejun Heo 	unsigned int cpu;
954c8efcc25STejun Heo 
955c8efcc25STejun Heo 	for_each_gcwq_cpu(cpu) {
956c8efcc25STejun Heo 		struct global_cwq *gcwq = get_gcwq(cpu);
957c8efcc25STejun Heo 		struct worker *worker;
958c8efcc25STejun Heo 		struct hlist_node *pos;
959c8efcc25STejun Heo 		int i;
960c8efcc25STejun Heo 
961c8efcc25STejun Heo 		spin_lock_irqsave(&gcwq->lock, flags);
962c8efcc25STejun Heo 		for_each_busy_worker(worker, i, pos, gcwq) {
963c8efcc25STejun Heo 			if (worker->task != current)
964c8efcc25STejun Heo 				continue;
965c8efcc25STejun Heo 			spin_unlock_irqrestore(&gcwq->lock, flags);
966c8efcc25STejun Heo 			/*
967c8efcc25STejun Heo 			 * I'm @worker, no locking necessary.  See if @work
968c8efcc25STejun Heo 			 * is headed to the same workqueue.
969c8efcc25STejun Heo 			 */
970c8efcc25STejun Heo 			return worker->current_cwq->wq == wq;
971c8efcc25STejun Heo 		}
972c8efcc25STejun Heo 		spin_unlock_irqrestore(&gcwq->lock, flags);
973c8efcc25STejun Heo 	}
974c8efcc25STejun Heo 	return false;
975c8efcc25STejun Heo }
976c8efcc25STejun Heo 
9774690c4abSTejun Heo static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
9781da177e4SLinus Torvalds 			 struct work_struct *work)
9791da177e4SLinus Torvalds {
980502ca9d8STejun Heo 	struct global_cwq *gcwq;
981502ca9d8STejun Heo 	struct cpu_workqueue_struct *cwq;
9821e19ffc6STejun Heo 	struct list_head *worklist;
9838a2e8e5dSTejun Heo 	unsigned int work_flags;
9841da177e4SLinus Torvalds 	unsigned long flags;
9851da177e4SLinus Torvalds 
986dc186ad7SThomas Gleixner 	debug_work_activate(work);
9871e19ffc6STejun Heo 
988c8efcc25STejun Heo 	/* if dying, only works from the same workqueue are allowed */
9899c5a2ba7STejun Heo 	if (unlikely(wq->flags & WQ_DRAINING) &&
990c8efcc25STejun Heo 	    WARN_ON_ONCE(!is_chained_work(wq)))
991e41e704bSTejun Heo 		return;
992e41e704bSTejun Heo 
993c7fc77f7STejun Heo 	/* determine gcwq to use */
994c7fc77f7STejun Heo 	if (!(wq->flags & WQ_UNBOUND)) {
995c7fc77f7STejun Heo 		struct global_cwq *last_gcwq;
996c7fc77f7STejun Heo 
997f3421797STejun Heo 		if (unlikely(cpu == WORK_CPU_UNBOUND))
998f3421797STejun Heo 			cpu = raw_smp_processor_id();
999f3421797STejun Heo 
100018aa9effSTejun Heo 		/*
100118aa9effSTejun Heo 		 * It's multi cpu.  If @wq is non-reentrant and @work
100218aa9effSTejun Heo 		 * was previously on a different cpu, it might still
100318aa9effSTejun Heo 		 * be running there, in which case the work needs to
100418aa9effSTejun Heo 		 * be queued on that cpu to guarantee non-reentrance.
100518aa9effSTejun Heo 		 */
1006502ca9d8STejun Heo 		gcwq = get_gcwq(cpu);
100718aa9effSTejun Heo 		if (wq->flags & WQ_NON_REENTRANT &&
100818aa9effSTejun Heo 		    (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
100918aa9effSTejun Heo 			struct worker *worker;
101018aa9effSTejun Heo 
101118aa9effSTejun Heo 			spin_lock_irqsave(&last_gcwq->lock, flags);
101218aa9effSTejun Heo 
101318aa9effSTejun Heo 			worker = find_worker_executing_work(last_gcwq, work);
101418aa9effSTejun Heo 
101518aa9effSTejun Heo 			if (worker && worker->current_cwq->wq == wq)
101618aa9effSTejun Heo 				gcwq = last_gcwq;
101718aa9effSTejun Heo 			else {
101818aa9effSTejun Heo 				/* meh... not running there, queue here */
101918aa9effSTejun Heo 				spin_unlock_irqrestore(&last_gcwq->lock, flags);
102018aa9effSTejun Heo 				spin_lock_irqsave(&gcwq->lock, flags);
102118aa9effSTejun Heo 			}
102218aa9effSTejun Heo 		} else
10238b03ae3cSTejun Heo 			spin_lock_irqsave(&gcwq->lock, flags);
1024f3421797STejun Heo 	} else {
1025f3421797STejun Heo 		gcwq = get_gcwq(WORK_CPU_UNBOUND);
1026f3421797STejun Heo 		spin_lock_irqsave(&gcwq->lock, flags);
1027502ca9d8STejun Heo 	}
1028502ca9d8STejun Heo 
1029502ca9d8STejun Heo 	/* gcwq determined, get cwq and queue */
1030502ca9d8STejun Heo 	cwq = get_cwq(gcwq->cpu, wq);
1031cdadf009STejun Heo 	trace_workqueue_queue_work(cpu, cwq, work);
1032502ca9d8STejun Heo 
1033f5b2552bSDan Carpenter 	if (WARN_ON(!list_empty(&work->entry))) {
1034f5b2552bSDan Carpenter 		spin_unlock_irqrestore(&gcwq->lock, flags);
1035f5b2552bSDan Carpenter 		return;
1036f5b2552bSDan Carpenter 	}
10371e19ffc6STejun Heo 
103873f53c4aSTejun Heo 	cwq->nr_in_flight[cwq->work_color]++;
10398a2e8e5dSTejun Heo 	work_flags = work_color_to_flags(cwq->work_color);
10401e19ffc6STejun Heo 
10411e19ffc6STejun Heo 	if (likely(cwq->nr_active < cwq->max_active)) {
1042cdadf009STejun Heo 		trace_workqueue_activate_work(work);
10431e19ffc6STejun Heo 		cwq->nr_active++;
10443270476aSTejun Heo 		worklist = &cwq->pool->worklist;
10458a2e8e5dSTejun Heo 	} else {
10468a2e8e5dSTejun Heo 		work_flags |= WORK_STRUCT_DELAYED;
10471e19ffc6STejun Heo 		worklist = &cwq->delayed_works;
10488a2e8e5dSTejun Heo 	}
10491e19ffc6STejun Heo 
10508a2e8e5dSTejun Heo 	insert_work(cwq, work, worklist, work_flags);
10511e19ffc6STejun Heo 
10528b03ae3cSTejun Heo 	spin_unlock_irqrestore(&gcwq->lock, flags);
10531da177e4SLinus Torvalds }
10541da177e4SLinus Torvalds 
10550fcb78c2SRolf Eike Beer /**
1056c1a220e7SZhang Rui  * queue_work_on - queue work on specific cpu
1057c1a220e7SZhang Rui  * @cpu: CPU number to execute work on
1058c1a220e7SZhang Rui  * @wq: workqueue to use
1059c1a220e7SZhang Rui  * @work: work to queue
1060c1a220e7SZhang Rui  *
1061d4283e93STejun Heo  * Returns %false if @work was already on a queue, %true otherwise.
1062c1a220e7SZhang Rui  *
1063c1a220e7SZhang Rui  * We queue the work to a specific CPU, the caller must ensure it
1064c1a220e7SZhang Rui  * can't go away.
1065c1a220e7SZhang Rui  */
1066d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq,
1067d4283e93STejun Heo 		   struct work_struct *work)
1068c1a220e7SZhang Rui {
1069d4283e93STejun Heo 	bool ret = false;
1070c1a220e7SZhang Rui 
107122df02bbSTejun Heo 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
10724690c4abSTejun Heo 		__queue_work(cpu, wq, work);
1073d4283e93STejun Heo 		ret = true;
1074c1a220e7SZhang Rui 	}
1075c1a220e7SZhang Rui 	return ret;
1076c1a220e7SZhang Rui }
1077c1a220e7SZhang Rui EXPORT_SYMBOL_GPL(queue_work_on);
1078c1a220e7SZhang Rui 
10790a13c00eSTejun Heo /**
10800a13c00eSTejun Heo  * queue_work - queue work on a workqueue
10810a13c00eSTejun Heo  * @wq: workqueue to use
10820a13c00eSTejun Heo  * @work: work to queue
10830a13c00eSTejun Heo  *
1084d4283e93STejun Heo  * Returns %false if @work was already on a queue, %true otherwise.
10850a13c00eSTejun Heo  *
10860a13c00eSTejun Heo  * We queue the work to the CPU on which it was submitted, but if the CPU dies
10870a13c00eSTejun Heo  * it can be processed by another CPU.
10880a13c00eSTejun Heo  */
1089d4283e93STejun Heo bool queue_work(struct workqueue_struct *wq, struct work_struct *work)
10900a13c00eSTejun Heo {
1091d4283e93STejun Heo 	bool ret;
10920a13c00eSTejun Heo 
10930a13c00eSTejun Heo 	ret = queue_work_on(get_cpu(), wq, work);
10940a13c00eSTejun Heo 	put_cpu();
10950a13c00eSTejun Heo 
10960a13c00eSTejun Heo 	return ret;
10970a13c00eSTejun Heo }
10980a13c00eSTejun Heo EXPORT_SYMBOL_GPL(queue_work);
10990a13c00eSTejun Heo 
11006d141c3fSLi Zefan static void delayed_work_timer_fn(unsigned long __data)
11011da177e4SLinus Torvalds {
110252bad64dSDavid Howells 	struct delayed_work *dwork = (struct delayed_work *)__data;
11037a22ad75STejun Heo 	struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
11041da177e4SLinus Torvalds 
11054690c4abSTejun Heo 	__queue_work(smp_processor_id(), cwq->wq, &dwork->work);
11061da177e4SLinus Torvalds }
11071da177e4SLinus Torvalds 
11080fcb78c2SRolf Eike Beer /**
11090fcb78c2SRolf Eike Beer  * queue_delayed_work_on - queue work on specific CPU after delay
11100fcb78c2SRolf Eike Beer  * @cpu: CPU number to execute work on
11110fcb78c2SRolf Eike Beer  * @wq: workqueue to use
1112af9997e4SRandy Dunlap  * @dwork: work to queue
11130fcb78c2SRolf Eike Beer  * @delay: number of jiffies to wait before queueing
11140fcb78c2SRolf Eike Beer  *
1115d4283e93STejun Heo  * Returns %false if @work was already on a queue, %true otherwise.
11160fcb78c2SRolf Eike Beer  */
1117d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
111852bad64dSDavid Howells 			   struct delayed_work *dwork, unsigned long delay)
11197a6bc1cdSVenkatesh Pallipadi {
112052bad64dSDavid Howells 	struct timer_list *timer = &dwork->timer;
112152bad64dSDavid Howells 	struct work_struct *work = &dwork->work;
1122d4283e93STejun Heo 	bool ret = false;
11237a6bc1cdSVenkatesh Pallipadi 
112422df02bbSTejun Heo 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
1125c7fc77f7STejun Heo 		unsigned int lcpu;
11267a22ad75STejun Heo 
11277a6bc1cdSVenkatesh Pallipadi 		BUG_ON(timer_pending(timer));
11287a6bc1cdSVenkatesh Pallipadi 		BUG_ON(!list_empty(&work->entry));
11297a6bc1cdSVenkatesh Pallipadi 
11308a3e77ccSAndrew Liu 		timer_stats_timer_set_start_info(&dwork->timer);
11318a3e77ccSAndrew Liu 
11327a22ad75STejun Heo 		/*
11337a22ad75STejun Heo 		 * This stores cwq for the moment, for the timer_fn.
11347a22ad75STejun Heo 		 * Note that the work's gcwq is preserved to allow
11357a22ad75STejun Heo 		 * reentrance detection for delayed works.
11367a22ad75STejun Heo 		 */
1137c7fc77f7STejun Heo 		if (!(wq->flags & WQ_UNBOUND)) {
1138c7fc77f7STejun Heo 			struct global_cwq *gcwq = get_work_gcwq(work);
1139c7fc77f7STejun Heo 
1140c7fc77f7STejun Heo 			if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND)
1141c7fc77f7STejun Heo 				lcpu = gcwq->cpu;
1142c7fc77f7STejun Heo 			else
1143c7fc77f7STejun Heo 				lcpu = raw_smp_processor_id();
1144c7fc77f7STejun Heo 		} else
1145c7fc77f7STejun Heo 			lcpu = WORK_CPU_UNBOUND;
1146c7fc77f7STejun Heo 
11477a22ad75STejun Heo 		set_work_cwq(work, get_cwq(lcpu, wq), 0);
1148c7fc77f7STejun Heo 
11497a6bc1cdSVenkatesh Pallipadi 		timer->expires = jiffies + delay;
115052bad64dSDavid Howells 		timer->data = (unsigned long)dwork;
11517a6bc1cdSVenkatesh Pallipadi 		timer->function = delayed_work_timer_fn;
115263bc0362SOleg Nesterov 
115363bc0362SOleg Nesterov 		if (unlikely(cpu >= 0))
11547a6bc1cdSVenkatesh Pallipadi 			add_timer_on(timer, cpu);
115563bc0362SOleg Nesterov 		else
115663bc0362SOleg Nesterov 			add_timer(timer);
1157d4283e93STejun Heo 		ret = true;
11587a6bc1cdSVenkatesh Pallipadi 	}
11597a6bc1cdSVenkatesh Pallipadi 	return ret;
11607a6bc1cdSVenkatesh Pallipadi }
1161ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work_on);
11621da177e4SLinus Torvalds 
1163c8e55f36STejun Heo /**
11640a13c00eSTejun Heo  * queue_delayed_work - queue work on a workqueue after delay
11650a13c00eSTejun Heo  * @wq: workqueue to use
11660a13c00eSTejun Heo  * @dwork: delayable work to queue
11670a13c00eSTejun Heo  * @delay: number of jiffies to wait before queueing
11680a13c00eSTejun Heo  *
1169d4283e93STejun Heo  * Returns %false if @work was already on a queue, %true otherwise.
11700a13c00eSTejun Heo  */
1171d4283e93STejun Heo bool queue_delayed_work(struct workqueue_struct *wq,
11720a13c00eSTejun Heo 			struct delayed_work *dwork, unsigned long delay)
11730a13c00eSTejun Heo {
11740a13c00eSTejun Heo 	if (delay == 0)
11750a13c00eSTejun Heo 		return queue_work(wq, &dwork->work);
11760a13c00eSTejun Heo 
11770a13c00eSTejun Heo 	return queue_delayed_work_on(-1, wq, dwork, delay);
11780a13c00eSTejun Heo }
11790a13c00eSTejun Heo EXPORT_SYMBOL_GPL(queue_delayed_work);
11800a13c00eSTejun Heo 
11810a13c00eSTejun Heo /**
1182c8e55f36STejun Heo  * worker_enter_idle - enter idle state
1183c8e55f36STejun Heo  * @worker: worker which is entering idle state
1184c8e55f36STejun Heo  *
1185c8e55f36STejun Heo  * @worker is entering idle state.  Update stats and idle timer if
1186c8e55f36STejun Heo  * necessary.
1187c8e55f36STejun Heo  *
1188c8e55f36STejun Heo  * LOCKING:
1189c8e55f36STejun Heo  * spin_lock_irq(gcwq->lock).
1190c8e55f36STejun Heo  */
1191c8e55f36STejun Heo static void worker_enter_idle(struct worker *worker)
11921da177e4SLinus Torvalds {
1193bd7bdd43STejun Heo 	struct worker_pool *pool = worker->pool;
1194bd7bdd43STejun Heo 	struct global_cwq *gcwq = pool->gcwq;
1195c8e55f36STejun Heo 
1196c8e55f36STejun Heo 	BUG_ON(worker->flags & WORKER_IDLE);
1197c8e55f36STejun Heo 	BUG_ON(!list_empty(&worker->entry) &&
1198c8e55f36STejun Heo 	       (worker->hentry.next || worker->hentry.pprev));
1199c8e55f36STejun Heo 
1200cb444766STejun Heo 	/* can't use worker_set_flags(), also called from start_worker() */
1201cb444766STejun Heo 	worker->flags |= WORKER_IDLE;
1202bd7bdd43STejun Heo 	pool->nr_idle++;
1203e22bee78STejun Heo 	worker->last_active = jiffies;
1204c8e55f36STejun Heo 
1205c8e55f36STejun Heo 	/* idle_list is LIFO */
1206bd7bdd43STejun Heo 	list_add(&worker->entry, &pool->idle_list);
1207db7bccf4STejun Heo 
120863d95a91STejun Heo 	if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1209628c78e7STejun Heo 		mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
1210cb444766STejun Heo 
1211544ecf31STejun Heo 	/*
1212628c78e7STejun Heo 	 * Sanity check nr_running.  Because gcwq_unbind_fn() releases
1213628c78e7STejun Heo 	 * gcwq->lock between setting %WORKER_UNBOUND and zapping
1214628c78e7STejun Heo 	 * nr_running, the warning may trigger spuriously.  Check iff
1215628c78e7STejun Heo 	 * unbind is not in progress.
1216544ecf31STejun Heo 	 */
1217628c78e7STejun Heo 	WARN_ON_ONCE(!(gcwq->flags & GCWQ_DISASSOCIATED) &&
1218bd7bdd43STejun Heo 		     pool->nr_workers == pool->nr_idle &&
121963d95a91STejun Heo 		     atomic_read(get_pool_nr_running(pool)));
1220c8e55f36STejun Heo }
1221c8e55f36STejun Heo 
1222c8e55f36STejun Heo /**
1223c8e55f36STejun Heo  * worker_leave_idle - leave idle state
1224c8e55f36STejun Heo  * @worker: worker which is leaving idle state
1225c8e55f36STejun Heo  *
1226c8e55f36STejun Heo  * @worker is leaving idle state.  Update stats.
1227c8e55f36STejun Heo  *
1228c8e55f36STejun Heo  * LOCKING:
1229c8e55f36STejun Heo  * spin_lock_irq(gcwq->lock).
1230c8e55f36STejun Heo  */
1231c8e55f36STejun Heo static void worker_leave_idle(struct worker *worker)
1232c8e55f36STejun Heo {
1233bd7bdd43STejun Heo 	struct worker_pool *pool = worker->pool;
1234c8e55f36STejun Heo 
1235c8e55f36STejun Heo 	BUG_ON(!(worker->flags & WORKER_IDLE));
1236d302f017STejun Heo 	worker_clr_flags(worker, WORKER_IDLE);
1237bd7bdd43STejun Heo 	pool->nr_idle--;
1238c8e55f36STejun Heo 	list_del_init(&worker->entry);
1239c8e55f36STejun Heo }
1240c8e55f36STejun Heo 
1241e22bee78STejun Heo /**
1242e22bee78STejun Heo  * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1243e22bee78STejun Heo  * @worker: self
1244e22bee78STejun Heo  *
1245e22bee78STejun Heo  * Works which are scheduled while the cpu is online must at least be
1246e22bee78STejun Heo  * scheduled to a worker which is bound to the cpu so that if they are
1247e22bee78STejun Heo  * flushed from cpu callbacks while cpu is going down, they are
1248e22bee78STejun Heo  * guaranteed to execute on the cpu.
1249e22bee78STejun Heo  *
1250e22bee78STejun Heo  * This function is to be used by rogue workers and rescuers to bind
1251e22bee78STejun Heo  * themselves to the target cpu and may race with cpu going down or
1252e22bee78STejun Heo  * coming online.  kthread_bind() can't be used because it may put the
1253e22bee78STejun Heo  * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1254e22bee78STejun Heo  * verbatim as it's best effort and blocking and gcwq may be
1255e22bee78STejun Heo  * [dis]associated in the meantime.
1256e22bee78STejun Heo  *
1257f2d5a0eeSTejun Heo  * This function tries set_cpus_allowed() and locks gcwq and verifies the
1258f2d5a0eeSTejun Heo  * binding against %GCWQ_DISASSOCIATED which is set during
1259f2d5a0eeSTejun Heo  * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1260f2d5a0eeSTejun Heo  * enters idle state or fetches works without dropping lock, it can
1261f2d5a0eeSTejun Heo  * guarantee the scheduling requirement described in the first paragraph.
1262e22bee78STejun Heo  *
1263e22bee78STejun Heo  * CONTEXT:
1264e22bee78STejun Heo  * Might sleep.  Called without any lock but returns with gcwq->lock
1265e22bee78STejun Heo  * held.
1266e22bee78STejun Heo  *
1267e22bee78STejun Heo  * RETURNS:
1268e22bee78STejun Heo  * %true if the associated gcwq is online (@worker is successfully
1269e22bee78STejun Heo  * bound), %false if offline.
1270e22bee78STejun Heo  */
1271e22bee78STejun Heo static bool worker_maybe_bind_and_lock(struct worker *worker)
1272972fa1c5SNamhyung Kim __acquires(&gcwq->lock)
1273e22bee78STejun Heo {
1274bd7bdd43STejun Heo 	struct global_cwq *gcwq = worker->pool->gcwq;
1275e22bee78STejun Heo 	struct task_struct *task = worker->task;
1276e22bee78STejun Heo 
1277e22bee78STejun Heo 	while (true) {
1278e22bee78STejun Heo 		/*
1279e22bee78STejun Heo 		 * The following call may fail, succeed or succeed
1280e22bee78STejun Heo 		 * without actually migrating the task to the cpu if
1281e22bee78STejun Heo 		 * it races with cpu hotunplug operation.  Verify
1282e22bee78STejun Heo 		 * against GCWQ_DISASSOCIATED.
1283e22bee78STejun Heo 		 */
1284f3421797STejun Heo 		if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1285e22bee78STejun Heo 			set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
1286e22bee78STejun Heo 
1287e22bee78STejun Heo 		spin_lock_irq(&gcwq->lock);
1288e22bee78STejun Heo 		if (gcwq->flags & GCWQ_DISASSOCIATED)
1289e22bee78STejun Heo 			return false;
1290e22bee78STejun Heo 		if (task_cpu(task) == gcwq->cpu &&
1291e22bee78STejun Heo 		    cpumask_equal(&current->cpus_allowed,
1292e22bee78STejun Heo 				  get_cpu_mask(gcwq->cpu)))
1293e22bee78STejun Heo 			return true;
1294e22bee78STejun Heo 		spin_unlock_irq(&gcwq->lock);
1295e22bee78STejun Heo 
12965035b20fSTejun Heo 		/*
12975035b20fSTejun Heo 		 * We've raced with CPU hot[un]plug.  Give it a breather
12985035b20fSTejun Heo 		 * and retry migration.  cond_resched() is required here;
12995035b20fSTejun Heo 		 * otherwise, we might deadlock against cpu_stop trying to
13005035b20fSTejun Heo 		 * bring down the CPU on non-preemptive kernel.
13015035b20fSTejun Heo 		 */
1302e22bee78STejun Heo 		cpu_relax();
13035035b20fSTejun Heo 		cond_resched();
1304e22bee78STejun Heo 	}
1305e22bee78STejun Heo }
1306e22bee78STejun Heo 
130725511a47STejun Heo struct idle_rebind {
130825511a47STejun Heo 	int			cnt;		/* # workers to be rebound */
130925511a47STejun Heo 	struct completion	done;		/* all workers rebound */
131025511a47STejun Heo };
131125511a47STejun Heo 
1312e22bee78STejun Heo /*
131325511a47STejun Heo  * Rebind an idle @worker to its CPU.  During CPU onlining, this has to
131425511a47STejun Heo  * happen synchronously for idle workers.  worker_thread() will test
131525511a47STejun Heo  * %WORKER_REBIND before leaving idle and call this function.
131625511a47STejun Heo  */
131725511a47STejun Heo static void idle_worker_rebind(struct worker *worker)
131825511a47STejun Heo {
131925511a47STejun Heo 	struct global_cwq *gcwq = worker->pool->gcwq;
132025511a47STejun Heo 
132125511a47STejun Heo 	/* CPU must be online at this point */
132225511a47STejun Heo 	WARN_ON(!worker_maybe_bind_and_lock(worker));
132325511a47STejun Heo 	if (!--worker->idle_rebind->cnt)
132425511a47STejun Heo 		complete(&worker->idle_rebind->done);
132525511a47STejun Heo 	spin_unlock_irq(&worker->pool->gcwq->lock);
132625511a47STejun Heo 
132725511a47STejun Heo 	/* we did our part, wait for rebind_workers() to finish up */
132825511a47STejun Heo 	wait_event(gcwq->rebind_hold, !(worker->flags & WORKER_REBIND));
132925511a47STejun Heo }
133025511a47STejun Heo 
133125511a47STejun Heo /*
133225511a47STejun Heo  * Function for @worker->rebind.work used to rebind unbound busy workers to
1333403c821dSTejun Heo  * the associated cpu which is coming back online.  This is scheduled by
1334403c821dSTejun Heo  * cpu up but can race with other cpu hotplug operations and may be
1335403c821dSTejun Heo  * executed twice without intervening cpu down.
1336e22bee78STejun Heo  */
133725511a47STejun Heo static void busy_worker_rebind_fn(struct work_struct *work)
1338e22bee78STejun Heo {
1339e22bee78STejun Heo 	struct worker *worker = container_of(work, struct worker, rebind_work);
1340bd7bdd43STejun Heo 	struct global_cwq *gcwq = worker->pool->gcwq;
1341e22bee78STejun Heo 
1342e22bee78STejun Heo 	if (worker_maybe_bind_and_lock(worker))
1343e22bee78STejun Heo 		worker_clr_flags(worker, WORKER_REBIND);
1344e22bee78STejun Heo 
1345e22bee78STejun Heo 	spin_unlock_irq(&gcwq->lock);
1346e22bee78STejun Heo }
1347e22bee78STejun Heo 
134825511a47STejun Heo /**
134925511a47STejun Heo  * rebind_workers - rebind all workers of a gcwq to the associated CPU
135025511a47STejun Heo  * @gcwq: gcwq of interest
135125511a47STejun Heo  *
135225511a47STejun Heo  * @gcwq->cpu is coming online.  Rebind all workers to the CPU.  Rebinding
135325511a47STejun Heo  * is different for idle and busy ones.
135425511a47STejun Heo  *
135525511a47STejun Heo  * The idle ones should be rebound synchronously and idle rebinding should
135625511a47STejun Heo  * be complete before any worker starts executing work items with
135725511a47STejun Heo  * concurrency management enabled; otherwise, scheduler may oops trying to
135825511a47STejun Heo  * wake up non-local idle worker from wq_worker_sleeping().
135925511a47STejun Heo  *
136025511a47STejun Heo  * This is achieved by repeatedly requesting rebinding until all idle
136125511a47STejun Heo  * workers are known to have been rebound under @gcwq->lock and holding all
136225511a47STejun Heo  * idle workers from becoming busy until idle rebinding is complete.
136325511a47STejun Heo  *
136425511a47STejun Heo  * Once idle workers are rebound, busy workers can be rebound as they
136525511a47STejun Heo  * finish executing their current work items.  Queueing the rebind work at
136625511a47STejun Heo  * the head of their scheduled lists is enough.  Note that nr_running will
136725511a47STejun Heo  * be properbly bumped as busy workers rebind.
136825511a47STejun Heo  *
136925511a47STejun Heo  * On return, all workers are guaranteed to either be bound or have rebind
137025511a47STejun Heo  * work item scheduled.
137125511a47STejun Heo  */
137225511a47STejun Heo static void rebind_workers(struct global_cwq *gcwq)
137325511a47STejun Heo 	__releases(&gcwq->lock) __acquires(&gcwq->lock)
137425511a47STejun Heo {
137525511a47STejun Heo 	struct idle_rebind idle_rebind;
137625511a47STejun Heo 	struct worker_pool *pool;
137725511a47STejun Heo 	struct worker *worker;
137825511a47STejun Heo 	struct hlist_node *pos;
137925511a47STejun Heo 	int i;
138025511a47STejun Heo 
138125511a47STejun Heo 	lockdep_assert_held(&gcwq->lock);
138225511a47STejun Heo 
138325511a47STejun Heo 	for_each_worker_pool(pool, gcwq)
138425511a47STejun Heo 		lockdep_assert_held(&pool->manager_mutex);
138525511a47STejun Heo 
138625511a47STejun Heo 	/*
138725511a47STejun Heo 	 * Rebind idle workers.  Interlocked both ways.  We wait for
138825511a47STejun Heo 	 * workers to rebind via @idle_rebind.done.  Workers will wait for
138925511a47STejun Heo 	 * us to finish up by watching %WORKER_REBIND.
139025511a47STejun Heo 	 */
139125511a47STejun Heo 	init_completion(&idle_rebind.done);
139225511a47STejun Heo retry:
139325511a47STejun Heo 	idle_rebind.cnt = 1;
139425511a47STejun Heo 	INIT_COMPLETION(idle_rebind.done);
139525511a47STejun Heo 
139625511a47STejun Heo 	/* set REBIND and kick idle ones, we'll wait for these later */
139725511a47STejun Heo 	for_each_worker_pool(pool, gcwq) {
139825511a47STejun Heo 		list_for_each_entry(worker, &pool->idle_list, entry) {
139925511a47STejun Heo 			if (worker->flags & WORKER_REBIND)
140025511a47STejun Heo 				continue;
140125511a47STejun Heo 
140225511a47STejun Heo 			/* morph UNBOUND to REBIND */
140325511a47STejun Heo 			worker->flags &= ~WORKER_UNBOUND;
140425511a47STejun Heo 			worker->flags |= WORKER_REBIND;
140525511a47STejun Heo 
140625511a47STejun Heo 			idle_rebind.cnt++;
140725511a47STejun Heo 			worker->idle_rebind = &idle_rebind;
140825511a47STejun Heo 
140925511a47STejun Heo 			/* worker_thread() will call idle_worker_rebind() */
141025511a47STejun Heo 			wake_up_process(worker->task);
141125511a47STejun Heo 		}
141225511a47STejun Heo 	}
141325511a47STejun Heo 
141425511a47STejun Heo 	if (--idle_rebind.cnt) {
141525511a47STejun Heo 		spin_unlock_irq(&gcwq->lock);
141625511a47STejun Heo 		wait_for_completion(&idle_rebind.done);
141725511a47STejun Heo 		spin_lock_irq(&gcwq->lock);
141825511a47STejun Heo 		/* busy ones might have become idle while waiting, retry */
141925511a47STejun Heo 		goto retry;
142025511a47STejun Heo 	}
142125511a47STejun Heo 
142225511a47STejun Heo 	/*
142325511a47STejun Heo 	 * All idle workers are rebound and waiting for %WORKER_REBIND to
142425511a47STejun Heo 	 * be cleared inside idle_worker_rebind().  Clear and release.
142525511a47STejun Heo 	 * Clearing %WORKER_REBIND from this foreign context is safe
142625511a47STejun Heo 	 * because these workers are still guaranteed to be idle.
142725511a47STejun Heo 	 */
142825511a47STejun Heo 	for_each_worker_pool(pool, gcwq)
142925511a47STejun Heo 		list_for_each_entry(worker, &pool->idle_list, entry)
143025511a47STejun Heo 			worker->flags &= ~WORKER_REBIND;
143125511a47STejun Heo 
143225511a47STejun Heo 	wake_up_all(&gcwq->rebind_hold);
143325511a47STejun Heo 
143425511a47STejun Heo 	/* rebind busy workers */
143525511a47STejun Heo 	for_each_busy_worker(worker, i, pos, gcwq) {
143625511a47STejun Heo 		struct work_struct *rebind_work = &worker->rebind_work;
143725511a47STejun Heo 
143825511a47STejun Heo 		/* morph UNBOUND to REBIND */
143925511a47STejun Heo 		worker->flags &= ~WORKER_UNBOUND;
144025511a47STejun Heo 		worker->flags |= WORKER_REBIND;
144125511a47STejun Heo 
144225511a47STejun Heo 		if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
144325511a47STejun Heo 				     work_data_bits(rebind_work)))
144425511a47STejun Heo 			continue;
144525511a47STejun Heo 
144625511a47STejun Heo 		/* wq doesn't matter, use the default one */
144725511a47STejun Heo 		debug_work_activate(rebind_work);
144825511a47STejun Heo 		insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
144925511a47STejun Heo 			    worker->scheduled.next,
145025511a47STejun Heo 			    work_color_to_flags(WORK_NO_COLOR));
145125511a47STejun Heo 	}
145225511a47STejun Heo }
145325511a47STejun Heo 
1454c34056a3STejun Heo static struct worker *alloc_worker(void)
1455c34056a3STejun Heo {
1456c34056a3STejun Heo 	struct worker *worker;
1457c34056a3STejun Heo 
1458c34056a3STejun Heo 	worker = kzalloc(sizeof(*worker), GFP_KERNEL);
1459c8e55f36STejun Heo 	if (worker) {
1460c8e55f36STejun Heo 		INIT_LIST_HEAD(&worker->entry);
1461affee4b2STejun Heo 		INIT_LIST_HEAD(&worker->scheduled);
146225511a47STejun Heo 		INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn);
1463e22bee78STejun Heo 		/* on creation a worker is in !idle && prep state */
1464e22bee78STejun Heo 		worker->flags = WORKER_PREP;
1465c8e55f36STejun Heo 	}
1466c34056a3STejun Heo 	return worker;
1467c34056a3STejun Heo }
1468c34056a3STejun Heo 
1469c34056a3STejun Heo /**
1470c34056a3STejun Heo  * create_worker - create a new workqueue worker
147163d95a91STejun Heo  * @pool: pool the new worker will belong to
1472c34056a3STejun Heo  *
147363d95a91STejun Heo  * Create a new worker which is bound to @pool.  The returned worker
1474c34056a3STejun Heo  * can be started by calling start_worker() or destroyed using
1475c34056a3STejun Heo  * destroy_worker().
1476c34056a3STejun Heo  *
1477c34056a3STejun Heo  * CONTEXT:
1478c34056a3STejun Heo  * Might sleep.  Does GFP_KERNEL allocations.
1479c34056a3STejun Heo  *
1480c34056a3STejun Heo  * RETURNS:
1481c34056a3STejun Heo  * Pointer to the newly created worker.
1482c34056a3STejun Heo  */
1483bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool)
1484c34056a3STejun Heo {
148563d95a91STejun Heo 	struct global_cwq *gcwq = pool->gcwq;
14863270476aSTejun Heo 	const char *pri = worker_pool_pri(pool) ? "H" : "";
1487c34056a3STejun Heo 	struct worker *worker = NULL;
1488f3421797STejun Heo 	int id = -1;
1489c34056a3STejun Heo 
14908b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
1491bd7bdd43STejun Heo 	while (ida_get_new(&pool->worker_ida, &id)) {
14928b03ae3cSTejun Heo 		spin_unlock_irq(&gcwq->lock);
1493bd7bdd43STejun Heo 		if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
1494c34056a3STejun Heo 			goto fail;
14958b03ae3cSTejun Heo 		spin_lock_irq(&gcwq->lock);
1496c34056a3STejun Heo 	}
14978b03ae3cSTejun Heo 	spin_unlock_irq(&gcwq->lock);
1498c34056a3STejun Heo 
1499c34056a3STejun Heo 	worker = alloc_worker();
1500c34056a3STejun Heo 	if (!worker)
1501c34056a3STejun Heo 		goto fail;
1502c34056a3STejun Heo 
1503bd7bdd43STejun Heo 	worker->pool = pool;
1504c34056a3STejun Heo 	worker->id = id;
1505c34056a3STejun Heo 
1506bc2ae0f5STejun Heo 	if (gcwq->cpu != WORK_CPU_UNBOUND)
150794dcf29aSEric Dumazet 		worker->task = kthread_create_on_node(worker_thread,
15083270476aSTejun Heo 					worker, cpu_to_node(gcwq->cpu),
15093270476aSTejun Heo 					"kworker/%u:%d%s", gcwq->cpu, id, pri);
1510f3421797STejun Heo 	else
1511f3421797STejun Heo 		worker->task = kthread_create(worker_thread, worker,
15123270476aSTejun Heo 					      "kworker/u:%d%s", id, pri);
1513c34056a3STejun Heo 	if (IS_ERR(worker->task))
1514c34056a3STejun Heo 		goto fail;
1515c34056a3STejun Heo 
15163270476aSTejun Heo 	if (worker_pool_pri(pool))
15173270476aSTejun Heo 		set_user_nice(worker->task, HIGHPRI_NICE_LEVEL);
15183270476aSTejun Heo 
1519db7bccf4STejun Heo 	/*
1520bc2ae0f5STejun Heo 	 * Determine CPU binding of the new worker depending on
1521bc2ae0f5STejun Heo 	 * %GCWQ_DISASSOCIATED.  The caller is responsible for ensuring the
1522bc2ae0f5STejun Heo 	 * flag remains stable across this function.  See the comments
1523bc2ae0f5STejun Heo 	 * above the flag definition for details.
1524bc2ae0f5STejun Heo 	 *
1525bc2ae0f5STejun Heo 	 * As an unbound worker may later become a regular one if CPU comes
1526bc2ae0f5STejun Heo 	 * online, make sure every worker has %PF_THREAD_BOUND set.
1527db7bccf4STejun Heo 	 */
1528bc2ae0f5STejun Heo 	if (!(gcwq->flags & GCWQ_DISASSOCIATED)) {
15298b03ae3cSTejun Heo 		kthread_bind(worker->task, gcwq->cpu);
1530bc2ae0f5STejun Heo 	} else {
1531db7bccf4STejun Heo 		worker->task->flags |= PF_THREAD_BOUND;
1532f3421797STejun Heo 		worker->flags |= WORKER_UNBOUND;
1533f3421797STejun Heo 	}
1534c34056a3STejun Heo 
1535c34056a3STejun Heo 	return worker;
1536c34056a3STejun Heo fail:
1537c34056a3STejun Heo 	if (id >= 0) {
15388b03ae3cSTejun Heo 		spin_lock_irq(&gcwq->lock);
1539bd7bdd43STejun Heo 		ida_remove(&pool->worker_ida, id);
15408b03ae3cSTejun Heo 		spin_unlock_irq(&gcwq->lock);
1541c34056a3STejun Heo 	}
1542c34056a3STejun Heo 	kfree(worker);
1543c34056a3STejun Heo 	return NULL;
1544c34056a3STejun Heo }
1545c34056a3STejun Heo 
1546c34056a3STejun Heo /**
1547c34056a3STejun Heo  * start_worker - start a newly created worker
1548c34056a3STejun Heo  * @worker: worker to start
1549c34056a3STejun Heo  *
1550c8e55f36STejun Heo  * Make the gcwq aware of @worker and start it.
1551c34056a3STejun Heo  *
1552c34056a3STejun Heo  * CONTEXT:
15538b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock).
1554c34056a3STejun Heo  */
1555c34056a3STejun Heo static void start_worker(struct worker *worker)
1556c34056a3STejun Heo {
1557cb444766STejun Heo 	worker->flags |= WORKER_STARTED;
1558bd7bdd43STejun Heo 	worker->pool->nr_workers++;
1559c8e55f36STejun Heo 	worker_enter_idle(worker);
1560c34056a3STejun Heo 	wake_up_process(worker->task);
1561c34056a3STejun Heo }
1562c34056a3STejun Heo 
1563c34056a3STejun Heo /**
1564c34056a3STejun Heo  * destroy_worker - destroy a workqueue worker
1565c34056a3STejun Heo  * @worker: worker to be destroyed
1566c34056a3STejun Heo  *
1567c8e55f36STejun Heo  * Destroy @worker and adjust @gcwq stats accordingly.
1568c8e55f36STejun Heo  *
1569c8e55f36STejun Heo  * CONTEXT:
1570c8e55f36STejun Heo  * spin_lock_irq(gcwq->lock) which is released and regrabbed.
1571c34056a3STejun Heo  */
1572c34056a3STejun Heo static void destroy_worker(struct worker *worker)
1573c34056a3STejun Heo {
1574bd7bdd43STejun Heo 	struct worker_pool *pool = worker->pool;
1575bd7bdd43STejun Heo 	struct global_cwq *gcwq = pool->gcwq;
1576c34056a3STejun Heo 	int id = worker->id;
1577c34056a3STejun Heo 
1578c34056a3STejun Heo 	/* sanity check frenzy */
1579c34056a3STejun Heo 	BUG_ON(worker->current_work);
1580affee4b2STejun Heo 	BUG_ON(!list_empty(&worker->scheduled));
1581c34056a3STejun Heo 
1582c8e55f36STejun Heo 	if (worker->flags & WORKER_STARTED)
1583bd7bdd43STejun Heo 		pool->nr_workers--;
1584c8e55f36STejun Heo 	if (worker->flags & WORKER_IDLE)
1585bd7bdd43STejun Heo 		pool->nr_idle--;
1586c8e55f36STejun Heo 
1587c8e55f36STejun Heo 	list_del_init(&worker->entry);
1588cb444766STejun Heo 	worker->flags |= WORKER_DIE;
1589c8e55f36STejun Heo 
1590c8e55f36STejun Heo 	spin_unlock_irq(&gcwq->lock);
1591c8e55f36STejun Heo 
1592c34056a3STejun Heo 	kthread_stop(worker->task);
1593c34056a3STejun Heo 	kfree(worker);
1594c34056a3STejun Heo 
15958b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
1596bd7bdd43STejun Heo 	ida_remove(&pool->worker_ida, id);
1597c34056a3STejun Heo }
1598c34056a3STejun Heo 
159963d95a91STejun Heo static void idle_worker_timeout(unsigned long __pool)
1600e22bee78STejun Heo {
160163d95a91STejun Heo 	struct worker_pool *pool = (void *)__pool;
160263d95a91STejun Heo 	struct global_cwq *gcwq = pool->gcwq;
1603e22bee78STejun Heo 
1604e22bee78STejun Heo 	spin_lock_irq(&gcwq->lock);
1605e22bee78STejun Heo 
160663d95a91STejun Heo 	if (too_many_workers(pool)) {
1607e22bee78STejun Heo 		struct worker *worker;
1608e22bee78STejun Heo 		unsigned long expires;
1609e22bee78STejun Heo 
1610e22bee78STejun Heo 		/* idle_list is kept in LIFO order, check the last one */
161163d95a91STejun Heo 		worker = list_entry(pool->idle_list.prev, struct worker, entry);
1612e22bee78STejun Heo 		expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1613e22bee78STejun Heo 
1614e22bee78STejun Heo 		if (time_before(jiffies, expires))
161563d95a91STejun Heo 			mod_timer(&pool->idle_timer, expires);
1616e22bee78STejun Heo 		else {
1617e22bee78STejun Heo 			/* it's been idle for too long, wake up manager */
161811ebea50STejun Heo 			pool->flags |= POOL_MANAGE_WORKERS;
161963d95a91STejun Heo 			wake_up_worker(pool);
1620e22bee78STejun Heo 		}
1621e22bee78STejun Heo 	}
1622e22bee78STejun Heo 
1623e22bee78STejun Heo 	spin_unlock_irq(&gcwq->lock);
1624e22bee78STejun Heo }
1625e22bee78STejun Heo 
1626e22bee78STejun Heo static bool send_mayday(struct work_struct *work)
1627e22bee78STejun Heo {
1628e22bee78STejun Heo 	struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1629e22bee78STejun Heo 	struct workqueue_struct *wq = cwq->wq;
1630f3421797STejun Heo 	unsigned int cpu;
1631e22bee78STejun Heo 
1632e22bee78STejun Heo 	if (!(wq->flags & WQ_RESCUER))
1633e22bee78STejun Heo 		return false;
1634e22bee78STejun Heo 
1635e22bee78STejun Heo 	/* mayday mayday mayday */
1636bd7bdd43STejun Heo 	cpu = cwq->pool->gcwq->cpu;
1637f3421797STejun Heo 	/* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1638f3421797STejun Heo 	if (cpu == WORK_CPU_UNBOUND)
1639f3421797STejun Heo 		cpu = 0;
1640f2e005aaSTejun Heo 	if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
1641e22bee78STejun Heo 		wake_up_process(wq->rescuer->task);
1642e22bee78STejun Heo 	return true;
1643e22bee78STejun Heo }
1644e22bee78STejun Heo 
164563d95a91STejun Heo static void gcwq_mayday_timeout(unsigned long __pool)
1646e22bee78STejun Heo {
164763d95a91STejun Heo 	struct worker_pool *pool = (void *)__pool;
164863d95a91STejun Heo 	struct global_cwq *gcwq = pool->gcwq;
1649e22bee78STejun Heo 	struct work_struct *work;
1650e22bee78STejun Heo 
1651e22bee78STejun Heo 	spin_lock_irq(&gcwq->lock);
1652e22bee78STejun Heo 
165363d95a91STejun Heo 	if (need_to_create_worker(pool)) {
1654e22bee78STejun Heo 		/*
1655e22bee78STejun Heo 		 * We've been trying to create a new worker but
1656e22bee78STejun Heo 		 * haven't been successful.  We might be hitting an
1657e22bee78STejun Heo 		 * allocation deadlock.  Send distress signals to
1658e22bee78STejun Heo 		 * rescuers.
1659e22bee78STejun Heo 		 */
166063d95a91STejun Heo 		list_for_each_entry(work, &pool->worklist, entry)
1661e22bee78STejun Heo 			send_mayday(work);
1662e22bee78STejun Heo 	}
1663e22bee78STejun Heo 
1664e22bee78STejun Heo 	spin_unlock_irq(&gcwq->lock);
1665e22bee78STejun Heo 
166663d95a91STejun Heo 	mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
1667e22bee78STejun Heo }
1668e22bee78STejun Heo 
1669e22bee78STejun Heo /**
1670e22bee78STejun Heo  * maybe_create_worker - create a new worker if necessary
167163d95a91STejun Heo  * @pool: pool to create a new worker for
1672e22bee78STejun Heo  *
167363d95a91STejun Heo  * Create a new worker for @pool if necessary.  @pool is guaranteed to
1674e22bee78STejun Heo  * have at least one idle worker on return from this function.  If
1675e22bee78STejun Heo  * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
167663d95a91STejun Heo  * sent to all rescuers with works scheduled on @pool to resolve
1677e22bee78STejun Heo  * possible allocation deadlock.
1678e22bee78STejun Heo  *
1679e22bee78STejun Heo  * On return, need_to_create_worker() is guaranteed to be false and
1680e22bee78STejun Heo  * may_start_working() true.
1681e22bee78STejun Heo  *
1682e22bee78STejun Heo  * LOCKING:
1683e22bee78STejun Heo  * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1684e22bee78STejun Heo  * multiple times.  Does GFP_KERNEL allocations.  Called only from
1685e22bee78STejun Heo  * manager.
1686e22bee78STejun Heo  *
1687e22bee78STejun Heo  * RETURNS:
1688e22bee78STejun Heo  * false if no action was taken and gcwq->lock stayed locked, true
1689e22bee78STejun Heo  * otherwise.
1690e22bee78STejun Heo  */
169163d95a91STejun Heo static bool maybe_create_worker(struct worker_pool *pool)
169206bd6ebfSNamhyung Kim __releases(&gcwq->lock)
169306bd6ebfSNamhyung Kim __acquires(&gcwq->lock)
1694e22bee78STejun Heo {
169563d95a91STejun Heo 	struct global_cwq *gcwq = pool->gcwq;
169663d95a91STejun Heo 
169763d95a91STejun Heo 	if (!need_to_create_worker(pool))
1698e22bee78STejun Heo 		return false;
1699e22bee78STejun Heo restart:
17009f9c2364STejun Heo 	spin_unlock_irq(&gcwq->lock);
17019f9c2364STejun Heo 
1702e22bee78STejun Heo 	/* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
170363d95a91STejun Heo 	mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
1704e22bee78STejun Heo 
1705e22bee78STejun Heo 	while (true) {
1706e22bee78STejun Heo 		struct worker *worker;
1707e22bee78STejun Heo 
1708bc2ae0f5STejun Heo 		worker = create_worker(pool);
1709e22bee78STejun Heo 		if (worker) {
171063d95a91STejun Heo 			del_timer_sync(&pool->mayday_timer);
1711e22bee78STejun Heo 			spin_lock_irq(&gcwq->lock);
1712e22bee78STejun Heo 			start_worker(worker);
171363d95a91STejun Heo 			BUG_ON(need_to_create_worker(pool));
1714e22bee78STejun Heo 			return true;
1715e22bee78STejun Heo 		}
1716e22bee78STejun Heo 
171763d95a91STejun Heo 		if (!need_to_create_worker(pool))
1718e22bee78STejun Heo 			break;
1719e22bee78STejun Heo 
1720e22bee78STejun Heo 		__set_current_state(TASK_INTERRUPTIBLE);
1721e22bee78STejun Heo 		schedule_timeout(CREATE_COOLDOWN);
17229f9c2364STejun Heo 
172363d95a91STejun Heo 		if (!need_to_create_worker(pool))
1724e22bee78STejun Heo 			break;
1725e22bee78STejun Heo 	}
1726e22bee78STejun Heo 
172763d95a91STejun Heo 	del_timer_sync(&pool->mayday_timer);
1728e22bee78STejun Heo 	spin_lock_irq(&gcwq->lock);
172963d95a91STejun Heo 	if (need_to_create_worker(pool))
1730e22bee78STejun Heo 		goto restart;
1731e22bee78STejun Heo 	return true;
1732e22bee78STejun Heo }
1733e22bee78STejun Heo 
1734e22bee78STejun Heo /**
1735e22bee78STejun Heo  * maybe_destroy_worker - destroy workers which have been idle for a while
173663d95a91STejun Heo  * @pool: pool to destroy workers for
1737e22bee78STejun Heo  *
173863d95a91STejun Heo  * Destroy @pool workers which have been idle for longer than
1739e22bee78STejun Heo  * IDLE_WORKER_TIMEOUT.
1740e22bee78STejun Heo  *
1741e22bee78STejun Heo  * LOCKING:
1742e22bee78STejun Heo  * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1743e22bee78STejun Heo  * multiple times.  Called only from manager.
1744e22bee78STejun Heo  *
1745e22bee78STejun Heo  * RETURNS:
1746e22bee78STejun Heo  * false if no action was taken and gcwq->lock stayed locked, true
1747e22bee78STejun Heo  * otherwise.
1748e22bee78STejun Heo  */
174963d95a91STejun Heo static bool maybe_destroy_workers(struct worker_pool *pool)
1750e22bee78STejun Heo {
1751e22bee78STejun Heo 	bool ret = false;
1752e22bee78STejun Heo 
175363d95a91STejun Heo 	while (too_many_workers(pool)) {
1754e22bee78STejun Heo 		struct worker *worker;
1755e22bee78STejun Heo 		unsigned long expires;
1756e22bee78STejun Heo 
175763d95a91STejun Heo 		worker = list_entry(pool->idle_list.prev, struct worker, entry);
1758e22bee78STejun Heo 		expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1759e22bee78STejun Heo 
1760e22bee78STejun Heo 		if (time_before(jiffies, expires)) {
176163d95a91STejun Heo 			mod_timer(&pool->idle_timer, expires);
1762e22bee78STejun Heo 			break;
1763e22bee78STejun Heo 		}
1764e22bee78STejun Heo 
1765e22bee78STejun Heo 		destroy_worker(worker);
1766e22bee78STejun Heo 		ret = true;
1767e22bee78STejun Heo 	}
1768e22bee78STejun Heo 
1769e22bee78STejun Heo 	return ret;
1770e22bee78STejun Heo }
1771e22bee78STejun Heo 
1772e22bee78STejun Heo /**
1773e22bee78STejun Heo  * manage_workers - manage worker pool
1774e22bee78STejun Heo  * @worker: self
1775e22bee78STejun Heo  *
1776e22bee78STejun Heo  * Assume the manager role and manage gcwq worker pool @worker belongs
1777e22bee78STejun Heo  * to.  At any given time, there can be only zero or one manager per
1778e22bee78STejun Heo  * gcwq.  The exclusion is handled automatically by this function.
1779e22bee78STejun Heo  *
1780e22bee78STejun Heo  * The caller can safely start processing works on false return.  On
1781e22bee78STejun Heo  * true return, it's guaranteed that need_to_create_worker() is false
1782e22bee78STejun Heo  * and may_start_working() is true.
1783e22bee78STejun Heo  *
1784e22bee78STejun Heo  * CONTEXT:
1785e22bee78STejun Heo  * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1786e22bee78STejun Heo  * multiple times.  Does GFP_KERNEL allocations.
1787e22bee78STejun Heo  *
1788e22bee78STejun Heo  * RETURNS:
1789e22bee78STejun Heo  * false if no action was taken and gcwq->lock stayed locked, true if
1790e22bee78STejun Heo  * some action was taken.
1791e22bee78STejun Heo  */
1792e22bee78STejun Heo static bool manage_workers(struct worker *worker)
1793e22bee78STejun Heo {
179463d95a91STejun Heo 	struct worker_pool *pool = worker->pool;
1795e22bee78STejun Heo 	bool ret = false;
1796e22bee78STejun Heo 
179760373152STejun Heo 	if (!mutex_trylock(&pool->manager_mutex))
1798e22bee78STejun Heo 		return ret;
1799e22bee78STejun Heo 
180011ebea50STejun Heo 	pool->flags &= ~POOL_MANAGE_WORKERS;
1801e22bee78STejun Heo 
1802e22bee78STejun Heo 	/*
1803e22bee78STejun Heo 	 * Destroy and then create so that may_start_working() is true
1804e22bee78STejun Heo 	 * on return.
1805e22bee78STejun Heo 	 */
180663d95a91STejun Heo 	ret |= maybe_destroy_workers(pool);
180763d95a91STejun Heo 	ret |= maybe_create_worker(pool);
1808e22bee78STejun Heo 
180960373152STejun Heo 	mutex_unlock(&pool->manager_mutex);
1810e22bee78STejun Heo 	return ret;
1811e22bee78STejun Heo }
1812e22bee78STejun Heo 
1813a62428c0STejun Heo /**
1814affee4b2STejun Heo  * move_linked_works - move linked works to a list
1815affee4b2STejun Heo  * @work: start of series of works to be scheduled
1816affee4b2STejun Heo  * @head: target list to append @work to
1817affee4b2STejun Heo  * @nextp: out paramter for nested worklist walking
1818affee4b2STejun Heo  *
1819affee4b2STejun Heo  * Schedule linked works starting from @work to @head.  Work series to
1820affee4b2STejun Heo  * be scheduled starts at @work and includes any consecutive work with
1821affee4b2STejun Heo  * WORK_STRUCT_LINKED set in its predecessor.
1822affee4b2STejun Heo  *
1823affee4b2STejun Heo  * If @nextp is not NULL, it's updated to point to the next work of
1824affee4b2STejun Heo  * the last scheduled work.  This allows move_linked_works() to be
1825affee4b2STejun Heo  * nested inside outer list_for_each_entry_safe().
1826affee4b2STejun Heo  *
1827affee4b2STejun Heo  * CONTEXT:
18288b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock).
1829affee4b2STejun Heo  */
1830affee4b2STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head,
1831affee4b2STejun Heo 			      struct work_struct **nextp)
1832affee4b2STejun Heo {
1833affee4b2STejun Heo 	struct work_struct *n;
1834affee4b2STejun Heo 
1835affee4b2STejun Heo 	/*
1836affee4b2STejun Heo 	 * Linked worklist will always end before the end of the list,
1837affee4b2STejun Heo 	 * use NULL for list head.
1838affee4b2STejun Heo 	 */
1839affee4b2STejun Heo 	list_for_each_entry_safe_from(work, n, NULL, entry) {
1840affee4b2STejun Heo 		list_move_tail(&work->entry, head);
1841affee4b2STejun Heo 		if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1842affee4b2STejun Heo 			break;
1843affee4b2STejun Heo 	}
1844affee4b2STejun Heo 
1845affee4b2STejun Heo 	/*
1846affee4b2STejun Heo 	 * If we're already inside safe list traversal and have moved
1847affee4b2STejun Heo 	 * multiple works to the scheduled queue, the next position
1848affee4b2STejun Heo 	 * needs to be updated.
1849affee4b2STejun Heo 	 */
1850affee4b2STejun Heo 	if (nextp)
1851affee4b2STejun Heo 		*nextp = n;
1852affee4b2STejun Heo }
1853affee4b2STejun Heo 
18541e19ffc6STejun Heo static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
18551e19ffc6STejun Heo {
18561e19ffc6STejun Heo 	struct work_struct *work = list_first_entry(&cwq->delayed_works,
18571da177e4SLinus Torvalds 						    struct work_struct, entry);
18581e19ffc6STejun Heo 
1859cdadf009STejun Heo 	trace_workqueue_activate_work(work);
18603270476aSTejun Heo 	move_linked_works(work, &cwq->pool->worklist, NULL);
18618a2e8e5dSTejun Heo 	__clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
18621e19ffc6STejun Heo 	cwq->nr_active++;
18631e19ffc6STejun Heo }
18641e19ffc6STejun Heo 
1865affee4b2STejun Heo /**
186673f53c4aSTejun Heo  * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
186773f53c4aSTejun Heo  * @cwq: cwq of interest
186873f53c4aSTejun Heo  * @color: color of work which left the queue
18698a2e8e5dSTejun Heo  * @delayed: for a delayed work
187073f53c4aSTejun Heo  *
187173f53c4aSTejun Heo  * A work either has completed or is removed from pending queue,
187273f53c4aSTejun Heo  * decrement nr_in_flight of its cwq and handle workqueue flushing.
187373f53c4aSTejun Heo  *
187473f53c4aSTejun Heo  * CONTEXT:
18758b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock).
187673f53c4aSTejun Heo  */
18778a2e8e5dSTejun Heo static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
18788a2e8e5dSTejun Heo 				 bool delayed)
187973f53c4aSTejun Heo {
188073f53c4aSTejun Heo 	/* ignore uncolored works */
188173f53c4aSTejun Heo 	if (color == WORK_NO_COLOR)
188273f53c4aSTejun Heo 		return;
188373f53c4aSTejun Heo 
188473f53c4aSTejun Heo 	cwq->nr_in_flight[color]--;
18851e19ffc6STejun Heo 
18868a2e8e5dSTejun Heo 	if (!delayed) {
18878a2e8e5dSTejun Heo 		cwq->nr_active--;
1888502ca9d8STejun Heo 		if (!list_empty(&cwq->delayed_works)) {
18891e19ffc6STejun Heo 			/* one down, submit a delayed one */
1890502ca9d8STejun Heo 			if (cwq->nr_active < cwq->max_active)
18911e19ffc6STejun Heo 				cwq_activate_first_delayed(cwq);
1892502ca9d8STejun Heo 		}
18938a2e8e5dSTejun Heo 	}
189473f53c4aSTejun Heo 
189573f53c4aSTejun Heo 	/* is flush in progress and are we at the flushing tip? */
189673f53c4aSTejun Heo 	if (likely(cwq->flush_color != color))
189773f53c4aSTejun Heo 		return;
189873f53c4aSTejun Heo 
189973f53c4aSTejun Heo 	/* are there still in-flight works? */
190073f53c4aSTejun Heo 	if (cwq->nr_in_flight[color])
190173f53c4aSTejun Heo 		return;
190273f53c4aSTejun Heo 
190373f53c4aSTejun Heo 	/* this cwq is done, clear flush_color */
190473f53c4aSTejun Heo 	cwq->flush_color = -1;
190573f53c4aSTejun Heo 
190673f53c4aSTejun Heo 	/*
190773f53c4aSTejun Heo 	 * If this was the last cwq, wake up the first flusher.  It
190873f53c4aSTejun Heo 	 * will handle the rest.
190973f53c4aSTejun Heo 	 */
191073f53c4aSTejun Heo 	if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
191173f53c4aSTejun Heo 		complete(&cwq->wq->first_flusher->done);
191273f53c4aSTejun Heo }
191373f53c4aSTejun Heo 
191473f53c4aSTejun Heo /**
1915a62428c0STejun Heo  * process_one_work - process single work
1916c34056a3STejun Heo  * @worker: self
1917a62428c0STejun Heo  * @work: work to process
1918a62428c0STejun Heo  *
1919a62428c0STejun Heo  * Process @work.  This function contains all the logics necessary to
1920a62428c0STejun Heo  * process a single work including synchronization against and
1921a62428c0STejun Heo  * interaction with other workers on the same cpu, queueing and
1922a62428c0STejun Heo  * flushing.  As long as context requirement is met, any worker can
1923a62428c0STejun Heo  * call this function to process a work.
1924a62428c0STejun Heo  *
1925a62428c0STejun Heo  * CONTEXT:
19268b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock) which is released and regrabbed.
1927a62428c0STejun Heo  */
1928c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work)
192906bd6ebfSNamhyung Kim __releases(&gcwq->lock)
193006bd6ebfSNamhyung Kim __acquires(&gcwq->lock)
19311da177e4SLinus Torvalds {
19327e11629dSTejun Heo 	struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1933bd7bdd43STejun Heo 	struct worker_pool *pool = worker->pool;
1934bd7bdd43STejun Heo 	struct global_cwq *gcwq = pool->gcwq;
1935c8e55f36STejun Heo 	struct hlist_head *bwh = busy_worker_head(gcwq, work);
1936fb0e7bebSTejun Heo 	bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
19376bb49e59SDavid Howells 	work_func_t f = work->func;
193873f53c4aSTejun Heo 	int work_color;
19397e11629dSTejun Heo 	struct worker *collision;
19404e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
19414e6045f1SJohannes Berg 	/*
1942a62428c0STejun Heo 	 * It is permissible to free the struct work_struct from
1943a62428c0STejun Heo 	 * inside the function that is called from it, this we need to
1944a62428c0STejun Heo 	 * take into account for lockdep too.  To avoid bogus "held
1945a62428c0STejun Heo 	 * lock freed" warnings as well as problems when looking into
1946a62428c0STejun Heo 	 * work->lockdep_map, make a copy and use that here.
19474e6045f1SJohannes Berg 	 */
19484d82a1deSPeter Zijlstra 	struct lockdep_map lockdep_map;
19494d82a1deSPeter Zijlstra 
19504d82a1deSPeter Zijlstra 	lockdep_copy_map(&lockdep_map, &work->lockdep_map);
19514e6045f1SJohannes Berg #endif
19526fec10a1STejun Heo 	/*
19536fec10a1STejun Heo 	 * Ensure we're on the correct CPU.  DISASSOCIATED test is
19546fec10a1STejun Heo 	 * necessary to avoid spurious warnings from rescuers servicing the
19556fec10a1STejun Heo 	 * unbound or a disassociated gcwq.
19566fec10a1STejun Heo 	 */
195725511a47STejun Heo 	WARN_ON_ONCE(!(worker->flags & (WORKER_UNBOUND | WORKER_REBIND)) &&
19586fec10a1STejun Heo 		     !(gcwq->flags & GCWQ_DISASSOCIATED) &&
195925511a47STejun Heo 		     raw_smp_processor_id() != gcwq->cpu);
196025511a47STejun Heo 
19617e11629dSTejun Heo 	/*
19627e11629dSTejun Heo 	 * A single work shouldn't be executed concurrently by
19637e11629dSTejun Heo 	 * multiple workers on a single cpu.  Check whether anyone is
19647e11629dSTejun Heo 	 * already processing the work.  If so, defer the work to the
19657e11629dSTejun Heo 	 * currently executing one.
19667e11629dSTejun Heo 	 */
19677e11629dSTejun Heo 	collision = __find_worker_executing_work(gcwq, bwh, work);
19687e11629dSTejun Heo 	if (unlikely(collision)) {
19697e11629dSTejun Heo 		move_linked_works(work, &collision->scheduled, NULL);
19707e11629dSTejun Heo 		return;
19717e11629dSTejun Heo 	}
19721da177e4SLinus Torvalds 
1973a62428c0STejun Heo 	/* claim and process */
19741da177e4SLinus Torvalds 	debug_work_deactivate(work);
1975c8e55f36STejun Heo 	hlist_add_head(&worker->hentry, bwh);
1976c34056a3STejun Heo 	worker->current_work = work;
19778cca0eeaSTejun Heo 	worker->current_cwq = cwq;
197873f53c4aSTejun Heo 	work_color = get_work_color(work);
19797a22ad75STejun Heo 
19807a22ad75STejun Heo 	/* record the current cpu number in the work data and dequeue */
19817a22ad75STejun Heo 	set_work_cpu(work, gcwq->cpu);
1982a62428c0STejun Heo 	list_del_init(&work->entry);
1983a62428c0STejun Heo 
1984649027d7STejun Heo 	/*
1985fb0e7bebSTejun Heo 	 * CPU intensive works don't participate in concurrency
1986fb0e7bebSTejun Heo 	 * management.  They're the scheduler's responsibility.
1987fb0e7bebSTejun Heo 	 */
1988fb0e7bebSTejun Heo 	if (unlikely(cpu_intensive))
1989fb0e7bebSTejun Heo 		worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
1990fb0e7bebSTejun Heo 
1991974271c4STejun Heo 	/*
1992974271c4STejun Heo 	 * Unbound gcwq isn't concurrency managed and work items should be
1993974271c4STejun Heo 	 * executed ASAP.  Wake up another worker if necessary.
1994974271c4STejun Heo 	 */
199563d95a91STejun Heo 	if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
199663d95a91STejun Heo 		wake_up_worker(pool);
1997974271c4STejun Heo 
19988b03ae3cSTejun Heo 	spin_unlock_irq(&gcwq->lock);
19991da177e4SLinus Torvalds 
200023b2e599SOleg Nesterov 	work_clear_pending(work);
2001e159489bSTejun Heo 	lock_map_acquire_read(&cwq->wq->lockdep_map);
20023295f0efSIngo Molnar 	lock_map_acquire(&lockdep_map);
2003e36c886aSArjan van de Ven 	trace_workqueue_execute_start(work);
200465f27f38SDavid Howells 	f(work);
2005e36c886aSArjan van de Ven 	/*
2006e36c886aSArjan van de Ven 	 * While we must be careful to not use "work" after this, the trace
2007e36c886aSArjan van de Ven 	 * point will only record its address.
2008e36c886aSArjan van de Ven 	 */
2009e36c886aSArjan van de Ven 	trace_workqueue_execute_end(work);
20103295f0efSIngo Molnar 	lock_map_release(&lockdep_map);
20113295f0efSIngo Molnar 	lock_map_release(&cwq->wq->lockdep_map);
20121da177e4SLinus Torvalds 
2013d5abe669SPeter Zijlstra 	if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
2014d5abe669SPeter Zijlstra 		printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
2015d5abe669SPeter Zijlstra 		       "%s/0x%08x/%d\n",
2016a62428c0STejun Heo 		       current->comm, preempt_count(), task_pid_nr(current));
2017d5abe669SPeter Zijlstra 		printk(KERN_ERR "    last function: ");
2018d5abe669SPeter Zijlstra 		print_symbol("%s\n", (unsigned long)f);
2019d5abe669SPeter Zijlstra 		debug_show_held_locks(current);
2020d5abe669SPeter Zijlstra 		dump_stack();
2021d5abe669SPeter Zijlstra 	}
2022d5abe669SPeter Zijlstra 
20238b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
2024a62428c0STejun Heo 
2025fb0e7bebSTejun Heo 	/* clear cpu intensive status */
2026fb0e7bebSTejun Heo 	if (unlikely(cpu_intensive))
2027fb0e7bebSTejun Heo 		worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2028fb0e7bebSTejun Heo 
2029a62428c0STejun Heo 	/* we're done with it, release */
2030c8e55f36STejun Heo 	hlist_del_init(&worker->hentry);
2031c34056a3STejun Heo 	worker->current_work = NULL;
20328cca0eeaSTejun Heo 	worker->current_cwq = NULL;
20338a2e8e5dSTejun Heo 	cwq_dec_nr_in_flight(cwq, work_color, false);
20341da177e4SLinus Torvalds }
20351da177e4SLinus Torvalds 
2036affee4b2STejun Heo /**
2037affee4b2STejun Heo  * process_scheduled_works - process scheduled works
2038affee4b2STejun Heo  * @worker: self
2039affee4b2STejun Heo  *
2040affee4b2STejun Heo  * Process all scheduled works.  Please note that the scheduled list
2041affee4b2STejun Heo  * may change while processing a work, so this function repeatedly
2042affee4b2STejun Heo  * fetches a work from the top and executes it.
2043affee4b2STejun Heo  *
2044affee4b2STejun Heo  * CONTEXT:
20458b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock) which may be released and regrabbed
2046affee4b2STejun Heo  * multiple times.
2047affee4b2STejun Heo  */
2048affee4b2STejun Heo static void process_scheduled_works(struct worker *worker)
20491da177e4SLinus Torvalds {
2050affee4b2STejun Heo 	while (!list_empty(&worker->scheduled)) {
2051affee4b2STejun Heo 		struct work_struct *work = list_first_entry(&worker->scheduled,
2052a62428c0STejun Heo 						struct work_struct, entry);
2053c34056a3STejun Heo 		process_one_work(worker, work);
2054a62428c0STejun Heo 	}
20551da177e4SLinus Torvalds }
20561da177e4SLinus Torvalds 
20574690c4abSTejun Heo /**
20584690c4abSTejun Heo  * worker_thread - the worker thread function
2059c34056a3STejun Heo  * @__worker: self
20604690c4abSTejun Heo  *
2061e22bee78STejun Heo  * The gcwq worker thread function.  There's a single dynamic pool of
2062e22bee78STejun Heo  * these per each cpu.  These workers process all works regardless of
2063e22bee78STejun Heo  * their specific target workqueue.  The only exception is works which
2064e22bee78STejun Heo  * belong to workqueues with a rescuer which will be explained in
2065e22bee78STejun Heo  * rescuer_thread().
20664690c4abSTejun Heo  */
2067c34056a3STejun Heo static int worker_thread(void *__worker)
20681da177e4SLinus Torvalds {
2069c34056a3STejun Heo 	struct worker *worker = __worker;
2070bd7bdd43STejun Heo 	struct worker_pool *pool = worker->pool;
2071bd7bdd43STejun Heo 	struct global_cwq *gcwq = pool->gcwq;
20721da177e4SLinus Torvalds 
2073e22bee78STejun Heo 	/* tell the scheduler that this is a workqueue worker */
2074e22bee78STejun Heo 	worker->task->flags |= PF_WQ_WORKER;
2075c8e55f36STejun Heo woke_up:
20768b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
2077affee4b2STejun Heo 
207825511a47STejun Heo 	/*
207925511a47STejun Heo 	 * DIE can be set only while idle and REBIND set while busy has
208025511a47STejun Heo 	 * @worker->rebind_work scheduled.  Checking here is enough.
208125511a47STejun Heo 	 */
208225511a47STejun Heo 	if (unlikely(worker->flags & (WORKER_REBIND | WORKER_DIE))) {
2083c8e55f36STejun Heo 		spin_unlock_irq(&gcwq->lock);
208425511a47STejun Heo 
208525511a47STejun Heo 		if (worker->flags & WORKER_DIE) {
2086e22bee78STejun Heo 			worker->task->flags &= ~PF_WQ_WORKER;
2087c8e55f36STejun Heo 			return 0;
2088c8e55f36STejun Heo 		}
2089c8e55f36STejun Heo 
209025511a47STejun Heo 		idle_worker_rebind(worker);
209125511a47STejun Heo 		goto woke_up;
209225511a47STejun Heo 	}
209325511a47STejun Heo 
2094c8e55f36STejun Heo 	worker_leave_idle(worker);
2095db7bccf4STejun Heo recheck:
2096e22bee78STejun Heo 	/* no more worker necessary? */
209763d95a91STejun Heo 	if (!need_more_worker(pool))
2098e22bee78STejun Heo 		goto sleep;
2099e22bee78STejun Heo 
2100e22bee78STejun Heo 	/* do we need to manage? */
210163d95a91STejun Heo 	if (unlikely(!may_start_working(pool)) && manage_workers(worker))
2102e22bee78STejun Heo 		goto recheck;
2103e22bee78STejun Heo 
2104c8e55f36STejun Heo 	/*
2105c8e55f36STejun Heo 	 * ->scheduled list can only be filled while a worker is
2106c8e55f36STejun Heo 	 * preparing to process a work or actually processing it.
2107c8e55f36STejun Heo 	 * Make sure nobody diddled with it while I was sleeping.
2108c8e55f36STejun Heo 	 */
2109c8e55f36STejun Heo 	BUG_ON(!list_empty(&worker->scheduled));
2110c8e55f36STejun Heo 
2111e22bee78STejun Heo 	/*
2112e22bee78STejun Heo 	 * When control reaches this point, we're guaranteed to have
2113e22bee78STejun Heo 	 * at least one idle worker or that someone else has already
2114e22bee78STejun Heo 	 * assumed the manager role.
2115e22bee78STejun Heo 	 */
2116e22bee78STejun Heo 	worker_clr_flags(worker, WORKER_PREP);
2117e22bee78STejun Heo 
2118e22bee78STejun Heo 	do {
2119affee4b2STejun Heo 		struct work_struct *work =
2120bd7bdd43STejun Heo 			list_first_entry(&pool->worklist,
2121affee4b2STejun Heo 					 struct work_struct, entry);
2122affee4b2STejun Heo 
2123c8e55f36STejun Heo 		if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2124affee4b2STejun Heo 			/* optimization path, not strictly necessary */
2125affee4b2STejun Heo 			process_one_work(worker, work);
2126affee4b2STejun Heo 			if (unlikely(!list_empty(&worker->scheduled)))
2127affee4b2STejun Heo 				process_scheduled_works(worker);
2128affee4b2STejun Heo 		} else {
2129c8e55f36STejun Heo 			move_linked_works(work, &worker->scheduled, NULL);
2130affee4b2STejun Heo 			process_scheduled_works(worker);
2131affee4b2STejun Heo 		}
213263d95a91STejun Heo 	} while (keep_working(pool));
2133affee4b2STejun Heo 
2134e22bee78STejun Heo 	worker_set_flags(worker, WORKER_PREP, false);
2135d313dd85STejun Heo sleep:
213663d95a91STejun Heo 	if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
2137e22bee78STejun Heo 		goto recheck;
2138d313dd85STejun Heo 
2139c8e55f36STejun Heo 	/*
2140e22bee78STejun Heo 	 * gcwq->lock is held and there's no work to process and no
2141e22bee78STejun Heo 	 * need to manage, sleep.  Workers are woken up only while
2142e22bee78STejun Heo 	 * holding gcwq->lock or from local cpu, so setting the
2143e22bee78STejun Heo 	 * current state before releasing gcwq->lock is enough to
2144e22bee78STejun Heo 	 * prevent losing any event.
2145c8e55f36STejun Heo 	 */
2146c8e55f36STejun Heo 	worker_enter_idle(worker);
2147c8e55f36STejun Heo 	__set_current_state(TASK_INTERRUPTIBLE);
21488b03ae3cSTejun Heo 	spin_unlock_irq(&gcwq->lock);
21491da177e4SLinus Torvalds 	schedule();
2150c8e55f36STejun Heo 	goto woke_up;
21511da177e4SLinus Torvalds }
21521da177e4SLinus Torvalds 
2153e22bee78STejun Heo /**
2154e22bee78STejun Heo  * rescuer_thread - the rescuer thread function
2155e22bee78STejun Heo  * @__wq: the associated workqueue
2156e22bee78STejun Heo  *
2157e22bee78STejun Heo  * Workqueue rescuer thread function.  There's one rescuer for each
2158e22bee78STejun Heo  * workqueue which has WQ_RESCUER set.
2159e22bee78STejun Heo  *
2160e22bee78STejun Heo  * Regular work processing on a gcwq may block trying to create a new
2161e22bee78STejun Heo  * worker which uses GFP_KERNEL allocation which has slight chance of
2162e22bee78STejun Heo  * developing into deadlock if some works currently on the same queue
2163e22bee78STejun Heo  * need to be processed to satisfy the GFP_KERNEL allocation.  This is
2164e22bee78STejun Heo  * the problem rescuer solves.
2165e22bee78STejun Heo  *
2166e22bee78STejun Heo  * When such condition is possible, the gcwq summons rescuers of all
2167e22bee78STejun Heo  * workqueues which have works queued on the gcwq and let them process
2168e22bee78STejun Heo  * those works so that forward progress can be guaranteed.
2169e22bee78STejun Heo  *
2170e22bee78STejun Heo  * This should happen rarely.
2171e22bee78STejun Heo  */
2172e22bee78STejun Heo static int rescuer_thread(void *__wq)
2173e22bee78STejun Heo {
2174e22bee78STejun Heo 	struct workqueue_struct *wq = __wq;
2175e22bee78STejun Heo 	struct worker *rescuer = wq->rescuer;
2176e22bee78STejun Heo 	struct list_head *scheduled = &rescuer->scheduled;
2177f3421797STejun Heo 	bool is_unbound = wq->flags & WQ_UNBOUND;
2178e22bee78STejun Heo 	unsigned int cpu;
2179e22bee78STejun Heo 
2180e22bee78STejun Heo 	set_user_nice(current, RESCUER_NICE_LEVEL);
2181e22bee78STejun Heo repeat:
2182e22bee78STejun Heo 	set_current_state(TASK_INTERRUPTIBLE);
21831da177e4SLinus Torvalds 
21841da177e4SLinus Torvalds 	if (kthread_should_stop())
2185e22bee78STejun Heo 		return 0;
21861da177e4SLinus Torvalds 
2187f3421797STejun Heo 	/*
2188f3421797STejun Heo 	 * See whether any cpu is asking for help.  Unbounded
2189f3421797STejun Heo 	 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2190f3421797STejun Heo 	 */
2191f2e005aaSTejun Heo 	for_each_mayday_cpu(cpu, wq->mayday_mask) {
2192f3421797STejun Heo 		unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2193f3421797STejun Heo 		struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
2194bd7bdd43STejun Heo 		struct worker_pool *pool = cwq->pool;
2195bd7bdd43STejun Heo 		struct global_cwq *gcwq = pool->gcwq;
2196e22bee78STejun Heo 		struct work_struct *work, *n;
2197e22bee78STejun Heo 
2198e22bee78STejun Heo 		__set_current_state(TASK_RUNNING);
2199f2e005aaSTejun Heo 		mayday_clear_cpu(cpu, wq->mayday_mask);
2200e22bee78STejun Heo 
2201e22bee78STejun Heo 		/* migrate to the target cpu if possible */
2202bd7bdd43STejun Heo 		rescuer->pool = pool;
2203e22bee78STejun Heo 		worker_maybe_bind_and_lock(rescuer);
2204e22bee78STejun Heo 
2205e22bee78STejun Heo 		/*
2206e22bee78STejun Heo 		 * Slurp in all works issued via this workqueue and
2207e22bee78STejun Heo 		 * process'em.
2208e22bee78STejun Heo 		 */
2209e22bee78STejun Heo 		BUG_ON(!list_empty(&rescuer->scheduled));
2210bd7bdd43STejun Heo 		list_for_each_entry_safe(work, n, &pool->worklist, entry)
2211e22bee78STejun Heo 			if (get_work_cwq(work) == cwq)
2212e22bee78STejun Heo 				move_linked_works(work, scheduled, &n);
2213e22bee78STejun Heo 
2214e22bee78STejun Heo 		process_scheduled_works(rescuer);
22157576958aSTejun Heo 
22167576958aSTejun Heo 		/*
22177576958aSTejun Heo 		 * Leave this gcwq.  If keep_working() is %true, notify a
22187576958aSTejun Heo 		 * regular worker; otherwise, we end up with 0 concurrency
22197576958aSTejun Heo 		 * and stalling the execution.
22207576958aSTejun Heo 		 */
222163d95a91STejun Heo 		if (keep_working(pool))
222263d95a91STejun Heo 			wake_up_worker(pool);
22237576958aSTejun Heo 
2224e22bee78STejun Heo 		spin_unlock_irq(&gcwq->lock);
22251da177e4SLinus Torvalds 	}
22261da177e4SLinus Torvalds 
2227e22bee78STejun Heo 	schedule();
2228e22bee78STejun Heo 	goto repeat;
22291da177e4SLinus Torvalds }
22301da177e4SLinus Torvalds 
2231fc2e4d70SOleg Nesterov struct wq_barrier {
2232fc2e4d70SOleg Nesterov 	struct work_struct	work;
2233fc2e4d70SOleg Nesterov 	struct completion	done;
2234fc2e4d70SOleg Nesterov };
2235fc2e4d70SOleg Nesterov 
2236fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work)
2237fc2e4d70SOleg Nesterov {
2238fc2e4d70SOleg Nesterov 	struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2239fc2e4d70SOleg Nesterov 	complete(&barr->done);
2240fc2e4d70SOleg Nesterov }
2241fc2e4d70SOleg Nesterov 
22424690c4abSTejun Heo /**
22434690c4abSTejun Heo  * insert_wq_barrier - insert a barrier work
22444690c4abSTejun Heo  * @cwq: cwq to insert barrier into
22454690c4abSTejun Heo  * @barr: wq_barrier to insert
2246affee4b2STejun Heo  * @target: target work to attach @barr to
2247affee4b2STejun Heo  * @worker: worker currently executing @target, NULL if @target is not executing
22484690c4abSTejun Heo  *
2249affee4b2STejun Heo  * @barr is linked to @target such that @barr is completed only after
2250affee4b2STejun Heo  * @target finishes execution.  Please note that the ordering
2251affee4b2STejun Heo  * guarantee is observed only with respect to @target and on the local
2252affee4b2STejun Heo  * cpu.
2253affee4b2STejun Heo  *
2254affee4b2STejun Heo  * Currently, a queued barrier can't be canceled.  This is because
2255affee4b2STejun Heo  * try_to_grab_pending() can't determine whether the work to be
2256affee4b2STejun Heo  * grabbed is at the head of the queue and thus can't clear LINKED
2257affee4b2STejun Heo  * flag of the previous work while there must be a valid next work
2258affee4b2STejun Heo  * after a work with LINKED flag set.
2259affee4b2STejun Heo  *
2260affee4b2STejun Heo  * Note that when @worker is non-NULL, @target may be modified
2261affee4b2STejun Heo  * underneath us, so we can't reliably determine cwq from @target.
22624690c4abSTejun Heo  *
22634690c4abSTejun Heo  * CONTEXT:
22648b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock).
22654690c4abSTejun Heo  */
226683c22520SOleg Nesterov static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
2267affee4b2STejun Heo 			      struct wq_barrier *barr,
2268affee4b2STejun Heo 			      struct work_struct *target, struct worker *worker)
2269fc2e4d70SOleg Nesterov {
2270affee4b2STejun Heo 	struct list_head *head;
2271affee4b2STejun Heo 	unsigned int linked = 0;
2272affee4b2STejun Heo 
2273dc186ad7SThomas Gleixner 	/*
22748b03ae3cSTejun Heo 	 * debugobject calls are safe here even with gcwq->lock locked
2275dc186ad7SThomas Gleixner 	 * as we know for sure that this will not trigger any of the
2276dc186ad7SThomas Gleixner 	 * checks and call back into the fixup functions where we
2277dc186ad7SThomas Gleixner 	 * might deadlock.
2278dc186ad7SThomas Gleixner 	 */
2279ca1cab37SAndrew Morton 	INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
228022df02bbSTejun Heo 	__set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
2281fc2e4d70SOleg Nesterov 	init_completion(&barr->done);
228283c22520SOleg Nesterov 
2283affee4b2STejun Heo 	/*
2284affee4b2STejun Heo 	 * If @target is currently being executed, schedule the
2285affee4b2STejun Heo 	 * barrier to the worker; otherwise, put it after @target.
2286affee4b2STejun Heo 	 */
2287affee4b2STejun Heo 	if (worker)
2288affee4b2STejun Heo 		head = worker->scheduled.next;
2289affee4b2STejun Heo 	else {
2290affee4b2STejun Heo 		unsigned long *bits = work_data_bits(target);
2291affee4b2STejun Heo 
2292affee4b2STejun Heo 		head = target->entry.next;
2293affee4b2STejun Heo 		/* there can already be other linked works, inherit and set */
2294affee4b2STejun Heo 		linked = *bits & WORK_STRUCT_LINKED;
2295affee4b2STejun Heo 		__set_bit(WORK_STRUCT_LINKED_BIT, bits);
2296affee4b2STejun Heo 	}
2297affee4b2STejun Heo 
2298dc186ad7SThomas Gleixner 	debug_work_activate(&barr->work);
2299affee4b2STejun Heo 	insert_work(cwq, &barr->work, head,
2300affee4b2STejun Heo 		    work_color_to_flags(WORK_NO_COLOR) | linked);
2301fc2e4d70SOleg Nesterov }
2302fc2e4d70SOleg Nesterov 
230373f53c4aSTejun Heo /**
230473f53c4aSTejun Heo  * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
230573f53c4aSTejun Heo  * @wq: workqueue being flushed
230673f53c4aSTejun Heo  * @flush_color: new flush color, < 0 for no-op
230773f53c4aSTejun Heo  * @work_color: new work color, < 0 for no-op
230873f53c4aSTejun Heo  *
230973f53c4aSTejun Heo  * Prepare cwqs for workqueue flushing.
231073f53c4aSTejun Heo  *
231173f53c4aSTejun Heo  * If @flush_color is non-negative, flush_color on all cwqs should be
231273f53c4aSTejun Heo  * -1.  If no cwq has in-flight commands at the specified color, all
231373f53c4aSTejun Heo  * cwq->flush_color's stay at -1 and %false is returned.  If any cwq
231473f53c4aSTejun Heo  * has in flight commands, its cwq->flush_color is set to
231573f53c4aSTejun Heo  * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
231673f53c4aSTejun Heo  * wakeup logic is armed and %true is returned.
231773f53c4aSTejun Heo  *
231873f53c4aSTejun Heo  * The caller should have initialized @wq->first_flusher prior to
231973f53c4aSTejun Heo  * calling this function with non-negative @flush_color.  If
232073f53c4aSTejun Heo  * @flush_color is negative, no flush color update is done and %false
232173f53c4aSTejun Heo  * is returned.
232273f53c4aSTejun Heo  *
232373f53c4aSTejun Heo  * If @work_color is non-negative, all cwqs should have the same
232473f53c4aSTejun Heo  * work_color which is previous to @work_color and all will be
232573f53c4aSTejun Heo  * advanced to @work_color.
232673f53c4aSTejun Heo  *
232773f53c4aSTejun Heo  * CONTEXT:
232873f53c4aSTejun Heo  * mutex_lock(wq->flush_mutex).
232973f53c4aSTejun Heo  *
233073f53c4aSTejun Heo  * RETURNS:
233173f53c4aSTejun Heo  * %true if @flush_color >= 0 and there's something to flush.  %false
233273f53c4aSTejun Heo  * otherwise.
233373f53c4aSTejun Heo  */
233473f53c4aSTejun Heo static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
233573f53c4aSTejun Heo 				      int flush_color, int work_color)
23361da177e4SLinus Torvalds {
233773f53c4aSTejun Heo 	bool wait = false;
233873f53c4aSTejun Heo 	unsigned int cpu;
23391da177e4SLinus Torvalds 
234073f53c4aSTejun Heo 	if (flush_color >= 0) {
234173f53c4aSTejun Heo 		BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
234273f53c4aSTejun Heo 		atomic_set(&wq->nr_cwqs_to_flush, 1);
2343dc186ad7SThomas Gleixner 	}
234414441960SOleg Nesterov 
2345f3421797STejun Heo 	for_each_cwq_cpu(cpu, wq) {
234673f53c4aSTejun Heo 		struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
2347bd7bdd43STejun Heo 		struct global_cwq *gcwq = cwq->pool->gcwq;
23481da177e4SLinus Torvalds 
23498b03ae3cSTejun Heo 		spin_lock_irq(&gcwq->lock);
235073f53c4aSTejun Heo 
235173f53c4aSTejun Heo 		if (flush_color >= 0) {
235273f53c4aSTejun Heo 			BUG_ON(cwq->flush_color != -1);
235373f53c4aSTejun Heo 
235473f53c4aSTejun Heo 			if (cwq->nr_in_flight[flush_color]) {
235573f53c4aSTejun Heo 				cwq->flush_color = flush_color;
235673f53c4aSTejun Heo 				atomic_inc(&wq->nr_cwqs_to_flush);
235773f53c4aSTejun Heo 				wait = true;
23581da177e4SLinus Torvalds 			}
235973f53c4aSTejun Heo 		}
236073f53c4aSTejun Heo 
236173f53c4aSTejun Heo 		if (work_color >= 0) {
236273f53c4aSTejun Heo 			BUG_ON(work_color != work_next_color(cwq->work_color));
236373f53c4aSTejun Heo 			cwq->work_color = work_color;
236473f53c4aSTejun Heo 		}
236573f53c4aSTejun Heo 
23668b03ae3cSTejun Heo 		spin_unlock_irq(&gcwq->lock);
23671da177e4SLinus Torvalds 	}
23681da177e4SLinus Torvalds 
236973f53c4aSTejun Heo 	if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
237073f53c4aSTejun Heo 		complete(&wq->first_flusher->done);
237173f53c4aSTejun Heo 
237273f53c4aSTejun Heo 	return wait;
237383c22520SOleg Nesterov }
23741da177e4SLinus Torvalds 
23750fcb78c2SRolf Eike Beer /**
23761da177e4SLinus Torvalds  * flush_workqueue - ensure that any scheduled work has run to completion.
23770fcb78c2SRolf Eike Beer  * @wq: workqueue to flush
23781da177e4SLinus Torvalds  *
23791da177e4SLinus Torvalds  * Forces execution of the workqueue and blocks until its completion.
23801da177e4SLinus Torvalds  * This is typically used in driver shutdown handlers.
23811da177e4SLinus Torvalds  *
2382fc2e4d70SOleg Nesterov  * We sleep until all works which were queued on entry have been handled,
2383fc2e4d70SOleg Nesterov  * but we are not livelocked by new incoming ones.
23841da177e4SLinus Torvalds  */
23857ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq)
23861da177e4SLinus Torvalds {
238773f53c4aSTejun Heo 	struct wq_flusher this_flusher = {
238873f53c4aSTejun Heo 		.list = LIST_HEAD_INIT(this_flusher.list),
238973f53c4aSTejun Heo 		.flush_color = -1,
239073f53c4aSTejun Heo 		.done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
239173f53c4aSTejun Heo 	};
239273f53c4aSTejun Heo 	int next_color;
2393b1f4ec17SOleg Nesterov 
23943295f0efSIngo Molnar 	lock_map_acquire(&wq->lockdep_map);
23953295f0efSIngo Molnar 	lock_map_release(&wq->lockdep_map);
239673f53c4aSTejun Heo 
239773f53c4aSTejun Heo 	mutex_lock(&wq->flush_mutex);
239873f53c4aSTejun Heo 
239973f53c4aSTejun Heo 	/*
240073f53c4aSTejun Heo 	 * Start-to-wait phase
240173f53c4aSTejun Heo 	 */
240273f53c4aSTejun Heo 	next_color = work_next_color(wq->work_color);
240373f53c4aSTejun Heo 
240473f53c4aSTejun Heo 	if (next_color != wq->flush_color) {
240573f53c4aSTejun Heo 		/*
240673f53c4aSTejun Heo 		 * Color space is not full.  The current work_color
240773f53c4aSTejun Heo 		 * becomes our flush_color and work_color is advanced
240873f53c4aSTejun Heo 		 * by one.
240973f53c4aSTejun Heo 		 */
241073f53c4aSTejun Heo 		BUG_ON(!list_empty(&wq->flusher_overflow));
241173f53c4aSTejun Heo 		this_flusher.flush_color = wq->work_color;
241273f53c4aSTejun Heo 		wq->work_color = next_color;
241373f53c4aSTejun Heo 
241473f53c4aSTejun Heo 		if (!wq->first_flusher) {
241573f53c4aSTejun Heo 			/* no flush in progress, become the first flusher */
241673f53c4aSTejun Heo 			BUG_ON(wq->flush_color != this_flusher.flush_color);
241773f53c4aSTejun Heo 
241873f53c4aSTejun Heo 			wq->first_flusher = &this_flusher;
241973f53c4aSTejun Heo 
242073f53c4aSTejun Heo 			if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
242173f53c4aSTejun Heo 						       wq->work_color)) {
242273f53c4aSTejun Heo 				/* nothing to flush, done */
242373f53c4aSTejun Heo 				wq->flush_color = next_color;
242473f53c4aSTejun Heo 				wq->first_flusher = NULL;
242573f53c4aSTejun Heo 				goto out_unlock;
242673f53c4aSTejun Heo 			}
242773f53c4aSTejun Heo 		} else {
242873f53c4aSTejun Heo 			/* wait in queue */
242973f53c4aSTejun Heo 			BUG_ON(wq->flush_color == this_flusher.flush_color);
243073f53c4aSTejun Heo 			list_add_tail(&this_flusher.list, &wq->flusher_queue);
243173f53c4aSTejun Heo 			flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
243273f53c4aSTejun Heo 		}
243373f53c4aSTejun Heo 	} else {
243473f53c4aSTejun Heo 		/*
243573f53c4aSTejun Heo 		 * Oops, color space is full, wait on overflow queue.
243673f53c4aSTejun Heo 		 * The next flush completion will assign us
243773f53c4aSTejun Heo 		 * flush_color and transfer to flusher_queue.
243873f53c4aSTejun Heo 		 */
243973f53c4aSTejun Heo 		list_add_tail(&this_flusher.list, &wq->flusher_overflow);
244073f53c4aSTejun Heo 	}
244173f53c4aSTejun Heo 
244273f53c4aSTejun Heo 	mutex_unlock(&wq->flush_mutex);
244373f53c4aSTejun Heo 
244473f53c4aSTejun Heo 	wait_for_completion(&this_flusher.done);
244573f53c4aSTejun Heo 
244673f53c4aSTejun Heo 	/*
244773f53c4aSTejun Heo 	 * Wake-up-and-cascade phase
244873f53c4aSTejun Heo 	 *
244973f53c4aSTejun Heo 	 * First flushers are responsible for cascading flushes and
245073f53c4aSTejun Heo 	 * handling overflow.  Non-first flushers can simply return.
245173f53c4aSTejun Heo 	 */
245273f53c4aSTejun Heo 	if (wq->first_flusher != &this_flusher)
245373f53c4aSTejun Heo 		return;
245473f53c4aSTejun Heo 
245573f53c4aSTejun Heo 	mutex_lock(&wq->flush_mutex);
245673f53c4aSTejun Heo 
24574ce48b37STejun Heo 	/* we might have raced, check again with mutex held */
24584ce48b37STejun Heo 	if (wq->first_flusher != &this_flusher)
24594ce48b37STejun Heo 		goto out_unlock;
24604ce48b37STejun Heo 
246173f53c4aSTejun Heo 	wq->first_flusher = NULL;
246273f53c4aSTejun Heo 
246373f53c4aSTejun Heo 	BUG_ON(!list_empty(&this_flusher.list));
246473f53c4aSTejun Heo 	BUG_ON(wq->flush_color != this_flusher.flush_color);
246573f53c4aSTejun Heo 
246673f53c4aSTejun Heo 	while (true) {
246773f53c4aSTejun Heo 		struct wq_flusher *next, *tmp;
246873f53c4aSTejun Heo 
246973f53c4aSTejun Heo 		/* complete all the flushers sharing the current flush color */
247073f53c4aSTejun Heo 		list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
247173f53c4aSTejun Heo 			if (next->flush_color != wq->flush_color)
247273f53c4aSTejun Heo 				break;
247373f53c4aSTejun Heo 			list_del_init(&next->list);
247473f53c4aSTejun Heo 			complete(&next->done);
247573f53c4aSTejun Heo 		}
247673f53c4aSTejun Heo 
247773f53c4aSTejun Heo 		BUG_ON(!list_empty(&wq->flusher_overflow) &&
247873f53c4aSTejun Heo 		       wq->flush_color != work_next_color(wq->work_color));
247973f53c4aSTejun Heo 
248073f53c4aSTejun Heo 		/* this flush_color is finished, advance by one */
248173f53c4aSTejun Heo 		wq->flush_color = work_next_color(wq->flush_color);
248273f53c4aSTejun Heo 
248373f53c4aSTejun Heo 		/* one color has been freed, handle overflow queue */
248473f53c4aSTejun Heo 		if (!list_empty(&wq->flusher_overflow)) {
248573f53c4aSTejun Heo 			/*
248673f53c4aSTejun Heo 			 * Assign the same color to all overflowed
248773f53c4aSTejun Heo 			 * flushers, advance work_color and append to
248873f53c4aSTejun Heo 			 * flusher_queue.  This is the start-to-wait
248973f53c4aSTejun Heo 			 * phase for these overflowed flushers.
249073f53c4aSTejun Heo 			 */
249173f53c4aSTejun Heo 			list_for_each_entry(tmp, &wq->flusher_overflow, list)
249273f53c4aSTejun Heo 				tmp->flush_color = wq->work_color;
249373f53c4aSTejun Heo 
249473f53c4aSTejun Heo 			wq->work_color = work_next_color(wq->work_color);
249573f53c4aSTejun Heo 
249673f53c4aSTejun Heo 			list_splice_tail_init(&wq->flusher_overflow,
249773f53c4aSTejun Heo 					      &wq->flusher_queue);
249873f53c4aSTejun Heo 			flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
249973f53c4aSTejun Heo 		}
250073f53c4aSTejun Heo 
250173f53c4aSTejun Heo 		if (list_empty(&wq->flusher_queue)) {
250273f53c4aSTejun Heo 			BUG_ON(wq->flush_color != wq->work_color);
250373f53c4aSTejun Heo 			break;
250473f53c4aSTejun Heo 		}
250573f53c4aSTejun Heo 
250673f53c4aSTejun Heo 		/*
250773f53c4aSTejun Heo 		 * Need to flush more colors.  Make the next flusher
250873f53c4aSTejun Heo 		 * the new first flusher and arm cwqs.
250973f53c4aSTejun Heo 		 */
251073f53c4aSTejun Heo 		BUG_ON(wq->flush_color == wq->work_color);
251173f53c4aSTejun Heo 		BUG_ON(wq->flush_color != next->flush_color);
251273f53c4aSTejun Heo 
251373f53c4aSTejun Heo 		list_del_init(&next->list);
251473f53c4aSTejun Heo 		wq->first_flusher = next;
251573f53c4aSTejun Heo 
251673f53c4aSTejun Heo 		if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
251773f53c4aSTejun Heo 			break;
251873f53c4aSTejun Heo 
251973f53c4aSTejun Heo 		/*
252073f53c4aSTejun Heo 		 * Meh... this color is already done, clear first
252173f53c4aSTejun Heo 		 * flusher and repeat cascading.
252273f53c4aSTejun Heo 		 */
252373f53c4aSTejun Heo 		wq->first_flusher = NULL;
252473f53c4aSTejun Heo 	}
252573f53c4aSTejun Heo 
252673f53c4aSTejun Heo out_unlock:
252773f53c4aSTejun Heo 	mutex_unlock(&wq->flush_mutex);
25281da177e4SLinus Torvalds }
2529ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(flush_workqueue);
25301da177e4SLinus Torvalds 
25319c5a2ba7STejun Heo /**
25329c5a2ba7STejun Heo  * drain_workqueue - drain a workqueue
25339c5a2ba7STejun Heo  * @wq: workqueue to drain
25349c5a2ba7STejun Heo  *
25359c5a2ba7STejun Heo  * Wait until the workqueue becomes empty.  While draining is in progress,
25369c5a2ba7STejun Heo  * only chain queueing is allowed.  IOW, only currently pending or running
25379c5a2ba7STejun Heo  * work items on @wq can queue further work items on it.  @wq is flushed
25389c5a2ba7STejun Heo  * repeatedly until it becomes empty.  The number of flushing is detemined
25399c5a2ba7STejun Heo  * by the depth of chaining and should be relatively short.  Whine if it
25409c5a2ba7STejun Heo  * takes too long.
25419c5a2ba7STejun Heo  */
25429c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq)
25439c5a2ba7STejun Heo {
25449c5a2ba7STejun Heo 	unsigned int flush_cnt = 0;
25459c5a2ba7STejun Heo 	unsigned int cpu;
25469c5a2ba7STejun Heo 
25479c5a2ba7STejun Heo 	/*
25489c5a2ba7STejun Heo 	 * __queue_work() needs to test whether there are drainers, is much
25499c5a2ba7STejun Heo 	 * hotter than drain_workqueue() and already looks at @wq->flags.
25509c5a2ba7STejun Heo 	 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
25519c5a2ba7STejun Heo 	 */
25529c5a2ba7STejun Heo 	spin_lock(&workqueue_lock);
25539c5a2ba7STejun Heo 	if (!wq->nr_drainers++)
25549c5a2ba7STejun Heo 		wq->flags |= WQ_DRAINING;
25559c5a2ba7STejun Heo 	spin_unlock(&workqueue_lock);
25569c5a2ba7STejun Heo reflush:
25579c5a2ba7STejun Heo 	flush_workqueue(wq);
25589c5a2ba7STejun Heo 
25599c5a2ba7STejun Heo 	for_each_cwq_cpu(cpu, wq) {
25609c5a2ba7STejun Heo 		struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
2561fa2563e4SThomas Tuttle 		bool drained;
25629c5a2ba7STejun Heo 
2563bd7bdd43STejun Heo 		spin_lock_irq(&cwq->pool->gcwq->lock);
2564fa2563e4SThomas Tuttle 		drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
2565bd7bdd43STejun Heo 		spin_unlock_irq(&cwq->pool->gcwq->lock);
2566fa2563e4SThomas Tuttle 
2567fa2563e4SThomas Tuttle 		if (drained)
25689c5a2ba7STejun Heo 			continue;
25699c5a2ba7STejun Heo 
25709c5a2ba7STejun Heo 		if (++flush_cnt == 10 ||
25719c5a2ba7STejun Heo 		    (flush_cnt % 100 == 0 && flush_cnt <= 1000))
25729c5a2ba7STejun Heo 			pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n",
25739c5a2ba7STejun Heo 				   wq->name, flush_cnt);
25749c5a2ba7STejun Heo 		goto reflush;
25759c5a2ba7STejun Heo 	}
25769c5a2ba7STejun Heo 
25779c5a2ba7STejun Heo 	spin_lock(&workqueue_lock);
25789c5a2ba7STejun Heo 	if (!--wq->nr_drainers)
25799c5a2ba7STejun Heo 		wq->flags &= ~WQ_DRAINING;
25809c5a2ba7STejun Heo 	spin_unlock(&workqueue_lock);
25819c5a2ba7STejun Heo }
25829c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue);
25839c5a2ba7STejun Heo 
2584baf59022STejun Heo static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
2585baf59022STejun Heo 			     bool wait_executing)
2586baf59022STejun Heo {
2587baf59022STejun Heo 	struct worker *worker = NULL;
2588baf59022STejun Heo 	struct global_cwq *gcwq;
2589baf59022STejun Heo 	struct cpu_workqueue_struct *cwq;
2590baf59022STejun Heo 
2591baf59022STejun Heo 	might_sleep();
2592baf59022STejun Heo 	gcwq = get_work_gcwq(work);
2593baf59022STejun Heo 	if (!gcwq)
2594baf59022STejun Heo 		return false;
2595baf59022STejun Heo 
2596baf59022STejun Heo 	spin_lock_irq(&gcwq->lock);
2597baf59022STejun Heo 	if (!list_empty(&work->entry)) {
2598baf59022STejun Heo 		/*
2599baf59022STejun Heo 		 * See the comment near try_to_grab_pending()->smp_rmb().
2600baf59022STejun Heo 		 * If it was re-queued to a different gcwq under us, we
2601baf59022STejun Heo 		 * are not going to wait.
2602baf59022STejun Heo 		 */
2603baf59022STejun Heo 		smp_rmb();
2604baf59022STejun Heo 		cwq = get_work_cwq(work);
2605bd7bdd43STejun Heo 		if (unlikely(!cwq || gcwq != cwq->pool->gcwq))
2606baf59022STejun Heo 			goto already_gone;
2607baf59022STejun Heo 	} else if (wait_executing) {
2608baf59022STejun Heo 		worker = find_worker_executing_work(gcwq, work);
2609baf59022STejun Heo 		if (!worker)
2610baf59022STejun Heo 			goto already_gone;
2611baf59022STejun Heo 		cwq = worker->current_cwq;
2612baf59022STejun Heo 	} else
2613baf59022STejun Heo 		goto already_gone;
2614baf59022STejun Heo 
2615baf59022STejun Heo 	insert_wq_barrier(cwq, barr, work, worker);
2616baf59022STejun Heo 	spin_unlock_irq(&gcwq->lock);
2617baf59022STejun Heo 
2618e159489bSTejun Heo 	/*
2619e159489bSTejun Heo 	 * If @max_active is 1 or rescuer is in use, flushing another work
2620e159489bSTejun Heo 	 * item on the same workqueue may lead to deadlock.  Make sure the
2621e159489bSTejun Heo 	 * flusher is not running on the same workqueue by verifying write
2622e159489bSTejun Heo 	 * access.
2623e159489bSTejun Heo 	 */
2624e159489bSTejun Heo 	if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2625baf59022STejun Heo 		lock_map_acquire(&cwq->wq->lockdep_map);
2626e159489bSTejun Heo 	else
2627e159489bSTejun Heo 		lock_map_acquire_read(&cwq->wq->lockdep_map);
2628baf59022STejun Heo 	lock_map_release(&cwq->wq->lockdep_map);
2629e159489bSTejun Heo 
2630baf59022STejun Heo 	return true;
2631baf59022STejun Heo already_gone:
2632baf59022STejun Heo 	spin_unlock_irq(&gcwq->lock);
2633baf59022STejun Heo 	return false;
2634baf59022STejun Heo }
2635baf59022STejun Heo 
2636db700897SOleg Nesterov /**
2637401a8d04STejun Heo  * flush_work - wait for a work to finish executing the last queueing instance
2638401a8d04STejun Heo  * @work: the work to flush
2639db700897SOleg Nesterov  *
2640401a8d04STejun Heo  * Wait until @work has finished execution.  This function considers
2641401a8d04STejun Heo  * only the last queueing instance of @work.  If @work has been
2642401a8d04STejun Heo  * enqueued across different CPUs on a non-reentrant workqueue or on
2643401a8d04STejun Heo  * multiple workqueues, @work might still be executing on return on
2644401a8d04STejun Heo  * some of the CPUs from earlier queueing.
2645a67da70dSOleg Nesterov  *
2646401a8d04STejun Heo  * If @work was queued only on a non-reentrant, ordered or unbound
2647401a8d04STejun Heo  * workqueue, @work is guaranteed to be idle on return if it hasn't
2648401a8d04STejun Heo  * been requeued since flush started.
2649401a8d04STejun Heo  *
2650401a8d04STejun Heo  * RETURNS:
2651401a8d04STejun Heo  * %true if flush_work() waited for the work to finish execution,
2652401a8d04STejun Heo  * %false if it was already idle.
2653db700897SOleg Nesterov  */
2654401a8d04STejun Heo bool flush_work(struct work_struct *work)
2655db700897SOleg Nesterov {
2656db700897SOleg Nesterov 	struct wq_barrier barr;
2657db700897SOleg Nesterov 
26580976dfc1SStephen Boyd 	lock_map_acquire(&work->lockdep_map);
26590976dfc1SStephen Boyd 	lock_map_release(&work->lockdep_map);
26600976dfc1SStephen Boyd 
2661baf59022STejun Heo 	if (start_flush_work(work, &barr, true)) {
2662db700897SOleg Nesterov 		wait_for_completion(&barr.done);
2663dc186ad7SThomas Gleixner 		destroy_work_on_stack(&barr.work);
2664401a8d04STejun Heo 		return true;
2665baf59022STejun Heo 	} else
2666401a8d04STejun Heo 		return false;
2667db700897SOleg Nesterov }
2668db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work);
2669db700897SOleg Nesterov 
2670401a8d04STejun Heo static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
2671401a8d04STejun Heo {
2672401a8d04STejun Heo 	struct wq_barrier barr;
2673401a8d04STejun Heo 	struct worker *worker;
2674401a8d04STejun Heo 
2675401a8d04STejun Heo 	spin_lock_irq(&gcwq->lock);
2676401a8d04STejun Heo 
2677401a8d04STejun Heo 	worker = find_worker_executing_work(gcwq, work);
2678401a8d04STejun Heo 	if (unlikely(worker))
2679401a8d04STejun Heo 		insert_wq_barrier(worker->current_cwq, &barr, work, worker);
2680401a8d04STejun Heo 
2681401a8d04STejun Heo 	spin_unlock_irq(&gcwq->lock);
2682401a8d04STejun Heo 
2683401a8d04STejun Heo 	if (unlikely(worker)) {
2684401a8d04STejun Heo 		wait_for_completion(&barr.done);
2685401a8d04STejun Heo 		destroy_work_on_stack(&barr.work);
2686401a8d04STejun Heo 		return true;
2687401a8d04STejun Heo 	} else
2688401a8d04STejun Heo 		return false;
2689401a8d04STejun Heo }
2690401a8d04STejun Heo 
2691401a8d04STejun Heo static bool wait_on_work(struct work_struct *work)
2692401a8d04STejun Heo {
2693401a8d04STejun Heo 	bool ret = false;
2694401a8d04STejun Heo 	int cpu;
2695401a8d04STejun Heo 
2696401a8d04STejun Heo 	might_sleep();
2697401a8d04STejun Heo 
2698401a8d04STejun Heo 	lock_map_acquire(&work->lockdep_map);
2699401a8d04STejun Heo 	lock_map_release(&work->lockdep_map);
2700401a8d04STejun Heo 
2701401a8d04STejun Heo 	for_each_gcwq_cpu(cpu)
2702401a8d04STejun Heo 		ret |= wait_on_cpu_work(get_gcwq(cpu), work);
2703401a8d04STejun Heo 	return ret;
2704401a8d04STejun Heo }
2705401a8d04STejun Heo 
270609383498STejun Heo /**
270709383498STejun Heo  * flush_work_sync - wait until a work has finished execution
270809383498STejun Heo  * @work: the work to flush
270909383498STejun Heo  *
271009383498STejun Heo  * Wait until @work has finished execution.  On return, it's
271109383498STejun Heo  * guaranteed that all queueing instances of @work which happened
271209383498STejun Heo  * before this function is called are finished.  In other words, if
271309383498STejun Heo  * @work hasn't been requeued since this function was called, @work is
271409383498STejun Heo  * guaranteed to be idle on return.
271509383498STejun Heo  *
271609383498STejun Heo  * RETURNS:
271709383498STejun Heo  * %true if flush_work_sync() waited for the work to finish execution,
271809383498STejun Heo  * %false if it was already idle.
271909383498STejun Heo  */
272009383498STejun Heo bool flush_work_sync(struct work_struct *work)
272109383498STejun Heo {
272209383498STejun Heo 	struct wq_barrier barr;
272309383498STejun Heo 	bool pending, waited;
272409383498STejun Heo 
272509383498STejun Heo 	/* we'll wait for executions separately, queue barr only if pending */
272609383498STejun Heo 	pending = start_flush_work(work, &barr, false);
272709383498STejun Heo 
272809383498STejun Heo 	/* wait for executions to finish */
272909383498STejun Heo 	waited = wait_on_work(work);
273009383498STejun Heo 
273109383498STejun Heo 	/* wait for the pending one */
273209383498STejun Heo 	if (pending) {
273309383498STejun Heo 		wait_for_completion(&barr.done);
273409383498STejun Heo 		destroy_work_on_stack(&barr.work);
273509383498STejun Heo 	}
273609383498STejun Heo 
273709383498STejun Heo 	return pending || waited;
273809383498STejun Heo }
273909383498STejun Heo EXPORT_SYMBOL_GPL(flush_work_sync);
274009383498STejun Heo 
27416e84d644SOleg Nesterov /*
27421f1f642eSOleg Nesterov  * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
27436e84d644SOleg Nesterov  * so this work can't be re-armed in any way.
27446e84d644SOleg Nesterov  */
27456e84d644SOleg Nesterov static int try_to_grab_pending(struct work_struct *work)
27466e84d644SOleg Nesterov {
27478b03ae3cSTejun Heo 	struct global_cwq *gcwq;
27481f1f642eSOleg Nesterov 	int ret = -1;
27496e84d644SOleg Nesterov 
275022df02bbSTejun Heo 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
27511f1f642eSOleg Nesterov 		return 0;
27526e84d644SOleg Nesterov 
27536e84d644SOleg Nesterov 	/*
27546e84d644SOleg Nesterov 	 * The queueing is in progress, or it is already queued. Try to
27556e84d644SOleg Nesterov 	 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
27566e84d644SOleg Nesterov 	 */
27577a22ad75STejun Heo 	gcwq = get_work_gcwq(work);
27587a22ad75STejun Heo 	if (!gcwq)
27596e84d644SOleg Nesterov 		return ret;
27606e84d644SOleg Nesterov 
27618b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
27626e84d644SOleg Nesterov 	if (!list_empty(&work->entry)) {
27636e84d644SOleg Nesterov 		/*
27647a22ad75STejun Heo 		 * This work is queued, but perhaps we locked the wrong gcwq.
27656e84d644SOleg Nesterov 		 * In that case we must see the new value after rmb(), see
27666e84d644SOleg Nesterov 		 * insert_work()->wmb().
27676e84d644SOleg Nesterov 		 */
27686e84d644SOleg Nesterov 		smp_rmb();
27697a22ad75STejun Heo 		if (gcwq == get_work_gcwq(work)) {
2770dc186ad7SThomas Gleixner 			debug_work_deactivate(work);
27716e84d644SOleg Nesterov 			list_del_init(&work->entry);
27727a22ad75STejun Heo 			cwq_dec_nr_in_flight(get_work_cwq(work),
27738a2e8e5dSTejun Heo 				get_work_color(work),
27748a2e8e5dSTejun Heo 				*work_data_bits(work) & WORK_STRUCT_DELAYED);
27756e84d644SOleg Nesterov 			ret = 1;
27766e84d644SOleg Nesterov 		}
27776e84d644SOleg Nesterov 	}
27788b03ae3cSTejun Heo 	spin_unlock_irq(&gcwq->lock);
27796e84d644SOleg Nesterov 
27806e84d644SOleg Nesterov 	return ret;
27816e84d644SOleg Nesterov }
27826e84d644SOleg Nesterov 
2783401a8d04STejun Heo static bool __cancel_work_timer(struct work_struct *work,
27841f1f642eSOleg Nesterov 				struct timer_list* timer)
27851f1f642eSOleg Nesterov {
27861f1f642eSOleg Nesterov 	int ret;
27871f1f642eSOleg Nesterov 
27881f1f642eSOleg Nesterov 	do {
27891f1f642eSOleg Nesterov 		ret = (timer && likely(del_timer(timer)));
27901f1f642eSOleg Nesterov 		if (!ret)
27911f1f642eSOleg Nesterov 			ret = try_to_grab_pending(work);
27921f1f642eSOleg Nesterov 		wait_on_work(work);
27931f1f642eSOleg Nesterov 	} while (unlikely(ret < 0));
27941f1f642eSOleg Nesterov 
27957a22ad75STejun Heo 	clear_work_data(work);
27961f1f642eSOleg Nesterov 	return ret;
27971f1f642eSOleg Nesterov }
27981f1f642eSOleg Nesterov 
27996e84d644SOleg Nesterov /**
2800401a8d04STejun Heo  * cancel_work_sync - cancel a work and wait for it to finish
2801401a8d04STejun Heo  * @work: the work to cancel
28026e84d644SOleg Nesterov  *
2803401a8d04STejun Heo  * Cancel @work and wait for its execution to finish.  This function
2804401a8d04STejun Heo  * can be used even if the work re-queues itself or migrates to
2805401a8d04STejun Heo  * another workqueue.  On return from this function, @work is
2806401a8d04STejun Heo  * guaranteed to be not pending or executing on any CPU.
28071f1f642eSOleg Nesterov  *
2808401a8d04STejun Heo  * cancel_work_sync(&delayed_work->work) must not be used for
2809401a8d04STejun Heo  * delayed_work's.  Use cancel_delayed_work_sync() instead.
28106e84d644SOleg Nesterov  *
2811401a8d04STejun Heo  * The caller must ensure that the workqueue on which @work was last
28126e84d644SOleg Nesterov  * queued can't be destroyed before this function returns.
2813401a8d04STejun Heo  *
2814401a8d04STejun Heo  * RETURNS:
2815401a8d04STejun Heo  * %true if @work was pending, %false otherwise.
28166e84d644SOleg Nesterov  */
2817401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work)
28186e84d644SOleg Nesterov {
28191f1f642eSOleg Nesterov 	return __cancel_work_timer(work, NULL);
2820b89deed3SOleg Nesterov }
282128e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync);
2822b89deed3SOleg Nesterov 
28236e84d644SOleg Nesterov /**
2824401a8d04STejun Heo  * flush_delayed_work - wait for a dwork to finish executing the last queueing
2825401a8d04STejun Heo  * @dwork: the delayed work to flush
28266e84d644SOleg Nesterov  *
2827401a8d04STejun Heo  * Delayed timer is cancelled and the pending work is queued for
2828401a8d04STejun Heo  * immediate execution.  Like flush_work(), this function only
2829401a8d04STejun Heo  * considers the last queueing instance of @dwork.
28301f1f642eSOleg Nesterov  *
2831401a8d04STejun Heo  * RETURNS:
2832401a8d04STejun Heo  * %true if flush_work() waited for the work to finish execution,
2833401a8d04STejun Heo  * %false if it was already idle.
28346e84d644SOleg Nesterov  */
2835401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork)
2836401a8d04STejun Heo {
2837401a8d04STejun Heo 	if (del_timer_sync(&dwork->timer))
2838401a8d04STejun Heo 		__queue_work(raw_smp_processor_id(),
2839401a8d04STejun Heo 			     get_work_cwq(&dwork->work)->wq, &dwork->work);
2840401a8d04STejun Heo 	return flush_work(&dwork->work);
2841401a8d04STejun Heo }
2842401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work);
2843401a8d04STejun Heo 
2844401a8d04STejun Heo /**
284509383498STejun Heo  * flush_delayed_work_sync - wait for a dwork to finish
284609383498STejun Heo  * @dwork: the delayed work to flush
284709383498STejun Heo  *
284809383498STejun Heo  * Delayed timer is cancelled and the pending work is queued for
284909383498STejun Heo  * execution immediately.  Other than timer handling, its behavior
285009383498STejun Heo  * is identical to flush_work_sync().
285109383498STejun Heo  *
285209383498STejun Heo  * RETURNS:
285309383498STejun Heo  * %true if flush_work_sync() waited for the work to finish execution,
285409383498STejun Heo  * %false if it was already idle.
285509383498STejun Heo  */
285609383498STejun Heo bool flush_delayed_work_sync(struct delayed_work *dwork)
285709383498STejun Heo {
285809383498STejun Heo 	if (del_timer_sync(&dwork->timer))
285909383498STejun Heo 		__queue_work(raw_smp_processor_id(),
286009383498STejun Heo 			     get_work_cwq(&dwork->work)->wq, &dwork->work);
286109383498STejun Heo 	return flush_work_sync(&dwork->work);
286209383498STejun Heo }
286309383498STejun Heo EXPORT_SYMBOL(flush_delayed_work_sync);
286409383498STejun Heo 
286509383498STejun Heo /**
2866401a8d04STejun Heo  * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2867401a8d04STejun Heo  * @dwork: the delayed work cancel
2868401a8d04STejun Heo  *
2869401a8d04STejun Heo  * This is cancel_work_sync() for delayed works.
2870401a8d04STejun Heo  *
2871401a8d04STejun Heo  * RETURNS:
2872401a8d04STejun Heo  * %true if @dwork was pending, %false otherwise.
2873401a8d04STejun Heo  */
2874401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork)
28756e84d644SOleg Nesterov {
28761f1f642eSOleg Nesterov 	return __cancel_work_timer(&dwork->work, &dwork->timer);
28776e84d644SOleg Nesterov }
2878f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync);
28791da177e4SLinus Torvalds 
2880d4283e93STejun Heo /**
28810a13c00eSTejun Heo  * schedule_work_on - put work task on a specific cpu
28820a13c00eSTejun Heo  * @cpu: cpu to put the work task on
28830a13c00eSTejun Heo  * @work: job to be done
28840a13c00eSTejun Heo  *
28850a13c00eSTejun Heo  * This puts a job on a specific cpu
28860a13c00eSTejun Heo  */
2887d4283e93STejun Heo bool schedule_work_on(int cpu, struct work_struct *work)
28880a13c00eSTejun Heo {
28890a13c00eSTejun Heo 	return queue_work_on(cpu, system_wq, work);
28900a13c00eSTejun Heo }
28910a13c00eSTejun Heo EXPORT_SYMBOL(schedule_work_on);
28920a13c00eSTejun Heo 
28930fcb78c2SRolf Eike Beer /**
28940fcb78c2SRolf Eike Beer  * schedule_work - put work task in global workqueue
28950fcb78c2SRolf Eike Beer  * @work: job to be done
28960fcb78c2SRolf Eike Beer  *
2897d4283e93STejun Heo  * Returns %false if @work was already on the kernel-global workqueue and
2898d4283e93STejun Heo  * %true otherwise.
28995b0f437dSBart Van Assche  *
29005b0f437dSBart Van Assche  * This puts a job in the kernel-global workqueue if it was not already
29015b0f437dSBart Van Assche  * queued and leaves it in the same position on the kernel-global
29025b0f437dSBart Van Assche  * workqueue otherwise.
29030fcb78c2SRolf Eike Beer  */
2904d4283e93STejun Heo bool schedule_work(struct work_struct *work)
29051da177e4SLinus Torvalds {
2906d320c038STejun Heo 	return queue_work(system_wq, work);
29071da177e4SLinus Torvalds }
2908ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_work);
29091da177e4SLinus Torvalds 
29100fcb78c2SRolf Eike Beer /**
29110fcb78c2SRolf Eike Beer  * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
29120fcb78c2SRolf Eike Beer  * @cpu: cpu to use
291352bad64dSDavid Howells  * @dwork: job to be done
29140fcb78c2SRolf Eike Beer  * @delay: number of jiffies to wait
29150fcb78c2SRolf Eike Beer  *
29160fcb78c2SRolf Eike Beer  * After waiting for a given time this puts a job in the kernel-global
29170fcb78c2SRolf Eike Beer  * workqueue on the specified CPU.
29180fcb78c2SRolf Eike Beer  */
2919d4283e93STejun Heo bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
2920d4283e93STejun Heo 			      unsigned long delay)
29211da177e4SLinus Torvalds {
2922d320c038STejun Heo 	return queue_delayed_work_on(cpu, system_wq, dwork, delay);
29231da177e4SLinus Torvalds }
2924ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work_on);
29251da177e4SLinus Torvalds 
2926b6136773SAndrew Morton /**
29270a13c00eSTejun Heo  * schedule_delayed_work - put work task in global workqueue after delay
29280a13c00eSTejun Heo  * @dwork: job to be done
29290a13c00eSTejun Heo  * @delay: number of jiffies to wait or 0 for immediate execution
29300a13c00eSTejun Heo  *
29310a13c00eSTejun Heo  * After waiting for a given time this puts a job in the kernel-global
29320a13c00eSTejun Heo  * workqueue.
29330a13c00eSTejun Heo  */
2934d4283e93STejun Heo bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay)
29350a13c00eSTejun Heo {
29360a13c00eSTejun Heo 	return queue_delayed_work(system_wq, dwork, delay);
29370a13c00eSTejun Heo }
29380a13c00eSTejun Heo EXPORT_SYMBOL(schedule_delayed_work);
29390a13c00eSTejun Heo 
29400a13c00eSTejun Heo /**
294131ddd871STejun Heo  * schedule_on_each_cpu - execute a function synchronously on each online CPU
2942b6136773SAndrew Morton  * @func: the function to call
2943b6136773SAndrew Morton  *
294431ddd871STejun Heo  * schedule_on_each_cpu() executes @func on each online CPU using the
294531ddd871STejun Heo  * system workqueue and blocks until all CPUs have completed.
2946b6136773SAndrew Morton  * schedule_on_each_cpu() is very slow.
294731ddd871STejun Heo  *
294831ddd871STejun Heo  * RETURNS:
294931ddd871STejun Heo  * 0 on success, -errno on failure.
2950b6136773SAndrew Morton  */
295165f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func)
295215316ba8SChristoph Lameter {
295315316ba8SChristoph Lameter 	int cpu;
295438f51568SNamhyung Kim 	struct work_struct __percpu *works;
295515316ba8SChristoph Lameter 
2956b6136773SAndrew Morton 	works = alloc_percpu(struct work_struct);
2957b6136773SAndrew Morton 	if (!works)
295815316ba8SChristoph Lameter 		return -ENOMEM;
2959b6136773SAndrew Morton 
296095402b38SGautham R Shenoy 	get_online_cpus();
296193981800STejun Heo 
296215316ba8SChristoph Lameter 	for_each_online_cpu(cpu) {
29639bfb1839SIngo Molnar 		struct work_struct *work = per_cpu_ptr(works, cpu);
29649bfb1839SIngo Molnar 
29659bfb1839SIngo Molnar 		INIT_WORK(work, func);
29668de6d308SOleg Nesterov 		schedule_work_on(cpu, work);
296715316ba8SChristoph Lameter 	}
296893981800STejun Heo 
296993981800STejun Heo 	for_each_online_cpu(cpu)
29708616a89aSOleg Nesterov 		flush_work(per_cpu_ptr(works, cpu));
297193981800STejun Heo 
297295402b38SGautham R Shenoy 	put_online_cpus();
2973b6136773SAndrew Morton 	free_percpu(works);
297415316ba8SChristoph Lameter 	return 0;
297515316ba8SChristoph Lameter }
297615316ba8SChristoph Lameter 
2977eef6a7d5SAlan Stern /**
2978eef6a7d5SAlan Stern  * flush_scheduled_work - ensure that any scheduled work has run to completion.
2979eef6a7d5SAlan Stern  *
2980eef6a7d5SAlan Stern  * Forces execution of the kernel-global workqueue and blocks until its
2981eef6a7d5SAlan Stern  * completion.
2982eef6a7d5SAlan Stern  *
2983eef6a7d5SAlan Stern  * Think twice before calling this function!  It's very easy to get into
2984eef6a7d5SAlan Stern  * trouble if you don't take great care.  Either of the following situations
2985eef6a7d5SAlan Stern  * will lead to deadlock:
2986eef6a7d5SAlan Stern  *
2987eef6a7d5SAlan Stern  *	One of the work items currently on the workqueue needs to acquire
2988eef6a7d5SAlan Stern  *	a lock held by your code or its caller.
2989eef6a7d5SAlan Stern  *
2990eef6a7d5SAlan Stern  *	Your code is running in the context of a work routine.
2991eef6a7d5SAlan Stern  *
2992eef6a7d5SAlan Stern  * They will be detected by lockdep when they occur, but the first might not
2993eef6a7d5SAlan Stern  * occur very often.  It depends on what work items are on the workqueue and
2994eef6a7d5SAlan Stern  * what locks they need, which you have no control over.
2995eef6a7d5SAlan Stern  *
2996eef6a7d5SAlan Stern  * In most situations flushing the entire workqueue is overkill; you merely
2997eef6a7d5SAlan Stern  * need to know that a particular work item isn't queued and isn't running.
2998eef6a7d5SAlan Stern  * In such cases you should use cancel_delayed_work_sync() or
2999eef6a7d5SAlan Stern  * cancel_work_sync() instead.
3000eef6a7d5SAlan Stern  */
30011da177e4SLinus Torvalds void flush_scheduled_work(void)
30021da177e4SLinus Torvalds {
3003d320c038STejun Heo 	flush_workqueue(system_wq);
30041da177e4SLinus Torvalds }
3005ae90dd5dSDave Jones EXPORT_SYMBOL(flush_scheduled_work);
30061da177e4SLinus Torvalds 
30071da177e4SLinus Torvalds /**
30081fa44ecaSJames Bottomley  * execute_in_process_context - reliably execute the routine with user context
30091fa44ecaSJames Bottomley  * @fn:		the function to execute
30101fa44ecaSJames Bottomley  * @ew:		guaranteed storage for the execute work structure (must
30111fa44ecaSJames Bottomley  *		be available when the work executes)
30121fa44ecaSJames Bottomley  *
30131fa44ecaSJames Bottomley  * Executes the function immediately if process context is available,
30141fa44ecaSJames Bottomley  * otherwise schedules the function for delayed execution.
30151fa44ecaSJames Bottomley  *
30161fa44ecaSJames Bottomley  * Returns:	0 - function was executed
30171fa44ecaSJames Bottomley  *		1 - function was scheduled for execution
30181fa44ecaSJames Bottomley  */
301965f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew)
30201fa44ecaSJames Bottomley {
30211fa44ecaSJames Bottomley 	if (!in_interrupt()) {
302265f27f38SDavid Howells 		fn(&ew->work);
30231fa44ecaSJames Bottomley 		return 0;
30241fa44ecaSJames Bottomley 	}
30251fa44ecaSJames Bottomley 
302665f27f38SDavid Howells 	INIT_WORK(&ew->work, fn);
30271fa44ecaSJames Bottomley 	schedule_work(&ew->work);
30281fa44ecaSJames Bottomley 
30291fa44ecaSJames Bottomley 	return 1;
30301fa44ecaSJames Bottomley }
30311fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context);
30321fa44ecaSJames Bottomley 
30331da177e4SLinus Torvalds int keventd_up(void)
30341da177e4SLinus Torvalds {
3035d320c038STejun Heo 	return system_wq != NULL;
30361da177e4SLinus Torvalds }
30371da177e4SLinus Torvalds 
3038bdbc5dd7STejun Heo static int alloc_cwqs(struct workqueue_struct *wq)
30391da177e4SLinus Torvalds {
30403af24433SOleg Nesterov 	/*
30410f900049STejun Heo 	 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
30420f900049STejun Heo 	 * Make sure that the alignment isn't lower than that of
30430f900049STejun Heo 	 * unsigned long long.
30443af24433SOleg Nesterov 	 */
30450f900049STejun Heo 	const size_t size = sizeof(struct cpu_workqueue_struct);
30460f900049STejun Heo 	const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
30470f900049STejun Heo 				   __alignof__(unsigned long long));
30483af24433SOleg Nesterov 
3049e06ffa1eSLai Jiangshan 	if (!(wq->flags & WQ_UNBOUND))
3050f3421797STejun Heo 		wq->cpu_wq.pcpu = __alloc_percpu(size, align);
3051931ac77eSTejun Heo 	else {
30520f900049STejun Heo 		void *ptr;
3053e1d8aa9fSFrederic Weisbecker 
30540f900049STejun Heo 		/*
3055f3421797STejun Heo 		 * Allocate enough room to align cwq and put an extra
3056f3421797STejun Heo 		 * pointer at the end pointing back to the originally
3057f3421797STejun Heo 		 * allocated pointer which will be used for free.
30580f900049STejun Heo 		 */
3059bdbc5dd7STejun Heo 		ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
3060bdbc5dd7STejun Heo 		if (ptr) {
3061bdbc5dd7STejun Heo 			wq->cpu_wq.single = PTR_ALIGN(ptr, align);
3062bdbc5dd7STejun Heo 			*(void **)(wq->cpu_wq.single + 1) = ptr;
3063bdbc5dd7STejun Heo 		}
30643af24433SOleg Nesterov 	}
30653af24433SOleg Nesterov 
30660415b00dSTejun Heo 	/* just in case, make sure it's actually aligned */
3067bdbc5dd7STejun Heo 	BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
3068bdbc5dd7STejun Heo 	return wq->cpu_wq.v ? 0 : -ENOMEM;
30690f900049STejun Heo }
30700f900049STejun Heo 
3071bdbc5dd7STejun Heo static void free_cwqs(struct workqueue_struct *wq)
307206ba38a9SOleg Nesterov {
3073e06ffa1eSLai Jiangshan 	if (!(wq->flags & WQ_UNBOUND))
3074bdbc5dd7STejun Heo 		free_percpu(wq->cpu_wq.pcpu);
3075f3421797STejun Heo 	else if (wq->cpu_wq.single) {
3076f3421797STejun Heo 		/* the pointer to free is stored right after the cwq */
3077f3421797STejun Heo 		kfree(*(void **)(wq->cpu_wq.single + 1));
307806ba38a9SOleg Nesterov 	}
307906ba38a9SOleg Nesterov }
308006ba38a9SOleg Nesterov 
3081f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags,
3082f3421797STejun Heo 			       const char *name)
3083b71ab8c2STejun Heo {
3084f3421797STejun Heo 	int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3085f3421797STejun Heo 
3086f3421797STejun Heo 	if (max_active < 1 || max_active > lim)
3087b71ab8c2STejun Heo 		printk(KERN_WARNING "workqueue: max_active %d requested for %s "
3088b71ab8c2STejun Heo 		       "is out of range, clamping between %d and %d\n",
3089f3421797STejun Heo 		       max_active, name, 1, lim);
3090b71ab8c2STejun Heo 
3091f3421797STejun Heo 	return clamp_val(max_active, 1, lim);
3092b71ab8c2STejun Heo }
3093b71ab8c2STejun Heo 
3094b196be89STejun Heo struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
309597e37d7bSTejun Heo 					       unsigned int flags,
30961e19ffc6STejun Heo 					       int max_active,
3097eb13ba87SJohannes Berg 					       struct lock_class_key *key,
3098b196be89STejun Heo 					       const char *lock_name, ...)
30993af24433SOleg Nesterov {
3100b196be89STejun Heo 	va_list args, args1;
31013af24433SOleg Nesterov 	struct workqueue_struct *wq;
3102c34056a3STejun Heo 	unsigned int cpu;
3103b196be89STejun Heo 	size_t namelen;
3104b196be89STejun Heo 
3105b196be89STejun Heo 	/* determine namelen, allocate wq and format name */
3106b196be89STejun Heo 	va_start(args, lock_name);
3107b196be89STejun Heo 	va_copy(args1, args);
3108b196be89STejun Heo 	namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3109b196be89STejun Heo 
3110b196be89STejun Heo 	wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3111b196be89STejun Heo 	if (!wq)
3112b196be89STejun Heo 		goto err;
3113b196be89STejun Heo 
3114b196be89STejun Heo 	vsnprintf(wq->name, namelen, fmt, args1);
3115b196be89STejun Heo 	va_end(args);
3116b196be89STejun Heo 	va_end(args1);
31173af24433SOleg Nesterov 
3118f3421797STejun Heo 	/*
31196370a6adSTejun Heo 	 * Workqueues which may be used during memory reclaim should
31206370a6adSTejun Heo 	 * have a rescuer to guarantee forward progress.
31216370a6adSTejun Heo 	 */
31226370a6adSTejun Heo 	if (flags & WQ_MEM_RECLAIM)
31236370a6adSTejun Heo 		flags |= WQ_RESCUER;
31246370a6adSTejun Heo 
3125d320c038STejun Heo 	max_active = max_active ?: WQ_DFL_ACTIVE;
3126b196be89STejun Heo 	max_active = wq_clamp_max_active(max_active, flags, wq->name);
31273af24433SOleg Nesterov 
3128b196be89STejun Heo 	/* init wq */
312997e37d7bSTejun Heo 	wq->flags = flags;
3130a0a1a5fdSTejun Heo 	wq->saved_max_active = max_active;
313173f53c4aSTejun Heo 	mutex_init(&wq->flush_mutex);
313273f53c4aSTejun Heo 	atomic_set(&wq->nr_cwqs_to_flush, 0);
313373f53c4aSTejun Heo 	INIT_LIST_HEAD(&wq->flusher_queue);
313473f53c4aSTejun Heo 	INIT_LIST_HEAD(&wq->flusher_overflow);
31353af24433SOleg Nesterov 
3136eb13ba87SJohannes Berg 	lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
3137cce1a165SOleg Nesterov 	INIT_LIST_HEAD(&wq->list);
31383af24433SOleg Nesterov 
3139bdbc5dd7STejun Heo 	if (alloc_cwqs(wq) < 0)
3140bdbc5dd7STejun Heo 		goto err;
3141bdbc5dd7STejun Heo 
3142f3421797STejun Heo 	for_each_cwq_cpu(cpu, wq) {
31431537663fSTejun Heo 		struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
31448b03ae3cSTejun Heo 		struct global_cwq *gcwq = get_gcwq(cpu);
31453270476aSTejun Heo 		int pool_idx = (bool)(flags & WQ_HIGHPRI);
31461537663fSTejun Heo 
31470f900049STejun Heo 		BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
31483270476aSTejun Heo 		cwq->pool = &gcwq->pools[pool_idx];
3149c34056a3STejun Heo 		cwq->wq = wq;
315073f53c4aSTejun Heo 		cwq->flush_color = -1;
31511e19ffc6STejun Heo 		cwq->max_active = max_active;
31521e19ffc6STejun Heo 		INIT_LIST_HEAD(&cwq->delayed_works);
3153e22bee78STejun Heo 	}
31541537663fSTejun Heo 
3155e22bee78STejun Heo 	if (flags & WQ_RESCUER) {
3156e22bee78STejun Heo 		struct worker *rescuer;
3157e22bee78STejun Heo 
3158f2e005aaSTejun Heo 		if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
3159e22bee78STejun Heo 			goto err;
3160e22bee78STejun Heo 
3161e22bee78STejun Heo 		wq->rescuer = rescuer = alloc_worker();
3162e22bee78STejun Heo 		if (!rescuer)
3163e22bee78STejun Heo 			goto err;
3164e22bee78STejun Heo 
3165b196be89STejun Heo 		rescuer->task = kthread_create(rescuer_thread, wq, "%s",
3166b196be89STejun Heo 					       wq->name);
3167e22bee78STejun Heo 		if (IS_ERR(rescuer->task))
3168e22bee78STejun Heo 			goto err;
3169e22bee78STejun Heo 
3170e22bee78STejun Heo 		rescuer->task->flags |= PF_THREAD_BOUND;
3171e22bee78STejun Heo 		wake_up_process(rescuer->task);
31723af24433SOleg Nesterov 	}
31731537663fSTejun Heo 
31743af24433SOleg Nesterov 	/*
3175a0a1a5fdSTejun Heo 	 * workqueue_lock protects global freeze state and workqueues
3176a0a1a5fdSTejun Heo 	 * list.  Grab it, set max_active accordingly and add the new
3177a0a1a5fdSTejun Heo 	 * workqueue to workqueues list.
31783af24433SOleg Nesterov 	 */
31793af24433SOleg Nesterov 	spin_lock(&workqueue_lock);
3180a0a1a5fdSTejun Heo 
318158a69cb4STejun Heo 	if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
3182f3421797STejun Heo 		for_each_cwq_cpu(cpu, wq)
3183a0a1a5fdSTejun Heo 			get_cwq(cpu, wq)->max_active = 0;
3184a0a1a5fdSTejun Heo 
31853af24433SOleg Nesterov 	list_add(&wq->list, &workqueues);
3186a0a1a5fdSTejun Heo 
31873af24433SOleg Nesterov 	spin_unlock(&workqueue_lock);
31883af24433SOleg Nesterov 
31893af24433SOleg Nesterov 	return wq;
31904690c4abSTejun Heo err:
31914690c4abSTejun Heo 	if (wq) {
3192bdbc5dd7STejun Heo 		free_cwqs(wq);
3193f2e005aaSTejun Heo 		free_mayday_mask(wq->mayday_mask);
3194e22bee78STejun Heo 		kfree(wq->rescuer);
31954690c4abSTejun Heo 		kfree(wq);
31963af24433SOleg Nesterov 	}
31974690c4abSTejun Heo 	return NULL;
31981da177e4SLinus Torvalds }
3199d320c038STejun Heo EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
32001da177e4SLinus Torvalds 
32013af24433SOleg Nesterov /**
32023af24433SOleg Nesterov  * destroy_workqueue - safely terminate a workqueue
32033af24433SOleg Nesterov  * @wq: target workqueue
32043af24433SOleg Nesterov  *
32053af24433SOleg Nesterov  * Safely destroy a workqueue. All work currently pending will be done first.
32063af24433SOleg Nesterov  */
32073af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq)
32083af24433SOleg Nesterov {
3209c8e55f36STejun Heo 	unsigned int cpu;
32103af24433SOleg Nesterov 
32119c5a2ba7STejun Heo 	/* drain it before proceeding with destruction */
32129c5a2ba7STejun Heo 	drain_workqueue(wq);
3213c8efcc25STejun Heo 
3214a0a1a5fdSTejun Heo 	/*
3215a0a1a5fdSTejun Heo 	 * wq list is used to freeze wq, remove from list after
3216a0a1a5fdSTejun Heo 	 * flushing is complete in case freeze races us.
3217a0a1a5fdSTejun Heo 	 */
321895402b38SGautham R Shenoy 	spin_lock(&workqueue_lock);
32193af24433SOleg Nesterov 	list_del(&wq->list);
322095402b38SGautham R Shenoy 	spin_unlock(&workqueue_lock);
32213af24433SOleg Nesterov 
3222e22bee78STejun Heo 	/* sanity check */
3223f3421797STejun Heo 	for_each_cwq_cpu(cpu, wq) {
322473f53c4aSTejun Heo 		struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
322573f53c4aSTejun Heo 		int i;
32263af24433SOleg Nesterov 
322773f53c4aSTejun Heo 		for (i = 0; i < WORK_NR_COLORS; i++)
322873f53c4aSTejun Heo 			BUG_ON(cwq->nr_in_flight[i]);
32291e19ffc6STejun Heo 		BUG_ON(cwq->nr_active);
32301e19ffc6STejun Heo 		BUG_ON(!list_empty(&cwq->delayed_works));
323173f53c4aSTejun Heo 	}
32321537663fSTejun Heo 
3233e22bee78STejun Heo 	if (wq->flags & WQ_RESCUER) {
3234e22bee78STejun Heo 		kthread_stop(wq->rescuer->task);
3235f2e005aaSTejun Heo 		free_mayday_mask(wq->mayday_mask);
32368d9df9f0SXiaotian Feng 		kfree(wq->rescuer);
3237e22bee78STejun Heo 	}
3238e22bee78STejun Heo 
3239bdbc5dd7STejun Heo 	free_cwqs(wq);
32403af24433SOleg Nesterov 	kfree(wq);
32413af24433SOleg Nesterov }
32423af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue);
32433af24433SOleg Nesterov 
3244dcd989cbSTejun Heo /**
3245dcd989cbSTejun Heo  * workqueue_set_max_active - adjust max_active of a workqueue
3246dcd989cbSTejun Heo  * @wq: target workqueue
3247dcd989cbSTejun Heo  * @max_active: new max_active value.
3248dcd989cbSTejun Heo  *
3249dcd989cbSTejun Heo  * Set max_active of @wq to @max_active.
3250dcd989cbSTejun Heo  *
3251dcd989cbSTejun Heo  * CONTEXT:
3252dcd989cbSTejun Heo  * Don't call from IRQ context.
3253dcd989cbSTejun Heo  */
3254dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3255dcd989cbSTejun Heo {
3256dcd989cbSTejun Heo 	unsigned int cpu;
3257dcd989cbSTejun Heo 
3258f3421797STejun Heo 	max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
3259dcd989cbSTejun Heo 
3260dcd989cbSTejun Heo 	spin_lock(&workqueue_lock);
3261dcd989cbSTejun Heo 
3262dcd989cbSTejun Heo 	wq->saved_max_active = max_active;
3263dcd989cbSTejun Heo 
3264f3421797STejun Heo 	for_each_cwq_cpu(cpu, wq) {
3265dcd989cbSTejun Heo 		struct global_cwq *gcwq = get_gcwq(cpu);
3266dcd989cbSTejun Heo 
3267dcd989cbSTejun Heo 		spin_lock_irq(&gcwq->lock);
3268dcd989cbSTejun Heo 
326958a69cb4STejun Heo 		if (!(wq->flags & WQ_FREEZABLE) ||
3270dcd989cbSTejun Heo 		    !(gcwq->flags & GCWQ_FREEZING))
3271dcd989cbSTejun Heo 			get_cwq(gcwq->cpu, wq)->max_active = max_active;
3272dcd989cbSTejun Heo 
3273dcd989cbSTejun Heo 		spin_unlock_irq(&gcwq->lock);
3274dcd989cbSTejun Heo 	}
3275dcd989cbSTejun Heo 
3276dcd989cbSTejun Heo 	spin_unlock(&workqueue_lock);
3277dcd989cbSTejun Heo }
3278dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3279dcd989cbSTejun Heo 
3280dcd989cbSTejun Heo /**
3281dcd989cbSTejun Heo  * workqueue_congested - test whether a workqueue is congested
3282dcd989cbSTejun Heo  * @cpu: CPU in question
3283dcd989cbSTejun Heo  * @wq: target workqueue
3284dcd989cbSTejun Heo  *
3285dcd989cbSTejun Heo  * Test whether @wq's cpu workqueue for @cpu is congested.  There is
3286dcd989cbSTejun Heo  * no synchronization around this function and the test result is
3287dcd989cbSTejun Heo  * unreliable and only useful as advisory hints or for debugging.
3288dcd989cbSTejun Heo  *
3289dcd989cbSTejun Heo  * RETURNS:
3290dcd989cbSTejun Heo  * %true if congested, %false otherwise.
3291dcd989cbSTejun Heo  */
3292dcd989cbSTejun Heo bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3293dcd989cbSTejun Heo {
3294dcd989cbSTejun Heo 	struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3295dcd989cbSTejun Heo 
3296dcd989cbSTejun Heo 	return !list_empty(&cwq->delayed_works);
3297dcd989cbSTejun Heo }
3298dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested);
3299dcd989cbSTejun Heo 
3300dcd989cbSTejun Heo /**
3301dcd989cbSTejun Heo  * work_cpu - return the last known associated cpu for @work
3302dcd989cbSTejun Heo  * @work: the work of interest
3303dcd989cbSTejun Heo  *
3304dcd989cbSTejun Heo  * RETURNS:
3305bdbc5dd7STejun Heo  * CPU number if @work was ever queued.  WORK_CPU_NONE otherwise.
3306dcd989cbSTejun Heo  */
3307dcd989cbSTejun Heo unsigned int work_cpu(struct work_struct *work)
3308dcd989cbSTejun Heo {
3309dcd989cbSTejun Heo 	struct global_cwq *gcwq = get_work_gcwq(work);
3310dcd989cbSTejun Heo 
3311bdbc5dd7STejun Heo 	return gcwq ? gcwq->cpu : WORK_CPU_NONE;
3312dcd989cbSTejun Heo }
3313dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_cpu);
3314dcd989cbSTejun Heo 
3315dcd989cbSTejun Heo /**
3316dcd989cbSTejun Heo  * work_busy - test whether a work is currently pending or running
3317dcd989cbSTejun Heo  * @work: the work to be tested
3318dcd989cbSTejun Heo  *
3319dcd989cbSTejun Heo  * Test whether @work is currently pending or running.  There is no
3320dcd989cbSTejun Heo  * synchronization around this function and the test result is
3321dcd989cbSTejun Heo  * unreliable and only useful as advisory hints or for debugging.
3322dcd989cbSTejun Heo  * Especially for reentrant wqs, the pending state might hide the
3323dcd989cbSTejun Heo  * running state.
3324dcd989cbSTejun Heo  *
3325dcd989cbSTejun Heo  * RETURNS:
3326dcd989cbSTejun Heo  * OR'd bitmask of WORK_BUSY_* bits.
3327dcd989cbSTejun Heo  */
3328dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work)
3329dcd989cbSTejun Heo {
3330dcd989cbSTejun Heo 	struct global_cwq *gcwq = get_work_gcwq(work);
3331dcd989cbSTejun Heo 	unsigned long flags;
3332dcd989cbSTejun Heo 	unsigned int ret = 0;
3333dcd989cbSTejun Heo 
3334dcd989cbSTejun Heo 	if (!gcwq)
3335dcd989cbSTejun Heo 		return false;
3336dcd989cbSTejun Heo 
3337dcd989cbSTejun Heo 	spin_lock_irqsave(&gcwq->lock, flags);
3338dcd989cbSTejun Heo 
3339dcd989cbSTejun Heo 	if (work_pending(work))
3340dcd989cbSTejun Heo 		ret |= WORK_BUSY_PENDING;
3341dcd989cbSTejun Heo 	if (find_worker_executing_work(gcwq, work))
3342dcd989cbSTejun Heo 		ret |= WORK_BUSY_RUNNING;
3343dcd989cbSTejun Heo 
3344dcd989cbSTejun Heo 	spin_unlock_irqrestore(&gcwq->lock, flags);
3345dcd989cbSTejun Heo 
3346dcd989cbSTejun Heo 	return ret;
3347dcd989cbSTejun Heo }
3348dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy);
3349dcd989cbSTejun Heo 
3350db7bccf4STejun Heo /*
3351db7bccf4STejun Heo  * CPU hotplug.
3352db7bccf4STejun Heo  *
3353e22bee78STejun Heo  * There are two challenges in supporting CPU hotplug.  Firstly, there
3354e22bee78STejun Heo  * are a lot of assumptions on strong associations among work, cwq and
3355e22bee78STejun Heo  * gcwq which make migrating pending and scheduled works very
3356e22bee78STejun Heo  * difficult to implement without impacting hot paths.  Secondly,
3357e22bee78STejun Heo  * gcwqs serve mix of short, long and very long running works making
3358e22bee78STejun Heo  * blocked draining impractical.
3359e22bee78STejun Heo  *
3360628c78e7STejun Heo  * This is solved by allowing a gcwq to be disassociated from the CPU
3361628c78e7STejun Heo  * running as an unbound one and allowing it to be reattached later if the
3362628c78e7STejun Heo  * cpu comes back online.
3363db7bccf4STejun Heo  */
3364db7bccf4STejun Heo 
336560373152STejun Heo /* claim manager positions of all pools */
33668db25e78STejun Heo static void gcwq_claim_management_and_lock(struct global_cwq *gcwq)
336760373152STejun Heo {
336860373152STejun Heo 	struct worker_pool *pool;
336960373152STejun Heo 
337060373152STejun Heo 	for_each_worker_pool(pool, gcwq)
337160373152STejun Heo 		mutex_lock_nested(&pool->manager_mutex, pool - gcwq->pools);
33728db25e78STejun Heo 	spin_lock_irq(&gcwq->lock);
337360373152STejun Heo }
337460373152STejun Heo 
337560373152STejun Heo /* release manager positions */
33768db25e78STejun Heo static void gcwq_release_management_and_unlock(struct global_cwq *gcwq)
337760373152STejun Heo {
337860373152STejun Heo 	struct worker_pool *pool;
337960373152STejun Heo 
33808db25e78STejun Heo 	spin_unlock_irq(&gcwq->lock);
338160373152STejun Heo 	for_each_worker_pool(pool, gcwq)
338260373152STejun Heo 		mutex_unlock(&pool->manager_mutex);
338360373152STejun Heo }
338460373152STejun Heo 
3385628c78e7STejun Heo static void gcwq_unbind_fn(struct work_struct *work)
3386db7bccf4STejun Heo {
3387628c78e7STejun Heo 	struct global_cwq *gcwq = get_gcwq(smp_processor_id());
33884ce62e9eSTejun Heo 	struct worker_pool *pool;
3389db7bccf4STejun Heo 	struct worker *worker;
3390db7bccf4STejun Heo 	struct hlist_node *pos;
3391db7bccf4STejun Heo 	int i;
3392db7bccf4STejun Heo 
3393db7bccf4STejun Heo 	BUG_ON(gcwq->cpu != smp_processor_id());
3394db7bccf4STejun Heo 
33958db25e78STejun Heo 	gcwq_claim_management_and_lock(gcwq);
3396e22bee78STejun Heo 
3397f2d5a0eeSTejun Heo 	/*
3398f2d5a0eeSTejun Heo 	 * We've claimed all manager positions.  Make all workers unbound
3399f2d5a0eeSTejun Heo 	 * and set DISASSOCIATED.  Before this, all workers except for the
3400f2d5a0eeSTejun Heo 	 * ones which are still executing works from before the last CPU
3401f2d5a0eeSTejun Heo 	 * down must be on the cpu.  After this, they may become diasporas.
3402f2d5a0eeSTejun Heo 	 */
340360373152STejun Heo 	for_each_worker_pool(pool, gcwq)
34044ce62e9eSTejun Heo 		list_for_each_entry(worker, &pool->idle_list, entry)
3405403c821dSTejun Heo 			worker->flags |= WORKER_UNBOUND;
3406db7bccf4STejun Heo 
3407db7bccf4STejun Heo 	for_each_busy_worker(worker, i, pos, gcwq)
3408403c821dSTejun Heo 		worker->flags |= WORKER_UNBOUND;
3409db7bccf4STejun Heo 
3410f2d5a0eeSTejun Heo 	gcwq->flags |= GCWQ_DISASSOCIATED;
3411f2d5a0eeSTejun Heo 
34128db25e78STejun Heo 	gcwq_release_management_and_unlock(gcwq);
3413e22bee78STejun Heo 
3414e22bee78STejun Heo 	/*
3415628c78e7STejun Heo 	 * Call schedule() so that we cross rq->lock and thus can guarantee
3416628c78e7STejun Heo 	 * sched callbacks see the %WORKER_UNBOUND flag.  This is necessary
3417628c78e7STejun Heo 	 * as scheduler callbacks may be invoked from other cpus.
3418628c78e7STejun Heo 	 */
3419628c78e7STejun Heo 	schedule();
3420628c78e7STejun Heo 
3421628c78e7STejun Heo 	/*
3422628c78e7STejun Heo 	 * Sched callbacks are disabled now.  Zap nr_running.  After this,
3423628c78e7STejun Heo 	 * nr_running stays zero and need_more_worker() and keep_working()
3424628c78e7STejun Heo 	 * are always true as long as the worklist is not empty.  @gcwq now
3425628c78e7STejun Heo 	 * behaves as unbound (in terms of concurrency management) gcwq
3426628c78e7STejun Heo 	 * which is served by workers tied to the CPU.
3427628c78e7STejun Heo 	 *
3428628c78e7STejun Heo 	 * On return from this function, the current worker would trigger
3429628c78e7STejun Heo 	 * unbound chain execution of pending work items if other workers
3430628c78e7STejun Heo 	 * didn't already.
3431e22bee78STejun Heo 	 */
34324ce62e9eSTejun Heo 	for_each_worker_pool(pool, gcwq)
34334ce62e9eSTejun Heo 		atomic_set(get_pool_nr_running(pool), 0);
3434db7bccf4STejun Heo }
3435db7bccf4STejun Heo 
34368db25e78STejun Heo /*
34378db25e78STejun Heo  * Workqueues should be brought up before normal priority CPU notifiers.
34388db25e78STejun Heo  * This will be registered high priority CPU notifier.
34398db25e78STejun Heo  */
34408db25e78STejun Heo static int __devinit workqueue_cpu_up_callback(struct notifier_block *nfb,
34411da177e4SLinus Torvalds 					       unsigned long action,
34421da177e4SLinus Torvalds 					       void *hcpu)
34431da177e4SLinus Torvalds {
34443af24433SOleg Nesterov 	unsigned int cpu = (unsigned long)hcpu;
3445db7bccf4STejun Heo 	struct global_cwq *gcwq = get_gcwq(cpu);
34464ce62e9eSTejun Heo 	struct worker_pool *pool;
34471da177e4SLinus Torvalds 
34488db25e78STejun Heo 	switch (action & ~CPU_TASKS_FROZEN) {
34493af24433SOleg Nesterov 	case CPU_UP_PREPARE:
34504ce62e9eSTejun Heo 		for_each_worker_pool(pool, gcwq) {
34513ce63377STejun Heo 			struct worker *worker;
34523ce63377STejun Heo 
34533ce63377STejun Heo 			if (pool->nr_workers)
34543ce63377STejun Heo 				continue;
34553ce63377STejun Heo 
34563ce63377STejun Heo 			worker = create_worker(pool);
34573ce63377STejun Heo 			if (!worker)
34583ce63377STejun Heo 				return NOTIFY_BAD;
34593ce63377STejun Heo 
34603ce63377STejun Heo 			spin_lock_irq(&gcwq->lock);
34613ce63377STejun Heo 			start_worker(worker);
34623ce63377STejun Heo 			spin_unlock_irq(&gcwq->lock);
34633af24433SOleg Nesterov 		}
34641da177e4SLinus Torvalds 		break;
34651da177e4SLinus Torvalds 
346665758202STejun Heo 	case CPU_DOWN_FAILED:
346765758202STejun Heo 	case CPU_ONLINE:
34688db25e78STejun Heo 		gcwq_claim_management_and_lock(gcwq);
34698db25e78STejun Heo 		gcwq->flags &= ~GCWQ_DISASSOCIATED;
34708db25e78STejun Heo 		rebind_workers(gcwq);
34718db25e78STejun Heo 		gcwq_release_management_and_unlock(gcwq);
34728db25e78STejun Heo 		break;
347365758202STejun Heo 	}
347465758202STejun Heo 	return NOTIFY_OK;
347565758202STejun Heo }
347665758202STejun Heo 
347765758202STejun Heo /*
347865758202STejun Heo  * Workqueues should be brought down after normal priority CPU notifiers.
347965758202STejun Heo  * This will be registered as low priority CPU notifier.
348065758202STejun Heo  */
348165758202STejun Heo static int __devinit workqueue_cpu_down_callback(struct notifier_block *nfb,
348265758202STejun Heo 						 unsigned long action,
348365758202STejun Heo 						 void *hcpu)
348465758202STejun Heo {
34858db25e78STejun Heo 	unsigned int cpu = (unsigned long)hcpu;
34868db25e78STejun Heo 	struct work_struct unbind_work;
34878db25e78STejun Heo 
348865758202STejun Heo 	switch (action & ~CPU_TASKS_FROZEN) {
348965758202STejun Heo 	case CPU_DOWN_PREPARE:
34908db25e78STejun Heo 		/* unbinding should happen on the local CPU */
34918db25e78STejun Heo 		INIT_WORK_ONSTACK(&unbind_work, gcwq_unbind_fn);
34928db25e78STejun Heo 		schedule_work_on(cpu, &unbind_work);
34938db25e78STejun Heo 		flush_work(&unbind_work);
34948db25e78STejun Heo 		break;
349565758202STejun Heo 	}
349665758202STejun Heo 	return NOTIFY_OK;
349765758202STejun Heo }
349865758202STejun Heo 
34992d3854a3SRusty Russell #ifdef CONFIG_SMP
35008ccad40dSRusty Russell 
35012d3854a3SRusty Russell struct work_for_cpu {
35026b44003eSAndrew Morton 	struct completion completion;
35032d3854a3SRusty Russell 	long (*fn)(void *);
35042d3854a3SRusty Russell 	void *arg;
35052d3854a3SRusty Russell 	long ret;
35062d3854a3SRusty Russell };
35072d3854a3SRusty Russell 
35086b44003eSAndrew Morton static int do_work_for_cpu(void *_wfc)
35092d3854a3SRusty Russell {
35106b44003eSAndrew Morton 	struct work_for_cpu *wfc = _wfc;
35112d3854a3SRusty Russell 	wfc->ret = wfc->fn(wfc->arg);
35126b44003eSAndrew Morton 	complete(&wfc->completion);
35136b44003eSAndrew Morton 	return 0;
35142d3854a3SRusty Russell }
35152d3854a3SRusty Russell 
35162d3854a3SRusty Russell /**
35172d3854a3SRusty Russell  * work_on_cpu - run a function in user context on a particular cpu
35182d3854a3SRusty Russell  * @cpu: the cpu to run on
35192d3854a3SRusty Russell  * @fn: the function to run
35202d3854a3SRusty Russell  * @arg: the function arg
35212d3854a3SRusty Russell  *
352231ad9081SRusty Russell  * This will return the value @fn returns.
352331ad9081SRusty Russell  * It is up to the caller to ensure that the cpu doesn't go offline.
35246b44003eSAndrew Morton  * The caller must not hold any locks which would prevent @fn from completing.
35252d3854a3SRusty Russell  */
35262d3854a3SRusty Russell long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
35272d3854a3SRusty Russell {
35286b44003eSAndrew Morton 	struct task_struct *sub_thread;
35296b44003eSAndrew Morton 	struct work_for_cpu wfc = {
35306b44003eSAndrew Morton 		.completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
35316b44003eSAndrew Morton 		.fn = fn,
35326b44003eSAndrew Morton 		.arg = arg,
35336b44003eSAndrew Morton 	};
35342d3854a3SRusty Russell 
35356b44003eSAndrew Morton 	sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
35366b44003eSAndrew Morton 	if (IS_ERR(sub_thread))
35376b44003eSAndrew Morton 		return PTR_ERR(sub_thread);
35386b44003eSAndrew Morton 	kthread_bind(sub_thread, cpu);
35396b44003eSAndrew Morton 	wake_up_process(sub_thread);
35406b44003eSAndrew Morton 	wait_for_completion(&wfc.completion);
35412d3854a3SRusty Russell 	return wfc.ret;
35422d3854a3SRusty Russell }
35432d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu);
35442d3854a3SRusty Russell #endif /* CONFIG_SMP */
35452d3854a3SRusty Russell 
3546a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER
3547e7577c50SRusty Russell 
3548a0a1a5fdSTejun Heo /**
3549a0a1a5fdSTejun Heo  * freeze_workqueues_begin - begin freezing workqueues
3550a0a1a5fdSTejun Heo  *
355158a69cb4STejun Heo  * Start freezing workqueues.  After this function returns, all freezable
355258a69cb4STejun Heo  * workqueues will queue new works to their frozen_works list instead of
355358a69cb4STejun Heo  * gcwq->worklist.
3554a0a1a5fdSTejun Heo  *
3555a0a1a5fdSTejun Heo  * CONTEXT:
35568b03ae3cSTejun Heo  * Grabs and releases workqueue_lock and gcwq->lock's.
3557a0a1a5fdSTejun Heo  */
3558a0a1a5fdSTejun Heo void freeze_workqueues_begin(void)
3559a0a1a5fdSTejun Heo {
3560a0a1a5fdSTejun Heo 	unsigned int cpu;
3561a0a1a5fdSTejun Heo 
3562a0a1a5fdSTejun Heo 	spin_lock(&workqueue_lock);
3563a0a1a5fdSTejun Heo 
3564a0a1a5fdSTejun Heo 	BUG_ON(workqueue_freezing);
3565a0a1a5fdSTejun Heo 	workqueue_freezing = true;
3566a0a1a5fdSTejun Heo 
3567f3421797STejun Heo 	for_each_gcwq_cpu(cpu) {
35688b03ae3cSTejun Heo 		struct global_cwq *gcwq = get_gcwq(cpu);
3569bdbc5dd7STejun Heo 		struct workqueue_struct *wq;
35708b03ae3cSTejun Heo 
35718b03ae3cSTejun Heo 		spin_lock_irq(&gcwq->lock);
35728b03ae3cSTejun Heo 
3573db7bccf4STejun Heo 		BUG_ON(gcwq->flags & GCWQ_FREEZING);
3574db7bccf4STejun Heo 		gcwq->flags |= GCWQ_FREEZING;
3575db7bccf4STejun Heo 
3576a0a1a5fdSTejun Heo 		list_for_each_entry(wq, &workqueues, list) {
3577a0a1a5fdSTejun Heo 			struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3578a0a1a5fdSTejun Heo 
357958a69cb4STejun Heo 			if (cwq && wq->flags & WQ_FREEZABLE)
3580a0a1a5fdSTejun Heo 				cwq->max_active = 0;
35811da177e4SLinus Torvalds 		}
35828b03ae3cSTejun Heo 
35838b03ae3cSTejun Heo 		spin_unlock_irq(&gcwq->lock);
3584a0a1a5fdSTejun Heo 	}
3585a0a1a5fdSTejun Heo 
3586a0a1a5fdSTejun Heo 	spin_unlock(&workqueue_lock);
3587a0a1a5fdSTejun Heo }
3588a0a1a5fdSTejun Heo 
3589a0a1a5fdSTejun Heo /**
359058a69cb4STejun Heo  * freeze_workqueues_busy - are freezable workqueues still busy?
3591a0a1a5fdSTejun Heo  *
3592a0a1a5fdSTejun Heo  * Check whether freezing is complete.  This function must be called
3593a0a1a5fdSTejun Heo  * between freeze_workqueues_begin() and thaw_workqueues().
3594a0a1a5fdSTejun Heo  *
3595a0a1a5fdSTejun Heo  * CONTEXT:
3596a0a1a5fdSTejun Heo  * Grabs and releases workqueue_lock.
3597a0a1a5fdSTejun Heo  *
3598a0a1a5fdSTejun Heo  * RETURNS:
359958a69cb4STejun Heo  * %true if some freezable workqueues are still busy.  %false if freezing
360058a69cb4STejun Heo  * is complete.
3601a0a1a5fdSTejun Heo  */
3602a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void)
3603a0a1a5fdSTejun Heo {
3604a0a1a5fdSTejun Heo 	unsigned int cpu;
3605a0a1a5fdSTejun Heo 	bool busy = false;
3606a0a1a5fdSTejun Heo 
3607a0a1a5fdSTejun Heo 	spin_lock(&workqueue_lock);
3608a0a1a5fdSTejun Heo 
3609a0a1a5fdSTejun Heo 	BUG_ON(!workqueue_freezing);
3610a0a1a5fdSTejun Heo 
3611f3421797STejun Heo 	for_each_gcwq_cpu(cpu) {
3612bdbc5dd7STejun Heo 		struct workqueue_struct *wq;
3613a0a1a5fdSTejun Heo 		/*
3614a0a1a5fdSTejun Heo 		 * nr_active is monotonically decreasing.  It's safe
3615a0a1a5fdSTejun Heo 		 * to peek without lock.
3616a0a1a5fdSTejun Heo 		 */
3617a0a1a5fdSTejun Heo 		list_for_each_entry(wq, &workqueues, list) {
3618a0a1a5fdSTejun Heo 			struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3619a0a1a5fdSTejun Heo 
362058a69cb4STejun Heo 			if (!cwq || !(wq->flags & WQ_FREEZABLE))
3621a0a1a5fdSTejun Heo 				continue;
3622a0a1a5fdSTejun Heo 
3623a0a1a5fdSTejun Heo 			BUG_ON(cwq->nr_active < 0);
3624a0a1a5fdSTejun Heo 			if (cwq->nr_active) {
3625a0a1a5fdSTejun Heo 				busy = true;
3626a0a1a5fdSTejun Heo 				goto out_unlock;
3627a0a1a5fdSTejun Heo 			}
3628a0a1a5fdSTejun Heo 		}
3629a0a1a5fdSTejun Heo 	}
3630a0a1a5fdSTejun Heo out_unlock:
3631a0a1a5fdSTejun Heo 	spin_unlock(&workqueue_lock);
3632a0a1a5fdSTejun Heo 	return busy;
3633a0a1a5fdSTejun Heo }
3634a0a1a5fdSTejun Heo 
3635a0a1a5fdSTejun Heo /**
3636a0a1a5fdSTejun Heo  * thaw_workqueues - thaw workqueues
3637a0a1a5fdSTejun Heo  *
3638a0a1a5fdSTejun Heo  * Thaw workqueues.  Normal queueing is restored and all collected
36397e11629dSTejun Heo  * frozen works are transferred to their respective gcwq worklists.
3640a0a1a5fdSTejun Heo  *
3641a0a1a5fdSTejun Heo  * CONTEXT:
36428b03ae3cSTejun Heo  * Grabs and releases workqueue_lock and gcwq->lock's.
3643a0a1a5fdSTejun Heo  */
3644a0a1a5fdSTejun Heo void thaw_workqueues(void)
3645a0a1a5fdSTejun Heo {
3646a0a1a5fdSTejun Heo 	unsigned int cpu;
3647a0a1a5fdSTejun Heo 
3648a0a1a5fdSTejun Heo 	spin_lock(&workqueue_lock);
3649a0a1a5fdSTejun Heo 
3650a0a1a5fdSTejun Heo 	if (!workqueue_freezing)
3651a0a1a5fdSTejun Heo 		goto out_unlock;
3652a0a1a5fdSTejun Heo 
3653f3421797STejun Heo 	for_each_gcwq_cpu(cpu) {
36548b03ae3cSTejun Heo 		struct global_cwq *gcwq = get_gcwq(cpu);
36554ce62e9eSTejun Heo 		struct worker_pool *pool;
3656bdbc5dd7STejun Heo 		struct workqueue_struct *wq;
36578b03ae3cSTejun Heo 
36588b03ae3cSTejun Heo 		spin_lock_irq(&gcwq->lock);
36598b03ae3cSTejun Heo 
3660db7bccf4STejun Heo 		BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3661db7bccf4STejun Heo 		gcwq->flags &= ~GCWQ_FREEZING;
3662db7bccf4STejun Heo 
3663a0a1a5fdSTejun Heo 		list_for_each_entry(wq, &workqueues, list) {
3664a0a1a5fdSTejun Heo 			struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3665a0a1a5fdSTejun Heo 
366658a69cb4STejun Heo 			if (!cwq || !(wq->flags & WQ_FREEZABLE))
3667a0a1a5fdSTejun Heo 				continue;
3668a0a1a5fdSTejun Heo 
3669a0a1a5fdSTejun Heo 			/* restore max_active and repopulate worklist */
3670a0a1a5fdSTejun Heo 			cwq->max_active = wq->saved_max_active;
3671a0a1a5fdSTejun Heo 
3672a0a1a5fdSTejun Heo 			while (!list_empty(&cwq->delayed_works) &&
3673a0a1a5fdSTejun Heo 			       cwq->nr_active < cwq->max_active)
3674a0a1a5fdSTejun Heo 				cwq_activate_first_delayed(cwq);
3675a0a1a5fdSTejun Heo 		}
36768b03ae3cSTejun Heo 
36774ce62e9eSTejun Heo 		for_each_worker_pool(pool, gcwq)
36784ce62e9eSTejun Heo 			wake_up_worker(pool);
3679e22bee78STejun Heo 
36808b03ae3cSTejun Heo 		spin_unlock_irq(&gcwq->lock);
3681a0a1a5fdSTejun Heo 	}
3682a0a1a5fdSTejun Heo 
3683a0a1a5fdSTejun Heo 	workqueue_freezing = false;
3684a0a1a5fdSTejun Heo out_unlock:
3685a0a1a5fdSTejun Heo 	spin_unlock(&workqueue_lock);
3686a0a1a5fdSTejun Heo }
3687a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */
3688a0a1a5fdSTejun Heo 
36896ee0578bSSuresh Siddha static int __init init_workqueues(void)
36901da177e4SLinus Torvalds {
3691c34056a3STejun Heo 	unsigned int cpu;
3692c8e55f36STejun Heo 	int i;
3693c34056a3STejun Heo 
369465758202STejun Heo 	cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
369565758202STejun Heo 	cpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
36968b03ae3cSTejun Heo 
36978b03ae3cSTejun Heo 	/* initialize gcwqs */
3698f3421797STejun Heo 	for_each_gcwq_cpu(cpu) {
36998b03ae3cSTejun Heo 		struct global_cwq *gcwq = get_gcwq(cpu);
37004ce62e9eSTejun Heo 		struct worker_pool *pool;
37018b03ae3cSTejun Heo 
37028b03ae3cSTejun Heo 		spin_lock_init(&gcwq->lock);
37038b03ae3cSTejun Heo 		gcwq->cpu = cpu;
3704f3421797STejun Heo 		gcwq->flags |= GCWQ_DISASSOCIATED;
37058b03ae3cSTejun Heo 
3706c8e55f36STejun Heo 		for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3707c8e55f36STejun Heo 			INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3708c8e55f36STejun Heo 
37094ce62e9eSTejun Heo 		for_each_worker_pool(pool, gcwq) {
37104ce62e9eSTejun Heo 			pool->gcwq = gcwq;
37114ce62e9eSTejun Heo 			INIT_LIST_HEAD(&pool->worklist);
37124ce62e9eSTejun Heo 			INIT_LIST_HEAD(&pool->idle_list);
3713e22bee78STejun Heo 
37144ce62e9eSTejun Heo 			init_timer_deferrable(&pool->idle_timer);
37154ce62e9eSTejun Heo 			pool->idle_timer.function = idle_worker_timeout;
37164ce62e9eSTejun Heo 			pool->idle_timer.data = (unsigned long)pool;
3717e22bee78STejun Heo 
37184ce62e9eSTejun Heo 			setup_timer(&pool->mayday_timer, gcwq_mayday_timeout,
37194ce62e9eSTejun Heo 				    (unsigned long)pool);
37204ce62e9eSTejun Heo 
372160373152STejun Heo 			mutex_init(&pool->manager_mutex);
37224ce62e9eSTejun Heo 			ida_init(&pool->worker_ida);
37234ce62e9eSTejun Heo 		}
3724db7bccf4STejun Heo 
372525511a47STejun Heo 		init_waitqueue_head(&gcwq->rebind_hold);
37268b03ae3cSTejun Heo 	}
37278b03ae3cSTejun Heo 
3728e22bee78STejun Heo 	/* create the initial worker */
3729f3421797STejun Heo 	for_each_online_gcwq_cpu(cpu) {
3730e22bee78STejun Heo 		struct global_cwq *gcwq = get_gcwq(cpu);
37314ce62e9eSTejun Heo 		struct worker_pool *pool;
3732e22bee78STejun Heo 
3733477a3c33STejun Heo 		if (cpu != WORK_CPU_UNBOUND)
3734477a3c33STejun Heo 			gcwq->flags &= ~GCWQ_DISASSOCIATED;
37354ce62e9eSTejun Heo 
37364ce62e9eSTejun Heo 		for_each_worker_pool(pool, gcwq) {
37374ce62e9eSTejun Heo 			struct worker *worker;
37384ce62e9eSTejun Heo 
3739bc2ae0f5STejun Heo 			worker = create_worker(pool);
3740e22bee78STejun Heo 			BUG_ON(!worker);
3741e22bee78STejun Heo 			spin_lock_irq(&gcwq->lock);
3742e22bee78STejun Heo 			start_worker(worker);
3743e22bee78STejun Heo 			spin_unlock_irq(&gcwq->lock);
3744e22bee78STejun Heo 		}
37454ce62e9eSTejun Heo 	}
3746e22bee78STejun Heo 
3747d320c038STejun Heo 	system_wq = alloc_workqueue("events", 0, 0);
3748d320c038STejun Heo 	system_long_wq = alloc_workqueue("events_long", 0, 0);
3749d320c038STejun Heo 	system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
3750f3421797STejun Heo 	system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3751f3421797STejun Heo 					    WQ_UNBOUND_MAX_ACTIVE);
375224d51addSTejun Heo 	system_freezable_wq = alloc_workqueue("events_freezable",
375324d51addSTejun Heo 					      WQ_FREEZABLE, 0);
375462d3c543SAlan Stern 	system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable",
375562d3c543SAlan Stern 			WQ_NON_REENTRANT | WQ_FREEZABLE, 0);
3756e5cba24eSHitoshi Mitake 	BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq ||
375762d3c543SAlan Stern 	       !system_unbound_wq || !system_freezable_wq ||
375862d3c543SAlan Stern 		!system_nrt_freezable_wq);
37596ee0578bSSuresh Siddha 	return 0;
37601da177e4SLinus Torvalds }
37616ee0578bSSuresh Siddha early_initcall(init_workqueues);
3762