xref: /linux-6.15/kernel/workqueue.c (revision 29c91e99)
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>
44*29c91e99STejun Heo #include <linux/jhash.h>
4542f8570fSSasha Levin #include <linux/hashtable.h>
4676af4d93STejun Heo #include <linux/rculist.h>
47e22bee78STejun Heo 
48ea138446STejun Heo #include "workqueue_internal.h"
491da177e4SLinus Torvalds 
50c8e55f36STejun Heo enum {
51bc2ae0f5STejun Heo 	/*
5224647570STejun Heo 	 * worker_pool flags
53bc2ae0f5STejun Heo 	 *
5424647570STejun Heo 	 * A bound pool is either associated or disassociated with its CPU.
55bc2ae0f5STejun Heo 	 * While associated (!DISASSOCIATED), all workers are bound to the
56bc2ae0f5STejun Heo 	 * CPU and none has %WORKER_UNBOUND set and concurrency management
57bc2ae0f5STejun Heo 	 * is in effect.
58bc2ae0f5STejun Heo 	 *
59bc2ae0f5STejun Heo 	 * While DISASSOCIATED, the cpu may be offline and all workers have
60bc2ae0f5STejun Heo 	 * %WORKER_UNBOUND set and concurrency management disabled, and may
6124647570STejun Heo 	 * be executing on any CPU.  The pool behaves as an unbound one.
62bc2ae0f5STejun Heo 	 *
63bc2ae0f5STejun Heo 	 * Note that DISASSOCIATED can be flipped only while holding
6424647570STejun Heo 	 * assoc_mutex to avoid changing binding state while
6524647570STejun Heo 	 * create_worker() is in progress.
66bc2ae0f5STejun Heo 	 */
6711ebea50STejun Heo 	POOL_MANAGE_WORKERS	= 1 << 0,	/* need to manage workers */
6824647570STejun Heo 	POOL_DISASSOCIATED	= 1 << 2,	/* cpu can't serve workers */
6935b6bb63STejun Heo 	POOL_FREEZING		= 1 << 3,	/* freeze in progress */
70db7bccf4STejun Heo 
71c8e55f36STejun Heo 	/* worker flags */
72c8e55f36STejun Heo 	WORKER_STARTED		= 1 << 0,	/* started */
73c8e55f36STejun Heo 	WORKER_DIE		= 1 << 1,	/* die die die */
74c8e55f36STejun Heo 	WORKER_IDLE		= 1 << 2,	/* is idle */
75e22bee78STejun Heo 	WORKER_PREP		= 1 << 3,	/* preparing to run works */
76fb0e7bebSTejun Heo 	WORKER_CPU_INTENSIVE	= 1 << 6,	/* cpu intensive */
77f3421797STejun Heo 	WORKER_UNBOUND		= 1 << 7,	/* worker is unbound */
78e22bee78STejun Heo 
795f7dabfdSLai Jiangshan 	WORKER_NOT_RUNNING	= WORKER_PREP | WORKER_UNBOUND |
80403c821dSTejun Heo 				  WORKER_CPU_INTENSIVE,
81db7bccf4STejun Heo 
82e34cdddbSTejun Heo 	NR_STD_WORKER_POOLS	= 2,		/* # standard pools per cpu */
834ce62e9eSTejun Heo 
84*29c91e99STejun Heo 	UNBOUND_POOL_HASH_ORDER	= 6,		/* hashed by pool->attrs */
85c8e55f36STejun Heo 	BUSY_WORKER_HASH_ORDER	= 6,		/* 64 pointers */
86db7bccf4STejun Heo 
87e22bee78STejun Heo 	MAX_IDLE_WORKERS_RATIO	= 4,		/* 1/4 of busy can be idle */
88e22bee78STejun Heo 	IDLE_WORKER_TIMEOUT	= 300 * HZ,	/* keep idle ones for 5 mins */
89e22bee78STejun Heo 
903233cdbdSTejun Heo 	MAYDAY_INITIAL_TIMEOUT  = HZ / 100 >= 2 ? HZ / 100 : 2,
913233cdbdSTejun Heo 						/* call for help after 10ms
923233cdbdSTejun Heo 						   (min two ticks) */
93e22bee78STejun Heo 	MAYDAY_INTERVAL		= HZ / 10,	/* and then every 100ms */
94e22bee78STejun Heo 	CREATE_COOLDOWN		= HZ,		/* time to breath after fail */
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 	/*
97e22bee78STejun Heo 	 * Rescue workers are used only on emergencies and shared by
98e22bee78STejun Heo 	 * all cpus.  Give -20.
99e22bee78STejun Heo 	 */
100e22bee78STejun Heo 	RESCUER_NICE_LEVEL	= -20,
1013270476aSTejun Heo 	HIGHPRI_NICE_LEVEL	= -20,
102c8e55f36STejun Heo };
103c8e55f36STejun Heo 
1041da177e4SLinus Torvalds /*
1054690c4abSTejun Heo  * Structure fields follow one of the following exclusion rules.
1064690c4abSTejun Heo  *
107e41e704bSTejun Heo  * I: Modifiable by initialization/destruction paths and read-only for
108e41e704bSTejun Heo  *    everyone else.
1094690c4abSTejun Heo  *
110e22bee78STejun Heo  * P: Preemption protected.  Disabling preemption is enough and should
111e22bee78STejun Heo  *    only be modified and accessed from the local cpu.
112e22bee78STejun Heo  *
113d565ed63STejun Heo  * L: pool->lock protected.  Access with pool->lock held.
1144690c4abSTejun Heo  *
115d565ed63STejun Heo  * X: During normal operation, modification requires pool->lock and should
116d565ed63STejun Heo  *    be done only from local cpu.  Either disabling preemption on local
117d565ed63STejun Heo  *    cpu or grabbing pool->lock is enough for read access.  If
118d565ed63STejun Heo  *    POOL_DISASSOCIATED is set, it's identical to L.
119e22bee78STejun Heo  *
12073f53c4aSTejun Heo  * F: wq->flush_mutex protected.
12173f53c4aSTejun Heo  *
1224690c4abSTejun Heo  * W: workqueue_lock protected.
12376af4d93STejun Heo  *
12476af4d93STejun Heo  * R: workqueue_lock protected for writes.  Sched-RCU protected for reads.
1254690c4abSTejun Heo  */
1264690c4abSTejun Heo 
1272eaebdb3STejun Heo /* struct worker is defined in workqueue_internal.h */
128c34056a3STejun Heo 
129bd7bdd43STejun Heo struct worker_pool {
130d565ed63STejun Heo 	spinlock_t		lock;		/* the pool lock */
131d84ff051STejun Heo 	int			cpu;		/* I: the associated cpu */
1329daf9e67STejun Heo 	int			id;		/* I: pool ID */
13311ebea50STejun Heo 	unsigned int		flags;		/* X: flags */
134bd7bdd43STejun Heo 
135bd7bdd43STejun Heo 	struct list_head	worklist;	/* L: list of pending works */
136bd7bdd43STejun Heo 	int			nr_workers;	/* L: total number of workers */
137ea1abd61SLai Jiangshan 
138ea1abd61SLai Jiangshan 	/* nr_idle includes the ones off idle_list for rebinding */
139bd7bdd43STejun Heo 	int			nr_idle;	/* L: currently idle ones */
140bd7bdd43STejun Heo 
141bd7bdd43STejun Heo 	struct list_head	idle_list;	/* X: list of idle workers */
142bd7bdd43STejun Heo 	struct timer_list	idle_timer;	/* L: worker idle timeout */
143bd7bdd43STejun Heo 	struct timer_list	mayday_timer;	/* L: SOS timer for workers */
144bd7bdd43STejun Heo 
145c9e7cf27STejun Heo 	/* workers are chained either in busy_hash or idle_list */
146c9e7cf27STejun Heo 	DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
147c9e7cf27STejun Heo 						/* L: hash of busy workers */
148c9e7cf27STejun Heo 
14934a06bd6STejun Heo 	struct mutex		manager_arb;	/* manager arbitration */
15024647570STejun Heo 	struct mutex		assoc_mutex;	/* protect POOL_DISASSOCIATED */
151bd7bdd43STejun Heo 	struct ida		worker_ida;	/* L: for worker IDs */
152e19e397aSTejun Heo 
1537a4e344cSTejun Heo 	struct workqueue_attrs	*attrs;		/* I: worker attributes */
154*29c91e99STejun Heo 	struct hlist_node	hash_node;	/* R: unbound_pool_hash node */
155*29c91e99STejun Heo 	int			refcnt;		/* refcnt for unbound pools */
1567a4e344cSTejun Heo 
157e19e397aSTejun Heo 	/*
158e19e397aSTejun Heo 	 * The current concurrency level.  As it's likely to be accessed
159e19e397aSTejun Heo 	 * from other CPUs during try_to_wake_up(), put it in a separate
160e19e397aSTejun Heo 	 * cacheline.
161e19e397aSTejun Heo 	 */
162e19e397aSTejun Heo 	atomic_t		nr_running ____cacheline_aligned_in_smp;
163*29c91e99STejun Heo 
164*29c91e99STejun Heo 	/*
165*29c91e99STejun Heo 	 * Destruction of pool is sched-RCU protected to allow dereferences
166*29c91e99STejun Heo 	 * from get_work_pool().
167*29c91e99STejun Heo 	 */
168*29c91e99STejun Heo 	struct rcu_head		rcu;
1698b03ae3cSTejun Heo } ____cacheline_aligned_in_smp;
1708b03ae3cSTejun Heo 
1718b03ae3cSTejun Heo /*
172112202d9STejun Heo  * The per-pool workqueue.  While queued, the lower WORK_STRUCT_FLAG_BITS
173112202d9STejun Heo  * of work_struct->data are used for flags and the remaining high bits
174112202d9STejun Heo  * point to the pwq; thus, pwqs need to be aligned at two's power of the
175112202d9STejun Heo  * number of flag bits.
1761da177e4SLinus Torvalds  */
177112202d9STejun Heo struct pool_workqueue {
178bd7bdd43STejun Heo 	struct worker_pool	*pool;		/* I: the associated pool */
1794690c4abSTejun Heo 	struct workqueue_struct *wq;		/* I: the owning workqueue */
18073f53c4aSTejun Heo 	int			work_color;	/* L: current color */
18173f53c4aSTejun Heo 	int			flush_color;	/* L: flushing color */
18273f53c4aSTejun Heo 	int			nr_in_flight[WORK_NR_COLORS];
18373f53c4aSTejun Heo 						/* L: nr of in_flight works */
1841e19ffc6STejun Heo 	int			nr_active;	/* L: nr of active works */
185a0a1a5fdSTejun Heo 	int			max_active;	/* L: max active works */
1861e19ffc6STejun Heo 	struct list_head	delayed_works;	/* L: delayed works */
18776af4d93STejun Heo 	struct list_head	pwqs_node;	/* R: node on wq->pwqs */
188493a1724STejun Heo 	struct list_head	mayday_node;	/* W: node on wq->maydays */
189e904e6c2STejun Heo } __aligned(1 << WORK_STRUCT_FLAG_BITS);
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds /*
19273f53c4aSTejun Heo  * Structure used to wait for workqueue flush.
19373f53c4aSTejun Heo  */
19473f53c4aSTejun Heo struct wq_flusher {
19573f53c4aSTejun Heo 	struct list_head	list;		/* F: list of flushers */
19673f53c4aSTejun Heo 	int			flush_color;	/* F: flush color waiting for */
19773f53c4aSTejun Heo 	struct completion	done;		/* flush completion */
19873f53c4aSTejun Heo };
1991da177e4SLinus Torvalds 
20073f53c4aSTejun Heo /*
2011da177e4SLinus Torvalds  * The externally visible workqueue abstraction is an array of
2021da177e4SLinus Torvalds  * per-CPU workqueues:
2031da177e4SLinus Torvalds  */
2041da177e4SLinus Torvalds struct workqueue_struct {
2059c5a2ba7STejun Heo 	unsigned int		flags;		/* W: WQ_* flags */
206420c0ddbSTejun Heo 	struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwq's */
20776af4d93STejun Heo 	struct list_head	pwqs;		/* R: all pwqs of this wq */
2084690c4abSTejun Heo 	struct list_head	list;		/* W: list of all workqueues */
20973f53c4aSTejun Heo 
21073f53c4aSTejun Heo 	struct mutex		flush_mutex;	/* protects wq flushing */
21173f53c4aSTejun Heo 	int			work_color;	/* F: current work color */
21273f53c4aSTejun Heo 	int			flush_color;	/* F: current flush color */
213112202d9STejun Heo 	atomic_t		nr_pwqs_to_flush; /* flush in progress */
21473f53c4aSTejun Heo 	struct wq_flusher	*first_flusher;	/* F: first flusher */
21573f53c4aSTejun Heo 	struct list_head	flusher_queue;	/* F: flush waiters */
21673f53c4aSTejun Heo 	struct list_head	flusher_overflow; /* F: flush overflow list */
21773f53c4aSTejun Heo 
218493a1724STejun Heo 	struct list_head	maydays;	/* W: pwqs requesting rescue */
219e22bee78STejun Heo 	struct worker		*rescuer;	/* I: rescue worker */
220e22bee78STejun Heo 
2219c5a2ba7STejun Heo 	int			nr_drainers;	/* W: drain in progress */
222112202d9STejun Heo 	int			saved_max_active; /* W: saved pwq max_active */
2234e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
2244e6045f1SJohannes Berg 	struct lockdep_map	lockdep_map;
2254e6045f1SJohannes Berg #endif
226b196be89STejun Heo 	char			name[];		/* I: workqueue name */
2271da177e4SLinus Torvalds };
2281da177e4SLinus Torvalds 
229e904e6c2STejun Heo static struct kmem_cache *pwq_cache;
230e904e6c2STejun Heo 
231*29c91e99STejun Heo /* hash of all unbound pools keyed by pool->attrs */
232*29c91e99STejun Heo static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER);
233*29c91e99STejun Heo 
234*29c91e99STejun Heo static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS];
235*29c91e99STejun Heo 
236d320c038STejun Heo struct workqueue_struct *system_wq __read_mostly;
237d320c038STejun Heo EXPORT_SYMBOL_GPL(system_wq);
238044c782cSValentin Ilie struct workqueue_struct *system_highpri_wq __read_mostly;
2391aabe902SJoonsoo Kim EXPORT_SYMBOL_GPL(system_highpri_wq);
240044c782cSValentin Ilie struct workqueue_struct *system_long_wq __read_mostly;
241d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq);
242044c782cSValentin Ilie struct workqueue_struct *system_unbound_wq __read_mostly;
243f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq);
244044c782cSValentin Ilie struct workqueue_struct *system_freezable_wq __read_mostly;
24524d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq);
246d320c038STejun Heo 
24797bd2347STejun Heo #define CREATE_TRACE_POINTS
24897bd2347STejun Heo #include <trace/events/workqueue.h>
24997bd2347STejun Heo 
25076af4d93STejun Heo #define assert_rcu_or_wq_lock()						\
25176af4d93STejun Heo 	rcu_lockdep_assert(rcu_read_lock_sched_held() ||		\
25276af4d93STejun Heo 			   lockdep_is_held(&workqueue_lock),		\
25376af4d93STejun Heo 			   "sched RCU or workqueue lock should be held")
25476af4d93STejun Heo 
25538db41d9STejun Heo #define for_each_std_worker_pool(pool, cpu)				\
256a60dc39cSTejun Heo 	for ((pool) = &std_worker_pools(cpu)[0];			\
257a60dc39cSTejun Heo 	     (pool) < &std_worker_pools(cpu)[NR_STD_WORKER_POOLS]; (pool)++)
2584ce62e9eSTejun Heo 
259b67bfe0dSSasha Levin #define for_each_busy_worker(worker, i, pool)				\
260b67bfe0dSSasha Levin 	hash_for_each(pool->busy_hash, i, worker, hentry)
261db7bccf4STejun Heo 
262706026c2STejun Heo static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
263f3421797STejun Heo 				unsigned int sw)
264f3421797STejun Heo {
265f3421797STejun Heo 	if (cpu < nr_cpu_ids) {
266f3421797STejun Heo 		if (sw & 1) {
267f3421797STejun Heo 			cpu = cpumask_next(cpu, mask);
268f3421797STejun Heo 			if (cpu < nr_cpu_ids)
269f3421797STejun Heo 				return cpu;
270f3421797STejun Heo 		}
271f3421797STejun Heo 		if (sw & 2)
272f3421797STejun Heo 			return WORK_CPU_UNBOUND;
273f3421797STejun Heo 	}
2746be19588SLai Jiangshan 	return WORK_CPU_END;
275f3421797STejun Heo }
276f3421797STejun Heo 
27709884951STejun Heo /*
27809884951STejun Heo  * CPU iterators
27909884951STejun Heo  *
280706026c2STejun Heo  * An extra cpu number is defined using an invalid cpu number
28109884951STejun Heo  * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
282706026c2STejun Heo  * specific CPU.  The following iterators are similar to for_each_*_cpu()
283706026c2STejun Heo  * iterators but also considers the unbound CPU.
28409884951STejun Heo  *
285706026c2STejun Heo  * for_each_wq_cpu()		: possible CPUs + WORK_CPU_UNBOUND
286706026c2STejun Heo  * for_each_online_wq_cpu()	: online CPUs + WORK_CPU_UNBOUND
28709884951STejun Heo  */
288706026c2STejun Heo #define for_each_wq_cpu(cpu)						\
289706026c2STejun Heo 	for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, 3);		\
2906be19588SLai Jiangshan 	     (cpu) < WORK_CPU_END;					\
291706026c2STejun Heo 	     (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, 3))
292f3421797STejun Heo 
293706026c2STejun Heo #define for_each_online_wq_cpu(cpu)					\
294706026c2STejun Heo 	for ((cpu) = __next_wq_cpu(-1, cpu_online_mask, 3);		\
2956be19588SLai Jiangshan 	     (cpu) < WORK_CPU_END;					\
296706026c2STejun Heo 	     (cpu) = __next_wq_cpu((cpu), cpu_online_mask, 3))
297f3421797STejun Heo 
29849e3cf44STejun Heo /**
29917116969STejun Heo  * for_each_pool - iterate through all worker_pools in the system
30017116969STejun Heo  * @pool: iteration cursor
30117116969STejun Heo  * @id: integer used for iteration
302fa1b54e6STejun Heo  *
303fa1b54e6STejun Heo  * This must be called either with workqueue_lock held or sched RCU read
304fa1b54e6STejun Heo  * locked.  If the pool needs to be used beyond the locking in effect, the
305fa1b54e6STejun Heo  * caller is responsible for guaranteeing that the pool stays online.
306fa1b54e6STejun Heo  *
307fa1b54e6STejun Heo  * The if/else clause exists only for the lockdep assertion and can be
308fa1b54e6STejun Heo  * ignored.
30917116969STejun Heo  */
31017116969STejun Heo #define for_each_pool(pool, id)						\
311fa1b54e6STejun Heo 	idr_for_each_entry(&worker_pool_idr, pool, id)			\
312fa1b54e6STejun Heo 		if (({ assert_rcu_or_wq_lock(); false; })) { }		\
313fa1b54e6STejun Heo 		else
31417116969STejun Heo 
31517116969STejun Heo /**
31649e3cf44STejun Heo  * for_each_pwq - iterate through all pool_workqueues of the specified workqueue
31749e3cf44STejun Heo  * @pwq: iteration cursor
31849e3cf44STejun Heo  * @wq: the target workqueue
31976af4d93STejun Heo  *
32076af4d93STejun Heo  * This must be called either with workqueue_lock held or sched RCU read
32176af4d93STejun Heo  * locked.  If the pwq needs to be used beyond the locking in effect, the
32276af4d93STejun Heo  * caller is responsible for guaranteeing that the pwq stays online.
32376af4d93STejun Heo  *
32476af4d93STejun Heo  * The if/else clause exists only for the lockdep assertion and can be
32576af4d93STejun Heo  * ignored.
32649e3cf44STejun Heo  */
32749e3cf44STejun Heo #define for_each_pwq(pwq, wq)						\
32876af4d93STejun Heo 	list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node)		\
32976af4d93STejun Heo 		if (({ assert_rcu_or_wq_lock(); false; })) { }		\
33076af4d93STejun Heo 		else
331f3421797STejun Heo 
332dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK
333dc186ad7SThomas Gleixner 
334dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr;
335dc186ad7SThomas Gleixner 
33699777288SStanislaw Gruszka static void *work_debug_hint(void *addr)
33799777288SStanislaw Gruszka {
33899777288SStanislaw Gruszka 	return ((struct work_struct *) addr)->func;
33999777288SStanislaw Gruszka }
34099777288SStanislaw Gruszka 
341dc186ad7SThomas Gleixner /*
342dc186ad7SThomas Gleixner  * fixup_init is called when:
343dc186ad7SThomas Gleixner  * - an active object is initialized
344dc186ad7SThomas Gleixner  */
345dc186ad7SThomas Gleixner static int work_fixup_init(void *addr, enum debug_obj_state state)
346dc186ad7SThomas Gleixner {
347dc186ad7SThomas Gleixner 	struct work_struct *work = addr;
348dc186ad7SThomas Gleixner 
349dc186ad7SThomas Gleixner 	switch (state) {
350dc186ad7SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
351dc186ad7SThomas Gleixner 		cancel_work_sync(work);
352dc186ad7SThomas Gleixner 		debug_object_init(work, &work_debug_descr);
353dc186ad7SThomas Gleixner 		return 1;
354dc186ad7SThomas Gleixner 	default:
355dc186ad7SThomas Gleixner 		return 0;
356dc186ad7SThomas Gleixner 	}
357dc186ad7SThomas Gleixner }
358dc186ad7SThomas Gleixner 
359dc186ad7SThomas Gleixner /*
360dc186ad7SThomas Gleixner  * fixup_activate is called when:
361dc186ad7SThomas Gleixner  * - an active object is activated
362dc186ad7SThomas Gleixner  * - an unknown object is activated (might be a statically initialized object)
363dc186ad7SThomas Gleixner  */
364dc186ad7SThomas Gleixner static int work_fixup_activate(void *addr, enum debug_obj_state state)
365dc186ad7SThomas Gleixner {
366dc186ad7SThomas Gleixner 	struct work_struct *work = addr;
367dc186ad7SThomas Gleixner 
368dc186ad7SThomas Gleixner 	switch (state) {
369dc186ad7SThomas Gleixner 
370dc186ad7SThomas Gleixner 	case ODEBUG_STATE_NOTAVAILABLE:
371dc186ad7SThomas Gleixner 		/*
372dc186ad7SThomas Gleixner 		 * This is not really a fixup. The work struct was
373dc186ad7SThomas Gleixner 		 * statically initialized. We just make sure that it
374dc186ad7SThomas Gleixner 		 * is tracked in the object tracker.
375dc186ad7SThomas Gleixner 		 */
37622df02bbSTejun Heo 		if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
377dc186ad7SThomas Gleixner 			debug_object_init(work, &work_debug_descr);
378dc186ad7SThomas Gleixner 			debug_object_activate(work, &work_debug_descr);
379dc186ad7SThomas Gleixner 			return 0;
380dc186ad7SThomas Gleixner 		}
381dc186ad7SThomas Gleixner 		WARN_ON_ONCE(1);
382dc186ad7SThomas Gleixner 		return 0;
383dc186ad7SThomas Gleixner 
384dc186ad7SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
385dc186ad7SThomas Gleixner 		WARN_ON(1);
386dc186ad7SThomas Gleixner 
387dc186ad7SThomas Gleixner 	default:
388dc186ad7SThomas Gleixner 		return 0;
389dc186ad7SThomas Gleixner 	}
390dc186ad7SThomas Gleixner }
391dc186ad7SThomas Gleixner 
392dc186ad7SThomas Gleixner /*
393dc186ad7SThomas Gleixner  * fixup_free is called when:
394dc186ad7SThomas Gleixner  * - an active object is freed
395dc186ad7SThomas Gleixner  */
396dc186ad7SThomas Gleixner static int work_fixup_free(void *addr, enum debug_obj_state state)
397dc186ad7SThomas Gleixner {
398dc186ad7SThomas Gleixner 	struct work_struct *work = addr;
399dc186ad7SThomas Gleixner 
400dc186ad7SThomas Gleixner 	switch (state) {
401dc186ad7SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
402dc186ad7SThomas Gleixner 		cancel_work_sync(work);
403dc186ad7SThomas Gleixner 		debug_object_free(work, &work_debug_descr);
404dc186ad7SThomas Gleixner 		return 1;
405dc186ad7SThomas Gleixner 	default:
406dc186ad7SThomas Gleixner 		return 0;
407dc186ad7SThomas Gleixner 	}
408dc186ad7SThomas Gleixner }
409dc186ad7SThomas Gleixner 
410dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr = {
411dc186ad7SThomas Gleixner 	.name		= "work_struct",
41299777288SStanislaw Gruszka 	.debug_hint	= work_debug_hint,
413dc186ad7SThomas Gleixner 	.fixup_init	= work_fixup_init,
414dc186ad7SThomas Gleixner 	.fixup_activate	= work_fixup_activate,
415dc186ad7SThomas Gleixner 	.fixup_free	= work_fixup_free,
416dc186ad7SThomas Gleixner };
417dc186ad7SThomas Gleixner 
418dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work)
419dc186ad7SThomas Gleixner {
420dc186ad7SThomas Gleixner 	debug_object_activate(work, &work_debug_descr);
421dc186ad7SThomas Gleixner }
422dc186ad7SThomas Gleixner 
423dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work)
424dc186ad7SThomas Gleixner {
425dc186ad7SThomas Gleixner 	debug_object_deactivate(work, &work_debug_descr);
426dc186ad7SThomas Gleixner }
427dc186ad7SThomas Gleixner 
428dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack)
429dc186ad7SThomas Gleixner {
430dc186ad7SThomas Gleixner 	if (onstack)
431dc186ad7SThomas Gleixner 		debug_object_init_on_stack(work, &work_debug_descr);
432dc186ad7SThomas Gleixner 	else
433dc186ad7SThomas Gleixner 		debug_object_init(work, &work_debug_descr);
434dc186ad7SThomas Gleixner }
435dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work);
436dc186ad7SThomas Gleixner 
437dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work)
438dc186ad7SThomas Gleixner {
439dc186ad7SThomas Gleixner 	debug_object_free(work, &work_debug_descr);
440dc186ad7SThomas Gleixner }
441dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack);
442dc186ad7SThomas Gleixner 
443dc186ad7SThomas Gleixner #else
444dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { }
445dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { }
446dc186ad7SThomas Gleixner #endif
447dc186ad7SThomas Gleixner 
44895402b38SGautham R Shenoy /* Serializes the accesses to the list of workqueues. */
44995402b38SGautham R Shenoy static DEFINE_SPINLOCK(workqueue_lock);
4501da177e4SLinus Torvalds static LIST_HEAD(workqueues);
451a0a1a5fdSTejun Heo static bool workqueue_freezing;		/* W: have wqs started freezing? */
4521da177e4SLinus Torvalds 
45314441960SOleg Nesterov /*
454e19e397aSTejun Heo  * The CPU and unbound standard worker pools.  The unbound ones have
455e19e397aSTejun Heo  * POOL_DISASSOCIATED set, and their workers have WORKER_UNBOUND set.
45614441960SOleg Nesterov  */
457e19e397aSTejun Heo static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
458a60dc39cSTejun Heo 				     cpu_std_worker_pools);
459a60dc39cSTejun Heo static struct worker_pool unbound_std_worker_pools[NR_STD_WORKER_POOLS];
460f3421797STejun Heo 
461fa1b54e6STejun Heo /*
462fa1b54e6STejun Heo  * idr of all pools.  Modifications are protected by workqueue_lock.  Read
463fa1b54e6STejun Heo  * accesses are protected by sched-RCU protected.
464fa1b54e6STejun Heo  */
4659daf9e67STejun Heo static DEFINE_IDR(worker_pool_idr);
4669daf9e67STejun Heo 
467c34056a3STejun Heo static int worker_thread(void *__worker);
4681da177e4SLinus Torvalds 
469a60dc39cSTejun Heo static struct worker_pool *std_worker_pools(int cpu)
4701da177e4SLinus Torvalds {
471f3421797STejun Heo 	if (cpu != WORK_CPU_UNBOUND)
472a60dc39cSTejun Heo 		return per_cpu(cpu_std_worker_pools, cpu);
473f3421797STejun Heo 	else
474a60dc39cSTejun Heo 		return unbound_std_worker_pools;
4751da177e4SLinus Torvalds }
4761da177e4SLinus Torvalds 
4774e8f0a60STejun Heo static int std_worker_pool_pri(struct worker_pool *pool)
4784e8f0a60STejun Heo {
479a60dc39cSTejun Heo 	return pool - std_worker_pools(pool->cpu);
4804e8f0a60STejun Heo }
4814e8f0a60STejun Heo 
4829daf9e67STejun Heo /* allocate ID and assign it to @pool */
4839daf9e67STejun Heo static int worker_pool_assign_id(struct worker_pool *pool)
4849daf9e67STejun Heo {
4859daf9e67STejun Heo 	int ret;
4869daf9e67STejun Heo 
487fa1b54e6STejun Heo 	do {
488fa1b54e6STejun Heo 		if (!idr_pre_get(&worker_pool_idr, GFP_KERNEL))
489fa1b54e6STejun Heo 			return -ENOMEM;
490fa1b54e6STejun Heo 
491fa1b54e6STejun Heo 		spin_lock_irq(&workqueue_lock);
4929daf9e67STejun Heo 		ret = idr_get_new(&worker_pool_idr, pool, &pool->id);
493fa1b54e6STejun Heo 		spin_unlock_irq(&workqueue_lock);
494fa1b54e6STejun Heo 	} while (ret == -EAGAIN);
4959daf9e67STejun Heo 
4969daf9e67STejun Heo 	return ret;
4979daf9e67STejun Heo }
4989daf9e67STejun Heo 
499d565ed63STejun Heo static struct worker_pool *get_std_worker_pool(int cpu, bool highpri)
500d565ed63STejun Heo {
501a60dc39cSTejun Heo 	struct worker_pool *pools = std_worker_pools(cpu);
502d565ed63STejun Heo 
503a60dc39cSTejun Heo 	return &pools[highpri];
504d565ed63STejun Heo }
505d565ed63STejun Heo 
50676af4d93STejun Heo /**
50776af4d93STejun Heo  * first_pwq - return the first pool_workqueue of the specified workqueue
50876af4d93STejun Heo  * @wq: the target workqueue
50976af4d93STejun Heo  *
51076af4d93STejun Heo  * This must be called either with workqueue_lock held or sched RCU read
51176af4d93STejun Heo  * locked.  If the pwq needs to be used beyond the locking in effect, the
51276af4d93STejun Heo  * caller is responsible for guaranteeing that the pwq stays online.
51376af4d93STejun Heo  */
5147fb98ea7STejun Heo static struct pool_workqueue *first_pwq(struct workqueue_struct *wq)
515a848e3b6SOleg Nesterov {
51676af4d93STejun Heo 	assert_rcu_or_wq_lock();
51776af4d93STejun Heo 	return list_first_or_null_rcu(&wq->pwqs, struct pool_workqueue,
51876af4d93STejun Heo 				      pwqs_node);
519f3421797STejun Heo }
520a848e3b6SOleg Nesterov 
52173f53c4aSTejun Heo static unsigned int work_color_to_flags(int color)
52273f53c4aSTejun Heo {
52373f53c4aSTejun Heo 	return color << WORK_STRUCT_COLOR_SHIFT;
52473f53c4aSTejun Heo }
52573f53c4aSTejun Heo 
52673f53c4aSTejun Heo static int get_work_color(struct work_struct *work)
52773f53c4aSTejun Heo {
52873f53c4aSTejun Heo 	return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
52973f53c4aSTejun Heo 		((1 << WORK_STRUCT_COLOR_BITS) - 1);
53073f53c4aSTejun Heo }
53173f53c4aSTejun Heo 
53273f53c4aSTejun Heo static int work_next_color(int color)
53373f53c4aSTejun Heo {
53473f53c4aSTejun Heo 	return (color + 1) % WORK_NR_COLORS;
535b1f4ec17SOleg Nesterov }
536b1f4ec17SOleg Nesterov 
5374594bf15SDavid Howells /*
538112202d9STejun Heo  * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data
539112202d9STejun Heo  * contain the pointer to the queued pwq.  Once execution starts, the flag
5407c3eed5cSTejun Heo  * is cleared and the high bits contain OFFQ flags and pool ID.
5417a22ad75STejun Heo  *
542112202d9STejun Heo  * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
543112202d9STejun Heo  * and clear_work_data() can be used to set the pwq, pool or clear
544bbb68dfaSTejun Heo  * work->data.  These functions should only be called while the work is
545bbb68dfaSTejun Heo  * owned - ie. while the PENDING bit is set.
5467a22ad75STejun Heo  *
547112202d9STejun Heo  * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq
5487c3eed5cSTejun Heo  * corresponding to a work.  Pool is available once the work has been
549112202d9STejun Heo  * queued anywhere after initialization until it is sync canceled.  pwq is
5507c3eed5cSTejun Heo  * available only while the work item is queued.
551bbb68dfaSTejun Heo  *
552bbb68dfaSTejun Heo  * %WORK_OFFQ_CANCELING is used to mark a work item which is being
553bbb68dfaSTejun Heo  * canceled.  While being canceled, a work item may have its PENDING set
554bbb68dfaSTejun Heo  * but stay off timer and worklist for arbitrarily long and nobody should
555bbb68dfaSTejun Heo  * try to steal the PENDING bit.
5564594bf15SDavid Howells  */
5577a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data,
5587a22ad75STejun Heo 				 unsigned long flags)
5597a22ad75STejun Heo {
5606183c009STejun Heo 	WARN_ON_ONCE(!work_pending(work));
5617a22ad75STejun Heo 	atomic_long_set(&work->data, data | flags | work_static(work));
5627a22ad75STejun Heo }
5637a22ad75STejun Heo 
564112202d9STejun Heo static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq,
5654690c4abSTejun Heo 			 unsigned long extra_flags)
566365970a1SDavid Howells {
567112202d9STejun Heo 	set_work_data(work, (unsigned long)pwq,
568112202d9STejun Heo 		      WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags);
569365970a1SDavid Howells }
570365970a1SDavid Howells 
5714468a00fSLai Jiangshan static void set_work_pool_and_keep_pending(struct work_struct *work,
5724468a00fSLai Jiangshan 					   int pool_id)
5734468a00fSLai Jiangshan {
5744468a00fSLai Jiangshan 	set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT,
5754468a00fSLai Jiangshan 		      WORK_STRUCT_PENDING);
5764468a00fSLai Jiangshan }
5774468a00fSLai Jiangshan 
5787c3eed5cSTejun Heo static void set_work_pool_and_clear_pending(struct work_struct *work,
5797c3eed5cSTejun Heo 					    int pool_id)
5804d707b9fSOleg Nesterov {
58123657bb1STejun Heo 	/*
58223657bb1STejun Heo 	 * The following wmb is paired with the implied mb in
58323657bb1STejun Heo 	 * test_and_set_bit(PENDING) and ensures all updates to @work made
58423657bb1STejun Heo 	 * here are visible to and precede any updates by the next PENDING
58523657bb1STejun Heo 	 * owner.
58623657bb1STejun Heo 	 */
58723657bb1STejun Heo 	smp_wmb();
5887c3eed5cSTejun Heo 	set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
5894d707b9fSOleg Nesterov }
5904d707b9fSOleg Nesterov 
5917a22ad75STejun Heo static void clear_work_data(struct work_struct *work)
592365970a1SDavid Howells {
5937c3eed5cSTejun Heo 	smp_wmb();	/* see set_work_pool_and_clear_pending() */
5947c3eed5cSTejun Heo 	set_work_data(work, WORK_STRUCT_NO_POOL, 0);
5957a22ad75STejun Heo }
5967a22ad75STejun Heo 
597112202d9STejun Heo static struct pool_workqueue *get_work_pwq(struct work_struct *work)
5987a22ad75STejun Heo {
599e120153dSTejun Heo 	unsigned long data = atomic_long_read(&work->data);
6007a22ad75STejun Heo 
601112202d9STejun Heo 	if (data & WORK_STRUCT_PWQ)
602e120153dSTejun Heo 		return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
603e120153dSTejun Heo 	else
604e120153dSTejun Heo 		return NULL;
6057a22ad75STejun Heo }
6067a22ad75STejun Heo 
6077c3eed5cSTejun Heo /**
6087c3eed5cSTejun Heo  * get_work_pool - return the worker_pool a given work was associated with
6097c3eed5cSTejun Heo  * @work: the work item of interest
6107c3eed5cSTejun Heo  *
6117c3eed5cSTejun Heo  * Return the worker_pool @work was last associated with.  %NULL if none.
612fa1b54e6STejun Heo  *
613fa1b54e6STejun Heo  * Pools are created and destroyed under workqueue_lock, and allows read
614fa1b54e6STejun Heo  * access under sched-RCU read lock.  As such, this function should be
615fa1b54e6STejun Heo  * called under workqueue_lock or with preemption disabled.
616fa1b54e6STejun Heo  *
617fa1b54e6STejun Heo  * All fields of the returned pool are accessible as long as the above
618fa1b54e6STejun Heo  * mentioned locking is in effect.  If the returned pool needs to be used
619fa1b54e6STejun Heo  * beyond the critical section, the caller is responsible for ensuring the
620fa1b54e6STejun Heo  * returned pool is and stays online.
6217c3eed5cSTejun Heo  */
6227c3eed5cSTejun Heo static struct worker_pool *get_work_pool(struct work_struct *work)
6237a22ad75STejun Heo {
624e120153dSTejun Heo 	unsigned long data = atomic_long_read(&work->data);
6257c3eed5cSTejun Heo 	int pool_id;
6267a22ad75STejun Heo 
627fa1b54e6STejun Heo 	assert_rcu_or_wq_lock();
628fa1b54e6STejun Heo 
629112202d9STejun Heo 	if (data & WORK_STRUCT_PWQ)
630112202d9STejun Heo 		return ((struct pool_workqueue *)
6317c3eed5cSTejun Heo 			(data & WORK_STRUCT_WQ_DATA_MASK))->pool;
6327a22ad75STejun Heo 
6337c3eed5cSTejun Heo 	pool_id = data >> WORK_OFFQ_POOL_SHIFT;
6347c3eed5cSTejun Heo 	if (pool_id == WORK_OFFQ_POOL_NONE)
6357a22ad75STejun Heo 		return NULL;
6367a22ad75STejun Heo 
637fa1b54e6STejun Heo 	return idr_find(&worker_pool_idr, pool_id);
6387c3eed5cSTejun Heo }
6397c3eed5cSTejun Heo 
6407c3eed5cSTejun Heo /**
6417c3eed5cSTejun Heo  * get_work_pool_id - return the worker pool ID a given work is associated with
6427c3eed5cSTejun Heo  * @work: the work item of interest
6437c3eed5cSTejun Heo  *
6447c3eed5cSTejun Heo  * Return the worker_pool ID @work was last associated with.
6457c3eed5cSTejun Heo  * %WORK_OFFQ_POOL_NONE if none.
6467c3eed5cSTejun Heo  */
6477c3eed5cSTejun Heo static int get_work_pool_id(struct work_struct *work)
6487c3eed5cSTejun Heo {
64954d5b7d0SLai Jiangshan 	unsigned long data = atomic_long_read(&work->data);
6507c3eed5cSTejun Heo 
651112202d9STejun Heo 	if (data & WORK_STRUCT_PWQ)
652112202d9STejun Heo 		return ((struct pool_workqueue *)
65354d5b7d0SLai Jiangshan 			(data & WORK_STRUCT_WQ_DATA_MASK))->pool->id;
65454d5b7d0SLai Jiangshan 
65554d5b7d0SLai Jiangshan 	return data >> WORK_OFFQ_POOL_SHIFT;
6567c3eed5cSTejun Heo }
6577c3eed5cSTejun Heo 
658bbb68dfaSTejun Heo static void mark_work_canceling(struct work_struct *work)
659bbb68dfaSTejun Heo {
6607c3eed5cSTejun Heo 	unsigned long pool_id = get_work_pool_id(work);
661bbb68dfaSTejun Heo 
6627c3eed5cSTejun Heo 	pool_id <<= WORK_OFFQ_POOL_SHIFT;
6637c3eed5cSTejun Heo 	set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
664bbb68dfaSTejun Heo }
665bbb68dfaSTejun Heo 
666bbb68dfaSTejun Heo static bool work_is_canceling(struct work_struct *work)
667bbb68dfaSTejun Heo {
668bbb68dfaSTejun Heo 	unsigned long data = atomic_long_read(&work->data);
669bbb68dfaSTejun Heo 
670112202d9STejun Heo 	return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING);
671bbb68dfaSTejun Heo }
672bbb68dfaSTejun Heo 
673e22bee78STejun Heo /*
6743270476aSTejun Heo  * Policy functions.  These define the policies on how the global worker
6753270476aSTejun Heo  * pools are managed.  Unless noted otherwise, these functions assume that
676d565ed63STejun Heo  * they're being called with pool->lock held.
677e22bee78STejun Heo  */
678e22bee78STejun Heo 
67963d95a91STejun Heo static bool __need_more_worker(struct worker_pool *pool)
680649027d7STejun Heo {
681e19e397aSTejun Heo 	return !atomic_read(&pool->nr_running);
682649027d7STejun Heo }
683649027d7STejun Heo 
684e22bee78STejun Heo /*
685e22bee78STejun Heo  * Need to wake up a worker?  Called from anything but currently
686e22bee78STejun Heo  * running workers.
687974271c4STejun Heo  *
688974271c4STejun Heo  * Note that, because unbound workers never contribute to nr_running, this
689706026c2STejun Heo  * function will always return %true for unbound pools as long as the
690974271c4STejun Heo  * worklist isn't empty.
691e22bee78STejun Heo  */
69263d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool)
693e22bee78STejun Heo {
69463d95a91STejun Heo 	return !list_empty(&pool->worklist) && __need_more_worker(pool);
695e22bee78STejun Heo }
696e22bee78STejun Heo 
697e22bee78STejun Heo /* Can I start working?  Called from busy but !running workers. */
69863d95a91STejun Heo static bool may_start_working(struct worker_pool *pool)
699e22bee78STejun Heo {
70063d95a91STejun Heo 	return pool->nr_idle;
701e22bee78STejun Heo }
702e22bee78STejun Heo 
703e22bee78STejun Heo /* Do I need to keep working?  Called from currently running workers. */
70463d95a91STejun Heo static bool keep_working(struct worker_pool *pool)
705e22bee78STejun Heo {
706e19e397aSTejun Heo 	return !list_empty(&pool->worklist) &&
707e19e397aSTejun Heo 		atomic_read(&pool->nr_running) <= 1;
708e22bee78STejun Heo }
709e22bee78STejun Heo 
710e22bee78STejun Heo /* Do we need a new worker?  Called from manager. */
71163d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool)
712e22bee78STejun Heo {
71363d95a91STejun Heo 	return need_more_worker(pool) && !may_start_working(pool);
714e22bee78STejun Heo }
715e22bee78STejun Heo 
716e22bee78STejun Heo /* Do I need to be the manager? */
71763d95a91STejun Heo static bool need_to_manage_workers(struct worker_pool *pool)
718e22bee78STejun Heo {
71963d95a91STejun Heo 	return need_to_create_worker(pool) ||
72011ebea50STejun Heo 		(pool->flags & POOL_MANAGE_WORKERS);
721e22bee78STejun Heo }
722e22bee78STejun Heo 
723e22bee78STejun Heo /* Do we have too many workers and should some go away? */
72463d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool)
725e22bee78STejun Heo {
72634a06bd6STejun Heo 	bool managing = mutex_is_locked(&pool->manager_arb);
72763d95a91STejun Heo 	int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
72863d95a91STejun Heo 	int nr_busy = pool->nr_workers - nr_idle;
729e22bee78STejun Heo 
730ea1abd61SLai Jiangshan 	/*
731ea1abd61SLai Jiangshan 	 * nr_idle and idle_list may disagree if idle rebinding is in
732ea1abd61SLai Jiangshan 	 * progress.  Never return %true if idle_list is empty.
733ea1abd61SLai Jiangshan 	 */
734ea1abd61SLai Jiangshan 	if (list_empty(&pool->idle_list))
735ea1abd61SLai Jiangshan 		return false;
736ea1abd61SLai Jiangshan 
737e22bee78STejun Heo 	return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
738e22bee78STejun Heo }
739e22bee78STejun Heo 
740e22bee78STejun Heo /*
741e22bee78STejun Heo  * Wake up functions.
742e22bee78STejun Heo  */
743e22bee78STejun Heo 
7447e11629dSTejun Heo /* Return the first worker.  Safe with preemption disabled */
74563d95a91STejun Heo static struct worker *first_worker(struct worker_pool *pool)
7467e11629dSTejun Heo {
74763d95a91STejun Heo 	if (unlikely(list_empty(&pool->idle_list)))
7487e11629dSTejun Heo 		return NULL;
7497e11629dSTejun Heo 
75063d95a91STejun Heo 	return list_first_entry(&pool->idle_list, struct worker, entry);
7517e11629dSTejun Heo }
7527e11629dSTejun Heo 
7537e11629dSTejun Heo /**
7547e11629dSTejun Heo  * wake_up_worker - wake up an idle worker
75563d95a91STejun Heo  * @pool: worker pool to wake worker from
7567e11629dSTejun Heo  *
75763d95a91STejun Heo  * Wake up the first idle worker of @pool.
7587e11629dSTejun Heo  *
7597e11629dSTejun Heo  * CONTEXT:
760d565ed63STejun Heo  * spin_lock_irq(pool->lock).
7617e11629dSTejun Heo  */
76263d95a91STejun Heo static void wake_up_worker(struct worker_pool *pool)
7637e11629dSTejun Heo {
76463d95a91STejun Heo 	struct worker *worker = first_worker(pool);
7657e11629dSTejun Heo 
7667e11629dSTejun Heo 	if (likely(worker))
7677e11629dSTejun Heo 		wake_up_process(worker->task);
7687e11629dSTejun Heo }
7697e11629dSTejun Heo 
7704690c4abSTejun Heo /**
771e22bee78STejun Heo  * wq_worker_waking_up - a worker is waking up
772e22bee78STejun Heo  * @task: task waking up
773e22bee78STejun Heo  * @cpu: CPU @task is waking up to
774e22bee78STejun Heo  *
775e22bee78STejun Heo  * This function is called during try_to_wake_up() when a worker is
776e22bee78STejun Heo  * being awoken.
777e22bee78STejun Heo  *
778e22bee78STejun Heo  * CONTEXT:
779e22bee78STejun Heo  * spin_lock_irq(rq->lock)
780e22bee78STejun Heo  */
781d84ff051STejun Heo void wq_worker_waking_up(struct task_struct *task, int cpu)
782e22bee78STejun Heo {
783e22bee78STejun Heo 	struct worker *worker = kthread_data(task);
784e22bee78STejun Heo 
78536576000SJoonsoo Kim 	if (!(worker->flags & WORKER_NOT_RUNNING)) {
786ec22ca5eSTejun Heo 		WARN_ON_ONCE(worker->pool->cpu != cpu);
787e19e397aSTejun Heo 		atomic_inc(&worker->pool->nr_running);
788e22bee78STejun Heo 	}
78936576000SJoonsoo Kim }
790e22bee78STejun Heo 
791e22bee78STejun Heo /**
792e22bee78STejun Heo  * wq_worker_sleeping - a worker is going to sleep
793e22bee78STejun Heo  * @task: task going to sleep
794e22bee78STejun Heo  * @cpu: CPU in question, must be the current CPU number
795e22bee78STejun Heo  *
796e22bee78STejun Heo  * This function is called during schedule() when a busy worker is
797e22bee78STejun Heo  * going to sleep.  Worker on the same cpu can be woken up by
798e22bee78STejun Heo  * returning pointer to its task.
799e22bee78STejun Heo  *
800e22bee78STejun Heo  * CONTEXT:
801e22bee78STejun Heo  * spin_lock_irq(rq->lock)
802e22bee78STejun Heo  *
803e22bee78STejun Heo  * RETURNS:
804e22bee78STejun Heo  * Worker task on @cpu to wake up, %NULL if none.
805e22bee78STejun Heo  */
806d84ff051STejun Heo struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
807e22bee78STejun Heo {
808e22bee78STejun Heo 	struct worker *worker = kthread_data(task), *to_wakeup = NULL;
809111c225aSTejun Heo 	struct worker_pool *pool;
810e22bee78STejun Heo 
811111c225aSTejun Heo 	/*
812111c225aSTejun Heo 	 * Rescuers, which may not have all the fields set up like normal
813111c225aSTejun Heo 	 * workers, also reach here, let's not access anything before
814111c225aSTejun Heo 	 * checking NOT_RUNNING.
815111c225aSTejun Heo 	 */
8162d64672eSSteven Rostedt 	if (worker->flags & WORKER_NOT_RUNNING)
817e22bee78STejun Heo 		return NULL;
818e22bee78STejun Heo 
819111c225aSTejun Heo 	pool = worker->pool;
820111c225aSTejun Heo 
821e22bee78STejun Heo 	/* this can only happen on the local cpu */
8226183c009STejun Heo 	if (WARN_ON_ONCE(cpu != raw_smp_processor_id()))
8236183c009STejun Heo 		return NULL;
824e22bee78STejun Heo 
825e22bee78STejun Heo 	/*
826e22bee78STejun Heo 	 * The counterpart of the following dec_and_test, implied mb,
827e22bee78STejun Heo 	 * worklist not empty test sequence is in insert_work().
828e22bee78STejun Heo 	 * Please read comment there.
829e22bee78STejun Heo 	 *
830628c78e7STejun Heo 	 * NOT_RUNNING is clear.  This means that we're bound to and
831628c78e7STejun Heo 	 * running on the local cpu w/ rq lock held and preemption
832628c78e7STejun Heo 	 * disabled, which in turn means that none else could be
833d565ed63STejun Heo 	 * manipulating idle_list, so dereferencing idle_list without pool
834628c78e7STejun Heo 	 * lock is safe.
835e22bee78STejun Heo 	 */
836e19e397aSTejun Heo 	if (atomic_dec_and_test(&pool->nr_running) &&
837e19e397aSTejun Heo 	    !list_empty(&pool->worklist))
83863d95a91STejun Heo 		to_wakeup = first_worker(pool);
839e22bee78STejun Heo 	return to_wakeup ? to_wakeup->task : NULL;
840e22bee78STejun Heo }
841e22bee78STejun Heo 
842e22bee78STejun Heo /**
843e22bee78STejun Heo  * worker_set_flags - set worker flags and adjust nr_running accordingly
844cb444766STejun Heo  * @worker: self
845d302f017STejun Heo  * @flags: flags to set
846d302f017STejun Heo  * @wakeup: wakeup an idle worker if necessary
847d302f017STejun Heo  *
848e22bee78STejun Heo  * Set @flags in @worker->flags and adjust nr_running accordingly.  If
849e22bee78STejun Heo  * nr_running becomes zero and @wakeup is %true, an idle worker is
850e22bee78STejun Heo  * woken up.
851d302f017STejun Heo  *
852cb444766STejun Heo  * CONTEXT:
853d565ed63STejun Heo  * spin_lock_irq(pool->lock)
854d302f017STejun Heo  */
855d302f017STejun Heo static inline void worker_set_flags(struct worker *worker, unsigned int flags,
856d302f017STejun Heo 				    bool wakeup)
857d302f017STejun Heo {
858bd7bdd43STejun Heo 	struct worker_pool *pool = worker->pool;
859e22bee78STejun Heo 
860cb444766STejun Heo 	WARN_ON_ONCE(worker->task != current);
861cb444766STejun Heo 
862e22bee78STejun Heo 	/*
863e22bee78STejun Heo 	 * If transitioning into NOT_RUNNING, adjust nr_running and
864e22bee78STejun Heo 	 * wake up an idle worker as necessary if requested by
865e22bee78STejun Heo 	 * @wakeup.
866e22bee78STejun Heo 	 */
867e22bee78STejun Heo 	if ((flags & WORKER_NOT_RUNNING) &&
868e22bee78STejun Heo 	    !(worker->flags & WORKER_NOT_RUNNING)) {
869e22bee78STejun Heo 		if (wakeup) {
870e19e397aSTejun Heo 			if (atomic_dec_and_test(&pool->nr_running) &&
871bd7bdd43STejun Heo 			    !list_empty(&pool->worklist))
87263d95a91STejun Heo 				wake_up_worker(pool);
873e22bee78STejun Heo 		} else
874e19e397aSTejun Heo 			atomic_dec(&pool->nr_running);
875e22bee78STejun Heo 	}
876e22bee78STejun Heo 
877d302f017STejun Heo 	worker->flags |= flags;
878d302f017STejun Heo }
879d302f017STejun Heo 
880d302f017STejun Heo /**
881e22bee78STejun Heo  * worker_clr_flags - clear worker flags and adjust nr_running accordingly
882cb444766STejun Heo  * @worker: self
883d302f017STejun Heo  * @flags: flags to clear
884d302f017STejun Heo  *
885e22bee78STejun Heo  * Clear @flags in @worker->flags and adjust nr_running accordingly.
886d302f017STejun Heo  *
887cb444766STejun Heo  * CONTEXT:
888d565ed63STejun Heo  * spin_lock_irq(pool->lock)
889d302f017STejun Heo  */
890d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
891d302f017STejun Heo {
89263d95a91STejun Heo 	struct worker_pool *pool = worker->pool;
893e22bee78STejun Heo 	unsigned int oflags = worker->flags;
894e22bee78STejun Heo 
895cb444766STejun Heo 	WARN_ON_ONCE(worker->task != current);
896cb444766STejun Heo 
897d302f017STejun Heo 	worker->flags &= ~flags;
898e22bee78STejun Heo 
89942c025f3STejun Heo 	/*
90042c025f3STejun Heo 	 * If transitioning out of NOT_RUNNING, increment nr_running.  Note
90142c025f3STejun Heo 	 * that the nested NOT_RUNNING is not a noop.  NOT_RUNNING is mask
90242c025f3STejun Heo 	 * of multiple flags, not a single flag.
90342c025f3STejun Heo 	 */
904e22bee78STejun Heo 	if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
905e22bee78STejun Heo 		if (!(worker->flags & WORKER_NOT_RUNNING))
906e19e397aSTejun Heo 			atomic_inc(&pool->nr_running);
907d302f017STejun Heo }
908d302f017STejun Heo 
909d302f017STejun Heo /**
9108cca0eeaSTejun Heo  * find_worker_executing_work - find worker which is executing a work
911c9e7cf27STejun Heo  * @pool: pool of interest
9128cca0eeaSTejun Heo  * @work: work to find worker for
9138cca0eeaSTejun Heo  *
914c9e7cf27STejun Heo  * Find a worker which is executing @work on @pool by searching
915c9e7cf27STejun Heo  * @pool->busy_hash which is keyed by the address of @work.  For a worker
916a2c1c57bSTejun Heo  * to match, its current execution should match the address of @work and
917a2c1c57bSTejun Heo  * its work function.  This is to avoid unwanted dependency between
918a2c1c57bSTejun Heo  * unrelated work executions through a work item being recycled while still
919a2c1c57bSTejun Heo  * being executed.
920a2c1c57bSTejun Heo  *
921a2c1c57bSTejun Heo  * This is a bit tricky.  A work item may be freed once its execution
922a2c1c57bSTejun Heo  * starts and nothing prevents the freed area from being recycled for
923a2c1c57bSTejun Heo  * another work item.  If the same work item address ends up being reused
924a2c1c57bSTejun Heo  * before the original execution finishes, workqueue will identify the
925a2c1c57bSTejun Heo  * recycled work item as currently executing and make it wait until the
926a2c1c57bSTejun Heo  * current execution finishes, introducing an unwanted dependency.
927a2c1c57bSTejun Heo  *
928a2c1c57bSTejun Heo  * This function checks the work item address, work function and workqueue
929a2c1c57bSTejun Heo  * to avoid false positives.  Note that this isn't complete as one may
930a2c1c57bSTejun Heo  * construct a work function which can introduce dependency onto itself
931a2c1c57bSTejun Heo  * through a recycled work item.  Well, if somebody wants to shoot oneself
932a2c1c57bSTejun Heo  * in the foot that badly, there's only so much we can do, and if such
933a2c1c57bSTejun Heo  * deadlock actually occurs, it should be easy to locate the culprit work
934a2c1c57bSTejun Heo  * function.
9358cca0eeaSTejun Heo  *
9368cca0eeaSTejun Heo  * CONTEXT:
937d565ed63STejun Heo  * spin_lock_irq(pool->lock).
9388cca0eeaSTejun Heo  *
9398cca0eeaSTejun Heo  * RETURNS:
9408cca0eeaSTejun Heo  * Pointer to worker which is executing @work if found, NULL
9418cca0eeaSTejun Heo  * otherwise.
9428cca0eeaSTejun Heo  */
943c9e7cf27STejun Heo static struct worker *find_worker_executing_work(struct worker_pool *pool,
9448cca0eeaSTejun Heo 						 struct work_struct *work)
9458cca0eeaSTejun Heo {
94642f8570fSSasha Levin 	struct worker *worker;
94742f8570fSSasha Levin 
948b67bfe0dSSasha Levin 	hash_for_each_possible(pool->busy_hash, worker, hentry,
949a2c1c57bSTejun Heo 			       (unsigned long)work)
950a2c1c57bSTejun Heo 		if (worker->current_work == work &&
951a2c1c57bSTejun Heo 		    worker->current_func == work->func)
95242f8570fSSasha Levin 			return worker;
95342f8570fSSasha Levin 
95442f8570fSSasha Levin 	return NULL;
9558cca0eeaSTejun Heo }
9568cca0eeaSTejun Heo 
9578cca0eeaSTejun Heo /**
958bf4ede01STejun Heo  * move_linked_works - move linked works to a list
959bf4ede01STejun Heo  * @work: start of series of works to be scheduled
960bf4ede01STejun Heo  * @head: target list to append @work to
961bf4ede01STejun Heo  * @nextp: out paramter for nested worklist walking
962bf4ede01STejun Heo  *
963bf4ede01STejun Heo  * Schedule linked works starting from @work to @head.  Work series to
964bf4ede01STejun Heo  * be scheduled starts at @work and includes any consecutive work with
965bf4ede01STejun Heo  * WORK_STRUCT_LINKED set in its predecessor.
966bf4ede01STejun Heo  *
967bf4ede01STejun Heo  * If @nextp is not NULL, it's updated to point to the next work of
968bf4ede01STejun Heo  * the last scheduled work.  This allows move_linked_works() to be
969bf4ede01STejun Heo  * nested inside outer list_for_each_entry_safe().
970bf4ede01STejun Heo  *
971bf4ede01STejun Heo  * CONTEXT:
972d565ed63STejun Heo  * spin_lock_irq(pool->lock).
973bf4ede01STejun Heo  */
974bf4ede01STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head,
975bf4ede01STejun Heo 			      struct work_struct **nextp)
976bf4ede01STejun Heo {
977bf4ede01STejun Heo 	struct work_struct *n;
978bf4ede01STejun Heo 
979bf4ede01STejun Heo 	/*
980bf4ede01STejun Heo 	 * Linked worklist will always end before the end of the list,
981bf4ede01STejun Heo 	 * use NULL for list head.
982bf4ede01STejun Heo 	 */
983bf4ede01STejun Heo 	list_for_each_entry_safe_from(work, n, NULL, entry) {
984bf4ede01STejun Heo 		list_move_tail(&work->entry, head);
985bf4ede01STejun Heo 		if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
986bf4ede01STejun Heo 			break;
987bf4ede01STejun Heo 	}
988bf4ede01STejun Heo 
989bf4ede01STejun Heo 	/*
990bf4ede01STejun Heo 	 * If we're already inside safe list traversal and have moved
991bf4ede01STejun Heo 	 * multiple works to the scheduled queue, the next position
992bf4ede01STejun Heo 	 * needs to be updated.
993bf4ede01STejun Heo 	 */
994bf4ede01STejun Heo 	if (nextp)
995bf4ede01STejun Heo 		*nextp = n;
996bf4ede01STejun Heo }
997bf4ede01STejun Heo 
998112202d9STejun Heo static void pwq_activate_delayed_work(struct work_struct *work)
999bf4ede01STejun Heo {
1000112202d9STejun Heo 	struct pool_workqueue *pwq = get_work_pwq(work);
1001bf4ede01STejun Heo 
1002bf4ede01STejun Heo 	trace_workqueue_activate_work(work);
1003112202d9STejun Heo 	move_linked_works(work, &pwq->pool->worklist, NULL);
1004bf4ede01STejun Heo 	__clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
1005112202d9STejun Heo 	pwq->nr_active++;
1006bf4ede01STejun Heo }
1007bf4ede01STejun Heo 
1008112202d9STejun Heo static void pwq_activate_first_delayed(struct pool_workqueue *pwq)
10093aa62497SLai Jiangshan {
1010112202d9STejun Heo 	struct work_struct *work = list_first_entry(&pwq->delayed_works,
10113aa62497SLai Jiangshan 						    struct work_struct, entry);
10123aa62497SLai Jiangshan 
1013112202d9STejun Heo 	pwq_activate_delayed_work(work);
10143aa62497SLai Jiangshan }
10153aa62497SLai Jiangshan 
1016bf4ede01STejun Heo /**
1017112202d9STejun Heo  * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight
1018112202d9STejun Heo  * @pwq: pwq of interest
1019bf4ede01STejun Heo  * @color: color of work which left the queue
1020bf4ede01STejun Heo  *
1021bf4ede01STejun Heo  * A work either has completed or is removed from pending queue,
1022112202d9STejun Heo  * decrement nr_in_flight of its pwq and handle workqueue flushing.
1023bf4ede01STejun Heo  *
1024bf4ede01STejun Heo  * CONTEXT:
1025d565ed63STejun Heo  * spin_lock_irq(pool->lock).
1026bf4ede01STejun Heo  */
1027112202d9STejun Heo static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color)
1028bf4ede01STejun Heo {
1029bf4ede01STejun Heo 	/* ignore uncolored works */
1030bf4ede01STejun Heo 	if (color == WORK_NO_COLOR)
1031bf4ede01STejun Heo 		return;
1032bf4ede01STejun Heo 
1033112202d9STejun Heo 	pwq->nr_in_flight[color]--;
1034bf4ede01STejun Heo 
1035112202d9STejun Heo 	pwq->nr_active--;
1036112202d9STejun Heo 	if (!list_empty(&pwq->delayed_works)) {
1037bf4ede01STejun Heo 		/* one down, submit a delayed one */
1038112202d9STejun Heo 		if (pwq->nr_active < pwq->max_active)
1039112202d9STejun Heo 			pwq_activate_first_delayed(pwq);
1040bf4ede01STejun Heo 	}
1041bf4ede01STejun Heo 
1042bf4ede01STejun Heo 	/* is flush in progress and are we at the flushing tip? */
1043112202d9STejun Heo 	if (likely(pwq->flush_color != color))
1044bf4ede01STejun Heo 		return;
1045bf4ede01STejun Heo 
1046bf4ede01STejun Heo 	/* are there still in-flight works? */
1047112202d9STejun Heo 	if (pwq->nr_in_flight[color])
1048bf4ede01STejun Heo 		return;
1049bf4ede01STejun Heo 
1050112202d9STejun Heo 	/* this pwq is done, clear flush_color */
1051112202d9STejun Heo 	pwq->flush_color = -1;
1052bf4ede01STejun Heo 
1053bf4ede01STejun Heo 	/*
1054112202d9STejun Heo 	 * If this was the last pwq, wake up the first flusher.  It
1055bf4ede01STejun Heo 	 * will handle the rest.
1056bf4ede01STejun Heo 	 */
1057112202d9STejun Heo 	if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))
1058112202d9STejun Heo 		complete(&pwq->wq->first_flusher->done);
1059bf4ede01STejun Heo }
1060bf4ede01STejun Heo 
106136e227d2STejun Heo /**
1062bbb68dfaSTejun Heo  * try_to_grab_pending - steal work item from worklist and disable irq
106336e227d2STejun Heo  * @work: work item to steal
106436e227d2STejun Heo  * @is_dwork: @work is a delayed_work
1065bbb68dfaSTejun Heo  * @flags: place to store irq state
106636e227d2STejun Heo  *
106736e227d2STejun Heo  * Try to grab PENDING bit of @work.  This function can handle @work in any
106836e227d2STejun Heo  * stable state - idle, on timer or on worklist.  Return values are
106936e227d2STejun Heo  *
107036e227d2STejun Heo  *  1		if @work was pending and we successfully stole PENDING
107136e227d2STejun Heo  *  0		if @work was idle and we claimed PENDING
107236e227d2STejun Heo  *  -EAGAIN	if PENDING couldn't be grabbed at the moment, safe to busy-retry
1073bbb68dfaSTejun Heo  *  -ENOENT	if someone else is canceling @work, this state may persist
1074bbb68dfaSTejun Heo  *		for arbitrarily long
107536e227d2STejun Heo  *
1076bbb68dfaSTejun Heo  * On >= 0 return, the caller owns @work's PENDING bit.  To avoid getting
1077e0aecdd8STejun Heo  * interrupted while holding PENDING and @work off queue, irq must be
1078e0aecdd8STejun Heo  * disabled on entry.  This, combined with delayed_work->timer being
1079e0aecdd8STejun Heo  * irqsafe, ensures that we return -EAGAIN for finite short period of time.
1080bbb68dfaSTejun Heo  *
1081bbb68dfaSTejun Heo  * On successful return, >= 0, irq is disabled and the caller is
1082bbb68dfaSTejun Heo  * responsible for releasing it using local_irq_restore(*@flags).
1083bbb68dfaSTejun Heo  *
1084e0aecdd8STejun Heo  * This function is safe to call from any context including IRQ handler.
1085bf4ede01STejun Heo  */
1086bbb68dfaSTejun Heo static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1087bbb68dfaSTejun Heo 			       unsigned long *flags)
1088bf4ede01STejun Heo {
1089d565ed63STejun Heo 	struct worker_pool *pool;
1090112202d9STejun Heo 	struct pool_workqueue *pwq;
1091bf4ede01STejun Heo 
1092bbb68dfaSTejun Heo 	local_irq_save(*flags);
1093bbb68dfaSTejun Heo 
109436e227d2STejun Heo 	/* try to steal the timer if it exists */
109536e227d2STejun Heo 	if (is_dwork) {
109636e227d2STejun Heo 		struct delayed_work *dwork = to_delayed_work(work);
109736e227d2STejun Heo 
1098e0aecdd8STejun Heo 		/*
1099e0aecdd8STejun Heo 		 * dwork->timer is irqsafe.  If del_timer() fails, it's
1100e0aecdd8STejun Heo 		 * guaranteed that the timer is not queued anywhere and not
1101e0aecdd8STejun Heo 		 * running on the local CPU.
1102e0aecdd8STejun Heo 		 */
110336e227d2STejun Heo 		if (likely(del_timer(&dwork->timer)))
110436e227d2STejun Heo 			return 1;
110536e227d2STejun Heo 	}
110636e227d2STejun Heo 
110736e227d2STejun Heo 	/* try to claim PENDING the normal way */
1108bf4ede01STejun Heo 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1109bf4ede01STejun Heo 		return 0;
1110bf4ede01STejun Heo 
1111bf4ede01STejun Heo 	/*
1112bf4ede01STejun Heo 	 * The queueing is in progress, or it is already queued. Try to
1113bf4ede01STejun Heo 	 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1114bf4ede01STejun Heo 	 */
1115d565ed63STejun Heo 	pool = get_work_pool(work);
1116d565ed63STejun Heo 	if (!pool)
1117bbb68dfaSTejun Heo 		goto fail;
1118bf4ede01STejun Heo 
1119d565ed63STejun Heo 	spin_lock(&pool->lock);
1120bf4ede01STejun Heo 	/*
1121112202d9STejun Heo 	 * work->data is guaranteed to point to pwq only while the work
1122112202d9STejun Heo 	 * item is queued on pwq->wq, and both updating work->data to point
1123112202d9STejun Heo 	 * to pwq on queueing and to pool on dequeueing are done under
1124112202d9STejun Heo 	 * pwq->pool->lock.  This in turn guarantees that, if work->data
1125112202d9STejun Heo 	 * points to pwq which is associated with a locked pool, the work
11260b3dae68SLai Jiangshan 	 * item is currently queued on that pool.
1127bf4ede01STejun Heo 	 */
1128112202d9STejun Heo 	pwq = get_work_pwq(work);
1129112202d9STejun Heo 	if (pwq && pwq->pool == pool) {
1130bf4ede01STejun Heo 		debug_work_deactivate(work);
11313aa62497SLai Jiangshan 
11323aa62497SLai Jiangshan 		/*
113316062836STejun Heo 		 * A delayed work item cannot be grabbed directly because
113416062836STejun Heo 		 * it might have linked NO_COLOR work items which, if left
1135112202d9STejun Heo 		 * on the delayed_list, will confuse pwq->nr_active
113616062836STejun Heo 		 * management later on and cause stall.  Make sure the work
113716062836STejun Heo 		 * item is activated before grabbing.
11383aa62497SLai Jiangshan 		 */
11393aa62497SLai Jiangshan 		if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
1140112202d9STejun Heo 			pwq_activate_delayed_work(work);
11413aa62497SLai Jiangshan 
1142bf4ede01STejun Heo 		list_del_init(&work->entry);
1143112202d9STejun Heo 		pwq_dec_nr_in_flight(get_work_pwq(work), get_work_color(work));
114436e227d2STejun Heo 
1145112202d9STejun Heo 		/* work->data points to pwq iff queued, point to pool */
11464468a00fSLai Jiangshan 		set_work_pool_and_keep_pending(work, pool->id);
11474468a00fSLai Jiangshan 
1148d565ed63STejun Heo 		spin_unlock(&pool->lock);
114936e227d2STejun Heo 		return 1;
1150bf4ede01STejun Heo 	}
1151d565ed63STejun Heo 	spin_unlock(&pool->lock);
1152bbb68dfaSTejun Heo fail:
1153bbb68dfaSTejun Heo 	local_irq_restore(*flags);
1154bbb68dfaSTejun Heo 	if (work_is_canceling(work))
1155bbb68dfaSTejun Heo 		return -ENOENT;
1156bbb68dfaSTejun Heo 	cpu_relax();
115736e227d2STejun Heo 	return -EAGAIN;
1158bf4ede01STejun Heo }
1159bf4ede01STejun Heo 
1160bf4ede01STejun Heo /**
1161706026c2STejun Heo  * insert_work - insert a work into a pool
1162112202d9STejun Heo  * @pwq: pwq @work belongs to
11634690c4abSTejun Heo  * @work: work to insert
11644690c4abSTejun Heo  * @head: insertion point
11654690c4abSTejun Heo  * @extra_flags: extra WORK_STRUCT_* flags to set
11664690c4abSTejun Heo  *
1167112202d9STejun Heo  * Insert @work which belongs to @pwq after @head.  @extra_flags is or'd to
1168706026c2STejun Heo  * work_struct flags.
11694690c4abSTejun Heo  *
11704690c4abSTejun Heo  * CONTEXT:
1171d565ed63STejun Heo  * spin_lock_irq(pool->lock).
1172365970a1SDavid Howells  */
1173112202d9STejun Heo static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
1174112202d9STejun Heo 			struct list_head *head, unsigned int extra_flags)
1175b89deed3SOleg Nesterov {
1176112202d9STejun Heo 	struct worker_pool *pool = pwq->pool;
1177e1d8aa9fSFrederic Weisbecker 
11784690c4abSTejun Heo 	/* we own @work, set data and link */
1179112202d9STejun Heo 	set_work_pwq(work, pwq, extra_flags);
11801a4d9b0aSOleg Nesterov 	list_add_tail(&work->entry, head);
1181e22bee78STejun Heo 
1182e22bee78STejun Heo 	/*
1183e22bee78STejun Heo 	 * Ensure either worker_sched_deactivated() sees the above
1184e22bee78STejun Heo 	 * list_add_tail() or we see zero nr_running to avoid workers
1185e22bee78STejun Heo 	 * lying around lazily while there are works to be processed.
1186e22bee78STejun Heo 	 */
1187e22bee78STejun Heo 	smp_mb();
1188e22bee78STejun Heo 
118963d95a91STejun Heo 	if (__need_more_worker(pool))
119063d95a91STejun Heo 		wake_up_worker(pool);
1191b89deed3SOleg Nesterov }
1192b89deed3SOleg Nesterov 
1193c8efcc25STejun Heo /*
1194c8efcc25STejun Heo  * Test whether @work is being queued from another work executing on the
11958d03ecfeSTejun Heo  * same workqueue.
1196c8efcc25STejun Heo  */
1197c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq)
1198c8efcc25STejun Heo {
1199c8efcc25STejun Heo 	struct worker *worker;
1200c8efcc25STejun Heo 
12018d03ecfeSTejun Heo 	worker = current_wq_worker();
1202c8efcc25STejun Heo 	/*
12038d03ecfeSTejun Heo 	 * Return %true iff I'm a worker execuing a work item on @wq.  If
12048d03ecfeSTejun Heo 	 * I'm @worker, it's safe to dereference it without locking.
1205c8efcc25STejun Heo 	 */
1206112202d9STejun Heo 	return worker && worker->current_pwq->wq == wq;
1207c8efcc25STejun Heo }
1208c8efcc25STejun Heo 
1209d84ff051STejun Heo static void __queue_work(int cpu, struct workqueue_struct *wq,
12101da177e4SLinus Torvalds 			 struct work_struct *work)
12111da177e4SLinus Torvalds {
1212112202d9STejun Heo 	struct pool_workqueue *pwq;
12131e19ffc6STejun Heo 	struct list_head *worklist;
12148a2e8e5dSTejun Heo 	unsigned int work_flags;
1215b75cac93SJoonsoo Kim 	unsigned int req_cpu = cpu;
12168930cabaSTejun Heo 
12178930cabaSTejun Heo 	/*
12188930cabaSTejun Heo 	 * While a work item is PENDING && off queue, a task trying to
12198930cabaSTejun Heo 	 * steal the PENDING will busy-loop waiting for it to either get
12208930cabaSTejun Heo 	 * queued or lose PENDING.  Grabbing PENDING and queueing should
12218930cabaSTejun Heo 	 * happen with IRQ disabled.
12228930cabaSTejun Heo 	 */
12238930cabaSTejun Heo 	WARN_ON_ONCE(!irqs_disabled());
12241da177e4SLinus Torvalds 
1225dc186ad7SThomas Gleixner 	debug_work_activate(work);
12261e19ffc6STejun Heo 
1227c8efcc25STejun Heo 	/* if dying, only works from the same workqueue are allowed */
12289c5a2ba7STejun Heo 	if (unlikely(wq->flags & WQ_DRAINING) &&
1229c8efcc25STejun Heo 	    WARN_ON_ONCE(!is_chained_work(wq)))
1230e41e704bSTejun Heo 		return;
1231e41e704bSTejun Heo 
1232112202d9STejun Heo 	/* determine the pwq to use */
1233c7fc77f7STejun Heo 	if (!(wq->flags & WQ_UNBOUND)) {
1234c9e7cf27STejun Heo 		struct worker_pool *last_pool;
1235c7fc77f7STejun Heo 
123657469821STejun Heo 		if (cpu == WORK_CPU_UNBOUND)
1237f3421797STejun Heo 			cpu = raw_smp_processor_id();
1238f3421797STejun Heo 
123918aa9effSTejun Heo 		/*
1240dbf2576eSTejun Heo 		 * It's multi cpu.  If @work was previously on a different
1241dbf2576eSTejun Heo 		 * cpu, it might still be running there, in which case the
1242dbf2576eSTejun Heo 		 * work needs to be queued on that cpu to guarantee
1243dbf2576eSTejun Heo 		 * non-reentrancy.
124418aa9effSTejun Heo 		 */
12457fb98ea7STejun Heo 		pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
1246c9e7cf27STejun Heo 		last_pool = get_work_pool(work);
1247dbf2576eSTejun Heo 
1248112202d9STejun Heo 		if (last_pool && last_pool != pwq->pool) {
124918aa9effSTejun Heo 			struct worker *worker;
125018aa9effSTejun Heo 
1251d565ed63STejun Heo 			spin_lock(&last_pool->lock);
125218aa9effSTejun Heo 
1253c9e7cf27STejun Heo 			worker = find_worker_executing_work(last_pool, work);
125418aa9effSTejun Heo 
1255112202d9STejun Heo 			if (worker && worker->current_pwq->wq == wq) {
12567fb98ea7STejun Heo 				pwq = per_cpu_ptr(wq->cpu_pwqs, last_pool->cpu);
12578594fadeSLai Jiangshan 			} else {
125818aa9effSTejun Heo 				/* meh... not running there, queue here */
1259d565ed63STejun Heo 				spin_unlock(&last_pool->lock);
1260112202d9STejun Heo 				spin_lock(&pwq->pool->lock);
126118aa9effSTejun Heo 			}
12628930cabaSTejun Heo 		} else {
1263112202d9STejun Heo 			spin_lock(&pwq->pool->lock);
12648930cabaSTejun Heo 		}
1265f3421797STejun Heo 	} else {
12667fb98ea7STejun Heo 		pwq = first_pwq(wq);
1267112202d9STejun Heo 		spin_lock(&pwq->pool->lock);
1268502ca9d8STejun Heo 	}
1269502ca9d8STejun Heo 
1270112202d9STejun Heo 	/* pwq determined, queue */
1271112202d9STejun Heo 	trace_workqueue_queue_work(req_cpu, pwq, work);
1272502ca9d8STejun Heo 
1273f5b2552bSDan Carpenter 	if (WARN_ON(!list_empty(&work->entry))) {
1274112202d9STejun Heo 		spin_unlock(&pwq->pool->lock);
1275f5b2552bSDan Carpenter 		return;
1276f5b2552bSDan Carpenter 	}
12771e19ffc6STejun Heo 
1278112202d9STejun Heo 	pwq->nr_in_flight[pwq->work_color]++;
1279112202d9STejun Heo 	work_flags = work_color_to_flags(pwq->work_color);
12801e19ffc6STejun Heo 
1281112202d9STejun Heo 	if (likely(pwq->nr_active < pwq->max_active)) {
1282cdadf009STejun Heo 		trace_workqueue_activate_work(work);
1283112202d9STejun Heo 		pwq->nr_active++;
1284112202d9STejun Heo 		worklist = &pwq->pool->worklist;
12858a2e8e5dSTejun Heo 	} else {
12868a2e8e5dSTejun Heo 		work_flags |= WORK_STRUCT_DELAYED;
1287112202d9STejun Heo 		worklist = &pwq->delayed_works;
12888a2e8e5dSTejun Heo 	}
12891e19ffc6STejun Heo 
1290112202d9STejun Heo 	insert_work(pwq, work, worklist, work_flags);
12911e19ffc6STejun Heo 
1292112202d9STejun Heo 	spin_unlock(&pwq->pool->lock);
12931da177e4SLinus Torvalds }
12941da177e4SLinus Torvalds 
12950fcb78c2SRolf Eike Beer /**
1296c1a220e7SZhang Rui  * queue_work_on - queue work on specific cpu
1297c1a220e7SZhang Rui  * @cpu: CPU number to execute work on
1298c1a220e7SZhang Rui  * @wq: workqueue to use
1299c1a220e7SZhang Rui  * @work: work to queue
1300c1a220e7SZhang Rui  *
1301d4283e93STejun Heo  * Returns %false if @work was already on a queue, %true otherwise.
1302c1a220e7SZhang Rui  *
1303c1a220e7SZhang Rui  * We queue the work to a specific CPU, the caller must ensure it
1304c1a220e7SZhang Rui  * can't go away.
1305c1a220e7SZhang Rui  */
1306d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq,
1307d4283e93STejun Heo 		   struct work_struct *work)
1308c1a220e7SZhang Rui {
1309d4283e93STejun Heo 	bool ret = false;
13108930cabaSTejun Heo 	unsigned long flags;
13118930cabaSTejun Heo 
13128930cabaSTejun Heo 	local_irq_save(flags);
1313c1a220e7SZhang Rui 
131422df02bbSTejun Heo 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
13154690c4abSTejun Heo 		__queue_work(cpu, wq, work);
1316d4283e93STejun Heo 		ret = true;
1317c1a220e7SZhang Rui 	}
13188930cabaSTejun Heo 
13198930cabaSTejun Heo 	local_irq_restore(flags);
1320c1a220e7SZhang Rui 	return ret;
1321c1a220e7SZhang Rui }
1322c1a220e7SZhang Rui EXPORT_SYMBOL_GPL(queue_work_on);
1323c1a220e7SZhang Rui 
13240a13c00eSTejun Heo /**
13250a13c00eSTejun Heo  * queue_work - queue work on a workqueue
13260a13c00eSTejun Heo  * @wq: workqueue to use
13270a13c00eSTejun Heo  * @work: work to queue
13280a13c00eSTejun Heo  *
1329d4283e93STejun Heo  * Returns %false if @work was already on a queue, %true otherwise.
13300a13c00eSTejun Heo  *
13310a13c00eSTejun Heo  * We queue the work to the CPU on which it was submitted, but if the CPU dies
13320a13c00eSTejun Heo  * it can be processed by another CPU.
13330a13c00eSTejun Heo  */
1334d4283e93STejun Heo bool queue_work(struct workqueue_struct *wq, struct work_struct *work)
13350a13c00eSTejun Heo {
133657469821STejun Heo 	return queue_work_on(WORK_CPU_UNBOUND, wq, work);
13370a13c00eSTejun Heo }
13380a13c00eSTejun Heo EXPORT_SYMBOL_GPL(queue_work);
13390a13c00eSTejun Heo 
1340d8e794dfSTejun Heo void delayed_work_timer_fn(unsigned long __data)
13411da177e4SLinus Torvalds {
134252bad64dSDavid Howells 	struct delayed_work *dwork = (struct delayed_work *)__data;
13431da177e4SLinus Torvalds 
1344e0aecdd8STejun Heo 	/* should have been called from irqsafe timer with irq already off */
134560c057bcSLai Jiangshan 	__queue_work(dwork->cpu, dwork->wq, &dwork->work);
13461da177e4SLinus Torvalds }
13471438ade5SKonstantin Khlebnikov EXPORT_SYMBOL(delayed_work_timer_fn);
13481da177e4SLinus Torvalds 
13497beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
135052bad64dSDavid Howells 				struct delayed_work *dwork, unsigned long delay)
13511da177e4SLinus Torvalds {
13527beb2edfSTejun Heo 	struct timer_list *timer = &dwork->timer;
13537beb2edfSTejun Heo 	struct work_struct *work = &dwork->work;
13541da177e4SLinus Torvalds 
13557beb2edfSTejun Heo 	WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
13567beb2edfSTejun Heo 		     timer->data != (unsigned long)dwork);
1357fc4b514fSTejun Heo 	WARN_ON_ONCE(timer_pending(timer));
1358fc4b514fSTejun Heo 	WARN_ON_ONCE(!list_empty(&work->entry));
13597beb2edfSTejun Heo 
13608852aac2STejun Heo 	/*
13618852aac2STejun Heo 	 * If @delay is 0, queue @dwork->work immediately.  This is for
13628852aac2STejun Heo 	 * both optimization and correctness.  The earliest @timer can
13638852aac2STejun Heo 	 * expire is on the closest next tick and delayed_work users depend
13648852aac2STejun Heo 	 * on that there's no such delay when @delay is 0.
13658852aac2STejun Heo 	 */
13668852aac2STejun Heo 	if (!delay) {
13678852aac2STejun Heo 		__queue_work(cpu, wq, &dwork->work);
13688852aac2STejun Heo 		return;
13698852aac2STejun Heo 	}
13708852aac2STejun Heo 
13717beb2edfSTejun Heo 	timer_stats_timer_set_start_info(&dwork->timer);
13727beb2edfSTejun Heo 
137360c057bcSLai Jiangshan 	dwork->wq = wq;
13741265057fSTejun Heo 	dwork->cpu = cpu;
13757beb2edfSTejun Heo 	timer->expires = jiffies + delay;
13767beb2edfSTejun Heo 
13777beb2edfSTejun Heo 	if (unlikely(cpu != WORK_CPU_UNBOUND))
13787beb2edfSTejun Heo 		add_timer_on(timer, cpu);
13797beb2edfSTejun Heo 	else
13807beb2edfSTejun Heo 		add_timer(timer);
13817beb2edfSTejun Heo }
13821da177e4SLinus Torvalds 
13830fcb78c2SRolf Eike Beer /**
13840fcb78c2SRolf Eike Beer  * queue_delayed_work_on - queue work on specific CPU after delay
13850fcb78c2SRolf Eike Beer  * @cpu: CPU number to execute work on
13860fcb78c2SRolf Eike Beer  * @wq: workqueue to use
1387af9997e4SRandy Dunlap  * @dwork: work to queue
13880fcb78c2SRolf Eike Beer  * @delay: number of jiffies to wait before queueing
13890fcb78c2SRolf Eike Beer  *
1390715f1300STejun Heo  * Returns %false if @work was already on a queue, %true otherwise.  If
1391715f1300STejun Heo  * @delay is zero and @dwork is idle, it will be scheduled for immediate
1392715f1300STejun Heo  * execution.
13930fcb78c2SRolf Eike Beer  */
1394d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
139552bad64dSDavid Howells 			   struct delayed_work *dwork, unsigned long delay)
13967a6bc1cdSVenkatesh Pallipadi {
139752bad64dSDavid Howells 	struct work_struct *work = &dwork->work;
1398d4283e93STejun Heo 	bool ret = false;
13998930cabaSTejun Heo 	unsigned long flags;
14008930cabaSTejun Heo 
14018930cabaSTejun Heo 	/* read the comment in __queue_work() */
14028930cabaSTejun Heo 	local_irq_save(flags);
14037a6bc1cdSVenkatesh Pallipadi 
140422df02bbSTejun Heo 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
14057beb2edfSTejun Heo 		__queue_delayed_work(cpu, wq, dwork, delay);
1406d4283e93STejun Heo 		ret = true;
14077a6bc1cdSVenkatesh Pallipadi 	}
14088930cabaSTejun Heo 
14098930cabaSTejun Heo 	local_irq_restore(flags);
14107a6bc1cdSVenkatesh Pallipadi 	return ret;
14117a6bc1cdSVenkatesh Pallipadi }
1412ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work_on);
14131da177e4SLinus Torvalds 
1414c8e55f36STejun Heo /**
14150a13c00eSTejun Heo  * queue_delayed_work - queue work on a workqueue after delay
14160a13c00eSTejun Heo  * @wq: workqueue to use
14170a13c00eSTejun Heo  * @dwork: delayable work to queue
14180a13c00eSTejun Heo  * @delay: number of jiffies to wait before queueing
14190a13c00eSTejun Heo  *
1420715f1300STejun Heo  * Equivalent to queue_delayed_work_on() but tries to use the local CPU.
14210a13c00eSTejun Heo  */
1422d4283e93STejun Heo bool queue_delayed_work(struct workqueue_struct *wq,
14230a13c00eSTejun Heo 			struct delayed_work *dwork, unsigned long delay)
14240a13c00eSTejun Heo {
142557469821STejun Heo 	return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
14260a13c00eSTejun Heo }
14270a13c00eSTejun Heo EXPORT_SYMBOL_GPL(queue_delayed_work);
14280a13c00eSTejun Heo 
14290a13c00eSTejun Heo /**
14308376fe22STejun Heo  * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
14318376fe22STejun Heo  * @cpu: CPU number to execute work on
14328376fe22STejun Heo  * @wq: workqueue to use
14338376fe22STejun Heo  * @dwork: work to queue
14348376fe22STejun Heo  * @delay: number of jiffies to wait before queueing
14358376fe22STejun Heo  *
14368376fe22STejun Heo  * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
14378376fe22STejun Heo  * modify @dwork's timer so that it expires after @delay.  If @delay is
14388376fe22STejun Heo  * zero, @work is guaranteed to be scheduled immediately regardless of its
14398376fe22STejun Heo  * current state.
14408376fe22STejun Heo  *
14418376fe22STejun Heo  * Returns %false if @dwork was idle and queued, %true if @dwork was
14428376fe22STejun Heo  * pending and its timer was modified.
14438376fe22STejun Heo  *
1444e0aecdd8STejun Heo  * This function is safe to call from any context including IRQ handler.
14458376fe22STejun Heo  * See try_to_grab_pending() for details.
14468376fe22STejun Heo  */
14478376fe22STejun Heo bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
14488376fe22STejun Heo 			 struct delayed_work *dwork, unsigned long delay)
14498376fe22STejun Heo {
14508376fe22STejun Heo 	unsigned long flags;
14518376fe22STejun Heo 	int ret;
14528376fe22STejun Heo 
14538376fe22STejun Heo 	do {
14548376fe22STejun Heo 		ret = try_to_grab_pending(&dwork->work, true, &flags);
14558376fe22STejun Heo 	} while (unlikely(ret == -EAGAIN));
14568376fe22STejun Heo 
14578376fe22STejun Heo 	if (likely(ret >= 0)) {
14588376fe22STejun Heo 		__queue_delayed_work(cpu, wq, dwork, delay);
14598376fe22STejun Heo 		local_irq_restore(flags);
14608376fe22STejun Heo 	}
14618376fe22STejun Heo 
14628376fe22STejun Heo 	/* -ENOENT from try_to_grab_pending() becomes %true */
14638376fe22STejun Heo 	return ret;
14648376fe22STejun Heo }
14658376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work_on);
14668376fe22STejun Heo 
14678376fe22STejun Heo /**
14688376fe22STejun Heo  * mod_delayed_work - modify delay of or queue a delayed work
14698376fe22STejun Heo  * @wq: workqueue to use
14708376fe22STejun Heo  * @dwork: work to queue
14718376fe22STejun Heo  * @delay: number of jiffies to wait before queueing
14728376fe22STejun Heo  *
14738376fe22STejun Heo  * mod_delayed_work_on() on local CPU.
14748376fe22STejun Heo  */
14758376fe22STejun Heo bool mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dwork,
14768376fe22STejun Heo 		      unsigned long delay)
14778376fe22STejun Heo {
14788376fe22STejun Heo 	return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
14798376fe22STejun Heo }
14808376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work);
14818376fe22STejun Heo 
14828376fe22STejun Heo /**
1483c8e55f36STejun Heo  * worker_enter_idle - enter idle state
1484c8e55f36STejun Heo  * @worker: worker which is entering idle state
1485c8e55f36STejun Heo  *
1486c8e55f36STejun Heo  * @worker is entering idle state.  Update stats and idle timer if
1487c8e55f36STejun Heo  * necessary.
1488c8e55f36STejun Heo  *
1489c8e55f36STejun Heo  * LOCKING:
1490d565ed63STejun Heo  * spin_lock_irq(pool->lock).
1491c8e55f36STejun Heo  */
1492c8e55f36STejun Heo static void worker_enter_idle(struct worker *worker)
14931da177e4SLinus Torvalds {
1494bd7bdd43STejun Heo 	struct worker_pool *pool = worker->pool;
1495c8e55f36STejun Heo 
14966183c009STejun Heo 	if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||
14976183c009STejun Heo 	    WARN_ON_ONCE(!list_empty(&worker->entry) &&
14986183c009STejun Heo 			 (worker->hentry.next || worker->hentry.pprev)))
14996183c009STejun Heo 		return;
1500c8e55f36STejun Heo 
1501cb444766STejun Heo 	/* can't use worker_set_flags(), also called from start_worker() */
1502cb444766STejun Heo 	worker->flags |= WORKER_IDLE;
1503bd7bdd43STejun Heo 	pool->nr_idle++;
1504e22bee78STejun Heo 	worker->last_active = jiffies;
1505c8e55f36STejun Heo 
1506c8e55f36STejun Heo 	/* idle_list is LIFO */
1507bd7bdd43STejun Heo 	list_add(&worker->entry, &pool->idle_list);
1508db7bccf4STejun Heo 
150963d95a91STejun Heo 	if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1510628c78e7STejun Heo 		mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
1511cb444766STejun Heo 
1512544ecf31STejun Heo 	/*
1513706026c2STejun Heo 	 * Sanity check nr_running.  Because wq_unbind_fn() releases
1514d565ed63STejun Heo 	 * pool->lock between setting %WORKER_UNBOUND and zapping
1515628c78e7STejun Heo 	 * nr_running, the warning may trigger spuriously.  Check iff
1516628c78e7STejun Heo 	 * unbind is not in progress.
1517544ecf31STejun Heo 	 */
151824647570STejun Heo 	WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
1519bd7bdd43STejun Heo 		     pool->nr_workers == pool->nr_idle &&
1520e19e397aSTejun Heo 		     atomic_read(&pool->nr_running));
1521c8e55f36STejun Heo }
1522c8e55f36STejun Heo 
1523c8e55f36STejun Heo /**
1524c8e55f36STejun Heo  * worker_leave_idle - leave idle state
1525c8e55f36STejun Heo  * @worker: worker which is leaving idle state
1526c8e55f36STejun Heo  *
1527c8e55f36STejun Heo  * @worker is leaving idle state.  Update stats.
1528c8e55f36STejun Heo  *
1529c8e55f36STejun Heo  * LOCKING:
1530d565ed63STejun Heo  * spin_lock_irq(pool->lock).
1531c8e55f36STejun Heo  */
1532c8e55f36STejun Heo static void worker_leave_idle(struct worker *worker)
1533c8e55f36STejun Heo {
1534bd7bdd43STejun Heo 	struct worker_pool *pool = worker->pool;
1535c8e55f36STejun Heo 
15366183c009STejun Heo 	if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))
15376183c009STejun Heo 		return;
1538d302f017STejun Heo 	worker_clr_flags(worker, WORKER_IDLE);
1539bd7bdd43STejun Heo 	pool->nr_idle--;
1540c8e55f36STejun Heo 	list_del_init(&worker->entry);
1541c8e55f36STejun Heo }
1542c8e55f36STejun Heo 
1543e22bee78STejun Heo /**
1544f36dc67bSLai Jiangshan  * worker_maybe_bind_and_lock - try to bind %current to worker_pool and lock it
1545f36dc67bSLai Jiangshan  * @pool: target worker_pool
1546f36dc67bSLai Jiangshan  *
1547f36dc67bSLai Jiangshan  * Bind %current to the cpu of @pool if it is associated and lock @pool.
1548e22bee78STejun Heo  *
1549e22bee78STejun Heo  * Works which are scheduled while the cpu is online must at least be
1550e22bee78STejun Heo  * scheduled to a worker which is bound to the cpu so that if they are
1551e22bee78STejun Heo  * flushed from cpu callbacks while cpu is going down, they are
1552e22bee78STejun Heo  * guaranteed to execute on the cpu.
1553e22bee78STejun Heo  *
1554f5faa077SLai Jiangshan  * This function is to be used by unbound workers and rescuers to bind
1555e22bee78STejun Heo  * themselves to the target cpu and may race with cpu going down or
1556e22bee78STejun Heo  * coming online.  kthread_bind() can't be used because it may put the
1557e22bee78STejun Heo  * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1558706026c2STejun Heo  * verbatim as it's best effort and blocking and pool may be
1559e22bee78STejun Heo  * [dis]associated in the meantime.
1560e22bee78STejun Heo  *
1561706026c2STejun Heo  * This function tries set_cpus_allowed() and locks pool and verifies the
156224647570STejun Heo  * binding against %POOL_DISASSOCIATED which is set during
1563f2d5a0eeSTejun Heo  * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1564f2d5a0eeSTejun Heo  * enters idle state or fetches works without dropping lock, it can
1565f2d5a0eeSTejun Heo  * guarantee the scheduling requirement described in the first paragraph.
1566e22bee78STejun Heo  *
1567e22bee78STejun Heo  * CONTEXT:
1568d565ed63STejun Heo  * Might sleep.  Called without any lock but returns with pool->lock
1569e22bee78STejun Heo  * held.
1570e22bee78STejun Heo  *
1571e22bee78STejun Heo  * RETURNS:
1572706026c2STejun Heo  * %true if the associated pool is online (@worker is successfully
1573e22bee78STejun Heo  * bound), %false if offline.
1574e22bee78STejun Heo  */
1575f36dc67bSLai Jiangshan static bool worker_maybe_bind_and_lock(struct worker_pool *pool)
1576d565ed63STejun Heo __acquires(&pool->lock)
1577e22bee78STejun Heo {
1578e22bee78STejun Heo 	while (true) {
1579e22bee78STejun Heo 		/*
1580e22bee78STejun Heo 		 * The following call may fail, succeed or succeed
1581e22bee78STejun Heo 		 * without actually migrating the task to the cpu if
1582e22bee78STejun Heo 		 * it races with cpu hotunplug operation.  Verify
158324647570STejun Heo 		 * against POOL_DISASSOCIATED.
1584e22bee78STejun Heo 		 */
158524647570STejun Heo 		if (!(pool->flags & POOL_DISASSOCIATED))
15867a4e344cSTejun Heo 			set_cpus_allowed_ptr(current, pool->attrs->cpumask);
1587e22bee78STejun Heo 
1588d565ed63STejun Heo 		spin_lock_irq(&pool->lock);
158924647570STejun Heo 		if (pool->flags & POOL_DISASSOCIATED)
1590e22bee78STejun Heo 			return false;
1591f5faa077SLai Jiangshan 		if (task_cpu(current) == pool->cpu &&
15927a4e344cSTejun Heo 		    cpumask_equal(&current->cpus_allowed, pool->attrs->cpumask))
1593e22bee78STejun Heo 			return true;
1594d565ed63STejun Heo 		spin_unlock_irq(&pool->lock);
1595e22bee78STejun Heo 
15965035b20fSTejun Heo 		/*
15975035b20fSTejun Heo 		 * We've raced with CPU hot[un]plug.  Give it a breather
15985035b20fSTejun Heo 		 * and retry migration.  cond_resched() is required here;
15995035b20fSTejun Heo 		 * otherwise, we might deadlock against cpu_stop trying to
16005035b20fSTejun Heo 		 * bring down the CPU on non-preemptive kernel.
16015035b20fSTejun Heo 		 */
1602e22bee78STejun Heo 		cpu_relax();
16035035b20fSTejun Heo 		cond_resched();
1604e22bee78STejun Heo 	}
1605e22bee78STejun Heo }
1606e22bee78STejun Heo 
1607e22bee78STejun Heo /*
1608ea1abd61SLai Jiangshan  * Rebind an idle @worker to its CPU.  worker_thread() will test
16095f7dabfdSLai Jiangshan  * list_empty(@worker->entry) before leaving idle and call this function.
161025511a47STejun Heo  */
161125511a47STejun Heo static void idle_worker_rebind(struct worker *worker)
161225511a47STejun Heo {
16135f7dabfdSLai Jiangshan 	/* CPU may go down again inbetween, clear UNBOUND only on success */
1614f36dc67bSLai Jiangshan 	if (worker_maybe_bind_and_lock(worker->pool))
16155f7dabfdSLai Jiangshan 		worker_clr_flags(worker, WORKER_UNBOUND);
161625511a47STejun Heo 
1617ea1abd61SLai Jiangshan 	/* rebind complete, become available again */
1618ea1abd61SLai Jiangshan 	list_add(&worker->entry, &worker->pool->idle_list);
1619d565ed63STejun Heo 	spin_unlock_irq(&worker->pool->lock);
162025511a47STejun Heo }
162125511a47STejun Heo 
162225511a47STejun Heo /*
162325511a47STejun Heo  * Function for @worker->rebind.work used to rebind unbound busy workers to
1624403c821dSTejun Heo  * the associated cpu which is coming back online.  This is scheduled by
1625403c821dSTejun Heo  * cpu up but can race with other cpu hotplug operations and may be
1626403c821dSTejun Heo  * executed twice without intervening cpu down.
1627e22bee78STejun Heo  */
162825511a47STejun Heo static void busy_worker_rebind_fn(struct work_struct *work)
1629e22bee78STejun Heo {
1630e22bee78STejun Heo 	struct worker *worker = container_of(work, struct worker, rebind_work);
1631e22bee78STejun Heo 
1632f36dc67bSLai Jiangshan 	if (worker_maybe_bind_and_lock(worker->pool))
1633eab6d828SLai Jiangshan 		worker_clr_flags(worker, WORKER_UNBOUND);
1634e22bee78STejun Heo 
1635d565ed63STejun Heo 	spin_unlock_irq(&worker->pool->lock);
1636e22bee78STejun Heo }
1637e22bee78STejun Heo 
163825511a47STejun Heo /**
163994cf58bbSTejun Heo  * rebind_workers - rebind all workers of a pool to the associated CPU
164094cf58bbSTejun Heo  * @pool: pool of interest
164125511a47STejun Heo  *
164294cf58bbSTejun Heo  * @pool->cpu is coming online.  Rebind all workers to the CPU.  Rebinding
164325511a47STejun Heo  * is different for idle and busy ones.
164425511a47STejun Heo  *
1645ea1abd61SLai Jiangshan  * Idle ones will be removed from the idle_list and woken up.  They will
1646ea1abd61SLai Jiangshan  * add themselves back after completing rebind.  This ensures that the
1647ea1abd61SLai Jiangshan  * idle_list doesn't contain any unbound workers when re-bound busy workers
1648ea1abd61SLai Jiangshan  * try to perform local wake-ups for concurrency management.
164925511a47STejun Heo  *
1650ea1abd61SLai Jiangshan  * Busy workers can rebind after they finish their current work items.
1651ea1abd61SLai Jiangshan  * Queueing the rebind work item at the head of the scheduled list is
1652ea1abd61SLai Jiangshan  * enough.  Note that nr_running will be properly bumped as busy workers
1653ea1abd61SLai Jiangshan  * rebind.
165425511a47STejun Heo  *
1655ea1abd61SLai Jiangshan  * On return, all non-manager workers are scheduled for rebind - see
1656ea1abd61SLai Jiangshan  * manage_workers() for the manager special case.  Any idle worker
1657ea1abd61SLai Jiangshan  * including the manager will not appear on @idle_list until rebind is
1658ea1abd61SLai Jiangshan  * complete, making local wake-ups safe.
165925511a47STejun Heo  */
166094cf58bbSTejun Heo static void rebind_workers(struct worker_pool *pool)
166125511a47STejun Heo {
1662ea1abd61SLai Jiangshan 	struct worker *worker, *n;
166325511a47STejun Heo 	int i;
166425511a47STejun Heo 
1665b2eb83d1SLai Jiangshan 	lockdep_assert_held(&pool->assoc_mutex);
1666d565ed63STejun Heo 	lockdep_assert_held(&pool->lock);
166725511a47STejun Heo 
16685f7dabfdSLai Jiangshan 	/* dequeue and kick idle ones */
1669ea1abd61SLai Jiangshan 	list_for_each_entry_safe(worker, n, &pool->idle_list, entry) {
1670ea1abd61SLai Jiangshan 		/*
167194cf58bbSTejun Heo 		 * idle workers should be off @pool->idle_list until rebind
167294cf58bbSTejun Heo 		 * is complete to avoid receiving premature local wake-ups.
1673ea1abd61SLai Jiangshan 		 */
1674ea1abd61SLai Jiangshan 		list_del_init(&worker->entry);
167525511a47STejun Heo 
167625511a47STejun Heo 		/*
167794cf58bbSTejun Heo 		 * worker_thread() will see the above dequeuing and call
167894cf58bbSTejun Heo 		 * idle_worker_rebind().
167925511a47STejun Heo 		 */
168025511a47STejun Heo 		wake_up_process(worker->task);
168125511a47STejun Heo 	}
168225511a47STejun Heo 
1683ea1abd61SLai Jiangshan 	/* rebind busy workers */
1684b67bfe0dSSasha Levin 	for_each_busy_worker(worker, i, pool) {
168525511a47STejun Heo 		struct work_struct *rebind_work = &worker->rebind_work;
1686e2b6a6d5SJoonsoo Kim 		struct workqueue_struct *wq;
168725511a47STejun Heo 
168825511a47STejun Heo 		if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
168925511a47STejun Heo 				     work_data_bits(rebind_work)))
169025511a47STejun Heo 			continue;
169125511a47STejun Heo 
169225511a47STejun Heo 		debug_work_activate(rebind_work);
169390beca5dSTejun Heo 
169490beca5dSTejun Heo 		/*
169594cf58bbSTejun Heo 		 * wq doesn't really matter but let's keep @worker->pool
1696112202d9STejun Heo 		 * and @pwq->pool consistent for sanity.
169790beca5dSTejun Heo 		 */
16987a4e344cSTejun Heo 		if (worker->pool->attrs->nice < 0)
1699e2b6a6d5SJoonsoo Kim 			wq = system_highpri_wq;
1700e2b6a6d5SJoonsoo Kim 		else
1701e2b6a6d5SJoonsoo Kim 			wq = system_wq;
1702ec58815aSTejun Heo 
17037fb98ea7STejun Heo 		insert_work(per_cpu_ptr(wq->cpu_pwqs, pool->cpu), rebind_work,
170425511a47STejun Heo 			    worker->scheduled.next,
170525511a47STejun Heo 			    work_color_to_flags(WORK_NO_COLOR));
1706ec58815aSTejun Heo 	}
170725511a47STejun Heo }
170825511a47STejun Heo 
1709c34056a3STejun Heo static struct worker *alloc_worker(void)
1710c34056a3STejun Heo {
1711c34056a3STejun Heo 	struct worker *worker;
1712c34056a3STejun Heo 
1713c34056a3STejun Heo 	worker = kzalloc(sizeof(*worker), GFP_KERNEL);
1714c8e55f36STejun Heo 	if (worker) {
1715c8e55f36STejun Heo 		INIT_LIST_HEAD(&worker->entry);
1716affee4b2STejun Heo 		INIT_LIST_HEAD(&worker->scheduled);
171725511a47STejun Heo 		INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn);
1718e22bee78STejun Heo 		/* on creation a worker is in !idle && prep state */
1719e22bee78STejun Heo 		worker->flags = WORKER_PREP;
1720c8e55f36STejun Heo 	}
1721c34056a3STejun Heo 	return worker;
1722c34056a3STejun Heo }
1723c34056a3STejun Heo 
1724c34056a3STejun Heo /**
1725c34056a3STejun Heo  * create_worker - create a new workqueue worker
172663d95a91STejun Heo  * @pool: pool the new worker will belong to
1727c34056a3STejun Heo  *
172863d95a91STejun Heo  * Create a new worker which is bound to @pool.  The returned worker
1729c34056a3STejun Heo  * can be started by calling start_worker() or destroyed using
1730c34056a3STejun Heo  * destroy_worker().
1731c34056a3STejun Heo  *
1732c34056a3STejun Heo  * CONTEXT:
1733c34056a3STejun Heo  * Might sleep.  Does GFP_KERNEL allocations.
1734c34056a3STejun Heo  *
1735c34056a3STejun Heo  * RETURNS:
1736c34056a3STejun Heo  * Pointer to the newly created worker.
1737c34056a3STejun Heo  */
1738bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool)
1739c34056a3STejun Heo {
17407a4e344cSTejun Heo 	const char *pri = pool->attrs->nice < 0  ? "H" : "";
1741c34056a3STejun Heo 	struct worker *worker = NULL;
1742f3421797STejun Heo 	int id = -1;
1743c34056a3STejun Heo 
1744d565ed63STejun Heo 	spin_lock_irq(&pool->lock);
1745bd7bdd43STejun Heo 	while (ida_get_new(&pool->worker_ida, &id)) {
1746d565ed63STejun Heo 		spin_unlock_irq(&pool->lock);
1747bd7bdd43STejun Heo 		if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
1748c34056a3STejun Heo 			goto fail;
1749d565ed63STejun Heo 		spin_lock_irq(&pool->lock);
1750c34056a3STejun Heo 	}
1751d565ed63STejun Heo 	spin_unlock_irq(&pool->lock);
1752c34056a3STejun Heo 
1753c34056a3STejun Heo 	worker = alloc_worker();
1754c34056a3STejun Heo 	if (!worker)
1755c34056a3STejun Heo 		goto fail;
1756c34056a3STejun Heo 
1757bd7bdd43STejun Heo 	worker->pool = pool;
1758c34056a3STejun Heo 	worker->id = id;
1759c34056a3STejun Heo 
1760*29c91e99STejun Heo 	if (pool->cpu >= 0)
176194dcf29aSEric Dumazet 		worker->task = kthread_create_on_node(worker_thread,
1762ec22ca5eSTejun Heo 					worker, cpu_to_node(pool->cpu),
1763d84ff051STejun Heo 					"kworker/%d:%d%s", pool->cpu, id, pri);
1764f3421797STejun Heo 	else
1765f3421797STejun Heo 		worker->task = kthread_create(worker_thread, worker,
17663270476aSTejun Heo 					      "kworker/u:%d%s", id, pri);
1767c34056a3STejun Heo 	if (IS_ERR(worker->task))
1768c34056a3STejun Heo 		goto fail;
1769c34056a3STejun Heo 
17707a4e344cSTejun Heo 	set_user_nice(worker->task, pool->attrs->nice);
17717a4e344cSTejun Heo 	set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
17723270476aSTejun Heo 
1773db7bccf4STejun Heo 	/*
17747a4e344cSTejun Heo 	 * %PF_THREAD_BOUND is used to prevent userland from meddling with
17757a4e344cSTejun Heo 	 * cpumask of workqueue workers.  This is an abuse.  We need
17767a4e344cSTejun Heo 	 * %PF_NO_SETAFFINITY.
1777db7bccf4STejun Heo 	 */
1778db7bccf4STejun Heo 	worker->task->flags |= PF_THREAD_BOUND;
17797a4e344cSTejun Heo 
17807a4e344cSTejun Heo 	/*
17817a4e344cSTejun Heo 	 * The caller is responsible for ensuring %POOL_DISASSOCIATED
17827a4e344cSTejun Heo 	 * remains stable across this function.  See the comments above the
17837a4e344cSTejun Heo 	 * flag definition for details.
17847a4e344cSTejun Heo 	 */
17857a4e344cSTejun Heo 	if (pool->flags & POOL_DISASSOCIATED)
1786f3421797STejun Heo 		worker->flags |= WORKER_UNBOUND;
1787c34056a3STejun Heo 
1788c34056a3STejun Heo 	return worker;
1789c34056a3STejun Heo fail:
1790c34056a3STejun Heo 	if (id >= 0) {
1791d565ed63STejun Heo 		spin_lock_irq(&pool->lock);
1792bd7bdd43STejun Heo 		ida_remove(&pool->worker_ida, id);
1793d565ed63STejun Heo 		spin_unlock_irq(&pool->lock);
1794c34056a3STejun Heo 	}
1795c34056a3STejun Heo 	kfree(worker);
1796c34056a3STejun Heo 	return NULL;
1797c34056a3STejun Heo }
1798c34056a3STejun Heo 
1799c34056a3STejun Heo /**
1800c34056a3STejun Heo  * start_worker - start a newly created worker
1801c34056a3STejun Heo  * @worker: worker to start
1802c34056a3STejun Heo  *
1803706026c2STejun Heo  * Make the pool aware of @worker and start it.
1804c34056a3STejun Heo  *
1805c34056a3STejun Heo  * CONTEXT:
1806d565ed63STejun Heo  * spin_lock_irq(pool->lock).
1807c34056a3STejun Heo  */
1808c34056a3STejun Heo static void start_worker(struct worker *worker)
1809c34056a3STejun Heo {
1810cb444766STejun Heo 	worker->flags |= WORKER_STARTED;
1811bd7bdd43STejun Heo 	worker->pool->nr_workers++;
1812c8e55f36STejun Heo 	worker_enter_idle(worker);
1813c34056a3STejun Heo 	wake_up_process(worker->task);
1814c34056a3STejun Heo }
1815c34056a3STejun Heo 
1816c34056a3STejun Heo /**
1817c34056a3STejun Heo  * destroy_worker - destroy a workqueue worker
1818c34056a3STejun Heo  * @worker: worker to be destroyed
1819c34056a3STejun Heo  *
1820706026c2STejun Heo  * Destroy @worker and adjust @pool stats accordingly.
1821c8e55f36STejun Heo  *
1822c8e55f36STejun Heo  * CONTEXT:
1823d565ed63STejun Heo  * spin_lock_irq(pool->lock) which is released and regrabbed.
1824c34056a3STejun Heo  */
1825c34056a3STejun Heo static void destroy_worker(struct worker *worker)
1826c34056a3STejun Heo {
1827bd7bdd43STejun Heo 	struct worker_pool *pool = worker->pool;
1828c34056a3STejun Heo 	int id = worker->id;
1829c34056a3STejun Heo 
1830c34056a3STejun Heo 	/* sanity check frenzy */
18316183c009STejun Heo 	if (WARN_ON(worker->current_work) ||
18326183c009STejun Heo 	    WARN_ON(!list_empty(&worker->scheduled)))
18336183c009STejun Heo 		return;
1834c34056a3STejun Heo 
1835c8e55f36STejun Heo 	if (worker->flags & WORKER_STARTED)
1836bd7bdd43STejun Heo 		pool->nr_workers--;
1837c8e55f36STejun Heo 	if (worker->flags & WORKER_IDLE)
1838bd7bdd43STejun Heo 		pool->nr_idle--;
1839c8e55f36STejun Heo 
1840c8e55f36STejun Heo 	list_del_init(&worker->entry);
1841cb444766STejun Heo 	worker->flags |= WORKER_DIE;
1842c8e55f36STejun Heo 
1843d565ed63STejun Heo 	spin_unlock_irq(&pool->lock);
1844c8e55f36STejun Heo 
1845c34056a3STejun Heo 	kthread_stop(worker->task);
1846c34056a3STejun Heo 	kfree(worker);
1847c34056a3STejun Heo 
1848d565ed63STejun Heo 	spin_lock_irq(&pool->lock);
1849bd7bdd43STejun Heo 	ida_remove(&pool->worker_ida, id);
1850c34056a3STejun Heo }
1851c34056a3STejun Heo 
185263d95a91STejun Heo static void idle_worker_timeout(unsigned long __pool)
1853e22bee78STejun Heo {
185463d95a91STejun Heo 	struct worker_pool *pool = (void *)__pool;
1855e22bee78STejun Heo 
1856d565ed63STejun Heo 	spin_lock_irq(&pool->lock);
1857e22bee78STejun Heo 
185863d95a91STejun Heo 	if (too_many_workers(pool)) {
1859e22bee78STejun Heo 		struct worker *worker;
1860e22bee78STejun Heo 		unsigned long expires;
1861e22bee78STejun Heo 
1862e22bee78STejun Heo 		/* idle_list is kept in LIFO order, check the last one */
186363d95a91STejun Heo 		worker = list_entry(pool->idle_list.prev, struct worker, entry);
1864e22bee78STejun Heo 		expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1865e22bee78STejun Heo 
1866e22bee78STejun Heo 		if (time_before(jiffies, expires))
186763d95a91STejun Heo 			mod_timer(&pool->idle_timer, expires);
1868e22bee78STejun Heo 		else {
1869e22bee78STejun Heo 			/* it's been idle for too long, wake up manager */
187011ebea50STejun Heo 			pool->flags |= POOL_MANAGE_WORKERS;
187163d95a91STejun Heo 			wake_up_worker(pool);
1872e22bee78STejun Heo 		}
1873e22bee78STejun Heo 	}
1874e22bee78STejun Heo 
1875d565ed63STejun Heo 	spin_unlock_irq(&pool->lock);
1876e22bee78STejun Heo }
1877e22bee78STejun Heo 
1878493a1724STejun Heo static void send_mayday(struct work_struct *work)
1879e22bee78STejun Heo {
1880112202d9STejun Heo 	struct pool_workqueue *pwq = get_work_pwq(work);
1881112202d9STejun Heo 	struct workqueue_struct *wq = pwq->wq;
1882493a1724STejun Heo 
1883493a1724STejun Heo 	lockdep_assert_held(&workqueue_lock);
1884e22bee78STejun Heo 
1885e22bee78STejun Heo 	if (!(wq->flags & WQ_RESCUER))
1886493a1724STejun Heo 		return;
1887e22bee78STejun Heo 
1888e22bee78STejun Heo 	/* mayday mayday mayday */
1889493a1724STejun Heo 	if (list_empty(&pwq->mayday_node)) {
1890493a1724STejun Heo 		list_add_tail(&pwq->mayday_node, &wq->maydays);
1891e22bee78STejun Heo 		wake_up_process(wq->rescuer->task);
1892493a1724STejun Heo 	}
1893e22bee78STejun Heo }
1894e22bee78STejun Heo 
1895706026c2STejun Heo static void pool_mayday_timeout(unsigned long __pool)
1896e22bee78STejun Heo {
189763d95a91STejun Heo 	struct worker_pool *pool = (void *)__pool;
1898e22bee78STejun Heo 	struct work_struct *work;
1899e22bee78STejun Heo 
1900493a1724STejun Heo 	spin_lock_irq(&workqueue_lock);		/* for wq->maydays */
1901493a1724STejun Heo 	spin_lock(&pool->lock);
1902e22bee78STejun Heo 
190363d95a91STejun Heo 	if (need_to_create_worker(pool)) {
1904e22bee78STejun Heo 		/*
1905e22bee78STejun Heo 		 * We've been trying to create a new worker but
1906e22bee78STejun Heo 		 * haven't been successful.  We might be hitting an
1907e22bee78STejun Heo 		 * allocation deadlock.  Send distress signals to
1908e22bee78STejun Heo 		 * rescuers.
1909e22bee78STejun Heo 		 */
191063d95a91STejun Heo 		list_for_each_entry(work, &pool->worklist, entry)
1911e22bee78STejun Heo 			send_mayday(work);
1912e22bee78STejun Heo 	}
1913e22bee78STejun Heo 
1914493a1724STejun Heo 	spin_unlock(&pool->lock);
1915493a1724STejun Heo 	spin_unlock_irq(&workqueue_lock);
1916e22bee78STejun Heo 
191763d95a91STejun Heo 	mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
1918e22bee78STejun Heo }
1919e22bee78STejun Heo 
1920e22bee78STejun Heo /**
1921e22bee78STejun Heo  * maybe_create_worker - create a new worker if necessary
192263d95a91STejun Heo  * @pool: pool to create a new worker for
1923e22bee78STejun Heo  *
192463d95a91STejun Heo  * Create a new worker for @pool if necessary.  @pool is guaranteed to
1925e22bee78STejun Heo  * have at least one idle worker on return from this function.  If
1926e22bee78STejun Heo  * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
192763d95a91STejun Heo  * sent to all rescuers with works scheduled on @pool to resolve
1928e22bee78STejun Heo  * possible allocation deadlock.
1929e22bee78STejun Heo  *
1930e22bee78STejun Heo  * On return, need_to_create_worker() is guaranteed to be false and
1931e22bee78STejun Heo  * may_start_working() true.
1932e22bee78STejun Heo  *
1933e22bee78STejun Heo  * LOCKING:
1934d565ed63STejun Heo  * spin_lock_irq(pool->lock) which may be released and regrabbed
1935e22bee78STejun Heo  * multiple times.  Does GFP_KERNEL allocations.  Called only from
1936e22bee78STejun Heo  * manager.
1937e22bee78STejun Heo  *
1938e22bee78STejun Heo  * RETURNS:
1939d565ed63STejun Heo  * false if no action was taken and pool->lock stayed locked, true
1940e22bee78STejun Heo  * otherwise.
1941e22bee78STejun Heo  */
194263d95a91STejun Heo static bool maybe_create_worker(struct worker_pool *pool)
1943d565ed63STejun Heo __releases(&pool->lock)
1944d565ed63STejun Heo __acquires(&pool->lock)
1945e22bee78STejun Heo {
194663d95a91STejun Heo 	if (!need_to_create_worker(pool))
1947e22bee78STejun Heo 		return false;
1948e22bee78STejun Heo restart:
1949d565ed63STejun Heo 	spin_unlock_irq(&pool->lock);
19509f9c2364STejun Heo 
1951e22bee78STejun Heo 	/* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
195263d95a91STejun Heo 	mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
1953e22bee78STejun Heo 
1954e22bee78STejun Heo 	while (true) {
1955e22bee78STejun Heo 		struct worker *worker;
1956e22bee78STejun Heo 
1957bc2ae0f5STejun Heo 		worker = create_worker(pool);
1958e22bee78STejun Heo 		if (worker) {
195963d95a91STejun Heo 			del_timer_sync(&pool->mayday_timer);
1960d565ed63STejun Heo 			spin_lock_irq(&pool->lock);
1961e22bee78STejun Heo 			start_worker(worker);
19626183c009STejun Heo 			if (WARN_ON_ONCE(need_to_create_worker(pool)))
19636183c009STejun Heo 				goto restart;
1964e22bee78STejun Heo 			return true;
1965e22bee78STejun Heo 		}
1966e22bee78STejun Heo 
196763d95a91STejun Heo 		if (!need_to_create_worker(pool))
1968e22bee78STejun Heo 			break;
1969e22bee78STejun Heo 
1970e22bee78STejun Heo 		__set_current_state(TASK_INTERRUPTIBLE);
1971e22bee78STejun Heo 		schedule_timeout(CREATE_COOLDOWN);
19729f9c2364STejun Heo 
197363d95a91STejun Heo 		if (!need_to_create_worker(pool))
1974e22bee78STejun Heo 			break;
1975e22bee78STejun Heo 	}
1976e22bee78STejun Heo 
197763d95a91STejun Heo 	del_timer_sync(&pool->mayday_timer);
1978d565ed63STejun Heo 	spin_lock_irq(&pool->lock);
197963d95a91STejun Heo 	if (need_to_create_worker(pool))
1980e22bee78STejun Heo 		goto restart;
1981e22bee78STejun Heo 	return true;
1982e22bee78STejun Heo }
1983e22bee78STejun Heo 
1984e22bee78STejun Heo /**
1985e22bee78STejun Heo  * maybe_destroy_worker - destroy workers which have been idle for a while
198663d95a91STejun Heo  * @pool: pool to destroy workers for
1987e22bee78STejun Heo  *
198863d95a91STejun Heo  * Destroy @pool workers which have been idle for longer than
1989e22bee78STejun Heo  * IDLE_WORKER_TIMEOUT.
1990e22bee78STejun Heo  *
1991e22bee78STejun Heo  * LOCKING:
1992d565ed63STejun Heo  * spin_lock_irq(pool->lock) which may be released and regrabbed
1993e22bee78STejun Heo  * multiple times.  Called only from manager.
1994e22bee78STejun Heo  *
1995e22bee78STejun Heo  * RETURNS:
1996d565ed63STejun Heo  * false if no action was taken and pool->lock stayed locked, true
1997e22bee78STejun Heo  * otherwise.
1998e22bee78STejun Heo  */
199963d95a91STejun Heo static bool maybe_destroy_workers(struct worker_pool *pool)
2000e22bee78STejun Heo {
2001e22bee78STejun Heo 	bool ret = false;
2002e22bee78STejun Heo 
200363d95a91STejun Heo 	while (too_many_workers(pool)) {
2004e22bee78STejun Heo 		struct worker *worker;
2005e22bee78STejun Heo 		unsigned long expires;
2006e22bee78STejun Heo 
200763d95a91STejun Heo 		worker = list_entry(pool->idle_list.prev, struct worker, entry);
2008e22bee78STejun Heo 		expires = worker->last_active + IDLE_WORKER_TIMEOUT;
2009e22bee78STejun Heo 
2010e22bee78STejun Heo 		if (time_before(jiffies, expires)) {
201163d95a91STejun Heo 			mod_timer(&pool->idle_timer, expires);
2012e22bee78STejun Heo 			break;
2013e22bee78STejun Heo 		}
2014e22bee78STejun Heo 
2015e22bee78STejun Heo 		destroy_worker(worker);
2016e22bee78STejun Heo 		ret = true;
2017e22bee78STejun Heo 	}
2018e22bee78STejun Heo 
2019e22bee78STejun Heo 	return ret;
2020e22bee78STejun Heo }
2021e22bee78STejun Heo 
2022e22bee78STejun Heo /**
2023e22bee78STejun Heo  * manage_workers - manage worker pool
2024e22bee78STejun Heo  * @worker: self
2025e22bee78STejun Heo  *
2026706026c2STejun Heo  * Assume the manager role and manage the worker pool @worker belongs
2027e22bee78STejun Heo  * to.  At any given time, there can be only zero or one manager per
2028706026c2STejun Heo  * pool.  The exclusion is handled automatically by this function.
2029e22bee78STejun Heo  *
2030e22bee78STejun Heo  * The caller can safely start processing works on false return.  On
2031e22bee78STejun Heo  * true return, it's guaranteed that need_to_create_worker() is false
2032e22bee78STejun Heo  * and may_start_working() is true.
2033e22bee78STejun Heo  *
2034e22bee78STejun Heo  * CONTEXT:
2035d565ed63STejun Heo  * spin_lock_irq(pool->lock) which may be released and regrabbed
2036e22bee78STejun Heo  * multiple times.  Does GFP_KERNEL allocations.
2037e22bee78STejun Heo  *
2038e22bee78STejun Heo  * RETURNS:
2039d565ed63STejun Heo  * spin_lock_irq(pool->lock) which may be released and regrabbed
2040d565ed63STejun Heo  * multiple times.  Does GFP_KERNEL allocations.
2041e22bee78STejun Heo  */
2042e22bee78STejun Heo static bool manage_workers(struct worker *worker)
2043e22bee78STejun Heo {
204463d95a91STejun Heo 	struct worker_pool *pool = worker->pool;
2045e22bee78STejun Heo 	bool ret = false;
2046e22bee78STejun Heo 
204734a06bd6STejun Heo 	if (!mutex_trylock(&pool->manager_arb))
2048e22bee78STejun Heo 		return ret;
2049e22bee78STejun Heo 
2050ee378aa4SLai Jiangshan 	/*
2051ee378aa4SLai Jiangshan 	 * To simplify both worker management and CPU hotplug, hold off
2052ee378aa4SLai Jiangshan 	 * management while hotplug is in progress.  CPU hotplug path can't
205334a06bd6STejun Heo 	 * grab @pool->manager_arb to achieve this because that can lead to
205434a06bd6STejun Heo 	 * idle worker depletion (all become busy thinking someone else is
205534a06bd6STejun Heo 	 * managing) which in turn can result in deadlock under extreme
205634a06bd6STejun Heo 	 * circumstances.  Use @pool->assoc_mutex to synchronize manager
205734a06bd6STejun Heo 	 * against CPU hotplug.
2058ee378aa4SLai Jiangshan 	 *
2059b2eb83d1SLai Jiangshan 	 * assoc_mutex would always be free unless CPU hotplug is in
2060d565ed63STejun Heo 	 * progress.  trylock first without dropping @pool->lock.
2061ee378aa4SLai Jiangshan 	 */
2062b2eb83d1SLai Jiangshan 	if (unlikely(!mutex_trylock(&pool->assoc_mutex))) {
2063d565ed63STejun Heo 		spin_unlock_irq(&pool->lock);
2064b2eb83d1SLai Jiangshan 		mutex_lock(&pool->assoc_mutex);
2065ee378aa4SLai Jiangshan 		/*
2066ee378aa4SLai Jiangshan 		 * CPU hotplug could have happened while we were waiting
2067b2eb83d1SLai Jiangshan 		 * for assoc_mutex.  Hotplug itself can't handle us
2068ee378aa4SLai Jiangshan 		 * because manager isn't either on idle or busy list, and
2069706026c2STejun Heo 		 * @pool's state and ours could have deviated.
2070ee378aa4SLai Jiangshan 		 *
2071b2eb83d1SLai Jiangshan 		 * As hotplug is now excluded via assoc_mutex, we can
2072ee378aa4SLai Jiangshan 		 * simply try to bind.  It will succeed or fail depending
2073706026c2STejun Heo 		 * on @pool's current state.  Try it and adjust
2074ee378aa4SLai Jiangshan 		 * %WORKER_UNBOUND accordingly.
2075ee378aa4SLai Jiangshan 		 */
2076f36dc67bSLai Jiangshan 		if (worker_maybe_bind_and_lock(pool))
2077ee378aa4SLai Jiangshan 			worker->flags &= ~WORKER_UNBOUND;
2078ee378aa4SLai Jiangshan 		else
2079ee378aa4SLai Jiangshan 			worker->flags |= WORKER_UNBOUND;
2080ee378aa4SLai Jiangshan 
2081ee378aa4SLai Jiangshan 		ret = true;
2082ee378aa4SLai Jiangshan 	}
2083ee378aa4SLai Jiangshan 
208411ebea50STejun Heo 	pool->flags &= ~POOL_MANAGE_WORKERS;
2085e22bee78STejun Heo 
2086e22bee78STejun Heo 	/*
2087e22bee78STejun Heo 	 * Destroy and then create so that may_start_working() is true
2088e22bee78STejun Heo 	 * on return.
2089e22bee78STejun Heo 	 */
209063d95a91STejun Heo 	ret |= maybe_destroy_workers(pool);
209163d95a91STejun Heo 	ret |= maybe_create_worker(pool);
2092e22bee78STejun Heo 
2093b2eb83d1SLai Jiangshan 	mutex_unlock(&pool->assoc_mutex);
209434a06bd6STejun Heo 	mutex_unlock(&pool->manager_arb);
2095e22bee78STejun Heo 	return ret;
2096e22bee78STejun Heo }
2097e22bee78STejun Heo 
2098a62428c0STejun Heo /**
2099a62428c0STejun Heo  * process_one_work - process single work
2100c34056a3STejun Heo  * @worker: self
2101a62428c0STejun Heo  * @work: work to process
2102a62428c0STejun Heo  *
2103a62428c0STejun Heo  * Process @work.  This function contains all the logics necessary to
2104a62428c0STejun Heo  * process a single work including synchronization against and
2105a62428c0STejun Heo  * interaction with other workers on the same cpu, queueing and
2106a62428c0STejun Heo  * flushing.  As long as context requirement is met, any worker can
2107a62428c0STejun Heo  * call this function to process a work.
2108a62428c0STejun Heo  *
2109a62428c0STejun Heo  * CONTEXT:
2110d565ed63STejun Heo  * spin_lock_irq(pool->lock) which is released and regrabbed.
2111a62428c0STejun Heo  */
2112c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work)
2113d565ed63STejun Heo __releases(&pool->lock)
2114d565ed63STejun Heo __acquires(&pool->lock)
21151da177e4SLinus Torvalds {
2116112202d9STejun Heo 	struct pool_workqueue *pwq = get_work_pwq(work);
2117bd7bdd43STejun Heo 	struct worker_pool *pool = worker->pool;
2118112202d9STejun Heo 	bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE;
211973f53c4aSTejun Heo 	int work_color;
21207e11629dSTejun Heo 	struct worker *collision;
21214e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
21224e6045f1SJohannes Berg 	/*
2123a62428c0STejun Heo 	 * It is permissible to free the struct work_struct from
2124a62428c0STejun Heo 	 * inside the function that is called from it, this we need to
2125a62428c0STejun Heo 	 * take into account for lockdep too.  To avoid bogus "held
2126a62428c0STejun Heo 	 * lock freed" warnings as well as problems when looking into
2127a62428c0STejun Heo 	 * work->lockdep_map, make a copy and use that here.
21284e6045f1SJohannes Berg 	 */
21294d82a1deSPeter Zijlstra 	struct lockdep_map lockdep_map;
21304d82a1deSPeter Zijlstra 
21314d82a1deSPeter Zijlstra 	lockdep_copy_map(&lockdep_map, &work->lockdep_map);
21324e6045f1SJohannes Berg #endif
21336fec10a1STejun Heo 	/*
21346fec10a1STejun Heo 	 * Ensure we're on the correct CPU.  DISASSOCIATED test is
21356fec10a1STejun Heo 	 * necessary to avoid spurious warnings from rescuers servicing the
213624647570STejun Heo 	 * unbound or a disassociated pool.
21376fec10a1STejun Heo 	 */
21385f7dabfdSLai Jiangshan 	WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) &&
213924647570STejun Heo 		     !(pool->flags & POOL_DISASSOCIATED) &&
2140ec22ca5eSTejun Heo 		     raw_smp_processor_id() != pool->cpu);
214125511a47STejun Heo 
21427e11629dSTejun Heo 	/*
21437e11629dSTejun Heo 	 * A single work shouldn't be executed concurrently by
21447e11629dSTejun Heo 	 * multiple workers on a single cpu.  Check whether anyone is
21457e11629dSTejun Heo 	 * already processing the work.  If so, defer the work to the
21467e11629dSTejun Heo 	 * currently executing one.
21477e11629dSTejun Heo 	 */
2148c9e7cf27STejun Heo 	collision = find_worker_executing_work(pool, work);
21497e11629dSTejun Heo 	if (unlikely(collision)) {
21507e11629dSTejun Heo 		move_linked_works(work, &collision->scheduled, NULL);
21517e11629dSTejun Heo 		return;
21527e11629dSTejun Heo 	}
21531da177e4SLinus Torvalds 
21548930cabaSTejun Heo 	/* claim and dequeue */
21551da177e4SLinus Torvalds 	debug_work_deactivate(work);
2156c9e7cf27STejun Heo 	hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
2157c34056a3STejun Heo 	worker->current_work = work;
2158a2c1c57bSTejun Heo 	worker->current_func = work->func;
2159112202d9STejun Heo 	worker->current_pwq = pwq;
216073f53c4aSTejun Heo 	work_color = get_work_color(work);
21617a22ad75STejun Heo 
2162a62428c0STejun Heo 	list_del_init(&work->entry);
2163a62428c0STejun Heo 
2164649027d7STejun Heo 	/*
2165fb0e7bebSTejun Heo 	 * CPU intensive works don't participate in concurrency
2166fb0e7bebSTejun Heo 	 * management.  They're the scheduler's responsibility.
2167fb0e7bebSTejun Heo 	 */
2168fb0e7bebSTejun Heo 	if (unlikely(cpu_intensive))
2169fb0e7bebSTejun Heo 		worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2170fb0e7bebSTejun Heo 
2171974271c4STejun Heo 	/*
2172d565ed63STejun Heo 	 * Unbound pool isn't concurrency managed and work items should be
2173974271c4STejun Heo 	 * executed ASAP.  Wake up another worker if necessary.
2174974271c4STejun Heo 	 */
217563d95a91STejun Heo 	if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
217663d95a91STejun Heo 		wake_up_worker(pool);
2177974271c4STejun Heo 
21788930cabaSTejun Heo 	/*
21797c3eed5cSTejun Heo 	 * Record the last pool and clear PENDING which should be the last
2180d565ed63STejun Heo 	 * update to @work.  Also, do this inside @pool->lock so that
218123657bb1STejun Heo 	 * PENDING and queued state changes happen together while IRQ is
218223657bb1STejun Heo 	 * disabled.
21838930cabaSTejun Heo 	 */
21847c3eed5cSTejun Heo 	set_work_pool_and_clear_pending(work, pool->id);
21851da177e4SLinus Torvalds 
2186d565ed63STejun Heo 	spin_unlock_irq(&pool->lock);
2187365970a1SDavid Howells 
2188112202d9STejun Heo 	lock_map_acquire_read(&pwq->wq->lockdep_map);
21893295f0efSIngo Molnar 	lock_map_acquire(&lockdep_map);
2190e36c886aSArjan van de Ven 	trace_workqueue_execute_start(work);
2191a2c1c57bSTejun Heo 	worker->current_func(work);
2192e36c886aSArjan van de Ven 	/*
2193e36c886aSArjan van de Ven 	 * While we must be careful to not use "work" after this, the trace
2194e36c886aSArjan van de Ven 	 * point will only record its address.
2195e36c886aSArjan van de Ven 	 */
2196e36c886aSArjan van de Ven 	trace_workqueue_execute_end(work);
21973295f0efSIngo Molnar 	lock_map_release(&lockdep_map);
2198112202d9STejun Heo 	lock_map_release(&pwq->wq->lockdep_map);
21991da177e4SLinus Torvalds 
2200d5abe669SPeter Zijlstra 	if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
2201044c782cSValentin Ilie 		pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2202044c782cSValentin Ilie 		       "     last function: %pf\n",
2203a2c1c57bSTejun Heo 		       current->comm, preempt_count(), task_pid_nr(current),
2204a2c1c57bSTejun Heo 		       worker->current_func);
2205d5abe669SPeter Zijlstra 		debug_show_held_locks(current);
2206d5abe669SPeter Zijlstra 		dump_stack();
2207d5abe669SPeter Zijlstra 	}
2208d5abe669SPeter Zijlstra 
2209d565ed63STejun Heo 	spin_lock_irq(&pool->lock);
2210a62428c0STejun Heo 
2211fb0e7bebSTejun Heo 	/* clear cpu intensive status */
2212fb0e7bebSTejun Heo 	if (unlikely(cpu_intensive))
2213fb0e7bebSTejun Heo 		worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2214fb0e7bebSTejun Heo 
2215a62428c0STejun Heo 	/* we're done with it, release */
221642f8570fSSasha Levin 	hash_del(&worker->hentry);
2217c34056a3STejun Heo 	worker->current_work = NULL;
2218a2c1c57bSTejun Heo 	worker->current_func = NULL;
2219112202d9STejun Heo 	worker->current_pwq = NULL;
2220112202d9STejun Heo 	pwq_dec_nr_in_flight(pwq, work_color);
22211da177e4SLinus Torvalds }
22221da177e4SLinus Torvalds 
2223affee4b2STejun Heo /**
2224affee4b2STejun Heo  * process_scheduled_works - process scheduled works
2225affee4b2STejun Heo  * @worker: self
2226affee4b2STejun Heo  *
2227affee4b2STejun Heo  * Process all scheduled works.  Please note that the scheduled list
2228affee4b2STejun Heo  * may change while processing a work, so this function repeatedly
2229affee4b2STejun Heo  * fetches a work from the top and executes it.
2230affee4b2STejun Heo  *
2231affee4b2STejun Heo  * CONTEXT:
2232d565ed63STejun Heo  * spin_lock_irq(pool->lock) which may be released and regrabbed
2233affee4b2STejun Heo  * multiple times.
2234affee4b2STejun Heo  */
2235affee4b2STejun Heo static void process_scheduled_works(struct worker *worker)
22361da177e4SLinus Torvalds {
2237affee4b2STejun Heo 	while (!list_empty(&worker->scheduled)) {
2238affee4b2STejun Heo 		struct work_struct *work = list_first_entry(&worker->scheduled,
2239a62428c0STejun Heo 						struct work_struct, entry);
2240c34056a3STejun Heo 		process_one_work(worker, work);
2241a62428c0STejun Heo 	}
22421da177e4SLinus Torvalds }
22431da177e4SLinus Torvalds 
22444690c4abSTejun Heo /**
22454690c4abSTejun Heo  * worker_thread - the worker thread function
2246c34056a3STejun Heo  * @__worker: self
22474690c4abSTejun Heo  *
2248706026c2STejun Heo  * The worker thread function.  There are NR_CPU_WORKER_POOLS dynamic pools
2249706026c2STejun Heo  * of these per each cpu.  These workers process all works regardless of
2250e22bee78STejun Heo  * their specific target workqueue.  The only exception is works which
2251e22bee78STejun Heo  * belong to workqueues with a rescuer which will be explained in
2252e22bee78STejun Heo  * rescuer_thread().
22534690c4abSTejun Heo  */
2254c34056a3STejun Heo static int worker_thread(void *__worker)
22551da177e4SLinus Torvalds {
2256c34056a3STejun Heo 	struct worker *worker = __worker;
2257bd7bdd43STejun Heo 	struct worker_pool *pool = worker->pool;
22581da177e4SLinus Torvalds 
2259e22bee78STejun Heo 	/* tell the scheduler that this is a workqueue worker */
2260e22bee78STejun Heo 	worker->task->flags |= PF_WQ_WORKER;
2261c8e55f36STejun Heo woke_up:
2262d565ed63STejun Heo 	spin_lock_irq(&pool->lock);
2263affee4b2STejun Heo 
22645f7dabfdSLai Jiangshan 	/* we are off idle list if destruction or rebind is requested */
22655f7dabfdSLai Jiangshan 	if (unlikely(list_empty(&worker->entry))) {
2266d565ed63STejun Heo 		spin_unlock_irq(&pool->lock);
226725511a47STejun Heo 
22685f7dabfdSLai Jiangshan 		/* if DIE is set, destruction is requested */
226925511a47STejun Heo 		if (worker->flags & WORKER_DIE) {
2270e22bee78STejun Heo 			worker->task->flags &= ~PF_WQ_WORKER;
2271c8e55f36STejun Heo 			return 0;
2272c8e55f36STejun Heo 		}
2273c8e55f36STejun Heo 
22745f7dabfdSLai Jiangshan 		/* otherwise, rebind */
227525511a47STejun Heo 		idle_worker_rebind(worker);
227625511a47STejun Heo 		goto woke_up;
227725511a47STejun Heo 	}
227825511a47STejun Heo 
2279c8e55f36STejun Heo 	worker_leave_idle(worker);
2280db7bccf4STejun Heo recheck:
2281e22bee78STejun Heo 	/* no more worker necessary? */
228263d95a91STejun Heo 	if (!need_more_worker(pool))
2283e22bee78STejun Heo 		goto sleep;
2284e22bee78STejun Heo 
2285e22bee78STejun Heo 	/* do we need to manage? */
228663d95a91STejun Heo 	if (unlikely(!may_start_working(pool)) && manage_workers(worker))
2287e22bee78STejun Heo 		goto recheck;
2288e22bee78STejun Heo 
2289c8e55f36STejun Heo 	/*
2290c8e55f36STejun Heo 	 * ->scheduled list can only be filled while a worker is
2291c8e55f36STejun Heo 	 * preparing to process a work or actually processing it.
2292c8e55f36STejun Heo 	 * Make sure nobody diddled with it while I was sleeping.
2293c8e55f36STejun Heo 	 */
22946183c009STejun Heo 	WARN_ON_ONCE(!list_empty(&worker->scheduled));
2295c8e55f36STejun Heo 
2296e22bee78STejun Heo 	/*
2297e22bee78STejun Heo 	 * When control reaches this point, we're guaranteed to have
2298e22bee78STejun Heo 	 * at least one idle worker or that someone else has already
2299e22bee78STejun Heo 	 * assumed the manager role.
2300e22bee78STejun Heo 	 */
2301e22bee78STejun Heo 	worker_clr_flags(worker, WORKER_PREP);
2302e22bee78STejun Heo 
2303e22bee78STejun Heo 	do {
2304affee4b2STejun Heo 		struct work_struct *work =
2305bd7bdd43STejun Heo 			list_first_entry(&pool->worklist,
2306affee4b2STejun Heo 					 struct work_struct, entry);
2307affee4b2STejun Heo 
2308c8e55f36STejun Heo 		if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2309affee4b2STejun Heo 			/* optimization path, not strictly necessary */
2310affee4b2STejun Heo 			process_one_work(worker, work);
2311affee4b2STejun Heo 			if (unlikely(!list_empty(&worker->scheduled)))
2312affee4b2STejun Heo 				process_scheduled_works(worker);
2313affee4b2STejun Heo 		} else {
2314c8e55f36STejun Heo 			move_linked_works(work, &worker->scheduled, NULL);
2315affee4b2STejun Heo 			process_scheduled_works(worker);
2316affee4b2STejun Heo 		}
231763d95a91STejun Heo 	} while (keep_working(pool));
2318affee4b2STejun Heo 
2319e22bee78STejun Heo 	worker_set_flags(worker, WORKER_PREP, false);
2320d313dd85STejun Heo sleep:
232163d95a91STejun Heo 	if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
2322e22bee78STejun Heo 		goto recheck;
2323d313dd85STejun Heo 
2324c8e55f36STejun Heo 	/*
2325d565ed63STejun Heo 	 * pool->lock is held and there's no work to process and no need to
2326d565ed63STejun Heo 	 * manage, sleep.  Workers are woken up only while holding
2327d565ed63STejun Heo 	 * pool->lock or from local cpu, so setting the current state
2328d565ed63STejun Heo 	 * before releasing pool->lock is enough to prevent losing any
2329d565ed63STejun Heo 	 * event.
2330c8e55f36STejun Heo 	 */
2331c8e55f36STejun Heo 	worker_enter_idle(worker);
2332c8e55f36STejun Heo 	__set_current_state(TASK_INTERRUPTIBLE);
2333d565ed63STejun Heo 	spin_unlock_irq(&pool->lock);
23341da177e4SLinus Torvalds 	schedule();
2335c8e55f36STejun Heo 	goto woke_up;
23361da177e4SLinus Torvalds }
23371da177e4SLinus Torvalds 
2338e22bee78STejun Heo /**
2339e22bee78STejun Heo  * rescuer_thread - the rescuer thread function
2340111c225aSTejun Heo  * @__rescuer: self
2341e22bee78STejun Heo  *
2342e22bee78STejun Heo  * Workqueue rescuer thread function.  There's one rescuer for each
2343e22bee78STejun Heo  * workqueue which has WQ_RESCUER set.
2344e22bee78STejun Heo  *
2345706026c2STejun Heo  * Regular work processing on a pool may block trying to create a new
2346e22bee78STejun Heo  * worker which uses GFP_KERNEL allocation which has slight chance of
2347e22bee78STejun Heo  * developing into deadlock if some works currently on the same queue
2348e22bee78STejun Heo  * need to be processed to satisfy the GFP_KERNEL allocation.  This is
2349e22bee78STejun Heo  * the problem rescuer solves.
2350e22bee78STejun Heo  *
2351706026c2STejun Heo  * When such condition is possible, the pool summons rescuers of all
2352706026c2STejun Heo  * workqueues which have works queued on the pool and let them process
2353e22bee78STejun Heo  * those works so that forward progress can be guaranteed.
2354e22bee78STejun Heo  *
2355e22bee78STejun Heo  * This should happen rarely.
2356e22bee78STejun Heo  */
2357111c225aSTejun Heo static int rescuer_thread(void *__rescuer)
2358e22bee78STejun Heo {
2359111c225aSTejun Heo 	struct worker *rescuer = __rescuer;
2360111c225aSTejun Heo 	struct workqueue_struct *wq = rescuer->rescue_wq;
2361e22bee78STejun Heo 	struct list_head *scheduled = &rescuer->scheduled;
2362e22bee78STejun Heo 
2363e22bee78STejun Heo 	set_user_nice(current, RESCUER_NICE_LEVEL);
2364111c225aSTejun Heo 
2365111c225aSTejun Heo 	/*
2366111c225aSTejun Heo 	 * Mark rescuer as worker too.  As WORKER_PREP is never cleared, it
2367111c225aSTejun Heo 	 * doesn't participate in concurrency management.
2368111c225aSTejun Heo 	 */
2369111c225aSTejun Heo 	rescuer->task->flags |= PF_WQ_WORKER;
2370e22bee78STejun Heo repeat:
2371e22bee78STejun Heo 	set_current_state(TASK_INTERRUPTIBLE);
23721da177e4SLinus Torvalds 
2373412d32e6SMike Galbraith 	if (kthread_should_stop()) {
2374412d32e6SMike Galbraith 		__set_current_state(TASK_RUNNING);
2375111c225aSTejun Heo 		rescuer->task->flags &= ~PF_WQ_WORKER;
2376e22bee78STejun Heo 		return 0;
2377412d32e6SMike Galbraith 	}
23781da177e4SLinus Torvalds 
2379493a1724STejun Heo 	/* see whether any pwq is asking for help */
2380493a1724STejun Heo 	spin_lock_irq(&workqueue_lock);
2381493a1724STejun Heo 
2382493a1724STejun Heo 	while (!list_empty(&wq->maydays)) {
2383493a1724STejun Heo 		struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
2384493a1724STejun Heo 					struct pool_workqueue, mayday_node);
2385112202d9STejun Heo 		struct worker_pool *pool = pwq->pool;
2386e22bee78STejun Heo 		struct work_struct *work, *n;
2387e22bee78STejun Heo 
2388e22bee78STejun Heo 		__set_current_state(TASK_RUNNING);
2389493a1724STejun Heo 		list_del_init(&pwq->mayday_node);
2390493a1724STejun Heo 
2391493a1724STejun Heo 		spin_unlock_irq(&workqueue_lock);
2392e22bee78STejun Heo 
2393e22bee78STejun Heo 		/* migrate to the target cpu if possible */
2394f36dc67bSLai Jiangshan 		worker_maybe_bind_and_lock(pool);
2395b3104104SLai Jiangshan 		rescuer->pool = pool;
2396e22bee78STejun Heo 
2397e22bee78STejun Heo 		/*
2398e22bee78STejun Heo 		 * Slurp in all works issued via this workqueue and
2399e22bee78STejun Heo 		 * process'em.
2400e22bee78STejun Heo 		 */
24016183c009STejun Heo 		WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
2402bd7bdd43STejun Heo 		list_for_each_entry_safe(work, n, &pool->worklist, entry)
2403112202d9STejun Heo 			if (get_work_pwq(work) == pwq)
2404e22bee78STejun Heo 				move_linked_works(work, scheduled, &n);
2405e22bee78STejun Heo 
2406e22bee78STejun Heo 		process_scheduled_works(rescuer);
24077576958aSTejun Heo 
24087576958aSTejun Heo 		/*
2409d565ed63STejun Heo 		 * Leave this pool.  If keep_working() is %true, notify a
24107576958aSTejun Heo 		 * regular worker; otherwise, we end up with 0 concurrency
24117576958aSTejun Heo 		 * and stalling the execution.
24127576958aSTejun Heo 		 */
241363d95a91STejun Heo 		if (keep_working(pool))
241463d95a91STejun Heo 			wake_up_worker(pool);
24157576958aSTejun Heo 
2416b3104104SLai Jiangshan 		rescuer->pool = NULL;
2417493a1724STejun Heo 		spin_unlock(&pool->lock);
2418493a1724STejun Heo 		spin_lock(&workqueue_lock);
24191da177e4SLinus Torvalds 	}
24201da177e4SLinus Torvalds 
2421493a1724STejun Heo 	spin_unlock_irq(&workqueue_lock);
2422493a1724STejun Heo 
2423111c225aSTejun Heo 	/* rescuers should never participate in concurrency management */
2424111c225aSTejun Heo 	WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
2425e22bee78STejun Heo 	schedule();
2426e22bee78STejun Heo 	goto repeat;
24271da177e4SLinus Torvalds }
24281da177e4SLinus Torvalds 
2429fc2e4d70SOleg Nesterov struct wq_barrier {
2430fc2e4d70SOleg Nesterov 	struct work_struct	work;
2431fc2e4d70SOleg Nesterov 	struct completion	done;
2432fc2e4d70SOleg Nesterov };
2433fc2e4d70SOleg Nesterov 
2434fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work)
2435fc2e4d70SOleg Nesterov {
2436fc2e4d70SOleg Nesterov 	struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2437fc2e4d70SOleg Nesterov 	complete(&barr->done);
2438fc2e4d70SOleg Nesterov }
2439fc2e4d70SOleg Nesterov 
24404690c4abSTejun Heo /**
24414690c4abSTejun Heo  * insert_wq_barrier - insert a barrier work
2442112202d9STejun Heo  * @pwq: pwq to insert barrier into
24434690c4abSTejun Heo  * @barr: wq_barrier to insert
2444affee4b2STejun Heo  * @target: target work to attach @barr to
2445affee4b2STejun Heo  * @worker: worker currently executing @target, NULL if @target is not executing
24464690c4abSTejun Heo  *
2447affee4b2STejun Heo  * @barr is linked to @target such that @barr is completed only after
2448affee4b2STejun Heo  * @target finishes execution.  Please note that the ordering
2449affee4b2STejun Heo  * guarantee is observed only with respect to @target and on the local
2450affee4b2STejun Heo  * cpu.
2451affee4b2STejun Heo  *
2452affee4b2STejun Heo  * Currently, a queued barrier can't be canceled.  This is because
2453affee4b2STejun Heo  * try_to_grab_pending() can't determine whether the work to be
2454affee4b2STejun Heo  * grabbed is at the head of the queue and thus can't clear LINKED
2455affee4b2STejun Heo  * flag of the previous work while there must be a valid next work
2456affee4b2STejun Heo  * after a work with LINKED flag set.
2457affee4b2STejun Heo  *
2458affee4b2STejun Heo  * Note that when @worker is non-NULL, @target may be modified
2459112202d9STejun Heo  * underneath us, so we can't reliably determine pwq from @target.
24604690c4abSTejun Heo  *
24614690c4abSTejun Heo  * CONTEXT:
2462d565ed63STejun Heo  * spin_lock_irq(pool->lock).
24634690c4abSTejun Heo  */
2464112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq,
2465affee4b2STejun Heo 			      struct wq_barrier *barr,
2466affee4b2STejun Heo 			      struct work_struct *target, struct worker *worker)
2467fc2e4d70SOleg Nesterov {
2468affee4b2STejun Heo 	struct list_head *head;
2469affee4b2STejun Heo 	unsigned int linked = 0;
2470affee4b2STejun Heo 
2471dc186ad7SThomas Gleixner 	/*
2472d565ed63STejun Heo 	 * debugobject calls are safe here even with pool->lock locked
2473dc186ad7SThomas Gleixner 	 * as we know for sure that this will not trigger any of the
2474dc186ad7SThomas Gleixner 	 * checks and call back into the fixup functions where we
2475dc186ad7SThomas Gleixner 	 * might deadlock.
2476dc186ad7SThomas Gleixner 	 */
2477ca1cab37SAndrew Morton 	INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
247822df02bbSTejun Heo 	__set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
2479fc2e4d70SOleg Nesterov 	init_completion(&barr->done);
248083c22520SOleg Nesterov 
2481affee4b2STejun Heo 	/*
2482affee4b2STejun Heo 	 * If @target is currently being executed, schedule the
2483affee4b2STejun Heo 	 * barrier to the worker; otherwise, put it after @target.
2484affee4b2STejun Heo 	 */
2485affee4b2STejun Heo 	if (worker)
2486affee4b2STejun Heo 		head = worker->scheduled.next;
2487affee4b2STejun Heo 	else {
2488affee4b2STejun Heo 		unsigned long *bits = work_data_bits(target);
2489affee4b2STejun Heo 
2490affee4b2STejun Heo 		head = target->entry.next;
2491affee4b2STejun Heo 		/* there can already be other linked works, inherit and set */
2492affee4b2STejun Heo 		linked = *bits & WORK_STRUCT_LINKED;
2493affee4b2STejun Heo 		__set_bit(WORK_STRUCT_LINKED_BIT, bits);
2494affee4b2STejun Heo 	}
2495affee4b2STejun Heo 
2496dc186ad7SThomas Gleixner 	debug_work_activate(&barr->work);
2497112202d9STejun Heo 	insert_work(pwq, &barr->work, head,
2498affee4b2STejun Heo 		    work_color_to_flags(WORK_NO_COLOR) | linked);
2499fc2e4d70SOleg Nesterov }
2500fc2e4d70SOleg Nesterov 
250173f53c4aSTejun Heo /**
2502112202d9STejun Heo  * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing
250373f53c4aSTejun Heo  * @wq: workqueue being flushed
250473f53c4aSTejun Heo  * @flush_color: new flush color, < 0 for no-op
250573f53c4aSTejun Heo  * @work_color: new work color, < 0 for no-op
250673f53c4aSTejun Heo  *
2507112202d9STejun Heo  * Prepare pwqs for workqueue flushing.
250873f53c4aSTejun Heo  *
2509112202d9STejun Heo  * If @flush_color is non-negative, flush_color on all pwqs should be
2510112202d9STejun Heo  * -1.  If no pwq has in-flight commands at the specified color, all
2511112202d9STejun Heo  * pwq->flush_color's stay at -1 and %false is returned.  If any pwq
2512112202d9STejun Heo  * has in flight commands, its pwq->flush_color is set to
2513112202d9STejun Heo  * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq
251473f53c4aSTejun Heo  * wakeup logic is armed and %true is returned.
251573f53c4aSTejun Heo  *
251673f53c4aSTejun Heo  * The caller should have initialized @wq->first_flusher prior to
251773f53c4aSTejun Heo  * calling this function with non-negative @flush_color.  If
251873f53c4aSTejun Heo  * @flush_color is negative, no flush color update is done and %false
251973f53c4aSTejun Heo  * is returned.
252073f53c4aSTejun Heo  *
2521112202d9STejun Heo  * If @work_color is non-negative, all pwqs should have the same
252273f53c4aSTejun Heo  * work_color which is previous to @work_color and all will be
252373f53c4aSTejun Heo  * advanced to @work_color.
252473f53c4aSTejun Heo  *
252573f53c4aSTejun Heo  * CONTEXT:
252673f53c4aSTejun Heo  * mutex_lock(wq->flush_mutex).
252773f53c4aSTejun Heo  *
252873f53c4aSTejun Heo  * RETURNS:
252973f53c4aSTejun Heo  * %true if @flush_color >= 0 and there's something to flush.  %false
253073f53c4aSTejun Heo  * otherwise.
253173f53c4aSTejun Heo  */
2532112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
253373f53c4aSTejun Heo 				      int flush_color, int work_color)
25341da177e4SLinus Torvalds {
253573f53c4aSTejun Heo 	bool wait = false;
253649e3cf44STejun Heo 	struct pool_workqueue *pwq;
25371da177e4SLinus Torvalds 
253873f53c4aSTejun Heo 	if (flush_color >= 0) {
25396183c009STejun Heo 		WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
2540112202d9STejun Heo 		atomic_set(&wq->nr_pwqs_to_flush, 1);
2541dc186ad7SThomas Gleixner 	}
254214441960SOleg Nesterov 
254376af4d93STejun Heo 	local_irq_disable();
254476af4d93STejun Heo 
254549e3cf44STejun Heo 	for_each_pwq(pwq, wq) {
2546112202d9STejun Heo 		struct worker_pool *pool = pwq->pool;
25471da177e4SLinus Torvalds 
254876af4d93STejun Heo 		spin_lock(&pool->lock);
254973f53c4aSTejun Heo 
255073f53c4aSTejun Heo 		if (flush_color >= 0) {
25516183c009STejun Heo 			WARN_ON_ONCE(pwq->flush_color != -1);
255273f53c4aSTejun Heo 
2553112202d9STejun Heo 			if (pwq->nr_in_flight[flush_color]) {
2554112202d9STejun Heo 				pwq->flush_color = flush_color;
2555112202d9STejun Heo 				atomic_inc(&wq->nr_pwqs_to_flush);
255673f53c4aSTejun Heo 				wait = true;
25571da177e4SLinus Torvalds 			}
255873f53c4aSTejun Heo 		}
255973f53c4aSTejun Heo 
256073f53c4aSTejun Heo 		if (work_color >= 0) {
25616183c009STejun Heo 			WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
2562112202d9STejun Heo 			pwq->work_color = work_color;
256373f53c4aSTejun Heo 		}
256473f53c4aSTejun Heo 
256576af4d93STejun Heo 		spin_unlock(&pool->lock);
25661da177e4SLinus Torvalds 	}
25671da177e4SLinus Torvalds 
256876af4d93STejun Heo 	local_irq_enable();
256976af4d93STejun Heo 
2570112202d9STejun Heo 	if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush))
257173f53c4aSTejun Heo 		complete(&wq->first_flusher->done);
257273f53c4aSTejun Heo 
257373f53c4aSTejun Heo 	return wait;
257483c22520SOleg Nesterov }
25751da177e4SLinus Torvalds 
25760fcb78c2SRolf Eike Beer /**
25771da177e4SLinus Torvalds  * flush_workqueue - ensure that any scheduled work has run to completion.
25780fcb78c2SRolf Eike Beer  * @wq: workqueue to flush
25791da177e4SLinus Torvalds  *
25801da177e4SLinus Torvalds  * Forces execution of the workqueue and blocks until its completion.
25811da177e4SLinus Torvalds  * This is typically used in driver shutdown handlers.
25821da177e4SLinus Torvalds  *
2583fc2e4d70SOleg Nesterov  * We sleep until all works which were queued on entry have been handled,
2584fc2e4d70SOleg Nesterov  * but we are not livelocked by new incoming ones.
25851da177e4SLinus Torvalds  */
25867ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq)
25871da177e4SLinus Torvalds {
258873f53c4aSTejun Heo 	struct wq_flusher this_flusher = {
258973f53c4aSTejun Heo 		.list = LIST_HEAD_INIT(this_flusher.list),
259073f53c4aSTejun Heo 		.flush_color = -1,
259173f53c4aSTejun Heo 		.done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
259273f53c4aSTejun Heo 	};
259373f53c4aSTejun Heo 	int next_color;
2594b1f4ec17SOleg Nesterov 
25953295f0efSIngo Molnar 	lock_map_acquire(&wq->lockdep_map);
25963295f0efSIngo Molnar 	lock_map_release(&wq->lockdep_map);
259773f53c4aSTejun Heo 
259873f53c4aSTejun Heo 	mutex_lock(&wq->flush_mutex);
259973f53c4aSTejun Heo 
260073f53c4aSTejun Heo 	/*
260173f53c4aSTejun Heo 	 * Start-to-wait phase
260273f53c4aSTejun Heo 	 */
260373f53c4aSTejun Heo 	next_color = work_next_color(wq->work_color);
260473f53c4aSTejun Heo 
260573f53c4aSTejun Heo 	if (next_color != wq->flush_color) {
260673f53c4aSTejun Heo 		/*
260773f53c4aSTejun Heo 		 * Color space is not full.  The current work_color
260873f53c4aSTejun Heo 		 * becomes our flush_color and work_color is advanced
260973f53c4aSTejun Heo 		 * by one.
261073f53c4aSTejun Heo 		 */
26116183c009STejun Heo 		WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
261273f53c4aSTejun Heo 		this_flusher.flush_color = wq->work_color;
261373f53c4aSTejun Heo 		wq->work_color = next_color;
261473f53c4aSTejun Heo 
261573f53c4aSTejun Heo 		if (!wq->first_flusher) {
261673f53c4aSTejun Heo 			/* no flush in progress, become the first flusher */
26176183c009STejun Heo 			WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
261873f53c4aSTejun Heo 
261973f53c4aSTejun Heo 			wq->first_flusher = &this_flusher;
262073f53c4aSTejun Heo 
2621112202d9STejun Heo 			if (!flush_workqueue_prep_pwqs(wq, wq->flush_color,
262273f53c4aSTejun Heo 						       wq->work_color)) {
262373f53c4aSTejun Heo 				/* nothing to flush, done */
262473f53c4aSTejun Heo 				wq->flush_color = next_color;
262573f53c4aSTejun Heo 				wq->first_flusher = NULL;
262673f53c4aSTejun Heo 				goto out_unlock;
262773f53c4aSTejun Heo 			}
262873f53c4aSTejun Heo 		} else {
262973f53c4aSTejun Heo 			/* wait in queue */
26306183c009STejun Heo 			WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
263173f53c4aSTejun Heo 			list_add_tail(&this_flusher.list, &wq->flusher_queue);
2632112202d9STejun Heo 			flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
263373f53c4aSTejun Heo 		}
263473f53c4aSTejun Heo 	} else {
263573f53c4aSTejun Heo 		/*
263673f53c4aSTejun Heo 		 * Oops, color space is full, wait on overflow queue.
263773f53c4aSTejun Heo 		 * The next flush completion will assign us
263873f53c4aSTejun Heo 		 * flush_color and transfer to flusher_queue.
263973f53c4aSTejun Heo 		 */
264073f53c4aSTejun Heo 		list_add_tail(&this_flusher.list, &wq->flusher_overflow);
264173f53c4aSTejun Heo 	}
264273f53c4aSTejun Heo 
264373f53c4aSTejun Heo 	mutex_unlock(&wq->flush_mutex);
264473f53c4aSTejun Heo 
264573f53c4aSTejun Heo 	wait_for_completion(&this_flusher.done);
264673f53c4aSTejun Heo 
264773f53c4aSTejun Heo 	/*
264873f53c4aSTejun Heo 	 * Wake-up-and-cascade phase
264973f53c4aSTejun Heo 	 *
265073f53c4aSTejun Heo 	 * First flushers are responsible for cascading flushes and
265173f53c4aSTejun Heo 	 * handling overflow.  Non-first flushers can simply return.
265273f53c4aSTejun Heo 	 */
265373f53c4aSTejun Heo 	if (wq->first_flusher != &this_flusher)
265473f53c4aSTejun Heo 		return;
265573f53c4aSTejun Heo 
265673f53c4aSTejun Heo 	mutex_lock(&wq->flush_mutex);
265773f53c4aSTejun Heo 
26584ce48b37STejun Heo 	/* we might have raced, check again with mutex held */
26594ce48b37STejun Heo 	if (wq->first_flusher != &this_flusher)
26604ce48b37STejun Heo 		goto out_unlock;
26614ce48b37STejun Heo 
266273f53c4aSTejun Heo 	wq->first_flusher = NULL;
266373f53c4aSTejun Heo 
26646183c009STejun Heo 	WARN_ON_ONCE(!list_empty(&this_flusher.list));
26656183c009STejun Heo 	WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
266673f53c4aSTejun Heo 
266773f53c4aSTejun Heo 	while (true) {
266873f53c4aSTejun Heo 		struct wq_flusher *next, *tmp;
266973f53c4aSTejun Heo 
267073f53c4aSTejun Heo 		/* complete all the flushers sharing the current flush color */
267173f53c4aSTejun Heo 		list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
267273f53c4aSTejun Heo 			if (next->flush_color != wq->flush_color)
267373f53c4aSTejun Heo 				break;
267473f53c4aSTejun Heo 			list_del_init(&next->list);
267573f53c4aSTejun Heo 			complete(&next->done);
267673f53c4aSTejun Heo 		}
267773f53c4aSTejun Heo 
26786183c009STejun Heo 		WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
267973f53c4aSTejun Heo 			     wq->flush_color != work_next_color(wq->work_color));
268073f53c4aSTejun Heo 
268173f53c4aSTejun Heo 		/* this flush_color is finished, advance by one */
268273f53c4aSTejun Heo 		wq->flush_color = work_next_color(wq->flush_color);
268373f53c4aSTejun Heo 
268473f53c4aSTejun Heo 		/* one color has been freed, handle overflow queue */
268573f53c4aSTejun Heo 		if (!list_empty(&wq->flusher_overflow)) {
268673f53c4aSTejun Heo 			/*
268773f53c4aSTejun Heo 			 * Assign the same color to all overflowed
268873f53c4aSTejun Heo 			 * flushers, advance work_color and append to
268973f53c4aSTejun Heo 			 * flusher_queue.  This is the start-to-wait
269073f53c4aSTejun Heo 			 * phase for these overflowed flushers.
269173f53c4aSTejun Heo 			 */
269273f53c4aSTejun Heo 			list_for_each_entry(tmp, &wq->flusher_overflow, list)
269373f53c4aSTejun Heo 				tmp->flush_color = wq->work_color;
269473f53c4aSTejun Heo 
269573f53c4aSTejun Heo 			wq->work_color = work_next_color(wq->work_color);
269673f53c4aSTejun Heo 
269773f53c4aSTejun Heo 			list_splice_tail_init(&wq->flusher_overflow,
269873f53c4aSTejun Heo 					      &wq->flusher_queue);
2699112202d9STejun Heo 			flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
270073f53c4aSTejun Heo 		}
270173f53c4aSTejun Heo 
270273f53c4aSTejun Heo 		if (list_empty(&wq->flusher_queue)) {
27036183c009STejun Heo 			WARN_ON_ONCE(wq->flush_color != wq->work_color);
270473f53c4aSTejun Heo 			break;
270573f53c4aSTejun Heo 		}
270673f53c4aSTejun Heo 
270773f53c4aSTejun Heo 		/*
270873f53c4aSTejun Heo 		 * Need to flush more colors.  Make the next flusher
2709112202d9STejun Heo 		 * the new first flusher and arm pwqs.
271073f53c4aSTejun Heo 		 */
27116183c009STejun Heo 		WARN_ON_ONCE(wq->flush_color == wq->work_color);
27126183c009STejun Heo 		WARN_ON_ONCE(wq->flush_color != next->flush_color);
271373f53c4aSTejun Heo 
271473f53c4aSTejun Heo 		list_del_init(&next->list);
271573f53c4aSTejun Heo 		wq->first_flusher = next;
271673f53c4aSTejun Heo 
2717112202d9STejun Heo 		if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1))
271873f53c4aSTejun Heo 			break;
271973f53c4aSTejun Heo 
272073f53c4aSTejun Heo 		/*
272173f53c4aSTejun Heo 		 * Meh... this color is already done, clear first
272273f53c4aSTejun Heo 		 * flusher and repeat cascading.
272373f53c4aSTejun Heo 		 */
272473f53c4aSTejun Heo 		wq->first_flusher = NULL;
272573f53c4aSTejun Heo 	}
272673f53c4aSTejun Heo 
272773f53c4aSTejun Heo out_unlock:
272873f53c4aSTejun Heo 	mutex_unlock(&wq->flush_mutex);
27291da177e4SLinus Torvalds }
2730ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(flush_workqueue);
27311da177e4SLinus Torvalds 
27329c5a2ba7STejun Heo /**
27339c5a2ba7STejun Heo  * drain_workqueue - drain a workqueue
27349c5a2ba7STejun Heo  * @wq: workqueue to drain
27359c5a2ba7STejun Heo  *
27369c5a2ba7STejun Heo  * Wait until the workqueue becomes empty.  While draining is in progress,
27379c5a2ba7STejun Heo  * only chain queueing is allowed.  IOW, only currently pending or running
27389c5a2ba7STejun Heo  * work items on @wq can queue further work items on it.  @wq is flushed
27399c5a2ba7STejun Heo  * repeatedly until it becomes empty.  The number of flushing is detemined
27409c5a2ba7STejun Heo  * by the depth of chaining and should be relatively short.  Whine if it
27419c5a2ba7STejun Heo  * takes too long.
27429c5a2ba7STejun Heo  */
27439c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq)
27449c5a2ba7STejun Heo {
27459c5a2ba7STejun Heo 	unsigned int flush_cnt = 0;
274649e3cf44STejun Heo 	struct pool_workqueue *pwq;
27479c5a2ba7STejun Heo 
27489c5a2ba7STejun Heo 	/*
27499c5a2ba7STejun Heo 	 * __queue_work() needs to test whether there are drainers, is much
27509c5a2ba7STejun Heo 	 * hotter than drain_workqueue() and already looks at @wq->flags.
27519c5a2ba7STejun Heo 	 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
27529c5a2ba7STejun Heo 	 */
2753e98d5b16STejun Heo 	spin_lock_irq(&workqueue_lock);
27549c5a2ba7STejun Heo 	if (!wq->nr_drainers++)
27559c5a2ba7STejun Heo 		wq->flags |= WQ_DRAINING;
2756e98d5b16STejun Heo 	spin_unlock_irq(&workqueue_lock);
27579c5a2ba7STejun Heo reflush:
27589c5a2ba7STejun Heo 	flush_workqueue(wq);
27599c5a2ba7STejun Heo 
276076af4d93STejun Heo 	local_irq_disable();
276176af4d93STejun Heo 
276249e3cf44STejun Heo 	for_each_pwq(pwq, wq) {
2763fa2563e4SThomas Tuttle 		bool drained;
27649c5a2ba7STejun Heo 
276576af4d93STejun Heo 		spin_lock(&pwq->pool->lock);
2766112202d9STejun Heo 		drained = !pwq->nr_active && list_empty(&pwq->delayed_works);
276776af4d93STejun Heo 		spin_unlock(&pwq->pool->lock);
2768fa2563e4SThomas Tuttle 
2769fa2563e4SThomas Tuttle 		if (drained)
27709c5a2ba7STejun Heo 			continue;
27719c5a2ba7STejun Heo 
27729c5a2ba7STejun Heo 		if (++flush_cnt == 10 ||
27739c5a2ba7STejun Heo 		    (flush_cnt % 100 == 0 && flush_cnt <= 1000))
2774044c782cSValentin Ilie 			pr_warn("workqueue %s: flush on destruction isn't complete after %u tries\n",
27759c5a2ba7STejun Heo 				wq->name, flush_cnt);
277676af4d93STejun Heo 
277776af4d93STejun Heo 		local_irq_enable();
27789c5a2ba7STejun Heo 		goto reflush;
27799c5a2ba7STejun Heo 	}
27809c5a2ba7STejun Heo 
278176af4d93STejun Heo 	spin_lock(&workqueue_lock);
27829c5a2ba7STejun Heo 	if (!--wq->nr_drainers)
27839c5a2ba7STejun Heo 		wq->flags &= ~WQ_DRAINING;
278476af4d93STejun Heo 	spin_unlock(&workqueue_lock);
278576af4d93STejun Heo 
278676af4d93STejun Heo 	local_irq_enable();
27879c5a2ba7STejun Heo }
27889c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue);
27899c5a2ba7STejun Heo 
2790606a5020STejun Heo static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
2791baf59022STejun Heo {
2792baf59022STejun Heo 	struct worker *worker = NULL;
2793c9e7cf27STejun Heo 	struct worker_pool *pool;
2794112202d9STejun Heo 	struct pool_workqueue *pwq;
2795baf59022STejun Heo 
2796baf59022STejun Heo 	might_sleep();
2797baf59022STejun Heo 
2798fa1b54e6STejun Heo 	local_irq_disable();
2799fa1b54e6STejun Heo 	pool = get_work_pool(work);
2800fa1b54e6STejun Heo 	if (!pool) {
2801fa1b54e6STejun Heo 		local_irq_enable();
2802fa1b54e6STejun Heo 		return false;
2803fa1b54e6STejun Heo 	}
2804fa1b54e6STejun Heo 
2805fa1b54e6STejun Heo 	spin_lock(&pool->lock);
28060b3dae68SLai Jiangshan 	/* see the comment in try_to_grab_pending() with the same code */
2807112202d9STejun Heo 	pwq = get_work_pwq(work);
2808112202d9STejun Heo 	if (pwq) {
2809112202d9STejun Heo 		if (unlikely(pwq->pool != pool))
2810baf59022STejun Heo 			goto already_gone;
2811606a5020STejun Heo 	} else {
2812c9e7cf27STejun Heo 		worker = find_worker_executing_work(pool, work);
2813baf59022STejun Heo 		if (!worker)
2814baf59022STejun Heo 			goto already_gone;
2815112202d9STejun Heo 		pwq = worker->current_pwq;
2816606a5020STejun Heo 	}
2817baf59022STejun Heo 
2818112202d9STejun Heo 	insert_wq_barrier(pwq, barr, work, worker);
2819d565ed63STejun Heo 	spin_unlock_irq(&pool->lock);
2820baf59022STejun Heo 
2821e159489bSTejun Heo 	/*
2822e159489bSTejun Heo 	 * If @max_active is 1 or rescuer is in use, flushing another work
2823e159489bSTejun Heo 	 * item on the same workqueue may lead to deadlock.  Make sure the
2824e159489bSTejun Heo 	 * flusher is not running on the same workqueue by verifying write
2825e159489bSTejun Heo 	 * access.
2826e159489bSTejun Heo 	 */
2827112202d9STejun Heo 	if (pwq->wq->saved_max_active == 1 || pwq->wq->flags & WQ_RESCUER)
2828112202d9STejun Heo 		lock_map_acquire(&pwq->wq->lockdep_map);
2829e159489bSTejun Heo 	else
2830112202d9STejun Heo 		lock_map_acquire_read(&pwq->wq->lockdep_map);
2831112202d9STejun Heo 	lock_map_release(&pwq->wq->lockdep_map);
2832e159489bSTejun Heo 
2833baf59022STejun Heo 	return true;
2834baf59022STejun Heo already_gone:
2835d565ed63STejun Heo 	spin_unlock_irq(&pool->lock);
2836baf59022STejun Heo 	return false;
2837baf59022STejun Heo }
2838baf59022STejun Heo 
2839db700897SOleg Nesterov /**
2840401a8d04STejun Heo  * flush_work - wait for a work to finish executing the last queueing instance
2841401a8d04STejun Heo  * @work: the work to flush
2842db700897SOleg Nesterov  *
2843606a5020STejun Heo  * Wait until @work has finished execution.  @work is guaranteed to be idle
2844606a5020STejun Heo  * on return if it hasn't been requeued since flush started.
2845401a8d04STejun Heo  *
2846401a8d04STejun Heo  * RETURNS:
2847401a8d04STejun Heo  * %true if flush_work() waited for the work to finish execution,
2848401a8d04STejun Heo  * %false if it was already idle.
2849db700897SOleg Nesterov  */
2850401a8d04STejun Heo bool flush_work(struct work_struct *work)
2851db700897SOleg Nesterov {
2852db700897SOleg Nesterov 	struct wq_barrier barr;
2853db700897SOleg Nesterov 
28540976dfc1SStephen Boyd 	lock_map_acquire(&work->lockdep_map);
28550976dfc1SStephen Boyd 	lock_map_release(&work->lockdep_map);
28560976dfc1SStephen Boyd 
2857606a5020STejun Heo 	if (start_flush_work(work, &barr)) {
2858db700897SOleg Nesterov 		wait_for_completion(&barr.done);
2859dc186ad7SThomas Gleixner 		destroy_work_on_stack(&barr.work);
2860401a8d04STejun Heo 		return true;
2861606a5020STejun Heo 	} else {
2862401a8d04STejun Heo 		return false;
2863db700897SOleg Nesterov 	}
2864606a5020STejun Heo }
2865db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work);
2866db700897SOleg Nesterov 
286736e227d2STejun Heo static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
2868401a8d04STejun Heo {
2869bbb68dfaSTejun Heo 	unsigned long flags;
28701f1f642eSOleg Nesterov 	int ret;
28711f1f642eSOleg Nesterov 
28721f1f642eSOleg Nesterov 	do {
2873bbb68dfaSTejun Heo 		ret = try_to_grab_pending(work, is_dwork, &flags);
2874bbb68dfaSTejun Heo 		/*
2875bbb68dfaSTejun Heo 		 * If someone else is canceling, wait for the same event it
2876bbb68dfaSTejun Heo 		 * would be waiting for before retrying.
2877bbb68dfaSTejun Heo 		 */
2878bbb68dfaSTejun Heo 		if (unlikely(ret == -ENOENT))
2879606a5020STejun Heo 			flush_work(work);
28801f1f642eSOleg Nesterov 	} while (unlikely(ret < 0));
28811f1f642eSOleg Nesterov 
2882bbb68dfaSTejun Heo 	/* tell other tasks trying to grab @work to back off */
2883bbb68dfaSTejun Heo 	mark_work_canceling(work);
2884bbb68dfaSTejun Heo 	local_irq_restore(flags);
2885bbb68dfaSTejun Heo 
2886606a5020STejun Heo 	flush_work(work);
28877a22ad75STejun Heo 	clear_work_data(work);
28881f1f642eSOleg Nesterov 	return ret;
28891f1f642eSOleg Nesterov }
28901f1f642eSOleg Nesterov 
28916e84d644SOleg Nesterov /**
2892401a8d04STejun Heo  * cancel_work_sync - cancel a work and wait for it to finish
2893401a8d04STejun Heo  * @work: the work to cancel
28946e84d644SOleg Nesterov  *
2895401a8d04STejun Heo  * Cancel @work and wait for its execution to finish.  This function
2896401a8d04STejun Heo  * can be used even if the work re-queues itself or migrates to
2897401a8d04STejun Heo  * another workqueue.  On return from this function, @work is
2898401a8d04STejun Heo  * guaranteed to be not pending or executing on any CPU.
28991f1f642eSOleg Nesterov  *
2900401a8d04STejun Heo  * cancel_work_sync(&delayed_work->work) must not be used for
2901401a8d04STejun Heo  * delayed_work's.  Use cancel_delayed_work_sync() instead.
29026e84d644SOleg Nesterov  *
2903401a8d04STejun Heo  * The caller must ensure that the workqueue on which @work was last
29046e84d644SOleg Nesterov  * queued can't be destroyed before this function returns.
2905401a8d04STejun Heo  *
2906401a8d04STejun Heo  * RETURNS:
2907401a8d04STejun Heo  * %true if @work was pending, %false otherwise.
29086e84d644SOleg Nesterov  */
2909401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work)
29106e84d644SOleg Nesterov {
291136e227d2STejun Heo 	return __cancel_work_timer(work, false);
2912b89deed3SOleg Nesterov }
291328e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync);
2914b89deed3SOleg Nesterov 
29156e84d644SOleg Nesterov /**
2916401a8d04STejun Heo  * flush_delayed_work - wait for a dwork to finish executing the last queueing
2917401a8d04STejun Heo  * @dwork: the delayed work to flush
29186e84d644SOleg Nesterov  *
2919401a8d04STejun Heo  * Delayed timer is cancelled and the pending work is queued for
2920401a8d04STejun Heo  * immediate execution.  Like flush_work(), this function only
2921401a8d04STejun Heo  * considers the last queueing instance of @dwork.
29221f1f642eSOleg Nesterov  *
2923401a8d04STejun Heo  * RETURNS:
2924401a8d04STejun Heo  * %true if flush_work() waited for the work to finish execution,
2925401a8d04STejun Heo  * %false if it was already idle.
29266e84d644SOleg Nesterov  */
2927401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork)
2928401a8d04STejun Heo {
29298930cabaSTejun Heo 	local_irq_disable();
2930401a8d04STejun Heo 	if (del_timer_sync(&dwork->timer))
293160c057bcSLai Jiangshan 		__queue_work(dwork->cpu, dwork->wq, &dwork->work);
29328930cabaSTejun Heo 	local_irq_enable();
2933401a8d04STejun Heo 	return flush_work(&dwork->work);
2934401a8d04STejun Heo }
2935401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work);
2936401a8d04STejun Heo 
2937401a8d04STejun Heo /**
293857b30ae7STejun Heo  * cancel_delayed_work - cancel a delayed work
293957b30ae7STejun Heo  * @dwork: delayed_work to cancel
294009383498STejun Heo  *
294157b30ae7STejun Heo  * Kill off a pending delayed_work.  Returns %true if @dwork was pending
294257b30ae7STejun Heo  * and canceled; %false if wasn't pending.  Note that the work callback
294357b30ae7STejun Heo  * function may still be running on return, unless it returns %true and the
294457b30ae7STejun Heo  * work doesn't re-arm itself.  Explicitly flush or use
294557b30ae7STejun Heo  * cancel_delayed_work_sync() to wait on it.
294609383498STejun Heo  *
294757b30ae7STejun Heo  * This function is safe to call from any context including IRQ handler.
294809383498STejun Heo  */
294957b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork)
295009383498STejun Heo {
295157b30ae7STejun Heo 	unsigned long flags;
295257b30ae7STejun Heo 	int ret;
295357b30ae7STejun Heo 
295457b30ae7STejun Heo 	do {
295557b30ae7STejun Heo 		ret = try_to_grab_pending(&dwork->work, true, &flags);
295657b30ae7STejun Heo 	} while (unlikely(ret == -EAGAIN));
295757b30ae7STejun Heo 
295857b30ae7STejun Heo 	if (unlikely(ret < 0))
295957b30ae7STejun Heo 		return false;
296057b30ae7STejun Heo 
29617c3eed5cSTejun Heo 	set_work_pool_and_clear_pending(&dwork->work,
29627c3eed5cSTejun Heo 					get_work_pool_id(&dwork->work));
296357b30ae7STejun Heo 	local_irq_restore(flags);
2964c0158ca6SDan Magenheimer 	return ret;
296509383498STejun Heo }
296657b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work);
296709383498STejun Heo 
296809383498STejun Heo /**
2969401a8d04STejun Heo  * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2970401a8d04STejun Heo  * @dwork: the delayed work cancel
2971401a8d04STejun Heo  *
2972401a8d04STejun Heo  * This is cancel_work_sync() for delayed works.
2973401a8d04STejun Heo  *
2974401a8d04STejun Heo  * RETURNS:
2975401a8d04STejun Heo  * %true if @dwork was pending, %false otherwise.
2976401a8d04STejun Heo  */
2977401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork)
29786e84d644SOleg Nesterov {
297936e227d2STejun Heo 	return __cancel_work_timer(&dwork->work, true);
29806e84d644SOleg Nesterov }
2981f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync);
29821da177e4SLinus Torvalds 
29830fcb78c2SRolf Eike Beer /**
2984c1a220e7SZhang Rui  * schedule_work_on - put work task on a specific cpu
2985c1a220e7SZhang Rui  * @cpu: cpu to put the work task on
2986c1a220e7SZhang Rui  * @work: job to be done
2987c1a220e7SZhang Rui  *
2988c1a220e7SZhang Rui  * This puts a job on a specific cpu
2989c1a220e7SZhang Rui  */
2990d4283e93STejun Heo bool schedule_work_on(int cpu, struct work_struct *work)
2991c1a220e7SZhang Rui {
2992d320c038STejun Heo 	return queue_work_on(cpu, system_wq, work);
2993c1a220e7SZhang Rui }
2994c1a220e7SZhang Rui EXPORT_SYMBOL(schedule_work_on);
2995c1a220e7SZhang Rui 
29960fcb78c2SRolf Eike Beer /**
2997ae90dd5dSDave Jones  * schedule_work - put work task in global workqueue
29981da177e4SLinus Torvalds  * @work: job to be done
29991da177e4SLinus Torvalds  *
3000d4283e93STejun Heo  * Returns %false if @work was already on the kernel-global workqueue and
3001d4283e93STejun Heo  * %true otherwise.
300252bad64dSDavid Howells  *
30030fcb78c2SRolf Eike Beer  * This puts a job in the kernel-global workqueue if it was not already
30040fcb78c2SRolf Eike Beer  * queued and leaves it in the same position on the kernel-global
30050fcb78c2SRolf Eike Beer  * workqueue otherwise.
30061da177e4SLinus Torvalds  */
3007d4283e93STejun Heo bool schedule_work(struct work_struct *work)
30081da177e4SLinus Torvalds {
30090fcb78c2SRolf Eike Beer 	return queue_work(system_wq, work);
30101da177e4SLinus Torvalds }
30110fcb78c2SRolf Eike Beer EXPORT_SYMBOL(schedule_work);
30121da177e4SLinus Torvalds 
30130fcb78c2SRolf Eike Beer /**
30140fcb78c2SRolf Eike Beer  * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
30150fcb78c2SRolf Eike Beer  * @cpu: cpu to use
30160fcb78c2SRolf Eike Beer  * @dwork: job to be done
30170fcb78c2SRolf Eike Beer  * @delay: number of jiffies to wait
30180fcb78c2SRolf Eike Beer  *
30190fcb78c2SRolf Eike Beer  * After waiting for a given time this puts a job in the kernel-global
30200fcb78c2SRolf Eike Beer  * workqueue on the specified CPU.
30210fcb78c2SRolf Eike Beer  */
3022d4283e93STejun Heo bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
3023d4283e93STejun Heo 			      unsigned long delay)
30241da177e4SLinus Torvalds {
3025d320c038STejun Heo 	return queue_delayed_work_on(cpu, system_wq, dwork, delay);
30261da177e4SLinus Torvalds }
3027ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work_on);
30281da177e4SLinus Torvalds 
3029b6136773SAndrew Morton /**
30300a13c00eSTejun Heo  * schedule_delayed_work - put work task in global workqueue after delay
30310a13c00eSTejun Heo  * @dwork: job to be done
30320a13c00eSTejun Heo  * @delay: number of jiffies to wait or 0 for immediate execution
30330a13c00eSTejun Heo  *
30340a13c00eSTejun Heo  * After waiting for a given time this puts a job in the kernel-global
30350a13c00eSTejun Heo  * workqueue.
30360a13c00eSTejun Heo  */
3037d4283e93STejun Heo bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay)
30380a13c00eSTejun Heo {
30390a13c00eSTejun Heo 	return queue_delayed_work(system_wq, dwork, delay);
30400a13c00eSTejun Heo }
30410a13c00eSTejun Heo EXPORT_SYMBOL(schedule_delayed_work);
30420a13c00eSTejun Heo 
30430a13c00eSTejun Heo /**
304431ddd871STejun Heo  * schedule_on_each_cpu - execute a function synchronously on each online CPU
3045b6136773SAndrew Morton  * @func: the function to call
3046b6136773SAndrew Morton  *
304731ddd871STejun Heo  * schedule_on_each_cpu() executes @func on each online CPU using the
304831ddd871STejun Heo  * system workqueue and blocks until all CPUs have completed.
3049b6136773SAndrew Morton  * schedule_on_each_cpu() is very slow.
305031ddd871STejun Heo  *
305131ddd871STejun Heo  * RETURNS:
305231ddd871STejun Heo  * 0 on success, -errno on failure.
3053b6136773SAndrew Morton  */
305465f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func)
305515316ba8SChristoph Lameter {
305615316ba8SChristoph Lameter 	int cpu;
305738f51568SNamhyung Kim 	struct work_struct __percpu *works;
305815316ba8SChristoph Lameter 
3059b6136773SAndrew Morton 	works = alloc_percpu(struct work_struct);
3060b6136773SAndrew Morton 	if (!works)
306115316ba8SChristoph Lameter 		return -ENOMEM;
3062b6136773SAndrew Morton 
306395402b38SGautham R Shenoy 	get_online_cpus();
306493981800STejun Heo 
306515316ba8SChristoph Lameter 	for_each_online_cpu(cpu) {
30669bfb1839SIngo Molnar 		struct work_struct *work = per_cpu_ptr(works, cpu);
30679bfb1839SIngo Molnar 
30689bfb1839SIngo Molnar 		INIT_WORK(work, func);
30698de6d308SOleg Nesterov 		schedule_work_on(cpu, work);
307015316ba8SChristoph Lameter 	}
307193981800STejun Heo 
307293981800STejun Heo 	for_each_online_cpu(cpu)
30738616a89aSOleg Nesterov 		flush_work(per_cpu_ptr(works, cpu));
307493981800STejun Heo 
307595402b38SGautham R Shenoy 	put_online_cpus();
3076b6136773SAndrew Morton 	free_percpu(works);
307715316ba8SChristoph Lameter 	return 0;
307815316ba8SChristoph Lameter }
307915316ba8SChristoph Lameter 
3080eef6a7d5SAlan Stern /**
3081eef6a7d5SAlan Stern  * flush_scheduled_work - ensure that any scheduled work has run to completion.
3082eef6a7d5SAlan Stern  *
3083eef6a7d5SAlan Stern  * Forces execution of the kernel-global workqueue and blocks until its
3084eef6a7d5SAlan Stern  * completion.
3085eef6a7d5SAlan Stern  *
3086eef6a7d5SAlan Stern  * Think twice before calling this function!  It's very easy to get into
3087eef6a7d5SAlan Stern  * trouble if you don't take great care.  Either of the following situations
3088eef6a7d5SAlan Stern  * will lead to deadlock:
3089eef6a7d5SAlan Stern  *
3090eef6a7d5SAlan Stern  *	One of the work items currently on the workqueue needs to acquire
3091eef6a7d5SAlan Stern  *	a lock held by your code or its caller.
3092eef6a7d5SAlan Stern  *
3093eef6a7d5SAlan Stern  *	Your code is running in the context of a work routine.
3094eef6a7d5SAlan Stern  *
3095eef6a7d5SAlan Stern  * They will be detected by lockdep when they occur, but the first might not
3096eef6a7d5SAlan Stern  * occur very often.  It depends on what work items are on the workqueue and
3097eef6a7d5SAlan Stern  * what locks they need, which you have no control over.
3098eef6a7d5SAlan Stern  *
3099eef6a7d5SAlan Stern  * In most situations flushing the entire workqueue is overkill; you merely
3100eef6a7d5SAlan Stern  * need to know that a particular work item isn't queued and isn't running.
3101eef6a7d5SAlan Stern  * In such cases you should use cancel_delayed_work_sync() or
3102eef6a7d5SAlan Stern  * cancel_work_sync() instead.
3103eef6a7d5SAlan Stern  */
31041da177e4SLinus Torvalds void flush_scheduled_work(void)
31051da177e4SLinus Torvalds {
3106d320c038STejun Heo 	flush_workqueue(system_wq);
31071da177e4SLinus Torvalds }
3108ae90dd5dSDave Jones EXPORT_SYMBOL(flush_scheduled_work);
31091da177e4SLinus Torvalds 
31101da177e4SLinus Torvalds /**
31111fa44ecaSJames Bottomley  * execute_in_process_context - reliably execute the routine with user context
31121fa44ecaSJames Bottomley  * @fn:		the function to execute
31131fa44ecaSJames Bottomley  * @ew:		guaranteed storage for the execute work structure (must
31141fa44ecaSJames Bottomley  *		be available when the work executes)
31151fa44ecaSJames Bottomley  *
31161fa44ecaSJames Bottomley  * Executes the function immediately if process context is available,
31171fa44ecaSJames Bottomley  * otherwise schedules the function for delayed execution.
31181fa44ecaSJames Bottomley  *
31191fa44ecaSJames Bottomley  * Returns:	0 - function was executed
31201fa44ecaSJames Bottomley  *		1 - function was scheduled for execution
31211fa44ecaSJames Bottomley  */
312265f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew)
31231fa44ecaSJames Bottomley {
31241fa44ecaSJames Bottomley 	if (!in_interrupt()) {
312565f27f38SDavid Howells 		fn(&ew->work);
31261fa44ecaSJames Bottomley 		return 0;
31271fa44ecaSJames Bottomley 	}
31281fa44ecaSJames Bottomley 
312965f27f38SDavid Howells 	INIT_WORK(&ew->work, fn);
31301fa44ecaSJames Bottomley 	schedule_work(&ew->work);
31311fa44ecaSJames Bottomley 
31321fa44ecaSJames Bottomley 	return 1;
31331fa44ecaSJames Bottomley }
31341fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context);
31351fa44ecaSJames Bottomley 
31361da177e4SLinus Torvalds int keventd_up(void)
31371da177e4SLinus Torvalds {
3138d320c038STejun Heo 	return system_wq != NULL;
31391da177e4SLinus Torvalds }
31401da177e4SLinus Torvalds 
31417a4e344cSTejun Heo /**
31427a4e344cSTejun Heo  * free_workqueue_attrs - free a workqueue_attrs
31437a4e344cSTejun Heo  * @attrs: workqueue_attrs to free
31447a4e344cSTejun Heo  *
31457a4e344cSTejun Heo  * Undo alloc_workqueue_attrs().
31467a4e344cSTejun Heo  */
31477a4e344cSTejun Heo void free_workqueue_attrs(struct workqueue_attrs *attrs)
31487a4e344cSTejun Heo {
31497a4e344cSTejun Heo 	if (attrs) {
31507a4e344cSTejun Heo 		free_cpumask_var(attrs->cpumask);
31517a4e344cSTejun Heo 		kfree(attrs);
31527a4e344cSTejun Heo 	}
31537a4e344cSTejun Heo }
31547a4e344cSTejun Heo 
31557a4e344cSTejun Heo /**
31567a4e344cSTejun Heo  * alloc_workqueue_attrs - allocate a workqueue_attrs
31577a4e344cSTejun Heo  * @gfp_mask: allocation mask to use
31587a4e344cSTejun Heo  *
31597a4e344cSTejun Heo  * Allocate a new workqueue_attrs, initialize with default settings and
31607a4e344cSTejun Heo  * return it.  Returns NULL on failure.
31617a4e344cSTejun Heo  */
31627a4e344cSTejun Heo struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask)
31637a4e344cSTejun Heo {
31647a4e344cSTejun Heo 	struct workqueue_attrs *attrs;
31657a4e344cSTejun Heo 
31667a4e344cSTejun Heo 	attrs = kzalloc(sizeof(*attrs), gfp_mask);
31677a4e344cSTejun Heo 	if (!attrs)
31687a4e344cSTejun Heo 		goto fail;
31697a4e344cSTejun Heo 	if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask))
31707a4e344cSTejun Heo 		goto fail;
31717a4e344cSTejun Heo 
31727a4e344cSTejun Heo 	cpumask_setall(attrs->cpumask);
31737a4e344cSTejun Heo 	return attrs;
31747a4e344cSTejun Heo fail:
31757a4e344cSTejun Heo 	free_workqueue_attrs(attrs);
31767a4e344cSTejun Heo 	return NULL;
31777a4e344cSTejun Heo }
31787a4e344cSTejun Heo 
3179*29c91e99STejun Heo static void copy_workqueue_attrs(struct workqueue_attrs *to,
3180*29c91e99STejun Heo 				 const struct workqueue_attrs *from)
3181*29c91e99STejun Heo {
3182*29c91e99STejun Heo 	to->nice = from->nice;
3183*29c91e99STejun Heo 	cpumask_copy(to->cpumask, from->cpumask);
3184*29c91e99STejun Heo }
3185*29c91e99STejun Heo 
3186*29c91e99STejun Heo /*
3187*29c91e99STejun Heo  * Hacky implementation of jhash of bitmaps which only considers the
3188*29c91e99STejun Heo  * specified number of bits.  We probably want a proper implementation in
3189*29c91e99STejun Heo  * include/linux/jhash.h.
3190*29c91e99STejun Heo  */
3191*29c91e99STejun Heo static u32 jhash_bitmap(const unsigned long *bitmap, int bits, u32 hash)
3192*29c91e99STejun Heo {
3193*29c91e99STejun Heo 	int nr_longs = bits / BITS_PER_LONG;
3194*29c91e99STejun Heo 	int nr_leftover = bits % BITS_PER_LONG;
3195*29c91e99STejun Heo 	unsigned long leftover = 0;
3196*29c91e99STejun Heo 
3197*29c91e99STejun Heo 	if (nr_longs)
3198*29c91e99STejun Heo 		hash = jhash(bitmap, nr_longs * sizeof(long), hash);
3199*29c91e99STejun Heo 	if (nr_leftover) {
3200*29c91e99STejun Heo 		bitmap_copy(&leftover, bitmap + nr_longs, nr_leftover);
3201*29c91e99STejun Heo 		hash = jhash(&leftover, sizeof(long), hash);
3202*29c91e99STejun Heo 	}
3203*29c91e99STejun Heo 	return hash;
3204*29c91e99STejun Heo }
3205*29c91e99STejun Heo 
3206*29c91e99STejun Heo /* hash value of the content of @attr */
3207*29c91e99STejun Heo static u32 wqattrs_hash(const struct workqueue_attrs *attrs)
3208*29c91e99STejun Heo {
3209*29c91e99STejun Heo 	u32 hash = 0;
3210*29c91e99STejun Heo 
3211*29c91e99STejun Heo 	hash = jhash_1word(attrs->nice, hash);
3212*29c91e99STejun Heo 	hash = jhash_bitmap(cpumask_bits(attrs->cpumask), nr_cpu_ids, hash);
3213*29c91e99STejun Heo 	return hash;
3214*29c91e99STejun Heo }
3215*29c91e99STejun Heo 
3216*29c91e99STejun Heo /* content equality test */
3217*29c91e99STejun Heo static bool wqattrs_equal(const struct workqueue_attrs *a,
3218*29c91e99STejun Heo 			  const struct workqueue_attrs *b)
3219*29c91e99STejun Heo {
3220*29c91e99STejun Heo 	if (a->nice != b->nice)
3221*29c91e99STejun Heo 		return false;
3222*29c91e99STejun Heo 	if (!cpumask_equal(a->cpumask, b->cpumask))
3223*29c91e99STejun Heo 		return false;
3224*29c91e99STejun Heo 	return true;
3225*29c91e99STejun Heo }
3226*29c91e99STejun Heo 
32277a4e344cSTejun Heo /**
32287a4e344cSTejun Heo  * init_worker_pool - initialize a newly zalloc'd worker_pool
32297a4e344cSTejun Heo  * @pool: worker_pool to initialize
32307a4e344cSTejun Heo  *
32317a4e344cSTejun Heo  * Initiailize a newly zalloc'd @pool.  It also allocates @pool->attrs.
3232*29c91e99STejun Heo  * Returns 0 on success, -errno on failure.  Even on failure, all fields
3233*29c91e99STejun Heo  * inside @pool proper are initialized and put_unbound_pool() can be called
3234*29c91e99STejun Heo  * on @pool safely to release it.
32357a4e344cSTejun Heo  */
32367a4e344cSTejun Heo static int init_worker_pool(struct worker_pool *pool)
32374e1a1f9aSTejun Heo {
32384e1a1f9aSTejun Heo 	spin_lock_init(&pool->lock);
3239*29c91e99STejun Heo 	pool->id = -1;
3240*29c91e99STejun Heo 	pool->cpu = -1;
32414e1a1f9aSTejun Heo 	pool->flags |= POOL_DISASSOCIATED;
32424e1a1f9aSTejun Heo 	INIT_LIST_HEAD(&pool->worklist);
32434e1a1f9aSTejun Heo 	INIT_LIST_HEAD(&pool->idle_list);
32444e1a1f9aSTejun Heo 	hash_init(pool->busy_hash);
32454e1a1f9aSTejun Heo 
32464e1a1f9aSTejun Heo 	init_timer_deferrable(&pool->idle_timer);
32474e1a1f9aSTejun Heo 	pool->idle_timer.function = idle_worker_timeout;
32484e1a1f9aSTejun Heo 	pool->idle_timer.data = (unsigned long)pool;
32494e1a1f9aSTejun Heo 
32504e1a1f9aSTejun Heo 	setup_timer(&pool->mayday_timer, pool_mayday_timeout,
32514e1a1f9aSTejun Heo 		    (unsigned long)pool);
32524e1a1f9aSTejun Heo 
32534e1a1f9aSTejun Heo 	mutex_init(&pool->manager_arb);
32544e1a1f9aSTejun Heo 	mutex_init(&pool->assoc_mutex);
32554e1a1f9aSTejun Heo 	ida_init(&pool->worker_ida);
32567a4e344cSTejun Heo 
3257*29c91e99STejun Heo 	INIT_HLIST_NODE(&pool->hash_node);
3258*29c91e99STejun Heo 	pool->refcnt = 1;
3259*29c91e99STejun Heo 
3260*29c91e99STejun Heo 	/* shouldn't fail above this point */
32617a4e344cSTejun Heo 	pool->attrs = alloc_workqueue_attrs(GFP_KERNEL);
32627a4e344cSTejun Heo 	if (!pool->attrs)
32637a4e344cSTejun Heo 		return -ENOMEM;
32647a4e344cSTejun Heo 	return 0;
32654e1a1f9aSTejun Heo }
32664e1a1f9aSTejun Heo 
3267*29c91e99STejun Heo static void rcu_free_pool(struct rcu_head *rcu)
3268*29c91e99STejun Heo {
3269*29c91e99STejun Heo 	struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu);
3270*29c91e99STejun Heo 
3271*29c91e99STejun Heo 	ida_destroy(&pool->worker_ida);
3272*29c91e99STejun Heo 	free_workqueue_attrs(pool->attrs);
3273*29c91e99STejun Heo 	kfree(pool);
3274*29c91e99STejun Heo }
3275*29c91e99STejun Heo 
3276*29c91e99STejun Heo /**
3277*29c91e99STejun Heo  * put_unbound_pool - put a worker_pool
3278*29c91e99STejun Heo  * @pool: worker_pool to put
3279*29c91e99STejun Heo  *
3280*29c91e99STejun Heo  * Put @pool.  If its refcnt reaches zero, it gets destroyed in sched-RCU
3281*29c91e99STejun Heo  * safe manner.
3282*29c91e99STejun Heo  */
3283*29c91e99STejun Heo static void put_unbound_pool(struct worker_pool *pool)
3284*29c91e99STejun Heo {
3285*29c91e99STejun Heo 	struct worker *worker;
3286*29c91e99STejun Heo 
3287*29c91e99STejun Heo 	spin_lock_irq(&workqueue_lock);
3288*29c91e99STejun Heo 	if (--pool->refcnt) {
3289*29c91e99STejun Heo 		spin_unlock_irq(&workqueue_lock);
3290*29c91e99STejun Heo 		return;
3291*29c91e99STejun Heo 	}
3292*29c91e99STejun Heo 
3293*29c91e99STejun Heo 	/* sanity checks */
3294*29c91e99STejun Heo 	if (WARN_ON(!(pool->flags & POOL_DISASSOCIATED)) ||
3295*29c91e99STejun Heo 	    WARN_ON(!list_empty(&pool->worklist))) {
3296*29c91e99STejun Heo 		spin_unlock_irq(&workqueue_lock);
3297*29c91e99STejun Heo 		return;
3298*29c91e99STejun Heo 	}
3299*29c91e99STejun Heo 
3300*29c91e99STejun Heo 	/* release id and unhash */
3301*29c91e99STejun Heo 	if (pool->id >= 0)
3302*29c91e99STejun Heo 		idr_remove(&worker_pool_idr, pool->id);
3303*29c91e99STejun Heo 	hash_del(&pool->hash_node);
3304*29c91e99STejun Heo 
3305*29c91e99STejun Heo 	spin_unlock_irq(&workqueue_lock);
3306*29c91e99STejun Heo 
3307*29c91e99STejun Heo 	/* lock out manager and destroy all workers */
3308*29c91e99STejun Heo 	mutex_lock(&pool->manager_arb);
3309*29c91e99STejun Heo 	spin_lock_irq(&pool->lock);
3310*29c91e99STejun Heo 
3311*29c91e99STejun Heo 	while ((worker = first_worker(pool)))
3312*29c91e99STejun Heo 		destroy_worker(worker);
3313*29c91e99STejun Heo 	WARN_ON(pool->nr_workers || pool->nr_idle);
3314*29c91e99STejun Heo 
3315*29c91e99STejun Heo 	spin_unlock_irq(&pool->lock);
3316*29c91e99STejun Heo 	mutex_unlock(&pool->manager_arb);
3317*29c91e99STejun Heo 
3318*29c91e99STejun Heo 	/* shut down the timers */
3319*29c91e99STejun Heo 	del_timer_sync(&pool->idle_timer);
3320*29c91e99STejun Heo 	del_timer_sync(&pool->mayday_timer);
3321*29c91e99STejun Heo 
3322*29c91e99STejun Heo 	/* sched-RCU protected to allow dereferences from get_work_pool() */
3323*29c91e99STejun Heo 	call_rcu_sched(&pool->rcu, rcu_free_pool);
3324*29c91e99STejun Heo }
3325*29c91e99STejun Heo 
3326*29c91e99STejun Heo /**
3327*29c91e99STejun Heo  * get_unbound_pool - get a worker_pool with the specified attributes
3328*29c91e99STejun Heo  * @attrs: the attributes of the worker_pool to get
3329*29c91e99STejun Heo  *
3330*29c91e99STejun Heo  * Obtain a worker_pool which has the same attributes as @attrs, bump the
3331*29c91e99STejun Heo  * reference count and return it.  If there already is a matching
3332*29c91e99STejun Heo  * worker_pool, it will be used; otherwise, this function attempts to
3333*29c91e99STejun Heo  * create a new one.  On failure, returns NULL.
3334*29c91e99STejun Heo  */
3335*29c91e99STejun Heo static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
3336*29c91e99STejun Heo {
3337*29c91e99STejun Heo 	static DEFINE_MUTEX(create_mutex);
3338*29c91e99STejun Heo 	u32 hash = wqattrs_hash(attrs);
3339*29c91e99STejun Heo 	struct worker_pool *pool;
3340*29c91e99STejun Heo 	struct worker *worker;
3341*29c91e99STejun Heo 
3342*29c91e99STejun Heo 	mutex_lock(&create_mutex);
3343*29c91e99STejun Heo 
3344*29c91e99STejun Heo 	/* do we already have a matching pool? */
3345*29c91e99STejun Heo 	spin_lock_irq(&workqueue_lock);
3346*29c91e99STejun Heo 	hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) {
3347*29c91e99STejun Heo 		if (wqattrs_equal(pool->attrs, attrs)) {
3348*29c91e99STejun Heo 			pool->refcnt++;
3349*29c91e99STejun Heo 			goto out_unlock;
3350*29c91e99STejun Heo 		}
3351*29c91e99STejun Heo 	}
3352*29c91e99STejun Heo 	spin_unlock_irq(&workqueue_lock);
3353*29c91e99STejun Heo 
3354*29c91e99STejun Heo 	/* nope, create a new one */
3355*29c91e99STejun Heo 	pool = kzalloc(sizeof(*pool), GFP_KERNEL);
3356*29c91e99STejun Heo 	if (!pool || init_worker_pool(pool) < 0)
3357*29c91e99STejun Heo 		goto fail;
3358*29c91e99STejun Heo 
3359*29c91e99STejun Heo 	copy_workqueue_attrs(pool->attrs, attrs);
3360*29c91e99STejun Heo 
3361*29c91e99STejun Heo 	if (worker_pool_assign_id(pool) < 0)
3362*29c91e99STejun Heo 		goto fail;
3363*29c91e99STejun Heo 
3364*29c91e99STejun Heo 	/* create and start the initial worker */
3365*29c91e99STejun Heo 	worker = create_worker(pool);
3366*29c91e99STejun Heo 	if (!worker)
3367*29c91e99STejun Heo 		goto fail;
3368*29c91e99STejun Heo 
3369*29c91e99STejun Heo 	spin_lock_irq(&pool->lock);
3370*29c91e99STejun Heo 	start_worker(worker);
3371*29c91e99STejun Heo 	spin_unlock_irq(&pool->lock);
3372*29c91e99STejun Heo 
3373*29c91e99STejun Heo 	/* install */
3374*29c91e99STejun Heo 	spin_lock_irq(&workqueue_lock);
3375*29c91e99STejun Heo 	hash_add(unbound_pool_hash, &pool->hash_node, hash);
3376*29c91e99STejun Heo out_unlock:
3377*29c91e99STejun Heo 	spin_unlock_irq(&workqueue_lock);
3378*29c91e99STejun Heo 	mutex_unlock(&create_mutex);
3379*29c91e99STejun Heo 	return pool;
3380*29c91e99STejun Heo fail:
3381*29c91e99STejun Heo 	mutex_unlock(&create_mutex);
3382*29c91e99STejun Heo 	if (pool)
3383*29c91e99STejun Heo 		put_unbound_pool(pool);
3384*29c91e99STejun Heo 	return NULL;
3385*29c91e99STejun Heo }
3386*29c91e99STejun Heo 
338730cdf249STejun Heo static int alloc_and_link_pwqs(struct workqueue_struct *wq)
33881da177e4SLinus Torvalds {
338949e3cf44STejun Heo 	bool highpri = wq->flags & WQ_HIGHPRI;
339030cdf249STejun Heo 	int cpu;
3391e1d8aa9fSFrederic Weisbecker 
339230cdf249STejun Heo 	if (!(wq->flags & WQ_UNBOUND)) {
3393420c0ddbSTejun Heo 		wq->cpu_pwqs = alloc_percpu(struct pool_workqueue);
3394420c0ddbSTejun Heo 		if (!wq->cpu_pwqs)
339530cdf249STejun Heo 			return -ENOMEM;
339630cdf249STejun Heo 
339730cdf249STejun Heo 		for_each_possible_cpu(cpu) {
33987fb98ea7STejun Heo 			struct pool_workqueue *pwq =
33997fb98ea7STejun Heo 				per_cpu_ptr(wq->cpu_pwqs, cpu);
340030cdf249STejun Heo 
340149e3cf44STejun Heo 			pwq->pool = get_std_worker_pool(cpu, highpri);
340276af4d93STejun Heo 			list_add_tail_rcu(&pwq->pwqs_node, &wq->pwqs);
340330cdf249STejun Heo 		}
340430cdf249STejun Heo 	} else {
340530cdf249STejun Heo 		struct pool_workqueue *pwq;
340630cdf249STejun Heo 
340730cdf249STejun Heo 		pwq = kmem_cache_zalloc(pwq_cache, GFP_KERNEL);
340830cdf249STejun Heo 		if (!pwq)
340930cdf249STejun Heo 			return -ENOMEM;
341030cdf249STejun Heo 
3411*29c91e99STejun Heo 		pwq->pool = get_unbound_pool(unbound_std_wq_attrs[highpri]);
3412*29c91e99STejun Heo 		if (!pwq->pool) {
3413*29c91e99STejun Heo 			kmem_cache_free(pwq_cache, pwq);
3414*29c91e99STejun Heo 			return -ENOMEM;
3415*29c91e99STejun Heo 		}
3416*29c91e99STejun Heo 
341776af4d93STejun Heo 		list_add_tail_rcu(&pwq->pwqs_node, &wq->pwqs);
341830cdf249STejun Heo 	}
341930cdf249STejun Heo 
342030cdf249STejun Heo 	return 0;
34210f900049STejun Heo }
34220f900049STejun Heo 
3423112202d9STejun Heo static void free_pwqs(struct workqueue_struct *wq)
342406ba38a9SOleg Nesterov {
3425e06ffa1eSLai Jiangshan 	if (!(wq->flags & WQ_UNBOUND))
3426420c0ddbSTejun Heo 		free_percpu(wq->cpu_pwqs);
3427420c0ddbSTejun Heo 	else if (!list_empty(&wq->pwqs))
3428420c0ddbSTejun Heo 		kmem_cache_free(pwq_cache, list_first_entry(&wq->pwqs,
3429420c0ddbSTejun Heo 					struct pool_workqueue, pwqs_node));
343006ba38a9SOleg Nesterov }
343106ba38a9SOleg Nesterov 
3432f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags,
3433f3421797STejun Heo 			       const char *name)
3434b71ab8c2STejun Heo {
3435f3421797STejun Heo 	int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3436f3421797STejun Heo 
3437f3421797STejun Heo 	if (max_active < 1 || max_active > lim)
3438044c782cSValentin Ilie 		pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
3439f3421797STejun Heo 			max_active, name, 1, lim);
3440b71ab8c2STejun Heo 
3441f3421797STejun Heo 	return clamp_val(max_active, 1, lim);
3442b71ab8c2STejun Heo }
3443b71ab8c2STejun Heo 
3444b196be89STejun Heo struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
344597e37d7bSTejun Heo 					       unsigned int flags,
34461e19ffc6STejun Heo 					       int max_active,
3447eb13ba87SJohannes Berg 					       struct lock_class_key *key,
3448b196be89STejun Heo 					       const char *lock_name, ...)
34493af24433SOleg Nesterov {
3450b196be89STejun Heo 	va_list args, args1;
34513af24433SOleg Nesterov 	struct workqueue_struct *wq;
345249e3cf44STejun Heo 	struct pool_workqueue *pwq;
3453b196be89STejun Heo 	size_t namelen;
3454b196be89STejun Heo 
3455b196be89STejun Heo 	/* determine namelen, allocate wq and format name */
3456b196be89STejun Heo 	va_start(args, lock_name);
3457b196be89STejun Heo 	va_copy(args1, args);
3458b196be89STejun Heo 	namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3459b196be89STejun Heo 
3460b196be89STejun Heo 	wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3461b196be89STejun Heo 	if (!wq)
3462b196be89STejun Heo 		goto err;
3463b196be89STejun Heo 
3464b196be89STejun Heo 	vsnprintf(wq->name, namelen, fmt, args1);
3465b196be89STejun Heo 	va_end(args);
3466b196be89STejun Heo 	va_end(args1);
34673af24433SOleg Nesterov 
3468f3421797STejun Heo 	/*
34696370a6adSTejun Heo 	 * Workqueues which may be used during memory reclaim should
34706370a6adSTejun Heo 	 * have a rescuer to guarantee forward progress.
34716370a6adSTejun Heo 	 */
34726370a6adSTejun Heo 	if (flags & WQ_MEM_RECLAIM)
34736370a6adSTejun Heo 		flags |= WQ_RESCUER;
34746370a6adSTejun Heo 
3475d320c038STejun Heo 	max_active = max_active ?: WQ_DFL_ACTIVE;
3476b196be89STejun Heo 	max_active = wq_clamp_max_active(max_active, flags, wq->name);
34773af24433SOleg Nesterov 
3478b196be89STejun Heo 	/* init wq */
347997e37d7bSTejun Heo 	wq->flags = flags;
3480a0a1a5fdSTejun Heo 	wq->saved_max_active = max_active;
348173f53c4aSTejun Heo 	mutex_init(&wq->flush_mutex);
3482112202d9STejun Heo 	atomic_set(&wq->nr_pwqs_to_flush, 0);
348330cdf249STejun Heo 	INIT_LIST_HEAD(&wq->pwqs);
348473f53c4aSTejun Heo 	INIT_LIST_HEAD(&wq->flusher_queue);
348573f53c4aSTejun Heo 	INIT_LIST_HEAD(&wq->flusher_overflow);
3486493a1724STejun Heo 	INIT_LIST_HEAD(&wq->maydays);
34873af24433SOleg Nesterov 
3488eb13ba87SJohannes Berg 	lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
3489cce1a165SOleg Nesterov 	INIT_LIST_HEAD(&wq->list);
34903af24433SOleg Nesterov 
349130cdf249STejun Heo 	if (alloc_and_link_pwqs(wq) < 0)
3492bdbc5dd7STejun Heo 		goto err;
3493bdbc5dd7STejun Heo 
349476af4d93STejun Heo 	local_irq_disable();
349549e3cf44STejun Heo 	for_each_pwq(pwq, wq) {
3496112202d9STejun Heo 		BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
3497112202d9STejun Heo 		pwq->wq = wq;
3498112202d9STejun Heo 		pwq->flush_color = -1;
3499112202d9STejun Heo 		pwq->max_active = max_active;
3500112202d9STejun Heo 		INIT_LIST_HEAD(&pwq->delayed_works);
3501493a1724STejun Heo 		INIT_LIST_HEAD(&pwq->mayday_node);
3502e22bee78STejun Heo 	}
350376af4d93STejun Heo 	local_irq_enable();
35041537663fSTejun Heo 
3505e22bee78STejun Heo 	if (flags & WQ_RESCUER) {
3506e22bee78STejun Heo 		struct worker *rescuer;
3507e22bee78STejun Heo 
3508e22bee78STejun Heo 		wq->rescuer = rescuer = alloc_worker();
3509e22bee78STejun Heo 		if (!rescuer)
3510e22bee78STejun Heo 			goto err;
3511e22bee78STejun Heo 
3512111c225aSTejun Heo 		rescuer->rescue_wq = wq;
3513111c225aSTejun Heo 		rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
3514b196be89STejun Heo 					       wq->name);
3515e22bee78STejun Heo 		if (IS_ERR(rescuer->task))
3516e22bee78STejun Heo 			goto err;
3517e22bee78STejun Heo 
3518e22bee78STejun Heo 		rescuer->task->flags |= PF_THREAD_BOUND;
3519e22bee78STejun Heo 		wake_up_process(rescuer->task);
35203af24433SOleg Nesterov 	}
35211537663fSTejun Heo 
35223af24433SOleg Nesterov 	/*
3523a0a1a5fdSTejun Heo 	 * workqueue_lock protects global freeze state and workqueues
3524a0a1a5fdSTejun Heo 	 * list.  Grab it, set max_active accordingly and add the new
3525a0a1a5fdSTejun Heo 	 * workqueue to workqueues list.
35263af24433SOleg Nesterov 	 */
3527e98d5b16STejun Heo 	spin_lock_irq(&workqueue_lock);
3528a0a1a5fdSTejun Heo 
352958a69cb4STejun Heo 	if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
353049e3cf44STejun Heo 		for_each_pwq(pwq, wq)
353149e3cf44STejun Heo 			pwq->max_active = 0;
3532a0a1a5fdSTejun Heo 
35333af24433SOleg Nesterov 	list_add(&wq->list, &workqueues);
3534a0a1a5fdSTejun Heo 
3535e98d5b16STejun Heo 	spin_unlock_irq(&workqueue_lock);
35363af24433SOleg Nesterov 
35373af24433SOleg Nesterov 	return wq;
35384690c4abSTejun Heo err:
35394690c4abSTejun Heo 	if (wq) {
3540112202d9STejun Heo 		free_pwqs(wq);
3541e22bee78STejun Heo 		kfree(wq->rescuer);
35424690c4abSTejun Heo 		kfree(wq);
35433af24433SOleg Nesterov 	}
35444690c4abSTejun Heo 	return NULL;
35451da177e4SLinus Torvalds }
3546d320c038STejun Heo EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
35471da177e4SLinus Torvalds 
35483af24433SOleg Nesterov /**
35493af24433SOleg Nesterov  * destroy_workqueue - safely terminate a workqueue
35503af24433SOleg Nesterov  * @wq: target workqueue
35513af24433SOleg Nesterov  *
35523af24433SOleg Nesterov  * Safely destroy a workqueue. All work currently pending will be done first.
35533af24433SOleg Nesterov  */
35543af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq)
35553af24433SOleg Nesterov {
355649e3cf44STejun Heo 	struct pool_workqueue *pwq;
35573af24433SOleg Nesterov 
35589c5a2ba7STejun Heo 	/* drain it before proceeding with destruction */
35599c5a2ba7STejun Heo 	drain_workqueue(wq);
3560c8efcc25STejun Heo 
356176af4d93STejun Heo 	spin_lock_irq(&workqueue_lock);
356276af4d93STejun Heo 
35636183c009STejun Heo 	/* sanity checks */
356449e3cf44STejun Heo 	for_each_pwq(pwq, wq) {
35656183c009STejun Heo 		int i;
35666183c009STejun Heo 
356776af4d93STejun Heo 		for (i = 0; i < WORK_NR_COLORS; i++) {
356876af4d93STejun Heo 			if (WARN_ON(pwq->nr_in_flight[i])) {
356976af4d93STejun Heo 				spin_unlock_irq(&workqueue_lock);
35706183c009STejun Heo 				return;
357176af4d93STejun Heo 			}
357276af4d93STejun Heo 		}
357376af4d93STejun Heo 
35746183c009STejun Heo 		if (WARN_ON(pwq->nr_active) ||
357576af4d93STejun Heo 		    WARN_ON(!list_empty(&pwq->delayed_works))) {
357676af4d93STejun Heo 			spin_unlock_irq(&workqueue_lock);
35776183c009STejun Heo 			return;
35786183c009STejun Heo 		}
357976af4d93STejun Heo 	}
35806183c009STejun Heo 
3581a0a1a5fdSTejun Heo 	/*
3582a0a1a5fdSTejun Heo 	 * wq list is used to freeze wq, remove from list after
3583a0a1a5fdSTejun Heo 	 * flushing is complete in case freeze races us.
3584a0a1a5fdSTejun Heo 	 */
35853af24433SOleg Nesterov 	list_del(&wq->list);
358676af4d93STejun Heo 
3587e98d5b16STejun Heo 	spin_unlock_irq(&workqueue_lock);
35883af24433SOleg Nesterov 
3589e22bee78STejun Heo 	if (wq->flags & WQ_RESCUER) {
3590e22bee78STejun Heo 		kthread_stop(wq->rescuer->task);
35918d9df9f0SXiaotian Feng 		kfree(wq->rescuer);
3592e22bee78STejun Heo 	}
3593e22bee78STejun Heo 
3594*29c91e99STejun Heo 	/*
3595*29c91e99STejun Heo 	 * We're the sole accessor of @wq at this point.  Directly access
3596*29c91e99STejun Heo 	 * the first pwq and put its pool.
3597*29c91e99STejun Heo 	 */
3598*29c91e99STejun Heo 	if (wq->flags & WQ_UNBOUND) {
3599*29c91e99STejun Heo 		pwq = list_first_entry(&wq->pwqs, struct pool_workqueue,
3600*29c91e99STejun Heo 				       pwqs_node);
3601*29c91e99STejun Heo 		put_unbound_pool(pwq->pool);
3602*29c91e99STejun Heo 	}
3603112202d9STejun Heo 	free_pwqs(wq);
36043af24433SOleg Nesterov 	kfree(wq);
36053af24433SOleg Nesterov }
36063af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue);
36073af24433SOleg Nesterov 
3608dcd989cbSTejun Heo /**
3609112202d9STejun Heo  * pwq_set_max_active - adjust max_active of a pwq
3610112202d9STejun Heo  * @pwq: target pool_workqueue
36119f4bd4cdSLai Jiangshan  * @max_active: new max_active value.
36129f4bd4cdSLai Jiangshan  *
3613112202d9STejun Heo  * Set @pwq->max_active to @max_active and activate delayed works if
36149f4bd4cdSLai Jiangshan  * increased.
36159f4bd4cdSLai Jiangshan  *
36169f4bd4cdSLai Jiangshan  * CONTEXT:
3617d565ed63STejun Heo  * spin_lock_irq(pool->lock).
36189f4bd4cdSLai Jiangshan  */
3619112202d9STejun Heo static void pwq_set_max_active(struct pool_workqueue *pwq, int max_active)
36209f4bd4cdSLai Jiangshan {
3621112202d9STejun Heo 	pwq->max_active = max_active;
36229f4bd4cdSLai Jiangshan 
3623112202d9STejun Heo 	while (!list_empty(&pwq->delayed_works) &&
3624112202d9STejun Heo 	       pwq->nr_active < pwq->max_active)
3625112202d9STejun Heo 		pwq_activate_first_delayed(pwq);
36269f4bd4cdSLai Jiangshan }
36279f4bd4cdSLai Jiangshan 
36289f4bd4cdSLai Jiangshan /**
3629dcd989cbSTejun Heo  * workqueue_set_max_active - adjust max_active of a workqueue
3630dcd989cbSTejun Heo  * @wq: target workqueue
3631dcd989cbSTejun Heo  * @max_active: new max_active value.
3632dcd989cbSTejun Heo  *
3633dcd989cbSTejun Heo  * Set max_active of @wq to @max_active.
3634dcd989cbSTejun Heo  *
3635dcd989cbSTejun Heo  * CONTEXT:
3636dcd989cbSTejun Heo  * Don't call from IRQ context.
3637dcd989cbSTejun Heo  */
3638dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3639dcd989cbSTejun Heo {
364049e3cf44STejun Heo 	struct pool_workqueue *pwq;
3641dcd989cbSTejun Heo 
3642f3421797STejun Heo 	max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
3643dcd989cbSTejun Heo 
3644e98d5b16STejun Heo 	spin_lock_irq(&workqueue_lock);
3645dcd989cbSTejun Heo 
3646dcd989cbSTejun Heo 	wq->saved_max_active = max_active;
3647dcd989cbSTejun Heo 
364849e3cf44STejun Heo 	for_each_pwq(pwq, wq) {
3649112202d9STejun Heo 		struct worker_pool *pool = pwq->pool;
3650dcd989cbSTejun Heo 
3651e98d5b16STejun Heo 		spin_lock(&pool->lock);
3652dcd989cbSTejun Heo 
365358a69cb4STejun Heo 		if (!(wq->flags & WQ_FREEZABLE) ||
365435b6bb63STejun Heo 		    !(pool->flags & POOL_FREEZING))
3655112202d9STejun Heo 			pwq_set_max_active(pwq, max_active);
3656dcd989cbSTejun Heo 
3657e98d5b16STejun Heo 		spin_unlock(&pool->lock);
3658dcd989cbSTejun Heo 	}
3659dcd989cbSTejun Heo 
3660e98d5b16STejun Heo 	spin_unlock_irq(&workqueue_lock);
3661dcd989cbSTejun Heo }
3662dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3663dcd989cbSTejun Heo 
3664dcd989cbSTejun Heo /**
3665dcd989cbSTejun Heo  * workqueue_congested - test whether a workqueue is congested
3666dcd989cbSTejun Heo  * @cpu: CPU in question
3667dcd989cbSTejun Heo  * @wq: target workqueue
3668dcd989cbSTejun Heo  *
3669dcd989cbSTejun Heo  * Test whether @wq's cpu workqueue for @cpu is congested.  There is
3670dcd989cbSTejun Heo  * no synchronization around this function and the test result is
3671dcd989cbSTejun Heo  * unreliable and only useful as advisory hints or for debugging.
3672dcd989cbSTejun Heo  *
3673dcd989cbSTejun Heo  * RETURNS:
3674dcd989cbSTejun Heo  * %true if congested, %false otherwise.
3675dcd989cbSTejun Heo  */
3676d84ff051STejun Heo bool workqueue_congested(int cpu, struct workqueue_struct *wq)
3677dcd989cbSTejun Heo {
36787fb98ea7STejun Heo 	struct pool_workqueue *pwq;
367976af4d93STejun Heo 	bool ret;
368076af4d93STejun Heo 
368176af4d93STejun Heo 	preempt_disable();
36827fb98ea7STejun Heo 
36837fb98ea7STejun Heo 	if (!(wq->flags & WQ_UNBOUND))
36847fb98ea7STejun Heo 		pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
36857fb98ea7STejun Heo 	else
36867fb98ea7STejun Heo 		pwq = first_pwq(wq);
3687dcd989cbSTejun Heo 
368876af4d93STejun Heo 	ret = !list_empty(&pwq->delayed_works);
368976af4d93STejun Heo 	preempt_enable();
369076af4d93STejun Heo 
369176af4d93STejun Heo 	return ret;
3692dcd989cbSTejun Heo }
3693dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested);
3694dcd989cbSTejun Heo 
3695dcd989cbSTejun Heo /**
3696dcd989cbSTejun Heo  * work_busy - test whether a work is currently pending or running
3697dcd989cbSTejun Heo  * @work: the work to be tested
3698dcd989cbSTejun Heo  *
3699dcd989cbSTejun Heo  * Test whether @work is currently pending or running.  There is no
3700dcd989cbSTejun Heo  * synchronization around this function and the test result is
3701dcd989cbSTejun Heo  * unreliable and only useful as advisory hints or for debugging.
3702dcd989cbSTejun Heo  *
3703dcd989cbSTejun Heo  * RETURNS:
3704dcd989cbSTejun Heo  * OR'd bitmask of WORK_BUSY_* bits.
3705dcd989cbSTejun Heo  */
3706dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work)
3707dcd989cbSTejun Heo {
3708fa1b54e6STejun Heo 	struct worker_pool *pool;
3709dcd989cbSTejun Heo 	unsigned long flags;
3710dcd989cbSTejun Heo 	unsigned int ret = 0;
3711dcd989cbSTejun Heo 
3712dcd989cbSTejun Heo 	if (work_pending(work))
3713dcd989cbSTejun Heo 		ret |= WORK_BUSY_PENDING;
3714038366c5SLai Jiangshan 
3715fa1b54e6STejun Heo 	local_irq_save(flags);
3716fa1b54e6STejun Heo 	pool = get_work_pool(work);
3717038366c5SLai Jiangshan 	if (pool) {
3718fa1b54e6STejun Heo 		spin_lock(&pool->lock);
3719c9e7cf27STejun Heo 		if (find_worker_executing_work(pool, work))
3720dcd989cbSTejun Heo 			ret |= WORK_BUSY_RUNNING;
3721fa1b54e6STejun Heo 		spin_unlock(&pool->lock);
3722038366c5SLai Jiangshan 	}
3723fa1b54e6STejun Heo 	local_irq_restore(flags);
3724dcd989cbSTejun Heo 
3725dcd989cbSTejun Heo 	return ret;
3726dcd989cbSTejun Heo }
3727dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy);
3728dcd989cbSTejun Heo 
3729db7bccf4STejun Heo /*
3730db7bccf4STejun Heo  * CPU hotplug.
3731db7bccf4STejun Heo  *
3732e22bee78STejun Heo  * There are two challenges in supporting CPU hotplug.  Firstly, there
3733112202d9STejun Heo  * are a lot of assumptions on strong associations among work, pwq and
3734706026c2STejun Heo  * pool which make migrating pending and scheduled works very
3735e22bee78STejun Heo  * difficult to implement without impacting hot paths.  Secondly,
373694cf58bbSTejun Heo  * worker pools serve mix of short, long and very long running works making
3737e22bee78STejun Heo  * blocked draining impractical.
3738e22bee78STejun Heo  *
373924647570STejun Heo  * This is solved by allowing the pools to be disassociated from the CPU
3740628c78e7STejun Heo  * running as an unbound one and allowing it to be reattached later if the
3741628c78e7STejun Heo  * cpu comes back online.
3742db7bccf4STejun Heo  */
3743db7bccf4STejun Heo 
3744706026c2STejun Heo static void wq_unbind_fn(struct work_struct *work)
3745db7bccf4STejun Heo {
374638db41d9STejun Heo 	int cpu = smp_processor_id();
37474ce62e9eSTejun Heo 	struct worker_pool *pool;
3748db7bccf4STejun Heo 	struct worker *worker;
3749db7bccf4STejun Heo 	int i;
3750db7bccf4STejun Heo 
375138db41d9STejun Heo 	for_each_std_worker_pool(pool, cpu) {
37526183c009STejun Heo 		WARN_ON_ONCE(cpu != smp_processor_id());
3753db7bccf4STejun Heo 
375494cf58bbSTejun Heo 		mutex_lock(&pool->assoc_mutex);
375594cf58bbSTejun Heo 		spin_lock_irq(&pool->lock);
3756e22bee78STejun Heo 
3757f2d5a0eeSTejun Heo 		/*
375894cf58bbSTejun Heo 		 * We've claimed all manager positions.  Make all workers
375994cf58bbSTejun Heo 		 * unbound and set DISASSOCIATED.  Before this, all workers
376094cf58bbSTejun Heo 		 * except for the ones which are still executing works from
376194cf58bbSTejun Heo 		 * before the last CPU down must be on the cpu.  After
376294cf58bbSTejun Heo 		 * this, they may become diasporas.
3763f2d5a0eeSTejun Heo 		 */
37644ce62e9eSTejun Heo 		list_for_each_entry(worker, &pool->idle_list, entry)
3765403c821dSTejun Heo 			worker->flags |= WORKER_UNBOUND;
3766db7bccf4STejun Heo 
3767b67bfe0dSSasha Levin 		for_each_busy_worker(worker, i, pool)
3768403c821dSTejun Heo 			worker->flags |= WORKER_UNBOUND;
3769db7bccf4STejun Heo 
377024647570STejun Heo 		pool->flags |= POOL_DISASSOCIATED;
3771f2d5a0eeSTejun Heo 
377294cf58bbSTejun Heo 		spin_unlock_irq(&pool->lock);
377394cf58bbSTejun Heo 		mutex_unlock(&pool->assoc_mutex);
377494cf58bbSTejun Heo 	}
3775e22bee78STejun Heo 
3776e22bee78STejun Heo 	/*
3777628c78e7STejun Heo 	 * Call schedule() so that we cross rq->lock and thus can guarantee
3778628c78e7STejun Heo 	 * sched callbacks see the %WORKER_UNBOUND flag.  This is necessary
3779628c78e7STejun Heo 	 * as scheduler callbacks may be invoked from other cpus.
3780628c78e7STejun Heo 	 */
3781628c78e7STejun Heo 	schedule();
3782628c78e7STejun Heo 
3783628c78e7STejun Heo 	/*
3784628c78e7STejun Heo 	 * Sched callbacks are disabled now.  Zap nr_running.  After this,
3785628c78e7STejun Heo 	 * nr_running stays zero and need_more_worker() and keep_working()
378638db41d9STejun Heo 	 * are always true as long as the worklist is not empty.  Pools on
378738db41d9STejun Heo 	 * @cpu now behave as unbound (in terms of concurrency management)
378838db41d9STejun Heo 	 * pools which are served by workers tied to the CPU.
3789628c78e7STejun Heo 	 *
3790628c78e7STejun Heo 	 * On return from this function, the current worker would trigger
3791628c78e7STejun Heo 	 * unbound chain execution of pending work items if other workers
3792628c78e7STejun Heo 	 * didn't already.
3793e22bee78STejun Heo 	 */
379438db41d9STejun Heo 	for_each_std_worker_pool(pool, cpu)
3795e19e397aSTejun Heo 		atomic_set(&pool->nr_running, 0);
3796db7bccf4STejun Heo }
3797db7bccf4STejun Heo 
37988db25e78STejun Heo /*
37998db25e78STejun Heo  * Workqueues should be brought up before normal priority CPU notifiers.
38008db25e78STejun Heo  * This will be registered high priority CPU notifier.
38018db25e78STejun Heo  */
38029fdf9b73SLai Jiangshan static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb,
38031da177e4SLinus Torvalds 					       unsigned long action,
38041da177e4SLinus Torvalds 					       void *hcpu)
38051da177e4SLinus Torvalds {
3806d84ff051STejun Heo 	int cpu = (unsigned long)hcpu;
38074ce62e9eSTejun Heo 	struct worker_pool *pool;
38081da177e4SLinus Torvalds 
38098db25e78STejun Heo 	switch (action & ~CPU_TASKS_FROZEN) {
38103af24433SOleg Nesterov 	case CPU_UP_PREPARE:
381138db41d9STejun Heo 		for_each_std_worker_pool(pool, cpu) {
38123ce63377STejun Heo 			struct worker *worker;
38133ce63377STejun Heo 
38143ce63377STejun Heo 			if (pool->nr_workers)
38153ce63377STejun Heo 				continue;
38163ce63377STejun Heo 
38173ce63377STejun Heo 			worker = create_worker(pool);
38183ce63377STejun Heo 			if (!worker)
38193ce63377STejun Heo 				return NOTIFY_BAD;
38203ce63377STejun Heo 
3821d565ed63STejun Heo 			spin_lock_irq(&pool->lock);
38223ce63377STejun Heo 			start_worker(worker);
3823d565ed63STejun Heo 			spin_unlock_irq(&pool->lock);
38243af24433SOleg Nesterov 		}
38251da177e4SLinus Torvalds 		break;
38261da177e4SLinus Torvalds 
382765758202STejun Heo 	case CPU_DOWN_FAILED:
382865758202STejun Heo 	case CPU_ONLINE:
382938db41d9STejun Heo 		for_each_std_worker_pool(pool, cpu) {
383094cf58bbSTejun Heo 			mutex_lock(&pool->assoc_mutex);
383194cf58bbSTejun Heo 			spin_lock_irq(&pool->lock);
383294cf58bbSTejun Heo 
383324647570STejun Heo 			pool->flags &= ~POOL_DISASSOCIATED;
383494cf58bbSTejun Heo 			rebind_workers(pool);
383594cf58bbSTejun Heo 
383694cf58bbSTejun Heo 			spin_unlock_irq(&pool->lock);
383794cf58bbSTejun Heo 			mutex_unlock(&pool->assoc_mutex);
383894cf58bbSTejun Heo 		}
38398db25e78STejun Heo 		break;
384065758202STejun Heo 	}
384165758202STejun Heo 	return NOTIFY_OK;
384265758202STejun Heo }
384365758202STejun Heo 
384465758202STejun Heo /*
384565758202STejun Heo  * Workqueues should be brought down after normal priority CPU notifiers.
384665758202STejun Heo  * This will be registered as low priority CPU notifier.
384765758202STejun Heo  */
38489fdf9b73SLai Jiangshan static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb,
384965758202STejun Heo 						 unsigned long action,
385065758202STejun Heo 						 void *hcpu)
385165758202STejun Heo {
3852d84ff051STejun Heo 	int cpu = (unsigned long)hcpu;
38538db25e78STejun Heo 	struct work_struct unbind_work;
38548db25e78STejun Heo 
385565758202STejun Heo 	switch (action & ~CPU_TASKS_FROZEN) {
385665758202STejun Heo 	case CPU_DOWN_PREPARE:
38578db25e78STejun Heo 		/* unbinding should happen on the local CPU */
3858706026c2STejun Heo 		INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
38597635d2fdSJoonsoo Kim 		queue_work_on(cpu, system_highpri_wq, &unbind_work);
38608db25e78STejun Heo 		flush_work(&unbind_work);
38618db25e78STejun Heo 		break;
386265758202STejun Heo 	}
386365758202STejun Heo 	return NOTIFY_OK;
386465758202STejun Heo }
386565758202STejun Heo 
38662d3854a3SRusty Russell #ifdef CONFIG_SMP
38678ccad40dSRusty Russell 
38682d3854a3SRusty Russell struct work_for_cpu {
3869ed48ece2STejun Heo 	struct work_struct work;
38702d3854a3SRusty Russell 	long (*fn)(void *);
38712d3854a3SRusty Russell 	void *arg;
38722d3854a3SRusty Russell 	long ret;
38732d3854a3SRusty Russell };
38742d3854a3SRusty Russell 
3875ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work)
38762d3854a3SRusty Russell {
3877ed48ece2STejun Heo 	struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
3878ed48ece2STejun Heo 
38792d3854a3SRusty Russell 	wfc->ret = wfc->fn(wfc->arg);
38802d3854a3SRusty Russell }
38812d3854a3SRusty Russell 
38822d3854a3SRusty Russell /**
38832d3854a3SRusty Russell  * work_on_cpu - run a function in user context on a particular cpu
38842d3854a3SRusty Russell  * @cpu: the cpu to run on
38852d3854a3SRusty Russell  * @fn: the function to run
38862d3854a3SRusty Russell  * @arg: the function arg
38872d3854a3SRusty Russell  *
388831ad9081SRusty Russell  * This will return the value @fn returns.
388931ad9081SRusty Russell  * It is up to the caller to ensure that the cpu doesn't go offline.
38906b44003eSAndrew Morton  * The caller must not hold any locks which would prevent @fn from completing.
38912d3854a3SRusty Russell  */
3892d84ff051STejun Heo long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
38932d3854a3SRusty Russell {
3894ed48ece2STejun Heo 	struct work_for_cpu wfc = { .fn = fn, .arg = arg };
38952d3854a3SRusty Russell 
3896ed48ece2STejun Heo 	INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
3897ed48ece2STejun Heo 	schedule_work_on(cpu, &wfc.work);
3898ed48ece2STejun Heo 	flush_work(&wfc.work);
38992d3854a3SRusty Russell 	return wfc.ret;
39002d3854a3SRusty Russell }
39012d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu);
39022d3854a3SRusty Russell #endif /* CONFIG_SMP */
39032d3854a3SRusty Russell 
3904a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER
3905e7577c50SRusty Russell 
3906a0a1a5fdSTejun Heo /**
3907a0a1a5fdSTejun Heo  * freeze_workqueues_begin - begin freezing workqueues
3908a0a1a5fdSTejun Heo  *
390958a69cb4STejun Heo  * Start freezing workqueues.  After this function returns, all freezable
391058a69cb4STejun Heo  * workqueues will queue new works to their frozen_works list instead of
3911706026c2STejun Heo  * pool->worklist.
3912a0a1a5fdSTejun Heo  *
3913a0a1a5fdSTejun Heo  * CONTEXT:
3914d565ed63STejun Heo  * Grabs and releases workqueue_lock and pool->lock's.
3915a0a1a5fdSTejun Heo  */
3916a0a1a5fdSTejun Heo void freeze_workqueues_begin(void)
3917a0a1a5fdSTejun Heo {
391817116969STejun Heo 	struct worker_pool *pool;
391924b8a847STejun Heo 	struct workqueue_struct *wq;
392024b8a847STejun Heo 	struct pool_workqueue *pwq;
392117116969STejun Heo 	int id;
3922a0a1a5fdSTejun Heo 
3923e98d5b16STejun Heo 	spin_lock_irq(&workqueue_lock);
3924a0a1a5fdSTejun Heo 
39256183c009STejun Heo 	WARN_ON_ONCE(workqueue_freezing);
3926a0a1a5fdSTejun Heo 	workqueue_freezing = true;
3927a0a1a5fdSTejun Heo 
392824b8a847STejun Heo 	/* set FREEZING */
392917116969STejun Heo 	for_each_pool(pool, id) {
3930e98d5b16STejun Heo 		spin_lock(&pool->lock);
393135b6bb63STejun Heo 		WARN_ON_ONCE(pool->flags & POOL_FREEZING);
393235b6bb63STejun Heo 		pool->flags |= POOL_FREEZING;
393324b8a847STejun Heo 		spin_unlock(&pool->lock);
39341da177e4SLinus Torvalds 	}
39358b03ae3cSTejun Heo 
393624b8a847STejun Heo 	/* suppress further executions by setting max_active to zero */
393724b8a847STejun Heo 	list_for_each_entry(wq, &workqueues, list) {
393824b8a847STejun Heo 		if (!(wq->flags & WQ_FREEZABLE))
393924b8a847STejun Heo 			continue;
394024b8a847STejun Heo 
394124b8a847STejun Heo 		for_each_pwq(pwq, wq) {
394224b8a847STejun Heo 			spin_lock(&pwq->pool->lock);
394324b8a847STejun Heo 			pwq->max_active = 0;
394424b8a847STejun Heo 			spin_unlock(&pwq->pool->lock);
394524b8a847STejun Heo 		}
3946a1056305STejun Heo 	}
3947a0a1a5fdSTejun Heo 
3948e98d5b16STejun Heo 	spin_unlock_irq(&workqueue_lock);
3949a0a1a5fdSTejun Heo }
3950a0a1a5fdSTejun Heo 
3951a0a1a5fdSTejun Heo /**
395258a69cb4STejun Heo  * freeze_workqueues_busy - are freezable workqueues still busy?
3953a0a1a5fdSTejun Heo  *
3954a0a1a5fdSTejun Heo  * Check whether freezing is complete.  This function must be called
3955a0a1a5fdSTejun Heo  * between freeze_workqueues_begin() and thaw_workqueues().
3956a0a1a5fdSTejun Heo  *
3957a0a1a5fdSTejun Heo  * CONTEXT:
3958a0a1a5fdSTejun Heo  * Grabs and releases workqueue_lock.
3959a0a1a5fdSTejun Heo  *
3960a0a1a5fdSTejun Heo  * RETURNS:
396158a69cb4STejun Heo  * %true if some freezable workqueues are still busy.  %false if freezing
396258a69cb4STejun Heo  * is complete.
3963a0a1a5fdSTejun Heo  */
3964a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void)
3965a0a1a5fdSTejun Heo {
3966a0a1a5fdSTejun Heo 	bool busy = false;
396724b8a847STejun Heo 	struct workqueue_struct *wq;
396824b8a847STejun Heo 	struct pool_workqueue *pwq;
3969a0a1a5fdSTejun Heo 
3970e98d5b16STejun Heo 	spin_lock_irq(&workqueue_lock);
3971a0a1a5fdSTejun Heo 
39726183c009STejun Heo 	WARN_ON_ONCE(!workqueue_freezing);
3973a0a1a5fdSTejun Heo 
397424b8a847STejun Heo 	list_for_each_entry(wq, &workqueues, list) {
397524b8a847STejun Heo 		if (!(wq->flags & WQ_FREEZABLE))
397624b8a847STejun Heo 			continue;
3977a0a1a5fdSTejun Heo 		/*
3978a0a1a5fdSTejun Heo 		 * nr_active is monotonically decreasing.  It's safe
3979a0a1a5fdSTejun Heo 		 * to peek without lock.
3980a0a1a5fdSTejun Heo 		 */
398124b8a847STejun Heo 		for_each_pwq(pwq, wq) {
39826183c009STejun Heo 			WARN_ON_ONCE(pwq->nr_active < 0);
3983112202d9STejun Heo 			if (pwq->nr_active) {
3984a0a1a5fdSTejun Heo 				busy = true;
3985a0a1a5fdSTejun Heo 				goto out_unlock;
3986a0a1a5fdSTejun Heo 			}
3987a0a1a5fdSTejun Heo 		}
3988a0a1a5fdSTejun Heo 	}
3989a0a1a5fdSTejun Heo out_unlock:
3990e98d5b16STejun Heo 	spin_unlock_irq(&workqueue_lock);
3991a0a1a5fdSTejun Heo 	return busy;
3992a0a1a5fdSTejun Heo }
3993a0a1a5fdSTejun Heo 
3994a0a1a5fdSTejun Heo /**
3995a0a1a5fdSTejun Heo  * thaw_workqueues - thaw workqueues
3996a0a1a5fdSTejun Heo  *
3997a0a1a5fdSTejun Heo  * Thaw workqueues.  Normal queueing is restored and all collected
3998706026c2STejun Heo  * frozen works are transferred to their respective pool worklists.
3999a0a1a5fdSTejun Heo  *
4000a0a1a5fdSTejun Heo  * CONTEXT:
4001d565ed63STejun Heo  * Grabs and releases workqueue_lock and pool->lock's.
4002a0a1a5fdSTejun Heo  */
4003a0a1a5fdSTejun Heo void thaw_workqueues(void)
4004a0a1a5fdSTejun Heo {
400524b8a847STejun Heo 	struct workqueue_struct *wq;
400624b8a847STejun Heo 	struct pool_workqueue *pwq;
400724b8a847STejun Heo 	struct worker_pool *pool;
400824b8a847STejun Heo 	int id;
4009a0a1a5fdSTejun Heo 
4010e98d5b16STejun Heo 	spin_lock_irq(&workqueue_lock);
4011a0a1a5fdSTejun Heo 
4012a0a1a5fdSTejun Heo 	if (!workqueue_freezing)
4013a0a1a5fdSTejun Heo 		goto out_unlock;
4014a0a1a5fdSTejun Heo 
401524b8a847STejun Heo 	/* clear FREEZING */
401624b8a847STejun Heo 	for_each_pool(pool, id) {
4017e98d5b16STejun Heo 		spin_lock(&pool->lock);
401835b6bb63STejun Heo 		WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
401935b6bb63STejun Heo 		pool->flags &= ~POOL_FREEZING;
4020e98d5b16STejun Heo 		spin_unlock(&pool->lock);
4021d565ed63STejun Heo 	}
402224b8a847STejun Heo 
402324b8a847STejun Heo 	/* restore max_active and repopulate worklist */
402424b8a847STejun Heo 	list_for_each_entry(wq, &workqueues, list) {
402524b8a847STejun Heo 		if (!(wq->flags & WQ_FREEZABLE))
402624b8a847STejun Heo 			continue;
402724b8a847STejun Heo 
402824b8a847STejun Heo 		for_each_pwq(pwq, wq) {
402924b8a847STejun Heo 			spin_lock(&pwq->pool->lock);
403024b8a847STejun Heo 			pwq_set_max_active(pwq, wq->saved_max_active);
403124b8a847STejun Heo 			spin_unlock(&pwq->pool->lock);
403224b8a847STejun Heo 		}
403324b8a847STejun Heo 	}
403424b8a847STejun Heo 
403524b8a847STejun Heo 	/* kick workers */
403624b8a847STejun Heo 	for_each_pool(pool, id) {
403724b8a847STejun Heo 		spin_lock(&pool->lock);
403824b8a847STejun Heo 		wake_up_worker(pool);
403924b8a847STejun Heo 		spin_unlock(&pool->lock);
4040a0a1a5fdSTejun Heo 	}
4041a0a1a5fdSTejun Heo 
4042a0a1a5fdSTejun Heo 	workqueue_freezing = false;
4043a0a1a5fdSTejun Heo out_unlock:
4044e98d5b16STejun Heo 	spin_unlock_irq(&workqueue_lock);
4045a0a1a5fdSTejun Heo }
4046a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */
4047a0a1a5fdSTejun Heo 
40486ee0578bSSuresh Siddha static int __init init_workqueues(void)
40491da177e4SLinus Torvalds {
40507a4e344cSTejun Heo 	int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL };
40517a4e344cSTejun Heo 	int i, cpu;
4052c34056a3STejun Heo 
40537c3eed5cSTejun Heo 	/* make sure we have enough bits for OFFQ pool ID */
40547c3eed5cSTejun Heo 	BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) <
40556be19588SLai Jiangshan 		     WORK_CPU_END * NR_STD_WORKER_POOLS);
4056b5490077STejun Heo 
4057e904e6c2STejun Heo 	WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
4058e904e6c2STejun Heo 
4059e904e6c2STejun Heo 	pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
4060e904e6c2STejun Heo 
406165758202STejun Heo 	cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
4062a5b4e57dSLai Jiangshan 	hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
40638b03ae3cSTejun Heo 
4064706026c2STejun Heo 	/* initialize CPU pools */
4065*29c91e99STejun Heo 	for_each_possible_cpu(cpu) {
40664ce62e9eSTejun Heo 		struct worker_pool *pool;
40678b03ae3cSTejun Heo 
40687a4e344cSTejun Heo 		i = 0;
406938db41d9STejun Heo 		for_each_std_worker_pool(pool, cpu) {
40707a4e344cSTejun Heo 			BUG_ON(init_worker_pool(pool));
4071ec22ca5eSTejun Heo 			pool->cpu = cpu;
40727a4e344cSTejun Heo 			cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu));
40737a4e344cSTejun Heo 			pool->attrs->nice = std_nice[i++];
40747a4e344cSTejun Heo 
40759daf9e67STejun Heo 			/* alloc pool ID */
40769daf9e67STejun Heo 			BUG_ON(worker_pool_assign_id(pool));
40774ce62e9eSTejun Heo 		}
40788b03ae3cSTejun Heo 	}
40798b03ae3cSTejun Heo 
4080e22bee78STejun Heo 	/* create the initial worker */
4081*29c91e99STejun Heo 	for_each_online_cpu(cpu) {
40824ce62e9eSTejun Heo 		struct worker_pool *pool;
4083e22bee78STejun Heo 
408438db41d9STejun Heo 		for_each_std_worker_pool(pool, cpu) {
40854ce62e9eSTejun Heo 			struct worker *worker;
40864ce62e9eSTejun Heo 
408724647570STejun Heo 			pool->flags &= ~POOL_DISASSOCIATED;
408824647570STejun Heo 
4089bc2ae0f5STejun Heo 			worker = create_worker(pool);
4090e22bee78STejun Heo 			BUG_ON(!worker);
4091d565ed63STejun Heo 			spin_lock_irq(&pool->lock);
4092e22bee78STejun Heo 			start_worker(worker);
4093d565ed63STejun Heo 			spin_unlock_irq(&pool->lock);
4094e22bee78STejun Heo 		}
40954ce62e9eSTejun Heo 	}
4096e22bee78STejun Heo 
4097*29c91e99STejun Heo 	/* create default unbound wq attrs */
4098*29c91e99STejun Heo 	for (i = 0; i < NR_STD_WORKER_POOLS; i++) {
4099*29c91e99STejun Heo 		struct workqueue_attrs *attrs;
4100*29c91e99STejun Heo 
4101*29c91e99STejun Heo 		BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL)));
4102*29c91e99STejun Heo 
4103*29c91e99STejun Heo 		attrs->nice = std_nice[i];
4104*29c91e99STejun Heo 		cpumask_setall(attrs->cpumask);
4105*29c91e99STejun Heo 
4106*29c91e99STejun Heo 		unbound_std_wq_attrs[i] = attrs;
4107*29c91e99STejun Heo 	}
4108*29c91e99STejun Heo 
4109d320c038STejun Heo 	system_wq = alloc_workqueue("events", 0, 0);
41101aabe902SJoonsoo Kim 	system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
4111d320c038STejun Heo 	system_long_wq = alloc_workqueue("events_long", 0, 0);
4112f3421797STejun Heo 	system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
4113f3421797STejun Heo 					    WQ_UNBOUND_MAX_ACTIVE);
411424d51addSTejun Heo 	system_freezable_wq = alloc_workqueue("events_freezable",
411524d51addSTejun Heo 					      WQ_FREEZABLE, 0);
41161aabe902SJoonsoo Kim 	BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
4117ae930e0fSTejun Heo 	       !system_unbound_wq || !system_freezable_wq);
41186ee0578bSSuresh Siddha 	return 0;
41191da177e4SLinus Torvalds }
41206ee0578bSSuresh Siddha early_initcall(init_workqueues);
4121