xref: /linux-6.15/kernel/workqueue.c (revision 8864b4e5)
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>
4429c91e99STejun 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 
8429c91e99STejun 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 */
15429c91e99STejun Heo 	struct hlist_node	hash_node;	/* R: unbound_pool_hash node */
15529c91e99STejun 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;
16329c91e99STejun Heo 
16429c91e99STejun Heo 	/*
16529c91e99STejun Heo 	 * Destruction of pool is sched-RCU protected to allow dereferences
16629c91e99STejun Heo 	 * from get_work_pool().
16729c91e99STejun Heo 	 */
16829c91e99STejun 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 */
182*8864b4e5STejun Heo 	int			refcnt;		/* L: reference count */
18373f53c4aSTejun Heo 	int			nr_in_flight[WORK_NR_COLORS];
18473f53c4aSTejun Heo 						/* L: nr of in_flight works */
1851e19ffc6STejun Heo 	int			nr_active;	/* L: nr of active works */
186a0a1a5fdSTejun Heo 	int			max_active;	/* L: max active works */
1871e19ffc6STejun Heo 	struct list_head	delayed_works;	/* L: delayed works */
18876af4d93STejun Heo 	struct list_head	pwqs_node;	/* R: node on wq->pwqs */
189493a1724STejun Heo 	struct list_head	mayday_node;	/* W: node on wq->maydays */
190*8864b4e5STejun Heo 
191*8864b4e5STejun Heo 	/*
192*8864b4e5STejun Heo 	 * Release of unbound pwq is punted to system_wq.  See put_pwq()
193*8864b4e5STejun Heo 	 * and pwq_unbound_release_workfn() for details.  pool_workqueue
194*8864b4e5STejun Heo 	 * itself is also sched-RCU protected so that the first pwq can be
195*8864b4e5STejun Heo 	 * determined without grabbing workqueue_lock.
196*8864b4e5STejun Heo 	 */
197*8864b4e5STejun Heo 	struct work_struct	unbound_release_work;
198*8864b4e5STejun Heo 	struct rcu_head		rcu;
199e904e6c2STejun Heo } __aligned(1 << WORK_STRUCT_FLAG_BITS);
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds /*
20273f53c4aSTejun Heo  * Structure used to wait for workqueue flush.
20373f53c4aSTejun Heo  */
20473f53c4aSTejun Heo struct wq_flusher {
20573f53c4aSTejun Heo 	struct list_head	list;		/* F: list of flushers */
20673f53c4aSTejun Heo 	int			flush_color;	/* F: flush color waiting for */
20773f53c4aSTejun Heo 	struct completion	done;		/* flush completion */
20873f53c4aSTejun Heo };
2091da177e4SLinus Torvalds 
21073f53c4aSTejun Heo /*
2111da177e4SLinus Torvalds  * The externally visible workqueue abstraction is an array of
2121da177e4SLinus Torvalds  * per-CPU workqueues:
2131da177e4SLinus Torvalds  */
2141da177e4SLinus Torvalds struct workqueue_struct {
2159c5a2ba7STejun Heo 	unsigned int		flags;		/* W: WQ_* flags */
216420c0ddbSTejun Heo 	struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwq's */
21776af4d93STejun Heo 	struct list_head	pwqs;		/* R: all pwqs of this wq */
2184690c4abSTejun Heo 	struct list_head	list;		/* W: list of all workqueues */
21973f53c4aSTejun Heo 
22073f53c4aSTejun Heo 	struct mutex		flush_mutex;	/* protects wq flushing */
22173f53c4aSTejun Heo 	int			work_color;	/* F: current work color */
22273f53c4aSTejun Heo 	int			flush_color;	/* F: current flush color */
223112202d9STejun Heo 	atomic_t		nr_pwqs_to_flush; /* flush in progress */
22473f53c4aSTejun Heo 	struct wq_flusher	*first_flusher;	/* F: first flusher */
22573f53c4aSTejun Heo 	struct list_head	flusher_queue;	/* F: flush waiters */
22673f53c4aSTejun Heo 	struct list_head	flusher_overflow; /* F: flush overflow list */
22773f53c4aSTejun Heo 
228493a1724STejun Heo 	struct list_head	maydays;	/* W: pwqs requesting rescue */
229e22bee78STejun Heo 	struct worker		*rescuer;	/* I: rescue worker */
230e22bee78STejun Heo 
2319c5a2ba7STejun Heo 	int			nr_drainers;	/* W: drain in progress */
232112202d9STejun Heo 	int			saved_max_active; /* W: saved pwq max_active */
2334e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
2344e6045f1SJohannes Berg 	struct lockdep_map	lockdep_map;
2354e6045f1SJohannes Berg #endif
236b196be89STejun Heo 	char			name[];		/* I: workqueue name */
2371da177e4SLinus Torvalds };
2381da177e4SLinus Torvalds 
239e904e6c2STejun Heo static struct kmem_cache *pwq_cache;
240e904e6c2STejun Heo 
24129c91e99STejun Heo /* hash of all unbound pools keyed by pool->attrs */
24229c91e99STejun Heo static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER);
24329c91e99STejun Heo 
24429c91e99STejun Heo static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS];
24529c91e99STejun Heo 
246d320c038STejun Heo struct workqueue_struct *system_wq __read_mostly;
247d320c038STejun Heo EXPORT_SYMBOL_GPL(system_wq);
248044c782cSValentin Ilie struct workqueue_struct *system_highpri_wq __read_mostly;
2491aabe902SJoonsoo Kim EXPORT_SYMBOL_GPL(system_highpri_wq);
250044c782cSValentin Ilie struct workqueue_struct *system_long_wq __read_mostly;
251d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq);
252044c782cSValentin Ilie struct workqueue_struct *system_unbound_wq __read_mostly;
253f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq);
254044c782cSValentin Ilie struct workqueue_struct *system_freezable_wq __read_mostly;
25524d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq);
256d320c038STejun Heo 
25797bd2347STejun Heo #define CREATE_TRACE_POINTS
25897bd2347STejun Heo #include <trace/events/workqueue.h>
25997bd2347STejun Heo 
26076af4d93STejun Heo #define assert_rcu_or_wq_lock()						\
26176af4d93STejun Heo 	rcu_lockdep_assert(rcu_read_lock_sched_held() ||		\
26276af4d93STejun Heo 			   lockdep_is_held(&workqueue_lock),		\
26376af4d93STejun Heo 			   "sched RCU or workqueue lock should be held")
26476af4d93STejun Heo 
265f02ae73aSTejun Heo #define for_each_cpu_worker_pool(pool, cpu)				\
266f02ae73aSTejun Heo 	for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0];		\
267f02ae73aSTejun Heo 	     (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \
2687a62c2c8STejun Heo 	     (pool)++)
2694ce62e9eSTejun Heo 
270b67bfe0dSSasha Levin #define for_each_busy_worker(worker, i, pool)				\
271b67bfe0dSSasha Levin 	hash_for_each(pool->busy_hash, i, worker, hentry)
272db7bccf4STejun Heo 
27349e3cf44STejun Heo /**
27417116969STejun Heo  * for_each_pool - iterate through all worker_pools in the system
27517116969STejun Heo  * @pool: iteration cursor
27617116969STejun Heo  * @id: integer used for iteration
277fa1b54e6STejun Heo  *
278fa1b54e6STejun Heo  * This must be called either with workqueue_lock held or sched RCU read
279fa1b54e6STejun Heo  * locked.  If the pool needs to be used beyond the locking in effect, the
280fa1b54e6STejun Heo  * caller is responsible for guaranteeing that the pool stays online.
281fa1b54e6STejun Heo  *
282fa1b54e6STejun Heo  * The if/else clause exists only for the lockdep assertion and can be
283fa1b54e6STejun Heo  * ignored.
28417116969STejun Heo  */
28517116969STejun Heo #define for_each_pool(pool, id)						\
286fa1b54e6STejun Heo 	idr_for_each_entry(&worker_pool_idr, pool, id)			\
287fa1b54e6STejun Heo 		if (({ assert_rcu_or_wq_lock(); false; })) { }		\
288fa1b54e6STejun Heo 		else
28917116969STejun Heo 
29017116969STejun Heo /**
29149e3cf44STejun Heo  * for_each_pwq - iterate through all pool_workqueues of the specified workqueue
29249e3cf44STejun Heo  * @pwq: iteration cursor
29349e3cf44STejun Heo  * @wq: the target workqueue
29476af4d93STejun Heo  *
29576af4d93STejun Heo  * This must be called either with workqueue_lock held or sched RCU read
29676af4d93STejun Heo  * locked.  If the pwq needs to be used beyond the locking in effect, the
29776af4d93STejun Heo  * caller is responsible for guaranteeing that the pwq stays online.
29876af4d93STejun Heo  *
29976af4d93STejun Heo  * The if/else clause exists only for the lockdep assertion and can be
30076af4d93STejun Heo  * ignored.
30149e3cf44STejun Heo  */
30249e3cf44STejun Heo #define for_each_pwq(pwq, wq)						\
30376af4d93STejun Heo 	list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node)		\
30476af4d93STejun Heo 		if (({ assert_rcu_or_wq_lock(); false; })) { }		\
30576af4d93STejun Heo 		else
306f3421797STejun Heo 
307dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK
308dc186ad7SThomas Gleixner 
309dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr;
310dc186ad7SThomas Gleixner 
31199777288SStanislaw Gruszka static void *work_debug_hint(void *addr)
31299777288SStanislaw Gruszka {
31399777288SStanislaw Gruszka 	return ((struct work_struct *) addr)->func;
31499777288SStanislaw Gruszka }
31599777288SStanislaw Gruszka 
316dc186ad7SThomas Gleixner /*
317dc186ad7SThomas Gleixner  * fixup_init is called when:
318dc186ad7SThomas Gleixner  * - an active object is initialized
319dc186ad7SThomas Gleixner  */
320dc186ad7SThomas Gleixner static int work_fixup_init(void *addr, enum debug_obj_state state)
321dc186ad7SThomas Gleixner {
322dc186ad7SThomas Gleixner 	struct work_struct *work = addr;
323dc186ad7SThomas Gleixner 
324dc186ad7SThomas Gleixner 	switch (state) {
325dc186ad7SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
326dc186ad7SThomas Gleixner 		cancel_work_sync(work);
327dc186ad7SThomas Gleixner 		debug_object_init(work, &work_debug_descr);
328dc186ad7SThomas Gleixner 		return 1;
329dc186ad7SThomas Gleixner 	default:
330dc186ad7SThomas Gleixner 		return 0;
331dc186ad7SThomas Gleixner 	}
332dc186ad7SThomas Gleixner }
333dc186ad7SThomas Gleixner 
334dc186ad7SThomas Gleixner /*
335dc186ad7SThomas Gleixner  * fixup_activate is called when:
336dc186ad7SThomas Gleixner  * - an active object is activated
337dc186ad7SThomas Gleixner  * - an unknown object is activated (might be a statically initialized object)
338dc186ad7SThomas Gleixner  */
339dc186ad7SThomas Gleixner static int work_fixup_activate(void *addr, enum debug_obj_state state)
340dc186ad7SThomas Gleixner {
341dc186ad7SThomas Gleixner 	struct work_struct *work = addr;
342dc186ad7SThomas Gleixner 
343dc186ad7SThomas Gleixner 	switch (state) {
344dc186ad7SThomas Gleixner 
345dc186ad7SThomas Gleixner 	case ODEBUG_STATE_NOTAVAILABLE:
346dc186ad7SThomas Gleixner 		/*
347dc186ad7SThomas Gleixner 		 * This is not really a fixup. The work struct was
348dc186ad7SThomas Gleixner 		 * statically initialized. We just make sure that it
349dc186ad7SThomas Gleixner 		 * is tracked in the object tracker.
350dc186ad7SThomas Gleixner 		 */
35122df02bbSTejun Heo 		if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
352dc186ad7SThomas Gleixner 			debug_object_init(work, &work_debug_descr);
353dc186ad7SThomas Gleixner 			debug_object_activate(work, &work_debug_descr);
354dc186ad7SThomas Gleixner 			return 0;
355dc186ad7SThomas Gleixner 		}
356dc186ad7SThomas Gleixner 		WARN_ON_ONCE(1);
357dc186ad7SThomas Gleixner 		return 0;
358dc186ad7SThomas Gleixner 
359dc186ad7SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
360dc186ad7SThomas Gleixner 		WARN_ON(1);
361dc186ad7SThomas Gleixner 
362dc186ad7SThomas Gleixner 	default:
363dc186ad7SThomas Gleixner 		return 0;
364dc186ad7SThomas Gleixner 	}
365dc186ad7SThomas Gleixner }
366dc186ad7SThomas Gleixner 
367dc186ad7SThomas Gleixner /*
368dc186ad7SThomas Gleixner  * fixup_free is called when:
369dc186ad7SThomas Gleixner  * - an active object is freed
370dc186ad7SThomas Gleixner  */
371dc186ad7SThomas Gleixner static int work_fixup_free(void *addr, enum debug_obj_state state)
372dc186ad7SThomas Gleixner {
373dc186ad7SThomas Gleixner 	struct work_struct *work = addr;
374dc186ad7SThomas Gleixner 
375dc186ad7SThomas Gleixner 	switch (state) {
376dc186ad7SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
377dc186ad7SThomas Gleixner 		cancel_work_sync(work);
378dc186ad7SThomas Gleixner 		debug_object_free(work, &work_debug_descr);
379dc186ad7SThomas Gleixner 		return 1;
380dc186ad7SThomas Gleixner 	default:
381dc186ad7SThomas Gleixner 		return 0;
382dc186ad7SThomas Gleixner 	}
383dc186ad7SThomas Gleixner }
384dc186ad7SThomas Gleixner 
385dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr = {
386dc186ad7SThomas Gleixner 	.name		= "work_struct",
38799777288SStanislaw Gruszka 	.debug_hint	= work_debug_hint,
388dc186ad7SThomas Gleixner 	.fixup_init	= work_fixup_init,
389dc186ad7SThomas Gleixner 	.fixup_activate	= work_fixup_activate,
390dc186ad7SThomas Gleixner 	.fixup_free	= work_fixup_free,
391dc186ad7SThomas Gleixner };
392dc186ad7SThomas Gleixner 
393dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work)
394dc186ad7SThomas Gleixner {
395dc186ad7SThomas Gleixner 	debug_object_activate(work, &work_debug_descr);
396dc186ad7SThomas Gleixner }
397dc186ad7SThomas Gleixner 
398dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work)
399dc186ad7SThomas Gleixner {
400dc186ad7SThomas Gleixner 	debug_object_deactivate(work, &work_debug_descr);
401dc186ad7SThomas Gleixner }
402dc186ad7SThomas Gleixner 
403dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack)
404dc186ad7SThomas Gleixner {
405dc186ad7SThomas Gleixner 	if (onstack)
406dc186ad7SThomas Gleixner 		debug_object_init_on_stack(work, &work_debug_descr);
407dc186ad7SThomas Gleixner 	else
408dc186ad7SThomas Gleixner 		debug_object_init(work, &work_debug_descr);
409dc186ad7SThomas Gleixner }
410dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work);
411dc186ad7SThomas Gleixner 
412dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work)
413dc186ad7SThomas Gleixner {
414dc186ad7SThomas Gleixner 	debug_object_free(work, &work_debug_descr);
415dc186ad7SThomas Gleixner }
416dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack);
417dc186ad7SThomas Gleixner 
418dc186ad7SThomas Gleixner #else
419dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { }
420dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { }
421dc186ad7SThomas Gleixner #endif
422dc186ad7SThomas Gleixner 
42395402b38SGautham R Shenoy /* Serializes the accesses to the list of workqueues. */
42495402b38SGautham R Shenoy static DEFINE_SPINLOCK(workqueue_lock);
4251da177e4SLinus Torvalds static LIST_HEAD(workqueues);
426a0a1a5fdSTejun Heo static bool workqueue_freezing;		/* W: have wqs started freezing? */
4271da177e4SLinus Torvalds 
42814441960SOleg Nesterov /*
429e19e397aSTejun Heo  * The CPU and unbound standard worker pools.  The unbound ones have
430e19e397aSTejun Heo  * POOL_DISASSOCIATED set, and their workers have WORKER_UNBOUND set.
43114441960SOleg Nesterov  */
432e19e397aSTejun Heo static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
433f02ae73aSTejun Heo 				     cpu_worker_pools);
434f3421797STejun Heo 
435fa1b54e6STejun Heo /*
436fa1b54e6STejun Heo  * idr of all pools.  Modifications are protected by workqueue_lock.  Read
437fa1b54e6STejun Heo  * accesses are protected by sched-RCU protected.
438fa1b54e6STejun Heo  */
4399daf9e67STejun Heo static DEFINE_IDR(worker_pool_idr);
4409daf9e67STejun Heo 
441c34056a3STejun Heo static int worker_thread(void *__worker);
4421da177e4SLinus Torvalds 
4439daf9e67STejun Heo /* allocate ID and assign it to @pool */
4449daf9e67STejun Heo static int worker_pool_assign_id(struct worker_pool *pool)
4459daf9e67STejun Heo {
4469daf9e67STejun Heo 	int ret;
4479daf9e67STejun Heo 
448fa1b54e6STejun Heo 	do {
449fa1b54e6STejun Heo 		if (!idr_pre_get(&worker_pool_idr, GFP_KERNEL))
450fa1b54e6STejun Heo 			return -ENOMEM;
451fa1b54e6STejun Heo 
452fa1b54e6STejun Heo 		spin_lock_irq(&workqueue_lock);
4539daf9e67STejun Heo 		ret = idr_get_new(&worker_pool_idr, pool, &pool->id);
454fa1b54e6STejun Heo 		spin_unlock_irq(&workqueue_lock);
455fa1b54e6STejun Heo 	} while (ret == -EAGAIN);
4569daf9e67STejun Heo 
4579daf9e67STejun Heo 	return ret;
4589daf9e67STejun Heo }
4599daf9e67STejun Heo 
46076af4d93STejun Heo /**
46176af4d93STejun Heo  * first_pwq - return the first pool_workqueue of the specified workqueue
46276af4d93STejun Heo  * @wq: the target workqueue
46376af4d93STejun Heo  *
46476af4d93STejun Heo  * This must be called either with workqueue_lock held or sched RCU read
46576af4d93STejun Heo  * locked.  If the pwq needs to be used beyond the locking in effect, the
46676af4d93STejun Heo  * caller is responsible for guaranteeing that the pwq stays online.
46776af4d93STejun Heo  */
4687fb98ea7STejun Heo static struct pool_workqueue *first_pwq(struct workqueue_struct *wq)
469a848e3b6SOleg Nesterov {
47076af4d93STejun Heo 	assert_rcu_or_wq_lock();
47176af4d93STejun Heo 	return list_first_or_null_rcu(&wq->pwqs, struct pool_workqueue,
47276af4d93STejun Heo 				      pwqs_node);
473f3421797STejun Heo }
474a848e3b6SOleg Nesterov 
47573f53c4aSTejun Heo static unsigned int work_color_to_flags(int color)
47673f53c4aSTejun Heo {
47773f53c4aSTejun Heo 	return color << WORK_STRUCT_COLOR_SHIFT;
47873f53c4aSTejun Heo }
47973f53c4aSTejun Heo 
48073f53c4aSTejun Heo static int get_work_color(struct work_struct *work)
48173f53c4aSTejun Heo {
48273f53c4aSTejun Heo 	return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
48373f53c4aSTejun Heo 		((1 << WORK_STRUCT_COLOR_BITS) - 1);
48473f53c4aSTejun Heo }
48573f53c4aSTejun Heo 
48673f53c4aSTejun Heo static int work_next_color(int color)
48773f53c4aSTejun Heo {
48873f53c4aSTejun Heo 	return (color + 1) % WORK_NR_COLORS;
489b1f4ec17SOleg Nesterov }
490b1f4ec17SOleg Nesterov 
4914594bf15SDavid Howells /*
492112202d9STejun Heo  * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data
493112202d9STejun Heo  * contain the pointer to the queued pwq.  Once execution starts, the flag
4947c3eed5cSTejun Heo  * is cleared and the high bits contain OFFQ flags and pool ID.
4957a22ad75STejun Heo  *
496112202d9STejun Heo  * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
497112202d9STejun Heo  * and clear_work_data() can be used to set the pwq, pool or clear
498bbb68dfaSTejun Heo  * work->data.  These functions should only be called while the work is
499bbb68dfaSTejun Heo  * owned - ie. while the PENDING bit is set.
5007a22ad75STejun Heo  *
501112202d9STejun Heo  * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq
5027c3eed5cSTejun Heo  * corresponding to a work.  Pool is available once the work has been
503112202d9STejun Heo  * queued anywhere after initialization until it is sync canceled.  pwq is
5047c3eed5cSTejun Heo  * available only while the work item is queued.
505bbb68dfaSTejun Heo  *
506bbb68dfaSTejun Heo  * %WORK_OFFQ_CANCELING is used to mark a work item which is being
507bbb68dfaSTejun Heo  * canceled.  While being canceled, a work item may have its PENDING set
508bbb68dfaSTejun Heo  * but stay off timer and worklist for arbitrarily long and nobody should
509bbb68dfaSTejun Heo  * try to steal the PENDING bit.
5104594bf15SDavid Howells  */
5117a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data,
5127a22ad75STejun Heo 				 unsigned long flags)
5137a22ad75STejun Heo {
5146183c009STejun Heo 	WARN_ON_ONCE(!work_pending(work));
5157a22ad75STejun Heo 	atomic_long_set(&work->data, data | flags | work_static(work));
5167a22ad75STejun Heo }
5177a22ad75STejun Heo 
518112202d9STejun Heo static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq,
5194690c4abSTejun Heo 			 unsigned long extra_flags)
520365970a1SDavid Howells {
521112202d9STejun Heo 	set_work_data(work, (unsigned long)pwq,
522112202d9STejun Heo 		      WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags);
523365970a1SDavid Howells }
524365970a1SDavid Howells 
5254468a00fSLai Jiangshan static void set_work_pool_and_keep_pending(struct work_struct *work,
5264468a00fSLai Jiangshan 					   int pool_id)
5274468a00fSLai Jiangshan {
5284468a00fSLai Jiangshan 	set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT,
5294468a00fSLai Jiangshan 		      WORK_STRUCT_PENDING);
5304468a00fSLai Jiangshan }
5314468a00fSLai Jiangshan 
5327c3eed5cSTejun Heo static void set_work_pool_and_clear_pending(struct work_struct *work,
5337c3eed5cSTejun Heo 					    int pool_id)
5344d707b9fSOleg Nesterov {
53523657bb1STejun Heo 	/*
53623657bb1STejun Heo 	 * The following wmb is paired with the implied mb in
53723657bb1STejun Heo 	 * test_and_set_bit(PENDING) and ensures all updates to @work made
53823657bb1STejun Heo 	 * here are visible to and precede any updates by the next PENDING
53923657bb1STejun Heo 	 * owner.
54023657bb1STejun Heo 	 */
54123657bb1STejun Heo 	smp_wmb();
5427c3eed5cSTejun Heo 	set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
5434d707b9fSOleg Nesterov }
5444d707b9fSOleg Nesterov 
5457a22ad75STejun Heo static void clear_work_data(struct work_struct *work)
546365970a1SDavid Howells {
5477c3eed5cSTejun Heo 	smp_wmb();	/* see set_work_pool_and_clear_pending() */
5487c3eed5cSTejun Heo 	set_work_data(work, WORK_STRUCT_NO_POOL, 0);
5497a22ad75STejun Heo }
5507a22ad75STejun Heo 
551112202d9STejun Heo static struct pool_workqueue *get_work_pwq(struct work_struct *work)
5527a22ad75STejun Heo {
553e120153dSTejun Heo 	unsigned long data = atomic_long_read(&work->data);
5547a22ad75STejun Heo 
555112202d9STejun Heo 	if (data & WORK_STRUCT_PWQ)
556e120153dSTejun Heo 		return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
557e120153dSTejun Heo 	else
558e120153dSTejun Heo 		return NULL;
5597a22ad75STejun Heo }
5607a22ad75STejun Heo 
5617c3eed5cSTejun Heo /**
5627c3eed5cSTejun Heo  * get_work_pool - return the worker_pool a given work was associated with
5637c3eed5cSTejun Heo  * @work: the work item of interest
5647c3eed5cSTejun Heo  *
5657c3eed5cSTejun Heo  * Return the worker_pool @work was last associated with.  %NULL if none.
566fa1b54e6STejun Heo  *
567fa1b54e6STejun Heo  * Pools are created and destroyed under workqueue_lock, and allows read
568fa1b54e6STejun Heo  * access under sched-RCU read lock.  As such, this function should be
569fa1b54e6STejun Heo  * called under workqueue_lock or with preemption disabled.
570fa1b54e6STejun Heo  *
571fa1b54e6STejun Heo  * All fields of the returned pool are accessible as long as the above
572fa1b54e6STejun Heo  * mentioned locking is in effect.  If the returned pool needs to be used
573fa1b54e6STejun Heo  * beyond the critical section, the caller is responsible for ensuring the
574fa1b54e6STejun Heo  * returned pool is and stays online.
5757c3eed5cSTejun Heo  */
5767c3eed5cSTejun Heo static struct worker_pool *get_work_pool(struct work_struct *work)
5777a22ad75STejun Heo {
578e120153dSTejun Heo 	unsigned long data = atomic_long_read(&work->data);
5797c3eed5cSTejun Heo 	int pool_id;
5807a22ad75STejun Heo 
581fa1b54e6STejun Heo 	assert_rcu_or_wq_lock();
582fa1b54e6STejun Heo 
583112202d9STejun Heo 	if (data & WORK_STRUCT_PWQ)
584112202d9STejun Heo 		return ((struct pool_workqueue *)
5857c3eed5cSTejun Heo 			(data & WORK_STRUCT_WQ_DATA_MASK))->pool;
5867a22ad75STejun Heo 
5877c3eed5cSTejun Heo 	pool_id = data >> WORK_OFFQ_POOL_SHIFT;
5887c3eed5cSTejun Heo 	if (pool_id == WORK_OFFQ_POOL_NONE)
5897a22ad75STejun Heo 		return NULL;
5907a22ad75STejun Heo 
591fa1b54e6STejun Heo 	return idr_find(&worker_pool_idr, pool_id);
5927c3eed5cSTejun Heo }
5937c3eed5cSTejun Heo 
5947c3eed5cSTejun Heo /**
5957c3eed5cSTejun Heo  * get_work_pool_id - return the worker pool ID a given work is associated with
5967c3eed5cSTejun Heo  * @work: the work item of interest
5977c3eed5cSTejun Heo  *
5987c3eed5cSTejun Heo  * Return the worker_pool ID @work was last associated with.
5997c3eed5cSTejun Heo  * %WORK_OFFQ_POOL_NONE if none.
6007c3eed5cSTejun Heo  */
6017c3eed5cSTejun Heo static int get_work_pool_id(struct work_struct *work)
6027c3eed5cSTejun Heo {
60354d5b7d0SLai Jiangshan 	unsigned long data = atomic_long_read(&work->data);
6047c3eed5cSTejun Heo 
605112202d9STejun Heo 	if (data & WORK_STRUCT_PWQ)
606112202d9STejun Heo 		return ((struct pool_workqueue *)
60754d5b7d0SLai Jiangshan 			(data & WORK_STRUCT_WQ_DATA_MASK))->pool->id;
60854d5b7d0SLai Jiangshan 
60954d5b7d0SLai Jiangshan 	return data >> WORK_OFFQ_POOL_SHIFT;
6107c3eed5cSTejun Heo }
6117c3eed5cSTejun Heo 
612bbb68dfaSTejun Heo static void mark_work_canceling(struct work_struct *work)
613bbb68dfaSTejun Heo {
6147c3eed5cSTejun Heo 	unsigned long pool_id = get_work_pool_id(work);
615bbb68dfaSTejun Heo 
6167c3eed5cSTejun Heo 	pool_id <<= WORK_OFFQ_POOL_SHIFT;
6177c3eed5cSTejun Heo 	set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
618bbb68dfaSTejun Heo }
619bbb68dfaSTejun Heo 
620bbb68dfaSTejun Heo static bool work_is_canceling(struct work_struct *work)
621bbb68dfaSTejun Heo {
622bbb68dfaSTejun Heo 	unsigned long data = atomic_long_read(&work->data);
623bbb68dfaSTejun Heo 
624112202d9STejun Heo 	return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING);
625bbb68dfaSTejun Heo }
626bbb68dfaSTejun Heo 
627e22bee78STejun Heo /*
6283270476aSTejun Heo  * Policy functions.  These define the policies on how the global worker
6293270476aSTejun Heo  * pools are managed.  Unless noted otherwise, these functions assume that
630d565ed63STejun Heo  * they're being called with pool->lock held.
631e22bee78STejun Heo  */
632e22bee78STejun Heo 
63363d95a91STejun Heo static bool __need_more_worker(struct worker_pool *pool)
634649027d7STejun Heo {
635e19e397aSTejun Heo 	return !atomic_read(&pool->nr_running);
636649027d7STejun Heo }
637649027d7STejun Heo 
638e22bee78STejun Heo /*
639e22bee78STejun Heo  * Need to wake up a worker?  Called from anything but currently
640e22bee78STejun Heo  * running workers.
641974271c4STejun Heo  *
642974271c4STejun Heo  * Note that, because unbound workers never contribute to nr_running, this
643706026c2STejun Heo  * function will always return %true for unbound pools as long as the
644974271c4STejun Heo  * worklist isn't empty.
645e22bee78STejun Heo  */
64663d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool)
647e22bee78STejun Heo {
64863d95a91STejun Heo 	return !list_empty(&pool->worklist) && __need_more_worker(pool);
649e22bee78STejun Heo }
650e22bee78STejun Heo 
651e22bee78STejun Heo /* Can I start working?  Called from busy but !running workers. */
65263d95a91STejun Heo static bool may_start_working(struct worker_pool *pool)
653e22bee78STejun Heo {
65463d95a91STejun Heo 	return pool->nr_idle;
655e22bee78STejun Heo }
656e22bee78STejun Heo 
657e22bee78STejun Heo /* Do I need to keep working?  Called from currently running workers. */
65863d95a91STejun Heo static bool keep_working(struct worker_pool *pool)
659e22bee78STejun Heo {
660e19e397aSTejun Heo 	return !list_empty(&pool->worklist) &&
661e19e397aSTejun Heo 		atomic_read(&pool->nr_running) <= 1;
662e22bee78STejun Heo }
663e22bee78STejun Heo 
664e22bee78STejun Heo /* Do we need a new worker?  Called from manager. */
66563d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool)
666e22bee78STejun Heo {
66763d95a91STejun Heo 	return need_more_worker(pool) && !may_start_working(pool);
668e22bee78STejun Heo }
669e22bee78STejun Heo 
670e22bee78STejun Heo /* Do I need to be the manager? */
67163d95a91STejun Heo static bool need_to_manage_workers(struct worker_pool *pool)
672e22bee78STejun Heo {
67363d95a91STejun Heo 	return need_to_create_worker(pool) ||
67411ebea50STejun Heo 		(pool->flags & POOL_MANAGE_WORKERS);
675e22bee78STejun Heo }
676e22bee78STejun Heo 
677e22bee78STejun Heo /* Do we have too many workers and should some go away? */
67863d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool)
679e22bee78STejun Heo {
68034a06bd6STejun Heo 	bool managing = mutex_is_locked(&pool->manager_arb);
68163d95a91STejun Heo 	int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
68263d95a91STejun Heo 	int nr_busy = pool->nr_workers - nr_idle;
683e22bee78STejun Heo 
684ea1abd61SLai Jiangshan 	/*
685ea1abd61SLai Jiangshan 	 * nr_idle and idle_list may disagree if idle rebinding is in
686ea1abd61SLai Jiangshan 	 * progress.  Never return %true if idle_list is empty.
687ea1abd61SLai Jiangshan 	 */
688ea1abd61SLai Jiangshan 	if (list_empty(&pool->idle_list))
689ea1abd61SLai Jiangshan 		return false;
690ea1abd61SLai Jiangshan 
691e22bee78STejun Heo 	return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
692e22bee78STejun Heo }
693e22bee78STejun Heo 
694e22bee78STejun Heo /*
695e22bee78STejun Heo  * Wake up functions.
696e22bee78STejun Heo  */
697e22bee78STejun Heo 
6987e11629dSTejun Heo /* Return the first worker.  Safe with preemption disabled */
69963d95a91STejun Heo static struct worker *first_worker(struct worker_pool *pool)
7007e11629dSTejun Heo {
70163d95a91STejun Heo 	if (unlikely(list_empty(&pool->idle_list)))
7027e11629dSTejun Heo 		return NULL;
7037e11629dSTejun Heo 
70463d95a91STejun Heo 	return list_first_entry(&pool->idle_list, struct worker, entry);
7057e11629dSTejun Heo }
7067e11629dSTejun Heo 
7077e11629dSTejun Heo /**
7087e11629dSTejun Heo  * wake_up_worker - wake up an idle worker
70963d95a91STejun Heo  * @pool: worker pool to wake worker from
7107e11629dSTejun Heo  *
71163d95a91STejun Heo  * Wake up the first idle worker of @pool.
7127e11629dSTejun Heo  *
7137e11629dSTejun Heo  * CONTEXT:
714d565ed63STejun Heo  * spin_lock_irq(pool->lock).
7157e11629dSTejun Heo  */
71663d95a91STejun Heo static void wake_up_worker(struct worker_pool *pool)
7177e11629dSTejun Heo {
71863d95a91STejun Heo 	struct worker *worker = first_worker(pool);
7197e11629dSTejun Heo 
7207e11629dSTejun Heo 	if (likely(worker))
7217e11629dSTejun Heo 		wake_up_process(worker->task);
7227e11629dSTejun Heo }
7237e11629dSTejun Heo 
7244690c4abSTejun Heo /**
725e22bee78STejun Heo  * wq_worker_waking_up - a worker is waking up
726e22bee78STejun Heo  * @task: task waking up
727e22bee78STejun Heo  * @cpu: CPU @task is waking up to
728e22bee78STejun Heo  *
729e22bee78STejun Heo  * This function is called during try_to_wake_up() when a worker is
730e22bee78STejun Heo  * being awoken.
731e22bee78STejun Heo  *
732e22bee78STejun Heo  * CONTEXT:
733e22bee78STejun Heo  * spin_lock_irq(rq->lock)
734e22bee78STejun Heo  */
735d84ff051STejun Heo void wq_worker_waking_up(struct task_struct *task, int cpu)
736e22bee78STejun Heo {
737e22bee78STejun Heo 	struct worker *worker = kthread_data(task);
738e22bee78STejun Heo 
73936576000SJoonsoo Kim 	if (!(worker->flags & WORKER_NOT_RUNNING)) {
740ec22ca5eSTejun Heo 		WARN_ON_ONCE(worker->pool->cpu != cpu);
741e19e397aSTejun Heo 		atomic_inc(&worker->pool->nr_running);
742e22bee78STejun Heo 	}
74336576000SJoonsoo Kim }
744e22bee78STejun Heo 
745e22bee78STejun Heo /**
746e22bee78STejun Heo  * wq_worker_sleeping - a worker is going to sleep
747e22bee78STejun Heo  * @task: task going to sleep
748e22bee78STejun Heo  * @cpu: CPU in question, must be the current CPU number
749e22bee78STejun Heo  *
750e22bee78STejun Heo  * This function is called during schedule() when a busy worker is
751e22bee78STejun Heo  * going to sleep.  Worker on the same cpu can be woken up by
752e22bee78STejun Heo  * returning pointer to its task.
753e22bee78STejun Heo  *
754e22bee78STejun Heo  * CONTEXT:
755e22bee78STejun Heo  * spin_lock_irq(rq->lock)
756e22bee78STejun Heo  *
757e22bee78STejun Heo  * RETURNS:
758e22bee78STejun Heo  * Worker task on @cpu to wake up, %NULL if none.
759e22bee78STejun Heo  */
760d84ff051STejun Heo struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
761e22bee78STejun Heo {
762e22bee78STejun Heo 	struct worker *worker = kthread_data(task), *to_wakeup = NULL;
763111c225aSTejun Heo 	struct worker_pool *pool;
764e22bee78STejun Heo 
765111c225aSTejun Heo 	/*
766111c225aSTejun Heo 	 * Rescuers, which may not have all the fields set up like normal
767111c225aSTejun Heo 	 * workers, also reach here, let's not access anything before
768111c225aSTejun Heo 	 * checking NOT_RUNNING.
769111c225aSTejun Heo 	 */
7702d64672eSSteven Rostedt 	if (worker->flags & WORKER_NOT_RUNNING)
771e22bee78STejun Heo 		return NULL;
772e22bee78STejun Heo 
773111c225aSTejun Heo 	pool = worker->pool;
774111c225aSTejun Heo 
775e22bee78STejun Heo 	/* this can only happen on the local cpu */
7766183c009STejun Heo 	if (WARN_ON_ONCE(cpu != raw_smp_processor_id()))
7776183c009STejun Heo 		return NULL;
778e22bee78STejun Heo 
779e22bee78STejun Heo 	/*
780e22bee78STejun Heo 	 * The counterpart of the following dec_and_test, implied mb,
781e22bee78STejun Heo 	 * worklist not empty test sequence is in insert_work().
782e22bee78STejun Heo 	 * Please read comment there.
783e22bee78STejun Heo 	 *
784628c78e7STejun Heo 	 * NOT_RUNNING is clear.  This means that we're bound to and
785628c78e7STejun Heo 	 * running on the local cpu w/ rq lock held and preemption
786628c78e7STejun Heo 	 * disabled, which in turn means that none else could be
787d565ed63STejun Heo 	 * manipulating idle_list, so dereferencing idle_list without pool
788628c78e7STejun Heo 	 * lock is safe.
789e22bee78STejun Heo 	 */
790e19e397aSTejun Heo 	if (atomic_dec_and_test(&pool->nr_running) &&
791e19e397aSTejun Heo 	    !list_empty(&pool->worklist))
79263d95a91STejun Heo 		to_wakeup = first_worker(pool);
793e22bee78STejun Heo 	return to_wakeup ? to_wakeup->task : NULL;
794e22bee78STejun Heo }
795e22bee78STejun Heo 
796e22bee78STejun Heo /**
797e22bee78STejun Heo  * worker_set_flags - set worker flags and adjust nr_running accordingly
798cb444766STejun Heo  * @worker: self
799d302f017STejun Heo  * @flags: flags to set
800d302f017STejun Heo  * @wakeup: wakeup an idle worker if necessary
801d302f017STejun Heo  *
802e22bee78STejun Heo  * Set @flags in @worker->flags and adjust nr_running accordingly.  If
803e22bee78STejun Heo  * nr_running becomes zero and @wakeup is %true, an idle worker is
804e22bee78STejun Heo  * woken up.
805d302f017STejun Heo  *
806cb444766STejun Heo  * CONTEXT:
807d565ed63STejun Heo  * spin_lock_irq(pool->lock)
808d302f017STejun Heo  */
809d302f017STejun Heo static inline void worker_set_flags(struct worker *worker, unsigned int flags,
810d302f017STejun Heo 				    bool wakeup)
811d302f017STejun Heo {
812bd7bdd43STejun Heo 	struct worker_pool *pool = worker->pool;
813e22bee78STejun Heo 
814cb444766STejun Heo 	WARN_ON_ONCE(worker->task != current);
815cb444766STejun Heo 
816e22bee78STejun Heo 	/*
817e22bee78STejun Heo 	 * If transitioning into NOT_RUNNING, adjust nr_running and
818e22bee78STejun Heo 	 * wake up an idle worker as necessary if requested by
819e22bee78STejun Heo 	 * @wakeup.
820e22bee78STejun Heo 	 */
821e22bee78STejun Heo 	if ((flags & WORKER_NOT_RUNNING) &&
822e22bee78STejun Heo 	    !(worker->flags & WORKER_NOT_RUNNING)) {
823e22bee78STejun Heo 		if (wakeup) {
824e19e397aSTejun Heo 			if (atomic_dec_and_test(&pool->nr_running) &&
825bd7bdd43STejun Heo 			    !list_empty(&pool->worklist))
82663d95a91STejun Heo 				wake_up_worker(pool);
827e22bee78STejun Heo 		} else
828e19e397aSTejun Heo 			atomic_dec(&pool->nr_running);
829e22bee78STejun Heo 	}
830e22bee78STejun Heo 
831d302f017STejun Heo 	worker->flags |= flags;
832d302f017STejun Heo }
833d302f017STejun Heo 
834d302f017STejun Heo /**
835e22bee78STejun Heo  * worker_clr_flags - clear worker flags and adjust nr_running accordingly
836cb444766STejun Heo  * @worker: self
837d302f017STejun Heo  * @flags: flags to clear
838d302f017STejun Heo  *
839e22bee78STejun Heo  * Clear @flags in @worker->flags and adjust nr_running accordingly.
840d302f017STejun Heo  *
841cb444766STejun Heo  * CONTEXT:
842d565ed63STejun Heo  * spin_lock_irq(pool->lock)
843d302f017STejun Heo  */
844d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
845d302f017STejun Heo {
84663d95a91STejun Heo 	struct worker_pool *pool = worker->pool;
847e22bee78STejun Heo 	unsigned int oflags = worker->flags;
848e22bee78STejun Heo 
849cb444766STejun Heo 	WARN_ON_ONCE(worker->task != current);
850cb444766STejun Heo 
851d302f017STejun Heo 	worker->flags &= ~flags;
852e22bee78STejun Heo 
85342c025f3STejun Heo 	/*
85442c025f3STejun Heo 	 * If transitioning out of NOT_RUNNING, increment nr_running.  Note
85542c025f3STejun Heo 	 * that the nested NOT_RUNNING is not a noop.  NOT_RUNNING is mask
85642c025f3STejun Heo 	 * of multiple flags, not a single flag.
85742c025f3STejun Heo 	 */
858e22bee78STejun Heo 	if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
859e22bee78STejun Heo 		if (!(worker->flags & WORKER_NOT_RUNNING))
860e19e397aSTejun Heo 			atomic_inc(&pool->nr_running);
861d302f017STejun Heo }
862d302f017STejun Heo 
863d302f017STejun Heo /**
8648cca0eeaSTejun Heo  * find_worker_executing_work - find worker which is executing a work
865c9e7cf27STejun Heo  * @pool: pool of interest
8668cca0eeaSTejun Heo  * @work: work to find worker for
8678cca0eeaSTejun Heo  *
868c9e7cf27STejun Heo  * Find a worker which is executing @work on @pool by searching
869c9e7cf27STejun Heo  * @pool->busy_hash which is keyed by the address of @work.  For a worker
870a2c1c57bSTejun Heo  * to match, its current execution should match the address of @work and
871a2c1c57bSTejun Heo  * its work function.  This is to avoid unwanted dependency between
872a2c1c57bSTejun Heo  * unrelated work executions through a work item being recycled while still
873a2c1c57bSTejun Heo  * being executed.
874a2c1c57bSTejun Heo  *
875a2c1c57bSTejun Heo  * This is a bit tricky.  A work item may be freed once its execution
876a2c1c57bSTejun Heo  * starts and nothing prevents the freed area from being recycled for
877a2c1c57bSTejun Heo  * another work item.  If the same work item address ends up being reused
878a2c1c57bSTejun Heo  * before the original execution finishes, workqueue will identify the
879a2c1c57bSTejun Heo  * recycled work item as currently executing and make it wait until the
880a2c1c57bSTejun Heo  * current execution finishes, introducing an unwanted dependency.
881a2c1c57bSTejun Heo  *
882a2c1c57bSTejun Heo  * This function checks the work item address, work function and workqueue
883a2c1c57bSTejun Heo  * to avoid false positives.  Note that this isn't complete as one may
884a2c1c57bSTejun Heo  * construct a work function which can introduce dependency onto itself
885a2c1c57bSTejun Heo  * through a recycled work item.  Well, if somebody wants to shoot oneself
886a2c1c57bSTejun Heo  * in the foot that badly, there's only so much we can do, and if such
887a2c1c57bSTejun Heo  * deadlock actually occurs, it should be easy to locate the culprit work
888a2c1c57bSTejun Heo  * function.
8898cca0eeaSTejun Heo  *
8908cca0eeaSTejun Heo  * CONTEXT:
891d565ed63STejun Heo  * spin_lock_irq(pool->lock).
8928cca0eeaSTejun Heo  *
8938cca0eeaSTejun Heo  * RETURNS:
8948cca0eeaSTejun Heo  * Pointer to worker which is executing @work if found, NULL
8958cca0eeaSTejun Heo  * otherwise.
8968cca0eeaSTejun Heo  */
897c9e7cf27STejun Heo static struct worker *find_worker_executing_work(struct worker_pool *pool,
8988cca0eeaSTejun Heo 						 struct work_struct *work)
8998cca0eeaSTejun Heo {
90042f8570fSSasha Levin 	struct worker *worker;
90142f8570fSSasha Levin 
902b67bfe0dSSasha Levin 	hash_for_each_possible(pool->busy_hash, worker, hentry,
903a2c1c57bSTejun Heo 			       (unsigned long)work)
904a2c1c57bSTejun Heo 		if (worker->current_work == work &&
905a2c1c57bSTejun Heo 		    worker->current_func == work->func)
90642f8570fSSasha Levin 			return worker;
90742f8570fSSasha Levin 
90842f8570fSSasha Levin 	return NULL;
9098cca0eeaSTejun Heo }
9108cca0eeaSTejun Heo 
9118cca0eeaSTejun Heo /**
912bf4ede01STejun Heo  * move_linked_works - move linked works to a list
913bf4ede01STejun Heo  * @work: start of series of works to be scheduled
914bf4ede01STejun Heo  * @head: target list to append @work to
915bf4ede01STejun Heo  * @nextp: out paramter for nested worklist walking
916bf4ede01STejun Heo  *
917bf4ede01STejun Heo  * Schedule linked works starting from @work to @head.  Work series to
918bf4ede01STejun Heo  * be scheduled starts at @work and includes any consecutive work with
919bf4ede01STejun Heo  * WORK_STRUCT_LINKED set in its predecessor.
920bf4ede01STejun Heo  *
921bf4ede01STejun Heo  * If @nextp is not NULL, it's updated to point to the next work of
922bf4ede01STejun Heo  * the last scheduled work.  This allows move_linked_works() to be
923bf4ede01STejun Heo  * nested inside outer list_for_each_entry_safe().
924bf4ede01STejun Heo  *
925bf4ede01STejun Heo  * CONTEXT:
926d565ed63STejun Heo  * spin_lock_irq(pool->lock).
927bf4ede01STejun Heo  */
928bf4ede01STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head,
929bf4ede01STejun Heo 			      struct work_struct **nextp)
930bf4ede01STejun Heo {
931bf4ede01STejun Heo 	struct work_struct *n;
932bf4ede01STejun Heo 
933bf4ede01STejun Heo 	/*
934bf4ede01STejun Heo 	 * Linked worklist will always end before the end of the list,
935bf4ede01STejun Heo 	 * use NULL for list head.
936bf4ede01STejun Heo 	 */
937bf4ede01STejun Heo 	list_for_each_entry_safe_from(work, n, NULL, entry) {
938bf4ede01STejun Heo 		list_move_tail(&work->entry, head);
939bf4ede01STejun Heo 		if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
940bf4ede01STejun Heo 			break;
941bf4ede01STejun Heo 	}
942bf4ede01STejun Heo 
943bf4ede01STejun Heo 	/*
944bf4ede01STejun Heo 	 * If we're already inside safe list traversal and have moved
945bf4ede01STejun Heo 	 * multiple works to the scheduled queue, the next position
946bf4ede01STejun Heo 	 * needs to be updated.
947bf4ede01STejun Heo 	 */
948bf4ede01STejun Heo 	if (nextp)
949bf4ede01STejun Heo 		*nextp = n;
950bf4ede01STejun Heo }
951bf4ede01STejun Heo 
952*8864b4e5STejun Heo /**
953*8864b4e5STejun Heo  * get_pwq - get an extra reference on the specified pool_workqueue
954*8864b4e5STejun Heo  * @pwq: pool_workqueue to get
955*8864b4e5STejun Heo  *
956*8864b4e5STejun Heo  * Obtain an extra reference on @pwq.  The caller should guarantee that
957*8864b4e5STejun Heo  * @pwq has positive refcnt and be holding the matching pool->lock.
958*8864b4e5STejun Heo  */
959*8864b4e5STejun Heo static void get_pwq(struct pool_workqueue *pwq)
960*8864b4e5STejun Heo {
961*8864b4e5STejun Heo 	lockdep_assert_held(&pwq->pool->lock);
962*8864b4e5STejun Heo 	WARN_ON_ONCE(pwq->refcnt <= 0);
963*8864b4e5STejun Heo 	pwq->refcnt++;
964*8864b4e5STejun Heo }
965*8864b4e5STejun Heo 
966*8864b4e5STejun Heo /**
967*8864b4e5STejun Heo  * put_pwq - put a pool_workqueue reference
968*8864b4e5STejun Heo  * @pwq: pool_workqueue to put
969*8864b4e5STejun Heo  *
970*8864b4e5STejun Heo  * Drop a reference of @pwq.  If its refcnt reaches zero, schedule its
971*8864b4e5STejun Heo  * destruction.  The caller should be holding the matching pool->lock.
972*8864b4e5STejun Heo  */
973*8864b4e5STejun Heo static void put_pwq(struct pool_workqueue *pwq)
974*8864b4e5STejun Heo {
975*8864b4e5STejun Heo 	lockdep_assert_held(&pwq->pool->lock);
976*8864b4e5STejun Heo 	if (likely(--pwq->refcnt))
977*8864b4e5STejun Heo 		return;
978*8864b4e5STejun Heo 	if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND)))
979*8864b4e5STejun Heo 		return;
980*8864b4e5STejun Heo 	/*
981*8864b4e5STejun Heo 	 * @pwq can't be released under pool->lock, bounce to
982*8864b4e5STejun Heo 	 * pwq_unbound_release_workfn().  This never recurses on the same
983*8864b4e5STejun Heo 	 * pool->lock as this path is taken only for unbound workqueues and
984*8864b4e5STejun Heo 	 * the release work item is scheduled on a per-cpu workqueue.  To
985*8864b4e5STejun Heo 	 * avoid lockdep warning, unbound pool->locks are given lockdep
986*8864b4e5STejun Heo 	 * subclass of 1 in get_unbound_pool().
987*8864b4e5STejun Heo 	 */
988*8864b4e5STejun Heo 	schedule_work(&pwq->unbound_release_work);
989*8864b4e5STejun Heo }
990*8864b4e5STejun Heo 
991112202d9STejun Heo static void pwq_activate_delayed_work(struct work_struct *work)
992bf4ede01STejun Heo {
993112202d9STejun Heo 	struct pool_workqueue *pwq = get_work_pwq(work);
994bf4ede01STejun Heo 
995bf4ede01STejun Heo 	trace_workqueue_activate_work(work);
996112202d9STejun Heo 	move_linked_works(work, &pwq->pool->worklist, NULL);
997bf4ede01STejun Heo 	__clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
998112202d9STejun Heo 	pwq->nr_active++;
999bf4ede01STejun Heo }
1000bf4ede01STejun Heo 
1001112202d9STejun Heo static void pwq_activate_first_delayed(struct pool_workqueue *pwq)
10023aa62497SLai Jiangshan {
1003112202d9STejun Heo 	struct work_struct *work = list_first_entry(&pwq->delayed_works,
10043aa62497SLai Jiangshan 						    struct work_struct, entry);
10053aa62497SLai Jiangshan 
1006112202d9STejun Heo 	pwq_activate_delayed_work(work);
10073aa62497SLai Jiangshan }
10083aa62497SLai Jiangshan 
1009bf4ede01STejun Heo /**
1010112202d9STejun Heo  * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight
1011112202d9STejun Heo  * @pwq: pwq of interest
1012bf4ede01STejun Heo  * @color: color of work which left the queue
1013bf4ede01STejun Heo  *
1014bf4ede01STejun Heo  * A work either has completed or is removed from pending queue,
1015112202d9STejun Heo  * decrement nr_in_flight of its pwq and handle workqueue flushing.
1016bf4ede01STejun Heo  *
1017bf4ede01STejun Heo  * CONTEXT:
1018d565ed63STejun Heo  * spin_lock_irq(pool->lock).
1019bf4ede01STejun Heo  */
1020112202d9STejun Heo static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color)
1021bf4ede01STejun Heo {
1022*8864b4e5STejun Heo 	/* uncolored work items don't participate in flushing or nr_active */
1023bf4ede01STejun Heo 	if (color == WORK_NO_COLOR)
1024*8864b4e5STejun Heo 		goto out_put;
1025bf4ede01STejun Heo 
1026112202d9STejun Heo 	pwq->nr_in_flight[color]--;
1027bf4ede01STejun Heo 
1028112202d9STejun Heo 	pwq->nr_active--;
1029112202d9STejun Heo 	if (!list_empty(&pwq->delayed_works)) {
1030bf4ede01STejun Heo 		/* one down, submit a delayed one */
1031112202d9STejun Heo 		if (pwq->nr_active < pwq->max_active)
1032112202d9STejun Heo 			pwq_activate_first_delayed(pwq);
1033bf4ede01STejun Heo 	}
1034bf4ede01STejun Heo 
1035bf4ede01STejun Heo 	/* is flush in progress and are we at the flushing tip? */
1036112202d9STejun Heo 	if (likely(pwq->flush_color != color))
1037*8864b4e5STejun Heo 		goto out_put;
1038bf4ede01STejun Heo 
1039bf4ede01STejun Heo 	/* are there still in-flight works? */
1040112202d9STejun Heo 	if (pwq->nr_in_flight[color])
1041*8864b4e5STejun Heo 		goto out_put;
1042bf4ede01STejun Heo 
1043112202d9STejun Heo 	/* this pwq is done, clear flush_color */
1044112202d9STejun Heo 	pwq->flush_color = -1;
1045bf4ede01STejun Heo 
1046bf4ede01STejun Heo 	/*
1047112202d9STejun Heo 	 * If this was the last pwq, wake up the first flusher.  It
1048bf4ede01STejun Heo 	 * will handle the rest.
1049bf4ede01STejun Heo 	 */
1050112202d9STejun Heo 	if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))
1051112202d9STejun Heo 		complete(&pwq->wq->first_flusher->done);
1052*8864b4e5STejun Heo out_put:
1053*8864b4e5STejun Heo 	put_pwq(pwq);
1054bf4ede01STejun Heo }
1055bf4ede01STejun Heo 
105636e227d2STejun Heo /**
1057bbb68dfaSTejun Heo  * try_to_grab_pending - steal work item from worklist and disable irq
105836e227d2STejun Heo  * @work: work item to steal
105936e227d2STejun Heo  * @is_dwork: @work is a delayed_work
1060bbb68dfaSTejun Heo  * @flags: place to store irq state
106136e227d2STejun Heo  *
106236e227d2STejun Heo  * Try to grab PENDING bit of @work.  This function can handle @work in any
106336e227d2STejun Heo  * stable state - idle, on timer or on worklist.  Return values are
106436e227d2STejun Heo  *
106536e227d2STejun Heo  *  1		if @work was pending and we successfully stole PENDING
106636e227d2STejun Heo  *  0		if @work was idle and we claimed PENDING
106736e227d2STejun Heo  *  -EAGAIN	if PENDING couldn't be grabbed at the moment, safe to busy-retry
1068bbb68dfaSTejun Heo  *  -ENOENT	if someone else is canceling @work, this state may persist
1069bbb68dfaSTejun Heo  *		for arbitrarily long
107036e227d2STejun Heo  *
1071bbb68dfaSTejun Heo  * On >= 0 return, the caller owns @work's PENDING bit.  To avoid getting
1072e0aecdd8STejun Heo  * interrupted while holding PENDING and @work off queue, irq must be
1073e0aecdd8STejun Heo  * disabled on entry.  This, combined with delayed_work->timer being
1074e0aecdd8STejun Heo  * irqsafe, ensures that we return -EAGAIN for finite short period of time.
1075bbb68dfaSTejun Heo  *
1076bbb68dfaSTejun Heo  * On successful return, >= 0, irq is disabled and the caller is
1077bbb68dfaSTejun Heo  * responsible for releasing it using local_irq_restore(*@flags).
1078bbb68dfaSTejun Heo  *
1079e0aecdd8STejun Heo  * This function is safe to call from any context including IRQ handler.
1080bf4ede01STejun Heo  */
1081bbb68dfaSTejun Heo static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1082bbb68dfaSTejun Heo 			       unsigned long *flags)
1083bf4ede01STejun Heo {
1084d565ed63STejun Heo 	struct worker_pool *pool;
1085112202d9STejun Heo 	struct pool_workqueue *pwq;
1086bf4ede01STejun Heo 
1087bbb68dfaSTejun Heo 	local_irq_save(*flags);
1088bbb68dfaSTejun Heo 
108936e227d2STejun Heo 	/* try to steal the timer if it exists */
109036e227d2STejun Heo 	if (is_dwork) {
109136e227d2STejun Heo 		struct delayed_work *dwork = to_delayed_work(work);
109236e227d2STejun Heo 
1093e0aecdd8STejun Heo 		/*
1094e0aecdd8STejun Heo 		 * dwork->timer is irqsafe.  If del_timer() fails, it's
1095e0aecdd8STejun Heo 		 * guaranteed that the timer is not queued anywhere and not
1096e0aecdd8STejun Heo 		 * running on the local CPU.
1097e0aecdd8STejun Heo 		 */
109836e227d2STejun Heo 		if (likely(del_timer(&dwork->timer)))
109936e227d2STejun Heo 			return 1;
110036e227d2STejun Heo 	}
110136e227d2STejun Heo 
110236e227d2STejun Heo 	/* try to claim PENDING the normal way */
1103bf4ede01STejun Heo 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1104bf4ede01STejun Heo 		return 0;
1105bf4ede01STejun Heo 
1106bf4ede01STejun Heo 	/*
1107bf4ede01STejun Heo 	 * The queueing is in progress, or it is already queued. Try to
1108bf4ede01STejun Heo 	 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1109bf4ede01STejun Heo 	 */
1110d565ed63STejun Heo 	pool = get_work_pool(work);
1111d565ed63STejun Heo 	if (!pool)
1112bbb68dfaSTejun Heo 		goto fail;
1113bf4ede01STejun Heo 
1114d565ed63STejun Heo 	spin_lock(&pool->lock);
1115bf4ede01STejun Heo 	/*
1116112202d9STejun Heo 	 * work->data is guaranteed to point to pwq only while the work
1117112202d9STejun Heo 	 * item is queued on pwq->wq, and both updating work->data to point
1118112202d9STejun Heo 	 * to pwq on queueing and to pool on dequeueing are done under
1119112202d9STejun Heo 	 * pwq->pool->lock.  This in turn guarantees that, if work->data
1120112202d9STejun Heo 	 * points to pwq which is associated with a locked pool, the work
11210b3dae68SLai Jiangshan 	 * item is currently queued on that pool.
1122bf4ede01STejun Heo 	 */
1123112202d9STejun Heo 	pwq = get_work_pwq(work);
1124112202d9STejun Heo 	if (pwq && pwq->pool == pool) {
1125bf4ede01STejun Heo 		debug_work_deactivate(work);
11263aa62497SLai Jiangshan 
11273aa62497SLai Jiangshan 		/*
112816062836STejun Heo 		 * A delayed work item cannot be grabbed directly because
112916062836STejun Heo 		 * it might have linked NO_COLOR work items which, if left
1130112202d9STejun Heo 		 * on the delayed_list, will confuse pwq->nr_active
113116062836STejun Heo 		 * management later on and cause stall.  Make sure the work
113216062836STejun Heo 		 * item is activated before grabbing.
11333aa62497SLai Jiangshan 		 */
11343aa62497SLai Jiangshan 		if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
1135112202d9STejun Heo 			pwq_activate_delayed_work(work);
11363aa62497SLai Jiangshan 
1137bf4ede01STejun Heo 		list_del_init(&work->entry);
1138112202d9STejun Heo 		pwq_dec_nr_in_flight(get_work_pwq(work), get_work_color(work));
113936e227d2STejun Heo 
1140112202d9STejun Heo 		/* work->data points to pwq iff queued, point to pool */
11414468a00fSLai Jiangshan 		set_work_pool_and_keep_pending(work, pool->id);
11424468a00fSLai Jiangshan 
1143d565ed63STejun Heo 		spin_unlock(&pool->lock);
114436e227d2STejun Heo 		return 1;
1145bf4ede01STejun Heo 	}
1146d565ed63STejun Heo 	spin_unlock(&pool->lock);
1147bbb68dfaSTejun Heo fail:
1148bbb68dfaSTejun Heo 	local_irq_restore(*flags);
1149bbb68dfaSTejun Heo 	if (work_is_canceling(work))
1150bbb68dfaSTejun Heo 		return -ENOENT;
1151bbb68dfaSTejun Heo 	cpu_relax();
115236e227d2STejun Heo 	return -EAGAIN;
1153bf4ede01STejun Heo }
1154bf4ede01STejun Heo 
1155bf4ede01STejun Heo /**
1156706026c2STejun Heo  * insert_work - insert a work into a pool
1157112202d9STejun Heo  * @pwq: pwq @work belongs to
11584690c4abSTejun Heo  * @work: work to insert
11594690c4abSTejun Heo  * @head: insertion point
11604690c4abSTejun Heo  * @extra_flags: extra WORK_STRUCT_* flags to set
11614690c4abSTejun Heo  *
1162112202d9STejun Heo  * Insert @work which belongs to @pwq after @head.  @extra_flags is or'd to
1163706026c2STejun Heo  * work_struct flags.
11644690c4abSTejun Heo  *
11654690c4abSTejun Heo  * CONTEXT:
1166d565ed63STejun Heo  * spin_lock_irq(pool->lock).
1167365970a1SDavid Howells  */
1168112202d9STejun Heo static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
1169112202d9STejun Heo 			struct list_head *head, unsigned int extra_flags)
1170b89deed3SOleg Nesterov {
1171112202d9STejun Heo 	struct worker_pool *pool = pwq->pool;
1172e1d8aa9fSFrederic Weisbecker 
11734690c4abSTejun Heo 	/* we own @work, set data and link */
1174112202d9STejun Heo 	set_work_pwq(work, pwq, extra_flags);
11751a4d9b0aSOleg Nesterov 	list_add_tail(&work->entry, head);
1176*8864b4e5STejun Heo 	get_pwq(pwq);
1177e22bee78STejun Heo 
1178e22bee78STejun Heo 	/*
1179e22bee78STejun Heo 	 * Ensure either worker_sched_deactivated() sees the above
1180e22bee78STejun Heo 	 * list_add_tail() or we see zero nr_running to avoid workers
1181e22bee78STejun Heo 	 * lying around lazily while there are works to be processed.
1182e22bee78STejun Heo 	 */
1183e22bee78STejun Heo 	smp_mb();
1184e22bee78STejun Heo 
118563d95a91STejun Heo 	if (__need_more_worker(pool))
118663d95a91STejun Heo 		wake_up_worker(pool);
1187b89deed3SOleg Nesterov }
1188b89deed3SOleg Nesterov 
1189c8efcc25STejun Heo /*
1190c8efcc25STejun Heo  * Test whether @work is being queued from another work executing on the
11918d03ecfeSTejun Heo  * same workqueue.
1192c8efcc25STejun Heo  */
1193c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq)
1194c8efcc25STejun Heo {
1195c8efcc25STejun Heo 	struct worker *worker;
1196c8efcc25STejun Heo 
11978d03ecfeSTejun Heo 	worker = current_wq_worker();
1198c8efcc25STejun Heo 	/*
11998d03ecfeSTejun Heo 	 * Return %true iff I'm a worker execuing a work item on @wq.  If
12008d03ecfeSTejun Heo 	 * I'm @worker, it's safe to dereference it without locking.
1201c8efcc25STejun Heo 	 */
1202112202d9STejun Heo 	return worker && worker->current_pwq->wq == wq;
1203c8efcc25STejun Heo }
1204c8efcc25STejun Heo 
1205d84ff051STejun Heo static void __queue_work(int cpu, struct workqueue_struct *wq,
12061da177e4SLinus Torvalds 			 struct work_struct *work)
12071da177e4SLinus Torvalds {
1208112202d9STejun Heo 	struct pool_workqueue *pwq;
12091e19ffc6STejun Heo 	struct list_head *worklist;
12108a2e8e5dSTejun Heo 	unsigned int work_flags;
1211b75cac93SJoonsoo Kim 	unsigned int req_cpu = cpu;
12128930cabaSTejun Heo 
12138930cabaSTejun Heo 	/*
12148930cabaSTejun Heo 	 * While a work item is PENDING && off queue, a task trying to
12158930cabaSTejun Heo 	 * steal the PENDING will busy-loop waiting for it to either get
12168930cabaSTejun Heo 	 * queued or lose PENDING.  Grabbing PENDING and queueing should
12178930cabaSTejun Heo 	 * happen with IRQ disabled.
12188930cabaSTejun Heo 	 */
12198930cabaSTejun Heo 	WARN_ON_ONCE(!irqs_disabled());
12201da177e4SLinus Torvalds 
1221dc186ad7SThomas Gleixner 	debug_work_activate(work);
12221e19ffc6STejun Heo 
1223c8efcc25STejun Heo 	/* if dying, only works from the same workqueue are allowed */
12249c5a2ba7STejun Heo 	if (unlikely(wq->flags & WQ_DRAINING) &&
1225c8efcc25STejun Heo 	    WARN_ON_ONCE(!is_chained_work(wq)))
1226e41e704bSTejun Heo 		return;
1227e41e704bSTejun Heo 
1228112202d9STejun Heo 	/* determine the pwq to use */
1229c7fc77f7STejun Heo 	if (!(wq->flags & WQ_UNBOUND)) {
1230c9e7cf27STejun Heo 		struct worker_pool *last_pool;
1231c7fc77f7STejun Heo 
123257469821STejun Heo 		if (cpu == WORK_CPU_UNBOUND)
1233f3421797STejun Heo 			cpu = raw_smp_processor_id();
1234f3421797STejun Heo 
123518aa9effSTejun Heo 		/*
1236dbf2576eSTejun Heo 		 * It's multi cpu.  If @work was previously on a different
1237dbf2576eSTejun Heo 		 * cpu, it might still be running there, in which case the
1238dbf2576eSTejun Heo 		 * work needs to be queued on that cpu to guarantee
1239dbf2576eSTejun Heo 		 * non-reentrancy.
124018aa9effSTejun Heo 		 */
12417fb98ea7STejun Heo 		pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
1242c9e7cf27STejun Heo 		last_pool = get_work_pool(work);
1243dbf2576eSTejun Heo 
1244112202d9STejun Heo 		if (last_pool && last_pool != pwq->pool) {
124518aa9effSTejun Heo 			struct worker *worker;
124618aa9effSTejun Heo 
1247d565ed63STejun Heo 			spin_lock(&last_pool->lock);
124818aa9effSTejun Heo 
1249c9e7cf27STejun Heo 			worker = find_worker_executing_work(last_pool, work);
125018aa9effSTejun Heo 
1251112202d9STejun Heo 			if (worker && worker->current_pwq->wq == wq) {
12527fb98ea7STejun Heo 				pwq = per_cpu_ptr(wq->cpu_pwqs, last_pool->cpu);
12538594fadeSLai Jiangshan 			} else {
125418aa9effSTejun Heo 				/* meh... not running there, queue here */
1255d565ed63STejun Heo 				spin_unlock(&last_pool->lock);
1256112202d9STejun Heo 				spin_lock(&pwq->pool->lock);
125718aa9effSTejun Heo 			}
12588930cabaSTejun Heo 		} else {
1259112202d9STejun Heo 			spin_lock(&pwq->pool->lock);
12608930cabaSTejun Heo 		}
1261f3421797STejun Heo 	} else {
12627fb98ea7STejun Heo 		pwq = first_pwq(wq);
1263112202d9STejun Heo 		spin_lock(&pwq->pool->lock);
1264502ca9d8STejun Heo 	}
1265502ca9d8STejun Heo 
1266112202d9STejun Heo 	/* pwq determined, queue */
1267112202d9STejun Heo 	trace_workqueue_queue_work(req_cpu, pwq, work);
1268502ca9d8STejun Heo 
1269f5b2552bSDan Carpenter 	if (WARN_ON(!list_empty(&work->entry))) {
1270112202d9STejun Heo 		spin_unlock(&pwq->pool->lock);
1271f5b2552bSDan Carpenter 		return;
1272f5b2552bSDan Carpenter 	}
12731e19ffc6STejun Heo 
1274112202d9STejun Heo 	pwq->nr_in_flight[pwq->work_color]++;
1275112202d9STejun Heo 	work_flags = work_color_to_flags(pwq->work_color);
12761e19ffc6STejun Heo 
1277112202d9STejun Heo 	if (likely(pwq->nr_active < pwq->max_active)) {
1278cdadf009STejun Heo 		trace_workqueue_activate_work(work);
1279112202d9STejun Heo 		pwq->nr_active++;
1280112202d9STejun Heo 		worklist = &pwq->pool->worklist;
12818a2e8e5dSTejun Heo 	} else {
12828a2e8e5dSTejun Heo 		work_flags |= WORK_STRUCT_DELAYED;
1283112202d9STejun Heo 		worklist = &pwq->delayed_works;
12848a2e8e5dSTejun Heo 	}
12851e19ffc6STejun Heo 
1286112202d9STejun Heo 	insert_work(pwq, work, worklist, work_flags);
12871e19ffc6STejun Heo 
1288112202d9STejun Heo 	spin_unlock(&pwq->pool->lock);
12891da177e4SLinus Torvalds }
12901da177e4SLinus Torvalds 
12910fcb78c2SRolf Eike Beer /**
1292c1a220e7SZhang Rui  * queue_work_on - queue work on specific cpu
1293c1a220e7SZhang Rui  * @cpu: CPU number to execute work on
1294c1a220e7SZhang Rui  * @wq: workqueue to use
1295c1a220e7SZhang Rui  * @work: work to queue
1296c1a220e7SZhang Rui  *
1297d4283e93STejun Heo  * Returns %false if @work was already on a queue, %true otherwise.
1298c1a220e7SZhang Rui  *
1299c1a220e7SZhang Rui  * We queue the work to a specific CPU, the caller must ensure it
1300c1a220e7SZhang Rui  * can't go away.
1301c1a220e7SZhang Rui  */
1302d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq,
1303d4283e93STejun Heo 		   struct work_struct *work)
1304c1a220e7SZhang Rui {
1305d4283e93STejun Heo 	bool ret = false;
13068930cabaSTejun Heo 	unsigned long flags;
13078930cabaSTejun Heo 
13088930cabaSTejun Heo 	local_irq_save(flags);
1309c1a220e7SZhang Rui 
131022df02bbSTejun Heo 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
13114690c4abSTejun Heo 		__queue_work(cpu, wq, work);
1312d4283e93STejun Heo 		ret = true;
1313c1a220e7SZhang Rui 	}
13148930cabaSTejun Heo 
13158930cabaSTejun Heo 	local_irq_restore(flags);
1316c1a220e7SZhang Rui 	return ret;
1317c1a220e7SZhang Rui }
1318c1a220e7SZhang Rui EXPORT_SYMBOL_GPL(queue_work_on);
1319c1a220e7SZhang Rui 
13200a13c00eSTejun Heo /**
13210a13c00eSTejun Heo  * queue_work - queue work on a workqueue
13220a13c00eSTejun Heo  * @wq: workqueue to use
13230a13c00eSTejun Heo  * @work: work to queue
13240a13c00eSTejun Heo  *
1325d4283e93STejun Heo  * Returns %false if @work was already on a queue, %true otherwise.
13260a13c00eSTejun Heo  *
13270a13c00eSTejun Heo  * We queue the work to the CPU on which it was submitted, but if the CPU dies
13280a13c00eSTejun Heo  * it can be processed by another CPU.
13290a13c00eSTejun Heo  */
1330d4283e93STejun Heo bool queue_work(struct workqueue_struct *wq, struct work_struct *work)
13310a13c00eSTejun Heo {
133257469821STejun Heo 	return queue_work_on(WORK_CPU_UNBOUND, wq, work);
13330a13c00eSTejun Heo }
13340a13c00eSTejun Heo EXPORT_SYMBOL_GPL(queue_work);
13350a13c00eSTejun Heo 
1336d8e794dfSTejun Heo void delayed_work_timer_fn(unsigned long __data)
13371da177e4SLinus Torvalds {
133852bad64dSDavid Howells 	struct delayed_work *dwork = (struct delayed_work *)__data;
13391da177e4SLinus Torvalds 
1340e0aecdd8STejun Heo 	/* should have been called from irqsafe timer with irq already off */
134160c057bcSLai Jiangshan 	__queue_work(dwork->cpu, dwork->wq, &dwork->work);
13421da177e4SLinus Torvalds }
13431438ade5SKonstantin Khlebnikov EXPORT_SYMBOL(delayed_work_timer_fn);
13441da177e4SLinus Torvalds 
13457beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
134652bad64dSDavid Howells 				struct delayed_work *dwork, unsigned long delay)
13471da177e4SLinus Torvalds {
13487beb2edfSTejun Heo 	struct timer_list *timer = &dwork->timer;
13497beb2edfSTejun Heo 	struct work_struct *work = &dwork->work;
13501da177e4SLinus Torvalds 
13517beb2edfSTejun Heo 	WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
13527beb2edfSTejun Heo 		     timer->data != (unsigned long)dwork);
1353fc4b514fSTejun Heo 	WARN_ON_ONCE(timer_pending(timer));
1354fc4b514fSTejun Heo 	WARN_ON_ONCE(!list_empty(&work->entry));
13557beb2edfSTejun Heo 
13568852aac2STejun Heo 	/*
13578852aac2STejun Heo 	 * If @delay is 0, queue @dwork->work immediately.  This is for
13588852aac2STejun Heo 	 * both optimization and correctness.  The earliest @timer can
13598852aac2STejun Heo 	 * expire is on the closest next tick and delayed_work users depend
13608852aac2STejun Heo 	 * on that there's no such delay when @delay is 0.
13618852aac2STejun Heo 	 */
13628852aac2STejun Heo 	if (!delay) {
13638852aac2STejun Heo 		__queue_work(cpu, wq, &dwork->work);
13648852aac2STejun Heo 		return;
13658852aac2STejun Heo 	}
13668852aac2STejun Heo 
13677beb2edfSTejun Heo 	timer_stats_timer_set_start_info(&dwork->timer);
13687beb2edfSTejun Heo 
136960c057bcSLai Jiangshan 	dwork->wq = wq;
13701265057fSTejun Heo 	dwork->cpu = cpu;
13717beb2edfSTejun Heo 	timer->expires = jiffies + delay;
13727beb2edfSTejun Heo 
13737beb2edfSTejun Heo 	if (unlikely(cpu != WORK_CPU_UNBOUND))
13747beb2edfSTejun Heo 		add_timer_on(timer, cpu);
13757beb2edfSTejun Heo 	else
13767beb2edfSTejun Heo 		add_timer(timer);
13777beb2edfSTejun Heo }
13781da177e4SLinus Torvalds 
13790fcb78c2SRolf Eike Beer /**
13800fcb78c2SRolf Eike Beer  * queue_delayed_work_on - queue work on specific CPU after delay
13810fcb78c2SRolf Eike Beer  * @cpu: CPU number to execute work on
13820fcb78c2SRolf Eike Beer  * @wq: workqueue to use
1383af9997e4SRandy Dunlap  * @dwork: work to queue
13840fcb78c2SRolf Eike Beer  * @delay: number of jiffies to wait before queueing
13850fcb78c2SRolf Eike Beer  *
1386715f1300STejun Heo  * Returns %false if @work was already on a queue, %true otherwise.  If
1387715f1300STejun Heo  * @delay is zero and @dwork is idle, it will be scheduled for immediate
1388715f1300STejun Heo  * execution.
13890fcb78c2SRolf Eike Beer  */
1390d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
139152bad64dSDavid Howells 			   struct delayed_work *dwork, unsigned long delay)
13927a6bc1cdSVenkatesh Pallipadi {
139352bad64dSDavid Howells 	struct work_struct *work = &dwork->work;
1394d4283e93STejun Heo 	bool ret = false;
13958930cabaSTejun Heo 	unsigned long flags;
13968930cabaSTejun Heo 
13978930cabaSTejun Heo 	/* read the comment in __queue_work() */
13988930cabaSTejun Heo 	local_irq_save(flags);
13997a6bc1cdSVenkatesh Pallipadi 
140022df02bbSTejun Heo 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
14017beb2edfSTejun Heo 		__queue_delayed_work(cpu, wq, dwork, delay);
1402d4283e93STejun Heo 		ret = true;
14037a6bc1cdSVenkatesh Pallipadi 	}
14048930cabaSTejun Heo 
14058930cabaSTejun Heo 	local_irq_restore(flags);
14067a6bc1cdSVenkatesh Pallipadi 	return ret;
14077a6bc1cdSVenkatesh Pallipadi }
1408ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work_on);
14091da177e4SLinus Torvalds 
1410c8e55f36STejun Heo /**
14110a13c00eSTejun Heo  * queue_delayed_work - queue work on a workqueue after delay
14120a13c00eSTejun Heo  * @wq: workqueue to use
14130a13c00eSTejun Heo  * @dwork: delayable work to queue
14140a13c00eSTejun Heo  * @delay: number of jiffies to wait before queueing
14150a13c00eSTejun Heo  *
1416715f1300STejun Heo  * Equivalent to queue_delayed_work_on() but tries to use the local CPU.
14170a13c00eSTejun Heo  */
1418d4283e93STejun Heo bool queue_delayed_work(struct workqueue_struct *wq,
14190a13c00eSTejun Heo 			struct delayed_work *dwork, unsigned long delay)
14200a13c00eSTejun Heo {
142157469821STejun Heo 	return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
14220a13c00eSTejun Heo }
14230a13c00eSTejun Heo EXPORT_SYMBOL_GPL(queue_delayed_work);
14240a13c00eSTejun Heo 
14250a13c00eSTejun Heo /**
14268376fe22STejun Heo  * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
14278376fe22STejun Heo  * @cpu: CPU number to execute work on
14288376fe22STejun Heo  * @wq: workqueue to use
14298376fe22STejun Heo  * @dwork: work to queue
14308376fe22STejun Heo  * @delay: number of jiffies to wait before queueing
14318376fe22STejun Heo  *
14328376fe22STejun Heo  * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
14338376fe22STejun Heo  * modify @dwork's timer so that it expires after @delay.  If @delay is
14348376fe22STejun Heo  * zero, @work is guaranteed to be scheduled immediately regardless of its
14358376fe22STejun Heo  * current state.
14368376fe22STejun Heo  *
14378376fe22STejun Heo  * Returns %false if @dwork was idle and queued, %true if @dwork was
14388376fe22STejun Heo  * pending and its timer was modified.
14398376fe22STejun Heo  *
1440e0aecdd8STejun Heo  * This function is safe to call from any context including IRQ handler.
14418376fe22STejun Heo  * See try_to_grab_pending() for details.
14428376fe22STejun Heo  */
14438376fe22STejun Heo bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
14448376fe22STejun Heo 			 struct delayed_work *dwork, unsigned long delay)
14458376fe22STejun Heo {
14468376fe22STejun Heo 	unsigned long flags;
14478376fe22STejun Heo 	int ret;
14488376fe22STejun Heo 
14498376fe22STejun Heo 	do {
14508376fe22STejun Heo 		ret = try_to_grab_pending(&dwork->work, true, &flags);
14518376fe22STejun Heo 	} while (unlikely(ret == -EAGAIN));
14528376fe22STejun Heo 
14538376fe22STejun Heo 	if (likely(ret >= 0)) {
14548376fe22STejun Heo 		__queue_delayed_work(cpu, wq, dwork, delay);
14558376fe22STejun Heo 		local_irq_restore(flags);
14568376fe22STejun Heo 	}
14578376fe22STejun Heo 
14588376fe22STejun Heo 	/* -ENOENT from try_to_grab_pending() becomes %true */
14598376fe22STejun Heo 	return ret;
14608376fe22STejun Heo }
14618376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work_on);
14628376fe22STejun Heo 
14638376fe22STejun Heo /**
14648376fe22STejun Heo  * mod_delayed_work - modify delay of or queue a delayed work
14658376fe22STejun Heo  * @wq: workqueue to use
14668376fe22STejun Heo  * @dwork: work to queue
14678376fe22STejun Heo  * @delay: number of jiffies to wait before queueing
14688376fe22STejun Heo  *
14698376fe22STejun Heo  * mod_delayed_work_on() on local CPU.
14708376fe22STejun Heo  */
14718376fe22STejun Heo bool mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dwork,
14728376fe22STejun Heo 		      unsigned long delay)
14738376fe22STejun Heo {
14748376fe22STejun Heo 	return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
14758376fe22STejun Heo }
14768376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work);
14778376fe22STejun Heo 
14788376fe22STejun Heo /**
1479c8e55f36STejun Heo  * worker_enter_idle - enter idle state
1480c8e55f36STejun Heo  * @worker: worker which is entering idle state
1481c8e55f36STejun Heo  *
1482c8e55f36STejun Heo  * @worker is entering idle state.  Update stats and idle timer if
1483c8e55f36STejun Heo  * necessary.
1484c8e55f36STejun Heo  *
1485c8e55f36STejun Heo  * LOCKING:
1486d565ed63STejun Heo  * spin_lock_irq(pool->lock).
1487c8e55f36STejun Heo  */
1488c8e55f36STejun Heo static void worker_enter_idle(struct worker *worker)
14891da177e4SLinus Torvalds {
1490bd7bdd43STejun Heo 	struct worker_pool *pool = worker->pool;
1491c8e55f36STejun Heo 
14926183c009STejun Heo 	if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||
14936183c009STejun Heo 	    WARN_ON_ONCE(!list_empty(&worker->entry) &&
14946183c009STejun Heo 			 (worker->hentry.next || worker->hentry.pprev)))
14956183c009STejun Heo 		return;
1496c8e55f36STejun Heo 
1497cb444766STejun Heo 	/* can't use worker_set_flags(), also called from start_worker() */
1498cb444766STejun Heo 	worker->flags |= WORKER_IDLE;
1499bd7bdd43STejun Heo 	pool->nr_idle++;
1500e22bee78STejun Heo 	worker->last_active = jiffies;
1501c8e55f36STejun Heo 
1502c8e55f36STejun Heo 	/* idle_list is LIFO */
1503bd7bdd43STejun Heo 	list_add(&worker->entry, &pool->idle_list);
1504db7bccf4STejun Heo 
150563d95a91STejun Heo 	if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1506628c78e7STejun Heo 		mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
1507cb444766STejun Heo 
1508544ecf31STejun Heo 	/*
1509706026c2STejun Heo 	 * Sanity check nr_running.  Because wq_unbind_fn() releases
1510d565ed63STejun Heo 	 * pool->lock between setting %WORKER_UNBOUND and zapping
1511628c78e7STejun Heo 	 * nr_running, the warning may trigger spuriously.  Check iff
1512628c78e7STejun Heo 	 * unbind is not in progress.
1513544ecf31STejun Heo 	 */
151424647570STejun Heo 	WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
1515bd7bdd43STejun Heo 		     pool->nr_workers == pool->nr_idle &&
1516e19e397aSTejun Heo 		     atomic_read(&pool->nr_running));
1517c8e55f36STejun Heo }
1518c8e55f36STejun Heo 
1519c8e55f36STejun Heo /**
1520c8e55f36STejun Heo  * worker_leave_idle - leave idle state
1521c8e55f36STejun Heo  * @worker: worker which is leaving idle state
1522c8e55f36STejun Heo  *
1523c8e55f36STejun Heo  * @worker is leaving idle state.  Update stats.
1524c8e55f36STejun Heo  *
1525c8e55f36STejun Heo  * LOCKING:
1526d565ed63STejun Heo  * spin_lock_irq(pool->lock).
1527c8e55f36STejun Heo  */
1528c8e55f36STejun Heo static void worker_leave_idle(struct worker *worker)
1529c8e55f36STejun Heo {
1530bd7bdd43STejun Heo 	struct worker_pool *pool = worker->pool;
1531c8e55f36STejun Heo 
15326183c009STejun Heo 	if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))
15336183c009STejun Heo 		return;
1534d302f017STejun Heo 	worker_clr_flags(worker, WORKER_IDLE);
1535bd7bdd43STejun Heo 	pool->nr_idle--;
1536c8e55f36STejun Heo 	list_del_init(&worker->entry);
1537c8e55f36STejun Heo }
1538c8e55f36STejun Heo 
1539e22bee78STejun Heo /**
1540f36dc67bSLai Jiangshan  * worker_maybe_bind_and_lock - try to bind %current to worker_pool and lock it
1541f36dc67bSLai Jiangshan  * @pool: target worker_pool
1542f36dc67bSLai Jiangshan  *
1543f36dc67bSLai Jiangshan  * Bind %current to the cpu of @pool if it is associated and lock @pool.
1544e22bee78STejun Heo  *
1545e22bee78STejun Heo  * Works which are scheduled while the cpu is online must at least be
1546e22bee78STejun Heo  * scheduled to a worker which is bound to the cpu so that if they are
1547e22bee78STejun Heo  * flushed from cpu callbacks while cpu is going down, they are
1548e22bee78STejun Heo  * guaranteed to execute on the cpu.
1549e22bee78STejun Heo  *
1550f5faa077SLai Jiangshan  * This function is to be used by unbound workers and rescuers to bind
1551e22bee78STejun Heo  * themselves to the target cpu and may race with cpu going down or
1552e22bee78STejun Heo  * coming online.  kthread_bind() can't be used because it may put the
1553e22bee78STejun Heo  * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1554706026c2STejun Heo  * verbatim as it's best effort and blocking and pool may be
1555e22bee78STejun Heo  * [dis]associated in the meantime.
1556e22bee78STejun Heo  *
1557706026c2STejun Heo  * This function tries set_cpus_allowed() and locks pool and verifies the
155824647570STejun Heo  * binding against %POOL_DISASSOCIATED which is set during
1559f2d5a0eeSTejun Heo  * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1560f2d5a0eeSTejun Heo  * enters idle state or fetches works without dropping lock, it can
1561f2d5a0eeSTejun Heo  * guarantee the scheduling requirement described in the first paragraph.
1562e22bee78STejun Heo  *
1563e22bee78STejun Heo  * CONTEXT:
1564d565ed63STejun Heo  * Might sleep.  Called without any lock but returns with pool->lock
1565e22bee78STejun Heo  * held.
1566e22bee78STejun Heo  *
1567e22bee78STejun Heo  * RETURNS:
1568706026c2STejun Heo  * %true if the associated pool is online (@worker is successfully
1569e22bee78STejun Heo  * bound), %false if offline.
1570e22bee78STejun Heo  */
1571f36dc67bSLai Jiangshan static bool worker_maybe_bind_and_lock(struct worker_pool *pool)
1572d565ed63STejun Heo __acquires(&pool->lock)
1573e22bee78STejun Heo {
1574e22bee78STejun Heo 	while (true) {
1575e22bee78STejun Heo 		/*
1576e22bee78STejun Heo 		 * The following call may fail, succeed or succeed
1577e22bee78STejun Heo 		 * without actually migrating the task to the cpu if
1578e22bee78STejun Heo 		 * it races with cpu hotunplug operation.  Verify
157924647570STejun Heo 		 * against POOL_DISASSOCIATED.
1580e22bee78STejun Heo 		 */
158124647570STejun Heo 		if (!(pool->flags & POOL_DISASSOCIATED))
15827a4e344cSTejun Heo 			set_cpus_allowed_ptr(current, pool->attrs->cpumask);
1583e22bee78STejun Heo 
1584d565ed63STejun Heo 		spin_lock_irq(&pool->lock);
158524647570STejun Heo 		if (pool->flags & POOL_DISASSOCIATED)
1586e22bee78STejun Heo 			return false;
1587f5faa077SLai Jiangshan 		if (task_cpu(current) == pool->cpu &&
15887a4e344cSTejun Heo 		    cpumask_equal(&current->cpus_allowed, pool->attrs->cpumask))
1589e22bee78STejun Heo 			return true;
1590d565ed63STejun Heo 		spin_unlock_irq(&pool->lock);
1591e22bee78STejun Heo 
15925035b20fSTejun Heo 		/*
15935035b20fSTejun Heo 		 * We've raced with CPU hot[un]plug.  Give it a breather
15945035b20fSTejun Heo 		 * and retry migration.  cond_resched() is required here;
15955035b20fSTejun Heo 		 * otherwise, we might deadlock against cpu_stop trying to
15965035b20fSTejun Heo 		 * bring down the CPU on non-preemptive kernel.
15975035b20fSTejun Heo 		 */
1598e22bee78STejun Heo 		cpu_relax();
15995035b20fSTejun Heo 		cond_resched();
1600e22bee78STejun Heo 	}
1601e22bee78STejun Heo }
1602e22bee78STejun Heo 
1603e22bee78STejun Heo /*
1604ea1abd61SLai Jiangshan  * Rebind an idle @worker to its CPU.  worker_thread() will test
16055f7dabfdSLai Jiangshan  * list_empty(@worker->entry) before leaving idle and call this function.
160625511a47STejun Heo  */
160725511a47STejun Heo static void idle_worker_rebind(struct worker *worker)
160825511a47STejun Heo {
16095f7dabfdSLai Jiangshan 	/* CPU may go down again inbetween, clear UNBOUND only on success */
1610f36dc67bSLai Jiangshan 	if (worker_maybe_bind_and_lock(worker->pool))
16115f7dabfdSLai Jiangshan 		worker_clr_flags(worker, WORKER_UNBOUND);
161225511a47STejun Heo 
1613ea1abd61SLai Jiangshan 	/* rebind complete, become available again */
1614ea1abd61SLai Jiangshan 	list_add(&worker->entry, &worker->pool->idle_list);
1615d565ed63STejun Heo 	spin_unlock_irq(&worker->pool->lock);
161625511a47STejun Heo }
161725511a47STejun Heo 
161825511a47STejun Heo /*
161925511a47STejun Heo  * Function for @worker->rebind.work used to rebind unbound busy workers to
1620403c821dSTejun Heo  * the associated cpu which is coming back online.  This is scheduled by
1621403c821dSTejun Heo  * cpu up but can race with other cpu hotplug operations and may be
1622403c821dSTejun Heo  * executed twice without intervening cpu down.
1623e22bee78STejun Heo  */
162425511a47STejun Heo static void busy_worker_rebind_fn(struct work_struct *work)
1625e22bee78STejun Heo {
1626e22bee78STejun Heo 	struct worker *worker = container_of(work, struct worker, rebind_work);
1627e22bee78STejun Heo 
1628f36dc67bSLai Jiangshan 	if (worker_maybe_bind_and_lock(worker->pool))
1629eab6d828SLai Jiangshan 		worker_clr_flags(worker, WORKER_UNBOUND);
1630e22bee78STejun Heo 
1631d565ed63STejun Heo 	spin_unlock_irq(&worker->pool->lock);
1632e22bee78STejun Heo }
1633e22bee78STejun Heo 
163425511a47STejun Heo /**
163594cf58bbSTejun Heo  * rebind_workers - rebind all workers of a pool to the associated CPU
163694cf58bbSTejun Heo  * @pool: pool of interest
163725511a47STejun Heo  *
163894cf58bbSTejun Heo  * @pool->cpu is coming online.  Rebind all workers to the CPU.  Rebinding
163925511a47STejun Heo  * is different for idle and busy ones.
164025511a47STejun Heo  *
1641ea1abd61SLai Jiangshan  * Idle ones will be removed from the idle_list and woken up.  They will
1642ea1abd61SLai Jiangshan  * add themselves back after completing rebind.  This ensures that the
1643ea1abd61SLai Jiangshan  * idle_list doesn't contain any unbound workers when re-bound busy workers
1644ea1abd61SLai Jiangshan  * try to perform local wake-ups for concurrency management.
164525511a47STejun Heo  *
1646ea1abd61SLai Jiangshan  * Busy workers can rebind after they finish their current work items.
1647ea1abd61SLai Jiangshan  * Queueing the rebind work item at the head of the scheduled list is
1648ea1abd61SLai Jiangshan  * enough.  Note that nr_running will be properly bumped as busy workers
1649ea1abd61SLai Jiangshan  * rebind.
165025511a47STejun Heo  *
1651ea1abd61SLai Jiangshan  * On return, all non-manager workers are scheduled for rebind - see
1652ea1abd61SLai Jiangshan  * manage_workers() for the manager special case.  Any idle worker
1653ea1abd61SLai Jiangshan  * including the manager will not appear on @idle_list until rebind is
1654ea1abd61SLai Jiangshan  * complete, making local wake-ups safe.
165525511a47STejun Heo  */
165694cf58bbSTejun Heo static void rebind_workers(struct worker_pool *pool)
165725511a47STejun Heo {
1658ea1abd61SLai Jiangshan 	struct worker *worker, *n;
165925511a47STejun Heo 	int i;
166025511a47STejun Heo 
1661b2eb83d1SLai Jiangshan 	lockdep_assert_held(&pool->assoc_mutex);
1662d565ed63STejun Heo 	lockdep_assert_held(&pool->lock);
166325511a47STejun Heo 
16645f7dabfdSLai Jiangshan 	/* dequeue and kick idle ones */
1665ea1abd61SLai Jiangshan 	list_for_each_entry_safe(worker, n, &pool->idle_list, entry) {
1666ea1abd61SLai Jiangshan 		/*
166794cf58bbSTejun Heo 		 * idle workers should be off @pool->idle_list until rebind
166894cf58bbSTejun Heo 		 * is complete to avoid receiving premature local wake-ups.
1669ea1abd61SLai Jiangshan 		 */
1670ea1abd61SLai Jiangshan 		list_del_init(&worker->entry);
167125511a47STejun Heo 
167225511a47STejun Heo 		/*
167394cf58bbSTejun Heo 		 * worker_thread() will see the above dequeuing and call
167494cf58bbSTejun Heo 		 * idle_worker_rebind().
167525511a47STejun Heo 		 */
167625511a47STejun Heo 		wake_up_process(worker->task);
167725511a47STejun Heo 	}
167825511a47STejun Heo 
1679ea1abd61SLai Jiangshan 	/* rebind busy workers */
1680b67bfe0dSSasha Levin 	for_each_busy_worker(worker, i, pool) {
168125511a47STejun Heo 		struct work_struct *rebind_work = &worker->rebind_work;
1682e2b6a6d5SJoonsoo Kim 		struct workqueue_struct *wq;
168325511a47STejun Heo 
168425511a47STejun Heo 		if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
168525511a47STejun Heo 				     work_data_bits(rebind_work)))
168625511a47STejun Heo 			continue;
168725511a47STejun Heo 
168825511a47STejun Heo 		debug_work_activate(rebind_work);
168990beca5dSTejun Heo 
169090beca5dSTejun Heo 		/*
169194cf58bbSTejun Heo 		 * wq doesn't really matter but let's keep @worker->pool
1692112202d9STejun Heo 		 * and @pwq->pool consistent for sanity.
169390beca5dSTejun Heo 		 */
16947a4e344cSTejun Heo 		if (worker->pool->attrs->nice < 0)
1695e2b6a6d5SJoonsoo Kim 			wq = system_highpri_wq;
1696e2b6a6d5SJoonsoo Kim 		else
1697e2b6a6d5SJoonsoo Kim 			wq = system_wq;
1698ec58815aSTejun Heo 
16997fb98ea7STejun Heo 		insert_work(per_cpu_ptr(wq->cpu_pwqs, pool->cpu), rebind_work,
170025511a47STejun Heo 			    worker->scheduled.next,
170125511a47STejun Heo 			    work_color_to_flags(WORK_NO_COLOR));
1702ec58815aSTejun Heo 	}
170325511a47STejun Heo }
170425511a47STejun Heo 
1705c34056a3STejun Heo static struct worker *alloc_worker(void)
1706c34056a3STejun Heo {
1707c34056a3STejun Heo 	struct worker *worker;
1708c34056a3STejun Heo 
1709c34056a3STejun Heo 	worker = kzalloc(sizeof(*worker), GFP_KERNEL);
1710c8e55f36STejun Heo 	if (worker) {
1711c8e55f36STejun Heo 		INIT_LIST_HEAD(&worker->entry);
1712affee4b2STejun Heo 		INIT_LIST_HEAD(&worker->scheduled);
171325511a47STejun Heo 		INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn);
1714e22bee78STejun Heo 		/* on creation a worker is in !idle && prep state */
1715e22bee78STejun Heo 		worker->flags = WORKER_PREP;
1716c8e55f36STejun Heo 	}
1717c34056a3STejun Heo 	return worker;
1718c34056a3STejun Heo }
1719c34056a3STejun Heo 
1720c34056a3STejun Heo /**
1721c34056a3STejun Heo  * create_worker - create a new workqueue worker
172263d95a91STejun Heo  * @pool: pool the new worker will belong to
1723c34056a3STejun Heo  *
172463d95a91STejun Heo  * Create a new worker which is bound to @pool.  The returned worker
1725c34056a3STejun Heo  * can be started by calling start_worker() or destroyed using
1726c34056a3STejun Heo  * destroy_worker().
1727c34056a3STejun Heo  *
1728c34056a3STejun Heo  * CONTEXT:
1729c34056a3STejun Heo  * Might sleep.  Does GFP_KERNEL allocations.
1730c34056a3STejun Heo  *
1731c34056a3STejun Heo  * RETURNS:
1732c34056a3STejun Heo  * Pointer to the newly created worker.
1733c34056a3STejun Heo  */
1734bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool)
1735c34056a3STejun Heo {
17367a4e344cSTejun Heo 	const char *pri = pool->attrs->nice < 0  ? "H" : "";
1737c34056a3STejun Heo 	struct worker *worker = NULL;
1738f3421797STejun Heo 	int id = -1;
1739c34056a3STejun Heo 
1740d565ed63STejun Heo 	spin_lock_irq(&pool->lock);
1741bd7bdd43STejun Heo 	while (ida_get_new(&pool->worker_ida, &id)) {
1742d565ed63STejun Heo 		spin_unlock_irq(&pool->lock);
1743bd7bdd43STejun Heo 		if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
1744c34056a3STejun Heo 			goto fail;
1745d565ed63STejun Heo 		spin_lock_irq(&pool->lock);
1746c34056a3STejun Heo 	}
1747d565ed63STejun Heo 	spin_unlock_irq(&pool->lock);
1748c34056a3STejun Heo 
1749c34056a3STejun Heo 	worker = alloc_worker();
1750c34056a3STejun Heo 	if (!worker)
1751c34056a3STejun Heo 		goto fail;
1752c34056a3STejun Heo 
1753bd7bdd43STejun Heo 	worker->pool = pool;
1754c34056a3STejun Heo 	worker->id = id;
1755c34056a3STejun Heo 
175629c91e99STejun Heo 	if (pool->cpu >= 0)
175794dcf29aSEric Dumazet 		worker->task = kthread_create_on_node(worker_thread,
1758ec22ca5eSTejun Heo 					worker, cpu_to_node(pool->cpu),
1759d84ff051STejun Heo 					"kworker/%d:%d%s", pool->cpu, id, pri);
1760f3421797STejun Heo 	else
1761f3421797STejun Heo 		worker->task = kthread_create(worker_thread, worker,
1762ac6104cdSTejun Heo 					      "kworker/u%d:%d%s",
1763ac6104cdSTejun Heo 					      pool->id, id, pri);
1764c34056a3STejun Heo 	if (IS_ERR(worker->task))
1765c34056a3STejun Heo 		goto fail;
1766c34056a3STejun Heo 
17677a4e344cSTejun Heo 	set_user_nice(worker->task, pool->attrs->nice);
17687a4e344cSTejun Heo 	set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
17693270476aSTejun Heo 
1770db7bccf4STejun Heo 	/*
17717a4e344cSTejun Heo 	 * %PF_THREAD_BOUND is used to prevent userland from meddling with
17727a4e344cSTejun Heo 	 * cpumask of workqueue workers.  This is an abuse.  We need
17737a4e344cSTejun Heo 	 * %PF_NO_SETAFFINITY.
1774db7bccf4STejun Heo 	 */
1775db7bccf4STejun Heo 	worker->task->flags |= PF_THREAD_BOUND;
17767a4e344cSTejun Heo 
17777a4e344cSTejun Heo 	/*
17787a4e344cSTejun Heo 	 * The caller is responsible for ensuring %POOL_DISASSOCIATED
17797a4e344cSTejun Heo 	 * remains stable across this function.  See the comments above the
17807a4e344cSTejun Heo 	 * flag definition for details.
17817a4e344cSTejun Heo 	 */
17827a4e344cSTejun Heo 	if (pool->flags & POOL_DISASSOCIATED)
1783f3421797STejun Heo 		worker->flags |= WORKER_UNBOUND;
1784c34056a3STejun Heo 
1785c34056a3STejun Heo 	return worker;
1786c34056a3STejun Heo fail:
1787c34056a3STejun Heo 	if (id >= 0) {
1788d565ed63STejun Heo 		spin_lock_irq(&pool->lock);
1789bd7bdd43STejun Heo 		ida_remove(&pool->worker_ida, id);
1790d565ed63STejun Heo 		spin_unlock_irq(&pool->lock);
1791c34056a3STejun Heo 	}
1792c34056a3STejun Heo 	kfree(worker);
1793c34056a3STejun Heo 	return NULL;
1794c34056a3STejun Heo }
1795c34056a3STejun Heo 
1796c34056a3STejun Heo /**
1797c34056a3STejun Heo  * start_worker - start a newly created worker
1798c34056a3STejun Heo  * @worker: worker to start
1799c34056a3STejun Heo  *
1800706026c2STejun Heo  * Make the pool aware of @worker and start it.
1801c34056a3STejun Heo  *
1802c34056a3STejun Heo  * CONTEXT:
1803d565ed63STejun Heo  * spin_lock_irq(pool->lock).
1804c34056a3STejun Heo  */
1805c34056a3STejun Heo static void start_worker(struct worker *worker)
1806c34056a3STejun Heo {
1807cb444766STejun Heo 	worker->flags |= WORKER_STARTED;
1808bd7bdd43STejun Heo 	worker->pool->nr_workers++;
1809c8e55f36STejun Heo 	worker_enter_idle(worker);
1810c34056a3STejun Heo 	wake_up_process(worker->task);
1811c34056a3STejun Heo }
1812c34056a3STejun Heo 
1813c34056a3STejun Heo /**
1814c34056a3STejun Heo  * destroy_worker - destroy a workqueue worker
1815c34056a3STejun Heo  * @worker: worker to be destroyed
1816c34056a3STejun Heo  *
1817706026c2STejun Heo  * Destroy @worker and adjust @pool stats accordingly.
1818c8e55f36STejun Heo  *
1819c8e55f36STejun Heo  * CONTEXT:
1820d565ed63STejun Heo  * spin_lock_irq(pool->lock) which is released and regrabbed.
1821c34056a3STejun Heo  */
1822c34056a3STejun Heo static void destroy_worker(struct worker *worker)
1823c34056a3STejun Heo {
1824bd7bdd43STejun Heo 	struct worker_pool *pool = worker->pool;
1825c34056a3STejun Heo 	int id = worker->id;
1826c34056a3STejun Heo 
1827c34056a3STejun Heo 	/* sanity check frenzy */
18286183c009STejun Heo 	if (WARN_ON(worker->current_work) ||
18296183c009STejun Heo 	    WARN_ON(!list_empty(&worker->scheduled)))
18306183c009STejun Heo 		return;
1831c34056a3STejun Heo 
1832c8e55f36STejun Heo 	if (worker->flags & WORKER_STARTED)
1833bd7bdd43STejun Heo 		pool->nr_workers--;
1834c8e55f36STejun Heo 	if (worker->flags & WORKER_IDLE)
1835bd7bdd43STejun Heo 		pool->nr_idle--;
1836c8e55f36STejun Heo 
1837c8e55f36STejun Heo 	list_del_init(&worker->entry);
1838cb444766STejun Heo 	worker->flags |= WORKER_DIE;
1839c8e55f36STejun Heo 
1840d565ed63STejun Heo 	spin_unlock_irq(&pool->lock);
1841c8e55f36STejun Heo 
1842c34056a3STejun Heo 	kthread_stop(worker->task);
1843c34056a3STejun Heo 	kfree(worker);
1844c34056a3STejun Heo 
1845d565ed63STejun Heo 	spin_lock_irq(&pool->lock);
1846bd7bdd43STejun Heo 	ida_remove(&pool->worker_ida, id);
1847c34056a3STejun Heo }
1848c34056a3STejun Heo 
184963d95a91STejun Heo static void idle_worker_timeout(unsigned long __pool)
1850e22bee78STejun Heo {
185163d95a91STejun Heo 	struct worker_pool *pool = (void *)__pool;
1852e22bee78STejun Heo 
1853d565ed63STejun Heo 	spin_lock_irq(&pool->lock);
1854e22bee78STejun Heo 
185563d95a91STejun Heo 	if (too_many_workers(pool)) {
1856e22bee78STejun Heo 		struct worker *worker;
1857e22bee78STejun Heo 		unsigned long expires;
1858e22bee78STejun Heo 
1859e22bee78STejun Heo 		/* idle_list is kept in LIFO order, check the last one */
186063d95a91STejun Heo 		worker = list_entry(pool->idle_list.prev, struct worker, entry);
1861e22bee78STejun Heo 		expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1862e22bee78STejun Heo 
1863e22bee78STejun Heo 		if (time_before(jiffies, expires))
186463d95a91STejun Heo 			mod_timer(&pool->idle_timer, expires);
1865e22bee78STejun Heo 		else {
1866e22bee78STejun Heo 			/* it's been idle for too long, wake up manager */
186711ebea50STejun Heo 			pool->flags |= POOL_MANAGE_WORKERS;
186863d95a91STejun Heo 			wake_up_worker(pool);
1869e22bee78STejun Heo 		}
1870e22bee78STejun Heo 	}
1871e22bee78STejun Heo 
1872d565ed63STejun Heo 	spin_unlock_irq(&pool->lock);
1873e22bee78STejun Heo }
1874e22bee78STejun Heo 
1875493a1724STejun Heo static void send_mayday(struct work_struct *work)
1876e22bee78STejun Heo {
1877112202d9STejun Heo 	struct pool_workqueue *pwq = get_work_pwq(work);
1878112202d9STejun Heo 	struct workqueue_struct *wq = pwq->wq;
1879493a1724STejun Heo 
1880493a1724STejun Heo 	lockdep_assert_held(&workqueue_lock);
1881e22bee78STejun Heo 
1882493008a8STejun Heo 	if (!wq->rescuer)
1883493a1724STejun Heo 		return;
1884e22bee78STejun Heo 
1885e22bee78STejun Heo 	/* mayday mayday mayday */
1886493a1724STejun Heo 	if (list_empty(&pwq->mayday_node)) {
1887493a1724STejun Heo 		list_add_tail(&pwq->mayday_node, &wq->maydays);
1888e22bee78STejun Heo 		wake_up_process(wq->rescuer->task);
1889493a1724STejun Heo 	}
1890e22bee78STejun Heo }
1891e22bee78STejun Heo 
1892706026c2STejun Heo static void pool_mayday_timeout(unsigned long __pool)
1893e22bee78STejun Heo {
189463d95a91STejun Heo 	struct worker_pool *pool = (void *)__pool;
1895e22bee78STejun Heo 	struct work_struct *work;
1896e22bee78STejun Heo 
1897493a1724STejun Heo 	spin_lock_irq(&workqueue_lock);		/* for wq->maydays */
1898493a1724STejun Heo 	spin_lock(&pool->lock);
1899e22bee78STejun Heo 
190063d95a91STejun Heo 	if (need_to_create_worker(pool)) {
1901e22bee78STejun Heo 		/*
1902e22bee78STejun Heo 		 * We've been trying to create a new worker but
1903e22bee78STejun Heo 		 * haven't been successful.  We might be hitting an
1904e22bee78STejun Heo 		 * allocation deadlock.  Send distress signals to
1905e22bee78STejun Heo 		 * rescuers.
1906e22bee78STejun Heo 		 */
190763d95a91STejun Heo 		list_for_each_entry(work, &pool->worklist, entry)
1908e22bee78STejun Heo 			send_mayday(work);
1909e22bee78STejun Heo 	}
1910e22bee78STejun Heo 
1911493a1724STejun Heo 	spin_unlock(&pool->lock);
1912493a1724STejun Heo 	spin_unlock_irq(&workqueue_lock);
1913e22bee78STejun Heo 
191463d95a91STejun Heo 	mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
1915e22bee78STejun Heo }
1916e22bee78STejun Heo 
1917e22bee78STejun Heo /**
1918e22bee78STejun Heo  * maybe_create_worker - create a new worker if necessary
191963d95a91STejun Heo  * @pool: pool to create a new worker for
1920e22bee78STejun Heo  *
192163d95a91STejun Heo  * Create a new worker for @pool if necessary.  @pool is guaranteed to
1922e22bee78STejun Heo  * have at least one idle worker on return from this function.  If
1923e22bee78STejun Heo  * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
192463d95a91STejun Heo  * sent to all rescuers with works scheduled on @pool to resolve
1925e22bee78STejun Heo  * possible allocation deadlock.
1926e22bee78STejun Heo  *
1927e22bee78STejun Heo  * On return, need_to_create_worker() is guaranteed to be false and
1928e22bee78STejun Heo  * may_start_working() true.
1929e22bee78STejun Heo  *
1930e22bee78STejun Heo  * LOCKING:
1931d565ed63STejun Heo  * spin_lock_irq(pool->lock) which may be released and regrabbed
1932e22bee78STejun Heo  * multiple times.  Does GFP_KERNEL allocations.  Called only from
1933e22bee78STejun Heo  * manager.
1934e22bee78STejun Heo  *
1935e22bee78STejun Heo  * RETURNS:
1936d565ed63STejun Heo  * false if no action was taken and pool->lock stayed locked, true
1937e22bee78STejun Heo  * otherwise.
1938e22bee78STejun Heo  */
193963d95a91STejun Heo static bool maybe_create_worker(struct worker_pool *pool)
1940d565ed63STejun Heo __releases(&pool->lock)
1941d565ed63STejun Heo __acquires(&pool->lock)
1942e22bee78STejun Heo {
194363d95a91STejun Heo 	if (!need_to_create_worker(pool))
1944e22bee78STejun Heo 		return false;
1945e22bee78STejun Heo restart:
1946d565ed63STejun Heo 	spin_unlock_irq(&pool->lock);
19479f9c2364STejun Heo 
1948e22bee78STejun Heo 	/* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
194963d95a91STejun Heo 	mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
1950e22bee78STejun Heo 
1951e22bee78STejun Heo 	while (true) {
1952e22bee78STejun Heo 		struct worker *worker;
1953e22bee78STejun Heo 
1954bc2ae0f5STejun Heo 		worker = create_worker(pool);
1955e22bee78STejun Heo 		if (worker) {
195663d95a91STejun Heo 			del_timer_sync(&pool->mayday_timer);
1957d565ed63STejun Heo 			spin_lock_irq(&pool->lock);
1958e22bee78STejun Heo 			start_worker(worker);
19596183c009STejun Heo 			if (WARN_ON_ONCE(need_to_create_worker(pool)))
19606183c009STejun Heo 				goto restart;
1961e22bee78STejun Heo 			return true;
1962e22bee78STejun Heo 		}
1963e22bee78STejun Heo 
196463d95a91STejun Heo 		if (!need_to_create_worker(pool))
1965e22bee78STejun Heo 			break;
1966e22bee78STejun Heo 
1967e22bee78STejun Heo 		__set_current_state(TASK_INTERRUPTIBLE);
1968e22bee78STejun Heo 		schedule_timeout(CREATE_COOLDOWN);
19699f9c2364STejun Heo 
197063d95a91STejun Heo 		if (!need_to_create_worker(pool))
1971e22bee78STejun Heo 			break;
1972e22bee78STejun Heo 	}
1973e22bee78STejun Heo 
197463d95a91STejun Heo 	del_timer_sync(&pool->mayday_timer);
1975d565ed63STejun Heo 	spin_lock_irq(&pool->lock);
197663d95a91STejun Heo 	if (need_to_create_worker(pool))
1977e22bee78STejun Heo 		goto restart;
1978e22bee78STejun Heo 	return true;
1979e22bee78STejun Heo }
1980e22bee78STejun Heo 
1981e22bee78STejun Heo /**
1982e22bee78STejun Heo  * maybe_destroy_worker - destroy workers which have been idle for a while
198363d95a91STejun Heo  * @pool: pool to destroy workers for
1984e22bee78STejun Heo  *
198563d95a91STejun Heo  * Destroy @pool workers which have been idle for longer than
1986e22bee78STejun Heo  * IDLE_WORKER_TIMEOUT.
1987e22bee78STejun Heo  *
1988e22bee78STejun Heo  * LOCKING:
1989d565ed63STejun Heo  * spin_lock_irq(pool->lock) which may be released and regrabbed
1990e22bee78STejun Heo  * multiple times.  Called only from manager.
1991e22bee78STejun Heo  *
1992e22bee78STejun Heo  * RETURNS:
1993d565ed63STejun Heo  * false if no action was taken and pool->lock stayed locked, true
1994e22bee78STejun Heo  * otherwise.
1995e22bee78STejun Heo  */
199663d95a91STejun Heo static bool maybe_destroy_workers(struct worker_pool *pool)
1997e22bee78STejun Heo {
1998e22bee78STejun Heo 	bool ret = false;
1999e22bee78STejun Heo 
200063d95a91STejun Heo 	while (too_many_workers(pool)) {
2001e22bee78STejun Heo 		struct worker *worker;
2002e22bee78STejun Heo 		unsigned long expires;
2003e22bee78STejun Heo 
200463d95a91STejun Heo 		worker = list_entry(pool->idle_list.prev, struct worker, entry);
2005e22bee78STejun Heo 		expires = worker->last_active + IDLE_WORKER_TIMEOUT;
2006e22bee78STejun Heo 
2007e22bee78STejun Heo 		if (time_before(jiffies, expires)) {
200863d95a91STejun Heo 			mod_timer(&pool->idle_timer, expires);
2009e22bee78STejun Heo 			break;
2010e22bee78STejun Heo 		}
2011e22bee78STejun Heo 
2012e22bee78STejun Heo 		destroy_worker(worker);
2013e22bee78STejun Heo 		ret = true;
2014e22bee78STejun Heo 	}
2015e22bee78STejun Heo 
2016e22bee78STejun Heo 	return ret;
2017e22bee78STejun Heo }
2018e22bee78STejun Heo 
2019e22bee78STejun Heo /**
2020e22bee78STejun Heo  * manage_workers - manage worker pool
2021e22bee78STejun Heo  * @worker: self
2022e22bee78STejun Heo  *
2023706026c2STejun Heo  * Assume the manager role and manage the worker pool @worker belongs
2024e22bee78STejun Heo  * to.  At any given time, there can be only zero or one manager per
2025706026c2STejun Heo  * pool.  The exclusion is handled automatically by this function.
2026e22bee78STejun Heo  *
2027e22bee78STejun Heo  * The caller can safely start processing works on false return.  On
2028e22bee78STejun Heo  * true return, it's guaranteed that need_to_create_worker() is false
2029e22bee78STejun Heo  * and may_start_working() is true.
2030e22bee78STejun Heo  *
2031e22bee78STejun Heo  * CONTEXT:
2032d565ed63STejun Heo  * spin_lock_irq(pool->lock) which may be released and regrabbed
2033e22bee78STejun Heo  * multiple times.  Does GFP_KERNEL allocations.
2034e22bee78STejun Heo  *
2035e22bee78STejun Heo  * RETURNS:
2036d565ed63STejun Heo  * spin_lock_irq(pool->lock) which may be released and regrabbed
2037d565ed63STejun Heo  * multiple times.  Does GFP_KERNEL allocations.
2038e22bee78STejun Heo  */
2039e22bee78STejun Heo static bool manage_workers(struct worker *worker)
2040e22bee78STejun Heo {
204163d95a91STejun Heo 	struct worker_pool *pool = worker->pool;
2042e22bee78STejun Heo 	bool ret = false;
2043e22bee78STejun Heo 
204434a06bd6STejun Heo 	if (!mutex_trylock(&pool->manager_arb))
2045e22bee78STejun Heo 		return ret;
2046e22bee78STejun Heo 
2047ee378aa4SLai Jiangshan 	/*
2048ee378aa4SLai Jiangshan 	 * To simplify both worker management and CPU hotplug, hold off
2049ee378aa4SLai Jiangshan 	 * management while hotplug is in progress.  CPU hotplug path can't
205034a06bd6STejun Heo 	 * grab @pool->manager_arb to achieve this because that can lead to
205134a06bd6STejun Heo 	 * idle worker depletion (all become busy thinking someone else is
205234a06bd6STejun Heo 	 * managing) which in turn can result in deadlock under extreme
205334a06bd6STejun Heo 	 * circumstances.  Use @pool->assoc_mutex to synchronize manager
205434a06bd6STejun Heo 	 * against CPU hotplug.
2055ee378aa4SLai Jiangshan 	 *
2056b2eb83d1SLai Jiangshan 	 * assoc_mutex would always be free unless CPU hotplug is in
2057d565ed63STejun Heo 	 * progress.  trylock first without dropping @pool->lock.
2058ee378aa4SLai Jiangshan 	 */
2059b2eb83d1SLai Jiangshan 	if (unlikely(!mutex_trylock(&pool->assoc_mutex))) {
2060d565ed63STejun Heo 		spin_unlock_irq(&pool->lock);
2061b2eb83d1SLai Jiangshan 		mutex_lock(&pool->assoc_mutex);
2062ee378aa4SLai Jiangshan 		/*
2063ee378aa4SLai Jiangshan 		 * CPU hotplug could have happened while we were waiting
2064b2eb83d1SLai Jiangshan 		 * for assoc_mutex.  Hotplug itself can't handle us
2065ee378aa4SLai Jiangshan 		 * because manager isn't either on idle or busy list, and
2066706026c2STejun Heo 		 * @pool's state and ours could have deviated.
2067ee378aa4SLai Jiangshan 		 *
2068b2eb83d1SLai Jiangshan 		 * As hotplug is now excluded via assoc_mutex, we can
2069ee378aa4SLai Jiangshan 		 * simply try to bind.  It will succeed or fail depending
2070706026c2STejun Heo 		 * on @pool's current state.  Try it and adjust
2071ee378aa4SLai Jiangshan 		 * %WORKER_UNBOUND accordingly.
2072ee378aa4SLai Jiangshan 		 */
2073f36dc67bSLai Jiangshan 		if (worker_maybe_bind_and_lock(pool))
2074ee378aa4SLai Jiangshan 			worker->flags &= ~WORKER_UNBOUND;
2075ee378aa4SLai Jiangshan 		else
2076ee378aa4SLai Jiangshan 			worker->flags |= WORKER_UNBOUND;
2077ee378aa4SLai Jiangshan 
2078ee378aa4SLai Jiangshan 		ret = true;
2079ee378aa4SLai Jiangshan 	}
2080ee378aa4SLai Jiangshan 
208111ebea50STejun Heo 	pool->flags &= ~POOL_MANAGE_WORKERS;
2082e22bee78STejun Heo 
2083e22bee78STejun Heo 	/*
2084e22bee78STejun Heo 	 * Destroy and then create so that may_start_working() is true
2085e22bee78STejun Heo 	 * on return.
2086e22bee78STejun Heo 	 */
208763d95a91STejun Heo 	ret |= maybe_destroy_workers(pool);
208863d95a91STejun Heo 	ret |= maybe_create_worker(pool);
2089e22bee78STejun Heo 
2090b2eb83d1SLai Jiangshan 	mutex_unlock(&pool->assoc_mutex);
209134a06bd6STejun Heo 	mutex_unlock(&pool->manager_arb);
2092e22bee78STejun Heo 	return ret;
2093e22bee78STejun Heo }
2094e22bee78STejun Heo 
2095a62428c0STejun Heo /**
2096a62428c0STejun Heo  * process_one_work - process single work
2097c34056a3STejun Heo  * @worker: self
2098a62428c0STejun Heo  * @work: work to process
2099a62428c0STejun Heo  *
2100a62428c0STejun Heo  * Process @work.  This function contains all the logics necessary to
2101a62428c0STejun Heo  * process a single work including synchronization against and
2102a62428c0STejun Heo  * interaction with other workers on the same cpu, queueing and
2103a62428c0STejun Heo  * flushing.  As long as context requirement is met, any worker can
2104a62428c0STejun Heo  * call this function to process a work.
2105a62428c0STejun Heo  *
2106a62428c0STejun Heo  * CONTEXT:
2107d565ed63STejun Heo  * spin_lock_irq(pool->lock) which is released and regrabbed.
2108a62428c0STejun Heo  */
2109c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work)
2110d565ed63STejun Heo __releases(&pool->lock)
2111d565ed63STejun Heo __acquires(&pool->lock)
21121da177e4SLinus Torvalds {
2113112202d9STejun Heo 	struct pool_workqueue *pwq = get_work_pwq(work);
2114bd7bdd43STejun Heo 	struct worker_pool *pool = worker->pool;
2115112202d9STejun Heo 	bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE;
211673f53c4aSTejun Heo 	int work_color;
21177e11629dSTejun Heo 	struct worker *collision;
21184e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
21194e6045f1SJohannes Berg 	/*
2120a62428c0STejun Heo 	 * It is permissible to free the struct work_struct from
2121a62428c0STejun Heo 	 * inside the function that is called from it, this we need to
2122a62428c0STejun Heo 	 * take into account for lockdep too.  To avoid bogus "held
2123a62428c0STejun Heo 	 * lock freed" warnings as well as problems when looking into
2124a62428c0STejun Heo 	 * work->lockdep_map, make a copy and use that here.
21254e6045f1SJohannes Berg 	 */
21264d82a1deSPeter Zijlstra 	struct lockdep_map lockdep_map;
21274d82a1deSPeter Zijlstra 
21284d82a1deSPeter Zijlstra 	lockdep_copy_map(&lockdep_map, &work->lockdep_map);
21294e6045f1SJohannes Berg #endif
21306fec10a1STejun Heo 	/*
21316fec10a1STejun Heo 	 * Ensure we're on the correct CPU.  DISASSOCIATED test is
21326fec10a1STejun Heo 	 * necessary to avoid spurious warnings from rescuers servicing the
213324647570STejun Heo 	 * unbound or a disassociated pool.
21346fec10a1STejun Heo 	 */
21355f7dabfdSLai Jiangshan 	WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) &&
213624647570STejun Heo 		     !(pool->flags & POOL_DISASSOCIATED) &&
2137ec22ca5eSTejun Heo 		     raw_smp_processor_id() != pool->cpu);
213825511a47STejun Heo 
21397e11629dSTejun Heo 	/*
21407e11629dSTejun Heo 	 * A single work shouldn't be executed concurrently by
21417e11629dSTejun Heo 	 * multiple workers on a single cpu.  Check whether anyone is
21427e11629dSTejun Heo 	 * already processing the work.  If so, defer the work to the
21437e11629dSTejun Heo 	 * currently executing one.
21447e11629dSTejun Heo 	 */
2145c9e7cf27STejun Heo 	collision = find_worker_executing_work(pool, work);
21467e11629dSTejun Heo 	if (unlikely(collision)) {
21477e11629dSTejun Heo 		move_linked_works(work, &collision->scheduled, NULL);
21487e11629dSTejun Heo 		return;
21497e11629dSTejun Heo 	}
21501da177e4SLinus Torvalds 
21518930cabaSTejun Heo 	/* claim and dequeue */
21521da177e4SLinus Torvalds 	debug_work_deactivate(work);
2153c9e7cf27STejun Heo 	hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
2154c34056a3STejun Heo 	worker->current_work = work;
2155a2c1c57bSTejun Heo 	worker->current_func = work->func;
2156112202d9STejun Heo 	worker->current_pwq = pwq;
215773f53c4aSTejun Heo 	work_color = get_work_color(work);
21587a22ad75STejun Heo 
2159a62428c0STejun Heo 	list_del_init(&work->entry);
2160a62428c0STejun Heo 
2161649027d7STejun Heo 	/*
2162fb0e7bebSTejun Heo 	 * CPU intensive works don't participate in concurrency
2163fb0e7bebSTejun Heo 	 * management.  They're the scheduler's responsibility.
2164fb0e7bebSTejun Heo 	 */
2165fb0e7bebSTejun Heo 	if (unlikely(cpu_intensive))
2166fb0e7bebSTejun Heo 		worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2167fb0e7bebSTejun Heo 
2168974271c4STejun Heo 	/*
2169d565ed63STejun Heo 	 * Unbound pool isn't concurrency managed and work items should be
2170974271c4STejun Heo 	 * executed ASAP.  Wake up another worker if necessary.
2171974271c4STejun Heo 	 */
217263d95a91STejun Heo 	if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
217363d95a91STejun Heo 		wake_up_worker(pool);
2174974271c4STejun Heo 
21758930cabaSTejun Heo 	/*
21767c3eed5cSTejun Heo 	 * Record the last pool and clear PENDING which should be the last
2177d565ed63STejun Heo 	 * update to @work.  Also, do this inside @pool->lock so that
217823657bb1STejun Heo 	 * PENDING and queued state changes happen together while IRQ is
217923657bb1STejun Heo 	 * disabled.
21808930cabaSTejun Heo 	 */
21817c3eed5cSTejun Heo 	set_work_pool_and_clear_pending(work, pool->id);
21821da177e4SLinus Torvalds 
2183d565ed63STejun Heo 	spin_unlock_irq(&pool->lock);
2184365970a1SDavid Howells 
2185112202d9STejun Heo 	lock_map_acquire_read(&pwq->wq->lockdep_map);
21863295f0efSIngo Molnar 	lock_map_acquire(&lockdep_map);
2187e36c886aSArjan van de Ven 	trace_workqueue_execute_start(work);
2188a2c1c57bSTejun Heo 	worker->current_func(work);
2189e36c886aSArjan van de Ven 	/*
2190e36c886aSArjan van de Ven 	 * While we must be careful to not use "work" after this, the trace
2191e36c886aSArjan van de Ven 	 * point will only record its address.
2192e36c886aSArjan van de Ven 	 */
2193e36c886aSArjan van de Ven 	trace_workqueue_execute_end(work);
21943295f0efSIngo Molnar 	lock_map_release(&lockdep_map);
2195112202d9STejun Heo 	lock_map_release(&pwq->wq->lockdep_map);
21961da177e4SLinus Torvalds 
2197d5abe669SPeter Zijlstra 	if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
2198044c782cSValentin Ilie 		pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2199044c782cSValentin Ilie 		       "     last function: %pf\n",
2200a2c1c57bSTejun Heo 		       current->comm, preempt_count(), task_pid_nr(current),
2201a2c1c57bSTejun Heo 		       worker->current_func);
2202d5abe669SPeter Zijlstra 		debug_show_held_locks(current);
2203d5abe669SPeter Zijlstra 		dump_stack();
2204d5abe669SPeter Zijlstra 	}
2205d5abe669SPeter Zijlstra 
2206d565ed63STejun Heo 	spin_lock_irq(&pool->lock);
2207a62428c0STejun Heo 
2208fb0e7bebSTejun Heo 	/* clear cpu intensive status */
2209fb0e7bebSTejun Heo 	if (unlikely(cpu_intensive))
2210fb0e7bebSTejun Heo 		worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2211fb0e7bebSTejun Heo 
2212a62428c0STejun Heo 	/* we're done with it, release */
221342f8570fSSasha Levin 	hash_del(&worker->hentry);
2214c34056a3STejun Heo 	worker->current_work = NULL;
2215a2c1c57bSTejun Heo 	worker->current_func = NULL;
2216112202d9STejun Heo 	worker->current_pwq = NULL;
2217112202d9STejun Heo 	pwq_dec_nr_in_flight(pwq, work_color);
22181da177e4SLinus Torvalds }
22191da177e4SLinus Torvalds 
2220affee4b2STejun Heo /**
2221affee4b2STejun Heo  * process_scheduled_works - process scheduled works
2222affee4b2STejun Heo  * @worker: self
2223affee4b2STejun Heo  *
2224affee4b2STejun Heo  * Process all scheduled works.  Please note that the scheduled list
2225affee4b2STejun Heo  * may change while processing a work, so this function repeatedly
2226affee4b2STejun Heo  * fetches a work from the top and executes it.
2227affee4b2STejun Heo  *
2228affee4b2STejun Heo  * CONTEXT:
2229d565ed63STejun Heo  * spin_lock_irq(pool->lock) which may be released and regrabbed
2230affee4b2STejun Heo  * multiple times.
2231affee4b2STejun Heo  */
2232affee4b2STejun Heo static void process_scheduled_works(struct worker *worker)
22331da177e4SLinus Torvalds {
2234affee4b2STejun Heo 	while (!list_empty(&worker->scheduled)) {
2235affee4b2STejun Heo 		struct work_struct *work = list_first_entry(&worker->scheduled,
2236a62428c0STejun Heo 						struct work_struct, entry);
2237c34056a3STejun Heo 		process_one_work(worker, work);
2238a62428c0STejun Heo 	}
22391da177e4SLinus Torvalds }
22401da177e4SLinus Torvalds 
22414690c4abSTejun Heo /**
22424690c4abSTejun Heo  * worker_thread - the worker thread function
2243c34056a3STejun Heo  * @__worker: self
22444690c4abSTejun Heo  *
2245706026c2STejun Heo  * The worker thread function.  There are NR_CPU_WORKER_POOLS dynamic pools
2246706026c2STejun Heo  * of these per each cpu.  These workers process all works regardless of
2247e22bee78STejun Heo  * their specific target workqueue.  The only exception is works which
2248e22bee78STejun Heo  * belong to workqueues with a rescuer which will be explained in
2249e22bee78STejun Heo  * rescuer_thread().
22504690c4abSTejun Heo  */
2251c34056a3STejun Heo static int worker_thread(void *__worker)
22521da177e4SLinus Torvalds {
2253c34056a3STejun Heo 	struct worker *worker = __worker;
2254bd7bdd43STejun Heo 	struct worker_pool *pool = worker->pool;
22551da177e4SLinus Torvalds 
2256e22bee78STejun Heo 	/* tell the scheduler that this is a workqueue worker */
2257e22bee78STejun Heo 	worker->task->flags |= PF_WQ_WORKER;
2258c8e55f36STejun Heo woke_up:
2259d565ed63STejun Heo 	spin_lock_irq(&pool->lock);
2260affee4b2STejun Heo 
22615f7dabfdSLai Jiangshan 	/* we are off idle list if destruction or rebind is requested */
22625f7dabfdSLai Jiangshan 	if (unlikely(list_empty(&worker->entry))) {
2263d565ed63STejun Heo 		spin_unlock_irq(&pool->lock);
226425511a47STejun Heo 
22655f7dabfdSLai Jiangshan 		/* if DIE is set, destruction is requested */
226625511a47STejun Heo 		if (worker->flags & WORKER_DIE) {
2267e22bee78STejun Heo 			worker->task->flags &= ~PF_WQ_WORKER;
2268c8e55f36STejun Heo 			return 0;
2269c8e55f36STejun Heo 		}
2270c8e55f36STejun Heo 
22715f7dabfdSLai Jiangshan 		/* otherwise, rebind */
227225511a47STejun Heo 		idle_worker_rebind(worker);
227325511a47STejun Heo 		goto woke_up;
227425511a47STejun Heo 	}
227525511a47STejun Heo 
2276c8e55f36STejun Heo 	worker_leave_idle(worker);
2277db7bccf4STejun Heo recheck:
2278e22bee78STejun Heo 	/* no more worker necessary? */
227963d95a91STejun Heo 	if (!need_more_worker(pool))
2280e22bee78STejun Heo 		goto sleep;
2281e22bee78STejun Heo 
2282e22bee78STejun Heo 	/* do we need to manage? */
228363d95a91STejun Heo 	if (unlikely(!may_start_working(pool)) && manage_workers(worker))
2284e22bee78STejun Heo 		goto recheck;
2285e22bee78STejun Heo 
2286c8e55f36STejun Heo 	/*
2287c8e55f36STejun Heo 	 * ->scheduled list can only be filled while a worker is
2288c8e55f36STejun Heo 	 * preparing to process a work or actually processing it.
2289c8e55f36STejun Heo 	 * Make sure nobody diddled with it while I was sleeping.
2290c8e55f36STejun Heo 	 */
22916183c009STejun Heo 	WARN_ON_ONCE(!list_empty(&worker->scheduled));
2292c8e55f36STejun Heo 
2293e22bee78STejun Heo 	/*
2294e22bee78STejun Heo 	 * When control reaches this point, we're guaranteed to have
2295e22bee78STejun Heo 	 * at least one idle worker or that someone else has already
2296e22bee78STejun Heo 	 * assumed the manager role.
2297e22bee78STejun Heo 	 */
2298e22bee78STejun Heo 	worker_clr_flags(worker, WORKER_PREP);
2299e22bee78STejun Heo 
2300e22bee78STejun Heo 	do {
2301affee4b2STejun Heo 		struct work_struct *work =
2302bd7bdd43STejun Heo 			list_first_entry(&pool->worklist,
2303affee4b2STejun Heo 					 struct work_struct, entry);
2304affee4b2STejun Heo 
2305c8e55f36STejun Heo 		if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2306affee4b2STejun Heo 			/* optimization path, not strictly necessary */
2307affee4b2STejun Heo 			process_one_work(worker, work);
2308affee4b2STejun Heo 			if (unlikely(!list_empty(&worker->scheduled)))
2309affee4b2STejun Heo 				process_scheduled_works(worker);
2310affee4b2STejun Heo 		} else {
2311c8e55f36STejun Heo 			move_linked_works(work, &worker->scheduled, NULL);
2312affee4b2STejun Heo 			process_scheduled_works(worker);
2313affee4b2STejun Heo 		}
231463d95a91STejun Heo 	} while (keep_working(pool));
2315affee4b2STejun Heo 
2316e22bee78STejun Heo 	worker_set_flags(worker, WORKER_PREP, false);
2317d313dd85STejun Heo sleep:
231863d95a91STejun Heo 	if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
2319e22bee78STejun Heo 		goto recheck;
2320d313dd85STejun Heo 
2321c8e55f36STejun Heo 	/*
2322d565ed63STejun Heo 	 * pool->lock is held and there's no work to process and no need to
2323d565ed63STejun Heo 	 * manage, sleep.  Workers are woken up only while holding
2324d565ed63STejun Heo 	 * pool->lock or from local cpu, so setting the current state
2325d565ed63STejun Heo 	 * before releasing pool->lock is enough to prevent losing any
2326d565ed63STejun Heo 	 * event.
2327c8e55f36STejun Heo 	 */
2328c8e55f36STejun Heo 	worker_enter_idle(worker);
2329c8e55f36STejun Heo 	__set_current_state(TASK_INTERRUPTIBLE);
2330d565ed63STejun Heo 	spin_unlock_irq(&pool->lock);
23311da177e4SLinus Torvalds 	schedule();
2332c8e55f36STejun Heo 	goto woke_up;
23331da177e4SLinus Torvalds }
23341da177e4SLinus Torvalds 
2335e22bee78STejun Heo /**
2336e22bee78STejun Heo  * rescuer_thread - the rescuer thread function
2337111c225aSTejun Heo  * @__rescuer: self
2338e22bee78STejun Heo  *
2339e22bee78STejun Heo  * Workqueue rescuer thread function.  There's one rescuer for each
2340493008a8STejun Heo  * workqueue which has WQ_MEM_RECLAIM set.
2341e22bee78STejun Heo  *
2342706026c2STejun Heo  * Regular work processing on a pool may block trying to create a new
2343e22bee78STejun Heo  * worker which uses GFP_KERNEL allocation which has slight chance of
2344e22bee78STejun Heo  * developing into deadlock if some works currently on the same queue
2345e22bee78STejun Heo  * need to be processed to satisfy the GFP_KERNEL allocation.  This is
2346e22bee78STejun Heo  * the problem rescuer solves.
2347e22bee78STejun Heo  *
2348706026c2STejun Heo  * When such condition is possible, the pool summons rescuers of all
2349706026c2STejun Heo  * workqueues which have works queued on the pool and let them process
2350e22bee78STejun Heo  * those works so that forward progress can be guaranteed.
2351e22bee78STejun Heo  *
2352e22bee78STejun Heo  * This should happen rarely.
2353e22bee78STejun Heo  */
2354111c225aSTejun Heo static int rescuer_thread(void *__rescuer)
2355e22bee78STejun Heo {
2356111c225aSTejun Heo 	struct worker *rescuer = __rescuer;
2357111c225aSTejun Heo 	struct workqueue_struct *wq = rescuer->rescue_wq;
2358e22bee78STejun Heo 	struct list_head *scheduled = &rescuer->scheduled;
2359e22bee78STejun Heo 
2360e22bee78STejun Heo 	set_user_nice(current, RESCUER_NICE_LEVEL);
2361111c225aSTejun Heo 
2362111c225aSTejun Heo 	/*
2363111c225aSTejun Heo 	 * Mark rescuer as worker too.  As WORKER_PREP is never cleared, it
2364111c225aSTejun Heo 	 * doesn't participate in concurrency management.
2365111c225aSTejun Heo 	 */
2366111c225aSTejun Heo 	rescuer->task->flags |= PF_WQ_WORKER;
2367e22bee78STejun Heo repeat:
2368e22bee78STejun Heo 	set_current_state(TASK_INTERRUPTIBLE);
23691da177e4SLinus Torvalds 
2370412d32e6SMike Galbraith 	if (kthread_should_stop()) {
2371412d32e6SMike Galbraith 		__set_current_state(TASK_RUNNING);
2372111c225aSTejun Heo 		rescuer->task->flags &= ~PF_WQ_WORKER;
2373e22bee78STejun Heo 		return 0;
2374412d32e6SMike Galbraith 	}
23751da177e4SLinus Torvalds 
2376493a1724STejun Heo 	/* see whether any pwq is asking for help */
2377493a1724STejun Heo 	spin_lock_irq(&workqueue_lock);
2378493a1724STejun Heo 
2379493a1724STejun Heo 	while (!list_empty(&wq->maydays)) {
2380493a1724STejun Heo 		struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
2381493a1724STejun Heo 					struct pool_workqueue, mayday_node);
2382112202d9STejun Heo 		struct worker_pool *pool = pwq->pool;
2383e22bee78STejun Heo 		struct work_struct *work, *n;
2384e22bee78STejun Heo 
2385e22bee78STejun Heo 		__set_current_state(TASK_RUNNING);
2386493a1724STejun Heo 		list_del_init(&pwq->mayday_node);
2387493a1724STejun Heo 
2388493a1724STejun Heo 		spin_unlock_irq(&workqueue_lock);
2389e22bee78STejun Heo 
2390e22bee78STejun Heo 		/* migrate to the target cpu if possible */
2391f36dc67bSLai Jiangshan 		worker_maybe_bind_and_lock(pool);
2392b3104104SLai Jiangshan 		rescuer->pool = pool;
2393e22bee78STejun Heo 
2394e22bee78STejun Heo 		/*
2395e22bee78STejun Heo 		 * Slurp in all works issued via this workqueue and
2396e22bee78STejun Heo 		 * process'em.
2397e22bee78STejun Heo 		 */
23986183c009STejun Heo 		WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
2399bd7bdd43STejun Heo 		list_for_each_entry_safe(work, n, &pool->worklist, entry)
2400112202d9STejun Heo 			if (get_work_pwq(work) == pwq)
2401e22bee78STejun Heo 				move_linked_works(work, scheduled, &n);
2402e22bee78STejun Heo 
2403e22bee78STejun Heo 		process_scheduled_works(rescuer);
24047576958aSTejun Heo 
24057576958aSTejun Heo 		/*
2406d565ed63STejun Heo 		 * Leave this pool.  If keep_working() is %true, notify a
24077576958aSTejun Heo 		 * regular worker; otherwise, we end up with 0 concurrency
24087576958aSTejun Heo 		 * and stalling the execution.
24097576958aSTejun Heo 		 */
241063d95a91STejun Heo 		if (keep_working(pool))
241163d95a91STejun Heo 			wake_up_worker(pool);
24127576958aSTejun Heo 
2413b3104104SLai Jiangshan 		rescuer->pool = NULL;
2414493a1724STejun Heo 		spin_unlock(&pool->lock);
2415493a1724STejun Heo 		spin_lock(&workqueue_lock);
24161da177e4SLinus Torvalds 	}
24171da177e4SLinus Torvalds 
2418493a1724STejun Heo 	spin_unlock_irq(&workqueue_lock);
2419493a1724STejun Heo 
2420111c225aSTejun Heo 	/* rescuers should never participate in concurrency management */
2421111c225aSTejun Heo 	WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
2422e22bee78STejun Heo 	schedule();
2423e22bee78STejun Heo 	goto repeat;
24241da177e4SLinus Torvalds }
24251da177e4SLinus Torvalds 
2426fc2e4d70SOleg Nesterov struct wq_barrier {
2427fc2e4d70SOleg Nesterov 	struct work_struct	work;
2428fc2e4d70SOleg Nesterov 	struct completion	done;
2429fc2e4d70SOleg Nesterov };
2430fc2e4d70SOleg Nesterov 
2431fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work)
2432fc2e4d70SOleg Nesterov {
2433fc2e4d70SOleg Nesterov 	struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2434fc2e4d70SOleg Nesterov 	complete(&barr->done);
2435fc2e4d70SOleg Nesterov }
2436fc2e4d70SOleg Nesterov 
24374690c4abSTejun Heo /**
24384690c4abSTejun Heo  * insert_wq_barrier - insert a barrier work
2439112202d9STejun Heo  * @pwq: pwq to insert barrier into
24404690c4abSTejun Heo  * @barr: wq_barrier to insert
2441affee4b2STejun Heo  * @target: target work to attach @barr to
2442affee4b2STejun Heo  * @worker: worker currently executing @target, NULL if @target is not executing
24434690c4abSTejun Heo  *
2444affee4b2STejun Heo  * @barr is linked to @target such that @barr is completed only after
2445affee4b2STejun Heo  * @target finishes execution.  Please note that the ordering
2446affee4b2STejun Heo  * guarantee is observed only with respect to @target and on the local
2447affee4b2STejun Heo  * cpu.
2448affee4b2STejun Heo  *
2449affee4b2STejun Heo  * Currently, a queued barrier can't be canceled.  This is because
2450affee4b2STejun Heo  * try_to_grab_pending() can't determine whether the work to be
2451affee4b2STejun Heo  * grabbed is at the head of the queue and thus can't clear LINKED
2452affee4b2STejun Heo  * flag of the previous work while there must be a valid next work
2453affee4b2STejun Heo  * after a work with LINKED flag set.
2454affee4b2STejun Heo  *
2455affee4b2STejun Heo  * Note that when @worker is non-NULL, @target may be modified
2456112202d9STejun Heo  * underneath us, so we can't reliably determine pwq from @target.
24574690c4abSTejun Heo  *
24584690c4abSTejun Heo  * CONTEXT:
2459d565ed63STejun Heo  * spin_lock_irq(pool->lock).
24604690c4abSTejun Heo  */
2461112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq,
2462affee4b2STejun Heo 			      struct wq_barrier *barr,
2463affee4b2STejun Heo 			      struct work_struct *target, struct worker *worker)
2464fc2e4d70SOleg Nesterov {
2465affee4b2STejun Heo 	struct list_head *head;
2466affee4b2STejun Heo 	unsigned int linked = 0;
2467affee4b2STejun Heo 
2468dc186ad7SThomas Gleixner 	/*
2469d565ed63STejun Heo 	 * debugobject calls are safe here even with pool->lock locked
2470dc186ad7SThomas Gleixner 	 * as we know for sure that this will not trigger any of the
2471dc186ad7SThomas Gleixner 	 * checks and call back into the fixup functions where we
2472dc186ad7SThomas Gleixner 	 * might deadlock.
2473dc186ad7SThomas Gleixner 	 */
2474ca1cab37SAndrew Morton 	INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
247522df02bbSTejun Heo 	__set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
2476fc2e4d70SOleg Nesterov 	init_completion(&barr->done);
247783c22520SOleg Nesterov 
2478affee4b2STejun Heo 	/*
2479affee4b2STejun Heo 	 * If @target is currently being executed, schedule the
2480affee4b2STejun Heo 	 * barrier to the worker; otherwise, put it after @target.
2481affee4b2STejun Heo 	 */
2482affee4b2STejun Heo 	if (worker)
2483affee4b2STejun Heo 		head = worker->scheduled.next;
2484affee4b2STejun Heo 	else {
2485affee4b2STejun Heo 		unsigned long *bits = work_data_bits(target);
2486affee4b2STejun Heo 
2487affee4b2STejun Heo 		head = target->entry.next;
2488affee4b2STejun Heo 		/* there can already be other linked works, inherit and set */
2489affee4b2STejun Heo 		linked = *bits & WORK_STRUCT_LINKED;
2490affee4b2STejun Heo 		__set_bit(WORK_STRUCT_LINKED_BIT, bits);
2491affee4b2STejun Heo 	}
2492affee4b2STejun Heo 
2493dc186ad7SThomas Gleixner 	debug_work_activate(&barr->work);
2494112202d9STejun Heo 	insert_work(pwq, &barr->work, head,
2495affee4b2STejun Heo 		    work_color_to_flags(WORK_NO_COLOR) | linked);
2496fc2e4d70SOleg Nesterov }
2497fc2e4d70SOleg Nesterov 
249873f53c4aSTejun Heo /**
2499112202d9STejun Heo  * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing
250073f53c4aSTejun Heo  * @wq: workqueue being flushed
250173f53c4aSTejun Heo  * @flush_color: new flush color, < 0 for no-op
250273f53c4aSTejun Heo  * @work_color: new work color, < 0 for no-op
250373f53c4aSTejun Heo  *
2504112202d9STejun Heo  * Prepare pwqs for workqueue flushing.
250573f53c4aSTejun Heo  *
2506112202d9STejun Heo  * If @flush_color is non-negative, flush_color on all pwqs should be
2507112202d9STejun Heo  * -1.  If no pwq has in-flight commands at the specified color, all
2508112202d9STejun Heo  * pwq->flush_color's stay at -1 and %false is returned.  If any pwq
2509112202d9STejun Heo  * has in flight commands, its pwq->flush_color is set to
2510112202d9STejun Heo  * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq
251173f53c4aSTejun Heo  * wakeup logic is armed and %true is returned.
251273f53c4aSTejun Heo  *
251373f53c4aSTejun Heo  * The caller should have initialized @wq->first_flusher prior to
251473f53c4aSTejun Heo  * calling this function with non-negative @flush_color.  If
251573f53c4aSTejun Heo  * @flush_color is negative, no flush color update is done and %false
251673f53c4aSTejun Heo  * is returned.
251773f53c4aSTejun Heo  *
2518112202d9STejun Heo  * If @work_color is non-negative, all pwqs should have the same
251973f53c4aSTejun Heo  * work_color which is previous to @work_color and all will be
252073f53c4aSTejun Heo  * advanced to @work_color.
252173f53c4aSTejun Heo  *
252273f53c4aSTejun Heo  * CONTEXT:
252373f53c4aSTejun Heo  * mutex_lock(wq->flush_mutex).
252473f53c4aSTejun Heo  *
252573f53c4aSTejun Heo  * RETURNS:
252673f53c4aSTejun Heo  * %true if @flush_color >= 0 and there's something to flush.  %false
252773f53c4aSTejun Heo  * otherwise.
252873f53c4aSTejun Heo  */
2529112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
253073f53c4aSTejun Heo 				      int flush_color, int work_color)
25311da177e4SLinus Torvalds {
253273f53c4aSTejun Heo 	bool wait = false;
253349e3cf44STejun Heo 	struct pool_workqueue *pwq;
25341da177e4SLinus Torvalds 
253573f53c4aSTejun Heo 	if (flush_color >= 0) {
25366183c009STejun Heo 		WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
2537112202d9STejun Heo 		atomic_set(&wq->nr_pwqs_to_flush, 1);
2538dc186ad7SThomas Gleixner 	}
253914441960SOleg Nesterov 
254076af4d93STejun Heo 	local_irq_disable();
254176af4d93STejun Heo 
254249e3cf44STejun Heo 	for_each_pwq(pwq, wq) {
2543112202d9STejun Heo 		struct worker_pool *pool = pwq->pool;
25441da177e4SLinus Torvalds 
254576af4d93STejun Heo 		spin_lock(&pool->lock);
254673f53c4aSTejun Heo 
254773f53c4aSTejun Heo 		if (flush_color >= 0) {
25486183c009STejun Heo 			WARN_ON_ONCE(pwq->flush_color != -1);
254973f53c4aSTejun Heo 
2550112202d9STejun Heo 			if (pwq->nr_in_flight[flush_color]) {
2551112202d9STejun Heo 				pwq->flush_color = flush_color;
2552112202d9STejun Heo 				atomic_inc(&wq->nr_pwqs_to_flush);
255373f53c4aSTejun Heo 				wait = true;
25541da177e4SLinus Torvalds 			}
255573f53c4aSTejun Heo 		}
255673f53c4aSTejun Heo 
255773f53c4aSTejun Heo 		if (work_color >= 0) {
25586183c009STejun Heo 			WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
2559112202d9STejun Heo 			pwq->work_color = work_color;
256073f53c4aSTejun Heo 		}
256173f53c4aSTejun Heo 
256276af4d93STejun Heo 		spin_unlock(&pool->lock);
25631da177e4SLinus Torvalds 	}
25641da177e4SLinus Torvalds 
256576af4d93STejun Heo 	local_irq_enable();
256676af4d93STejun Heo 
2567112202d9STejun Heo 	if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush))
256873f53c4aSTejun Heo 		complete(&wq->first_flusher->done);
256973f53c4aSTejun Heo 
257073f53c4aSTejun Heo 	return wait;
257183c22520SOleg Nesterov }
25721da177e4SLinus Torvalds 
25730fcb78c2SRolf Eike Beer /**
25741da177e4SLinus Torvalds  * flush_workqueue - ensure that any scheduled work has run to completion.
25750fcb78c2SRolf Eike Beer  * @wq: workqueue to flush
25761da177e4SLinus Torvalds  *
25771da177e4SLinus Torvalds  * Forces execution of the workqueue and blocks until its completion.
25781da177e4SLinus Torvalds  * This is typically used in driver shutdown handlers.
25791da177e4SLinus Torvalds  *
2580fc2e4d70SOleg Nesterov  * We sleep until all works which were queued on entry have been handled,
2581fc2e4d70SOleg Nesterov  * but we are not livelocked by new incoming ones.
25821da177e4SLinus Torvalds  */
25837ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq)
25841da177e4SLinus Torvalds {
258573f53c4aSTejun Heo 	struct wq_flusher this_flusher = {
258673f53c4aSTejun Heo 		.list = LIST_HEAD_INIT(this_flusher.list),
258773f53c4aSTejun Heo 		.flush_color = -1,
258873f53c4aSTejun Heo 		.done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
258973f53c4aSTejun Heo 	};
259073f53c4aSTejun Heo 	int next_color;
2591b1f4ec17SOleg Nesterov 
25923295f0efSIngo Molnar 	lock_map_acquire(&wq->lockdep_map);
25933295f0efSIngo Molnar 	lock_map_release(&wq->lockdep_map);
259473f53c4aSTejun Heo 
259573f53c4aSTejun Heo 	mutex_lock(&wq->flush_mutex);
259673f53c4aSTejun Heo 
259773f53c4aSTejun Heo 	/*
259873f53c4aSTejun Heo 	 * Start-to-wait phase
259973f53c4aSTejun Heo 	 */
260073f53c4aSTejun Heo 	next_color = work_next_color(wq->work_color);
260173f53c4aSTejun Heo 
260273f53c4aSTejun Heo 	if (next_color != wq->flush_color) {
260373f53c4aSTejun Heo 		/*
260473f53c4aSTejun Heo 		 * Color space is not full.  The current work_color
260573f53c4aSTejun Heo 		 * becomes our flush_color and work_color is advanced
260673f53c4aSTejun Heo 		 * by one.
260773f53c4aSTejun Heo 		 */
26086183c009STejun Heo 		WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
260973f53c4aSTejun Heo 		this_flusher.flush_color = wq->work_color;
261073f53c4aSTejun Heo 		wq->work_color = next_color;
261173f53c4aSTejun Heo 
261273f53c4aSTejun Heo 		if (!wq->first_flusher) {
261373f53c4aSTejun Heo 			/* no flush in progress, become the first flusher */
26146183c009STejun Heo 			WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
261573f53c4aSTejun Heo 
261673f53c4aSTejun Heo 			wq->first_flusher = &this_flusher;
261773f53c4aSTejun Heo 
2618112202d9STejun Heo 			if (!flush_workqueue_prep_pwqs(wq, wq->flush_color,
261973f53c4aSTejun Heo 						       wq->work_color)) {
262073f53c4aSTejun Heo 				/* nothing to flush, done */
262173f53c4aSTejun Heo 				wq->flush_color = next_color;
262273f53c4aSTejun Heo 				wq->first_flusher = NULL;
262373f53c4aSTejun Heo 				goto out_unlock;
262473f53c4aSTejun Heo 			}
262573f53c4aSTejun Heo 		} else {
262673f53c4aSTejun Heo 			/* wait in queue */
26276183c009STejun Heo 			WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
262873f53c4aSTejun Heo 			list_add_tail(&this_flusher.list, &wq->flusher_queue);
2629112202d9STejun Heo 			flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
263073f53c4aSTejun Heo 		}
263173f53c4aSTejun Heo 	} else {
263273f53c4aSTejun Heo 		/*
263373f53c4aSTejun Heo 		 * Oops, color space is full, wait on overflow queue.
263473f53c4aSTejun Heo 		 * The next flush completion will assign us
263573f53c4aSTejun Heo 		 * flush_color and transfer to flusher_queue.
263673f53c4aSTejun Heo 		 */
263773f53c4aSTejun Heo 		list_add_tail(&this_flusher.list, &wq->flusher_overflow);
263873f53c4aSTejun Heo 	}
263973f53c4aSTejun Heo 
264073f53c4aSTejun Heo 	mutex_unlock(&wq->flush_mutex);
264173f53c4aSTejun Heo 
264273f53c4aSTejun Heo 	wait_for_completion(&this_flusher.done);
264373f53c4aSTejun Heo 
264473f53c4aSTejun Heo 	/*
264573f53c4aSTejun Heo 	 * Wake-up-and-cascade phase
264673f53c4aSTejun Heo 	 *
264773f53c4aSTejun Heo 	 * First flushers are responsible for cascading flushes and
264873f53c4aSTejun Heo 	 * handling overflow.  Non-first flushers can simply return.
264973f53c4aSTejun Heo 	 */
265073f53c4aSTejun Heo 	if (wq->first_flusher != &this_flusher)
265173f53c4aSTejun Heo 		return;
265273f53c4aSTejun Heo 
265373f53c4aSTejun Heo 	mutex_lock(&wq->flush_mutex);
265473f53c4aSTejun Heo 
26554ce48b37STejun Heo 	/* we might have raced, check again with mutex held */
26564ce48b37STejun Heo 	if (wq->first_flusher != &this_flusher)
26574ce48b37STejun Heo 		goto out_unlock;
26584ce48b37STejun Heo 
265973f53c4aSTejun Heo 	wq->first_flusher = NULL;
266073f53c4aSTejun Heo 
26616183c009STejun Heo 	WARN_ON_ONCE(!list_empty(&this_flusher.list));
26626183c009STejun Heo 	WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
266373f53c4aSTejun Heo 
266473f53c4aSTejun Heo 	while (true) {
266573f53c4aSTejun Heo 		struct wq_flusher *next, *tmp;
266673f53c4aSTejun Heo 
266773f53c4aSTejun Heo 		/* complete all the flushers sharing the current flush color */
266873f53c4aSTejun Heo 		list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
266973f53c4aSTejun Heo 			if (next->flush_color != wq->flush_color)
267073f53c4aSTejun Heo 				break;
267173f53c4aSTejun Heo 			list_del_init(&next->list);
267273f53c4aSTejun Heo 			complete(&next->done);
267373f53c4aSTejun Heo 		}
267473f53c4aSTejun Heo 
26756183c009STejun Heo 		WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
267673f53c4aSTejun Heo 			     wq->flush_color != work_next_color(wq->work_color));
267773f53c4aSTejun Heo 
267873f53c4aSTejun Heo 		/* this flush_color is finished, advance by one */
267973f53c4aSTejun Heo 		wq->flush_color = work_next_color(wq->flush_color);
268073f53c4aSTejun Heo 
268173f53c4aSTejun Heo 		/* one color has been freed, handle overflow queue */
268273f53c4aSTejun Heo 		if (!list_empty(&wq->flusher_overflow)) {
268373f53c4aSTejun Heo 			/*
268473f53c4aSTejun Heo 			 * Assign the same color to all overflowed
268573f53c4aSTejun Heo 			 * flushers, advance work_color and append to
268673f53c4aSTejun Heo 			 * flusher_queue.  This is the start-to-wait
268773f53c4aSTejun Heo 			 * phase for these overflowed flushers.
268873f53c4aSTejun Heo 			 */
268973f53c4aSTejun Heo 			list_for_each_entry(tmp, &wq->flusher_overflow, list)
269073f53c4aSTejun Heo 				tmp->flush_color = wq->work_color;
269173f53c4aSTejun Heo 
269273f53c4aSTejun Heo 			wq->work_color = work_next_color(wq->work_color);
269373f53c4aSTejun Heo 
269473f53c4aSTejun Heo 			list_splice_tail_init(&wq->flusher_overflow,
269573f53c4aSTejun Heo 					      &wq->flusher_queue);
2696112202d9STejun Heo 			flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
269773f53c4aSTejun Heo 		}
269873f53c4aSTejun Heo 
269973f53c4aSTejun Heo 		if (list_empty(&wq->flusher_queue)) {
27006183c009STejun Heo 			WARN_ON_ONCE(wq->flush_color != wq->work_color);
270173f53c4aSTejun Heo 			break;
270273f53c4aSTejun Heo 		}
270373f53c4aSTejun Heo 
270473f53c4aSTejun Heo 		/*
270573f53c4aSTejun Heo 		 * Need to flush more colors.  Make the next flusher
2706112202d9STejun Heo 		 * the new first flusher and arm pwqs.
270773f53c4aSTejun Heo 		 */
27086183c009STejun Heo 		WARN_ON_ONCE(wq->flush_color == wq->work_color);
27096183c009STejun Heo 		WARN_ON_ONCE(wq->flush_color != next->flush_color);
271073f53c4aSTejun Heo 
271173f53c4aSTejun Heo 		list_del_init(&next->list);
271273f53c4aSTejun Heo 		wq->first_flusher = next;
271373f53c4aSTejun Heo 
2714112202d9STejun Heo 		if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1))
271573f53c4aSTejun Heo 			break;
271673f53c4aSTejun Heo 
271773f53c4aSTejun Heo 		/*
271873f53c4aSTejun Heo 		 * Meh... this color is already done, clear first
271973f53c4aSTejun Heo 		 * flusher and repeat cascading.
272073f53c4aSTejun Heo 		 */
272173f53c4aSTejun Heo 		wq->first_flusher = NULL;
272273f53c4aSTejun Heo 	}
272373f53c4aSTejun Heo 
272473f53c4aSTejun Heo out_unlock:
272573f53c4aSTejun Heo 	mutex_unlock(&wq->flush_mutex);
27261da177e4SLinus Torvalds }
2727ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(flush_workqueue);
27281da177e4SLinus Torvalds 
27299c5a2ba7STejun Heo /**
27309c5a2ba7STejun Heo  * drain_workqueue - drain a workqueue
27319c5a2ba7STejun Heo  * @wq: workqueue to drain
27329c5a2ba7STejun Heo  *
27339c5a2ba7STejun Heo  * Wait until the workqueue becomes empty.  While draining is in progress,
27349c5a2ba7STejun Heo  * only chain queueing is allowed.  IOW, only currently pending or running
27359c5a2ba7STejun Heo  * work items on @wq can queue further work items on it.  @wq is flushed
27369c5a2ba7STejun Heo  * repeatedly until it becomes empty.  The number of flushing is detemined
27379c5a2ba7STejun Heo  * by the depth of chaining and should be relatively short.  Whine if it
27389c5a2ba7STejun Heo  * takes too long.
27399c5a2ba7STejun Heo  */
27409c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq)
27419c5a2ba7STejun Heo {
27429c5a2ba7STejun Heo 	unsigned int flush_cnt = 0;
274349e3cf44STejun Heo 	struct pool_workqueue *pwq;
27449c5a2ba7STejun Heo 
27459c5a2ba7STejun Heo 	/*
27469c5a2ba7STejun Heo 	 * __queue_work() needs to test whether there are drainers, is much
27479c5a2ba7STejun Heo 	 * hotter than drain_workqueue() and already looks at @wq->flags.
27489c5a2ba7STejun Heo 	 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
27499c5a2ba7STejun Heo 	 */
2750e98d5b16STejun Heo 	spin_lock_irq(&workqueue_lock);
27519c5a2ba7STejun Heo 	if (!wq->nr_drainers++)
27529c5a2ba7STejun Heo 		wq->flags |= WQ_DRAINING;
2753e98d5b16STejun Heo 	spin_unlock_irq(&workqueue_lock);
27549c5a2ba7STejun Heo reflush:
27559c5a2ba7STejun Heo 	flush_workqueue(wq);
27569c5a2ba7STejun Heo 
275776af4d93STejun Heo 	local_irq_disable();
275876af4d93STejun Heo 
275949e3cf44STejun Heo 	for_each_pwq(pwq, wq) {
2760fa2563e4SThomas Tuttle 		bool drained;
27619c5a2ba7STejun Heo 
276276af4d93STejun Heo 		spin_lock(&pwq->pool->lock);
2763112202d9STejun Heo 		drained = !pwq->nr_active && list_empty(&pwq->delayed_works);
276476af4d93STejun Heo 		spin_unlock(&pwq->pool->lock);
2765fa2563e4SThomas Tuttle 
2766fa2563e4SThomas Tuttle 		if (drained)
27679c5a2ba7STejun Heo 			continue;
27689c5a2ba7STejun Heo 
27699c5a2ba7STejun Heo 		if (++flush_cnt == 10 ||
27709c5a2ba7STejun Heo 		    (flush_cnt % 100 == 0 && flush_cnt <= 1000))
2771044c782cSValentin Ilie 			pr_warn("workqueue %s: flush on destruction isn't complete after %u tries\n",
27729c5a2ba7STejun Heo 				wq->name, flush_cnt);
277376af4d93STejun Heo 
277476af4d93STejun Heo 		local_irq_enable();
27759c5a2ba7STejun Heo 		goto reflush;
27769c5a2ba7STejun Heo 	}
27779c5a2ba7STejun Heo 
277876af4d93STejun Heo 	spin_lock(&workqueue_lock);
27799c5a2ba7STejun Heo 	if (!--wq->nr_drainers)
27809c5a2ba7STejun Heo 		wq->flags &= ~WQ_DRAINING;
278176af4d93STejun Heo 	spin_unlock(&workqueue_lock);
278276af4d93STejun Heo 
278376af4d93STejun Heo 	local_irq_enable();
27849c5a2ba7STejun Heo }
27859c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue);
27869c5a2ba7STejun Heo 
2787606a5020STejun Heo static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
2788baf59022STejun Heo {
2789baf59022STejun Heo 	struct worker *worker = NULL;
2790c9e7cf27STejun Heo 	struct worker_pool *pool;
2791112202d9STejun Heo 	struct pool_workqueue *pwq;
2792baf59022STejun Heo 
2793baf59022STejun Heo 	might_sleep();
2794baf59022STejun Heo 
2795fa1b54e6STejun Heo 	local_irq_disable();
2796fa1b54e6STejun Heo 	pool = get_work_pool(work);
2797fa1b54e6STejun Heo 	if (!pool) {
2798fa1b54e6STejun Heo 		local_irq_enable();
2799fa1b54e6STejun Heo 		return false;
2800fa1b54e6STejun Heo 	}
2801fa1b54e6STejun Heo 
2802fa1b54e6STejun Heo 	spin_lock(&pool->lock);
28030b3dae68SLai Jiangshan 	/* see the comment in try_to_grab_pending() with the same code */
2804112202d9STejun Heo 	pwq = get_work_pwq(work);
2805112202d9STejun Heo 	if (pwq) {
2806112202d9STejun Heo 		if (unlikely(pwq->pool != pool))
2807baf59022STejun Heo 			goto already_gone;
2808606a5020STejun Heo 	} else {
2809c9e7cf27STejun Heo 		worker = find_worker_executing_work(pool, work);
2810baf59022STejun Heo 		if (!worker)
2811baf59022STejun Heo 			goto already_gone;
2812112202d9STejun Heo 		pwq = worker->current_pwq;
2813606a5020STejun Heo 	}
2814baf59022STejun Heo 
2815112202d9STejun Heo 	insert_wq_barrier(pwq, barr, work, worker);
2816d565ed63STejun Heo 	spin_unlock_irq(&pool->lock);
2817baf59022STejun Heo 
2818e159489bSTejun Heo 	/*
2819e159489bSTejun Heo 	 * If @max_active is 1 or rescuer is in use, flushing another work
2820e159489bSTejun Heo 	 * item on the same workqueue may lead to deadlock.  Make sure the
2821e159489bSTejun Heo 	 * flusher is not running on the same workqueue by verifying write
2822e159489bSTejun Heo 	 * access.
2823e159489bSTejun Heo 	 */
2824493008a8STejun Heo 	if (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)
2825112202d9STejun Heo 		lock_map_acquire(&pwq->wq->lockdep_map);
2826e159489bSTejun Heo 	else
2827112202d9STejun Heo 		lock_map_acquire_read(&pwq->wq->lockdep_map);
2828112202d9STejun Heo 	lock_map_release(&pwq->wq->lockdep_map);
2829e159489bSTejun Heo 
2830baf59022STejun Heo 	return true;
2831baf59022STejun Heo already_gone:
2832d565ed63STejun Heo 	spin_unlock_irq(&pool->lock);
2833baf59022STejun Heo 	return false;
2834baf59022STejun Heo }
2835baf59022STejun Heo 
2836db700897SOleg Nesterov /**
2837401a8d04STejun Heo  * flush_work - wait for a work to finish executing the last queueing instance
2838401a8d04STejun Heo  * @work: the work to flush
2839db700897SOleg Nesterov  *
2840606a5020STejun Heo  * Wait until @work has finished execution.  @work is guaranteed to be idle
2841606a5020STejun Heo  * on return if it hasn't been requeued since flush started.
2842401a8d04STejun Heo  *
2843401a8d04STejun Heo  * RETURNS:
2844401a8d04STejun Heo  * %true if flush_work() waited for the work to finish execution,
2845401a8d04STejun Heo  * %false if it was already idle.
2846db700897SOleg Nesterov  */
2847401a8d04STejun Heo bool flush_work(struct work_struct *work)
2848db700897SOleg Nesterov {
2849db700897SOleg Nesterov 	struct wq_barrier barr;
2850db700897SOleg Nesterov 
28510976dfc1SStephen Boyd 	lock_map_acquire(&work->lockdep_map);
28520976dfc1SStephen Boyd 	lock_map_release(&work->lockdep_map);
28530976dfc1SStephen Boyd 
2854606a5020STejun Heo 	if (start_flush_work(work, &barr)) {
2855db700897SOleg Nesterov 		wait_for_completion(&barr.done);
2856dc186ad7SThomas Gleixner 		destroy_work_on_stack(&barr.work);
2857401a8d04STejun Heo 		return true;
2858606a5020STejun Heo 	} else {
2859401a8d04STejun Heo 		return false;
2860db700897SOleg Nesterov 	}
2861606a5020STejun Heo }
2862db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work);
2863db700897SOleg Nesterov 
286436e227d2STejun Heo static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
2865401a8d04STejun Heo {
2866bbb68dfaSTejun Heo 	unsigned long flags;
28671f1f642eSOleg Nesterov 	int ret;
28681f1f642eSOleg Nesterov 
28691f1f642eSOleg Nesterov 	do {
2870bbb68dfaSTejun Heo 		ret = try_to_grab_pending(work, is_dwork, &flags);
2871bbb68dfaSTejun Heo 		/*
2872bbb68dfaSTejun Heo 		 * If someone else is canceling, wait for the same event it
2873bbb68dfaSTejun Heo 		 * would be waiting for before retrying.
2874bbb68dfaSTejun Heo 		 */
2875bbb68dfaSTejun Heo 		if (unlikely(ret == -ENOENT))
2876606a5020STejun Heo 			flush_work(work);
28771f1f642eSOleg Nesterov 	} while (unlikely(ret < 0));
28781f1f642eSOleg Nesterov 
2879bbb68dfaSTejun Heo 	/* tell other tasks trying to grab @work to back off */
2880bbb68dfaSTejun Heo 	mark_work_canceling(work);
2881bbb68dfaSTejun Heo 	local_irq_restore(flags);
2882bbb68dfaSTejun Heo 
2883606a5020STejun Heo 	flush_work(work);
28847a22ad75STejun Heo 	clear_work_data(work);
28851f1f642eSOleg Nesterov 	return ret;
28861f1f642eSOleg Nesterov }
28871f1f642eSOleg Nesterov 
28886e84d644SOleg Nesterov /**
2889401a8d04STejun Heo  * cancel_work_sync - cancel a work and wait for it to finish
2890401a8d04STejun Heo  * @work: the work to cancel
28916e84d644SOleg Nesterov  *
2892401a8d04STejun Heo  * Cancel @work and wait for its execution to finish.  This function
2893401a8d04STejun Heo  * can be used even if the work re-queues itself or migrates to
2894401a8d04STejun Heo  * another workqueue.  On return from this function, @work is
2895401a8d04STejun Heo  * guaranteed to be not pending or executing on any CPU.
28961f1f642eSOleg Nesterov  *
2897401a8d04STejun Heo  * cancel_work_sync(&delayed_work->work) must not be used for
2898401a8d04STejun Heo  * delayed_work's.  Use cancel_delayed_work_sync() instead.
28996e84d644SOleg Nesterov  *
2900401a8d04STejun Heo  * The caller must ensure that the workqueue on which @work was last
29016e84d644SOleg Nesterov  * queued can't be destroyed before this function returns.
2902401a8d04STejun Heo  *
2903401a8d04STejun Heo  * RETURNS:
2904401a8d04STejun Heo  * %true if @work was pending, %false otherwise.
29056e84d644SOleg Nesterov  */
2906401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work)
29076e84d644SOleg Nesterov {
290836e227d2STejun Heo 	return __cancel_work_timer(work, false);
2909b89deed3SOleg Nesterov }
291028e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync);
2911b89deed3SOleg Nesterov 
29126e84d644SOleg Nesterov /**
2913401a8d04STejun Heo  * flush_delayed_work - wait for a dwork to finish executing the last queueing
2914401a8d04STejun Heo  * @dwork: the delayed work to flush
29156e84d644SOleg Nesterov  *
2916401a8d04STejun Heo  * Delayed timer is cancelled and the pending work is queued for
2917401a8d04STejun Heo  * immediate execution.  Like flush_work(), this function only
2918401a8d04STejun Heo  * considers the last queueing instance of @dwork.
29191f1f642eSOleg Nesterov  *
2920401a8d04STejun Heo  * RETURNS:
2921401a8d04STejun Heo  * %true if flush_work() waited for the work to finish execution,
2922401a8d04STejun Heo  * %false if it was already idle.
29236e84d644SOleg Nesterov  */
2924401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork)
2925401a8d04STejun Heo {
29268930cabaSTejun Heo 	local_irq_disable();
2927401a8d04STejun Heo 	if (del_timer_sync(&dwork->timer))
292860c057bcSLai Jiangshan 		__queue_work(dwork->cpu, dwork->wq, &dwork->work);
29298930cabaSTejun Heo 	local_irq_enable();
2930401a8d04STejun Heo 	return flush_work(&dwork->work);
2931401a8d04STejun Heo }
2932401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work);
2933401a8d04STejun Heo 
2934401a8d04STejun Heo /**
293557b30ae7STejun Heo  * cancel_delayed_work - cancel a delayed work
293657b30ae7STejun Heo  * @dwork: delayed_work to cancel
293709383498STejun Heo  *
293857b30ae7STejun Heo  * Kill off a pending delayed_work.  Returns %true if @dwork was pending
293957b30ae7STejun Heo  * and canceled; %false if wasn't pending.  Note that the work callback
294057b30ae7STejun Heo  * function may still be running on return, unless it returns %true and the
294157b30ae7STejun Heo  * work doesn't re-arm itself.  Explicitly flush or use
294257b30ae7STejun Heo  * cancel_delayed_work_sync() to wait on it.
294309383498STejun Heo  *
294457b30ae7STejun Heo  * This function is safe to call from any context including IRQ handler.
294509383498STejun Heo  */
294657b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork)
294709383498STejun Heo {
294857b30ae7STejun Heo 	unsigned long flags;
294957b30ae7STejun Heo 	int ret;
295057b30ae7STejun Heo 
295157b30ae7STejun Heo 	do {
295257b30ae7STejun Heo 		ret = try_to_grab_pending(&dwork->work, true, &flags);
295357b30ae7STejun Heo 	} while (unlikely(ret == -EAGAIN));
295457b30ae7STejun Heo 
295557b30ae7STejun Heo 	if (unlikely(ret < 0))
295657b30ae7STejun Heo 		return false;
295757b30ae7STejun Heo 
29587c3eed5cSTejun Heo 	set_work_pool_and_clear_pending(&dwork->work,
29597c3eed5cSTejun Heo 					get_work_pool_id(&dwork->work));
296057b30ae7STejun Heo 	local_irq_restore(flags);
2961c0158ca6SDan Magenheimer 	return ret;
296209383498STejun Heo }
296357b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work);
296409383498STejun Heo 
296509383498STejun Heo /**
2966401a8d04STejun Heo  * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2967401a8d04STejun Heo  * @dwork: the delayed work cancel
2968401a8d04STejun Heo  *
2969401a8d04STejun Heo  * This is cancel_work_sync() for delayed works.
2970401a8d04STejun Heo  *
2971401a8d04STejun Heo  * RETURNS:
2972401a8d04STejun Heo  * %true if @dwork was pending, %false otherwise.
2973401a8d04STejun Heo  */
2974401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork)
29756e84d644SOleg Nesterov {
297636e227d2STejun Heo 	return __cancel_work_timer(&dwork->work, true);
29776e84d644SOleg Nesterov }
2978f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync);
29791da177e4SLinus Torvalds 
29800fcb78c2SRolf Eike Beer /**
2981c1a220e7SZhang Rui  * schedule_work_on - put work task on a specific cpu
2982c1a220e7SZhang Rui  * @cpu: cpu to put the work task on
2983c1a220e7SZhang Rui  * @work: job to be done
2984c1a220e7SZhang Rui  *
2985c1a220e7SZhang Rui  * This puts a job on a specific cpu
2986c1a220e7SZhang Rui  */
2987d4283e93STejun Heo bool schedule_work_on(int cpu, struct work_struct *work)
2988c1a220e7SZhang Rui {
2989d320c038STejun Heo 	return queue_work_on(cpu, system_wq, work);
2990c1a220e7SZhang Rui }
2991c1a220e7SZhang Rui EXPORT_SYMBOL(schedule_work_on);
2992c1a220e7SZhang Rui 
29930fcb78c2SRolf Eike Beer /**
2994ae90dd5dSDave Jones  * schedule_work - put work task in global workqueue
29951da177e4SLinus Torvalds  * @work: job to be done
29961da177e4SLinus Torvalds  *
2997d4283e93STejun Heo  * Returns %false if @work was already on the kernel-global workqueue and
2998d4283e93STejun Heo  * %true otherwise.
299952bad64dSDavid Howells  *
30000fcb78c2SRolf Eike Beer  * This puts a job in the kernel-global workqueue if it was not already
30010fcb78c2SRolf Eike Beer  * queued and leaves it in the same position on the kernel-global
30020fcb78c2SRolf Eike Beer  * workqueue otherwise.
30031da177e4SLinus Torvalds  */
3004d4283e93STejun Heo bool schedule_work(struct work_struct *work)
30051da177e4SLinus Torvalds {
30060fcb78c2SRolf Eike Beer 	return queue_work(system_wq, work);
30071da177e4SLinus Torvalds }
30080fcb78c2SRolf Eike Beer EXPORT_SYMBOL(schedule_work);
30091da177e4SLinus Torvalds 
30100fcb78c2SRolf Eike Beer /**
30110fcb78c2SRolf Eike Beer  * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
30120fcb78c2SRolf Eike Beer  * @cpu: cpu to use
30130fcb78c2SRolf Eike Beer  * @dwork: job to be done
30140fcb78c2SRolf Eike Beer  * @delay: number of jiffies to wait
30150fcb78c2SRolf Eike Beer  *
30160fcb78c2SRolf Eike Beer  * After waiting for a given time this puts a job in the kernel-global
30170fcb78c2SRolf Eike Beer  * workqueue on the specified CPU.
30180fcb78c2SRolf Eike Beer  */
3019d4283e93STejun Heo bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
3020d4283e93STejun Heo 			      unsigned long delay)
30211da177e4SLinus Torvalds {
3022d320c038STejun Heo 	return queue_delayed_work_on(cpu, system_wq, dwork, delay);
30231da177e4SLinus Torvalds }
3024ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work_on);
30251da177e4SLinus Torvalds 
3026b6136773SAndrew Morton /**
30270a13c00eSTejun Heo  * schedule_delayed_work - put work task in global workqueue after delay
30280a13c00eSTejun Heo  * @dwork: job to be done
30290a13c00eSTejun Heo  * @delay: number of jiffies to wait or 0 for immediate execution
30300a13c00eSTejun Heo  *
30310a13c00eSTejun Heo  * After waiting for a given time this puts a job in the kernel-global
30320a13c00eSTejun Heo  * workqueue.
30330a13c00eSTejun Heo  */
3034d4283e93STejun Heo bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay)
30350a13c00eSTejun Heo {
30360a13c00eSTejun Heo 	return queue_delayed_work(system_wq, dwork, delay);
30370a13c00eSTejun Heo }
30380a13c00eSTejun Heo EXPORT_SYMBOL(schedule_delayed_work);
30390a13c00eSTejun Heo 
30400a13c00eSTejun Heo /**
304131ddd871STejun Heo  * schedule_on_each_cpu - execute a function synchronously on each online CPU
3042b6136773SAndrew Morton  * @func: the function to call
3043b6136773SAndrew Morton  *
304431ddd871STejun Heo  * schedule_on_each_cpu() executes @func on each online CPU using the
304531ddd871STejun Heo  * system workqueue and blocks until all CPUs have completed.
3046b6136773SAndrew Morton  * schedule_on_each_cpu() is very slow.
304731ddd871STejun Heo  *
304831ddd871STejun Heo  * RETURNS:
304931ddd871STejun Heo  * 0 on success, -errno on failure.
3050b6136773SAndrew Morton  */
305165f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func)
305215316ba8SChristoph Lameter {
305315316ba8SChristoph Lameter 	int cpu;
305438f51568SNamhyung Kim 	struct work_struct __percpu *works;
305515316ba8SChristoph Lameter 
3056b6136773SAndrew Morton 	works = alloc_percpu(struct work_struct);
3057b6136773SAndrew Morton 	if (!works)
305815316ba8SChristoph Lameter 		return -ENOMEM;
3059b6136773SAndrew Morton 
306095402b38SGautham R Shenoy 	get_online_cpus();
306193981800STejun Heo 
306215316ba8SChristoph Lameter 	for_each_online_cpu(cpu) {
30639bfb1839SIngo Molnar 		struct work_struct *work = per_cpu_ptr(works, cpu);
30649bfb1839SIngo Molnar 
30659bfb1839SIngo Molnar 		INIT_WORK(work, func);
30668de6d308SOleg Nesterov 		schedule_work_on(cpu, work);
306715316ba8SChristoph Lameter 	}
306893981800STejun Heo 
306993981800STejun Heo 	for_each_online_cpu(cpu)
30708616a89aSOleg Nesterov 		flush_work(per_cpu_ptr(works, cpu));
307193981800STejun Heo 
307295402b38SGautham R Shenoy 	put_online_cpus();
3073b6136773SAndrew Morton 	free_percpu(works);
307415316ba8SChristoph Lameter 	return 0;
307515316ba8SChristoph Lameter }
307615316ba8SChristoph Lameter 
3077eef6a7d5SAlan Stern /**
3078eef6a7d5SAlan Stern  * flush_scheduled_work - ensure that any scheduled work has run to completion.
3079eef6a7d5SAlan Stern  *
3080eef6a7d5SAlan Stern  * Forces execution of the kernel-global workqueue and blocks until its
3081eef6a7d5SAlan Stern  * completion.
3082eef6a7d5SAlan Stern  *
3083eef6a7d5SAlan Stern  * Think twice before calling this function!  It's very easy to get into
3084eef6a7d5SAlan Stern  * trouble if you don't take great care.  Either of the following situations
3085eef6a7d5SAlan Stern  * will lead to deadlock:
3086eef6a7d5SAlan Stern  *
3087eef6a7d5SAlan Stern  *	One of the work items currently on the workqueue needs to acquire
3088eef6a7d5SAlan Stern  *	a lock held by your code or its caller.
3089eef6a7d5SAlan Stern  *
3090eef6a7d5SAlan Stern  *	Your code is running in the context of a work routine.
3091eef6a7d5SAlan Stern  *
3092eef6a7d5SAlan Stern  * They will be detected by lockdep when they occur, but the first might not
3093eef6a7d5SAlan Stern  * occur very often.  It depends on what work items are on the workqueue and
3094eef6a7d5SAlan Stern  * what locks they need, which you have no control over.
3095eef6a7d5SAlan Stern  *
3096eef6a7d5SAlan Stern  * In most situations flushing the entire workqueue is overkill; you merely
3097eef6a7d5SAlan Stern  * need to know that a particular work item isn't queued and isn't running.
3098eef6a7d5SAlan Stern  * In such cases you should use cancel_delayed_work_sync() or
3099eef6a7d5SAlan Stern  * cancel_work_sync() instead.
3100eef6a7d5SAlan Stern  */
31011da177e4SLinus Torvalds void flush_scheduled_work(void)
31021da177e4SLinus Torvalds {
3103d320c038STejun Heo 	flush_workqueue(system_wq);
31041da177e4SLinus Torvalds }
3105ae90dd5dSDave Jones EXPORT_SYMBOL(flush_scheduled_work);
31061da177e4SLinus Torvalds 
31071da177e4SLinus Torvalds /**
31081fa44ecaSJames Bottomley  * execute_in_process_context - reliably execute the routine with user context
31091fa44ecaSJames Bottomley  * @fn:		the function to execute
31101fa44ecaSJames Bottomley  * @ew:		guaranteed storage for the execute work structure (must
31111fa44ecaSJames Bottomley  *		be available when the work executes)
31121fa44ecaSJames Bottomley  *
31131fa44ecaSJames Bottomley  * Executes the function immediately if process context is available,
31141fa44ecaSJames Bottomley  * otherwise schedules the function for delayed execution.
31151fa44ecaSJames Bottomley  *
31161fa44ecaSJames Bottomley  * Returns:	0 - function was executed
31171fa44ecaSJames Bottomley  *		1 - function was scheduled for execution
31181fa44ecaSJames Bottomley  */
311965f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew)
31201fa44ecaSJames Bottomley {
31211fa44ecaSJames Bottomley 	if (!in_interrupt()) {
312265f27f38SDavid Howells 		fn(&ew->work);
31231fa44ecaSJames Bottomley 		return 0;
31241fa44ecaSJames Bottomley 	}
31251fa44ecaSJames Bottomley 
312665f27f38SDavid Howells 	INIT_WORK(&ew->work, fn);
31271fa44ecaSJames Bottomley 	schedule_work(&ew->work);
31281fa44ecaSJames Bottomley 
31291fa44ecaSJames Bottomley 	return 1;
31301fa44ecaSJames Bottomley }
31311fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context);
31321fa44ecaSJames Bottomley 
31331da177e4SLinus Torvalds int keventd_up(void)
31341da177e4SLinus Torvalds {
3135d320c038STejun Heo 	return system_wq != NULL;
31361da177e4SLinus Torvalds }
31371da177e4SLinus Torvalds 
31387a4e344cSTejun Heo /**
31397a4e344cSTejun Heo  * free_workqueue_attrs - free a workqueue_attrs
31407a4e344cSTejun Heo  * @attrs: workqueue_attrs to free
31417a4e344cSTejun Heo  *
31427a4e344cSTejun Heo  * Undo alloc_workqueue_attrs().
31437a4e344cSTejun Heo  */
31447a4e344cSTejun Heo void free_workqueue_attrs(struct workqueue_attrs *attrs)
31457a4e344cSTejun Heo {
31467a4e344cSTejun Heo 	if (attrs) {
31477a4e344cSTejun Heo 		free_cpumask_var(attrs->cpumask);
31487a4e344cSTejun Heo 		kfree(attrs);
31497a4e344cSTejun Heo 	}
31507a4e344cSTejun Heo }
31517a4e344cSTejun Heo 
31527a4e344cSTejun Heo /**
31537a4e344cSTejun Heo  * alloc_workqueue_attrs - allocate a workqueue_attrs
31547a4e344cSTejun Heo  * @gfp_mask: allocation mask to use
31557a4e344cSTejun Heo  *
31567a4e344cSTejun Heo  * Allocate a new workqueue_attrs, initialize with default settings and
31577a4e344cSTejun Heo  * return it.  Returns NULL on failure.
31587a4e344cSTejun Heo  */
31597a4e344cSTejun Heo struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask)
31607a4e344cSTejun Heo {
31617a4e344cSTejun Heo 	struct workqueue_attrs *attrs;
31627a4e344cSTejun Heo 
31637a4e344cSTejun Heo 	attrs = kzalloc(sizeof(*attrs), gfp_mask);
31647a4e344cSTejun Heo 	if (!attrs)
31657a4e344cSTejun Heo 		goto fail;
31667a4e344cSTejun Heo 	if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask))
31677a4e344cSTejun Heo 		goto fail;
31687a4e344cSTejun Heo 
31697a4e344cSTejun Heo 	cpumask_setall(attrs->cpumask);
31707a4e344cSTejun Heo 	return attrs;
31717a4e344cSTejun Heo fail:
31727a4e344cSTejun Heo 	free_workqueue_attrs(attrs);
31737a4e344cSTejun Heo 	return NULL;
31747a4e344cSTejun Heo }
31757a4e344cSTejun Heo 
317629c91e99STejun Heo static void copy_workqueue_attrs(struct workqueue_attrs *to,
317729c91e99STejun Heo 				 const struct workqueue_attrs *from)
317829c91e99STejun Heo {
317929c91e99STejun Heo 	to->nice = from->nice;
318029c91e99STejun Heo 	cpumask_copy(to->cpumask, from->cpumask);
318129c91e99STejun Heo }
318229c91e99STejun Heo 
318329c91e99STejun Heo /*
318429c91e99STejun Heo  * Hacky implementation of jhash of bitmaps which only considers the
318529c91e99STejun Heo  * specified number of bits.  We probably want a proper implementation in
318629c91e99STejun Heo  * include/linux/jhash.h.
318729c91e99STejun Heo  */
318829c91e99STejun Heo static u32 jhash_bitmap(const unsigned long *bitmap, int bits, u32 hash)
318929c91e99STejun Heo {
319029c91e99STejun Heo 	int nr_longs = bits / BITS_PER_LONG;
319129c91e99STejun Heo 	int nr_leftover = bits % BITS_PER_LONG;
319229c91e99STejun Heo 	unsigned long leftover = 0;
319329c91e99STejun Heo 
319429c91e99STejun Heo 	if (nr_longs)
319529c91e99STejun Heo 		hash = jhash(bitmap, nr_longs * sizeof(long), hash);
319629c91e99STejun Heo 	if (nr_leftover) {
319729c91e99STejun Heo 		bitmap_copy(&leftover, bitmap + nr_longs, nr_leftover);
319829c91e99STejun Heo 		hash = jhash(&leftover, sizeof(long), hash);
319929c91e99STejun Heo 	}
320029c91e99STejun Heo 	return hash;
320129c91e99STejun Heo }
320229c91e99STejun Heo 
320329c91e99STejun Heo /* hash value of the content of @attr */
320429c91e99STejun Heo static u32 wqattrs_hash(const struct workqueue_attrs *attrs)
320529c91e99STejun Heo {
320629c91e99STejun Heo 	u32 hash = 0;
320729c91e99STejun Heo 
320829c91e99STejun Heo 	hash = jhash_1word(attrs->nice, hash);
320929c91e99STejun Heo 	hash = jhash_bitmap(cpumask_bits(attrs->cpumask), nr_cpu_ids, hash);
321029c91e99STejun Heo 	return hash;
321129c91e99STejun Heo }
321229c91e99STejun Heo 
321329c91e99STejun Heo /* content equality test */
321429c91e99STejun Heo static bool wqattrs_equal(const struct workqueue_attrs *a,
321529c91e99STejun Heo 			  const struct workqueue_attrs *b)
321629c91e99STejun Heo {
321729c91e99STejun Heo 	if (a->nice != b->nice)
321829c91e99STejun Heo 		return false;
321929c91e99STejun Heo 	if (!cpumask_equal(a->cpumask, b->cpumask))
322029c91e99STejun Heo 		return false;
322129c91e99STejun Heo 	return true;
322229c91e99STejun Heo }
322329c91e99STejun Heo 
32247a4e344cSTejun Heo /**
32257a4e344cSTejun Heo  * init_worker_pool - initialize a newly zalloc'd worker_pool
32267a4e344cSTejun Heo  * @pool: worker_pool to initialize
32277a4e344cSTejun Heo  *
32287a4e344cSTejun Heo  * Initiailize a newly zalloc'd @pool.  It also allocates @pool->attrs.
322929c91e99STejun Heo  * Returns 0 on success, -errno on failure.  Even on failure, all fields
323029c91e99STejun Heo  * inside @pool proper are initialized and put_unbound_pool() can be called
323129c91e99STejun Heo  * on @pool safely to release it.
32327a4e344cSTejun Heo  */
32337a4e344cSTejun Heo static int init_worker_pool(struct worker_pool *pool)
32344e1a1f9aSTejun Heo {
32354e1a1f9aSTejun Heo 	spin_lock_init(&pool->lock);
323629c91e99STejun Heo 	pool->id = -1;
323729c91e99STejun Heo 	pool->cpu = -1;
32384e1a1f9aSTejun Heo 	pool->flags |= POOL_DISASSOCIATED;
32394e1a1f9aSTejun Heo 	INIT_LIST_HEAD(&pool->worklist);
32404e1a1f9aSTejun Heo 	INIT_LIST_HEAD(&pool->idle_list);
32414e1a1f9aSTejun Heo 	hash_init(pool->busy_hash);
32424e1a1f9aSTejun Heo 
32434e1a1f9aSTejun Heo 	init_timer_deferrable(&pool->idle_timer);
32444e1a1f9aSTejun Heo 	pool->idle_timer.function = idle_worker_timeout;
32454e1a1f9aSTejun Heo 	pool->idle_timer.data = (unsigned long)pool;
32464e1a1f9aSTejun Heo 
32474e1a1f9aSTejun Heo 	setup_timer(&pool->mayday_timer, pool_mayday_timeout,
32484e1a1f9aSTejun Heo 		    (unsigned long)pool);
32494e1a1f9aSTejun Heo 
32504e1a1f9aSTejun Heo 	mutex_init(&pool->manager_arb);
32514e1a1f9aSTejun Heo 	mutex_init(&pool->assoc_mutex);
32524e1a1f9aSTejun Heo 	ida_init(&pool->worker_ida);
32537a4e344cSTejun Heo 
325429c91e99STejun Heo 	INIT_HLIST_NODE(&pool->hash_node);
325529c91e99STejun Heo 	pool->refcnt = 1;
325629c91e99STejun Heo 
325729c91e99STejun Heo 	/* shouldn't fail above this point */
32587a4e344cSTejun Heo 	pool->attrs = alloc_workqueue_attrs(GFP_KERNEL);
32597a4e344cSTejun Heo 	if (!pool->attrs)
32607a4e344cSTejun Heo 		return -ENOMEM;
32617a4e344cSTejun Heo 	return 0;
32624e1a1f9aSTejun Heo }
32634e1a1f9aSTejun Heo 
326429c91e99STejun Heo static void rcu_free_pool(struct rcu_head *rcu)
326529c91e99STejun Heo {
326629c91e99STejun Heo 	struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu);
326729c91e99STejun Heo 
326829c91e99STejun Heo 	ida_destroy(&pool->worker_ida);
326929c91e99STejun Heo 	free_workqueue_attrs(pool->attrs);
327029c91e99STejun Heo 	kfree(pool);
327129c91e99STejun Heo }
327229c91e99STejun Heo 
327329c91e99STejun Heo /**
327429c91e99STejun Heo  * put_unbound_pool - put a worker_pool
327529c91e99STejun Heo  * @pool: worker_pool to put
327629c91e99STejun Heo  *
327729c91e99STejun Heo  * Put @pool.  If its refcnt reaches zero, it gets destroyed in sched-RCU
327829c91e99STejun Heo  * safe manner.
327929c91e99STejun Heo  */
328029c91e99STejun Heo static void put_unbound_pool(struct worker_pool *pool)
328129c91e99STejun Heo {
328229c91e99STejun Heo 	struct worker *worker;
328329c91e99STejun Heo 
328429c91e99STejun Heo 	spin_lock_irq(&workqueue_lock);
328529c91e99STejun Heo 	if (--pool->refcnt) {
328629c91e99STejun Heo 		spin_unlock_irq(&workqueue_lock);
328729c91e99STejun Heo 		return;
328829c91e99STejun Heo 	}
328929c91e99STejun Heo 
329029c91e99STejun Heo 	/* sanity checks */
329129c91e99STejun Heo 	if (WARN_ON(!(pool->flags & POOL_DISASSOCIATED)) ||
329229c91e99STejun Heo 	    WARN_ON(!list_empty(&pool->worklist))) {
329329c91e99STejun Heo 		spin_unlock_irq(&workqueue_lock);
329429c91e99STejun Heo 		return;
329529c91e99STejun Heo 	}
329629c91e99STejun Heo 
329729c91e99STejun Heo 	/* release id and unhash */
329829c91e99STejun Heo 	if (pool->id >= 0)
329929c91e99STejun Heo 		idr_remove(&worker_pool_idr, pool->id);
330029c91e99STejun Heo 	hash_del(&pool->hash_node);
330129c91e99STejun Heo 
330229c91e99STejun Heo 	spin_unlock_irq(&workqueue_lock);
330329c91e99STejun Heo 
330429c91e99STejun Heo 	/* lock out manager and destroy all workers */
330529c91e99STejun Heo 	mutex_lock(&pool->manager_arb);
330629c91e99STejun Heo 	spin_lock_irq(&pool->lock);
330729c91e99STejun Heo 
330829c91e99STejun Heo 	while ((worker = first_worker(pool)))
330929c91e99STejun Heo 		destroy_worker(worker);
331029c91e99STejun Heo 	WARN_ON(pool->nr_workers || pool->nr_idle);
331129c91e99STejun Heo 
331229c91e99STejun Heo 	spin_unlock_irq(&pool->lock);
331329c91e99STejun Heo 	mutex_unlock(&pool->manager_arb);
331429c91e99STejun Heo 
331529c91e99STejun Heo 	/* shut down the timers */
331629c91e99STejun Heo 	del_timer_sync(&pool->idle_timer);
331729c91e99STejun Heo 	del_timer_sync(&pool->mayday_timer);
331829c91e99STejun Heo 
331929c91e99STejun Heo 	/* sched-RCU protected to allow dereferences from get_work_pool() */
332029c91e99STejun Heo 	call_rcu_sched(&pool->rcu, rcu_free_pool);
332129c91e99STejun Heo }
332229c91e99STejun Heo 
332329c91e99STejun Heo /**
332429c91e99STejun Heo  * get_unbound_pool - get a worker_pool with the specified attributes
332529c91e99STejun Heo  * @attrs: the attributes of the worker_pool to get
332629c91e99STejun Heo  *
332729c91e99STejun Heo  * Obtain a worker_pool which has the same attributes as @attrs, bump the
332829c91e99STejun Heo  * reference count and return it.  If there already is a matching
332929c91e99STejun Heo  * worker_pool, it will be used; otherwise, this function attempts to
333029c91e99STejun Heo  * create a new one.  On failure, returns NULL.
333129c91e99STejun Heo  */
333229c91e99STejun Heo static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
333329c91e99STejun Heo {
333429c91e99STejun Heo 	static DEFINE_MUTEX(create_mutex);
333529c91e99STejun Heo 	u32 hash = wqattrs_hash(attrs);
333629c91e99STejun Heo 	struct worker_pool *pool;
333729c91e99STejun Heo 	struct worker *worker;
333829c91e99STejun Heo 
333929c91e99STejun Heo 	mutex_lock(&create_mutex);
334029c91e99STejun Heo 
334129c91e99STejun Heo 	/* do we already have a matching pool? */
334229c91e99STejun Heo 	spin_lock_irq(&workqueue_lock);
334329c91e99STejun Heo 	hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) {
334429c91e99STejun Heo 		if (wqattrs_equal(pool->attrs, attrs)) {
334529c91e99STejun Heo 			pool->refcnt++;
334629c91e99STejun Heo 			goto out_unlock;
334729c91e99STejun Heo 		}
334829c91e99STejun Heo 	}
334929c91e99STejun Heo 	spin_unlock_irq(&workqueue_lock);
335029c91e99STejun Heo 
335129c91e99STejun Heo 	/* nope, create a new one */
335229c91e99STejun Heo 	pool = kzalloc(sizeof(*pool), GFP_KERNEL);
335329c91e99STejun Heo 	if (!pool || init_worker_pool(pool) < 0)
335429c91e99STejun Heo 		goto fail;
335529c91e99STejun Heo 
3356*8864b4e5STejun Heo 	lockdep_set_subclass(&pool->lock, 1);	/* see put_pwq() */
335729c91e99STejun Heo 	copy_workqueue_attrs(pool->attrs, attrs);
335829c91e99STejun Heo 
335929c91e99STejun Heo 	if (worker_pool_assign_id(pool) < 0)
336029c91e99STejun Heo 		goto fail;
336129c91e99STejun Heo 
336229c91e99STejun Heo 	/* create and start the initial worker */
336329c91e99STejun Heo 	worker = create_worker(pool);
336429c91e99STejun Heo 	if (!worker)
336529c91e99STejun Heo 		goto fail;
336629c91e99STejun Heo 
336729c91e99STejun Heo 	spin_lock_irq(&pool->lock);
336829c91e99STejun Heo 	start_worker(worker);
336929c91e99STejun Heo 	spin_unlock_irq(&pool->lock);
337029c91e99STejun Heo 
337129c91e99STejun Heo 	/* install */
337229c91e99STejun Heo 	spin_lock_irq(&workqueue_lock);
337329c91e99STejun Heo 	hash_add(unbound_pool_hash, &pool->hash_node, hash);
337429c91e99STejun Heo out_unlock:
337529c91e99STejun Heo 	spin_unlock_irq(&workqueue_lock);
337629c91e99STejun Heo 	mutex_unlock(&create_mutex);
337729c91e99STejun Heo 	return pool;
337829c91e99STejun Heo fail:
337929c91e99STejun Heo 	mutex_unlock(&create_mutex);
338029c91e99STejun Heo 	if (pool)
338129c91e99STejun Heo 		put_unbound_pool(pool);
338229c91e99STejun Heo 	return NULL;
338329c91e99STejun Heo }
338429c91e99STejun Heo 
3385*8864b4e5STejun Heo static void rcu_free_pwq(struct rcu_head *rcu)
3386*8864b4e5STejun Heo {
3387*8864b4e5STejun Heo 	kmem_cache_free(pwq_cache,
3388*8864b4e5STejun Heo 			container_of(rcu, struct pool_workqueue, rcu));
3389*8864b4e5STejun Heo }
3390*8864b4e5STejun Heo 
3391*8864b4e5STejun Heo /*
3392*8864b4e5STejun Heo  * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt
3393*8864b4e5STejun Heo  * and needs to be destroyed.
3394*8864b4e5STejun Heo  */
3395*8864b4e5STejun Heo static void pwq_unbound_release_workfn(struct work_struct *work)
3396*8864b4e5STejun Heo {
3397*8864b4e5STejun Heo 	struct pool_workqueue *pwq = container_of(work, struct pool_workqueue,
3398*8864b4e5STejun Heo 						  unbound_release_work);
3399*8864b4e5STejun Heo 	struct workqueue_struct *wq = pwq->wq;
3400*8864b4e5STejun Heo 	struct worker_pool *pool = pwq->pool;
3401*8864b4e5STejun Heo 
3402*8864b4e5STejun Heo 	if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)))
3403*8864b4e5STejun Heo 		return;
3404*8864b4e5STejun Heo 
3405*8864b4e5STejun Heo 	spin_lock_irq(&workqueue_lock);
3406*8864b4e5STejun Heo 	list_del_rcu(&pwq->pwqs_node);
3407*8864b4e5STejun Heo 	spin_unlock_irq(&workqueue_lock);
3408*8864b4e5STejun Heo 
3409*8864b4e5STejun Heo 	put_unbound_pool(pool);
3410*8864b4e5STejun Heo 	call_rcu_sched(&pwq->rcu, rcu_free_pwq);
3411*8864b4e5STejun Heo 
3412*8864b4e5STejun Heo 	/*
3413*8864b4e5STejun Heo 	 * If we're the last pwq going away, @wq is already dead and no one
3414*8864b4e5STejun Heo 	 * is gonna access it anymore.  Free it.
3415*8864b4e5STejun Heo 	 */
3416*8864b4e5STejun Heo 	if (list_empty(&wq->pwqs))
3417*8864b4e5STejun Heo 		kfree(wq);
3418*8864b4e5STejun Heo }
3419*8864b4e5STejun Heo 
3420d2c1d404STejun Heo static void init_and_link_pwq(struct pool_workqueue *pwq,
3421d2c1d404STejun Heo 			      struct workqueue_struct *wq,
3422d2c1d404STejun Heo 			      struct worker_pool *pool)
3423d2c1d404STejun Heo {
3424d2c1d404STejun Heo 	BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
3425d2c1d404STejun Heo 
3426d2c1d404STejun Heo 	pwq->pool = pool;
3427d2c1d404STejun Heo 	pwq->wq = wq;
3428d2c1d404STejun Heo 	pwq->flush_color = -1;
3429*8864b4e5STejun Heo 	pwq->refcnt = 1;
3430d2c1d404STejun Heo 	pwq->max_active = wq->saved_max_active;
3431d2c1d404STejun Heo 	INIT_LIST_HEAD(&pwq->delayed_works);
3432d2c1d404STejun Heo 	INIT_LIST_HEAD(&pwq->mayday_node);
3433*8864b4e5STejun Heo 	INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn);
3434d2c1d404STejun Heo 
3435d2c1d404STejun Heo 	list_add_tail_rcu(&pwq->pwqs_node, &wq->pwqs);
3436d2c1d404STejun Heo }
3437d2c1d404STejun Heo 
343830cdf249STejun Heo static int alloc_and_link_pwqs(struct workqueue_struct *wq)
34391da177e4SLinus Torvalds {
344049e3cf44STejun Heo 	bool highpri = wq->flags & WQ_HIGHPRI;
344130cdf249STejun Heo 	int cpu;
3442e1d8aa9fSFrederic Weisbecker 
344330cdf249STejun Heo 	if (!(wq->flags & WQ_UNBOUND)) {
3444420c0ddbSTejun Heo 		wq->cpu_pwqs = alloc_percpu(struct pool_workqueue);
3445420c0ddbSTejun Heo 		if (!wq->cpu_pwqs)
344630cdf249STejun Heo 			return -ENOMEM;
344730cdf249STejun Heo 
344830cdf249STejun Heo 		for_each_possible_cpu(cpu) {
34497fb98ea7STejun Heo 			struct pool_workqueue *pwq =
34507fb98ea7STejun Heo 				per_cpu_ptr(wq->cpu_pwqs, cpu);
34517a62c2c8STejun Heo 			struct worker_pool *cpu_pools =
3452f02ae73aSTejun Heo 				per_cpu(cpu_worker_pools, cpu);
345330cdf249STejun Heo 
3454d2c1d404STejun Heo 			init_and_link_pwq(pwq, wq, &cpu_pools[highpri]);
345530cdf249STejun Heo 		}
345630cdf249STejun Heo 	} else {
345730cdf249STejun Heo 		struct pool_workqueue *pwq;
3458d2c1d404STejun Heo 		struct worker_pool *pool;
345930cdf249STejun Heo 
346030cdf249STejun Heo 		pwq = kmem_cache_zalloc(pwq_cache, GFP_KERNEL);
346130cdf249STejun Heo 		if (!pwq)
346230cdf249STejun Heo 			return -ENOMEM;
346330cdf249STejun Heo 
3464d2c1d404STejun Heo 		pool = get_unbound_pool(unbound_std_wq_attrs[highpri]);
3465d2c1d404STejun Heo 		if (!pool) {
346629c91e99STejun Heo 			kmem_cache_free(pwq_cache, pwq);
346729c91e99STejun Heo 			return -ENOMEM;
346829c91e99STejun Heo 		}
346929c91e99STejun Heo 
3470d2c1d404STejun Heo 		init_and_link_pwq(pwq, wq, pool);
347130cdf249STejun Heo 	}
347230cdf249STejun Heo 
347330cdf249STejun Heo 	return 0;
34740f900049STejun Heo }
34750f900049STejun Heo 
3476f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags,
3477f3421797STejun Heo 			       const char *name)
3478b71ab8c2STejun Heo {
3479f3421797STejun Heo 	int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3480f3421797STejun Heo 
3481f3421797STejun Heo 	if (max_active < 1 || max_active > lim)
3482044c782cSValentin Ilie 		pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
3483f3421797STejun Heo 			max_active, name, 1, lim);
3484b71ab8c2STejun Heo 
3485f3421797STejun Heo 	return clamp_val(max_active, 1, lim);
3486b71ab8c2STejun Heo }
3487b71ab8c2STejun Heo 
3488b196be89STejun Heo struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
348997e37d7bSTejun Heo 					       unsigned int flags,
34901e19ffc6STejun Heo 					       int max_active,
3491eb13ba87SJohannes Berg 					       struct lock_class_key *key,
3492b196be89STejun Heo 					       const char *lock_name, ...)
34933af24433SOleg Nesterov {
3494b196be89STejun Heo 	va_list args, args1;
34953af24433SOleg Nesterov 	struct workqueue_struct *wq;
349649e3cf44STejun Heo 	struct pool_workqueue *pwq;
3497b196be89STejun Heo 	size_t namelen;
3498b196be89STejun Heo 
3499b196be89STejun Heo 	/* determine namelen, allocate wq and format name */
3500b196be89STejun Heo 	va_start(args, lock_name);
3501b196be89STejun Heo 	va_copy(args1, args);
3502b196be89STejun Heo 	namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3503b196be89STejun Heo 
3504b196be89STejun Heo 	wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3505b196be89STejun Heo 	if (!wq)
3506d2c1d404STejun Heo 		return NULL;
3507b196be89STejun Heo 
3508b196be89STejun Heo 	vsnprintf(wq->name, namelen, fmt, args1);
3509b196be89STejun Heo 	va_end(args);
3510b196be89STejun Heo 	va_end(args1);
35113af24433SOleg Nesterov 
3512d320c038STejun Heo 	max_active = max_active ?: WQ_DFL_ACTIVE;
3513b196be89STejun Heo 	max_active = wq_clamp_max_active(max_active, flags, wq->name);
35143af24433SOleg Nesterov 
3515b196be89STejun Heo 	/* init wq */
351697e37d7bSTejun Heo 	wq->flags = flags;
3517a0a1a5fdSTejun Heo 	wq->saved_max_active = max_active;
351873f53c4aSTejun Heo 	mutex_init(&wq->flush_mutex);
3519112202d9STejun Heo 	atomic_set(&wq->nr_pwqs_to_flush, 0);
352030cdf249STejun Heo 	INIT_LIST_HEAD(&wq->pwqs);
352173f53c4aSTejun Heo 	INIT_LIST_HEAD(&wq->flusher_queue);
352273f53c4aSTejun Heo 	INIT_LIST_HEAD(&wq->flusher_overflow);
3523493a1724STejun Heo 	INIT_LIST_HEAD(&wq->maydays);
35243af24433SOleg Nesterov 
3525eb13ba87SJohannes Berg 	lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
3526cce1a165SOleg Nesterov 	INIT_LIST_HEAD(&wq->list);
35273af24433SOleg Nesterov 
352830cdf249STejun Heo 	if (alloc_and_link_pwqs(wq) < 0)
3529d2c1d404STejun Heo 		goto err_free_wq;
35301537663fSTejun Heo 
3531493008a8STejun Heo 	/*
3532493008a8STejun Heo 	 * Workqueues which may be used during memory reclaim should
3533493008a8STejun Heo 	 * have a rescuer to guarantee forward progress.
3534493008a8STejun Heo 	 */
3535493008a8STejun Heo 	if (flags & WQ_MEM_RECLAIM) {
3536e22bee78STejun Heo 		struct worker *rescuer;
3537e22bee78STejun Heo 
3538d2c1d404STejun Heo 		rescuer = alloc_worker();
3539e22bee78STejun Heo 		if (!rescuer)
3540d2c1d404STejun Heo 			goto err_destroy;
3541e22bee78STejun Heo 
3542111c225aSTejun Heo 		rescuer->rescue_wq = wq;
3543111c225aSTejun Heo 		rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
3544b196be89STejun Heo 					       wq->name);
3545d2c1d404STejun Heo 		if (IS_ERR(rescuer->task)) {
3546d2c1d404STejun Heo 			kfree(rescuer);
3547d2c1d404STejun Heo 			goto err_destroy;
3548d2c1d404STejun Heo 		}
3549e22bee78STejun Heo 
3550d2c1d404STejun Heo 		wq->rescuer = rescuer;
3551e22bee78STejun Heo 		rescuer->task->flags |= PF_THREAD_BOUND;
3552e22bee78STejun Heo 		wake_up_process(rescuer->task);
35533af24433SOleg Nesterov 	}
35541537663fSTejun Heo 
35553af24433SOleg Nesterov 	/*
3556a0a1a5fdSTejun Heo 	 * workqueue_lock protects global freeze state and workqueues
3557a0a1a5fdSTejun Heo 	 * list.  Grab it, set max_active accordingly and add the new
3558a0a1a5fdSTejun Heo 	 * workqueue to workqueues list.
35593af24433SOleg Nesterov 	 */
3560e98d5b16STejun Heo 	spin_lock_irq(&workqueue_lock);
3561a0a1a5fdSTejun Heo 
356258a69cb4STejun Heo 	if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
356349e3cf44STejun Heo 		for_each_pwq(pwq, wq)
356449e3cf44STejun Heo 			pwq->max_active = 0;
3565a0a1a5fdSTejun Heo 
35663af24433SOleg Nesterov 	list_add(&wq->list, &workqueues);
3567a0a1a5fdSTejun Heo 
3568e98d5b16STejun Heo 	spin_unlock_irq(&workqueue_lock);
35693af24433SOleg Nesterov 
35703af24433SOleg Nesterov 	return wq;
3571d2c1d404STejun Heo 
3572d2c1d404STejun Heo err_free_wq:
35734690c4abSTejun Heo 	kfree(wq);
3574d2c1d404STejun Heo 	return NULL;
3575d2c1d404STejun Heo err_destroy:
3576d2c1d404STejun Heo 	destroy_workqueue(wq);
35774690c4abSTejun Heo 	return NULL;
35781da177e4SLinus Torvalds }
3579d320c038STejun Heo EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
35801da177e4SLinus Torvalds 
35813af24433SOleg Nesterov /**
35823af24433SOleg Nesterov  * destroy_workqueue - safely terminate a workqueue
35833af24433SOleg Nesterov  * @wq: target workqueue
35843af24433SOleg Nesterov  *
35853af24433SOleg Nesterov  * Safely destroy a workqueue. All work currently pending will be done first.
35863af24433SOleg Nesterov  */
35873af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq)
35883af24433SOleg Nesterov {
358949e3cf44STejun Heo 	struct pool_workqueue *pwq;
35903af24433SOleg Nesterov 
35919c5a2ba7STejun Heo 	/* drain it before proceeding with destruction */
35929c5a2ba7STejun Heo 	drain_workqueue(wq);
3593c8efcc25STejun Heo 
359476af4d93STejun Heo 	spin_lock_irq(&workqueue_lock);
359576af4d93STejun Heo 
35966183c009STejun Heo 	/* sanity checks */
359749e3cf44STejun Heo 	for_each_pwq(pwq, wq) {
35986183c009STejun Heo 		int i;
35996183c009STejun Heo 
360076af4d93STejun Heo 		for (i = 0; i < WORK_NR_COLORS; i++) {
360176af4d93STejun Heo 			if (WARN_ON(pwq->nr_in_flight[i])) {
360276af4d93STejun Heo 				spin_unlock_irq(&workqueue_lock);
36036183c009STejun Heo 				return;
360476af4d93STejun Heo 			}
360576af4d93STejun Heo 		}
360676af4d93STejun Heo 
3607*8864b4e5STejun Heo 		if (WARN_ON(pwq->refcnt > 1) ||
3608*8864b4e5STejun Heo 		    WARN_ON(pwq->nr_active) ||
360976af4d93STejun Heo 		    WARN_ON(!list_empty(&pwq->delayed_works))) {
361076af4d93STejun Heo 			spin_unlock_irq(&workqueue_lock);
36116183c009STejun Heo 			return;
36126183c009STejun Heo 		}
361376af4d93STejun Heo 	}
36146183c009STejun Heo 
3615a0a1a5fdSTejun Heo 	/*
3616a0a1a5fdSTejun Heo 	 * wq list is used to freeze wq, remove from list after
3617a0a1a5fdSTejun Heo 	 * flushing is complete in case freeze races us.
3618a0a1a5fdSTejun Heo 	 */
3619d2c1d404STejun Heo 	list_del_init(&wq->list);
362076af4d93STejun Heo 
3621e98d5b16STejun Heo 	spin_unlock_irq(&workqueue_lock);
36223af24433SOleg Nesterov 
3623493008a8STejun Heo 	if (wq->rescuer) {
3624e22bee78STejun Heo 		kthread_stop(wq->rescuer->task);
36258d9df9f0SXiaotian Feng 		kfree(wq->rescuer);
3626493008a8STejun Heo 		wq->rescuer = NULL;
3627e22bee78STejun Heo 	}
3628e22bee78STejun Heo 
3629*8864b4e5STejun Heo 	if (!(wq->flags & WQ_UNBOUND)) {
363029c91e99STejun Heo 		/*
3631*8864b4e5STejun Heo 		 * The base ref is never dropped on per-cpu pwqs.  Directly
3632*8864b4e5STejun Heo 		 * free the pwqs and wq.
363329c91e99STejun Heo 		 */
3634*8864b4e5STejun Heo 		free_percpu(wq->cpu_pwqs);
3635*8864b4e5STejun Heo 		kfree(wq);
3636*8864b4e5STejun Heo 	} else {
3637*8864b4e5STejun Heo 		/*
3638*8864b4e5STejun Heo 		 * We're the sole accessor of @wq at this point.  Directly
3639*8864b4e5STejun Heo 		 * access the first pwq and put the base ref.  As both pwqs
3640*8864b4e5STejun Heo 		 * and pools are sched-RCU protected, the lock operations
3641*8864b4e5STejun Heo 		 * are safe.  @wq will be freed when the last pwq is
3642*8864b4e5STejun Heo 		 * released.
3643*8864b4e5STejun Heo 		 */
364429c91e99STejun Heo 		pwq = list_first_entry(&wq->pwqs, struct pool_workqueue,
364529c91e99STejun Heo 				       pwqs_node);
3646*8864b4e5STejun Heo 		spin_lock_irq(&pwq->pool->lock);
3647*8864b4e5STejun Heo 		put_pwq(pwq);
3648*8864b4e5STejun Heo 		spin_unlock_irq(&pwq->pool->lock);
364929c91e99STejun Heo 	}
36503af24433SOleg Nesterov }
36513af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue);
36523af24433SOleg Nesterov 
3653dcd989cbSTejun Heo /**
3654112202d9STejun Heo  * pwq_set_max_active - adjust max_active of a pwq
3655112202d9STejun Heo  * @pwq: target pool_workqueue
36569f4bd4cdSLai Jiangshan  * @max_active: new max_active value.
36579f4bd4cdSLai Jiangshan  *
3658112202d9STejun Heo  * Set @pwq->max_active to @max_active and activate delayed works if
36599f4bd4cdSLai Jiangshan  * increased.
36609f4bd4cdSLai Jiangshan  *
36619f4bd4cdSLai Jiangshan  * CONTEXT:
3662d565ed63STejun Heo  * spin_lock_irq(pool->lock).
36639f4bd4cdSLai Jiangshan  */
3664112202d9STejun Heo static void pwq_set_max_active(struct pool_workqueue *pwq, int max_active)
36659f4bd4cdSLai Jiangshan {
3666112202d9STejun Heo 	pwq->max_active = max_active;
36679f4bd4cdSLai Jiangshan 
3668112202d9STejun Heo 	while (!list_empty(&pwq->delayed_works) &&
3669112202d9STejun Heo 	       pwq->nr_active < pwq->max_active)
3670112202d9STejun Heo 		pwq_activate_first_delayed(pwq);
36719f4bd4cdSLai Jiangshan }
36729f4bd4cdSLai Jiangshan 
36739f4bd4cdSLai Jiangshan /**
3674dcd989cbSTejun Heo  * workqueue_set_max_active - adjust max_active of a workqueue
3675dcd989cbSTejun Heo  * @wq: target workqueue
3676dcd989cbSTejun Heo  * @max_active: new max_active value.
3677dcd989cbSTejun Heo  *
3678dcd989cbSTejun Heo  * Set max_active of @wq to @max_active.
3679dcd989cbSTejun Heo  *
3680dcd989cbSTejun Heo  * CONTEXT:
3681dcd989cbSTejun Heo  * Don't call from IRQ context.
3682dcd989cbSTejun Heo  */
3683dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3684dcd989cbSTejun Heo {
368549e3cf44STejun Heo 	struct pool_workqueue *pwq;
3686dcd989cbSTejun Heo 
3687f3421797STejun Heo 	max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
3688dcd989cbSTejun Heo 
3689e98d5b16STejun Heo 	spin_lock_irq(&workqueue_lock);
3690dcd989cbSTejun Heo 
3691dcd989cbSTejun Heo 	wq->saved_max_active = max_active;
3692dcd989cbSTejun Heo 
369349e3cf44STejun Heo 	for_each_pwq(pwq, wq) {
3694112202d9STejun Heo 		struct worker_pool *pool = pwq->pool;
3695dcd989cbSTejun Heo 
3696e98d5b16STejun Heo 		spin_lock(&pool->lock);
3697dcd989cbSTejun Heo 
369858a69cb4STejun Heo 		if (!(wq->flags & WQ_FREEZABLE) ||
369935b6bb63STejun Heo 		    !(pool->flags & POOL_FREEZING))
3700112202d9STejun Heo 			pwq_set_max_active(pwq, max_active);
3701dcd989cbSTejun Heo 
3702e98d5b16STejun Heo 		spin_unlock(&pool->lock);
3703dcd989cbSTejun Heo 	}
3704dcd989cbSTejun Heo 
3705e98d5b16STejun Heo 	spin_unlock_irq(&workqueue_lock);
3706dcd989cbSTejun Heo }
3707dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3708dcd989cbSTejun Heo 
3709dcd989cbSTejun Heo /**
3710dcd989cbSTejun Heo  * workqueue_congested - test whether a workqueue is congested
3711dcd989cbSTejun Heo  * @cpu: CPU in question
3712dcd989cbSTejun Heo  * @wq: target workqueue
3713dcd989cbSTejun Heo  *
3714dcd989cbSTejun Heo  * Test whether @wq's cpu workqueue for @cpu is congested.  There is
3715dcd989cbSTejun Heo  * no synchronization around this function and the test result is
3716dcd989cbSTejun Heo  * unreliable and only useful as advisory hints or for debugging.
3717dcd989cbSTejun Heo  *
3718dcd989cbSTejun Heo  * RETURNS:
3719dcd989cbSTejun Heo  * %true if congested, %false otherwise.
3720dcd989cbSTejun Heo  */
3721d84ff051STejun Heo bool workqueue_congested(int cpu, struct workqueue_struct *wq)
3722dcd989cbSTejun Heo {
37237fb98ea7STejun Heo 	struct pool_workqueue *pwq;
372476af4d93STejun Heo 	bool ret;
372576af4d93STejun Heo 
372676af4d93STejun Heo 	preempt_disable();
37277fb98ea7STejun Heo 
37287fb98ea7STejun Heo 	if (!(wq->flags & WQ_UNBOUND))
37297fb98ea7STejun Heo 		pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
37307fb98ea7STejun Heo 	else
37317fb98ea7STejun Heo 		pwq = first_pwq(wq);
3732dcd989cbSTejun Heo 
373376af4d93STejun Heo 	ret = !list_empty(&pwq->delayed_works);
373476af4d93STejun Heo 	preempt_enable();
373576af4d93STejun Heo 
373676af4d93STejun Heo 	return ret;
3737dcd989cbSTejun Heo }
3738dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested);
3739dcd989cbSTejun Heo 
3740dcd989cbSTejun Heo /**
3741dcd989cbSTejun Heo  * work_busy - test whether a work is currently pending or running
3742dcd989cbSTejun Heo  * @work: the work to be tested
3743dcd989cbSTejun Heo  *
3744dcd989cbSTejun Heo  * Test whether @work is currently pending or running.  There is no
3745dcd989cbSTejun Heo  * synchronization around this function and the test result is
3746dcd989cbSTejun Heo  * unreliable and only useful as advisory hints or for debugging.
3747dcd989cbSTejun Heo  *
3748dcd989cbSTejun Heo  * RETURNS:
3749dcd989cbSTejun Heo  * OR'd bitmask of WORK_BUSY_* bits.
3750dcd989cbSTejun Heo  */
3751dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work)
3752dcd989cbSTejun Heo {
3753fa1b54e6STejun Heo 	struct worker_pool *pool;
3754dcd989cbSTejun Heo 	unsigned long flags;
3755dcd989cbSTejun Heo 	unsigned int ret = 0;
3756dcd989cbSTejun Heo 
3757dcd989cbSTejun Heo 	if (work_pending(work))
3758dcd989cbSTejun Heo 		ret |= WORK_BUSY_PENDING;
3759038366c5SLai Jiangshan 
3760fa1b54e6STejun Heo 	local_irq_save(flags);
3761fa1b54e6STejun Heo 	pool = get_work_pool(work);
3762038366c5SLai Jiangshan 	if (pool) {
3763fa1b54e6STejun Heo 		spin_lock(&pool->lock);
3764c9e7cf27STejun Heo 		if (find_worker_executing_work(pool, work))
3765dcd989cbSTejun Heo 			ret |= WORK_BUSY_RUNNING;
3766fa1b54e6STejun Heo 		spin_unlock(&pool->lock);
3767038366c5SLai Jiangshan 	}
3768fa1b54e6STejun Heo 	local_irq_restore(flags);
3769dcd989cbSTejun Heo 
3770dcd989cbSTejun Heo 	return ret;
3771dcd989cbSTejun Heo }
3772dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy);
3773dcd989cbSTejun Heo 
3774db7bccf4STejun Heo /*
3775db7bccf4STejun Heo  * CPU hotplug.
3776db7bccf4STejun Heo  *
3777e22bee78STejun Heo  * There are two challenges in supporting CPU hotplug.  Firstly, there
3778112202d9STejun Heo  * are a lot of assumptions on strong associations among work, pwq and
3779706026c2STejun Heo  * pool which make migrating pending and scheduled works very
3780e22bee78STejun Heo  * difficult to implement without impacting hot paths.  Secondly,
378194cf58bbSTejun Heo  * worker pools serve mix of short, long and very long running works making
3782e22bee78STejun Heo  * blocked draining impractical.
3783e22bee78STejun Heo  *
378424647570STejun Heo  * This is solved by allowing the pools to be disassociated from the CPU
3785628c78e7STejun Heo  * running as an unbound one and allowing it to be reattached later if the
3786628c78e7STejun Heo  * cpu comes back online.
3787db7bccf4STejun Heo  */
3788db7bccf4STejun Heo 
3789706026c2STejun Heo static void wq_unbind_fn(struct work_struct *work)
3790db7bccf4STejun Heo {
379138db41d9STejun Heo 	int cpu = smp_processor_id();
37924ce62e9eSTejun Heo 	struct worker_pool *pool;
3793db7bccf4STejun Heo 	struct worker *worker;
3794db7bccf4STejun Heo 	int i;
3795db7bccf4STejun Heo 
3796f02ae73aSTejun Heo 	for_each_cpu_worker_pool(pool, cpu) {
37976183c009STejun Heo 		WARN_ON_ONCE(cpu != smp_processor_id());
3798db7bccf4STejun Heo 
379994cf58bbSTejun Heo 		mutex_lock(&pool->assoc_mutex);
380094cf58bbSTejun Heo 		spin_lock_irq(&pool->lock);
3801e22bee78STejun Heo 
3802f2d5a0eeSTejun Heo 		/*
380394cf58bbSTejun Heo 		 * We've claimed all manager positions.  Make all workers
380494cf58bbSTejun Heo 		 * unbound and set DISASSOCIATED.  Before this, all workers
380594cf58bbSTejun Heo 		 * except for the ones which are still executing works from
380694cf58bbSTejun Heo 		 * before the last CPU down must be on the cpu.  After
380794cf58bbSTejun Heo 		 * this, they may become diasporas.
3808f2d5a0eeSTejun Heo 		 */
38094ce62e9eSTejun Heo 		list_for_each_entry(worker, &pool->idle_list, entry)
3810403c821dSTejun Heo 			worker->flags |= WORKER_UNBOUND;
3811db7bccf4STejun Heo 
3812b67bfe0dSSasha Levin 		for_each_busy_worker(worker, i, pool)
3813403c821dSTejun Heo 			worker->flags |= WORKER_UNBOUND;
3814db7bccf4STejun Heo 
381524647570STejun Heo 		pool->flags |= POOL_DISASSOCIATED;
3816f2d5a0eeSTejun Heo 
381794cf58bbSTejun Heo 		spin_unlock_irq(&pool->lock);
381894cf58bbSTejun Heo 		mutex_unlock(&pool->assoc_mutex);
381994cf58bbSTejun Heo 	}
3820e22bee78STejun Heo 
3821e22bee78STejun Heo 	/*
3822628c78e7STejun Heo 	 * Call schedule() so that we cross rq->lock and thus can guarantee
3823628c78e7STejun Heo 	 * sched callbacks see the %WORKER_UNBOUND flag.  This is necessary
3824628c78e7STejun Heo 	 * as scheduler callbacks may be invoked from other cpus.
3825628c78e7STejun Heo 	 */
3826628c78e7STejun Heo 	schedule();
3827628c78e7STejun Heo 
3828628c78e7STejun Heo 	/*
3829628c78e7STejun Heo 	 * Sched callbacks are disabled now.  Zap nr_running.  After this,
3830628c78e7STejun Heo 	 * nr_running stays zero and need_more_worker() and keep_working()
383138db41d9STejun Heo 	 * are always true as long as the worklist is not empty.  Pools on
383238db41d9STejun Heo 	 * @cpu now behave as unbound (in terms of concurrency management)
383338db41d9STejun Heo 	 * pools which are served by workers tied to the CPU.
3834628c78e7STejun Heo 	 *
3835628c78e7STejun Heo 	 * On return from this function, the current worker would trigger
3836628c78e7STejun Heo 	 * unbound chain execution of pending work items if other workers
3837628c78e7STejun Heo 	 * didn't already.
3838e22bee78STejun Heo 	 */
3839f02ae73aSTejun Heo 	for_each_cpu_worker_pool(pool, cpu)
3840e19e397aSTejun Heo 		atomic_set(&pool->nr_running, 0);
3841db7bccf4STejun Heo }
3842db7bccf4STejun Heo 
38438db25e78STejun Heo /*
38448db25e78STejun Heo  * Workqueues should be brought up before normal priority CPU notifiers.
38458db25e78STejun Heo  * This will be registered high priority CPU notifier.
38468db25e78STejun Heo  */
38479fdf9b73SLai Jiangshan static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb,
38481da177e4SLinus Torvalds 					       unsigned long action,
38491da177e4SLinus Torvalds 					       void *hcpu)
38501da177e4SLinus Torvalds {
3851d84ff051STejun Heo 	int cpu = (unsigned long)hcpu;
38524ce62e9eSTejun Heo 	struct worker_pool *pool;
38531da177e4SLinus Torvalds 
38548db25e78STejun Heo 	switch (action & ~CPU_TASKS_FROZEN) {
38553af24433SOleg Nesterov 	case CPU_UP_PREPARE:
3856f02ae73aSTejun Heo 		for_each_cpu_worker_pool(pool, cpu) {
38573ce63377STejun Heo 			struct worker *worker;
38583ce63377STejun Heo 
38593ce63377STejun Heo 			if (pool->nr_workers)
38603ce63377STejun Heo 				continue;
38613ce63377STejun Heo 
38623ce63377STejun Heo 			worker = create_worker(pool);
38633ce63377STejun Heo 			if (!worker)
38643ce63377STejun Heo 				return NOTIFY_BAD;
38653ce63377STejun Heo 
3866d565ed63STejun Heo 			spin_lock_irq(&pool->lock);
38673ce63377STejun Heo 			start_worker(worker);
3868d565ed63STejun Heo 			spin_unlock_irq(&pool->lock);
38693af24433SOleg Nesterov 		}
38701da177e4SLinus Torvalds 		break;
38711da177e4SLinus Torvalds 
387265758202STejun Heo 	case CPU_DOWN_FAILED:
387365758202STejun Heo 	case CPU_ONLINE:
3874f02ae73aSTejun Heo 		for_each_cpu_worker_pool(pool, cpu) {
387594cf58bbSTejun Heo 			mutex_lock(&pool->assoc_mutex);
387694cf58bbSTejun Heo 			spin_lock_irq(&pool->lock);
387794cf58bbSTejun Heo 
387824647570STejun Heo 			pool->flags &= ~POOL_DISASSOCIATED;
387994cf58bbSTejun Heo 			rebind_workers(pool);
388094cf58bbSTejun Heo 
388194cf58bbSTejun Heo 			spin_unlock_irq(&pool->lock);
388294cf58bbSTejun Heo 			mutex_unlock(&pool->assoc_mutex);
388394cf58bbSTejun Heo 		}
38848db25e78STejun Heo 		break;
388565758202STejun Heo 	}
388665758202STejun Heo 	return NOTIFY_OK;
388765758202STejun Heo }
388865758202STejun Heo 
388965758202STejun Heo /*
389065758202STejun Heo  * Workqueues should be brought down after normal priority CPU notifiers.
389165758202STejun Heo  * This will be registered as low priority CPU notifier.
389265758202STejun Heo  */
38939fdf9b73SLai Jiangshan static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb,
389465758202STejun Heo 						 unsigned long action,
389565758202STejun Heo 						 void *hcpu)
389665758202STejun Heo {
3897d84ff051STejun Heo 	int cpu = (unsigned long)hcpu;
38988db25e78STejun Heo 	struct work_struct unbind_work;
38998db25e78STejun Heo 
390065758202STejun Heo 	switch (action & ~CPU_TASKS_FROZEN) {
390165758202STejun Heo 	case CPU_DOWN_PREPARE:
39028db25e78STejun Heo 		/* unbinding should happen on the local CPU */
3903706026c2STejun Heo 		INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
39047635d2fdSJoonsoo Kim 		queue_work_on(cpu, system_highpri_wq, &unbind_work);
39058db25e78STejun Heo 		flush_work(&unbind_work);
39068db25e78STejun Heo 		break;
390765758202STejun Heo 	}
390865758202STejun Heo 	return NOTIFY_OK;
390965758202STejun Heo }
391065758202STejun Heo 
39112d3854a3SRusty Russell #ifdef CONFIG_SMP
39128ccad40dSRusty Russell 
39132d3854a3SRusty Russell struct work_for_cpu {
3914ed48ece2STejun Heo 	struct work_struct work;
39152d3854a3SRusty Russell 	long (*fn)(void *);
39162d3854a3SRusty Russell 	void *arg;
39172d3854a3SRusty Russell 	long ret;
39182d3854a3SRusty Russell };
39192d3854a3SRusty Russell 
3920ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work)
39212d3854a3SRusty Russell {
3922ed48ece2STejun Heo 	struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
3923ed48ece2STejun Heo 
39242d3854a3SRusty Russell 	wfc->ret = wfc->fn(wfc->arg);
39252d3854a3SRusty Russell }
39262d3854a3SRusty Russell 
39272d3854a3SRusty Russell /**
39282d3854a3SRusty Russell  * work_on_cpu - run a function in user context on a particular cpu
39292d3854a3SRusty Russell  * @cpu: the cpu to run on
39302d3854a3SRusty Russell  * @fn: the function to run
39312d3854a3SRusty Russell  * @arg: the function arg
39322d3854a3SRusty Russell  *
393331ad9081SRusty Russell  * This will return the value @fn returns.
393431ad9081SRusty Russell  * It is up to the caller to ensure that the cpu doesn't go offline.
39356b44003eSAndrew Morton  * The caller must not hold any locks which would prevent @fn from completing.
39362d3854a3SRusty Russell  */
3937d84ff051STejun Heo long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
39382d3854a3SRusty Russell {
3939ed48ece2STejun Heo 	struct work_for_cpu wfc = { .fn = fn, .arg = arg };
39402d3854a3SRusty Russell 
3941ed48ece2STejun Heo 	INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
3942ed48ece2STejun Heo 	schedule_work_on(cpu, &wfc.work);
3943ed48ece2STejun Heo 	flush_work(&wfc.work);
39442d3854a3SRusty Russell 	return wfc.ret;
39452d3854a3SRusty Russell }
39462d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu);
39472d3854a3SRusty Russell #endif /* CONFIG_SMP */
39482d3854a3SRusty Russell 
3949a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER
3950e7577c50SRusty Russell 
3951a0a1a5fdSTejun Heo /**
3952a0a1a5fdSTejun Heo  * freeze_workqueues_begin - begin freezing workqueues
3953a0a1a5fdSTejun Heo  *
395458a69cb4STejun Heo  * Start freezing workqueues.  After this function returns, all freezable
395558a69cb4STejun Heo  * workqueues will queue new works to their frozen_works list instead of
3956706026c2STejun Heo  * pool->worklist.
3957a0a1a5fdSTejun Heo  *
3958a0a1a5fdSTejun Heo  * CONTEXT:
3959d565ed63STejun Heo  * Grabs and releases workqueue_lock and pool->lock's.
3960a0a1a5fdSTejun Heo  */
3961a0a1a5fdSTejun Heo void freeze_workqueues_begin(void)
3962a0a1a5fdSTejun Heo {
396317116969STejun Heo 	struct worker_pool *pool;
396424b8a847STejun Heo 	struct workqueue_struct *wq;
396524b8a847STejun Heo 	struct pool_workqueue *pwq;
396617116969STejun Heo 	int id;
3967a0a1a5fdSTejun Heo 
3968e98d5b16STejun Heo 	spin_lock_irq(&workqueue_lock);
3969a0a1a5fdSTejun Heo 
39706183c009STejun Heo 	WARN_ON_ONCE(workqueue_freezing);
3971a0a1a5fdSTejun Heo 	workqueue_freezing = true;
3972a0a1a5fdSTejun Heo 
397324b8a847STejun Heo 	/* set FREEZING */
397417116969STejun Heo 	for_each_pool(pool, id) {
3975e98d5b16STejun Heo 		spin_lock(&pool->lock);
397635b6bb63STejun Heo 		WARN_ON_ONCE(pool->flags & POOL_FREEZING);
397735b6bb63STejun Heo 		pool->flags |= POOL_FREEZING;
397824b8a847STejun Heo 		spin_unlock(&pool->lock);
39791da177e4SLinus Torvalds 	}
39808b03ae3cSTejun Heo 
398124b8a847STejun Heo 	/* suppress further executions by setting max_active to zero */
398224b8a847STejun Heo 	list_for_each_entry(wq, &workqueues, list) {
398324b8a847STejun Heo 		if (!(wq->flags & WQ_FREEZABLE))
398424b8a847STejun Heo 			continue;
398524b8a847STejun Heo 
398624b8a847STejun Heo 		for_each_pwq(pwq, wq) {
398724b8a847STejun Heo 			spin_lock(&pwq->pool->lock);
398824b8a847STejun Heo 			pwq->max_active = 0;
398924b8a847STejun Heo 			spin_unlock(&pwq->pool->lock);
399024b8a847STejun Heo 		}
3991a1056305STejun Heo 	}
3992a0a1a5fdSTejun Heo 
3993e98d5b16STejun Heo 	spin_unlock_irq(&workqueue_lock);
3994a0a1a5fdSTejun Heo }
3995a0a1a5fdSTejun Heo 
3996a0a1a5fdSTejun Heo /**
399758a69cb4STejun Heo  * freeze_workqueues_busy - are freezable workqueues still busy?
3998a0a1a5fdSTejun Heo  *
3999a0a1a5fdSTejun Heo  * Check whether freezing is complete.  This function must be called
4000a0a1a5fdSTejun Heo  * between freeze_workqueues_begin() and thaw_workqueues().
4001a0a1a5fdSTejun Heo  *
4002a0a1a5fdSTejun Heo  * CONTEXT:
4003a0a1a5fdSTejun Heo  * Grabs and releases workqueue_lock.
4004a0a1a5fdSTejun Heo  *
4005a0a1a5fdSTejun Heo  * RETURNS:
400658a69cb4STejun Heo  * %true if some freezable workqueues are still busy.  %false if freezing
400758a69cb4STejun Heo  * is complete.
4008a0a1a5fdSTejun Heo  */
4009a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void)
4010a0a1a5fdSTejun Heo {
4011a0a1a5fdSTejun Heo 	bool busy = false;
401224b8a847STejun Heo 	struct workqueue_struct *wq;
401324b8a847STejun Heo 	struct pool_workqueue *pwq;
4014a0a1a5fdSTejun Heo 
4015e98d5b16STejun Heo 	spin_lock_irq(&workqueue_lock);
4016a0a1a5fdSTejun Heo 
40176183c009STejun Heo 	WARN_ON_ONCE(!workqueue_freezing);
4018a0a1a5fdSTejun Heo 
401924b8a847STejun Heo 	list_for_each_entry(wq, &workqueues, list) {
402024b8a847STejun Heo 		if (!(wq->flags & WQ_FREEZABLE))
402124b8a847STejun Heo 			continue;
4022a0a1a5fdSTejun Heo 		/*
4023a0a1a5fdSTejun Heo 		 * nr_active is monotonically decreasing.  It's safe
4024a0a1a5fdSTejun Heo 		 * to peek without lock.
4025a0a1a5fdSTejun Heo 		 */
402624b8a847STejun Heo 		for_each_pwq(pwq, wq) {
40276183c009STejun Heo 			WARN_ON_ONCE(pwq->nr_active < 0);
4028112202d9STejun Heo 			if (pwq->nr_active) {
4029a0a1a5fdSTejun Heo 				busy = true;
4030a0a1a5fdSTejun Heo 				goto out_unlock;
4031a0a1a5fdSTejun Heo 			}
4032a0a1a5fdSTejun Heo 		}
4033a0a1a5fdSTejun Heo 	}
4034a0a1a5fdSTejun Heo out_unlock:
4035e98d5b16STejun Heo 	spin_unlock_irq(&workqueue_lock);
4036a0a1a5fdSTejun Heo 	return busy;
4037a0a1a5fdSTejun Heo }
4038a0a1a5fdSTejun Heo 
4039a0a1a5fdSTejun Heo /**
4040a0a1a5fdSTejun Heo  * thaw_workqueues - thaw workqueues
4041a0a1a5fdSTejun Heo  *
4042a0a1a5fdSTejun Heo  * Thaw workqueues.  Normal queueing is restored and all collected
4043706026c2STejun Heo  * frozen works are transferred to their respective pool worklists.
4044a0a1a5fdSTejun Heo  *
4045a0a1a5fdSTejun Heo  * CONTEXT:
4046d565ed63STejun Heo  * Grabs and releases workqueue_lock and pool->lock's.
4047a0a1a5fdSTejun Heo  */
4048a0a1a5fdSTejun Heo void thaw_workqueues(void)
4049a0a1a5fdSTejun Heo {
405024b8a847STejun Heo 	struct workqueue_struct *wq;
405124b8a847STejun Heo 	struct pool_workqueue *pwq;
405224b8a847STejun Heo 	struct worker_pool *pool;
405324b8a847STejun Heo 	int id;
4054a0a1a5fdSTejun Heo 
4055e98d5b16STejun Heo 	spin_lock_irq(&workqueue_lock);
4056a0a1a5fdSTejun Heo 
4057a0a1a5fdSTejun Heo 	if (!workqueue_freezing)
4058a0a1a5fdSTejun Heo 		goto out_unlock;
4059a0a1a5fdSTejun Heo 
406024b8a847STejun Heo 	/* clear FREEZING */
406124b8a847STejun Heo 	for_each_pool(pool, id) {
4062e98d5b16STejun Heo 		spin_lock(&pool->lock);
406335b6bb63STejun Heo 		WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
406435b6bb63STejun Heo 		pool->flags &= ~POOL_FREEZING;
4065e98d5b16STejun Heo 		spin_unlock(&pool->lock);
4066d565ed63STejun Heo 	}
406724b8a847STejun Heo 
406824b8a847STejun Heo 	/* restore max_active and repopulate worklist */
406924b8a847STejun Heo 	list_for_each_entry(wq, &workqueues, list) {
407024b8a847STejun Heo 		if (!(wq->flags & WQ_FREEZABLE))
407124b8a847STejun Heo 			continue;
407224b8a847STejun Heo 
407324b8a847STejun Heo 		for_each_pwq(pwq, wq) {
407424b8a847STejun Heo 			spin_lock(&pwq->pool->lock);
407524b8a847STejun Heo 			pwq_set_max_active(pwq, wq->saved_max_active);
407624b8a847STejun Heo 			spin_unlock(&pwq->pool->lock);
407724b8a847STejun Heo 		}
407824b8a847STejun Heo 	}
407924b8a847STejun Heo 
408024b8a847STejun Heo 	/* kick workers */
408124b8a847STejun Heo 	for_each_pool(pool, id) {
408224b8a847STejun Heo 		spin_lock(&pool->lock);
408324b8a847STejun Heo 		wake_up_worker(pool);
408424b8a847STejun Heo 		spin_unlock(&pool->lock);
4085a0a1a5fdSTejun Heo 	}
4086a0a1a5fdSTejun Heo 
4087a0a1a5fdSTejun Heo 	workqueue_freezing = false;
4088a0a1a5fdSTejun Heo out_unlock:
4089e98d5b16STejun Heo 	spin_unlock_irq(&workqueue_lock);
4090a0a1a5fdSTejun Heo }
4091a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */
4092a0a1a5fdSTejun Heo 
40936ee0578bSSuresh Siddha static int __init init_workqueues(void)
40941da177e4SLinus Torvalds {
40957a4e344cSTejun Heo 	int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL };
40967a4e344cSTejun Heo 	int i, cpu;
4097c34056a3STejun Heo 
40987c3eed5cSTejun Heo 	/* make sure we have enough bits for OFFQ pool ID */
40997c3eed5cSTejun Heo 	BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) <
41006be19588SLai Jiangshan 		     WORK_CPU_END * NR_STD_WORKER_POOLS);
4101b5490077STejun Heo 
4102e904e6c2STejun Heo 	WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
4103e904e6c2STejun Heo 
4104e904e6c2STejun Heo 	pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
4105e904e6c2STejun Heo 
410665758202STejun Heo 	cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
4107a5b4e57dSLai Jiangshan 	hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
41088b03ae3cSTejun Heo 
4109706026c2STejun Heo 	/* initialize CPU pools */
411029c91e99STejun Heo 	for_each_possible_cpu(cpu) {
41114ce62e9eSTejun Heo 		struct worker_pool *pool;
41128b03ae3cSTejun Heo 
41137a4e344cSTejun Heo 		i = 0;
4114f02ae73aSTejun Heo 		for_each_cpu_worker_pool(pool, cpu) {
41157a4e344cSTejun Heo 			BUG_ON(init_worker_pool(pool));
4116ec22ca5eSTejun Heo 			pool->cpu = cpu;
41177a4e344cSTejun Heo 			cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu));
41187a4e344cSTejun Heo 			pool->attrs->nice = std_nice[i++];
41197a4e344cSTejun Heo 
41209daf9e67STejun Heo 			/* alloc pool ID */
41219daf9e67STejun Heo 			BUG_ON(worker_pool_assign_id(pool));
41224ce62e9eSTejun Heo 		}
41238b03ae3cSTejun Heo 	}
41248b03ae3cSTejun Heo 
4125e22bee78STejun Heo 	/* create the initial worker */
412629c91e99STejun Heo 	for_each_online_cpu(cpu) {
41274ce62e9eSTejun Heo 		struct worker_pool *pool;
4128e22bee78STejun Heo 
4129f02ae73aSTejun Heo 		for_each_cpu_worker_pool(pool, cpu) {
41304ce62e9eSTejun Heo 			struct worker *worker;
41314ce62e9eSTejun Heo 
413224647570STejun Heo 			pool->flags &= ~POOL_DISASSOCIATED;
413324647570STejun Heo 
4134bc2ae0f5STejun Heo 			worker = create_worker(pool);
4135e22bee78STejun Heo 			BUG_ON(!worker);
4136d565ed63STejun Heo 			spin_lock_irq(&pool->lock);
4137e22bee78STejun Heo 			start_worker(worker);
4138d565ed63STejun Heo 			spin_unlock_irq(&pool->lock);
4139e22bee78STejun Heo 		}
41404ce62e9eSTejun Heo 	}
4141e22bee78STejun Heo 
414229c91e99STejun Heo 	/* create default unbound wq attrs */
414329c91e99STejun Heo 	for (i = 0; i < NR_STD_WORKER_POOLS; i++) {
414429c91e99STejun Heo 		struct workqueue_attrs *attrs;
414529c91e99STejun Heo 
414629c91e99STejun Heo 		BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL)));
414729c91e99STejun Heo 
414829c91e99STejun Heo 		attrs->nice = std_nice[i];
414929c91e99STejun Heo 		cpumask_setall(attrs->cpumask);
415029c91e99STejun Heo 
415129c91e99STejun Heo 		unbound_std_wq_attrs[i] = attrs;
415229c91e99STejun Heo 	}
415329c91e99STejun Heo 
4154d320c038STejun Heo 	system_wq = alloc_workqueue("events", 0, 0);
41551aabe902SJoonsoo Kim 	system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
4156d320c038STejun Heo 	system_long_wq = alloc_workqueue("events_long", 0, 0);
4157f3421797STejun Heo 	system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
4158f3421797STejun Heo 					    WQ_UNBOUND_MAX_ACTIVE);
415924d51addSTejun Heo 	system_freezable_wq = alloc_workqueue("events_freezable",
416024d51addSTejun Heo 					      WQ_FREEZABLE, 0);
41611aabe902SJoonsoo Kim 	BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
4162ae930e0fSTejun Heo 	       !system_unbound_wq || !system_freezable_wq);
41636ee0578bSSuresh Siddha 	return 0;
41641da177e4SLinus Torvalds }
41656ee0578bSSuresh Siddha early_initcall(init_workqueues);
4166