xref: /linux-6.15/kernel/workqueue.c (revision ec58815a)
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 <[email protected]>
8e1f8e874SFrancois Cami  *     Andrew Morton
91da177e4SLinus Torvalds  *     Kai Petzke <[email protected]>
101da177e4SLinus Torvalds  *     Theodore Ts'o <[email protected]>
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 <[email protected]>
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 /**
10560fcb78c2SRolf Eike Beer  * queue_work - queue work on a workqueue
10570fcb78c2SRolf Eike Beer  * @wq: workqueue to use
10580fcb78c2SRolf Eike Beer  * @work: work to queue
10590fcb78c2SRolf Eike Beer  *
1060057647fcSAlan Stern  * Returns 0 if @work was already on a queue, non-zero otherwise.
10611da177e4SLinus Torvalds  *
106200dfcaf7SOleg Nesterov  * We queue the work to the CPU on which it was submitted, but if the CPU dies
106300dfcaf7SOleg Nesterov  * it can be processed by another CPU.
10641da177e4SLinus Torvalds  */
10657ad5b3a5SHarvey Harrison int queue_work(struct workqueue_struct *wq, struct work_struct *work)
10661da177e4SLinus Torvalds {
1067ef1ca236SOleg Nesterov 	int ret;
10681da177e4SLinus Torvalds 
1069ef1ca236SOleg Nesterov 	ret = queue_work_on(get_cpu(), wq, work);
1070a848e3b6SOleg Nesterov 	put_cpu();
1071ef1ca236SOleg Nesterov 
10721da177e4SLinus Torvalds 	return ret;
10731da177e4SLinus Torvalds }
1074ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_work);
10751da177e4SLinus Torvalds 
1076c1a220e7SZhang Rui /**
1077c1a220e7SZhang Rui  * queue_work_on - queue work on specific cpu
1078c1a220e7SZhang Rui  * @cpu: CPU number to execute work on
1079c1a220e7SZhang Rui  * @wq: workqueue to use
1080c1a220e7SZhang Rui  * @work: work to queue
1081c1a220e7SZhang Rui  *
1082c1a220e7SZhang Rui  * Returns 0 if @work was already on a queue, non-zero otherwise.
1083c1a220e7SZhang Rui  *
1084c1a220e7SZhang Rui  * We queue the work to a specific CPU, the caller must ensure it
1085c1a220e7SZhang Rui  * can't go away.
1086c1a220e7SZhang Rui  */
1087c1a220e7SZhang Rui int
1088c1a220e7SZhang Rui queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
1089c1a220e7SZhang Rui {
1090c1a220e7SZhang Rui 	int ret = 0;
1091c1a220e7SZhang Rui 
109222df02bbSTejun Heo 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
10934690c4abSTejun Heo 		__queue_work(cpu, wq, work);
1094c1a220e7SZhang Rui 		ret = 1;
1095c1a220e7SZhang Rui 	}
1096c1a220e7SZhang Rui 	return ret;
1097c1a220e7SZhang Rui }
1098c1a220e7SZhang Rui EXPORT_SYMBOL_GPL(queue_work_on);
1099c1a220e7SZhang Rui 
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 - queue work on a workqueue after delay
11100fcb78c2SRolf Eike Beer  * @wq: workqueue to use
1111af9997e4SRandy Dunlap  * @dwork: delayable work to queue
11120fcb78c2SRolf Eike Beer  * @delay: number of jiffies to wait before queueing
11130fcb78c2SRolf Eike Beer  *
1114057647fcSAlan Stern  * Returns 0 if @work was already on a queue, non-zero otherwise.
11150fcb78c2SRolf Eike Beer  */
11167ad5b3a5SHarvey Harrison int queue_delayed_work(struct workqueue_struct *wq,
111752bad64dSDavid Howells 			struct delayed_work *dwork, unsigned long delay)
11181da177e4SLinus Torvalds {
111952bad64dSDavid Howells 	if (delay == 0)
112063bc0362SOleg Nesterov 		return queue_work(wq, &dwork->work);
11211da177e4SLinus Torvalds 
112263bc0362SOleg Nesterov 	return queue_delayed_work_on(-1, wq, dwork, delay);
11231da177e4SLinus Torvalds }
1124ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work);
11251da177e4SLinus Torvalds 
11260fcb78c2SRolf Eike Beer /**
11270fcb78c2SRolf Eike Beer  * queue_delayed_work_on - queue work on specific CPU after delay
11280fcb78c2SRolf Eike Beer  * @cpu: CPU number to execute work on
11290fcb78c2SRolf Eike Beer  * @wq: workqueue to use
1130af9997e4SRandy Dunlap  * @dwork: work to queue
11310fcb78c2SRolf Eike Beer  * @delay: number of jiffies to wait before queueing
11320fcb78c2SRolf Eike Beer  *
1133057647fcSAlan Stern  * Returns 0 if @work was already on a queue, non-zero otherwise.
11340fcb78c2SRolf Eike Beer  */
11357a6bc1cdSVenkatesh Pallipadi int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
113652bad64dSDavid Howells 			struct delayed_work *dwork, unsigned long delay)
11377a6bc1cdSVenkatesh Pallipadi {
11387a6bc1cdSVenkatesh Pallipadi 	int ret = 0;
113952bad64dSDavid Howells 	struct timer_list *timer = &dwork->timer;
114052bad64dSDavid Howells 	struct work_struct *work = &dwork->work;
11417a6bc1cdSVenkatesh Pallipadi 
114222df02bbSTejun Heo 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
1143c7fc77f7STejun Heo 		unsigned int lcpu;
11447a22ad75STejun Heo 
11457a6bc1cdSVenkatesh Pallipadi 		BUG_ON(timer_pending(timer));
11467a6bc1cdSVenkatesh Pallipadi 		BUG_ON(!list_empty(&work->entry));
11477a6bc1cdSVenkatesh Pallipadi 
11488a3e77ccSAndrew Liu 		timer_stats_timer_set_start_info(&dwork->timer);
11498a3e77ccSAndrew Liu 
11507a22ad75STejun Heo 		/*
11517a22ad75STejun Heo 		 * This stores cwq for the moment, for the timer_fn.
11527a22ad75STejun Heo 		 * Note that the work's gcwq is preserved to allow
11537a22ad75STejun Heo 		 * reentrance detection for delayed works.
11547a22ad75STejun Heo 		 */
1155c7fc77f7STejun Heo 		if (!(wq->flags & WQ_UNBOUND)) {
1156c7fc77f7STejun Heo 			struct global_cwq *gcwq = get_work_gcwq(work);
1157c7fc77f7STejun Heo 
1158c7fc77f7STejun Heo 			if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND)
1159c7fc77f7STejun Heo 				lcpu = gcwq->cpu;
1160c7fc77f7STejun Heo 			else
1161c7fc77f7STejun Heo 				lcpu = raw_smp_processor_id();
1162c7fc77f7STejun Heo 		} else
1163c7fc77f7STejun Heo 			lcpu = WORK_CPU_UNBOUND;
1164c7fc77f7STejun Heo 
11657a22ad75STejun Heo 		set_work_cwq(work, get_cwq(lcpu, wq), 0);
1166c7fc77f7STejun Heo 
11677a6bc1cdSVenkatesh Pallipadi 		timer->expires = jiffies + delay;
116852bad64dSDavid Howells 		timer->data = (unsigned long)dwork;
11697a6bc1cdSVenkatesh Pallipadi 		timer->function = delayed_work_timer_fn;
117063bc0362SOleg Nesterov 
117163bc0362SOleg Nesterov 		if (unlikely(cpu >= 0))
11727a6bc1cdSVenkatesh Pallipadi 			add_timer_on(timer, cpu);
117363bc0362SOleg Nesterov 		else
117463bc0362SOleg Nesterov 			add_timer(timer);
11757a6bc1cdSVenkatesh Pallipadi 		ret = 1;
11767a6bc1cdSVenkatesh Pallipadi 	}
11777a6bc1cdSVenkatesh Pallipadi 	return ret;
11787a6bc1cdSVenkatesh Pallipadi }
1179ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work_on);
11801da177e4SLinus Torvalds 
1181c8e55f36STejun 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));
1329*ec58815aSTejun Heo 
1330*ec58815aSTejun Heo 	/*
1331*ec58815aSTejun Heo 	 * rebind_workers() shouldn't finish until all workers passed the
1332*ec58815aSTejun Heo 	 * above WORKER_REBIND wait.  Tell it when done.
1333*ec58815aSTejun Heo 	 */
1334*ec58815aSTejun Heo 	spin_lock_irq(&worker->pool->gcwq->lock);
1335*ec58815aSTejun Heo 	if (!--worker->idle_rebind->cnt)
1336*ec58815aSTejun Heo 		complete(&worker->idle_rebind->done);
1337*ec58815aSTejun Heo 	spin_unlock_irq(&worker->pool->gcwq->lock);
133825511a47STejun Heo }
133925511a47STejun Heo 
134025511a47STejun Heo /*
134125511a47STejun Heo  * Function for @worker->rebind.work used to rebind unbound busy workers to
1342403c821dSTejun Heo  * the associated cpu which is coming back online.  This is scheduled by
1343403c821dSTejun Heo  * cpu up but can race with other cpu hotplug operations and may be
1344403c821dSTejun Heo  * executed twice without intervening cpu down.
1345e22bee78STejun Heo  */
134625511a47STejun Heo static void busy_worker_rebind_fn(struct work_struct *work)
1347e22bee78STejun Heo {
1348e22bee78STejun Heo 	struct worker *worker = container_of(work, struct worker, rebind_work);
1349bd7bdd43STejun Heo 	struct global_cwq *gcwq = worker->pool->gcwq;
1350e22bee78STejun Heo 
1351e22bee78STejun Heo 	if (worker_maybe_bind_and_lock(worker))
1352e22bee78STejun Heo 		worker_clr_flags(worker, WORKER_REBIND);
1353e22bee78STejun Heo 
1354e22bee78STejun Heo 	spin_unlock_irq(&gcwq->lock);
1355e22bee78STejun Heo }
1356e22bee78STejun Heo 
135725511a47STejun Heo /**
135825511a47STejun Heo  * rebind_workers - rebind all workers of a gcwq to the associated CPU
135925511a47STejun Heo  * @gcwq: gcwq of interest
136025511a47STejun Heo  *
136125511a47STejun Heo  * @gcwq->cpu is coming online.  Rebind all workers to the CPU.  Rebinding
136225511a47STejun Heo  * is different for idle and busy ones.
136325511a47STejun Heo  *
136425511a47STejun Heo  * The idle ones should be rebound synchronously and idle rebinding should
136525511a47STejun Heo  * be complete before any worker starts executing work items with
136625511a47STejun Heo  * concurrency management enabled; otherwise, scheduler may oops trying to
136725511a47STejun Heo  * wake up non-local idle worker from wq_worker_sleeping().
136825511a47STejun Heo  *
136925511a47STejun Heo  * This is achieved by repeatedly requesting rebinding until all idle
137025511a47STejun Heo  * workers are known to have been rebound under @gcwq->lock and holding all
137125511a47STejun Heo  * idle workers from becoming busy until idle rebinding is complete.
137225511a47STejun Heo  *
137325511a47STejun Heo  * Once idle workers are rebound, busy workers can be rebound as they
137425511a47STejun Heo  * finish executing their current work items.  Queueing the rebind work at
137525511a47STejun Heo  * the head of their scheduled lists is enough.  Note that nr_running will
137625511a47STejun Heo  * be properbly bumped as busy workers rebind.
137725511a47STejun Heo  *
137825511a47STejun Heo  * On return, all workers are guaranteed to either be bound or have rebind
137925511a47STejun Heo  * work item scheduled.
138025511a47STejun Heo  */
138125511a47STejun Heo static void rebind_workers(struct global_cwq *gcwq)
138225511a47STejun Heo 	__releases(&gcwq->lock) __acquires(&gcwq->lock)
138325511a47STejun Heo {
138425511a47STejun Heo 	struct idle_rebind idle_rebind;
138525511a47STejun Heo 	struct worker_pool *pool;
138625511a47STejun Heo 	struct worker *worker;
138725511a47STejun Heo 	struct hlist_node *pos;
138825511a47STejun Heo 	int i;
138925511a47STejun Heo 
139025511a47STejun Heo 	lockdep_assert_held(&gcwq->lock);
139125511a47STejun Heo 
139225511a47STejun Heo 	for_each_worker_pool(pool, gcwq)
139325511a47STejun Heo 		lockdep_assert_held(&pool->manager_mutex);
139425511a47STejun Heo 
139525511a47STejun Heo 	/*
139625511a47STejun Heo 	 * Rebind idle workers.  Interlocked both ways.  We wait for
139725511a47STejun Heo 	 * workers to rebind via @idle_rebind.done.  Workers will wait for
139825511a47STejun Heo 	 * us to finish up by watching %WORKER_REBIND.
139925511a47STejun Heo 	 */
140025511a47STejun Heo 	init_completion(&idle_rebind.done);
140125511a47STejun Heo retry:
140225511a47STejun Heo 	idle_rebind.cnt = 1;
140325511a47STejun Heo 	INIT_COMPLETION(idle_rebind.done);
140425511a47STejun Heo 
140525511a47STejun Heo 	/* set REBIND and kick idle ones, we'll wait for these later */
140625511a47STejun Heo 	for_each_worker_pool(pool, gcwq) {
140725511a47STejun Heo 		list_for_each_entry(worker, &pool->idle_list, entry) {
140896e65306SLai Jiangshan 			unsigned long worker_flags = worker->flags;
140996e65306SLai Jiangshan 
141025511a47STejun Heo 			if (worker->flags & WORKER_REBIND)
141125511a47STejun Heo 				continue;
141225511a47STejun Heo 
141396e65306SLai Jiangshan 			/* morph UNBOUND to REBIND atomically */
141496e65306SLai Jiangshan 			worker_flags &= ~WORKER_UNBOUND;
141596e65306SLai Jiangshan 			worker_flags |= WORKER_REBIND;
141696e65306SLai Jiangshan 			ACCESS_ONCE(worker->flags) = worker_flags;
141725511a47STejun Heo 
141825511a47STejun Heo 			idle_rebind.cnt++;
141925511a47STejun Heo 			worker->idle_rebind = &idle_rebind;
142025511a47STejun Heo 
142125511a47STejun Heo 			/* worker_thread() will call idle_worker_rebind() */
142225511a47STejun Heo 			wake_up_process(worker->task);
142325511a47STejun Heo 		}
142425511a47STejun Heo 	}
142525511a47STejun Heo 
142625511a47STejun Heo 	if (--idle_rebind.cnt) {
142725511a47STejun Heo 		spin_unlock_irq(&gcwq->lock);
142825511a47STejun Heo 		wait_for_completion(&idle_rebind.done);
142925511a47STejun Heo 		spin_lock_irq(&gcwq->lock);
143025511a47STejun Heo 		/* busy ones might have become idle while waiting, retry */
143125511a47STejun Heo 		goto retry;
143225511a47STejun Heo 	}
143325511a47STejun Heo 
143490beca5dSTejun Heo 	/* all idle workers are rebound, rebind busy workers */
143525511a47STejun Heo 	for_each_busy_worker(worker, i, pos, gcwq) {
143625511a47STejun Heo 		struct work_struct *rebind_work = &worker->rebind_work;
143796e65306SLai Jiangshan 		unsigned long worker_flags = worker->flags;
143825511a47STejun Heo 
143996e65306SLai Jiangshan 		/* morph UNBOUND to REBIND atomically */
144096e65306SLai Jiangshan 		worker_flags &= ~WORKER_UNBOUND;
144196e65306SLai Jiangshan 		worker_flags |= WORKER_REBIND;
144296e65306SLai Jiangshan 		ACCESS_ONCE(worker->flags) = worker_flags;
144325511a47STejun Heo 
144425511a47STejun Heo 		if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
144525511a47STejun Heo 				     work_data_bits(rebind_work)))
144625511a47STejun Heo 			continue;
144725511a47STejun Heo 
144825511a47STejun Heo 		/* wq doesn't matter, use the default one */
144925511a47STejun Heo 		debug_work_activate(rebind_work);
145025511a47STejun Heo 		insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
145125511a47STejun Heo 			    worker->scheduled.next,
145225511a47STejun Heo 			    work_color_to_flags(WORK_NO_COLOR));
145325511a47STejun Heo 	}
145490beca5dSTejun Heo 
145590beca5dSTejun Heo 	/*
145690beca5dSTejun Heo 	 * All idle workers are rebound and waiting for %WORKER_REBIND to
145790beca5dSTejun Heo 	 * be cleared inside idle_worker_rebind().  Clear and release.
145890beca5dSTejun Heo 	 * Clearing %WORKER_REBIND from this foreign context is safe
145990beca5dSTejun Heo 	 * because these workers are still guaranteed to be idle.
1460*ec58815aSTejun Heo 	 *
1461*ec58815aSTejun Heo 	 * We need to make sure all idle workers passed WORKER_REBIND wait
1462*ec58815aSTejun Heo 	 * in idle_worker_rebind() before returning; otherwise, workers can
1463*ec58815aSTejun Heo 	 * get stuck at the wait if hotplug cycle repeats.
146490beca5dSTejun Heo 	 */
1465*ec58815aSTejun Heo 	idle_rebind.cnt = 1;
1466*ec58815aSTejun Heo 	INIT_COMPLETION(idle_rebind.done);
1467*ec58815aSTejun Heo 
1468*ec58815aSTejun Heo 	for_each_worker_pool(pool, gcwq) {
1469*ec58815aSTejun Heo 		list_for_each_entry(worker, &pool->idle_list, entry) {
147090beca5dSTejun Heo 			worker->flags &= ~WORKER_REBIND;
1471*ec58815aSTejun Heo 			idle_rebind.cnt++;
1472*ec58815aSTejun Heo 		}
1473*ec58815aSTejun Heo 	}
147490beca5dSTejun Heo 
147590beca5dSTejun Heo 	wake_up_all(&gcwq->rebind_hold);
1476*ec58815aSTejun Heo 
1477*ec58815aSTejun Heo 	if (--idle_rebind.cnt) {
1478*ec58815aSTejun Heo 		spin_unlock_irq(&gcwq->lock);
1479*ec58815aSTejun Heo 		wait_for_completion(&idle_rebind.done);
1480*ec58815aSTejun Heo 		spin_lock_irq(&gcwq->lock);
1481*ec58815aSTejun Heo 	}
148225511a47STejun Heo }
148325511a47STejun Heo 
1484c34056a3STejun Heo static struct worker *alloc_worker(void)
1485c34056a3STejun Heo {
1486c34056a3STejun Heo 	struct worker *worker;
1487c34056a3STejun Heo 
1488c34056a3STejun Heo 	worker = kzalloc(sizeof(*worker), GFP_KERNEL);
1489c8e55f36STejun Heo 	if (worker) {
1490c8e55f36STejun Heo 		INIT_LIST_HEAD(&worker->entry);
1491affee4b2STejun Heo 		INIT_LIST_HEAD(&worker->scheduled);
149225511a47STejun Heo 		INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn);
1493e22bee78STejun Heo 		/* on creation a worker is in !idle && prep state */
1494e22bee78STejun Heo 		worker->flags = WORKER_PREP;
1495c8e55f36STejun Heo 	}
1496c34056a3STejun Heo 	return worker;
1497c34056a3STejun Heo }
1498c34056a3STejun Heo 
1499c34056a3STejun Heo /**
1500c34056a3STejun Heo  * create_worker - create a new workqueue worker
150163d95a91STejun Heo  * @pool: pool the new worker will belong to
1502c34056a3STejun Heo  *
150363d95a91STejun Heo  * Create a new worker which is bound to @pool.  The returned worker
1504c34056a3STejun Heo  * can be started by calling start_worker() or destroyed using
1505c34056a3STejun Heo  * destroy_worker().
1506c34056a3STejun Heo  *
1507c34056a3STejun Heo  * CONTEXT:
1508c34056a3STejun Heo  * Might sleep.  Does GFP_KERNEL allocations.
1509c34056a3STejun Heo  *
1510c34056a3STejun Heo  * RETURNS:
1511c34056a3STejun Heo  * Pointer to the newly created worker.
1512c34056a3STejun Heo  */
1513bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool)
1514c34056a3STejun Heo {
151563d95a91STejun Heo 	struct global_cwq *gcwq = pool->gcwq;
15163270476aSTejun Heo 	const char *pri = worker_pool_pri(pool) ? "H" : "";
1517c34056a3STejun Heo 	struct worker *worker = NULL;
1518f3421797STejun Heo 	int id = -1;
1519c34056a3STejun Heo 
15208b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
1521bd7bdd43STejun Heo 	while (ida_get_new(&pool->worker_ida, &id)) {
15228b03ae3cSTejun Heo 		spin_unlock_irq(&gcwq->lock);
1523bd7bdd43STejun Heo 		if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
1524c34056a3STejun Heo 			goto fail;
15258b03ae3cSTejun Heo 		spin_lock_irq(&gcwq->lock);
1526c34056a3STejun Heo 	}
15278b03ae3cSTejun Heo 	spin_unlock_irq(&gcwq->lock);
1528c34056a3STejun Heo 
1529c34056a3STejun Heo 	worker = alloc_worker();
1530c34056a3STejun Heo 	if (!worker)
1531c34056a3STejun Heo 		goto fail;
1532c34056a3STejun Heo 
1533bd7bdd43STejun Heo 	worker->pool = pool;
1534c34056a3STejun Heo 	worker->id = id;
1535c34056a3STejun Heo 
1536bc2ae0f5STejun Heo 	if (gcwq->cpu != WORK_CPU_UNBOUND)
153794dcf29aSEric Dumazet 		worker->task = kthread_create_on_node(worker_thread,
15383270476aSTejun Heo 					worker, cpu_to_node(gcwq->cpu),
15393270476aSTejun Heo 					"kworker/%u:%d%s", gcwq->cpu, id, pri);
1540f3421797STejun Heo 	else
1541f3421797STejun Heo 		worker->task = kthread_create(worker_thread, worker,
15423270476aSTejun Heo 					      "kworker/u:%d%s", id, pri);
1543c34056a3STejun Heo 	if (IS_ERR(worker->task))
1544c34056a3STejun Heo 		goto fail;
1545c34056a3STejun Heo 
15463270476aSTejun Heo 	if (worker_pool_pri(pool))
15473270476aSTejun Heo 		set_user_nice(worker->task, HIGHPRI_NICE_LEVEL);
15483270476aSTejun Heo 
1549db7bccf4STejun Heo 	/*
1550bc2ae0f5STejun Heo 	 * Determine CPU binding of the new worker depending on
1551bc2ae0f5STejun Heo 	 * %GCWQ_DISASSOCIATED.  The caller is responsible for ensuring the
1552bc2ae0f5STejun Heo 	 * flag remains stable across this function.  See the comments
1553bc2ae0f5STejun Heo 	 * above the flag definition for details.
1554bc2ae0f5STejun Heo 	 *
1555bc2ae0f5STejun Heo 	 * As an unbound worker may later become a regular one if CPU comes
1556bc2ae0f5STejun Heo 	 * online, make sure every worker has %PF_THREAD_BOUND set.
1557db7bccf4STejun Heo 	 */
1558bc2ae0f5STejun Heo 	if (!(gcwq->flags & GCWQ_DISASSOCIATED)) {
15598b03ae3cSTejun Heo 		kthread_bind(worker->task, gcwq->cpu);
1560bc2ae0f5STejun Heo 	} else {
1561db7bccf4STejun Heo 		worker->task->flags |= PF_THREAD_BOUND;
1562f3421797STejun Heo 		worker->flags |= WORKER_UNBOUND;
1563f3421797STejun Heo 	}
1564c34056a3STejun Heo 
1565c34056a3STejun Heo 	return worker;
1566c34056a3STejun Heo fail:
1567c34056a3STejun Heo 	if (id >= 0) {
15688b03ae3cSTejun Heo 		spin_lock_irq(&gcwq->lock);
1569bd7bdd43STejun Heo 		ida_remove(&pool->worker_ida, id);
15708b03ae3cSTejun Heo 		spin_unlock_irq(&gcwq->lock);
1571c34056a3STejun Heo 	}
1572c34056a3STejun Heo 	kfree(worker);
1573c34056a3STejun Heo 	return NULL;
1574c34056a3STejun Heo }
1575c34056a3STejun Heo 
1576c34056a3STejun Heo /**
1577c34056a3STejun Heo  * start_worker - start a newly created worker
1578c34056a3STejun Heo  * @worker: worker to start
1579c34056a3STejun Heo  *
1580c8e55f36STejun Heo  * Make the gcwq aware of @worker and start it.
1581c34056a3STejun Heo  *
1582c34056a3STejun Heo  * CONTEXT:
15838b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock).
1584c34056a3STejun Heo  */
1585c34056a3STejun Heo static void start_worker(struct worker *worker)
1586c34056a3STejun Heo {
1587cb444766STejun Heo 	worker->flags |= WORKER_STARTED;
1588bd7bdd43STejun Heo 	worker->pool->nr_workers++;
1589c8e55f36STejun Heo 	worker_enter_idle(worker);
1590c34056a3STejun Heo 	wake_up_process(worker->task);
1591c34056a3STejun Heo }
1592c34056a3STejun Heo 
1593c34056a3STejun Heo /**
1594c34056a3STejun Heo  * destroy_worker - destroy a workqueue worker
1595c34056a3STejun Heo  * @worker: worker to be destroyed
1596c34056a3STejun Heo  *
1597c8e55f36STejun Heo  * Destroy @worker and adjust @gcwq stats accordingly.
1598c8e55f36STejun Heo  *
1599c8e55f36STejun Heo  * CONTEXT:
1600c8e55f36STejun Heo  * spin_lock_irq(gcwq->lock) which is released and regrabbed.
1601c34056a3STejun Heo  */
1602c34056a3STejun Heo static void destroy_worker(struct worker *worker)
1603c34056a3STejun Heo {
1604bd7bdd43STejun Heo 	struct worker_pool *pool = worker->pool;
1605bd7bdd43STejun Heo 	struct global_cwq *gcwq = pool->gcwq;
1606c34056a3STejun Heo 	int id = worker->id;
1607c34056a3STejun Heo 
1608c34056a3STejun Heo 	/* sanity check frenzy */
1609c34056a3STejun Heo 	BUG_ON(worker->current_work);
1610affee4b2STejun Heo 	BUG_ON(!list_empty(&worker->scheduled));
1611c34056a3STejun Heo 
1612c8e55f36STejun Heo 	if (worker->flags & WORKER_STARTED)
1613bd7bdd43STejun Heo 		pool->nr_workers--;
1614c8e55f36STejun Heo 	if (worker->flags & WORKER_IDLE)
1615bd7bdd43STejun Heo 		pool->nr_idle--;
1616c8e55f36STejun Heo 
1617c8e55f36STejun Heo 	list_del_init(&worker->entry);
1618cb444766STejun Heo 	worker->flags |= WORKER_DIE;
1619c8e55f36STejun Heo 
1620c8e55f36STejun Heo 	spin_unlock_irq(&gcwq->lock);
1621c8e55f36STejun Heo 
1622c34056a3STejun Heo 	kthread_stop(worker->task);
1623c34056a3STejun Heo 	kfree(worker);
1624c34056a3STejun Heo 
16258b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
1626bd7bdd43STejun Heo 	ida_remove(&pool->worker_ida, id);
1627c34056a3STejun Heo }
1628c34056a3STejun Heo 
162963d95a91STejun Heo static void idle_worker_timeout(unsigned long __pool)
1630e22bee78STejun Heo {
163163d95a91STejun Heo 	struct worker_pool *pool = (void *)__pool;
163263d95a91STejun Heo 	struct global_cwq *gcwq = pool->gcwq;
1633e22bee78STejun Heo 
1634e22bee78STejun Heo 	spin_lock_irq(&gcwq->lock);
1635e22bee78STejun Heo 
163663d95a91STejun Heo 	if (too_many_workers(pool)) {
1637e22bee78STejun Heo 		struct worker *worker;
1638e22bee78STejun Heo 		unsigned long expires;
1639e22bee78STejun Heo 
1640e22bee78STejun Heo 		/* idle_list is kept in LIFO order, check the last one */
164163d95a91STejun Heo 		worker = list_entry(pool->idle_list.prev, struct worker, entry);
1642e22bee78STejun Heo 		expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1643e22bee78STejun Heo 
1644e22bee78STejun Heo 		if (time_before(jiffies, expires))
164563d95a91STejun Heo 			mod_timer(&pool->idle_timer, expires);
1646e22bee78STejun Heo 		else {
1647e22bee78STejun Heo 			/* it's been idle for too long, wake up manager */
164811ebea50STejun Heo 			pool->flags |= POOL_MANAGE_WORKERS;
164963d95a91STejun Heo 			wake_up_worker(pool);
1650e22bee78STejun Heo 		}
1651e22bee78STejun Heo 	}
1652e22bee78STejun Heo 
1653e22bee78STejun Heo 	spin_unlock_irq(&gcwq->lock);
1654e22bee78STejun Heo }
1655e22bee78STejun Heo 
1656e22bee78STejun Heo static bool send_mayday(struct work_struct *work)
1657e22bee78STejun Heo {
1658e22bee78STejun Heo 	struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1659e22bee78STejun Heo 	struct workqueue_struct *wq = cwq->wq;
1660f3421797STejun Heo 	unsigned int cpu;
1661e22bee78STejun Heo 
1662e22bee78STejun Heo 	if (!(wq->flags & WQ_RESCUER))
1663e22bee78STejun Heo 		return false;
1664e22bee78STejun Heo 
1665e22bee78STejun Heo 	/* mayday mayday mayday */
1666bd7bdd43STejun Heo 	cpu = cwq->pool->gcwq->cpu;
1667f3421797STejun Heo 	/* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1668f3421797STejun Heo 	if (cpu == WORK_CPU_UNBOUND)
1669f3421797STejun Heo 		cpu = 0;
1670f2e005aaSTejun Heo 	if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
1671e22bee78STejun Heo 		wake_up_process(wq->rescuer->task);
1672e22bee78STejun Heo 	return true;
1673e22bee78STejun Heo }
1674e22bee78STejun Heo 
167563d95a91STejun Heo static void gcwq_mayday_timeout(unsigned long __pool)
1676e22bee78STejun Heo {
167763d95a91STejun Heo 	struct worker_pool *pool = (void *)__pool;
167863d95a91STejun Heo 	struct global_cwq *gcwq = pool->gcwq;
1679e22bee78STejun Heo 	struct work_struct *work;
1680e22bee78STejun Heo 
1681e22bee78STejun Heo 	spin_lock_irq(&gcwq->lock);
1682e22bee78STejun Heo 
168363d95a91STejun Heo 	if (need_to_create_worker(pool)) {
1684e22bee78STejun Heo 		/*
1685e22bee78STejun Heo 		 * We've been trying to create a new worker but
1686e22bee78STejun Heo 		 * haven't been successful.  We might be hitting an
1687e22bee78STejun Heo 		 * allocation deadlock.  Send distress signals to
1688e22bee78STejun Heo 		 * rescuers.
1689e22bee78STejun Heo 		 */
169063d95a91STejun Heo 		list_for_each_entry(work, &pool->worklist, entry)
1691e22bee78STejun Heo 			send_mayday(work);
1692e22bee78STejun Heo 	}
1693e22bee78STejun Heo 
1694e22bee78STejun Heo 	spin_unlock_irq(&gcwq->lock);
1695e22bee78STejun Heo 
169663d95a91STejun Heo 	mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
1697e22bee78STejun Heo }
1698e22bee78STejun Heo 
1699e22bee78STejun Heo /**
1700e22bee78STejun Heo  * maybe_create_worker - create a new worker if necessary
170163d95a91STejun Heo  * @pool: pool to create a new worker for
1702e22bee78STejun Heo  *
170363d95a91STejun Heo  * Create a new worker for @pool if necessary.  @pool is guaranteed to
1704e22bee78STejun Heo  * have at least one idle worker on return from this function.  If
1705e22bee78STejun Heo  * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
170663d95a91STejun Heo  * sent to all rescuers with works scheduled on @pool to resolve
1707e22bee78STejun Heo  * possible allocation deadlock.
1708e22bee78STejun Heo  *
1709e22bee78STejun Heo  * On return, need_to_create_worker() is guaranteed to be false and
1710e22bee78STejun Heo  * may_start_working() true.
1711e22bee78STejun Heo  *
1712e22bee78STejun Heo  * LOCKING:
1713e22bee78STejun Heo  * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1714e22bee78STejun Heo  * multiple times.  Does GFP_KERNEL allocations.  Called only from
1715e22bee78STejun Heo  * manager.
1716e22bee78STejun Heo  *
1717e22bee78STejun Heo  * RETURNS:
1718e22bee78STejun Heo  * false if no action was taken and gcwq->lock stayed locked, true
1719e22bee78STejun Heo  * otherwise.
1720e22bee78STejun Heo  */
172163d95a91STejun Heo static bool maybe_create_worker(struct worker_pool *pool)
172206bd6ebfSNamhyung Kim __releases(&gcwq->lock)
172306bd6ebfSNamhyung Kim __acquires(&gcwq->lock)
1724e22bee78STejun Heo {
172563d95a91STejun Heo 	struct global_cwq *gcwq = pool->gcwq;
172663d95a91STejun Heo 
172763d95a91STejun Heo 	if (!need_to_create_worker(pool))
1728e22bee78STejun Heo 		return false;
1729e22bee78STejun Heo restart:
17309f9c2364STejun Heo 	spin_unlock_irq(&gcwq->lock);
17319f9c2364STejun Heo 
1732e22bee78STejun Heo 	/* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
173363d95a91STejun Heo 	mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
1734e22bee78STejun Heo 
1735e22bee78STejun Heo 	while (true) {
1736e22bee78STejun Heo 		struct worker *worker;
1737e22bee78STejun Heo 
1738bc2ae0f5STejun Heo 		worker = create_worker(pool);
1739e22bee78STejun Heo 		if (worker) {
174063d95a91STejun Heo 			del_timer_sync(&pool->mayday_timer);
1741e22bee78STejun Heo 			spin_lock_irq(&gcwq->lock);
1742e22bee78STejun Heo 			start_worker(worker);
174363d95a91STejun Heo 			BUG_ON(need_to_create_worker(pool));
1744e22bee78STejun Heo 			return true;
1745e22bee78STejun Heo 		}
1746e22bee78STejun Heo 
174763d95a91STejun Heo 		if (!need_to_create_worker(pool))
1748e22bee78STejun Heo 			break;
1749e22bee78STejun Heo 
1750e22bee78STejun Heo 		__set_current_state(TASK_INTERRUPTIBLE);
1751e22bee78STejun Heo 		schedule_timeout(CREATE_COOLDOWN);
17529f9c2364STejun Heo 
175363d95a91STejun Heo 		if (!need_to_create_worker(pool))
1754e22bee78STejun Heo 			break;
1755e22bee78STejun Heo 	}
1756e22bee78STejun Heo 
175763d95a91STejun Heo 	del_timer_sync(&pool->mayday_timer);
1758e22bee78STejun Heo 	spin_lock_irq(&gcwq->lock);
175963d95a91STejun Heo 	if (need_to_create_worker(pool))
1760e22bee78STejun Heo 		goto restart;
1761e22bee78STejun Heo 	return true;
1762e22bee78STejun Heo }
1763e22bee78STejun Heo 
1764e22bee78STejun Heo /**
1765e22bee78STejun Heo  * maybe_destroy_worker - destroy workers which have been idle for a while
176663d95a91STejun Heo  * @pool: pool to destroy workers for
1767e22bee78STejun Heo  *
176863d95a91STejun Heo  * Destroy @pool workers which have been idle for longer than
1769e22bee78STejun Heo  * IDLE_WORKER_TIMEOUT.
1770e22bee78STejun Heo  *
1771e22bee78STejun Heo  * LOCKING:
1772e22bee78STejun Heo  * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1773e22bee78STejun Heo  * multiple times.  Called only from manager.
1774e22bee78STejun Heo  *
1775e22bee78STejun Heo  * RETURNS:
1776e22bee78STejun Heo  * false if no action was taken and gcwq->lock stayed locked, true
1777e22bee78STejun Heo  * otherwise.
1778e22bee78STejun Heo  */
177963d95a91STejun Heo static bool maybe_destroy_workers(struct worker_pool *pool)
1780e22bee78STejun Heo {
1781e22bee78STejun Heo 	bool ret = false;
1782e22bee78STejun Heo 
178363d95a91STejun Heo 	while (too_many_workers(pool)) {
1784e22bee78STejun Heo 		struct worker *worker;
1785e22bee78STejun Heo 		unsigned long expires;
1786e22bee78STejun Heo 
178763d95a91STejun Heo 		worker = list_entry(pool->idle_list.prev, struct worker, entry);
1788e22bee78STejun Heo 		expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1789e22bee78STejun Heo 
1790e22bee78STejun Heo 		if (time_before(jiffies, expires)) {
179163d95a91STejun Heo 			mod_timer(&pool->idle_timer, expires);
1792e22bee78STejun Heo 			break;
1793e22bee78STejun Heo 		}
1794e22bee78STejun Heo 
1795e22bee78STejun Heo 		destroy_worker(worker);
1796e22bee78STejun Heo 		ret = true;
1797e22bee78STejun Heo 	}
1798e22bee78STejun Heo 
1799e22bee78STejun Heo 	return ret;
1800e22bee78STejun Heo }
1801e22bee78STejun Heo 
1802e22bee78STejun Heo /**
1803e22bee78STejun Heo  * manage_workers - manage worker pool
1804e22bee78STejun Heo  * @worker: self
1805e22bee78STejun Heo  *
1806e22bee78STejun Heo  * Assume the manager role and manage gcwq worker pool @worker belongs
1807e22bee78STejun Heo  * to.  At any given time, there can be only zero or one manager per
1808e22bee78STejun Heo  * gcwq.  The exclusion is handled automatically by this function.
1809e22bee78STejun Heo  *
1810e22bee78STejun Heo  * The caller can safely start processing works on false return.  On
1811e22bee78STejun Heo  * true return, it's guaranteed that need_to_create_worker() is false
1812e22bee78STejun Heo  * and may_start_working() is true.
1813e22bee78STejun Heo  *
1814e22bee78STejun Heo  * CONTEXT:
1815e22bee78STejun Heo  * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1816e22bee78STejun Heo  * multiple times.  Does GFP_KERNEL allocations.
1817e22bee78STejun Heo  *
1818e22bee78STejun Heo  * RETURNS:
1819e22bee78STejun Heo  * false if no action was taken and gcwq->lock stayed locked, true if
1820e22bee78STejun Heo  * some action was taken.
1821e22bee78STejun Heo  */
1822e22bee78STejun Heo static bool manage_workers(struct worker *worker)
1823e22bee78STejun Heo {
182463d95a91STejun Heo 	struct worker_pool *pool = worker->pool;
1825e22bee78STejun Heo 	bool ret = false;
1826e22bee78STejun Heo 
182760373152STejun Heo 	if (!mutex_trylock(&pool->manager_mutex))
1828e22bee78STejun Heo 		return ret;
1829e22bee78STejun Heo 
183011ebea50STejun Heo 	pool->flags &= ~POOL_MANAGE_WORKERS;
1831e22bee78STejun Heo 
1832e22bee78STejun Heo 	/*
1833e22bee78STejun Heo 	 * Destroy and then create so that may_start_working() is true
1834e22bee78STejun Heo 	 * on return.
1835e22bee78STejun Heo 	 */
183663d95a91STejun Heo 	ret |= maybe_destroy_workers(pool);
183763d95a91STejun Heo 	ret |= maybe_create_worker(pool);
1838e22bee78STejun Heo 
183960373152STejun Heo 	mutex_unlock(&pool->manager_mutex);
1840e22bee78STejun Heo 	return ret;
1841e22bee78STejun Heo }
1842e22bee78STejun Heo 
1843a62428c0STejun Heo /**
1844affee4b2STejun Heo  * move_linked_works - move linked works to a list
1845affee4b2STejun Heo  * @work: start of series of works to be scheduled
1846affee4b2STejun Heo  * @head: target list to append @work to
1847affee4b2STejun Heo  * @nextp: out paramter for nested worklist walking
1848affee4b2STejun Heo  *
1849affee4b2STejun Heo  * Schedule linked works starting from @work to @head.  Work series to
1850affee4b2STejun Heo  * be scheduled starts at @work and includes any consecutive work with
1851affee4b2STejun Heo  * WORK_STRUCT_LINKED set in its predecessor.
1852affee4b2STejun Heo  *
1853affee4b2STejun Heo  * If @nextp is not NULL, it's updated to point to the next work of
1854affee4b2STejun Heo  * the last scheduled work.  This allows move_linked_works() to be
1855affee4b2STejun Heo  * nested inside outer list_for_each_entry_safe().
1856affee4b2STejun Heo  *
1857affee4b2STejun Heo  * CONTEXT:
18588b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock).
1859affee4b2STejun Heo  */
1860affee4b2STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head,
1861affee4b2STejun Heo 			      struct work_struct **nextp)
1862affee4b2STejun Heo {
1863affee4b2STejun Heo 	struct work_struct *n;
1864affee4b2STejun Heo 
1865affee4b2STejun Heo 	/*
1866affee4b2STejun Heo 	 * Linked worklist will always end before the end of the list,
1867affee4b2STejun Heo 	 * use NULL for list head.
1868affee4b2STejun Heo 	 */
1869affee4b2STejun Heo 	list_for_each_entry_safe_from(work, n, NULL, entry) {
1870affee4b2STejun Heo 		list_move_tail(&work->entry, head);
1871affee4b2STejun Heo 		if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1872affee4b2STejun Heo 			break;
1873affee4b2STejun Heo 	}
1874affee4b2STejun Heo 
1875affee4b2STejun Heo 	/*
1876affee4b2STejun Heo 	 * If we're already inside safe list traversal and have moved
1877affee4b2STejun Heo 	 * multiple works to the scheduled queue, the next position
1878affee4b2STejun Heo 	 * needs to be updated.
1879affee4b2STejun Heo 	 */
1880affee4b2STejun Heo 	if (nextp)
1881affee4b2STejun Heo 		*nextp = n;
1882affee4b2STejun Heo }
1883affee4b2STejun Heo 
18841e19ffc6STejun Heo static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
18851e19ffc6STejun Heo {
18861e19ffc6STejun Heo 	struct work_struct *work = list_first_entry(&cwq->delayed_works,
18871da177e4SLinus Torvalds 						    struct work_struct, entry);
18881e19ffc6STejun Heo 
1889cdadf009STejun Heo 	trace_workqueue_activate_work(work);
18903270476aSTejun Heo 	move_linked_works(work, &cwq->pool->worklist, NULL);
18918a2e8e5dSTejun Heo 	__clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
18921e19ffc6STejun Heo 	cwq->nr_active++;
18931e19ffc6STejun Heo }
18941e19ffc6STejun Heo 
1895affee4b2STejun Heo /**
189673f53c4aSTejun Heo  * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
189773f53c4aSTejun Heo  * @cwq: cwq of interest
189873f53c4aSTejun Heo  * @color: color of work which left the queue
18998a2e8e5dSTejun Heo  * @delayed: for a delayed work
190073f53c4aSTejun Heo  *
190173f53c4aSTejun Heo  * A work either has completed or is removed from pending queue,
190273f53c4aSTejun Heo  * decrement nr_in_flight of its cwq and handle workqueue flushing.
190373f53c4aSTejun Heo  *
190473f53c4aSTejun Heo  * CONTEXT:
19058b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock).
190673f53c4aSTejun Heo  */
19078a2e8e5dSTejun Heo static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
19088a2e8e5dSTejun Heo 				 bool delayed)
190973f53c4aSTejun Heo {
191073f53c4aSTejun Heo 	/* ignore uncolored works */
191173f53c4aSTejun Heo 	if (color == WORK_NO_COLOR)
191273f53c4aSTejun Heo 		return;
191373f53c4aSTejun Heo 
191473f53c4aSTejun Heo 	cwq->nr_in_flight[color]--;
19151e19ffc6STejun Heo 
19168a2e8e5dSTejun Heo 	if (!delayed) {
19178a2e8e5dSTejun Heo 		cwq->nr_active--;
1918502ca9d8STejun Heo 		if (!list_empty(&cwq->delayed_works)) {
19191e19ffc6STejun Heo 			/* one down, submit a delayed one */
1920502ca9d8STejun Heo 			if (cwq->nr_active < cwq->max_active)
19211e19ffc6STejun Heo 				cwq_activate_first_delayed(cwq);
1922502ca9d8STejun Heo 		}
19238a2e8e5dSTejun Heo 	}
192473f53c4aSTejun Heo 
192573f53c4aSTejun Heo 	/* is flush in progress and are we at the flushing tip? */
192673f53c4aSTejun Heo 	if (likely(cwq->flush_color != color))
192773f53c4aSTejun Heo 		return;
192873f53c4aSTejun Heo 
192973f53c4aSTejun Heo 	/* are there still in-flight works? */
193073f53c4aSTejun Heo 	if (cwq->nr_in_flight[color])
193173f53c4aSTejun Heo 		return;
193273f53c4aSTejun Heo 
193373f53c4aSTejun Heo 	/* this cwq is done, clear flush_color */
193473f53c4aSTejun Heo 	cwq->flush_color = -1;
193573f53c4aSTejun Heo 
193673f53c4aSTejun Heo 	/*
193773f53c4aSTejun Heo 	 * If this was the last cwq, wake up the first flusher.  It
193873f53c4aSTejun Heo 	 * will handle the rest.
193973f53c4aSTejun Heo 	 */
194073f53c4aSTejun Heo 	if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
194173f53c4aSTejun Heo 		complete(&cwq->wq->first_flusher->done);
194273f53c4aSTejun Heo }
194373f53c4aSTejun Heo 
194473f53c4aSTejun Heo /**
1945a62428c0STejun Heo  * process_one_work - process single work
1946c34056a3STejun Heo  * @worker: self
1947a62428c0STejun Heo  * @work: work to process
1948a62428c0STejun Heo  *
1949a62428c0STejun Heo  * Process @work.  This function contains all the logics necessary to
1950a62428c0STejun Heo  * process a single work including synchronization against and
1951a62428c0STejun Heo  * interaction with other workers on the same cpu, queueing and
1952a62428c0STejun Heo  * flushing.  As long as context requirement is met, any worker can
1953a62428c0STejun Heo  * call this function to process a work.
1954a62428c0STejun Heo  *
1955a62428c0STejun Heo  * CONTEXT:
19568b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock) which is released and regrabbed.
1957a62428c0STejun Heo  */
1958c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work)
195906bd6ebfSNamhyung Kim __releases(&gcwq->lock)
196006bd6ebfSNamhyung Kim __acquires(&gcwq->lock)
19611da177e4SLinus Torvalds {
19627e11629dSTejun Heo 	struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1963bd7bdd43STejun Heo 	struct worker_pool *pool = worker->pool;
1964bd7bdd43STejun Heo 	struct global_cwq *gcwq = pool->gcwq;
1965c8e55f36STejun Heo 	struct hlist_head *bwh = busy_worker_head(gcwq, work);
1966fb0e7bebSTejun Heo 	bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
19676bb49e59SDavid Howells 	work_func_t f = work->func;
196873f53c4aSTejun Heo 	int work_color;
19697e11629dSTejun Heo 	struct worker *collision;
19704e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
19714e6045f1SJohannes Berg 	/*
1972a62428c0STejun Heo 	 * It is permissible to free the struct work_struct from
1973a62428c0STejun Heo 	 * inside the function that is called from it, this we need to
1974a62428c0STejun Heo 	 * take into account for lockdep too.  To avoid bogus "held
1975a62428c0STejun Heo 	 * lock freed" warnings as well as problems when looking into
1976a62428c0STejun Heo 	 * work->lockdep_map, make a copy and use that here.
19774e6045f1SJohannes Berg 	 */
19784d82a1deSPeter Zijlstra 	struct lockdep_map lockdep_map;
19794d82a1deSPeter Zijlstra 
19804d82a1deSPeter Zijlstra 	lockdep_copy_map(&lockdep_map, &work->lockdep_map);
19814e6045f1SJohannes Berg #endif
19826fec10a1STejun Heo 	/*
19836fec10a1STejun Heo 	 * Ensure we're on the correct CPU.  DISASSOCIATED test is
19846fec10a1STejun Heo 	 * necessary to avoid spurious warnings from rescuers servicing the
19856fec10a1STejun Heo 	 * unbound or a disassociated gcwq.
19866fec10a1STejun Heo 	 */
198725511a47STejun Heo 	WARN_ON_ONCE(!(worker->flags & (WORKER_UNBOUND | WORKER_REBIND)) &&
19886fec10a1STejun Heo 		     !(gcwq->flags & GCWQ_DISASSOCIATED) &&
198925511a47STejun Heo 		     raw_smp_processor_id() != gcwq->cpu);
199025511a47STejun Heo 
19917e11629dSTejun Heo 	/*
19927e11629dSTejun Heo 	 * A single work shouldn't be executed concurrently by
19937e11629dSTejun Heo 	 * multiple workers on a single cpu.  Check whether anyone is
19947e11629dSTejun Heo 	 * already processing the work.  If so, defer the work to the
19957e11629dSTejun Heo 	 * currently executing one.
19967e11629dSTejun Heo 	 */
19977e11629dSTejun Heo 	collision = __find_worker_executing_work(gcwq, bwh, work);
19987e11629dSTejun Heo 	if (unlikely(collision)) {
19997e11629dSTejun Heo 		move_linked_works(work, &collision->scheduled, NULL);
20007e11629dSTejun Heo 		return;
20017e11629dSTejun Heo 	}
20021da177e4SLinus Torvalds 
2003a62428c0STejun Heo 	/* claim and process */
20041da177e4SLinus Torvalds 	debug_work_deactivate(work);
2005c8e55f36STejun Heo 	hlist_add_head(&worker->hentry, bwh);
2006c34056a3STejun Heo 	worker->current_work = work;
20078cca0eeaSTejun Heo 	worker->current_cwq = cwq;
200873f53c4aSTejun Heo 	work_color = get_work_color(work);
20097a22ad75STejun Heo 
20107a22ad75STejun Heo 	/* record the current cpu number in the work data and dequeue */
20117a22ad75STejun Heo 	set_work_cpu(work, gcwq->cpu);
2012a62428c0STejun Heo 	list_del_init(&work->entry);
2013a62428c0STejun Heo 
2014649027d7STejun Heo 	/*
2015fb0e7bebSTejun Heo 	 * CPU intensive works don't participate in concurrency
2016fb0e7bebSTejun Heo 	 * management.  They're the scheduler's responsibility.
2017fb0e7bebSTejun Heo 	 */
2018fb0e7bebSTejun Heo 	if (unlikely(cpu_intensive))
2019fb0e7bebSTejun Heo 		worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2020fb0e7bebSTejun Heo 
2021974271c4STejun Heo 	/*
2022974271c4STejun Heo 	 * Unbound gcwq isn't concurrency managed and work items should be
2023974271c4STejun Heo 	 * executed ASAP.  Wake up another worker if necessary.
2024974271c4STejun Heo 	 */
202563d95a91STejun Heo 	if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
202663d95a91STejun Heo 		wake_up_worker(pool);
2027974271c4STejun Heo 
20288b03ae3cSTejun Heo 	spin_unlock_irq(&gcwq->lock);
20291da177e4SLinus Torvalds 
203023b2e599SOleg Nesterov 	work_clear_pending(work);
2031e159489bSTejun Heo 	lock_map_acquire_read(&cwq->wq->lockdep_map);
20323295f0efSIngo Molnar 	lock_map_acquire(&lockdep_map);
2033e36c886aSArjan van de Ven 	trace_workqueue_execute_start(work);
203465f27f38SDavid Howells 	f(work);
2035e36c886aSArjan van de Ven 	/*
2036e36c886aSArjan van de Ven 	 * While we must be careful to not use "work" after this, the trace
2037e36c886aSArjan van de Ven 	 * point will only record its address.
2038e36c886aSArjan van de Ven 	 */
2039e36c886aSArjan van de Ven 	trace_workqueue_execute_end(work);
20403295f0efSIngo Molnar 	lock_map_release(&lockdep_map);
20413295f0efSIngo Molnar 	lock_map_release(&cwq->wq->lockdep_map);
20421da177e4SLinus Torvalds 
2043d5abe669SPeter Zijlstra 	if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
2044d5abe669SPeter Zijlstra 		printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
2045d5abe669SPeter Zijlstra 		       "%s/0x%08x/%d\n",
2046a62428c0STejun Heo 		       current->comm, preempt_count(), task_pid_nr(current));
2047d5abe669SPeter Zijlstra 		printk(KERN_ERR "    last function: ");
2048d5abe669SPeter Zijlstra 		print_symbol("%s\n", (unsigned long)f);
2049d5abe669SPeter Zijlstra 		debug_show_held_locks(current);
2050d5abe669SPeter Zijlstra 		dump_stack();
2051d5abe669SPeter Zijlstra 	}
2052d5abe669SPeter Zijlstra 
20538b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
2054a62428c0STejun Heo 
2055fb0e7bebSTejun Heo 	/* clear cpu intensive status */
2056fb0e7bebSTejun Heo 	if (unlikely(cpu_intensive))
2057fb0e7bebSTejun Heo 		worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2058fb0e7bebSTejun Heo 
2059a62428c0STejun Heo 	/* we're done with it, release */
2060c8e55f36STejun Heo 	hlist_del_init(&worker->hentry);
2061c34056a3STejun Heo 	worker->current_work = NULL;
20628cca0eeaSTejun Heo 	worker->current_cwq = NULL;
20638a2e8e5dSTejun Heo 	cwq_dec_nr_in_flight(cwq, work_color, false);
20641da177e4SLinus Torvalds }
20651da177e4SLinus Torvalds 
2066affee4b2STejun Heo /**
2067affee4b2STejun Heo  * process_scheduled_works - process scheduled works
2068affee4b2STejun Heo  * @worker: self
2069affee4b2STejun Heo  *
2070affee4b2STejun Heo  * Process all scheduled works.  Please note that the scheduled list
2071affee4b2STejun Heo  * may change while processing a work, so this function repeatedly
2072affee4b2STejun Heo  * fetches a work from the top and executes it.
2073affee4b2STejun Heo  *
2074affee4b2STejun Heo  * CONTEXT:
20758b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock) which may be released and regrabbed
2076affee4b2STejun Heo  * multiple times.
2077affee4b2STejun Heo  */
2078affee4b2STejun Heo static void process_scheduled_works(struct worker *worker)
20791da177e4SLinus Torvalds {
2080affee4b2STejun Heo 	while (!list_empty(&worker->scheduled)) {
2081affee4b2STejun Heo 		struct work_struct *work = list_first_entry(&worker->scheduled,
2082a62428c0STejun Heo 						struct work_struct, entry);
2083c34056a3STejun Heo 		process_one_work(worker, work);
2084a62428c0STejun Heo 	}
20851da177e4SLinus Torvalds }
20861da177e4SLinus Torvalds 
20874690c4abSTejun Heo /**
20884690c4abSTejun Heo  * worker_thread - the worker thread function
2089c34056a3STejun Heo  * @__worker: self
20904690c4abSTejun Heo  *
2091e22bee78STejun Heo  * The gcwq worker thread function.  There's a single dynamic pool of
2092e22bee78STejun Heo  * these per each cpu.  These workers process all works regardless of
2093e22bee78STejun Heo  * their specific target workqueue.  The only exception is works which
2094e22bee78STejun Heo  * belong to workqueues with a rescuer which will be explained in
2095e22bee78STejun Heo  * rescuer_thread().
20964690c4abSTejun Heo  */
2097c34056a3STejun Heo static int worker_thread(void *__worker)
20981da177e4SLinus Torvalds {
2099c34056a3STejun Heo 	struct worker *worker = __worker;
2100bd7bdd43STejun Heo 	struct worker_pool *pool = worker->pool;
2101bd7bdd43STejun Heo 	struct global_cwq *gcwq = pool->gcwq;
21021da177e4SLinus Torvalds 
2103e22bee78STejun Heo 	/* tell the scheduler that this is a workqueue worker */
2104e22bee78STejun Heo 	worker->task->flags |= PF_WQ_WORKER;
2105c8e55f36STejun Heo woke_up:
21068b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
2107affee4b2STejun Heo 
210825511a47STejun Heo 	/*
210925511a47STejun Heo 	 * DIE can be set only while idle and REBIND set while busy has
211025511a47STejun Heo 	 * @worker->rebind_work scheduled.  Checking here is enough.
211125511a47STejun Heo 	 */
211225511a47STejun Heo 	if (unlikely(worker->flags & (WORKER_REBIND | WORKER_DIE))) {
2113c8e55f36STejun Heo 		spin_unlock_irq(&gcwq->lock);
211425511a47STejun Heo 
211525511a47STejun Heo 		if (worker->flags & WORKER_DIE) {
2116e22bee78STejun Heo 			worker->task->flags &= ~PF_WQ_WORKER;
2117c8e55f36STejun Heo 			return 0;
2118c8e55f36STejun Heo 		}
2119c8e55f36STejun Heo 
212025511a47STejun Heo 		idle_worker_rebind(worker);
212125511a47STejun Heo 		goto woke_up;
212225511a47STejun Heo 	}
212325511a47STejun Heo 
2124c8e55f36STejun Heo 	worker_leave_idle(worker);
2125db7bccf4STejun Heo recheck:
2126e22bee78STejun Heo 	/* no more worker necessary? */
212763d95a91STejun Heo 	if (!need_more_worker(pool))
2128e22bee78STejun Heo 		goto sleep;
2129e22bee78STejun Heo 
2130e22bee78STejun Heo 	/* do we need to manage? */
213163d95a91STejun Heo 	if (unlikely(!may_start_working(pool)) && manage_workers(worker))
2132e22bee78STejun Heo 		goto recheck;
2133e22bee78STejun Heo 
2134c8e55f36STejun Heo 	/*
2135c8e55f36STejun Heo 	 * ->scheduled list can only be filled while a worker is
2136c8e55f36STejun Heo 	 * preparing to process a work or actually processing it.
2137c8e55f36STejun Heo 	 * Make sure nobody diddled with it while I was sleeping.
2138c8e55f36STejun Heo 	 */
2139c8e55f36STejun Heo 	BUG_ON(!list_empty(&worker->scheduled));
2140c8e55f36STejun Heo 
2141e22bee78STejun Heo 	/*
2142e22bee78STejun Heo 	 * When control reaches this point, we're guaranteed to have
2143e22bee78STejun Heo 	 * at least one idle worker or that someone else has already
2144e22bee78STejun Heo 	 * assumed the manager role.
2145e22bee78STejun Heo 	 */
2146e22bee78STejun Heo 	worker_clr_flags(worker, WORKER_PREP);
2147e22bee78STejun Heo 
2148e22bee78STejun Heo 	do {
2149affee4b2STejun Heo 		struct work_struct *work =
2150bd7bdd43STejun Heo 			list_first_entry(&pool->worklist,
2151affee4b2STejun Heo 					 struct work_struct, entry);
2152affee4b2STejun Heo 
2153c8e55f36STejun Heo 		if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2154affee4b2STejun Heo 			/* optimization path, not strictly necessary */
2155affee4b2STejun Heo 			process_one_work(worker, work);
2156affee4b2STejun Heo 			if (unlikely(!list_empty(&worker->scheduled)))
2157affee4b2STejun Heo 				process_scheduled_works(worker);
2158affee4b2STejun Heo 		} else {
2159c8e55f36STejun Heo 			move_linked_works(work, &worker->scheduled, NULL);
2160affee4b2STejun Heo 			process_scheduled_works(worker);
2161affee4b2STejun Heo 		}
216263d95a91STejun Heo 	} while (keep_working(pool));
2163affee4b2STejun Heo 
2164e22bee78STejun Heo 	worker_set_flags(worker, WORKER_PREP, false);
2165d313dd85STejun Heo sleep:
216663d95a91STejun Heo 	if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
2167e22bee78STejun Heo 		goto recheck;
2168d313dd85STejun Heo 
2169c8e55f36STejun Heo 	/*
2170e22bee78STejun Heo 	 * gcwq->lock is held and there's no work to process and no
2171e22bee78STejun Heo 	 * need to manage, sleep.  Workers are woken up only while
2172e22bee78STejun Heo 	 * holding gcwq->lock or from local cpu, so setting the
2173e22bee78STejun Heo 	 * current state before releasing gcwq->lock is enough to
2174e22bee78STejun Heo 	 * prevent losing any event.
2175c8e55f36STejun Heo 	 */
2176c8e55f36STejun Heo 	worker_enter_idle(worker);
2177c8e55f36STejun Heo 	__set_current_state(TASK_INTERRUPTIBLE);
21788b03ae3cSTejun Heo 	spin_unlock_irq(&gcwq->lock);
21791da177e4SLinus Torvalds 	schedule();
2180c8e55f36STejun Heo 	goto woke_up;
21811da177e4SLinus Torvalds }
21821da177e4SLinus Torvalds 
2183e22bee78STejun Heo /**
2184e22bee78STejun Heo  * rescuer_thread - the rescuer thread function
2185e22bee78STejun Heo  * @__wq: the associated workqueue
2186e22bee78STejun Heo  *
2187e22bee78STejun Heo  * Workqueue rescuer thread function.  There's one rescuer for each
2188e22bee78STejun Heo  * workqueue which has WQ_RESCUER set.
2189e22bee78STejun Heo  *
2190e22bee78STejun Heo  * Regular work processing on a gcwq may block trying to create a new
2191e22bee78STejun Heo  * worker which uses GFP_KERNEL allocation which has slight chance of
2192e22bee78STejun Heo  * developing into deadlock if some works currently on the same queue
2193e22bee78STejun Heo  * need to be processed to satisfy the GFP_KERNEL allocation.  This is
2194e22bee78STejun Heo  * the problem rescuer solves.
2195e22bee78STejun Heo  *
2196e22bee78STejun Heo  * When such condition is possible, the gcwq summons rescuers of all
2197e22bee78STejun Heo  * workqueues which have works queued on the gcwq and let them process
2198e22bee78STejun Heo  * those works so that forward progress can be guaranteed.
2199e22bee78STejun Heo  *
2200e22bee78STejun Heo  * This should happen rarely.
2201e22bee78STejun Heo  */
2202e22bee78STejun Heo static int rescuer_thread(void *__wq)
2203e22bee78STejun Heo {
2204e22bee78STejun Heo 	struct workqueue_struct *wq = __wq;
2205e22bee78STejun Heo 	struct worker *rescuer = wq->rescuer;
2206e22bee78STejun Heo 	struct list_head *scheduled = &rescuer->scheduled;
2207f3421797STejun Heo 	bool is_unbound = wq->flags & WQ_UNBOUND;
2208e22bee78STejun Heo 	unsigned int cpu;
2209e22bee78STejun Heo 
2210e22bee78STejun Heo 	set_user_nice(current, RESCUER_NICE_LEVEL);
2211e22bee78STejun Heo repeat:
2212e22bee78STejun Heo 	set_current_state(TASK_INTERRUPTIBLE);
22131da177e4SLinus Torvalds 
22141da177e4SLinus Torvalds 	if (kthread_should_stop())
2215e22bee78STejun Heo 		return 0;
22161da177e4SLinus Torvalds 
2217f3421797STejun Heo 	/*
2218f3421797STejun Heo 	 * See whether any cpu is asking for help.  Unbounded
2219f3421797STejun Heo 	 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2220f3421797STejun Heo 	 */
2221f2e005aaSTejun Heo 	for_each_mayday_cpu(cpu, wq->mayday_mask) {
2222f3421797STejun Heo 		unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2223f3421797STejun Heo 		struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
2224bd7bdd43STejun Heo 		struct worker_pool *pool = cwq->pool;
2225bd7bdd43STejun Heo 		struct global_cwq *gcwq = pool->gcwq;
2226e22bee78STejun Heo 		struct work_struct *work, *n;
2227e22bee78STejun Heo 
2228e22bee78STejun Heo 		__set_current_state(TASK_RUNNING);
2229f2e005aaSTejun Heo 		mayday_clear_cpu(cpu, wq->mayday_mask);
2230e22bee78STejun Heo 
2231e22bee78STejun Heo 		/* migrate to the target cpu if possible */
2232bd7bdd43STejun Heo 		rescuer->pool = pool;
2233e22bee78STejun Heo 		worker_maybe_bind_and_lock(rescuer);
2234e22bee78STejun Heo 
2235e22bee78STejun Heo 		/*
2236e22bee78STejun Heo 		 * Slurp in all works issued via this workqueue and
2237e22bee78STejun Heo 		 * process'em.
2238e22bee78STejun Heo 		 */
2239e22bee78STejun Heo 		BUG_ON(!list_empty(&rescuer->scheduled));
2240bd7bdd43STejun Heo 		list_for_each_entry_safe(work, n, &pool->worklist, entry)
2241e22bee78STejun Heo 			if (get_work_cwq(work) == cwq)
2242e22bee78STejun Heo 				move_linked_works(work, scheduled, &n);
2243e22bee78STejun Heo 
2244e22bee78STejun Heo 		process_scheduled_works(rescuer);
22457576958aSTejun Heo 
22467576958aSTejun Heo 		/*
22477576958aSTejun Heo 		 * Leave this gcwq.  If keep_working() is %true, notify a
22487576958aSTejun Heo 		 * regular worker; otherwise, we end up with 0 concurrency
22497576958aSTejun Heo 		 * and stalling the execution.
22507576958aSTejun Heo 		 */
225163d95a91STejun Heo 		if (keep_working(pool))
225263d95a91STejun Heo 			wake_up_worker(pool);
22537576958aSTejun Heo 
2254e22bee78STejun Heo 		spin_unlock_irq(&gcwq->lock);
22551da177e4SLinus Torvalds 	}
22561da177e4SLinus Torvalds 
2257e22bee78STejun Heo 	schedule();
2258e22bee78STejun Heo 	goto repeat;
22591da177e4SLinus Torvalds }
22601da177e4SLinus Torvalds 
2261fc2e4d70SOleg Nesterov struct wq_barrier {
2262fc2e4d70SOleg Nesterov 	struct work_struct	work;
2263fc2e4d70SOleg Nesterov 	struct completion	done;
2264fc2e4d70SOleg Nesterov };
2265fc2e4d70SOleg Nesterov 
2266fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work)
2267fc2e4d70SOleg Nesterov {
2268fc2e4d70SOleg Nesterov 	struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2269fc2e4d70SOleg Nesterov 	complete(&barr->done);
2270fc2e4d70SOleg Nesterov }
2271fc2e4d70SOleg Nesterov 
22724690c4abSTejun Heo /**
22734690c4abSTejun Heo  * insert_wq_barrier - insert a barrier work
22744690c4abSTejun Heo  * @cwq: cwq to insert barrier into
22754690c4abSTejun Heo  * @barr: wq_barrier to insert
2276affee4b2STejun Heo  * @target: target work to attach @barr to
2277affee4b2STejun Heo  * @worker: worker currently executing @target, NULL if @target is not executing
22784690c4abSTejun Heo  *
2279affee4b2STejun Heo  * @barr is linked to @target such that @barr is completed only after
2280affee4b2STejun Heo  * @target finishes execution.  Please note that the ordering
2281affee4b2STejun Heo  * guarantee is observed only with respect to @target and on the local
2282affee4b2STejun Heo  * cpu.
2283affee4b2STejun Heo  *
2284affee4b2STejun Heo  * Currently, a queued barrier can't be canceled.  This is because
2285affee4b2STejun Heo  * try_to_grab_pending() can't determine whether the work to be
2286affee4b2STejun Heo  * grabbed is at the head of the queue and thus can't clear LINKED
2287affee4b2STejun Heo  * flag of the previous work while there must be a valid next work
2288affee4b2STejun Heo  * after a work with LINKED flag set.
2289affee4b2STejun Heo  *
2290affee4b2STejun Heo  * Note that when @worker is non-NULL, @target may be modified
2291affee4b2STejun Heo  * underneath us, so we can't reliably determine cwq from @target.
22924690c4abSTejun Heo  *
22934690c4abSTejun Heo  * CONTEXT:
22948b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock).
22954690c4abSTejun Heo  */
229683c22520SOleg Nesterov static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
2297affee4b2STejun Heo 			      struct wq_barrier *barr,
2298affee4b2STejun Heo 			      struct work_struct *target, struct worker *worker)
2299fc2e4d70SOleg Nesterov {
2300affee4b2STejun Heo 	struct list_head *head;
2301affee4b2STejun Heo 	unsigned int linked = 0;
2302affee4b2STejun Heo 
2303dc186ad7SThomas Gleixner 	/*
23048b03ae3cSTejun Heo 	 * debugobject calls are safe here even with gcwq->lock locked
2305dc186ad7SThomas Gleixner 	 * as we know for sure that this will not trigger any of the
2306dc186ad7SThomas Gleixner 	 * checks and call back into the fixup functions where we
2307dc186ad7SThomas Gleixner 	 * might deadlock.
2308dc186ad7SThomas Gleixner 	 */
2309ca1cab37SAndrew Morton 	INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
231022df02bbSTejun Heo 	__set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
2311fc2e4d70SOleg Nesterov 	init_completion(&barr->done);
231283c22520SOleg Nesterov 
2313affee4b2STejun Heo 	/*
2314affee4b2STejun Heo 	 * If @target is currently being executed, schedule the
2315affee4b2STejun Heo 	 * barrier to the worker; otherwise, put it after @target.
2316affee4b2STejun Heo 	 */
2317affee4b2STejun Heo 	if (worker)
2318affee4b2STejun Heo 		head = worker->scheduled.next;
2319affee4b2STejun Heo 	else {
2320affee4b2STejun Heo 		unsigned long *bits = work_data_bits(target);
2321affee4b2STejun Heo 
2322affee4b2STejun Heo 		head = target->entry.next;
2323affee4b2STejun Heo 		/* there can already be other linked works, inherit and set */
2324affee4b2STejun Heo 		linked = *bits & WORK_STRUCT_LINKED;
2325affee4b2STejun Heo 		__set_bit(WORK_STRUCT_LINKED_BIT, bits);
2326affee4b2STejun Heo 	}
2327affee4b2STejun Heo 
2328dc186ad7SThomas Gleixner 	debug_work_activate(&barr->work);
2329affee4b2STejun Heo 	insert_work(cwq, &barr->work, head,
2330affee4b2STejun Heo 		    work_color_to_flags(WORK_NO_COLOR) | linked);
2331fc2e4d70SOleg Nesterov }
2332fc2e4d70SOleg Nesterov 
233373f53c4aSTejun Heo /**
233473f53c4aSTejun Heo  * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
233573f53c4aSTejun Heo  * @wq: workqueue being flushed
233673f53c4aSTejun Heo  * @flush_color: new flush color, < 0 for no-op
233773f53c4aSTejun Heo  * @work_color: new work color, < 0 for no-op
233873f53c4aSTejun Heo  *
233973f53c4aSTejun Heo  * Prepare cwqs for workqueue flushing.
234073f53c4aSTejun Heo  *
234173f53c4aSTejun Heo  * If @flush_color is non-negative, flush_color on all cwqs should be
234273f53c4aSTejun Heo  * -1.  If no cwq has in-flight commands at the specified color, all
234373f53c4aSTejun Heo  * cwq->flush_color's stay at -1 and %false is returned.  If any cwq
234473f53c4aSTejun Heo  * has in flight commands, its cwq->flush_color is set to
234573f53c4aSTejun Heo  * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
234673f53c4aSTejun Heo  * wakeup logic is armed and %true is returned.
234773f53c4aSTejun Heo  *
234873f53c4aSTejun Heo  * The caller should have initialized @wq->first_flusher prior to
234973f53c4aSTejun Heo  * calling this function with non-negative @flush_color.  If
235073f53c4aSTejun Heo  * @flush_color is negative, no flush color update is done and %false
235173f53c4aSTejun Heo  * is returned.
235273f53c4aSTejun Heo  *
235373f53c4aSTejun Heo  * If @work_color is non-negative, all cwqs should have the same
235473f53c4aSTejun Heo  * work_color which is previous to @work_color and all will be
235573f53c4aSTejun Heo  * advanced to @work_color.
235673f53c4aSTejun Heo  *
235773f53c4aSTejun Heo  * CONTEXT:
235873f53c4aSTejun Heo  * mutex_lock(wq->flush_mutex).
235973f53c4aSTejun Heo  *
236073f53c4aSTejun Heo  * RETURNS:
236173f53c4aSTejun Heo  * %true if @flush_color >= 0 and there's something to flush.  %false
236273f53c4aSTejun Heo  * otherwise.
236373f53c4aSTejun Heo  */
236473f53c4aSTejun Heo static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
236573f53c4aSTejun Heo 				      int flush_color, int work_color)
23661da177e4SLinus Torvalds {
236773f53c4aSTejun Heo 	bool wait = false;
236873f53c4aSTejun Heo 	unsigned int cpu;
23691da177e4SLinus Torvalds 
237073f53c4aSTejun Heo 	if (flush_color >= 0) {
237173f53c4aSTejun Heo 		BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
237273f53c4aSTejun Heo 		atomic_set(&wq->nr_cwqs_to_flush, 1);
2373dc186ad7SThomas Gleixner 	}
237414441960SOleg Nesterov 
2375f3421797STejun Heo 	for_each_cwq_cpu(cpu, wq) {
237673f53c4aSTejun Heo 		struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
2377bd7bdd43STejun Heo 		struct global_cwq *gcwq = cwq->pool->gcwq;
23781da177e4SLinus Torvalds 
23798b03ae3cSTejun Heo 		spin_lock_irq(&gcwq->lock);
238073f53c4aSTejun Heo 
238173f53c4aSTejun Heo 		if (flush_color >= 0) {
238273f53c4aSTejun Heo 			BUG_ON(cwq->flush_color != -1);
238373f53c4aSTejun Heo 
238473f53c4aSTejun Heo 			if (cwq->nr_in_flight[flush_color]) {
238573f53c4aSTejun Heo 				cwq->flush_color = flush_color;
238673f53c4aSTejun Heo 				atomic_inc(&wq->nr_cwqs_to_flush);
238773f53c4aSTejun Heo 				wait = true;
23881da177e4SLinus Torvalds 			}
238973f53c4aSTejun Heo 		}
239073f53c4aSTejun Heo 
239173f53c4aSTejun Heo 		if (work_color >= 0) {
239273f53c4aSTejun Heo 			BUG_ON(work_color != work_next_color(cwq->work_color));
239373f53c4aSTejun Heo 			cwq->work_color = work_color;
239473f53c4aSTejun Heo 		}
239573f53c4aSTejun Heo 
23968b03ae3cSTejun Heo 		spin_unlock_irq(&gcwq->lock);
23971da177e4SLinus Torvalds 	}
23981da177e4SLinus Torvalds 
239973f53c4aSTejun Heo 	if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
240073f53c4aSTejun Heo 		complete(&wq->first_flusher->done);
240173f53c4aSTejun Heo 
240273f53c4aSTejun Heo 	return wait;
240383c22520SOleg Nesterov }
24041da177e4SLinus Torvalds 
24050fcb78c2SRolf Eike Beer /**
24061da177e4SLinus Torvalds  * flush_workqueue - ensure that any scheduled work has run to completion.
24070fcb78c2SRolf Eike Beer  * @wq: workqueue to flush
24081da177e4SLinus Torvalds  *
24091da177e4SLinus Torvalds  * Forces execution of the workqueue and blocks until its completion.
24101da177e4SLinus Torvalds  * This is typically used in driver shutdown handlers.
24111da177e4SLinus Torvalds  *
2412fc2e4d70SOleg Nesterov  * We sleep until all works which were queued on entry have been handled,
2413fc2e4d70SOleg Nesterov  * but we are not livelocked by new incoming ones.
24141da177e4SLinus Torvalds  */
24157ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq)
24161da177e4SLinus Torvalds {
241773f53c4aSTejun Heo 	struct wq_flusher this_flusher = {
241873f53c4aSTejun Heo 		.list = LIST_HEAD_INIT(this_flusher.list),
241973f53c4aSTejun Heo 		.flush_color = -1,
242073f53c4aSTejun Heo 		.done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
242173f53c4aSTejun Heo 	};
242273f53c4aSTejun Heo 	int next_color;
2423b1f4ec17SOleg Nesterov 
24243295f0efSIngo Molnar 	lock_map_acquire(&wq->lockdep_map);
24253295f0efSIngo Molnar 	lock_map_release(&wq->lockdep_map);
242673f53c4aSTejun Heo 
242773f53c4aSTejun Heo 	mutex_lock(&wq->flush_mutex);
242873f53c4aSTejun Heo 
242973f53c4aSTejun Heo 	/*
243073f53c4aSTejun Heo 	 * Start-to-wait phase
243173f53c4aSTejun Heo 	 */
243273f53c4aSTejun Heo 	next_color = work_next_color(wq->work_color);
243373f53c4aSTejun Heo 
243473f53c4aSTejun Heo 	if (next_color != wq->flush_color) {
243573f53c4aSTejun Heo 		/*
243673f53c4aSTejun Heo 		 * Color space is not full.  The current work_color
243773f53c4aSTejun Heo 		 * becomes our flush_color and work_color is advanced
243873f53c4aSTejun Heo 		 * by one.
243973f53c4aSTejun Heo 		 */
244073f53c4aSTejun Heo 		BUG_ON(!list_empty(&wq->flusher_overflow));
244173f53c4aSTejun Heo 		this_flusher.flush_color = wq->work_color;
244273f53c4aSTejun Heo 		wq->work_color = next_color;
244373f53c4aSTejun Heo 
244473f53c4aSTejun Heo 		if (!wq->first_flusher) {
244573f53c4aSTejun Heo 			/* no flush in progress, become the first flusher */
244673f53c4aSTejun Heo 			BUG_ON(wq->flush_color != this_flusher.flush_color);
244773f53c4aSTejun Heo 
244873f53c4aSTejun Heo 			wq->first_flusher = &this_flusher;
244973f53c4aSTejun Heo 
245073f53c4aSTejun Heo 			if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
245173f53c4aSTejun Heo 						       wq->work_color)) {
245273f53c4aSTejun Heo 				/* nothing to flush, done */
245373f53c4aSTejun Heo 				wq->flush_color = next_color;
245473f53c4aSTejun Heo 				wq->first_flusher = NULL;
245573f53c4aSTejun Heo 				goto out_unlock;
245673f53c4aSTejun Heo 			}
245773f53c4aSTejun Heo 		} else {
245873f53c4aSTejun Heo 			/* wait in queue */
245973f53c4aSTejun Heo 			BUG_ON(wq->flush_color == this_flusher.flush_color);
246073f53c4aSTejun Heo 			list_add_tail(&this_flusher.list, &wq->flusher_queue);
246173f53c4aSTejun Heo 			flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
246273f53c4aSTejun Heo 		}
246373f53c4aSTejun Heo 	} else {
246473f53c4aSTejun Heo 		/*
246573f53c4aSTejun Heo 		 * Oops, color space is full, wait on overflow queue.
246673f53c4aSTejun Heo 		 * The next flush completion will assign us
246773f53c4aSTejun Heo 		 * flush_color and transfer to flusher_queue.
246873f53c4aSTejun Heo 		 */
246973f53c4aSTejun Heo 		list_add_tail(&this_flusher.list, &wq->flusher_overflow);
247073f53c4aSTejun Heo 	}
247173f53c4aSTejun Heo 
247273f53c4aSTejun Heo 	mutex_unlock(&wq->flush_mutex);
247373f53c4aSTejun Heo 
247473f53c4aSTejun Heo 	wait_for_completion(&this_flusher.done);
247573f53c4aSTejun Heo 
247673f53c4aSTejun Heo 	/*
247773f53c4aSTejun Heo 	 * Wake-up-and-cascade phase
247873f53c4aSTejun Heo 	 *
247973f53c4aSTejun Heo 	 * First flushers are responsible for cascading flushes and
248073f53c4aSTejun Heo 	 * handling overflow.  Non-first flushers can simply return.
248173f53c4aSTejun Heo 	 */
248273f53c4aSTejun Heo 	if (wq->first_flusher != &this_flusher)
248373f53c4aSTejun Heo 		return;
248473f53c4aSTejun Heo 
248573f53c4aSTejun Heo 	mutex_lock(&wq->flush_mutex);
248673f53c4aSTejun Heo 
24874ce48b37STejun Heo 	/* we might have raced, check again with mutex held */
24884ce48b37STejun Heo 	if (wq->first_flusher != &this_flusher)
24894ce48b37STejun Heo 		goto out_unlock;
24904ce48b37STejun Heo 
249173f53c4aSTejun Heo 	wq->first_flusher = NULL;
249273f53c4aSTejun Heo 
249373f53c4aSTejun Heo 	BUG_ON(!list_empty(&this_flusher.list));
249473f53c4aSTejun Heo 	BUG_ON(wq->flush_color != this_flusher.flush_color);
249573f53c4aSTejun Heo 
249673f53c4aSTejun Heo 	while (true) {
249773f53c4aSTejun Heo 		struct wq_flusher *next, *tmp;
249873f53c4aSTejun Heo 
249973f53c4aSTejun Heo 		/* complete all the flushers sharing the current flush color */
250073f53c4aSTejun Heo 		list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
250173f53c4aSTejun Heo 			if (next->flush_color != wq->flush_color)
250273f53c4aSTejun Heo 				break;
250373f53c4aSTejun Heo 			list_del_init(&next->list);
250473f53c4aSTejun Heo 			complete(&next->done);
250573f53c4aSTejun Heo 		}
250673f53c4aSTejun Heo 
250773f53c4aSTejun Heo 		BUG_ON(!list_empty(&wq->flusher_overflow) &&
250873f53c4aSTejun Heo 		       wq->flush_color != work_next_color(wq->work_color));
250973f53c4aSTejun Heo 
251073f53c4aSTejun Heo 		/* this flush_color is finished, advance by one */
251173f53c4aSTejun Heo 		wq->flush_color = work_next_color(wq->flush_color);
251273f53c4aSTejun Heo 
251373f53c4aSTejun Heo 		/* one color has been freed, handle overflow queue */
251473f53c4aSTejun Heo 		if (!list_empty(&wq->flusher_overflow)) {
251573f53c4aSTejun Heo 			/*
251673f53c4aSTejun Heo 			 * Assign the same color to all overflowed
251773f53c4aSTejun Heo 			 * flushers, advance work_color and append to
251873f53c4aSTejun Heo 			 * flusher_queue.  This is the start-to-wait
251973f53c4aSTejun Heo 			 * phase for these overflowed flushers.
252073f53c4aSTejun Heo 			 */
252173f53c4aSTejun Heo 			list_for_each_entry(tmp, &wq->flusher_overflow, list)
252273f53c4aSTejun Heo 				tmp->flush_color = wq->work_color;
252373f53c4aSTejun Heo 
252473f53c4aSTejun Heo 			wq->work_color = work_next_color(wq->work_color);
252573f53c4aSTejun Heo 
252673f53c4aSTejun Heo 			list_splice_tail_init(&wq->flusher_overflow,
252773f53c4aSTejun Heo 					      &wq->flusher_queue);
252873f53c4aSTejun Heo 			flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
252973f53c4aSTejun Heo 		}
253073f53c4aSTejun Heo 
253173f53c4aSTejun Heo 		if (list_empty(&wq->flusher_queue)) {
253273f53c4aSTejun Heo 			BUG_ON(wq->flush_color != wq->work_color);
253373f53c4aSTejun Heo 			break;
253473f53c4aSTejun Heo 		}
253573f53c4aSTejun Heo 
253673f53c4aSTejun Heo 		/*
253773f53c4aSTejun Heo 		 * Need to flush more colors.  Make the next flusher
253873f53c4aSTejun Heo 		 * the new first flusher and arm cwqs.
253973f53c4aSTejun Heo 		 */
254073f53c4aSTejun Heo 		BUG_ON(wq->flush_color == wq->work_color);
254173f53c4aSTejun Heo 		BUG_ON(wq->flush_color != next->flush_color);
254273f53c4aSTejun Heo 
254373f53c4aSTejun Heo 		list_del_init(&next->list);
254473f53c4aSTejun Heo 		wq->first_flusher = next;
254573f53c4aSTejun Heo 
254673f53c4aSTejun Heo 		if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
254773f53c4aSTejun Heo 			break;
254873f53c4aSTejun Heo 
254973f53c4aSTejun Heo 		/*
255073f53c4aSTejun Heo 		 * Meh... this color is already done, clear first
255173f53c4aSTejun Heo 		 * flusher and repeat cascading.
255273f53c4aSTejun Heo 		 */
255373f53c4aSTejun Heo 		wq->first_flusher = NULL;
255473f53c4aSTejun Heo 	}
255573f53c4aSTejun Heo 
255673f53c4aSTejun Heo out_unlock:
255773f53c4aSTejun Heo 	mutex_unlock(&wq->flush_mutex);
25581da177e4SLinus Torvalds }
2559ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(flush_workqueue);
25601da177e4SLinus Torvalds 
25619c5a2ba7STejun Heo /**
25629c5a2ba7STejun Heo  * drain_workqueue - drain a workqueue
25639c5a2ba7STejun Heo  * @wq: workqueue to drain
25649c5a2ba7STejun Heo  *
25659c5a2ba7STejun Heo  * Wait until the workqueue becomes empty.  While draining is in progress,
25669c5a2ba7STejun Heo  * only chain queueing is allowed.  IOW, only currently pending or running
25679c5a2ba7STejun Heo  * work items on @wq can queue further work items on it.  @wq is flushed
25689c5a2ba7STejun Heo  * repeatedly until it becomes empty.  The number of flushing is detemined
25699c5a2ba7STejun Heo  * by the depth of chaining and should be relatively short.  Whine if it
25709c5a2ba7STejun Heo  * takes too long.
25719c5a2ba7STejun Heo  */
25729c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq)
25739c5a2ba7STejun Heo {
25749c5a2ba7STejun Heo 	unsigned int flush_cnt = 0;
25759c5a2ba7STejun Heo 	unsigned int cpu;
25769c5a2ba7STejun Heo 
25779c5a2ba7STejun Heo 	/*
25789c5a2ba7STejun Heo 	 * __queue_work() needs to test whether there are drainers, is much
25799c5a2ba7STejun Heo 	 * hotter than drain_workqueue() and already looks at @wq->flags.
25809c5a2ba7STejun Heo 	 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
25819c5a2ba7STejun Heo 	 */
25829c5a2ba7STejun Heo 	spin_lock(&workqueue_lock);
25839c5a2ba7STejun Heo 	if (!wq->nr_drainers++)
25849c5a2ba7STejun Heo 		wq->flags |= WQ_DRAINING;
25859c5a2ba7STejun Heo 	spin_unlock(&workqueue_lock);
25869c5a2ba7STejun Heo reflush:
25879c5a2ba7STejun Heo 	flush_workqueue(wq);
25889c5a2ba7STejun Heo 
25899c5a2ba7STejun Heo 	for_each_cwq_cpu(cpu, wq) {
25909c5a2ba7STejun Heo 		struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
2591fa2563e4SThomas Tuttle 		bool drained;
25929c5a2ba7STejun Heo 
2593bd7bdd43STejun Heo 		spin_lock_irq(&cwq->pool->gcwq->lock);
2594fa2563e4SThomas Tuttle 		drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
2595bd7bdd43STejun Heo 		spin_unlock_irq(&cwq->pool->gcwq->lock);
2596fa2563e4SThomas Tuttle 
2597fa2563e4SThomas Tuttle 		if (drained)
25989c5a2ba7STejun Heo 			continue;
25999c5a2ba7STejun Heo 
26009c5a2ba7STejun Heo 		if (++flush_cnt == 10 ||
26019c5a2ba7STejun Heo 		    (flush_cnt % 100 == 0 && flush_cnt <= 1000))
26029c5a2ba7STejun Heo 			pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n",
26039c5a2ba7STejun Heo 				   wq->name, flush_cnt);
26049c5a2ba7STejun Heo 		goto reflush;
26059c5a2ba7STejun Heo 	}
26069c5a2ba7STejun Heo 
26079c5a2ba7STejun Heo 	spin_lock(&workqueue_lock);
26089c5a2ba7STejun Heo 	if (!--wq->nr_drainers)
26099c5a2ba7STejun Heo 		wq->flags &= ~WQ_DRAINING;
26109c5a2ba7STejun Heo 	spin_unlock(&workqueue_lock);
26119c5a2ba7STejun Heo }
26129c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue);
26139c5a2ba7STejun Heo 
2614baf59022STejun Heo static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
2615baf59022STejun Heo 			     bool wait_executing)
2616baf59022STejun Heo {
2617baf59022STejun Heo 	struct worker *worker = NULL;
2618baf59022STejun Heo 	struct global_cwq *gcwq;
2619baf59022STejun Heo 	struct cpu_workqueue_struct *cwq;
2620baf59022STejun Heo 
2621baf59022STejun Heo 	might_sleep();
2622baf59022STejun Heo 	gcwq = get_work_gcwq(work);
2623baf59022STejun Heo 	if (!gcwq)
2624baf59022STejun Heo 		return false;
2625baf59022STejun Heo 
2626baf59022STejun Heo 	spin_lock_irq(&gcwq->lock);
2627baf59022STejun Heo 	if (!list_empty(&work->entry)) {
2628baf59022STejun Heo 		/*
2629baf59022STejun Heo 		 * See the comment near try_to_grab_pending()->smp_rmb().
2630baf59022STejun Heo 		 * If it was re-queued to a different gcwq under us, we
2631baf59022STejun Heo 		 * are not going to wait.
2632baf59022STejun Heo 		 */
2633baf59022STejun Heo 		smp_rmb();
2634baf59022STejun Heo 		cwq = get_work_cwq(work);
2635bd7bdd43STejun Heo 		if (unlikely(!cwq || gcwq != cwq->pool->gcwq))
2636baf59022STejun Heo 			goto already_gone;
2637baf59022STejun Heo 	} else if (wait_executing) {
2638baf59022STejun Heo 		worker = find_worker_executing_work(gcwq, work);
2639baf59022STejun Heo 		if (!worker)
2640baf59022STejun Heo 			goto already_gone;
2641baf59022STejun Heo 		cwq = worker->current_cwq;
2642baf59022STejun Heo 	} else
2643baf59022STejun Heo 		goto already_gone;
2644baf59022STejun Heo 
2645baf59022STejun Heo 	insert_wq_barrier(cwq, barr, work, worker);
2646baf59022STejun Heo 	spin_unlock_irq(&gcwq->lock);
2647baf59022STejun Heo 
2648e159489bSTejun Heo 	/*
2649e159489bSTejun Heo 	 * If @max_active is 1 or rescuer is in use, flushing another work
2650e159489bSTejun Heo 	 * item on the same workqueue may lead to deadlock.  Make sure the
2651e159489bSTejun Heo 	 * flusher is not running on the same workqueue by verifying write
2652e159489bSTejun Heo 	 * access.
2653e159489bSTejun Heo 	 */
2654e159489bSTejun Heo 	if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2655baf59022STejun Heo 		lock_map_acquire(&cwq->wq->lockdep_map);
2656e159489bSTejun Heo 	else
2657e159489bSTejun Heo 		lock_map_acquire_read(&cwq->wq->lockdep_map);
2658baf59022STejun Heo 	lock_map_release(&cwq->wq->lockdep_map);
2659e159489bSTejun Heo 
2660baf59022STejun Heo 	return true;
2661baf59022STejun Heo already_gone:
2662baf59022STejun Heo 	spin_unlock_irq(&gcwq->lock);
2663baf59022STejun Heo 	return false;
2664baf59022STejun Heo }
2665baf59022STejun Heo 
2666db700897SOleg Nesterov /**
2667401a8d04STejun Heo  * flush_work - wait for a work to finish executing the last queueing instance
2668401a8d04STejun Heo  * @work: the work to flush
2669db700897SOleg Nesterov  *
2670401a8d04STejun Heo  * Wait until @work has finished execution.  This function considers
2671401a8d04STejun Heo  * only the last queueing instance of @work.  If @work has been
2672401a8d04STejun Heo  * enqueued across different CPUs on a non-reentrant workqueue or on
2673401a8d04STejun Heo  * multiple workqueues, @work might still be executing on return on
2674401a8d04STejun Heo  * some of the CPUs from earlier queueing.
2675a67da70dSOleg Nesterov  *
2676401a8d04STejun Heo  * If @work was queued only on a non-reentrant, ordered or unbound
2677401a8d04STejun Heo  * workqueue, @work is guaranteed to be idle on return if it hasn't
2678401a8d04STejun Heo  * been requeued since flush started.
2679401a8d04STejun Heo  *
2680401a8d04STejun Heo  * RETURNS:
2681401a8d04STejun Heo  * %true if flush_work() waited for the work to finish execution,
2682401a8d04STejun Heo  * %false if it was already idle.
2683db700897SOleg Nesterov  */
2684401a8d04STejun Heo bool flush_work(struct work_struct *work)
2685db700897SOleg Nesterov {
2686db700897SOleg Nesterov 	struct wq_barrier barr;
2687db700897SOleg Nesterov 
26880976dfc1SStephen Boyd 	lock_map_acquire(&work->lockdep_map);
26890976dfc1SStephen Boyd 	lock_map_release(&work->lockdep_map);
26900976dfc1SStephen Boyd 
2691baf59022STejun Heo 	if (start_flush_work(work, &barr, true)) {
2692db700897SOleg Nesterov 		wait_for_completion(&barr.done);
2693dc186ad7SThomas Gleixner 		destroy_work_on_stack(&barr.work);
2694401a8d04STejun Heo 		return true;
2695baf59022STejun Heo 	} else
2696401a8d04STejun Heo 		return false;
2697db700897SOleg Nesterov }
2698db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work);
2699db700897SOleg Nesterov 
2700401a8d04STejun Heo static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
2701401a8d04STejun Heo {
2702401a8d04STejun Heo 	struct wq_barrier barr;
2703401a8d04STejun Heo 	struct worker *worker;
2704401a8d04STejun Heo 
2705401a8d04STejun Heo 	spin_lock_irq(&gcwq->lock);
2706401a8d04STejun Heo 
2707401a8d04STejun Heo 	worker = find_worker_executing_work(gcwq, work);
2708401a8d04STejun Heo 	if (unlikely(worker))
2709401a8d04STejun Heo 		insert_wq_barrier(worker->current_cwq, &barr, work, worker);
2710401a8d04STejun Heo 
2711401a8d04STejun Heo 	spin_unlock_irq(&gcwq->lock);
2712401a8d04STejun Heo 
2713401a8d04STejun Heo 	if (unlikely(worker)) {
2714401a8d04STejun Heo 		wait_for_completion(&barr.done);
2715401a8d04STejun Heo 		destroy_work_on_stack(&barr.work);
2716401a8d04STejun Heo 		return true;
2717401a8d04STejun Heo 	} else
2718401a8d04STejun Heo 		return false;
2719401a8d04STejun Heo }
2720401a8d04STejun Heo 
2721401a8d04STejun Heo static bool wait_on_work(struct work_struct *work)
2722401a8d04STejun Heo {
2723401a8d04STejun Heo 	bool ret = false;
2724401a8d04STejun Heo 	int cpu;
2725401a8d04STejun Heo 
2726401a8d04STejun Heo 	might_sleep();
2727401a8d04STejun Heo 
2728401a8d04STejun Heo 	lock_map_acquire(&work->lockdep_map);
2729401a8d04STejun Heo 	lock_map_release(&work->lockdep_map);
2730401a8d04STejun Heo 
2731401a8d04STejun Heo 	for_each_gcwq_cpu(cpu)
2732401a8d04STejun Heo 		ret |= wait_on_cpu_work(get_gcwq(cpu), work);
2733401a8d04STejun Heo 	return ret;
2734401a8d04STejun Heo }
2735401a8d04STejun Heo 
273609383498STejun Heo /**
273709383498STejun Heo  * flush_work_sync - wait until a work has finished execution
273809383498STejun Heo  * @work: the work to flush
273909383498STejun Heo  *
274009383498STejun Heo  * Wait until @work has finished execution.  On return, it's
274109383498STejun Heo  * guaranteed that all queueing instances of @work which happened
274209383498STejun Heo  * before this function is called are finished.  In other words, if
274309383498STejun Heo  * @work hasn't been requeued since this function was called, @work is
274409383498STejun Heo  * guaranteed to be idle on return.
274509383498STejun Heo  *
274609383498STejun Heo  * RETURNS:
274709383498STejun Heo  * %true if flush_work_sync() waited for the work to finish execution,
274809383498STejun Heo  * %false if it was already idle.
274909383498STejun Heo  */
275009383498STejun Heo bool flush_work_sync(struct work_struct *work)
275109383498STejun Heo {
275209383498STejun Heo 	struct wq_barrier barr;
275309383498STejun Heo 	bool pending, waited;
275409383498STejun Heo 
275509383498STejun Heo 	/* we'll wait for executions separately, queue barr only if pending */
275609383498STejun Heo 	pending = start_flush_work(work, &barr, false);
275709383498STejun Heo 
275809383498STejun Heo 	/* wait for executions to finish */
275909383498STejun Heo 	waited = wait_on_work(work);
276009383498STejun Heo 
276109383498STejun Heo 	/* wait for the pending one */
276209383498STejun Heo 	if (pending) {
276309383498STejun Heo 		wait_for_completion(&barr.done);
276409383498STejun Heo 		destroy_work_on_stack(&barr.work);
276509383498STejun Heo 	}
276609383498STejun Heo 
276709383498STejun Heo 	return pending || waited;
276809383498STejun Heo }
276909383498STejun Heo EXPORT_SYMBOL_GPL(flush_work_sync);
277009383498STejun Heo 
27716e84d644SOleg Nesterov /*
27721f1f642eSOleg Nesterov  * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
27736e84d644SOleg Nesterov  * so this work can't be re-armed in any way.
27746e84d644SOleg Nesterov  */
27756e84d644SOleg Nesterov static int try_to_grab_pending(struct work_struct *work)
27766e84d644SOleg Nesterov {
27778b03ae3cSTejun Heo 	struct global_cwq *gcwq;
27781f1f642eSOleg Nesterov 	int ret = -1;
27796e84d644SOleg Nesterov 
278022df02bbSTejun Heo 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
27811f1f642eSOleg Nesterov 		return 0;
27826e84d644SOleg Nesterov 
27836e84d644SOleg Nesterov 	/*
27846e84d644SOleg Nesterov 	 * The queueing is in progress, or it is already queued. Try to
27856e84d644SOleg Nesterov 	 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
27866e84d644SOleg Nesterov 	 */
27877a22ad75STejun Heo 	gcwq = get_work_gcwq(work);
27887a22ad75STejun Heo 	if (!gcwq)
27896e84d644SOleg Nesterov 		return ret;
27906e84d644SOleg Nesterov 
27918b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
27926e84d644SOleg Nesterov 	if (!list_empty(&work->entry)) {
27936e84d644SOleg Nesterov 		/*
27947a22ad75STejun Heo 		 * This work is queued, but perhaps we locked the wrong gcwq.
27956e84d644SOleg Nesterov 		 * In that case we must see the new value after rmb(), see
27966e84d644SOleg Nesterov 		 * insert_work()->wmb().
27976e84d644SOleg Nesterov 		 */
27986e84d644SOleg Nesterov 		smp_rmb();
27997a22ad75STejun Heo 		if (gcwq == get_work_gcwq(work)) {
2800dc186ad7SThomas Gleixner 			debug_work_deactivate(work);
28016e84d644SOleg Nesterov 			list_del_init(&work->entry);
28027a22ad75STejun Heo 			cwq_dec_nr_in_flight(get_work_cwq(work),
28038a2e8e5dSTejun Heo 				get_work_color(work),
28048a2e8e5dSTejun Heo 				*work_data_bits(work) & WORK_STRUCT_DELAYED);
28056e84d644SOleg Nesterov 			ret = 1;
28066e84d644SOleg Nesterov 		}
28076e84d644SOleg Nesterov 	}
28088b03ae3cSTejun Heo 	spin_unlock_irq(&gcwq->lock);
28096e84d644SOleg Nesterov 
28106e84d644SOleg Nesterov 	return ret;
28116e84d644SOleg Nesterov }
28126e84d644SOleg Nesterov 
2813401a8d04STejun Heo static bool __cancel_work_timer(struct work_struct *work,
28141f1f642eSOleg Nesterov 				struct timer_list* timer)
28151f1f642eSOleg Nesterov {
28161f1f642eSOleg Nesterov 	int ret;
28171f1f642eSOleg Nesterov 
28181f1f642eSOleg Nesterov 	do {
28191f1f642eSOleg Nesterov 		ret = (timer && likely(del_timer(timer)));
28201f1f642eSOleg Nesterov 		if (!ret)
28211f1f642eSOleg Nesterov 			ret = try_to_grab_pending(work);
28221f1f642eSOleg Nesterov 		wait_on_work(work);
28231f1f642eSOleg Nesterov 	} while (unlikely(ret < 0));
28241f1f642eSOleg Nesterov 
28257a22ad75STejun Heo 	clear_work_data(work);
28261f1f642eSOleg Nesterov 	return ret;
28271f1f642eSOleg Nesterov }
28281f1f642eSOleg Nesterov 
28296e84d644SOleg Nesterov /**
2830401a8d04STejun Heo  * cancel_work_sync - cancel a work and wait for it to finish
2831401a8d04STejun Heo  * @work: the work to cancel
28326e84d644SOleg Nesterov  *
2833401a8d04STejun Heo  * Cancel @work and wait for its execution to finish.  This function
2834401a8d04STejun Heo  * can be used even if the work re-queues itself or migrates to
2835401a8d04STejun Heo  * another workqueue.  On return from this function, @work is
2836401a8d04STejun Heo  * guaranteed to be not pending or executing on any CPU.
28371f1f642eSOleg Nesterov  *
2838401a8d04STejun Heo  * cancel_work_sync(&delayed_work->work) must not be used for
2839401a8d04STejun Heo  * delayed_work's.  Use cancel_delayed_work_sync() instead.
28406e84d644SOleg Nesterov  *
2841401a8d04STejun Heo  * The caller must ensure that the workqueue on which @work was last
28426e84d644SOleg Nesterov  * queued can't be destroyed before this function returns.
2843401a8d04STejun Heo  *
2844401a8d04STejun Heo  * RETURNS:
2845401a8d04STejun Heo  * %true if @work was pending, %false otherwise.
28466e84d644SOleg Nesterov  */
2847401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work)
28486e84d644SOleg Nesterov {
28491f1f642eSOleg Nesterov 	return __cancel_work_timer(work, NULL);
2850b89deed3SOleg Nesterov }
285128e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync);
2852b89deed3SOleg Nesterov 
28536e84d644SOleg Nesterov /**
2854401a8d04STejun Heo  * flush_delayed_work - wait for a dwork to finish executing the last queueing
2855401a8d04STejun Heo  * @dwork: the delayed work to flush
28566e84d644SOleg Nesterov  *
2857401a8d04STejun Heo  * Delayed timer is cancelled and the pending work is queued for
2858401a8d04STejun Heo  * immediate execution.  Like flush_work(), this function only
2859401a8d04STejun Heo  * considers the last queueing instance of @dwork.
28601f1f642eSOleg Nesterov  *
2861401a8d04STejun Heo  * RETURNS:
2862401a8d04STejun Heo  * %true if flush_work() waited for the work to finish execution,
2863401a8d04STejun Heo  * %false if it was already idle.
28646e84d644SOleg Nesterov  */
2865401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork)
2866401a8d04STejun Heo {
2867401a8d04STejun Heo 	if (del_timer_sync(&dwork->timer))
2868401a8d04STejun Heo 		__queue_work(raw_smp_processor_id(),
2869401a8d04STejun Heo 			     get_work_cwq(&dwork->work)->wq, &dwork->work);
2870401a8d04STejun Heo 	return flush_work(&dwork->work);
2871401a8d04STejun Heo }
2872401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work);
2873401a8d04STejun Heo 
2874401a8d04STejun Heo /**
287509383498STejun Heo  * flush_delayed_work_sync - wait for a dwork to finish
287609383498STejun Heo  * @dwork: the delayed work to flush
287709383498STejun Heo  *
287809383498STejun Heo  * Delayed timer is cancelled and the pending work is queued for
287909383498STejun Heo  * execution immediately.  Other than timer handling, its behavior
288009383498STejun Heo  * is identical to flush_work_sync().
288109383498STejun Heo  *
288209383498STejun Heo  * RETURNS:
288309383498STejun Heo  * %true if flush_work_sync() waited for the work to finish execution,
288409383498STejun Heo  * %false if it was already idle.
288509383498STejun Heo  */
288609383498STejun Heo bool flush_delayed_work_sync(struct delayed_work *dwork)
288709383498STejun Heo {
288809383498STejun Heo 	if (del_timer_sync(&dwork->timer))
288909383498STejun Heo 		__queue_work(raw_smp_processor_id(),
289009383498STejun Heo 			     get_work_cwq(&dwork->work)->wq, &dwork->work);
289109383498STejun Heo 	return flush_work_sync(&dwork->work);
289209383498STejun Heo }
289309383498STejun Heo EXPORT_SYMBOL(flush_delayed_work_sync);
289409383498STejun Heo 
289509383498STejun Heo /**
2896401a8d04STejun Heo  * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2897401a8d04STejun Heo  * @dwork: the delayed work cancel
2898401a8d04STejun Heo  *
2899401a8d04STejun Heo  * This is cancel_work_sync() for delayed works.
2900401a8d04STejun Heo  *
2901401a8d04STejun Heo  * RETURNS:
2902401a8d04STejun Heo  * %true if @dwork was pending, %false otherwise.
2903401a8d04STejun Heo  */
2904401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork)
29056e84d644SOleg Nesterov {
29061f1f642eSOleg Nesterov 	return __cancel_work_timer(&dwork->work, &dwork->timer);
29076e84d644SOleg Nesterov }
2908f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync);
29091da177e4SLinus Torvalds 
29100fcb78c2SRolf Eike Beer /**
29110fcb78c2SRolf Eike Beer  * schedule_work - put work task in global workqueue
29120fcb78c2SRolf Eike Beer  * @work: job to be done
29130fcb78c2SRolf Eike Beer  *
29145b0f437dSBart Van Assche  * Returns zero if @work was already on the kernel-global workqueue and
29155b0f437dSBart Van Assche  * non-zero otherwise.
29165b0f437dSBart Van Assche  *
29175b0f437dSBart Van Assche  * This puts a job in the kernel-global workqueue if it was not already
29185b0f437dSBart Van Assche  * queued and leaves it in the same position on the kernel-global
29195b0f437dSBart Van Assche  * workqueue otherwise.
29200fcb78c2SRolf Eike Beer  */
29217ad5b3a5SHarvey Harrison int schedule_work(struct work_struct *work)
29221da177e4SLinus Torvalds {
2923d320c038STejun Heo 	return queue_work(system_wq, work);
29241da177e4SLinus Torvalds }
2925ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_work);
29261da177e4SLinus Torvalds 
2927c1a220e7SZhang Rui /*
2928c1a220e7SZhang Rui  * schedule_work_on - put work task on a specific cpu
2929c1a220e7SZhang Rui  * @cpu: cpu to put the work task on
2930c1a220e7SZhang Rui  * @work: job to be done
2931c1a220e7SZhang Rui  *
2932c1a220e7SZhang Rui  * This puts a job on a specific cpu
2933c1a220e7SZhang Rui  */
2934c1a220e7SZhang Rui int schedule_work_on(int cpu, struct work_struct *work)
2935c1a220e7SZhang Rui {
2936d320c038STejun Heo 	return queue_work_on(cpu, system_wq, work);
2937c1a220e7SZhang Rui }
2938c1a220e7SZhang Rui EXPORT_SYMBOL(schedule_work_on);
2939c1a220e7SZhang Rui 
29400fcb78c2SRolf Eike Beer /**
29410fcb78c2SRolf Eike Beer  * schedule_delayed_work - put work task in global workqueue after delay
294252bad64dSDavid Howells  * @dwork: job to be done
294352bad64dSDavid Howells  * @delay: number of jiffies to wait or 0 for immediate execution
29440fcb78c2SRolf Eike Beer  *
29450fcb78c2SRolf Eike Beer  * After waiting for a given time this puts a job in the kernel-global
29460fcb78c2SRolf Eike Beer  * workqueue.
29470fcb78c2SRolf Eike Beer  */
29487ad5b3a5SHarvey Harrison int schedule_delayed_work(struct delayed_work *dwork,
294982f67cd9SIngo Molnar 					unsigned long delay)
29501da177e4SLinus Torvalds {
2951d320c038STejun Heo 	return queue_delayed_work(system_wq, dwork, delay);
29521da177e4SLinus Torvalds }
2953ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work);
29541da177e4SLinus Torvalds 
29550fcb78c2SRolf Eike Beer /**
29560fcb78c2SRolf Eike Beer  * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
29570fcb78c2SRolf Eike Beer  * @cpu: cpu to use
295852bad64dSDavid Howells  * @dwork: job to be done
29590fcb78c2SRolf Eike Beer  * @delay: number of jiffies to wait
29600fcb78c2SRolf Eike Beer  *
29610fcb78c2SRolf Eike Beer  * After waiting for a given time this puts a job in the kernel-global
29620fcb78c2SRolf Eike Beer  * workqueue on the specified CPU.
29630fcb78c2SRolf Eike Beer  */
29641da177e4SLinus Torvalds int schedule_delayed_work_on(int cpu,
296552bad64dSDavid Howells 			struct delayed_work *dwork, unsigned long delay)
29661da177e4SLinus Torvalds {
2967d320c038STejun Heo 	return queue_delayed_work_on(cpu, system_wq, dwork, delay);
29681da177e4SLinus Torvalds }
2969ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work_on);
29701da177e4SLinus Torvalds 
2971b6136773SAndrew Morton /**
297231ddd871STejun Heo  * schedule_on_each_cpu - execute a function synchronously on each online CPU
2973b6136773SAndrew Morton  * @func: the function to call
2974b6136773SAndrew Morton  *
297531ddd871STejun Heo  * schedule_on_each_cpu() executes @func on each online CPU using the
297631ddd871STejun Heo  * system workqueue and blocks until all CPUs have completed.
2977b6136773SAndrew Morton  * schedule_on_each_cpu() is very slow.
297831ddd871STejun Heo  *
297931ddd871STejun Heo  * RETURNS:
298031ddd871STejun Heo  * 0 on success, -errno on failure.
2981b6136773SAndrew Morton  */
298265f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func)
298315316ba8SChristoph Lameter {
298415316ba8SChristoph Lameter 	int cpu;
298538f51568SNamhyung Kim 	struct work_struct __percpu *works;
298615316ba8SChristoph Lameter 
2987b6136773SAndrew Morton 	works = alloc_percpu(struct work_struct);
2988b6136773SAndrew Morton 	if (!works)
298915316ba8SChristoph Lameter 		return -ENOMEM;
2990b6136773SAndrew Morton 
299195402b38SGautham R Shenoy 	get_online_cpus();
299293981800STejun Heo 
299315316ba8SChristoph Lameter 	for_each_online_cpu(cpu) {
29949bfb1839SIngo Molnar 		struct work_struct *work = per_cpu_ptr(works, cpu);
29959bfb1839SIngo Molnar 
29969bfb1839SIngo Molnar 		INIT_WORK(work, func);
29978de6d308SOleg Nesterov 		schedule_work_on(cpu, work);
299815316ba8SChristoph Lameter 	}
299993981800STejun Heo 
300093981800STejun Heo 	for_each_online_cpu(cpu)
30018616a89aSOleg Nesterov 		flush_work(per_cpu_ptr(works, cpu));
300293981800STejun Heo 
300395402b38SGautham R Shenoy 	put_online_cpus();
3004b6136773SAndrew Morton 	free_percpu(works);
300515316ba8SChristoph Lameter 	return 0;
300615316ba8SChristoph Lameter }
300715316ba8SChristoph Lameter 
3008eef6a7d5SAlan Stern /**
3009eef6a7d5SAlan Stern  * flush_scheduled_work - ensure that any scheduled work has run to completion.
3010eef6a7d5SAlan Stern  *
3011eef6a7d5SAlan Stern  * Forces execution of the kernel-global workqueue and blocks until its
3012eef6a7d5SAlan Stern  * completion.
3013eef6a7d5SAlan Stern  *
3014eef6a7d5SAlan Stern  * Think twice before calling this function!  It's very easy to get into
3015eef6a7d5SAlan Stern  * trouble if you don't take great care.  Either of the following situations
3016eef6a7d5SAlan Stern  * will lead to deadlock:
3017eef6a7d5SAlan Stern  *
3018eef6a7d5SAlan Stern  *	One of the work items currently on the workqueue needs to acquire
3019eef6a7d5SAlan Stern  *	a lock held by your code or its caller.
3020eef6a7d5SAlan Stern  *
3021eef6a7d5SAlan Stern  *	Your code is running in the context of a work routine.
3022eef6a7d5SAlan Stern  *
3023eef6a7d5SAlan Stern  * They will be detected by lockdep when they occur, but the first might not
3024eef6a7d5SAlan Stern  * occur very often.  It depends on what work items are on the workqueue and
3025eef6a7d5SAlan Stern  * what locks they need, which you have no control over.
3026eef6a7d5SAlan Stern  *
3027eef6a7d5SAlan Stern  * In most situations flushing the entire workqueue is overkill; you merely
3028eef6a7d5SAlan Stern  * need to know that a particular work item isn't queued and isn't running.
3029eef6a7d5SAlan Stern  * In such cases you should use cancel_delayed_work_sync() or
3030eef6a7d5SAlan Stern  * cancel_work_sync() instead.
3031eef6a7d5SAlan Stern  */
30321da177e4SLinus Torvalds void flush_scheduled_work(void)
30331da177e4SLinus Torvalds {
3034d320c038STejun Heo 	flush_workqueue(system_wq);
30351da177e4SLinus Torvalds }
3036ae90dd5dSDave Jones EXPORT_SYMBOL(flush_scheduled_work);
30371da177e4SLinus Torvalds 
30381da177e4SLinus Torvalds /**
30391fa44ecaSJames Bottomley  * execute_in_process_context - reliably execute the routine with user context
30401fa44ecaSJames Bottomley  * @fn:		the function to execute
30411fa44ecaSJames Bottomley  * @ew:		guaranteed storage for the execute work structure (must
30421fa44ecaSJames Bottomley  *		be available when the work executes)
30431fa44ecaSJames Bottomley  *
30441fa44ecaSJames Bottomley  * Executes the function immediately if process context is available,
30451fa44ecaSJames Bottomley  * otherwise schedules the function for delayed execution.
30461fa44ecaSJames Bottomley  *
30471fa44ecaSJames Bottomley  * Returns:	0 - function was executed
30481fa44ecaSJames Bottomley  *		1 - function was scheduled for execution
30491fa44ecaSJames Bottomley  */
305065f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew)
30511fa44ecaSJames Bottomley {
30521fa44ecaSJames Bottomley 	if (!in_interrupt()) {
305365f27f38SDavid Howells 		fn(&ew->work);
30541fa44ecaSJames Bottomley 		return 0;
30551fa44ecaSJames Bottomley 	}
30561fa44ecaSJames Bottomley 
305765f27f38SDavid Howells 	INIT_WORK(&ew->work, fn);
30581fa44ecaSJames Bottomley 	schedule_work(&ew->work);
30591fa44ecaSJames Bottomley 
30601fa44ecaSJames Bottomley 	return 1;
30611fa44ecaSJames Bottomley }
30621fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context);
30631fa44ecaSJames Bottomley 
30641da177e4SLinus Torvalds int keventd_up(void)
30651da177e4SLinus Torvalds {
3066d320c038STejun Heo 	return system_wq != NULL;
30671da177e4SLinus Torvalds }
30681da177e4SLinus Torvalds 
3069bdbc5dd7STejun Heo static int alloc_cwqs(struct workqueue_struct *wq)
30701da177e4SLinus Torvalds {
30713af24433SOleg Nesterov 	/*
30720f900049STejun Heo 	 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
30730f900049STejun Heo 	 * Make sure that the alignment isn't lower than that of
30740f900049STejun Heo 	 * unsigned long long.
30753af24433SOleg Nesterov 	 */
30760f900049STejun Heo 	const size_t size = sizeof(struct cpu_workqueue_struct);
30770f900049STejun Heo 	const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
30780f900049STejun Heo 				   __alignof__(unsigned long long));
30793af24433SOleg Nesterov 
3080e06ffa1eSLai Jiangshan 	if (!(wq->flags & WQ_UNBOUND))
3081f3421797STejun Heo 		wq->cpu_wq.pcpu = __alloc_percpu(size, align);
3082931ac77eSTejun Heo 	else {
30830f900049STejun Heo 		void *ptr;
3084e1d8aa9fSFrederic Weisbecker 
30850f900049STejun Heo 		/*
3086f3421797STejun Heo 		 * Allocate enough room to align cwq and put an extra
3087f3421797STejun Heo 		 * pointer at the end pointing back to the originally
3088f3421797STejun Heo 		 * allocated pointer which will be used for free.
30890f900049STejun Heo 		 */
3090bdbc5dd7STejun Heo 		ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
3091bdbc5dd7STejun Heo 		if (ptr) {
3092bdbc5dd7STejun Heo 			wq->cpu_wq.single = PTR_ALIGN(ptr, align);
3093bdbc5dd7STejun Heo 			*(void **)(wq->cpu_wq.single + 1) = ptr;
3094bdbc5dd7STejun Heo 		}
30953af24433SOleg Nesterov 	}
30963af24433SOleg Nesterov 
30970415b00dSTejun Heo 	/* just in case, make sure it's actually aligned */
3098bdbc5dd7STejun Heo 	BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
3099bdbc5dd7STejun Heo 	return wq->cpu_wq.v ? 0 : -ENOMEM;
31000f900049STejun Heo }
31010f900049STejun Heo 
3102bdbc5dd7STejun Heo static void free_cwqs(struct workqueue_struct *wq)
310306ba38a9SOleg Nesterov {
3104e06ffa1eSLai Jiangshan 	if (!(wq->flags & WQ_UNBOUND))
3105bdbc5dd7STejun Heo 		free_percpu(wq->cpu_wq.pcpu);
3106f3421797STejun Heo 	else if (wq->cpu_wq.single) {
3107f3421797STejun Heo 		/* the pointer to free is stored right after the cwq */
3108f3421797STejun Heo 		kfree(*(void **)(wq->cpu_wq.single + 1));
310906ba38a9SOleg Nesterov 	}
311006ba38a9SOleg Nesterov }
311106ba38a9SOleg Nesterov 
3112f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags,
3113f3421797STejun Heo 			       const char *name)
3114b71ab8c2STejun Heo {
3115f3421797STejun Heo 	int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3116f3421797STejun Heo 
3117f3421797STejun Heo 	if (max_active < 1 || max_active > lim)
3118b71ab8c2STejun Heo 		printk(KERN_WARNING "workqueue: max_active %d requested for %s "
3119b71ab8c2STejun Heo 		       "is out of range, clamping between %d and %d\n",
3120f3421797STejun Heo 		       max_active, name, 1, lim);
3121b71ab8c2STejun Heo 
3122f3421797STejun Heo 	return clamp_val(max_active, 1, lim);
3123b71ab8c2STejun Heo }
3124b71ab8c2STejun Heo 
3125b196be89STejun Heo struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
312697e37d7bSTejun Heo 					       unsigned int flags,
31271e19ffc6STejun Heo 					       int max_active,
3128eb13ba87SJohannes Berg 					       struct lock_class_key *key,
3129b196be89STejun Heo 					       const char *lock_name, ...)
31303af24433SOleg Nesterov {
3131b196be89STejun Heo 	va_list args, args1;
31323af24433SOleg Nesterov 	struct workqueue_struct *wq;
3133c34056a3STejun Heo 	unsigned int cpu;
3134b196be89STejun Heo 	size_t namelen;
3135b196be89STejun Heo 
3136b196be89STejun Heo 	/* determine namelen, allocate wq and format name */
3137b196be89STejun Heo 	va_start(args, lock_name);
3138b196be89STejun Heo 	va_copy(args1, args);
3139b196be89STejun Heo 	namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3140b196be89STejun Heo 
3141b196be89STejun Heo 	wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3142b196be89STejun Heo 	if (!wq)
3143b196be89STejun Heo 		goto err;
3144b196be89STejun Heo 
3145b196be89STejun Heo 	vsnprintf(wq->name, namelen, fmt, args1);
3146b196be89STejun Heo 	va_end(args);
3147b196be89STejun Heo 	va_end(args1);
31483af24433SOleg Nesterov 
3149f3421797STejun Heo 	/*
31506370a6adSTejun Heo 	 * Workqueues which may be used during memory reclaim should
31516370a6adSTejun Heo 	 * have a rescuer to guarantee forward progress.
31526370a6adSTejun Heo 	 */
31536370a6adSTejun Heo 	if (flags & WQ_MEM_RECLAIM)
31546370a6adSTejun Heo 		flags |= WQ_RESCUER;
31556370a6adSTejun Heo 
3156d320c038STejun Heo 	max_active = max_active ?: WQ_DFL_ACTIVE;
3157b196be89STejun Heo 	max_active = wq_clamp_max_active(max_active, flags, wq->name);
31583af24433SOleg Nesterov 
3159b196be89STejun Heo 	/* init wq */
316097e37d7bSTejun Heo 	wq->flags = flags;
3161a0a1a5fdSTejun Heo 	wq->saved_max_active = max_active;
316273f53c4aSTejun Heo 	mutex_init(&wq->flush_mutex);
316373f53c4aSTejun Heo 	atomic_set(&wq->nr_cwqs_to_flush, 0);
316473f53c4aSTejun Heo 	INIT_LIST_HEAD(&wq->flusher_queue);
316573f53c4aSTejun Heo 	INIT_LIST_HEAD(&wq->flusher_overflow);
31663af24433SOleg Nesterov 
3167eb13ba87SJohannes Berg 	lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
3168cce1a165SOleg Nesterov 	INIT_LIST_HEAD(&wq->list);
31693af24433SOleg Nesterov 
3170bdbc5dd7STejun Heo 	if (alloc_cwqs(wq) < 0)
3171bdbc5dd7STejun Heo 		goto err;
3172bdbc5dd7STejun Heo 
3173f3421797STejun Heo 	for_each_cwq_cpu(cpu, wq) {
31741537663fSTejun Heo 		struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
31758b03ae3cSTejun Heo 		struct global_cwq *gcwq = get_gcwq(cpu);
31763270476aSTejun Heo 		int pool_idx = (bool)(flags & WQ_HIGHPRI);
31771537663fSTejun Heo 
31780f900049STejun Heo 		BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
31793270476aSTejun Heo 		cwq->pool = &gcwq->pools[pool_idx];
3180c34056a3STejun Heo 		cwq->wq = wq;
318173f53c4aSTejun Heo 		cwq->flush_color = -1;
31821e19ffc6STejun Heo 		cwq->max_active = max_active;
31831e19ffc6STejun Heo 		INIT_LIST_HEAD(&cwq->delayed_works);
3184e22bee78STejun Heo 	}
31851537663fSTejun Heo 
3186e22bee78STejun Heo 	if (flags & WQ_RESCUER) {
3187e22bee78STejun Heo 		struct worker *rescuer;
3188e22bee78STejun Heo 
3189f2e005aaSTejun Heo 		if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
3190e22bee78STejun Heo 			goto err;
3191e22bee78STejun Heo 
3192e22bee78STejun Heo 		wq->rescuer = rescuer = alloc_worker();
3193e22bee78STejun Heo 		if (!rescuer)
3194e22bee78STejun Heo 			goto err;
3195e22bee78STejun Heo 
3196b196be89STejun Heo 		rescuer->task = kthread_create(rescuer_thread, wq, "%s",
3197b196be89STejun Heo 					       wq->name);
3198e22bee78STejun Heo 		if (IS_ERR(rescuer->task))
3199e22bee78STejun Heo 			goto err;
3200e22bee78STejun Heo 
3201e22bee78STejun Heo 		rescuer->task->flags |= PF_THREAD_BOUND;
3202e22bee78STejun Heo 		wake_up_process(rescuer->task);
32033af24433SOleg Nesterov 	}
32041537663fSTejun Heo 
32053af24433SOleg Nesterov 	/*
3206a0a1a5fdSTejun Heo 	 * workqueue_lock protects global freeze state and workqueues
3207a0a1a5fdSTejun Heo 	 * list.  Grab it, set max_active accordingly and add the new
3208a0a1a5fdSTejun Heo 	 * workqueue to workqueues list.
32093af24433SOleg Nesterov 	 */
32103af24433SOleg Nesterov 	spin_lock(&workqueue_lock);
3211a0a1a5fdSTejun Heo 
321258a69cb4STejun Heo 	if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
3213f3421797STejun Heo 		for_each_cwq_cpu(cpu, wq)
3214a0a1a5fdSTejun Heo 			get_cwq(cpu, wq)->max_active = 0;
3215a0a1a5fdSTejun Heo 
32163af24433SOleg Nesterov 	list_add(&wq->list, &workqueues);
3217a0a1a5fdSTejun Heo 
32183af24433SOleg Nesterov 	spin_unlock(&workqueue_lock);
32193af24433SOleg Nesterov 
32203af24433SOleg Nesterov 	return wq;
32214690c4abSTejun Heo err:
32224690c4abSTejun Heo 	if (wq) {
3223bdbc5dd7STejun Heo 		free_cwqs(wq);
3224f2e005aaSTejun Heo 		free_mayday_mask(wq->mayday_mask);
3225e22bee78STejun Heo 		kfree(wq->rescuer);
32264690c4abSTejun Heo 		kfree(wq);
32273af24433SOleg Nesterov 	}
32284690c4abSTejun Heo 	return NULL;
32291da177e4SLinus Torvalds }
3230d320c038STejun Heo EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
32311da177e4SLinus Torvalds 
32323af24433SOleg Nesterov /**
32333af24433SOleg Nesterov  * destroy_workqueue - safely terminate a workqueue
32343af24433SOleg Nesterov  * @wq: target workqueue
32353af24433SOleg Nesterov  *
32363af24433SOleg Nesterov  * Safely destroy a workqueue. All work currently pending will be done first.
32373af24433SOleg Nesterov  */
32383af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq)
32393af24433SOleg Nesterov {
3240c8e55f36STejun Heo 	unsigned int cpu;
32413af24433SOleg Nesterov 
32429c5a2ba7STejun Heo 	/* drain it before proceeding with destruction */
32439c5a2ba7STejun Heo 	drain_workqueue(wq);
3244c8efcc25STejun Heo 
3245a0a1a5fdSTejun Heo 	/*
3246a0a1a5fdSTejun Heo 	 * wq list is used to freeze wq, remove from list after
3247a0a1a5fdSTejun Heo 	 * flushing is complete in case freeze races us.
3248a0a1a5fdSTejun Heo 	 */
324995402b38SGautham R Shenoy 	spin_lock(&workqueue_lock);
32503af24433SOleg Nesterov 	list_del(&wq->list);
325195402b38SGautham R Shenoy 	spin_unlock(&workqueue_lock);
32523af24433SOleg Nesterov 
3253e22bee78STejun Heo 	/* sanity check */
3254f3421797STejun Heo 	for_each_cwq_cpu(cpu, wq) {
325573f53c4aSTejun Heo 		struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
325673f53c4aSTejun Heo 		int i;
32573af24433SOleg Nesterov 
325873f53c4aSTejun Heo 		for (i = 0; i < WORK_NR_COLORS; i++)
325973f53c4aSTejun Heo 			BUG_ON(cwq->nr_in_flight[i]);
32601e19ffc6STejun Heo 		BUG_ON(cwq->nr_active);
32611e19ffc6STejun Heo 		BUG_ON(!list_empty(&cwq->delayed_works));
326273f53c4aSTejun Heo 	}
32631537663fSTejun Heo 
3264e22bee78STejun Heo 	if (wq->flags & WQ_RESCUER) {
3265e22bee78STejun Heo 		kthread_stop(wq->rescuer->task);
3266f2e005aaSTejun Heo 		free_mayday_mask(wq->mayday_mask);
32678d9df9f0SXiaotian Feng 		kfree(wq->rescuer);
3268e22bee78STejun Heo 	}
3269e22bee78STejun Heo 
3270bdbc5dd7STejun Heo 	free_cwqs(wq);
32713af24433SOleg Nesterov 	kfree(wq);
32723af24433SOleg Nesterov }
32733af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue);
32743af24433SOleg Nesterov 
3275dcd989cbSTejun Heo /**
3276dcd989cbSTejun Heo  * workqueue_set_max_active - adjust max_active of a workqueue
3277dcd989cbSTejun Heo  * @wq: target workqueue
3278dcd989cbSTejun Heo  * @max_active: new max_active value.
3279dcd989cbSTejun Heo  *
3280dcd989cbSTejun Heo  * Set max_active of @wq to @max_active.
3281dcd989cbSTejun Heo  *
3282dcd989cbSTejun Heo  * CONTEXT:
3283dcd989cbSTejun Heo  * Don't call from IRQ context.
3284dcd989cbSTejun Heo  */
3285dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3286dcd989cbSTejun Heo {
3287dcd989cbSTejun Heo 	unsigned int cpu;
3288dcd989cbSTejun Heo 
3289f3421797STejun Heo 	max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
3290dcd989cbSTejun Heo 
3291dcd989cbSTejun Heo 	spin_lock(&workqueue_lock);
3292dcd989cbSTejun Heo 
3293dcd989cbSTejun Heo 	wq->saved_max_active = max_active;
3294dcd989cbSTejun Heo 
3295f3421797STejun Heo 	for_each_cwq_cpu(cpu, wq) {
3296dcd989cbSTejun Heo 		struct global_cwq *gcwq = get_gcwq(cpu);
3297dcd989cbSTejun Heo 
3298dcd989cbSTejun Heo 		spin_lock_irq(&gcwq->lock);
3299dcd989cbSTejun Heo 
330058a69cb4STejun Heo 		if (!(wq->flags & WQ_FREEZABLE) ||
3301dcd989cbSTejun Heo 		    !(gcwq->flags & GCWQ_FREEZING))
3302dcd989cbSTejun Heo 			get_cwq(gcwq->cpu, wq)->max_active = max_active;
3303dcd989cbSTejun Heo 
3304dcd989cbSTejun Heo 		spin_unlock_irq(&gcwq->lock);
3305dcd989cbSTejun Heo 	}
3306dcd989cbSTejun Heo 
3307dcd989cbSTejun Heo 	spin_unlock(&workqueue_lock);
3308dcd989cbSTejun Heo }
3309dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3310dcd989cbSTejun Heo 
3311dcd989cbSTejun Heo /**
3312dcd989cbSTejun Heo  * workqueue_congested - test whether a workqueue is congested
3313dcd989cbSTejun Heo  * @cpu: CPU in question
3314dcd989cbSTejun Heo  * @wq: target workqueue
3315dcd989cbSTejun Heo  *
3316dcd989cbSTejun Heo  * Test whether @wq's cpu workqueue for @cpu is congested.  There is
3317dcd989cbSTejun Heo  * no synchronization around this function and the test result is
3318dcd989cbSTejun Heo  * unreliable and only useful as advisory hints or for debugging.
3319dcd989cbSTejun Heo  *
3320dcd989cbSTejun Heo  * RETURNS:
3321dcd989cbSTejun Heo  * %true if congested, %false otherwise.
3322dcd989cbSTejun Heo  */
3323dcd989cbSTejun Heo bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3324dcd989cbSTejun Heo {
3325dcd989cbSTejun Heo 	struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3326dcd989cbSTejun Heo 
3327dcd989cbSTejun Heo 	return !list_empty(&cwq->delayed_works);
3328dcd989cbSTejun Heo }
3329dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested);
3330dcd989cbSTejun Heo 
3331dcd989cbSTejun Heo /**
3332dcd989cbSTejun Heo  * work_cpu - return the last known associated cpu for @work
3333dcd989cbSTejun Heo  * @work: the work of interest
3334dcd989cbSTejun Heo  *
3335dcd989cbSTejun Heo  * RETURNS:
3336bdbc5dd7STejun Heo  * CPU number if @work was ever queued.  WORK_CPU_NONE otherwise.
3337dcd989cbSTejun Heo  */
3338dcd989cbSTejun Heo unsigned int work_cpu(struct work_struct *work)
3339dcd989cbSTejun Heo {
3340dcd989cbSTejun Heo 	struct global_cwq *gcwq = get_work_gcwq(work);
3341dcd989cbSTejun Heo 
3342bdbc5dd7STejun Heo 	return gcwq ? gcwq->cpu : WORK_CPU_NONE;
3343dcd989cbSTejun Heo }
3344dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_cpu);
3345dcd989cbSTejun Heo 
3346dcd989cbSTejun Heo /**
3347dcd989cbSTejun Heo  * work_busy - test whether a work is currently pending or running
3348dcd989cbSTejun Heo  * @work: the work to be tested
3349dcd989cbSTejun Heo  *
3350dcd989cbSTejun Heo  * Test whether @work is currently pending or running.  There is no
3351dcd989cbSTejun Heo  * synchronization around this function and the test result is
3352dcd989cbSTejun Heo  * unreliable and only useful as advisory hints or for debugging.
3353dcd989cbSTejun Heo  * Especially for reentrant wqs, the pending state might hide the
3354dcd989cbSTejun Heo  * running state.
3355dcd989cbSTejun Heo  *
3356dcd989cbSTejun Heo  * RETURNS:
3357dcd989cbSTejun Heo  * OR'd bitmask of WORK_BUSY_* bits.
3358dcd989cbSTejun Heo  */
3359dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work)
3360dcd989cbSTejun Heo {
3361dcd989cbSTejun Heo 	struct global_cwq *gcwq = get_work_gcwq(work);
3362dcd989cbSTejun Heo 	unsigned long flags;
3363dcd989cbSTejun Heo 	unsigned int ret = 0;
3364dcd989cbSTejun Heo 
3365dcd989cbSTejun Heo 	if (!gcwq)
3366dcd989cbSTejun Heo 		return false;
3367dcd989cbSTejun Heo 
3368dcd989cbSTejun Heo 	spin_lock_irqsave(&gcwq->lock, flags);
3369dcd989cbSTejun Heo 
3370dcd989cbSTejun Heo 	if (work_pending(work))
3371dcd989cbSTejun Heo 		ret |= WORK_BUSY_PENDING;
3372dcd989cbSTejun Heo 	if (find_worker_executing_work(gcwq, work))
3373dcd989cbSTejun Heo 		ret |= WORK_BUSY_RUNNING;
3374dcd989cbSTejun Heo 
3375dcd989cbSTejun Heo 	spin_unlock_irqrestore(&gcwq->lock, flags);
3376dcd989cbSTejun Heo 
3377dcd989cbSTejun Heo 	return ret;
3378dcd989cbSTejun Heo }
3379dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy);
3380dcd989cbSTejun Heo 
3381db7bccf4STejun Heo /*
3382db7bccf4STejun Heo  * CPU hotplug.
3383db7bccf4STejun Heo  *
3384e22bee78STejun Heo  * There are two challenges in supporting CPU hotplug.  Firstly, there
3385e22bee78STejun Heo  * are a lot of assumptions on strong associations among work, cwq and
3386e22bee78STejun Heo  * gcwq which make migrating pending and scheduled works very
3387e22bee78STejun Heo  * difficult to implement without impacting hot paths.  Secondly,
3388e22bee78STejun Heo  * gcwqs serve mix of short, long and very long running works making
3389e22bee78STejun Heo  * blocked draining impractical.
3390e22bee78STejun Heo  *
3391628c78e7STejun Heo  * This is solved by allowing a gcwq to be disassociated from the CPU
3392628c78e7STejun Heo  * running as an unbound one and allowing it to be reattached later if the
3393628c78e7STejun Heo  * cpu comes back online.
3394db7bccf4STejun Heo  */
3395db7bccf4STejun Heo 
339660373152STejun Heo /* claim manager positions of all pools */
33978db25e78STejun Heo static void gcwq_claim_management_and_lock(struct global_cwq *gcwq)
339860373152STejun Heo {
339960373152STejun Heo 	struct worker_pool *pool;
340060373152STejun Heo 
340160373152STejun Heo 	for_each_worker_pool(pool, gcwq)
340260373152STejun Heo 		mutex_lock_nested(&pool->manager_mutex, pool - gcwq->pools);
34038db25e78STejun Heo 	spin_lock_irq(&gcwq->lock);
340460373152STejun Heo }
340560373152STejun Heo 
340660373152STejun Heo /* release manager positions */
34078db25e78STejun Heo static void gcwq_release_management_and_unlock(struct global_cwq *gcwq)
340860373152STejun Heo {
340960373152STejun Heo 	struct worker_pool *pool;
341060373152STejun Heo 
34118db25e78STejun Heo 	spin_unlock_irq(&gcwq->lock);
341260373152STejun Heo 	for_each_worker_pool(pool, gcwq)
341360373152STejun Heo 		mutex_unlock(&pool->manager_mutex);
341460373152STejun Heo }
341560373152STejun Heo 
3416628c78e7STejun Heo static void gcwq_unbind_fn(struct work_struct *work)
3417db7bccf4STejun Heo {
3418628c78e7STejun Heo 	struct global_cwq *gcwq = get_gcwq(smp_processor_id());
34194ce62e9eSTejun Heo 	struct worker_pool *pool;
3420db7bccf4STejun Heo 	struct worker *worker;
3421db7bccf4STejun Heo 	struct hlist_node *pos;
3422db7bccf4STejun Heo 	int i;
3423db7bccf4STejun Heo 
3424db7bccf4STejun Heo 	BUG_ON(gcwq->cpu != smp_processor_id());
3425db7bccf4STejun Heo 
34268db25e78STejun Heo 	gcwq_claim_management_and_lock(gcwq);
3427e22bee78STejun Heo 
3428f2d5a0eeSTejun Heo 	/*
3429f2d5a0eeSTejun Heo 	 * We've claimed all manager positions.  Make all workers unbound
3430f2d5a0eeSTejun Heo 	 * and set DISASSOCIATED.  Before this, all workers except for the
3431f2d5a0eeSTejun Heo 	 * ones which are still executing works from before the last CPU
3432f2d5a0eeSTejun Heo 	 * down must be on the cpu.  After this, they may become diasporas.
3433f2d5a0eeSTejun Heo 	 */
343460373152STejun Heo 	for_each_worker_pool(pool, gcwq)
34354ce62e9eSTejun Heo 		list_for_each_entry(worker, &pool->idle_list, entry)
3436403c821dSTejun Heo 			worker->flags |= WORKER_UNBOUND;
3437db7bccf4STejun Heo 
3438db7bccf4STejun Heo 	for_each_busy_worker(worker, i, pos, gcwq)
3439403c821dSTejun Heo 		worker->flags |= WORKER_UNBOUND;
3440db7bccf4STejun Heo 
3441f2d5a0eeSTejun Heo 	gcwq->flags |= GCWQ_DISASSOCIATED;
3442f2d5a0eeSTejun Heo 
34438db25e78STejun Heo 	gcwq_release_management_and_unlock(gcwq);
3444e22bee78STejun Heo 
3445e22bee78STejun Heo 	/*
3446628c78e7STejun Heo 	 * Call schedule() so that we cross rq->lock and thus can guarantee
3447628c78e7STejun Heo 	 * sched callbacks see the %WORKER_UNBOUND flag.  This is necessary
3448628c78e7STejun Heo 	 * as scheduler callbacks may be invoked from other cpus.
3449628c78e7STejun Heo 	 */
3450628c78e7STejun Heo 	schedule();
3451628c78e7STejun Heo 
3452628c78e7STejun Heo 	/*
3453628c78e7STejun Heo 	 * Sched callbacks are disabled now.  Zap nr_running.  After this,
3454628c78e7STejun Heo 	 * nr_running stays zero and need_more_worker() and keep_working()
3455628c78e7STejun Heo 	 * are always true as long as the worklist is not empty.  @gcwq now
3456628c78e7STejun Heo 	 * behaves as unbound (in terms of concurrency management) gcwq
3457628c78e7STejun Heo 	 * which is served by workers tied to the CPU.
3458628c78e7STejun Heo 	 *
3459628c78e7STejun Heo 	 * On return from this function, the current worker would trigger
3460628c78e7STejun Heo 	 * unbound chain execution of pending work items if other workers
3461628c78e7STejun Heo 	 * didn't already.
3462e22bee78STejun Heo 	 */
34634ce62e9eSTejun Heo 	for_each_worker_pool(pool, gcwq)
34644ce62e9eSTejun Heo 		atomic_set(get_pool_nr_running(pool), 0);
3465db7bccf4STejun Heo }
3466db7bccf4STejun Heo 
34678db25e78STejun Heo /*
34688db25e78STejun Heo  * Workqueues should be brought up before normal priority CPU notifiers.
34698db25e78STejun Heo  * This will be registered high priority CPU notifier.
34708db25e78STejun Heo  */
34718db25e78STejun Heo static int __devinit workqueue_cpu_up_callback(struct notifier_block *nfb,
34721da177e4SLinus Torvalds 					       unsigned long action,
34731da177e4SLinus Torvalds 					       void *hcpu)
34741da177e4SLinus Torvalds {
34753af24433SOleg Nesterov 	unsigned int cpu = (unsigned long)hcpu;
3476db7bccf4STejun Heo 	struct global_cwq *gcwq = get_gcwq(cpu);
34774ce62e9eSTejun Heo 	struct worker_pool *pool;
34781da177e4SLinus Torvalds 
34798db25e78STejun Heo 	switch (action & ~CPU_TASKS_FROZEN) {
34803af24433SOleg Nesterov 	case CPU_UP_PREPARE:
34814ce62e9eSTejun Heo 		for_each_worker_pool(pool, gcwq) {
34823ce63377STejun Heo 			struct worker *worker;
34833ce63377STejun Heo 
34843ce63377STejun Heo 			if (pool->nr_workers)
34853ce63377STejun Heo 				continue;
34863ce63377STejun Heo 
34873ce63377STejun Heo 			worker = create_worker(pool);
34883ce63377STejun Heo 			if (!worker)
34893ce63377STejun Heo 				return NOTIFY_BAD;
34903ce63377STejun Heo 
34913ce63377STejun Heo 			spin_lock_irq(&gcwq->lock);
34923ce63377STejun Heo 			start_worker(worker);
34933ce63377STejun Heo 			spin_unlock_irq(&gcwq->lock);
34943af24433SOleg Nesterov 		}
34951da177e4SLinus Torvalds 		break;
34961da177e4SLinus Torvalds 
349765758202STejun Heo 	case CPU_DOWN_FAILED:
349865758202STejun Heo 	case CPU_ONLINE:
34998db25e78STejun Heo 		gcwq_claim_management_and_lock(gcwq);
35008db25e78STejun Heo 		gcwq->flags &= ~GCWQ_DISASSOCIATED;
35018db25e78STejun Heo 		rebind_workers(gcwq);
35028db25e78STejun Heo 		gcwq_release_management_and_unlock(gcwq);
35038db25e78STejun Heo 		break;
350465758202STejun Heo 	}
350565758202STejun Heo 	return NOTIFY_OK;
350665758202STejun Heo }
350765758202STejun Heo 
350865758202STejun Heo /*
350965758202STejun Heo  * Workqueues should be brought down after normal priority CPU notifiers.
351065758202STejun Heo  * This will be registered as low priority CPU notifier.
351165758202STejun Heo  */
351265758202STejun Heo static int __devinit workqueue_cpu_down_callback(struct notifier_block *nfb,
351365758202STejun Heo 						 unsigned long action,
351465758202STejun Heo 						 void *hcpu)
351565758202STejun Heo {
35168db25e78STejun Heo 	unsigned int cpu = (unsigned long)hcpu;
35178db25e78STejun Heo 	struct work_struct unbind_work;
35188db25e78STejun Heo 
351965758202STejun Heo 	switch (action & ~CPU_TASKS_FROZEN) {
352065758202STejun Heo 	case CPU_DOWN_PREPARE:
35218db25e78STejun Heo 		/* unbinding should happen on the local CPU */
35228db25e78STejun Heo 		INIT_WORK_ONSTACK(&unbind_work, gcwq_unbind_fn);
35238db25e78STejun Heo 		schedule_work_on(cpu, &unbind_work);
35248db25e78STejun Heo 		flush_work(&unbind_work);
35258db25e78STejun Heo 		break;
352665758202STejun Heo 	}
352765758202STejun Heo 	return NOTIFY_OK;
352865758202STejun Heo }
352965758202STejun Heo 
35302d3854a3SRusty Russell #ifdef CONFIG_SMP
35318ccad40dSRusty Russell 
35322d3854a3SRusty Russell struct work_for_cpu {
35336b44003eSAndrew Morton 	struct completion completion;
35342d3854a3SRusty Russell 	long (*fn)(void *);
35352d3854a3SRusty Russell 	void *arg;
35362d3854a3SRusty Russell 	long ret;
35372d3854a3SRusty Russell };
35382d3854a3SRusty Russell 
35396b44003eSAndrew Morton static int do_work_for_cpu(void *_wfc)
35402d3854a3SRusty Russell {
35416b44003eSAndrew Morton 	struct work_for_cpu *wfc = _wfc;
35422d3854a3SRusty Russell 	wfc->ret = wfc->fn(wfc->arg);
35436b44003eSAndrew Morton 	complete(&wfc->completion);
35446b44003eSAndrew Morton 	return 0;
35452d3854a3SRusty Russell }
35462d3854a3SRusty Russell 
35472d3854a3SRusty Russell /**
35482d3854a3SRusty Russell  * work_on_cpu - run a function in user context on a particular cpu
35492d3854a3SRusty Russell  * @cpu: the cpu to run on
35502d3854a3SRusty Russell  * @fn: the function to run
35512d3854a3SRusty Russell  * @arg: the function arg
35522d3854a3SRusty Russell  *
355331ad9081SRusty Russell  * This will return the value @fn returns.
355431ad9081SRusty Russell  * It is up to the caller to ensure that the cpu doesn't go offline.
35556b44003eSAndrew Morton  * The caller must not hold any locks which would prevent @fn from completing.
35562d3854a3SRusty Russell  */
35572d3854a3SRusty Russell long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
35582d3854a3SRusty Russell {
35596b44003eSAndrew Morton 	struct task_struct *sub_thread;
35606b44003eSAndrew Morton 	struct work_for_cpu wfc = {
35616b44003eSAndrew Morton 		.completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
35626b44003eSAndrew Morton 		.fn = fn,
35636b44003eSAndrew Morton 		.arg = arg,
35646b44003eSAndrew Morton 	};
35652d3854a3SRusty Russell 
35666b44003eSAndrew Morton 	sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
35676b44003eSAndrew Morton 	if (IS_ERR(sub_thread))
35686b44003eSAndrew Morton 		return PTR_ERR(sub_thread);
35696b44003eSAndrew Morton 	kthread_bind(sub_thread, cpu);
35706b44003eSAndrew Morton 	wake_up_process(sub_thread);
35716b44003eSAndrew Morton 	wait_for_completion(&wfc.completion);
35722d3854a3SRusty Russell 	return wfc.ret;
35732d3854a3SRusty Russell }
35742d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu);
35752d3854a3SRusty Russell #endif /* CONFIG_SMP */
35762d3854a3SRusty Russell 
3577a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER
3578e7577c50SRusty Russell 
3579a0a1a5fdSTejun Heo /**
3580a0a1a5fdSTejun Heo  * freeze_workqueues_begin - begin freezing workqueues
3581a0a1a5fdSTejun Heo  *
358258a69cb4STejun Heo  * Start freezing workqueues.  After this function returns, all freezable
358358a69cb4STejun Heo  * workqueues will queue new works to their frozen_works list instead of
358458a69cb4STejun Heo  * gcwq->worklist.
3585a0a1a5fdSTejun Heo  *
3586a0a1a5fdSTejun Heo  * CONTEXT:
35878b03ae3cSTejun Heo  * Grabs and releases workqueue_lock and gcwq->lock's.
3588a0a1a5fdSTejun Heo  */
3589a0a1a5fdSTejun Heo void freeze_workqueues_begin(void)
3590a0a1a5fdSTejun Heo {
3591a0a1a5fdSTejun Heo 	unsigned int cpu;
3592a0a1a5fdSTejun Heo 
3593a0a1a5fdSTejun Heo 	spin_lock(&workqueue_lock);
3594a0a1a5fdSTejun Heo 
3595a0a1a5fdSTejun Heo 	BUG_ON(workqueue_freezing);
3596a0a1a5fdSTejun Heo 	workqueue_freezing = true;
3597a0a1a5fdSTejun Heo 
3598f3421797STejun Heo 	for_each_gcwq_cpu(cpu) {
35998b03ae3cSTejun Heo 		struct global_cwq *gcwq = get_gcwq(cpu);
3600bdbc5dd7STejun Heo 		struct workqueue_struct *wq;
36018b03ae3cSTejun Heo 
36028b03ae3cSTejun Heo 		spin_lock_irq(&gcwq->lock);
36038b03ae3cSTejun Heo 
3604db7bccf4STejun Heo 		BUG_ON(gcwq->flags & GCWQ_FREEZING);
3605db7bccf4STejun Heo 		gcwq->flags |= GCWQ_FREEZING;
3606db7bccf4STejun Heo 
3607a0a1a5fdSTejun Heo 		list_for_each_entry(wq, &workqueues, list) {
3608a0a1a5fdSTejun Heo 			struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3609a0a1a5fdSTejun Heo 
361058a69cb4STejun Heo 			if (cwq && wq->flags & WQ_FREEZABLE)
3611a0a1a5fdSTejun Heo 				cwq->max_active = 0;
36121da177e4SLinus Torvalds 		}
36138b03ae3cSTejun Heo 
36148b03ae3cSTejun Heo 		spin_unlock_irq(&gcwq->lock);
3615a0a1a5fdSTejun Heo 	}
3616a0a1a5fdSTejun Heo 
3617a0a1a5fdSTejun Heo 	spin_unlock(&workqueue_lock);
3618a0a1a5fdSTejun Heo }
3619a0a1a5fdSTejun Heo 
3620a0a1a5fdSTejun Heo /**
362158a69cb4STejun Heo  * freeze_workqueues_busy - are freezable workqueues still busy?
3622a0a1a5fdSTejun Heo  *
3623a0a1a5fdSTejun Heo  * Check whether freezing is complete.  This function must be called
3624a0a1a5fdSTejun Heo  * between freeze_workqueues_begin() and thaw_workqueues().
3625a0a1a5fdSTejun Heo  *
3626a0a1a5fdSTejun Heo  * CONTEXT:
3627a0a1a5fdSTejun Heo  * Grabs and releases workqueue_lock.
3628a0a1a5fdSTejun Heo  *
3629a0a1a5fdSTejun Heo  * RETURNS:
363058a69cb4STejun Heo  * %true if some freezable workqueues are still busy.  %false if freezing
363158a69cb4STejun Heo  * is complete.
3632a0a1a5fdSTejun Heo  */
3633a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void)
3634a0a1a5fdSTejun Heo {
3635a0a1a5fdSTejun Heo 	unsigned int cpu;
3636a0a1a5fdSTejun Heo 	bool busy = false;
3637a0a1a5fdSTejun Heo 
3638a0a1a5fdSTejun Heo 	spin_lock(&workqueue_lock);
3639a0a1a5fdSTejun Heo 
3640a0a1a5fdSTejun Heo 	BUG_ON(!workqueue_freezing);
3641a0a1a5fdSTejun Heo 
3642f3421797STejun Heo 	for_each_gcwq_cpu(cpu) {
3643bdbc5dd7STejun Heo 		struct workqueue_struct *wq;
3644a0a1a5fdSTejun Heo 		/*
3645a0a1a5fdSTejun Heo 		 * nr_active is monotonically decreasing.  It's safe
3646a0a1a5fdSTejun Heo 		 * to peek without lock.
3647a0a1a5fdSTejun Heo 		 */
3648a0a1a5fdSTejun Heo 		list_for_each_entry(wq, &workqueues, list) {
3649a0a1a5fdSTejun Heo 			struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3650a0a1a5fdSTejun Heo 
365158a69cb4STejun Heo 			if (!cwq || !(wq->flags & WQ_FREEZABLE))
3652a0a1a5fdSTejun Heo 				continue;
3653a0a1a5fdSTejun Heo 
3654a0a1a5fdSTejun Heo 			BUG_ON(cwq->nr_active < 0);
3655a0a1a5fdSTejun Heo 			if (cwq->nr_active) {
3656a0a1a5fdSTejun Heo 				busy = true;
3657a0a1a5fdSTejun Heo 				goto out_unlock;
3658a0a1a5fdSTejun Heo 			}
3659a0a1a5fdSTejun Heo 		}
3660a0a1a5fdSTejun Heo 	}
3661a0a1a5fdSTejun Heo out_unlock:
3662a0a1a5fdSTejun Heo 	spin_unlock(&workqueue_lock);
3663a0a1a5fdSTejun Heo 	return busy;
3664a0a1a5fdSTejun Heo }
3665a0a1a5fdSTejun Heo 
3666a0a1a5fdSTejun Heo /**
3667a0a1a5fdSTejun Heo  * thaw_workqueues - thaw workqueues
3668a0a1a5fdSTejun Heo  *
3669a0a1a5fdSTejun Heo  * Thaw workqueues.  Normal queueing is restored and all collected
36707e11629dSTejun Heo  * frozen works are transferred to their respective gcwq worklists.
3671a0a1a5fdSTejun Heo  *
3672a0a1a5fdSTejun Heo  * CONTEXT:
36738b03ae3cSTejun Heo  * Grabs and releases workqueue_lock and gcwq->lock's.
3674a0a1a5fdSTejun Heo  */
3675a0a1a5fdSTejun Heo void thaw_workqueues(void)
3676a0a1a5fdSTejun Heo {
3677a0a1a5fdSTejun Heo 	unsigned int cpu;
3678a0a1a5fdSTejun Heo 
3679a0a1a5fdSTejun Heo 	spin_lock(&workqueue_lock);
3680a0a1a5fdSTejun Heo 
3681a0a1a5fdSTejun Heo 	if (!workqueue_freezing)
3682a0a1a5fdSTejun Heo 		goto out_unlock;
3683a0a1a5fdSTejun Heo 
3684f3421797STejun Heo 	for_each_gcwq_cpu(cpu) {
36858b03ae3cSTejun Heo 		struct global_cwq *gcwq = get_gcwq(cpu);
36864ce62e9eSTejun Heo 		struct worker_pool *pool;
3687bdbc5dd7STejun Heo 		struct workqueue_struct *wq;
36888b03ae3cSTejun Heo 
36898b03ae3cSTejun Heo 		spin_lock_irq(&gcwq->lock);
36908b03ae3cSTejun Heo 
3691db7bccf4STejun Heo 		BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3692db7bccf4STejun Heo 		gcwq->flags &= ~GCWQ_FREEZING;
3693db7bccf4STejun Heo 
3694a0a1a5fdSTejun Heo 		list_for_each_entry(wq, &workqueues, list) {
3695a0a1a5fdSTejun Heo 			struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3696a0a1a5fdSTejun Heo 
369758a69cb4STejun Heo 			if (!cwq || !(wq->flags & WQ_FREEZABLE))
3698a0a1a5fdSTejun Heo 				continue;
3699a0a1a5fdSTejun Heo 
3700a0a1a5fdSTejun Heo 			/* restore max_active and repopulate worklist */
3701a0a1a5fdSTejun Heo 			cwq->max_active = wq->saved_max_active;
3702a0a1a5fdSTejun Heo 
3703a0a1a5fdSTejun Heo 			while (!list_empty(&cwq->delayed_works) &&
3704a0a1a5fdSTejun Heo 			       cwq->nr_active < cwq->max_active)
3705a0a1a5fdSTejun Heo 				cwq_activate_first_delayed(cwq);
3706a0a1a5fdSTejun Heo 		}
37078b03ae3cSTejun Heo 
37084ce62e9eSTejun Heo 		for_each_worker_pool(pool, gcwq)
37094ce62e9eSTejun Heo 			wake_up_worker(pool);
3710e22bee78STejun Heo 
37118b03ae3cSTejun Heo 		spin_unlock_irq(&gcwq->lock);
3712a0a1a5fdSTejun Heo 	}
3713a0a1a5fdSTejun Heo 
3714a0a1a5fdSTejun Heo 	workqueue_freezing = false;
3715a0a1a5fdSTejun Heo out_unlock:
3716a0a1a5fdSTejun Heo 	spin_unlock(&workqueue_lock);
3717a0a1a5fdSTejun Heo }
3718a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */
3719a0a1a5fdSTejun Heo 
37206ee0578bSSuresh Siddha static int __init init_workqueues(void)
37211da177e4SLinus Torvalds {
3722c34056a3STejun Heo 	unsigned int cpu;
3723c8e55f36STejun Heo 	int i;
3724c34056a3STejun Heo 
372565758202STejun Heo 	cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
372665758202STejun Heo 	cpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
37278b03ae3cSTejun Heo 
37288b03ae3cSTejun Heo 	/* initialize gcwqs */
3729f3421797STejun Heo 	for_each_gcwq_cpu(cpu) {
37308b03ae3cSTejun Heo 		struct global_cwq *gcwq = get_gcwq(cpu);
37314ce62e9eSTejun Heo 		struct worker_pool *pool;
37328b03ae3cSTejun Heo 
37338b03ae3cSTejun Heo 		spin_lock_init(&gcwq->lock);
37348b03ae3cSTejun Heo 		gcwq->cpu = cpu;
3735f3421797STejun Heo 		gcwq->flags |= GCWQ_DISASSOCIATED;
37368b03ae3cSTejun Heo 
3737c8e55f36STejun Heo 		for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3738c8e55f36STejun Heo 			INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3739c8e55f36STejun Heo 
37404ce62e9eSTejun Heo 		for_each_worker_pool(pool, gcwq) {
37414ce62e9eSTejun Heo 			pool->gcwq = gcwq;
37424ce62e9eSTejun Heo 			INIT_LIST_HEAD(&pool->worklist);
37434ce62e9eSTejun Heo 			INIT_LIST_HEAD(&pool->idle_list);
3744e22bee78STejun Heo 
37454ce62e9eSTejun Heo 			init_timer_deferrable(&pool->idle_timer);
37464ce62e9eSTejun Heo 			pool->idle_timer.function = idle_worker_timeout;
37474ce62e9eSTejun Heo 			pool->idle_timer.data = (unsigned long)pool;
3748e22bee78STejun Heo 
37494ce62e9eSTejun Heo 			setup_timer(&pool->mayday_timer, gcwq_mayday_timeout,
37504ce62e9eSTejun Heo 				    (unsigned long)pool);
37514ce62e9eSTejun Heo 
375260373152STejun Heo 			mutex_init(&pool->manager_mutex);
37534ce62e9eSTejun Heo 			ida_init(&pool->worker_ida);
37544ce62e9eSTejun Heo 		}
3755db7bccf4STejun Heo 
375625511a47STejun Heo 		init_waitqueue_head(&gcwq->rebind_hold);
37578b03ae3cSTejun Heo 	}
37588b03ae3cSTejun Heo 
3759e22bee78STejun Heo 	/* create the initial worker */
3760f3421797STejun Heo 	for_each_online_gcwq_cpu(cpu) {
3761e22bee78STejun Heo 		struct global_cwq *gcwq = get_gcwq(cpu);
37624ce62e9eSTejun Heo 		struct worker_pool *pool;
3763e22bee78STejun Heo 
3764477a3c33STejun Heo 		if (cpu != WORK_CPU_UNBOUND)
3765477a3c33STejun Heo 			gcwq->flags &= ~GCWQ_DISASSOCIATED;
37664ce62e9eSTejun Heo 
37674ce62e9eSTejun Heo 		for_each_worker_pool(pool, gcwq) {
37684ce62e9eSTejun Heo 			struct worker *worker;
37694ce62e9eSTejun Heo 
3770bc2ae0f5STejun Heo 			worker = create_worker(pool);
3771e22bee78STejun Heo 			BUG_ON(!worker);
3772e22bee78STejun Heo 			spin_lock_irq(&gcwq->lock);
3773e22bee78STejun Heo 			start_worker(worker);
3774e22bee78STejun Heo 			spin_unlock_irq(&gcwq->lock);
3775e22bee78STejun Heo 		}
37764ce62e9eSTejun Heo 	}
3777e22bee78STejun Heo 
3778d320c038STejun Heo 	system_wq = alloc_workqueue("events", 0, 0);
3779d320c038STejun Heo 	system_long_wq = alloc_workqueue("events_long", 0, 0);
3780d320c038STejun Heo 	system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
3781f3421797STejun Heo 	system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3782f3421797STejun Heo 					    WQ_UNBOUND_MAX_ACTIVE);
378324d51addSTejun Heo 	system_freezable_wq = alloc_workqueue("events_freezable",
378424d51addSTejun Heo 					      WQ_FREEZABLE, 0);
378562d3c543SAlan Stern 	system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable",
378662d3c543SAlan Stern 			WQ_NON_REENTRANT | WQ_FREEZABLE, 0);
3787e5cba24eSHitoshi Mitake 	BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq ||
378862d3c543SAlan Stern 	       !system_unbound_wq || !system_freezable_wq ||
378962d3c543SAlan Stern 		!system_nrt_freezable_wq);
37906ee0578bSSuresh Siddha 	return 0;
37911da177e4SLinus Torvalds }
37926ee0578bSSuresh Siddha early_initcall(init_workqueues);
3793