xref: /linux-6.15/kernel/workqueue.c (revision 7e11629d)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * linux/kernel/workqueue.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Generic mechanism for defining kernel helper threads for running
51da177e4SLinus Torvalds  * arbitrary tasks in process context.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Started by Ingo Molnar, Copyright (C) 2002
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Derived from the taskqueue/keventd code by:
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *   David Woodhouse <[email protected]>
12e1f8e874SFrancois Cami  *   Andrew Morton
131da177e4SLinus Torvalds  *   Kai Petzke <[email protected]>
141da177e4SLinus Torvalds  *   Theodore Ts'o <[email protected]>
1589ada679SChristoph Lameter  *
16cde53535SChristoph Lameter  * Made to use alloc_percpu by Christoph Lameter.
171da177e4SLinus Torvalds  */
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #include <linux/module.h>
201da177e4SLinus Torvalds #include <linux/kernel.h>
211da177e4SLinus Torvalds #include <linux/sched.h>
221da177e4SLinus Torvalds #include <linux/init.h>
231da177e4SLinus Torvalds #include <linux/signal.h>
241da177e4SLinus Torvalds #include <linux/completion.h>
251da177e4SLinus Torvalds #include <linux/workqueue.h>
261da177e4SLinus Torvalds #include <linux/slab.h>
271da177e4SLinus Torvalds #include <linux/cpu.h>
281da177e4SLinus Torvalds #include <linux/notifier.h>
291da177e4SLinus Torvalds #include <linux/kthread.h>
301fa44ecaSJames Bottomley #include <linux/hardirq.h>
3146934023SChristoph Lameter #include <linux/mempolicy.h>
32341a5958SRafael J. Wysocki #include <linux/freezer.h>
33d5abe669SPeter Zijlstra #include <linux/kallsyms.h>
34d5abe669SPeter Zijlstra #include <linux/debug_locks.h>
354e6045f1SJohannes Berg #include <linux/lockdep.h>
36c34056a3STejun Heo #include <linux/idr.h>
37*7e11629dSTejun Heo #include <linux/delay.h>
381da177e4SLinus Torvalds 
39c8e55f36STejun Heo enum {
40db7bccf4STejun Heo 	/* global_cwq flags */
41db7bccf4STejun Heo 	GCWQ_FREEZING		= 1 << 3,	/* freeze in progress */
42db7bccf4STejun Heo 
43c8e55f36STejun Heo 	/* worker flags */
44c8e55f36STejun Heo 	WORKER_STARTED		= 1 << 0,	/* started */
45c8e55f36STejun Heo 	WORKER_DIE		= 1 << 1,	/* die die die */
46c8e55f36STejun Heo 	WORKER_IDLE		= 1 << 2,	/* is idle */
47db7bccf4STejun Heo 	WORKER_ROGUE		= 1 << 4,	/* not bound to any cpu */
48db7bccf4STejun Heo 
49db7bccf4STejun Heo 	/* gcwq->trustee_state */
50db7bccf4STejun Heo 	TRUSTEE_START		= 0,		/* start */
51db7bccf4STejun Heo 	TRUSTEE_IN_CHARGE	= 1,		/* trustee in charge of gcwq */
52db7bccf4STejun Heo 	TRUSTEE_BUTCHER		= 2,		/* butcher workers */
53db7bccf4STejun Heo 	TRUSTEE_RELEASE		= 3,		/* release workers */
54db7bccf4STejun Heo 	TRUSTEE_DONE		= 4,		/* trustee is done */
55c8e55f36STejun Heo 
56c8e55f36STejun Heo 	BUSY_WORKER_HASH_ORDER	= 6,		/* 64 pointers */
57c8e55f36STejun Heo 	BUSY_WORKER_HASH_SIZE	= 1 << BUSY_WORKER_HASH_ORDER,
58c8e55f36STejun Heo 	BUSY_WORKER_HASH_MASK	= BUSY_WORKER_HASH_SIZE - 1,
59db7bccf4STejun Heo 
60db7bccf4STejun Heo 	TRUSTEE_COOLDOWN	= HZ / 10,	/* for trustee draining */
61c8e55f36STejun Heo };
62c8e55f36STejun Heo 
631da177e4SLinus Torvalds /*
644690c4abSTejun Heo  * Structure fields follow one of the following exclusion rules.
654690c4abSTejun Heo  *
664690c4abSTejun Heo  * I: Set during initialization and read-only afterwards.
674690c4abSTejun Heo  *
688b03ae3cSTejun Heo  * L: gcwq->lock protected.  Access with gcwq->lock held.
694690c4abSTejun Heo  *
7073f53c4aSTejun Heo  * F: wq->flush_mutex protected.
7173f53c4aSTejun Heo  *
724690c4abSTejun Heo  * W: workqueue_lock protected.
734690c4abSTejun Heo  */
744690c4abSTejun Heo 
758b03ae3cSTejun Heo struct global_cwq;
76c34056a3STejun Heo 
77c34056a3STejun Heo struct worker {
78c8e55f36STejun Heo 	/* on idle list while idle, on busy hash table while busy */
79c8e55f36STejun Heo 	union {
80c8e55f36STejun Heo 		struct list_head	entry;	/* L: while idle */
81c8e55f36STejun Heo 		struct hlist_node	hentry;	/* L: while busy */
82c8e55f36STejun Heo 	};
83c8e55f36STejun Heo 
84c34056a3STejun Heo 	struct work_struct	*current_work;	/* L: work being processed */
858cca0eeaSTejun Heo 	struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */
86affee4b2STejun Heo 	struct list_head	scheduled;	/* L: scheduled works */
87c34056a3STejun Heo 	struct task_struct	*task;		/* I: worker task */
888b03ae3cSTejun Heo 	struct global_cwq	*gcwq;		/* I: the associated gcwq */
89c8e55f36STejun Heo 	unsigned int		flags;		/* L: flags */
90c34056a3STejun Heo 	int			id;		/* I: worker id */
91c34056a3STejun Heo };
92c34056a3STejun Heo 
934690c4abSTejun Heo /*
948b03ae3cSTejun Heo  * Global per-cpu workqueue.
958b03ae3cSTejun Heo  */
968b03ae3cSTejun Heo struct global_cwq {
978b03ae3cSTejun Heo 	spinlock_t		lock;		/* the gcwq lock */
98*7e11629dSTejun Heo 	struct list_head	worklist;	/* L: list of pending works */
998b03ae3cSTejun Heo 	unsigned int		cpu;		/* I: the associated cpu */
100db7bccf4STejun Heo 	unsigned int		flags;		/* L: GCWQ_* flags */
101c8e55f36STejun Heo 
102c8e55f36STejun Heo 	int			nr_workers;	/* L: total number of workers */
103c8e55f36STejun Heo 	int			nr_idle;	/* L: currently idle ones */
104c8e55f36STejun Heo 
105c8e55f36STejun Heo 	/* workers are chained either in the idle_list or busy_hash */
106c8e55f36STejun Heo 	struct list_head	idle_list;	/* L: list of idle workers */
107c8e55f36STejun Heo 	struct hlist_head	busy_hash[BUSY_WORKER_HASH_SIZE];
108c8e55f36STejun Heo 						/* L: hash of busy workers */
109c8e55f36STejun Heo 
1108b03ae3cSTejun Heo 	struct ida		worker_ida;	/* L: for worker IDs */
111db7bccf4STejun Heo 
112db7bccf4STejun Heo 	struct task_struct	*trustee;	/* L: for gcwq shutdown */
113db7bccf4STejun Heo 	unsigned int		trustee_state;	/* L: trustee state */
114db7bccf4STejun Heo 	wait_queue_head_t	trustee_wait;	/* trustee wait */
1158b03ae3cSTejun Heo } ____cacheline_aligned_in_smp;
1168b03ae3cSTejun Heo 
1178b03ae3cSTejun Heo /*
118502ca9d8STejun Heo  * The per-CPU workqueue.  The lower WORK_STRUCT_FLAG_BITS of
1190f900049STejun Heo  * work_struct->data are used for flags and thus cwqs need to be
1200f900049STejun Heo  * aligned at two's power of the number of flag bits.
1211da177e4SLinus Torvalds  */
1221da177e4SLinus Torvalds struct cpu_workqueue_struct {
1238b03ae3cSTejun Heo 	struct global_cwq	*gcwq;		/* I: the associated gcwq */
124c34056a3STejun Heo 	struct worker		*worker;
1254690c4abSTejun Heo 	struct workqueue_struct *wq;		/* I: the owning workqueue */
12673f53c4aSTejun Heo 	int			work_color;	/* L: current color */
12773f53c4aSTejun Heo 	int			flush_color;	/* L: flushing color */
12873f53c4aSTejun Heo 	int			nr_in_flight[WORK_NR_COLORS];
12973f53c4aSTejun Heo 						/* L: nr of in_flight works */
1301e19ffc6STejun Heo 	int			nr_active;	/* L: nr of active works */
131a0a1a5fdSTejun Heo 	int			max_active;	/* L: max active works */
1321e19ffc6STejun Heo 	struct list_head	delayed_works;	/* L: delayed works */
1330f900049STejun Heo };
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds /*
13673f53c4aSTejun Heo  * Structure used to wait for workqueue flush.
13773f53c4aSTejun Heo  */
13873f53c4aSTejun Heo struct wq_flusher {
13973f53c4aSTejun Heo 	struct list_head	list;		/* F: list of flushers */
14073f53c4aSTejun Heo 	int			flush_color;	/* F: flush color waiting for */
14173f53c4aSTejun Heo 	struct completion	done;		/* flush completion */
14273f53c4aSTejun Heo };
14373f53c4aSTejun Heo 
14473f53c4aSTejun Heo /*
1451da177e4SLinus Torvalds  * The externally visible workqueue abstraction is an array of
1461da177e4SLinus Torvalds  * per-CPU workqueues:
1471da177e4SLinus Torvalds  */
1481da177e4SLinus Torvalds struct workqueue_struct {
14997e37d7bSTejun Heo 	unsigned int		flags;		/* I: WQ_* flags */
1504690c4abSTejun Heo 	struct cpu_workqueue_struct *cpu_wq;	/* I: cwq's */
1514690c4abSTejun Heo 	struct list_head	list;		/* W: list of all workqueues */
15273f53c4aSTejun Heo 
15373f53c4aSTejun Heo 	struct mutex		flush_mutex;	/* protects wq flushing */
15473f53c4aSTejun Heo 	int			work_color;	/* F: current work color */
15573f53c4aSTejun Heo 	int			flush_color;	/* F: current flush color */
15673f53c4aSTejun Heo 	atomic_t		nr_cwqs_to_flush; /* flush in progress */
15773f53c4aSTejun Heo 	struct wq_flusher	*first_flusher;	/* F: first flusher */
15873f53c4aSTejun Heo 	struct list_head	flusher_queue;	/* F: flush waiters */
15973f53c4aSTejun Heo 	struct list_head	flusher_overflow; /* F: flush overflow list */
16073f53c4aSTejun Heo 
161502ca9d8STejun Heo 	unsigned long		single_cpu;	/* cpu for single cpu wq */
162502ca9d8STejun Heo 
163a0a1a5fdSTejun Heo 	int			saved_max_active; /* I: saved cwq max_active */
1644690c4abSTejun Heo 	const char		*name;		/* I: workqueue name */
1654e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
1664e6045f1SJohannes Berg 	struct lockdep_map	lockdep_map;
1674e6045f1SJohannes Berg #endif
1681da177e4SLinus Torvalds };
1691da177e4SLinus Torvalds 
170db7bccf4STejun Heo #define for_each_busy_worker(worker, i, pos, gcwq)			\
171db7bccf4STejun Heo 	for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)			\
172db7bccf4STejun Heo 		hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
173db7bccf4STejun Heo 
174dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK
175dc186ad7SThomas Gleixner 
176dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr;
177dc186ad7SThomas Gleixner 
178dc186ad7SThomas Gleixner /*
179dc186ad7SThomas Gleixner  * fixup_init is called when:
180dc186ad7SThomas Gleixner  * - an active object is initialized
181dc186ad7SThomas Gleixner  */
182dc186ad7SThomas Gleixner static int work_fixup_init(void *addr, enum debug_obj_state state)
183dc186ad7SThomas Gleixner {
184dc186ad7SThomas Gleixner 	struct work_struct *work = addr;
185dc186ad7SThomas Gleixner 
186dc186ad7SThomas Gleixner 	switch (state) {
187dc186ad7SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
188dc186ad7SThomas Gleixner 		cancel_work_sync(work);
189dc186ad7SThomas Gleixner 		debug_object_init(work, &work_debug_descr);
190dc186ad7SThomas Gleixner 		return 1;
191dc186ad7SThomas Gleixner 	default:
192dc186ad7SThomas Gleixner 		return 0;
193dc186ad7SThomas Gleixner 	}
194dc186ad7SThomas Gleixner }
195dc186ad7SThomas Gleixner 
196dc186ad7SThomas Gleixner /*
197dc186ad7SThomas Gleixner  * fixup_activate is called when:
198dc186ad7SThomas Gleixner  * - an active object is activated
199dc186ad7SThomas Gleixner  * - an unknown object is activated (might be a statically initialized object)
200dc186ad7SThomas Gleixner  */
201dc186ad7SThomas Gleixner static int work_fixup_activate(void *addr, enum debug_obj_state state)
202dc186ad7SThomas Gleixner {
203dc186ad7SThomas Gleixner 	struct work_struct *work = addr;
204dc186ad7SThomas Gleixner 
205dc186ad7SThomas Gleixner 	switch (state) {
206dc186ad7SThomas Gleixner 
207dc186ad7SThomas Gleixner 	case ODEBUG_STATE_NOTAVAILABLE:
208dc186ad7SThomas Gleixner 		/*
209dc186ad7SThomas Gleixner 		 * This is not really a fixup. The work struct was
210dc186ad7SThomas Gleixner 		 * statically initialized. We just make sure that it
211dc186ad7SThomas Gleixner 		 * is tracked in the object tracker.
212dc186ad7SThomas Gleixner 		 */
21322df02bbSTejun Heo 		if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
214dc186ad7SThomas Gleixner 			debug_object_init(work, &work_debug_descr);
215dc186ad7SThomas Gleixner 			debug_object_activate(work, &work_debug_descr);
216dc186ad7SThomas Gleixner 			return 0;
217dc186ad7SThomas Gleixner 		}
218dc186ad7SThomas Gleixner 		WARN_ON_ONCE(1);
219dc186ad7SThomas Gleixner 		return 0;
220dc186ad7SThomas Gleixner 
221dc186ad7SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
222dc186ad7SThomas Gleixner 		WARN_ON(1);
223dc186ad7SThomas Gleixner 
224dc186ad7SThomas Gleixner 	default:
225dc186ad7SThomas Gleixner 		return 0;
226dc186ad7SThomas Gleixner 	}
227dc186ad7SThomas Gleixner }
228dc186ad7SThomas Gleixner 
229dc186ad7SThomas Gleixner /*
230dc186ad7SThomas Gleixner  * fixup_free is called when:
231dc186ad7SThomas Gleixner  * - an active object is freed
232dc186ad7SThomas Gleixner  */
233dc186ad7SThomas Gleixner static int work_fixup_free(void *addr, enum debug_obj_state state)
234dc186ad7SThomas Gleixner {
235dc186ad7SThomas Gleixner 	struct work_struct *work = addr;
236dc186ad7SThomas Gleixner 
237dc186ad7SThomas Gleixner 	switch (state) {
238dc186ad7SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
239dc186ad7SThomas Gleixner 		cancel_work_sync(work);
240dc186ad7SThomas Gleixner 		debug_object_free(work, &work_debug_descr);
241dc186ad7SThomas Gleixner 		return 1;
242dc186ad7SThomas Gleixner 	default:
243dc186ad7SThomas Gleixner 		return 0;
244dc186ad7SThomas Gleixner 	}
245dc186ad7SThomas Gleixner }
246dc186ad7SThomas Gleixner 
247dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr = {
248dc186ad7SThomas Gleixner 	.name		= "work_struct",
249dc186ad7SThomas Gleixner 	.fixup_init	= work_fixup_init,
250dc186ad7SThomas Gleixner 	.fixup_activate	= work_fixup_activate,
251dc186ad7SThomas Gleixner 	.fixup_free	= work_fixup_free,
252dc186ad7SThomas Gleixner };
253dc186ad7SThomas Gleixner 
254dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work)
255dc186ad7SThomas Gleixner {
256dc186ad7SThomas Gleixner 	debug_object_activate(work, &work_debug_descr);
257dc186ad7SThomas Gleixner }
258dc186ad7SThomas Gleixner 
259dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work)
260dc186ad7SThomas Gleixner {
261dc186ad7SThomas Gleixner 	debug_object_deactivate(work, &work_debug_descr);
262dc186ad7SThomas Gleixner }
263dc186ad7SThomas Gleixner 
264dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack)
265dc186ad7SThomas Gleixner {
266dc186ad7SThomas Gleixner 	if (onstack)
267dc186ad7SThomas Gleixner 		debug_object_init_on_stack(work, &work_debug_descr);
268dc186ad7SThomas Gleixner 	else
269dc186ad7SThomas Gleixner 		debug_object_init(work, &work_debug_descr);
270dc186ad7SThomas Gleixner }
271dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work);
272dc186ad7SThomas Gleixner 
273dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work)
274dc186ad7SThomas Gleixner {
275dc186ad7SThomas Gleixner 	debug_object_free(work, &work_debug_descr);
276dc186ad7SThomas Gleixner }
277dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack);
278dc186ad7SThomas Gleixner 
279dc186ad7SThomas Gleixner #else
280dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { }
281dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { }
282dc186ad7SThomas Gleixner #endif
283dc186ad7SThomas Gleixner 
28495402b38SGautham R Shenoy /* Serializes the accesses to the list of workqueues. */
28595402b38SGautham R Shenoy static DEFINE_SPINLOCK(workqueue_lock);
2861da177e4SLinus Torvalds static LIST_HEAD(workqueues);
287a0a1a5fdSTejun Heo static bool workqueue_freezing;		/* W: have wqs started freezing? */
288c34056a3STejun Heo 
2898b03ae3cSTejun Heo static DEFINE_PER_CPU(struct global_cwq, global_cwq);
2908b03ae3cSTejun Heo 
291c34056a3STejun Heo static int worker_thread(void *__worker);
2921da177e4SLinus Torvalds 
2938b03ae3cSTejun Heo static struct global_cwq *get_gcwq(unsigned int cpu)
2948b03ae3cSTejun Heo {
2958b03ae3cSTejun Heo 	return &per_cpu(global_cwq, cpu);
2968b03ae3cSTejun Heo }
2978b03ae3cSTejun Heo 
2984690c4abSTejun Heo static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
2994690c4abSTejun Heo 					    struct workqueue_struct *wq)
300a848e3b6SOleg Nesterov {
301a848e3b6SOleg Nesterov 	return per_cpu_ptr(wq->cpu_wq, cpu);
302a848e3b6SOleg Nesterov }
303a848e3b6SOleg Nesterov 
30473f53c4aSTejun Heo static unsigned int work_color_to_flags(int color)
30573f53c4aSTejun Heo {
30673f53c4aSTejun Heo 	return color << WORK_STRUCT_COLOR_SHIFT;
30773f53c4aSTejun Heo }
30873f53c4aSTejun Heo 
30973f53c4aSTejun Heo static int get_work_color(struct work_struct *work)
31073f53c4aSTejun Heo {
31173f53c4aSTejun Heo 	return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
31273f53c4aSTejun Heo 		((1 << WORK_STRUCT_COLOR_BITS) - 1);
31373f53c4aSTejun Heo }
31473f53c4aSTejun Heo 
31573f53c4aSTejun Heo static int work_next_color(int color)
31673f53c4aSTejun Heo {
31773f53c4aSTejun Heo 	return (color + 1) % WORK_NR_COLORS;
31873f53c4aSTejun Heo }
31973f53c4aSTejun Heo 
3204594bf15SDavid Howells /*
3217a22ad75STejun Heo  * Work data points to the cwq while a work is on queue.  Once
3227a22ad75STejun Heo  * execution starts, it points to the cpu the work was last on.  This
3237a22ad75STejun Heo  * can be distinguished by comparing the data value against
3247a22ad75STejun Heo  * PAGE_OFFSET.
3257a22ad75STejun Heo  *
3267a22ad75STejun Heo  * set_work_{cwq|cpu}() and clear_work_data() can be used to set the
3277a22ad75STejun Heo  * cwq, cpu or clear work->data.  These functions should only be
3287a22ad75STejun Heo  * called while the work is owned - ie. while the PENDING bit is set.
3297a22ad75STejun Heo  *
3307a22ad75STejun Heo  * get_work_[g]cwq() can be used to obtain the gcwq or cwq
3317a22ad75STejun Heo  * corresponding to a work.  gcwq is available once the work has been
3327a22ad75STejun Heo  * queued anywhere after initialization.  cwq is available only from
3337a22ad75STejun Heo  * queueing until execution starts.
3344594bf15SDavid Howells  */
3357a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data,
3367a22ad75STejun Heo 				 unsigned long flags)
3377a22ad75STejun Heo {
3387a22ad75STejun Heo 	BUG_ON(!work_pending(work));
3397a22ad75STejun Heo 	atomic_long_set(&work->data, data | flags | work_static(work));
3407a22ad75STejun Heo }
3417a22ad75STejun Heo 
3427a22ad75STejun Heo static void set_work_cwq(struct work_struct *work,
3434690c4abSTejun Heo 			 struct cpu_workqueue_struct *cwq,
3444690c4abSTejun Heo 			 unsigned long extra_flags)
345365970a1SDavid Howells {
3467a22ad75STejun Heo 	set_work_data(work, (unsigned long)cwq,
34722df02bbSTejun Heo 		      WORK_STRUCT_PENDING | extra_flags);
348365970a1SDavid Howells }
349365970a1SDavid Howells 
3507a22ad75STejun Heo static void set_work_cpu(struct work_struct *work, unsigned int cpu)
3514d707b9fSOleg Nesterov {
3527a22ad75STejun Heo 	set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING);
3534d707b9fSOleg Nesterov }
3544d707b9fSOleg Nesterov 
3557a22ad75STejun Heo static void clear_work_data(struct work_struct *work)
356365970a1SDavid Howells {
3577a22ad75STejun Heo 	set_work_data(work, WORK_STRUCT_NO_CPU, 0);
3587a22ad75STejun Heo }
3597a22ad75STejun Heo 
3607a22ad75STejun Heo static inline unsigned long get_work_data(struct work_struct *work)
3617a22ad75STejun Heo {
3627a22ad75STejun Heo 	return atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK;
3637a22ad75STejun Heo }
3647a22ad75STejun Heo 
3657a22ad75STejun Heo static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
3667a22ad75STejun Heo {
3677a22ad75STejun Heo 	unsigned long data = get_work_data(work);
3687a22ad75STejun Heo 
3697a22ad75STejun Heo 	return data >= PAGE_OFFSET ? (void *)data : NULL;
3707a22ad75STejun Heo }
3717a22ad75STejun Heo 
3727a22ad75STejun Heo static struct global_cwq *get_work_gcwq(struct work_struct *work)
3737a22ad75STejun Heo {
3747a22ad75STejun Heo 	unsigned long data = get_work_data(work);
3757a22ad75STejun Heo 	unsigned int cpu;
3767a22ad75STejun Heo 
3777a22ad75STejun Heo 	if (data >= PAGE_OFFSET)
3787a22ad75STejun Heo 		return ((struct cpu_workqueue_struct *)data)->gcwq;
3797a22ad75STejun Heo 
3807a22ad75STejun Heo 	cpu = data >> WORK_STRUCT_FLAG_BITS;
3817a22ad75STejun Heo 	if (cpu == NR_CPUS)
3827a22ad75STejun Heo 		return NULL;
3837a22ad75STejun Heo 
3847a22ad75STejun Heo 	BUG_ON(cpu >= num_possible_cpus());
3857a22ad75STejun Heo 	return get_gcwq(cpu);
386365970a1SDavid Howells }
387365970a1SDavid Howells 
388*7e11629dSTejun Heo /* Return the first worker.  Safe with preemption disabled */
389*7e11629dSTejun Heo static struct worker *first_worker(struct global_cwq *gcwq)
390*7e11629dSTejun Heo {
391*7e11629dSTejun Heo 	if (unlikely(list_empty(&gcwq->idle_list)))
392*7e11629dSTejun Heo 		return NULL;
393*7e11629dSTejun Heo 
394*7e11629dSTejun Heo 	return list_first_entry(&gcwq->idle_list, struct worker, entry);
395*7e11629dSTejun Heo }
396*7e11629dSTejun Heo 
397*7e11629dSTejun Heo /**
398*7e11629dSTejun Heo  * wake_up_worker - wake up an idle worker
399*7e11629dSTejun Heo  * @gcwq: gcwq to wake worker for
400*7e11629dSTejun Heo  *
401*7e11629dSTejun Heo  * Wake up the first idle worker of @gcwq.
402*7e11629dSTejun Heo  *
403*7e11629dSTejun Heo  * CONTEXT:
404*7e11629dSTejun Heo  * spin_lock_irq(gcwq->lock).
405*7e11629dSTejun Heo  */
406*7e11629dSTejun Heo static void wake_up_worker(struct global_cwq *gcwq)
407*7e11629dSTejun Heo {
408*7e11629dSTejun Heo 	struct worker *worker = first_worker(gcwq);
409*7e11629dSTejun Heo 
410*7e11629dSTejun Heo 	if (likely(worker))
411*7e11629dSTejun Heo 		wake_up_process(worker->task);
412*7e11629dSTejun Heo }
413*7e11629dSTejun Heo 
4144690c4abSTejun Heo /**
415c8e55f36STejun Heo  * busy_worker_head - return the busy hash head for a work
416c8e55f36STejun Heo  * @gcwq: gcwq of interest
417c8e55f36STejun Heo  * @work: work to be hashed
418c8e55f36STejun Heo  *
419c8e55f36STejun Heo  * Return hash head of @gcwq for @work.
420c8e55f36STejun Heo  *
421c8e55f36STejun Heo  * CONTEXT:
422c8e55f36STejun Heo  * spin_lock_irq(gcwq->lock).
423c8e55f36STejun Heo  *
424c8e55f36STejun Heo  * RETURNS:
425c8e55f36STejun Heo  * Pointer to the hash head.
426c8e55f36STejun Heo  */
427c8e55f36STejun Heo static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
428c8e55f36STejun Heo 					   struct work_struct *work)
429c8e55f36STejun Heo {
430c8e55f36STejun Heo 	const int base_shift = ilog2(sizeof(struct work_struct));
431c8e55f36STejun Heo 	unsigned long v = (unsigned long)work;
432c8e55f36STejun Heo 
433c8e55f36STejun Heo 	/* simple shift and fold hash, do we need something better? */
434c8e55f36STejun Heo 	v >>= base_shift;
435c8e55f36STejun Heo 	v += v >> BUSY_WORKER_HASH_ORDER;
436c8e55f36STejun Heo 	v &= BUSY_WORKER_HASH_MASK;
437c8e55f36STejun Heo 
438c8e55f36STejun Heo 	return &gcwq->busy_hash[v];
439c8e55f36STejun Heo }
440c8e55f36STejun Heo 
441c8e55f36STejun Heo /**
4428cca0eeaSTejun Heo  * __find_worker_executing_work - find worker which is executing a work
4438cca0eeaSTejun Heo  * @gcwq: gcwq of interest
4448cca0eeaSTejun Heo  * @bwh: hash head as returned by busy_worker_head()
4458cca0eeaSTejun Heo  * @work: work to find worker for
4468cca0eeaSTejun Heo  *
4478cca0eeaSTejun Heo  * Find a worker which is executing @work on @gcwq.  @bwh should be
4488cca0eeaSTejun Heo  * the hash head obtained by calling busy_worker_head() with the same
4498cca0eeaSTejun Heo  * work.
4508cca0eeaSTejun Heo  *
4518cca0eeaSTejun Heo  * CONTEXT:
4528cca0eeaSTejun Heo  * spin_lock_irq(gcwq->lock).
4538cca0eeaSTejun Heo  *
4548cca0eeaSTejun Heo  * RETURNS:
4558cca0eeaSTejun Heo  * Pointer to worker which is executing @work if found, NULL
4568cca0eeaSTejun Heo  * otherwise.
4578cca0eeaSTejun Heo  */
4588cca0eeaSTejun Heo static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
4598cca0eeaSTejun Heo 						   struct hlist_head *bwh,
4608cca0eeaSTejun Heo 						   struct work_struct *work)
4618cca0eeaSTejun Heo {
4628cca0eeaSTejun Heo 	struct worker *worker;
4638cca0eeaSTejun Heo 	struct hlist_node *tmp;
4648cca0eeaSTejun Heo 
4658cca0eeaSTejun Heo 	hlist_for_each_entry(worker, tmp, bwh, hentry)
4668cca0eeaSTejun Heo 		if (worker->current_work == work)
4678cca0eeaSTejun Heo 			return worker;
4688cca0eeaSTejun Heo 	return NULL;
4698cca0eeaSTejun Heo }
4708cca0eeaSTejun Heo 
4718cca0eeaSTejun Heo /**
4728cca0eeaSTejun Heo  * find_worker_executing_work - find worker which is executing a work
4738cca0eeaSTejun Heo  * @gcwq: gcwq of interest
4748cca0eeaSTejun Heo  * @work: work to find worker for
4758cca0eeaSTejun Heo  *
4768cca0eeaSTejun Heo  * Find a worker which is executing @work on @gcwq.  This function is
4778cca0eeaSTejun Heo  * identical to __find_worker_executing_work() except that this
4788cca0eeaSTejun Heo  * function calculates @bwh itself.
4798cca0eeaSTejun Heo  *
4808cca0eeaSTejun Heo  * CONTEXT:
4818cca0eeaSTejun Heo  * spin_lock_irq(gcwq->lock).
4828cca0eeaSTejun Heo  *
4838cca0eeaSTejun Heo  * RETURNS:
4848cca0eeaSTejun Heo  * Pointer to worker which is executing @work if found, NULL
4858cca0eeaSTejun Heo  * otherwise.
4868cca0eeaSTejun Heo  */
4878cca0eeaSTejun Heo static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
4888cca0eeaSTejun Heo 						 struct work_struct *work)
4898cca0eeaSTejun Heo {
4908cca0eeaSTejun Heo 	return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
4918cca0eeaSTejun Heo 					    work);
4928cca0eeaSTejun Heo }
4938cca0eeaSTejun Heo 
4948cca0eeaSTejun Heo /**
495*7e11629dSTejun Heo  * insert_work - insert a work into gcwq
4964690c4abSTejun Heo  * @cwq: cwq @work belongs to
4974690c4abSTejun Heo  * @work: work to insert
4984690c4abSTejun Heo  * @head: insertion point
4994690c4abSTejun Heo  * @extra_flags: extra WORK_STRUCT_* flags to set
5004690c4abSTejun Heo  *
501*7e11629dSTejun Heo  * Insert @work which belongs to @cwq into @gcwq after @head.
502*7e11629dSTejun Heo  * @extra_flags is or'd to work_struct flags.
5034690c4abSTejun Heo  *
5044690c4abSTejun Heo  * CONTEXT:
5058b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock).
5064690c4abSTejun Heo  */
507b89deed3SOleg Nesterov static void insert_work(struct cpu_workqueue_struct *cwq,
5084690c4abSTejun Heo 			struct work_struct *work, struct list_head *head,
5094690c4abSTejun Heo 			unsigned int extra_flags)
510b89deed3SOleg Nesterov {
5114690c4abSTejun Heo 	/* we own @work, set data and link */
5127a22ad75STejun Heo 	set_work_cwq(work, cwq, extra_flags);
5134690c4abSTejun Heo 
5146e84d644SOleg Nesterov 	/*
5156e84d644SOleg Nesterov 	 * Ensure that we get the right work->data if we see the
5166e84d644SOleg Nesterov 	 * result of list_add() below, see try_to_grab_pending().
5176e84d644SOleg Nesterov 	 */
5186e84d644SOleg Nesterov 	smp_wmb();
5194690c4abSTejun Heo 
5201a4d9b0aSOleg Nesterov 	list_add_tail(&work->entry, head);
521*7e11629dSTejun Heo 	wake_up_worker(cwq->gcwq);
522b89deed3SOleg Nesterov }
523b89deed3SOleg Nesterov 
524502ca9d8STejun Heo /**
525502ca9d8STejun Heo  * cwq_unbind_single_cpu - unbind cwq from single cpu workqueue processing
526502ca9d8STejun Heo  * @cwq: cwq to unbind
527502ca9d8STejun Heo  *
528502ca9d8STejun Heo  * Try to unbind @cwq from single cpu workqueue processing.  If
529502ca9d8STejun Heo  * @cwq->wq is frozen, unbind is delayed till the workqueue is thawed.
530502ca9d8STejun Heo  *
531502ca9d8STejun Heo  * CONTEXT:
532502ca9d8STejun Heo  * spin_lock_irq(gcwq->lock).
533502ca9d8STejun Heo  */
534502ca9d8STejun Heo static void cwq_unbind_single_cpu(struct cpu_workqueue_struct *cwq)
535502ca9d8STejun Heo {
536502ca9d8STejun Heo 	struct workqueue_struct *wq = cwq->wq;
537502ca9d8STejun Heo 	struct global_cwq *gcwq = cwq->gcwq;
538502ca9d8STejun Heo 
539502ca9d8STejun Heo 	BUG_ON(wq->single_cpu != gcwq->cpu);
540502ca9d8STejun Heo 	/*
541502ca9d8STejun Heo 	 * Unbind from workqueue if @cwq is not frozen.  If frozen,
542502ca9d8STejun Heo 	 * thaw_workqueues() will either restart processing on this
543502ca9d8STejun Heo 	 * cpu or unbind if empty.  This keeps works queued while
544502ca9d8STejun Heo 	 * frozen fully ordered and flushable.
545502ca9d8STejun Heo 	 */
546502ca9d8STejun Heo 	if (likely(!(gcwq->flags & GCWQ_FREEZING))) {
547502ca9d8STejun Heo 		smp_wmb();	/* paired with cmpxchg() in __queue_work() */
548502ca9d8STejun Heo 		wq->single_cpu = NR_CPUS;
549502ca9d8STejun Heo 	}
550502ca9d8STejun Heo }
551502ca9d8STejun Heo 
5524690c4abSTejun Heo static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
5531da177e4SLinus Torvalds 			 struct work_struct *work)
5541da177e4SLinus Torvalds {
555502ca9d8STejun Heo 	struct global_cwq *gcwq;
556502ca9d8STejun Heo 	struct cpu_workqueue_struct *cwq;
5571e19ffc6STejun Heo 	struct list_head *worklist;
5581da177e4SLinus Torvalds 	unsigned long flags;
559502ca9d8STejun Heo 	bool arbitrate;
5601da177e4SLinus Torvalds 
561dc186ad7SThomas Gleixner 	debug_work_activate(work);
5621e19ffc6STejun Heo 
56318aa9effSTejun Heo 	/*
56418aa9effSTejun Heo 	 * Determine gcwq to use.  SINGLE_CPU is inherently
56518aa9effSTejun Heo 	 * NON_REENTRANT, so test it first.
56618aa9effSTejun Heo 	 */
567502ca9d8STejun Heo 	if (!(wq->flags & WQ_SINGLE_CPU)) {
56818aa9effSTejun Heo 		struct global_cwq *last_gcwq;
56918aa9effSTejun Heo 
57018aa9effSTejun Heo 		/*
57118aa9effSTejun Heo 		 * It's multi cpu.  If @wq is non-reentrant and @work
57218aa9effSTejun Heo 		 * was previously on a different cpu, it might still
57318aa9effSTejun Heo 		 * be running there, in which case the work needs to
57418aa9effSTejun Heo 		 * be queued on that cpu to guarantee non-reentrance.
57518aa9effSTejun Heo 		 */
576502ca9d8STejun Heo 		gcwq = get_gcwq(cpu);
57718aa9effSTejun Heo 		if (wq->flags & WQ_NON_REENTRANT &&
57818aa9effSTejun Heo 		    (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
57918aa9effSTejun Heo 			struct worker *worker;
58018aa9effSTejun Heo 
58118aa9effSTejun Heo 			spin_lock_irqsave(&last_gcwq->lock, flags);
58218aa9effSTejun Heo 
58318aa9effSTejun Heo 			worker = find_worker_executing_work(last_gcwq, work);
58418aa9effSTejun Heo 
58518aa9effSTejun Heo 			if (worker && worker->current_cwq->wq == wq)
58618aa9effSTejun Heo 				gcwq = last_gcwq;
58718aa9effSTejun Heo 			else {
58818aa9effSTejun Heo 				/* meh... not running there, queue here */
58918aa9effSTejun Heo 				spin_unlock_irqrestore(&last_gcwq->lock, flags);
59018aa9effSTejun Heo 				spin_lock_irqsave(&gcwq->lock, flags);
59118aa9effSTejun Heo 			}
59218aa9effSTejun Heo 		} else
5938b03ae3cSTejun Heo 			spin_lock_irqsave(&gcwq->lock, flags);
594502ca9d8STejun Heo 	} else {
595502ca9d8STejun Heo 		unsigned int req_cpu = cpu;
596502ca9d8STejun Heo 
597502ca9d8STejun Heo 		/*
598502ca9d8STejun Heo 		 * It's a bit more complex for single cpu workqueues.
599502ca9d8STejun Heo 		 * We first need to determine which cpu is going to be
600502ca9d8STejun Heo 		 * used.  If no cpu is currently serving this
601502ca9d8STejun Heo 		 * workqueue, arbitrate using atomic accesses to
602502ca9d8STejun Heo 		 * wq->single_cpu; otherwise, use the current one.
603502ca9d8STejun Heo 		 */
604502ca9d8STejun Heo 	retry:
605502ca9d8STejun Heo 		cpu = wq->single_cpu;
606502ca9d8STejun Heo 		arbitrate = cpu == NR_CPUS;
607502ca9d8STejun Heo 		if (arbitrate)
608502ca9d8STejun Heo 			cpu = req_cpu;
609502ca9d8STejun Heo 
610502ca9d8STejun Heo 		gcwq = get_gcwq(cpu);
611502ca9d8STejun Heo 		spin_lock_irqsave(&gcwq->lock, flags);
612502ca9d8STejun Heo 
613502ca9d8STejun Heo 		/*
614502ca9d8STejun Heo 		 * The following cmpxchg() is a full barrier paired
615502ca9d8STejun Heo 		 * with smp_wmb() in cwq_unbind_single_cpu() and
616502ca9d8STejun Heo 		 * guarantees that all changes to wq->st_* fields are
617502ca9d8STejun Heo 		 * visible on the new cpu after this point.
618502ca9d8STejun Heo 		 */
619502ca9d8STejun Heo 		if (arbitrate)
620502ca9d8STejun Heo 			cmpxchg(&wq->single_cpu, NR_CPUS, cpu);
621502ca9d8STejun Heo 
622502ca9d8STejun Heo 		if (unlikely(wq->single_cpu != cpu)) {
623502ca9d8STejun Heo 			spin_unlock_irqrestore(&gcwq->lock, flags);
624502ca9d8STejun Heo 			goto retry;
625502ca9d8STejun Heo 		}
626502ca9d8STejun Heo 	}
627502ca9d8STejun Heo 
628502ca9d8STejun Heo 	/* gcwq determined, get cwq and queue */
629502ca9d8STejun Heo 	cwq = get_cwq(gcwq->cpu, wq);
630502ca9d8STejun Heo 
6314690c4abSTejun Heo 	BUG_ON(!list_empty(&work->entry));
6321e19ffc6STejun Heo 
63373f53c4aSTejun Heo 	cwq->nr_in_flight[cwq->work_color]++;
6341e19ffc6STejun Heo 
6351e19ffc6STejun Heo 	if (likely(cwq->nr_active < cwq->max_active)) {
6361e19ffc6STejun Heo 		cwq->nr_active++;
637*7e11629dSTejun Heo 		worklist = &gcwq->worklist;
6381e19ffc6STejun Heo 	} else
6391e19ffc6STejun Heo 		worklist = &cwq->delayed_works;
6401e19ffc6STejun Heo 
6411e19ffc6STejun Heo 	insert_work(cwq, work, worklist, work_color_to_flags(cwq->work_color));
6421e19ffc6STejun Heo 
6438b03ae3cSTejun Heo 	spin_unlock_irqrestore(&gcwq->lock, flags);
6441da177e4SLinus Torvalds }
6451da177e4SLinus Torvalds 
6460fcb78c2SRolf Eike Beer /**
6470fcb78c2SRolf Eike Beer  * queue_work - queue work on a workqueue
6480fcb78c2SRolf Eike Beer  * @wq: workqueue to use
6490fcb78c2SRolf Eike Beer  * @work: work to queue
6500fcb78c2SRolf Eike Beer  *
651057647fcSAlan Stern  * Returns 0 if @work was already on a queue, non-zero otherwise.
6521da177e4SLinus Torvalds  *
65300dfcaf7SOleg Nesterov  * We queue the work to the CPU on which it was submitted, but if the CPU dies
65400dfcaf7SOleg Nesterov  * it can be processed by another CPU.
6551da177e4SLinus Torvalds  */
6567ad5b3a5SHarvey Harrison int queue_work(struct workqueue_struct *wq, struct work_struct *work)
6571da177e4SLinus Torvalds {
658ef1ca236SOleg Nesterov 	int ret;
6591da177e4SLinus Torvalds 
660ef1ca236SOleg Nesterov 	ret = queue_work_on(get_cpu(), wq, work);
661a848e3b6SOleg Nesterov 	put_cpu();
662ef1ca236SOleg Nesterov 
6631da177e4SLinus Torvalds 	return ret;
6641da177e4SLinus Torvalds }
665ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_work);
6661da177e4SLinus Torvalds 
667c1a220e7SZhang Rui /**
668c1a220e7SZhang Rui  * queue_work_on - queue work on specific cpu
669c1a220e7SZhang Rui  * @cpu: CPU number to execute work on
670c1a220e7SZhang Rui  * @wq: workqueue to use
671c1a220e7SZhang Rui  * @work: work to queue
672c1a220e7SZhang Rui  *
673c1a220e7SZhang Rui  * Returns 0 if @work was already on a queue, non-zero otherwise.
674c1a220e7SZhang Rui  *
675c1a220e7SZhang Rui  * We queue the work to a specific CPU, the caller must ensure it
676c1a220e7SZhang Rui  * can't go away.
677c1a220e7SZhang Rui  */
678c1a220e7SZhang Rui int
679c1a220e7SZhang Rui queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
680c1a220e7SZhang Rui {
681c1a220e7SZhang Rui 	int ret = 0;
682c1a220e7SZhang Rui 
68322df02bbSTejun Heo 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
6844690c4abSTejun Heo 		__queue_work(cpu, wq, work);
685c1a220e7SZhang Rui 		ret = 1;
686c1a220e7SZhang Rui 	}
687c1a220e7SZhang Rui 	return ret;
688c1a220e7SZhang Rui }
689c1a220e7SZhang Rui EXPORT_SYMBOL_GPL(queue_work_on);
690c1a220e7SZhang Rui 
6916d141c3fSLi Zefan static void delayed_work_timer_fn(unsigned long __data)
6921da177e4SLinus Torvalds {
69352bad64dSDavid Howells 	struct delayed_work *dwork = (struct delayed_work *)__data;
6947a22ad75STejun Heo 	struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
6951da177e4SLinus Torvalds 
6964690c4abSTejun Heo 	__queue_work(smp_processor_id(), cwq->wq, &dwork->work);
6971da177e4SLinus Torvalds }
6981da177e4SLinus Torvalds 
6990fcb78c2SRolf Eike Beer /**
7000fcb78c2SRolf Eike Beer  * queue_delayed_work - queue work on a workqueue after delay
7010fcb78c2SRolf Eike Beer  * @wq: workqueue to use
702af9997e4SRandy Dunlap  * @dwork: delayable work to queue
7030fcb78c2SRolf Eike Beer  * @delay: number of jiffies to wait before queueing
7040fcb78c2SRolf Eike Beer  *
705057647fcSAlan Stern  * Returns 0 if @work was already on a queue, non-zero otherwise.
7060fcb78c2SRolf Eike Beer  */
7077ad5b3a5SHarvey Harrison int queue_delayed_work(struct workqueue_struct *wq,
70852bad64dSDavid Howells 			struct delayed_work *dwork, unsigned long delay)
7091da177e4SLinus Torvalds {
71052bad64dSDavid Howells 	if (delay == 0)
71163bc0362SOleg Nesterov 		return queue_work(wq, &dwork->work);
7121da177e4SLinus Torvalds 
71363bc0362SOleg Nesterov 	return queue_delayed_work_on(-1, wq, dwork, delay);
7141da177e4SLinus Torvalds }
715ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work);
7161da177e4SLinus Torvalds 
7170fcb78c2SRolf Eike Beer /**
7180fcb78c2SRolf Eike Beer  * queue_delayed_work_on - queue work on specific CPU after delay
7190fcb78c2SRolf Eike Beer  * @cpu: CPU number to execute work on
7200fcb78c2SRolf Eike Beer  * @wq: workqueue to use
721af9997e4SRandy Dunlap  * @dwork: work to queue
7220fcb78c2SRolf Eike Beer  * @delay: number of jiffies to wait before queueing
7230fcb78c2SRolf Eike Beer  *
724057647fcSAlan Stern  * Returns 0 if @work was already on a queue, non-zero otherwise.
7250fcb78c2SRolf Eike Beer  */
7267a6bc1cdSVenkatesh Pallipadi int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
72752bad64dSDavid Howells 			struct delayed_work *dwork, unsigned long delay)
7287a6bc1cdSVenkatesh Pallipadi {
7297a6bc1cdSVenkatesh Pallipadi 	int ret = 0;
73052bad64dSDavid Howells 	struct timer_list *timer = &dwork->timer;
73152bad64dSDavid Howells 	struct work_struct *work = &dwork->work;
7327a6bc1cdSVenkatesh Pallipadi 
73322df02bbSTejun Heo 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
7347a22ad75STejun Heo 		struct global_cwq *gcwq = get_work_gcwq(work);
7357a22ad75STejun Heo 		unsigned int lcpu = gcwq ? gcwq->cpu : raw_smp_processor_id();
7367a22ad75STejun Heo 
7377a6bc1cdSVenkatesh Pallipadi 		BUG_ON(timer_pending(timer));
7387a6bc1cdSVenkatesh Pallipadi 		BUG_ON(!list_empty(&work->entry));
7397a6bc1cdSVenkatesh Pallipadi 
7408a3e77ccSAndrew Liu 		timer_stats_timer_set_start_info(&dwork->timer);
7417a22ad75STejun Heo 		/*
7427a22ad75STejun Heo 		 * This stores cwq for the moment, for the timer_fn.
7437a22ad75STejun Heo 		 * Note that the work's gcwq is preserved to allow
7447a22ad75STejun Heo 		 * reentrance detection for delayed works.
7457a22ad75STejun Heo 		 */
7467a22ad75STejun Heo 		set_work_cwq(work, get_cwq(lcpu, wq), 0);
7477a6bc1cdSVenkatesh Pallipadi 		timer->expires = jiffies + delay;
74852bad64dSDavid Howells 		timer->data = (unsigned long)dwork;
7497a6bc1cdSVenkatesh Pallipadi 		timer->function = delayed_work_timer_fn;
75063bc0362SOleg Nesterov 
75163bc0362SOleg Nesterov 		if (unlikely(cpu >= 0))
7527a6bc1cdSVenkatesh Pallipadi 			add_timer_on(timer, cpu);
75363bc0362SOleg Nesterov 		else
75463bc0362SOleg Nesterov 			add_timer(timer);
7557a6bc1cdSVenkatesh Pallipadi 		ret = 1;
7567a6bc1cdSVenkatesh Pallipadi 	}
7577a6bc1cdSVenkatesh Pallipadi 	return ret;
7587a6bc1cdSVenkatesh Pallipadi }
759ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work_on);
7601da177e4SLinus Torvalds 
761c8e55f36STejun Heo /**
762c8e55f36STejun Heo  * worker_enter_idle - enter idle state
763c8e55f36STejun Heo  * @worker: worker which is entering idle state
764c8e55f36STejun Heo  *
765c8e55f36STejun Heo  * @worker is entering idle state.  Update stats and idle timer if
766c8e55f36STejun Heo  * necessary.
767c8e55f36STejun Heo  *
768c8e55f36STejun Heo  * LOCKING:
769c8e55f36STejun Heo  * spin_lock_irq(gcwq->lock).
770c8e55f36STejun Heo  */
771c8e55f36STejun Heo static void worker_enter_idle(struct worker *worker)
772c8e55f36STejun Heo {
773c8e55f36STejun Heo 	struct global_cwq *gcwq = worker->gcwq;
774c8e55f36STejun Heo 
775c8e55f36STejun Heo 	BUG_ON(worker->flags & WORKER_IDLE);
776c8e55f36STejun Heo 	BUG_ON(!list_empty(&worker->entry) &&
777c8e55f36STejun Heo 	       (worker->hentry.next || worker->hentry.pprev));
778c8e55f36STejun Heo 
779c8e55f36STejun Heo 	worker->flags |= WORKER_IDLE;
780c8e55f36STejun Heo 	gcwq->nr_idle++;
781c8e55f36STejun Heo 
782c8e55f36STejun Heo 	/* idle_list is LIFO */
783c8e55f36STejun Heo 	list_add(&worker->entry, &gcwq->idle_list);
784db7bccf4STejun Heo 
785db7bccf4STejun Heo 	if (unlikely(worker->flags & WORKER_ROGUE))
786db7bccf4STejun Heo 		wake_up_all(&gcwq->trustee_wait);
787c8e55f36STejun Heo }
788c8e55f36STejun Heo 
789c8e55f36STejun Heo /**
790c8e55f36STejun Heo  * worker_leave_idle - leave idle state
791c8e55f36STejun Heo  * @worker: worker which is leaving idle state
792c8e55f36STejun Heo  *
793c8e55f36STejun Heo  * @worker is leaving idle state.  Update stats.
794c8e55f36STejun Heo  *
795c8e55f36STejun Heo  * LOCKING:
796c8e55f36STejun Heo  * spin_lock_irq(gcwq->lock).
797c8e55f36STejun Heo  */
798c8e55f36STejun Heo static void worker_leave_idle(struct worker *worker)
799c8e55f36STejun Heo {
800c8e55f36STejun Heo 	struct global_cwq *gcwq = worker->gcwq;
801c8e55f36STejun Heo 
802c8e55f36STejun Heo 	BUG_ON(!(worker->flags & WORKER_IDLE));
803c8e55f36STejun Heo 	worker->flags &= ~WORKER_IDLE;
804c8e55f36STejun Heo 	gcwq->nr_idle--;
805c8e55f36STejun Heo 	list_del_init(&worker->entry);
806c8e55f36STejun Heo }
807c8e55f36STejun Heo 
808c34056a3STejun Heo static struct worker *alloc_worker(void)
809c34056a3STejun Heo {
810c34056a3STejun Heo 	struct worker *worker;
811c34056a3STejun Heo 
812c34056a3STejun Heo 	worker = kzalloc(sizeof(*worker), GFP_KERNEL);
813c8e55f36STejun Heo 	if (worker) {
814c8e55f36STejun Heo 		INIT_LIST_HEAD(&worker->entry);
815affee4b2STejun Heo 		INIT_LIST_HEAD(&worker->scheduled);
816c8e55f36STejun Heo 	}
817c34056a3STejun Heo 	return worker;
818c34056a3STejun Heo }
819c34056a3STejun Heo 
820c34056a3STejun Heo /**
821c34056a3STejun Heo  * create_worker - create a new workqueue worker
822*7e11629dSTejun Heo  * @gcwq: gcwq the new worker will belong to
823c34056a3STejun Heo  * @bind: whether to set affinity to @cpu or not
824c34056a3STejun Heo  *
825*7e11629dSTejun Heo  * Create a new worker which is bound to @gcwq.  The returned worker
826c34056a3STejun Heo  * can be started by calling start_worker() or destroyed using
827c34056a3STejun Heo  * destroy_worker().
828c34056a3STejun Heo  *
829c34056a3STejun Heo  * CONTEXT:
830c34056a3STejun Heo  * Might sleep.  Does GFP_KERNEL allocations.
831c34056a3STejun Heo  *
832c34056a3STejun Heo  * RETURNS:
833c34056a3STejun Heo  * Pointer to the newly created worker.
834c34056a3STejun Heo  */
835*7e11629dSTejun Heo static struct worker *create_worker(struct global_cwq *gcwq, bool bind)
836c34056a3STejun Heo {
837c34056a3STejun Heo 	int id = -1;
838c34056a3STejun Heo 	struct worker *worker = NULL;
839c34056a3STejun Heo 
8408b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
8418b03ae3cSTejun Heo 	while (ida_get_new(&gcwq->worker_ida, &id)) {
8428b03ae3cSTejun Heo 		spin_unlock_irq(&gcwq->lock);
8438b03ae3cSTejun Heo 		if (!ida_pre_get(&gcwq->worker_ida, GFP_KERNEL))
844c34056a3STejun Heo 			goto fail;
8458b03ae3cSTejun Heo 		spin_lock_irq(&gcwq->lock);
846c34056a3STejun Heo 	}
8478b03ae3cSTejun Heo 	spin_unlock_irq(&gcwq->lock);
848c34056a3STejun Heo 
849c34056a3STejun Heo 	worker = alloc_worker();
850c34056a3STejun Heo 	if (!worker)
851c34056a3STejun Heo 		goto fail;
852c34056a3STejun Heo 
8538b03ae3cSTejun Heo 	worker->gcwq = gcwq;
854c34056a3STejun Heo 	worker->id = id;
855c34056a3STejun Heo 
856c34056a3STejun Heo 	worker->task = kthread_create(worker_thread, worker, "kworker/%u:%d",
8578b03ae3cSTejun Heo 				      gcwq->cpu, id);
858c34056a3STejun Heo 	if (IS_ERR(worker->task))
859c34056a3STejun Heo 		goto fail;
860c34056a3STejun Heo 
861db7bccf4STejun Heo 	/*
862db7bccf4STejun Heo 	 * A rogue worker will become a regular one if CPU comes
863db7bccf4STejun Heo 	 * online later on.  Make sure every worker has
864db7bccf4STejun Heo 	 * PF_THREAD_BOUND set.
865db7bccf4STejun Heo 	 */
866c34056a3STejun Heo 	if (bind)
8678b03ae3cSTejun Heo 		kthread_bind(worker->task, gcwq->cpu);
868db7bccf4STejun Heo 	else
869db7bccf4STejun Heo 		worker->task->flags |= PF_THREAD_BOUND;
870c34056a3STejun Heo 
871c34056a3STejun Heo 	return worker;
872c34056a3STejun Heo fail:
873c34056a3STejun Heo 	if (id >= 0) {
8748b03ae3cSTejun Heo 		spin_lock_irq(&gcwq->lock);
8758b03ae3cSTejun Heo 		ida_remove(&gcwq->worker_ida, id);
8768b03ae3cSTejun Heo 		spin_unlock_irq(&gcwq->lock);
877c34056a3STejun Heo 	}
878c34056a3STejun Heo 	kfree(worker);
879c34056a3STejun Heo 	return NULL;
880c34056a3STejun Heo }
881c34056a3STejun Heo 
882c34056a3STejun Heo /**
883c34056a3STejun Heo  * start_worker - start a newly created worker
884c34056a3STejun Heo  * @worker: worker to start
885c34056a3STejun Heo  *
886c8e55f36STejun Heo  * Make the gcwq aware of @worker and start it.
887c34056a3STejun Heo  *
888c34056a3STejun Heo  * CONTEXT:
8898b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock).
890c34056a3STejun Heo  */
891c34056a3STejun Heo static void start_worker(struct worker *worker)
892c34056a3STejun Heo {
893c8e55f36STejun Heo 	worker->flags |= WORKER_STARTED;
894c8e55f36STejun Heo 	worker->gcwq->nr_workers++;
895c8e55f36STejun Heo 	worker_enter_idle(worker);
896c34056a3STejun Heo 	wake_up_process(worker->task);
897c34056a3STejun Heo }
898c34056a3STejun Heo 
899c34056a3STejun Heo /**
900c34056a3STejun Heo  * destroy_worker - destroy a workqueue worker
901c34056a3STejun Heo  * @worker: worker to be destroyed
902c34056a3STejun Heo  *
903c8e55f36STejun Heo  * Destroy @worker and adjust @gcwq stats accordingly.
904c8e55f36STejun Heo  *
905c8e55f36STejun Heo  * CONTEXT:
906c8e55f36STejun Heo  * spin_lock_irq(gcwq->lock) which is released and regrabbed.
907c34056a3STejun Heo  */
908c34056a3STejun Heo static void destroy_worker(struct worker *worker)
909c34056a3STejun Heo {
9108b03ae3cSTejun Heo 	struct global_cwq *gcwq = worker->gcwq;
911c34056a3STejun Heo 	int id = worker->id;
912c34056a3STejun Heo 
913c34056a3STejun Heo 	/* sanity check frenzy */
914c34056a3STejun Heo 	BUG_ON(worker->current_work);
915affee4b2STejun Heo 	BUG_ON(!list_empty(&worker->scheduled));
916c34056a3STejun Heo 
917c8e55f36STejun Heo 	if (worker->flags & WORKER_STARTED)
918c8e55f36STejun Heo 		gcwq->nr_workers--;
919c8e55f36STejun Heo 	if (worker->flags & WORKER_IDLE)
920c8e55f36STejun Heo 		gcwq->nr_idle--;
921c8e55f36STejun Heo 
922c8e55f36STejun Heo 	list_del_init(&worker->entry);
923c8e55f36STejun Heo 	worker->flags |= WORKER_DIE;
924c8e55f36STejun Heo 
925c8e55f36STejun Heo 	spin_unlock_irq(&gcwq->lock);
926c8e55f36STejun Heo 
927c34056a3STejun Heo 	kthread_stop(worker->task);
928c34056a3STejun Heo 	kfree(worker);
929c34056a3STejun Heo 
9308b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
9318b03ae3cSTejun Heo 	ida_remove(&gcwq->worker_ida, id);
932c34056a3STejun Heo }
933c34056a3STejun Heo 
934a62428c0STejun Heo /**
935affee4b2STejun Heo  * move_linked_works - move linked works to a list
936affee4b2STejun Heo  * @work: start of series of works to be scheduled
937affee4b2STejun Heo  * @head: target list to append @work to
938affee4b2STejun Heo  * @nextp: out paramter for nested worklist walking
939affee4b2STejun Heo  *
940affee4b2STejun Heo  * Schedule linked works starting from @work to @head.  Work series to
941affee4b2STejun Heo  * be scheduled starts at @work and includes any consecutive work with
942affee4b2STejun Heo  * WORK_STRUCT_LINKED set in its predecessor.
943affee4b2STejun Heo  *
944affee4b2STejun Heo  * If @nextp is not NULL, it's updated to point to the next work of
945affee4b2STejun Heo  * the last scheduled work.  This allows move_linked_works() to be
946affee4b2STejun Heo  * nested inside outer list_for_each_entry_safe().
947affee4b2STejun Heo  *
948affee4b2STejun Heo  * CONTEXT:
9498b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock).
950affee4b2STejun Heo  */
951affee4b2STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head,
952affee4b2STejun Heo 			      struct work_struct **nextp)
953affee4b2STejun Heo {
954affee4b2STejun Heo 	struct work_struct *n;
955affee4b2STejun Heo 
956affee4b2STejun Heo 	/*
957affee4b2STejun Heo 	 * Linked worklist will always end before the end of the list,
958affee4b2STejun Heo 	 * use NULL for list head.
959affee4b2STejun Heo 	 */
960affee4b2STejun Heo 	list_for_each_entry_safe_from(work, n, NULL, entry) {
961affee4b2STejun Heo 		list_move_tail(&work->entry, head);
962affee4b2STejun Heo 		if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
963affee4b2STejun Heo 			break;
964affee4b2STejun Heo 	}
965affee4b2STejun Heo 
966affee4b2STejun Heo 	/*
967affee4b2STejun Heo 	 * If we're already inside safe list traversal and have moved
968affee4b2STejun Heo 	 * multiple works to the scheduled queue, the next position
969affee4b2STejun Heo 	 * needs to be updated.
970affee4b2STejun Heo 	 */
971affee4b2STejun Heo 	if (nextp)
972affee4b2STejun Heo 		*nextp = n;
973affee4b2STejun Heo }
974affee4b2STejun Heo 
9751e19ffc6STejun Heo static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
9761e19ffc6STejun Heo {
9771e19ffc6STejun Heo 	struct work_struct *work = list_first_entry(&cwq->delayed_works,
9781e19ffc6STejun Heo 						    struct work_struct, entry);
9791e19ffc6STejun Heo 
980*7e11629dSTejun Heo 	move_linked_works(work, &cwq->gcwq->worklist, NULL);
9811e19ffc6STejun Heo 	cwq->nr_active++;
9821e19ffc6STejun Heo }
9831e19ffc6STejun Heo 
984affee4b2STejun Heo /**
98573f53c4aSTejun Heo  * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
98673f53c4aSTejun Heo  * @cwq: cwq of interest
98773f53c4aSTejun Heo  * @color: color of work which left the queue
98873f53c4aSTejun Heo  *
98973f53c4aSTejun Heo  * A work either has completed or is removed from pending queue,
99073f53c4aSTejun Heo  * decrement nr_in_flight of its cwq and handle workqueue flushing.
99173f53c4aSTejun Heo  *
99273f53c4aSTejun Heo  * CONTEXT:
9938b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock).
99473f53c4aSTejun Heo  */
99573f53c4aSTejun Heo static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color)
99673f53c4aSTejun Heo {
99773f53c4aSTejun Heo 	/* ignore uncolored works */
99873f53c4aSTejun Heo 	if (color == WORK_NO_COLOR)
99973f53c4aSTejun Heo 		return;
100073f53c4aSTejun Heo 
100173f53c4aSTejun Heo 	cwq->nr_in_flight[color]--;
10021e19ffc6STejun Heo 	cwq->nr_active--;
10031e19ffc6STejun Heo 
1004502ca9d8STejun Heo 	if (!list_empty(&cwq->delayed_works)) {
10051e19ffc6STejun Heo 		/* one down, submit a delayed one */
1006502ca9d8STejun Heo 		if (cwq->nr_active < cwq->max_active)
10071e19ffc6STejun Heo 			cwq_activate_first_delayed(cwq);
1008502ca9d8STejun Heo 	} else if (!cwq->nr_active && cwq->wq->flags & WQ_SINGLE_CPU) {
1009502ca9d8STejun Heo 		/* this was the last work, unbind from single cpu */
1010502ca9d8STejun Heo 		cwq_unbind_single_cpu(cwq);
1011502ca9d8STejun Heo 	}
101273f53c4aSTejun Heo 
101373f53c4aSTejun Heo 	/* is flush in progress and are we at the flushing tip? */
101473f53c4aSTejun Heo 	if (likely(cwq->flush_color != color))
101573f53c4aSTejun Heo 		return;
101673f53c4aSTejun Heo 
101773f53c4aSTejun Heo 	/* are there still in-flight works? */
101873f53c4aSTejun Heo 	if (cwq->nr_in_flight[color])
101973f53c4aSTejun Heo 		return;
102073f53c4aSTejun Heo 
102173f53c4aSTejun Heo 	/* this cwq is done, clear flush_color */
102273f53c4aSTejun Heo 	cwq->flush_color = -1;
102373f53c4aSTejun Heo 
102473f53c4aSTejun Heo 	/*
102573f53c4aSTejun Heo 	 * If this was the last cwq, wake up the first flusher.  It
102673f53c4aSTejun Heo 	 * will handle the rest.
102773f53c4aSTejun Heo 	 */
102873f53c4aSTejun Heo 	if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
102973f53c4aSTejun Heo 		complete(&cwq->wq->first_flusher->done);
103073f53c4aSTejun Heo }
103173f53c4aSTejun Heo 
103273f53c4aSTejun Heo /**
1033a62428c0STejun Heo  * process_one_work - process single work
1034c34056a3STejun Heo  * @worker: self
1035a62428c0STejun Heo  * @work: work to process
1036a62428c0STejun Heo  *
1037a62428c0STejun Heo  * Process @work.  This function contains all the logics necessary to
1038a62428c0STejun Heo  * process a single work including synchronization against and
1039a62428c0STejun Heo  * interaction with other workers on the same cpu, queueing and
1040a62428c0STejun Heo  * flushing.  As long as context requirement is met, any worker can
1041a62428c0STejun Heo  * call this function to process a work.
1042a62428c0STejun Heo  *
1043a62428c0STejun Heo  * CONTEXT:
10448b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock) which is released and regrabbed.
1045a62428c0STejun Heo  */
1046c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work)
10471da177e4SLinus Torvalds {
1048*7e11629dSTejun Heo 	struct cpu_workqueue_struct *cwq = get_work_cwq(work);
10498b03ae3cSTejun Heo 	struct global_cwq *gcwq = cwq->gcwq;
1050c8e55f36STejun Heo 	struct hlist_head *bwh = busy_worker_head(gcwq, work);
10516bb49e59SDavid Howells 	work_func_t f = work->func;
105273f53c4aSTejun Heo 	int work_color;
1053*7e11629dSTejun Heo 	struct worker *collision;
10544e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
10554e6045f1SJohannes Berg 	/*
1056a62428c0STejun Heo 	 * It is permissible to free the struct work_struct from
1057a62428c0STejun Heo 	 * inside the function that is called from it, this we need to
1058a62428c0STejun Heo 	 * take into account for lockdep too.  To avoid bogus "held
1059a62428c0STejun Heo 	 * lock freed" warnings as well as problems when looking into
1060a62428c0STejun Heo 	 * work->lockdep_map, make a copy and use that here.
10614e6045f1SJohannes Berg 	 */
10624e6045f1SJohannes Berg 	struct lockdep_map lockdep_map = work->lockdep_map;
10634e6045f1SJohannes Berg #endif
1064*7e11629dSTejun Heo 	/*
1065*7e11629dSTejun Heo 	 * A single work shouldn't be executed concurrently by
1066*7e11629dSTejun Heo 	 * multiple workers on a single cpu.  Check whether anyone is
1067*7e11629dSTejun Heo 	 * already processing the work.  If so, defer the work to the
1068*7e11629dSTejun Heo 	 * currently executing one.
1069*7e11629dSTejun Heo 	 */
1070*7e11629dSTejun Heo 	collision = __find_worker_executing_work(gcwq, bwh, work);
1071*7e11629dSTejun Heo 	if (unlikely(collision)) {
1072*7e11629dSTejun Heo 		move_linked_works(work, &collision->scheduled, NULL);
1073*7e11629dSTejun Heo 		return;
1074*7e11629dSTejun Heo 	}
1075*7e11629dSTejun Heo 
1076a62428c0STejun Heo 	/* claim and process */
1077dc186ad7SThomas Gleixner 	debug_work_deactivate(work);
1078c8e55f36STejun Heo 	hlist_add_head(&worker->hentry, bwh);
1079c34056a3STejun Heo 	worker->current_work = work;
10808cca0eeaSTejun Heo 	worker->current_cwq = cwq;
108173f53c4aSTejun Heo 	work_color = get_work_color(work);
10827a22ad75STejun Heo 
10837a22ad75STejun Heo 	/* record the current cpu number in the work data and dequeue */
10847a22ad75STejun Heo 	set_work_cpu(work, gcwq->cpu);
1085a62428c0STejun Heo 	list_del_init(&work->entry);
1086a62428c0STejun Heo 
10878b03ae3cSTejun Heo 	spin_unlock_irq(&gcwq->lock);
10881da177e4SLinus Torvalds 
108923b2e599SOleg Nesterov 	work_clear_pending(work);
10903295f0efSIngo Molnar 	lock_map_acquire(&cwq->wq->lockdep_map);
10913295f0efSIngo Molnar 	lock_map_acquire(&lockdep_map);
109265f27f38SDavid Howells 	f(work);
10933295f0efSIngo Molnar 	lock_map_release(&lockdep_map);
10943295f0efSIngo Molnar 	lock_map_release(&cwq->wq->lockdep_map);
10951da177e4SLinus Torvalds 
1096d5abe669SPeter Zijlstra 	if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
1097d5abe669SPeter Zijlstra 		printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
1098d5abe669SPeter Zijlstra 		       "%s/0x%08x/%d\n",
1099a62428c0STejun Heo 		       current->comm, preempt_count(), task_pid_nr(current));
1100d5abe669SPeter Zijlstra 		printk(KERN_ERR "    last function: ");
1101d5abe669SPeter Zijlstra 		print_symbol("%s\n", (unsigned long)f);
1102d5abe669SPeter Zijlstra 		debug_show_held_locks(current);
1103d5abe669SPeter Zijlstra 		dump_stack();
1104d5abe669SPeter Zijlstra 	}
1105d5abe669SPeter Zijlstra 
11068b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
1107a62428c0STejun Heo 
1108a62428c0STejun Heo 	/* we're done with it, release */
1109c8e55f36STejun Heo 	hlist_del_init(&worker->hentry);
1110c34056a3STejun Heo 	worker->current_work = NULL;
11118cca0eeaSTejun Heo 	worker->current_cwq = NULL;
111273f53c4aSTejun Heo 	cwq_dec_nr_in_flight(cwq, work_color);
11131da177e4SLinus Torvalds }
1114a62428c0STejun Heo 
1115affee4b2STejun Heo /**
1116affee4b2STejun Heo  * process_scheduled_works - process scheduled works
1117affee4b2STejun Heo  * @worker: self
1118affee4b2STejun Heo  *
1119affee4b2STejun Heo  * Process all scheduled works.  Please note that the scheduled list
1120affee4b2STejun Heo  * may change while processing a work, so this function repeatedly
1121affee4b2STejun Heo  * fetches a work from the top and executes it.
1122affee4b2STejun Heo  *
1123affee4b2STejun Heo  * CONTEXT:
11248b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1125affee4b2STejun Heo  * multiple times.
1126affee4b2STejun Heo  */
1127affee4b2STejun Heo static void process_scheduled_works(struct worker *worker)
1128a62428c0STejun Heo {
1129affee4b2STejun Heo 	while (!list_empty(&worker->scheduled)) {
1130affee4b2STejun Heo 		struct work_struct *work = list_first_entry(&worker->scheduled,
1131a62428c0STejun Heo 						struct work_struct, entry);
1132c34056a3STejun Heo 		process_one_work(worker, work);
1133a62428c0STejun Heo 	}
11341da177e4SLinus Torvalds }
11351da177e4SLinus Torvalds 
11364690c4abSTejun Heo /**
11374690c4abSTejun Heo  * worker_thread - the worker thread function
1138c34056a3STejun Heo  * @__worker: self
11394690c4abSTejun Heo  *
11404690c4abSTejun Heo  * The cwq worker thread function.
11414690c4abSTejun Heo  */
1142c34056a3STejun Heo static int worker_thread(void *__worker)
11431da177e4SLinus Torvalds {
1144c34056a3STejun Heo 	struct worker *worker = __worker;
11458b03ae3cSTejun Heo 	struct global_cwq *gcwq = worker->gcwq;
11461da177e4SLinus Torvalds 
1147c8e55f36STejun Heo woke_up:
11488b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
1149affee4b2STejun Heo 
1150c8e55f36STejun Heo 	/* DIE can be set only while we're idle, checking here is enough */
1151c8e55f36STejun Heo 	if (worker->flags & WORKER_DIE) {
1152c8e55f36STejun Heo 		spin_unlock_irq(&gcwq->lock);
1153c8e55f36STejun Heo 		return 0;
1154c8e55f36STejun Heo 	}
1155c8e55f36STejun Heo 
1156c8e55f36STejun Heo 	worker_leave_idle(worker);
1157db7bccf4STejun Heo recheck:
1158c8e55f36STejun Heo 	/*
1159c8e55f36STejun Heo 	 * ->scheduled list can only be filled while a worker is
1160c8e55f36STejun Heo 	 * preparing to process a work or actually processing it.
1161c8e55f36STejun Heo 	 * Make sure nobody diddled with it while I was sleeping.
1162c8e55f36STejun Heo 	 */
1163c8e55f36STejun Heo 	BUG_ON(!list_empty(&worker->scheduled));
1164c8e55f36STejun Heo 
1165*7e11629dSTejun Heo 	while (!list_empty(&gcwq->worklist)) {
1166affee4b2STejun Heo 		struct work_struct *work =
1167*7e11629dSTejun Heo 			list_first_entry(&gcwq->worklist,
1168affee4b2STejun Heo 					 struct work_struct, entry);
1169affee4b2STejun Heo 
1170db7bccf4STejun Heo 		/*
1171db7bccf4STejun Heo 		 * The following is a rather inefficient way to close
1172db7bccf4STejun Heo 		 * race window against cpu hotplug operations.  Will
1173db7bccf4STejun Heo 		 * be replaced soon.
1174db7bccf4STejun Heo 		 */
1175db7bccf4STejun Heo 		if (unlikely(!(worker->flags & WORKER_ROGUE) &&
1176db7bccf4STejun Heo 			     !cpumask_equal(&worker->task->cpus_allowed,
1177db7bccf4STejun Heo 					    get_cpu_mask(gcwq->cpu)))) {
1178db7bccf4STejun Heo 			spin_unlock_irq(&gcwq->lock);
1179db7bccf4STejun Heo 			set_cpus_allowed_ptr(worker->task,
1180db7bccf4STejun Heo 					     get_cpu_mask(gcwq->cpu));
1181db7bccf4STejun Heo 			cpu_relax();
1182db7bccf4STejun Heo 			spin_lock_irq(&gcwq->lock);
1183db7bccf4STejun Heo 			goto recheck;
1184db7bccf4STejun Heo 		}
1185db7bccf4STejun Heo 
1186c8e55f36STejun Heo 		if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
1187affee4b2STejun Heo 			/* optimization path, not strictly necessary */
1188affee4b2STejun Heo 			process_one_work(worker, work);
1189affee4b2STejun Heo 			if (unlikely(!list_empty(&worker->scheduled)))
1190affee4b2STejun Heo 				process_scheduled_works(worker);
1191affee4b2STejun Heo 		} else {
1192c8e55f36STejun Heo 			move_linked_works(work, &worker->scheduled, NULL);
1193affee4b2STejun Heo 			process_scheduled_works(worker);
1194affee4b2STejun Heo 		}
1195affee4b2STejun Heo 	}
1196affee4b2STejun Heo 
1197c8e55f36STejun Heo 	/*
1198c8e55f36STejun Heo 	 * gcwq->lock is held and there's no work to process, sleep.
1199c8e55f36STejun Heo 	 * Workers are woken up only while holding gcwq->lock, so
1200c8e55f36STejun Heo 	 * setting the current state before releasing gcwq->lock is
1201c8e55f36STejun Heo 	 * enough to prevent losing any event.
1202c8e55f36STejun Heo 	 */
1203c8e55f36STejun Heo 	worker_enter_idle(worker);
1204c8e55f36STejun Heo 	__set_current_state(TASK_INTERRUPTIBLE);
12058b03ae3cSTejun Heo 	spin_unlock_irq(&gcwq->lock);
1206c8e55f36STejun Heo 	schedule();
1207c8e55f36STejun Heo 	goto woke_up;
12081da177e4SLinus Torvalds }
12091da177e4SLinus Torvalds 
1210fc2e4d70SOleg Nesterov struct wq_barrier {
1211fc2e4d70SOleg Nesterov 	struct work_struct	work;
1212fc2e4d70SOleg Nesterov 	struct completion	done;
1213fc2e4d70SOleg Nesterov };
1214fc2e4d70SOleg Nesterov 
1215fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work)
1216fc2e4d70SOleg Nesterov {
1217fc2e4d70SOleg Nesterov 	struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
1218fc2e4d70SOleg Nesterov 	complete(&barr->done);
1219fc2e4d70SOleg Nesterov }
1220fc2e4d70SOleg Nesterov 
12214690c4abSTejun Heo /**
12224690c4abSTejun Heo  * insert_wq_barrier - insert a barrier work
12234690c4abSTejun Heo  * @cwq: cwq to insert barrier into
12244690c4abSTejun Heo  * @barr: wq_barrier to insert
1225affee4b2STejun Heo  * @target: target work to attach @barr to
1226affee4b2STejun Heo  * @worker: worker currently executing @target, NULL if @target is not executing
12274690c4abSTejun Heo  *
1228affee4b2STejun Heo  * @barr is linked to @target such that @barr is completed only after
1229affee4b2STejun Heo  * @target finishes execution.  Please note that the ordering
1230affee4b2STejun Heo  * guarantee is observed only with respect to @target and on the local
1231affee4b2STejun Heo  * cpu.
1232affee4b2STejun Heo  *
1233affee4b2STejun Heo  * Currently, a queued barrier can't be canceled.  This is because
1234affee4b2STejun Heo  * try_to_grab_pending() can't determine whether the work to be
1235affee4b2STejun Heo  * grabbed is at the head of the queue and thus can't clear LINKED
1236affee4b2STejun Heo  * flag of the previous work while there must be a valid next work
1237affee4b2STejun Heo  * after a work with LINKED flag set.
1238affee4b2STejun Heo  *
1239affee4b2STejun Heo  * Note that when @worker is non-NULL, @target may be modified
1240affee4b2STejun Heo  * underneath us, so we can't reliably determine cwq from @target.
12414690c4abSTejun Heo  *
12424690c4abSTejun Heo  * CONTEXT:
12438b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock).
12444690c4abSTejun Heo  */
124583c22520SOleg Nesterov static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
1246affee4b2STejun Heo 			      struct wq_barrier *barr,
1247affee4b2STejun Heo 			      struct work_struct *target, struct worker *worker)
1248fc2e4d70SOleg Nesterov {
1249affee4b2STejun Heo 	struct list_head *head;
1250affee4b2STejun Heo 	unsigned int linked = 0;
1251affee4b2STejun Heo 
1252dc186ad7SThomas Gleixner 	/*
12538b03ae3cSTejun Heo 	 * debugobject calls are safe here even with gcwq->lock locked
1254dc186ad7SThomas Gleixner 	 * as we know for sure that this will not trigger any of the
1255dc186ad7SThomas Gleixner 	 * checks and call back into the fixup functions where we
1256dc186ad7SThomas Gleixner 	 * might deadlock.
1257dc186ad7SThomas Gleixner 	 */
1258dc186ad7SThomas Gleixner 	INIT_WORK_ON_STACK(&barr->work, wq_barrier_func);
125922df02bbSTejun Heo 	__set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
1260fc2e4d70SOleg Nesterov 	init_completion(&barr->done);
126183c22520SOleg Nesterov 
1262affee4b2STejun Heo 	/*
1263affee4b2STejun Heo 	 * If @target is currently being executed, schedule the
1264affee4b2STejun Heo 	 * barrier to the worker; otherwise, put it after @target.
1265affee4b2STejun Heo 	 */
1266affee4b2STejun Heo 	if (worker)
1267affee4b2STejun Heo 		head = worker->scheduled.next;
1268affee4b2STejun Heo 	else {
1269affee4b2STejun Heo 		unsigned long *bits = work_data_bits(target);
1270affee4b2STejun Heo 
1271affee4b2STejun Heo 		head = target->entry.next;
1272affee4b2STejun Heo 		/* there can already be other linked works, inherit and set */
1273affee4b2STejun Heo 		linked = *bits & WORK_STRUCT_LINKED;
1274affee4b2STejun Heo 		__set_bit(WORK_STRUCT_LINKED_BIT, bits);
1275affee4b2STejun Heo 	}
1276affee4b2STejun Heo 
1277dc186ad7SThomas Gleixner 	debug_work_activate(&barr->work);
1278affee4b2STejun Heo 	insert_work(cwq, &barr->work, head,
1279affee4b2STejun Heo 		    work_color_to_flags(WORK_NO_COLOR) | linked);
1280fc2e4d70SOleg Nesterov }
1281fc2e4d70SOleg Nesterov 
128273f53c4aSTejun Heo /**
128373f53c4aSTejun Heo  * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
128473f53c4aSTejun Heo  * @wq: workqueue being flushed
128573f53c4aSTejun Heo  * @flush_color: new flush color, < 0 for no-op
128673f53c4aSTejun Heo  * @work_color: new work color, < 0 for no-op
128773f53c4aSTejun Heo  *
128873f53c4aSTejun Heo  * Prepare cwqs for workqueue flushing.
128973f53c4aSTejun Heo  *
129073f53c4aSTejun Heo  * If @flush_color is non-negative, flush_color on all cwqs should be
129173f53c4aSTejun Heo  * -1.  If no cwq has in-flight commands at the specified color, all
129273f53c4aSTejun Heo  * cwq->flush_color's stay at -1 and %false is returned.  If any cwq
129373f53c4aSTejun Heo  * has in flight commands, its cwq->flush_color is set to
129473f53c4aSTejun Heo  * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
129573f53c4aSTejun Heo  * wakeup logic is armed and %true is returned.
129673f53c4aSTejun Heo  *
129773f53c4aSTejun Heo  * The caller should have initialized @wq->first_flusher prior to
129873f53c4aSTejun Heo  * calling this function with non-negative @flush_color.  If
129973f53c4aSTejun Heo  * @flush_color is negative, no flush color update is done and %false
130073f53c4aSTejun Heo  * is returned.
130173f53c4aSTejun Heo  *
130273f53c4aSTejun Heo  * If @work_color is non-negative, all cwqs should have the same
130373f53c4aSTejun Heo  * work_color which is previous to @work_color and all will be
130473f53c4aSTejun Heo  * advanced to @work_color.
130573f53c4aSTejun Heo  *
130673f53c4aSTejun Heo  * CONTEXT:
130773f53c4aSTejun Heo  * mutex_lock(wq->flush_mutex).
130873f53c4aSTejun Heo  *
130973f53c4aSTejun Heo  * RETURNS:
131073f53c4aSTejun Heo  * %true if @flush_color >= 0 and there's something to flush.  %false
131173f53c4aSTejun Heo  * otherwise.
131273f53c4aSTejun Heo  */
131373f53c4aSTejun Heo static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
131473f53c4aSTejun Heo 				      int flush_color, int work_color)
13151da177e4SLinus Torvalds {
131673f53c4aSTejun Heo 	bool wait = false;
131773f53c4aSTejun Heo 	unsigned int cpu;
13181da177e4SLinus Torvalds 
131973f53c4aSTejun Heo 	if (flush_color >= 0) {
132073f53c4aSTejun Heo 		BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
132173f53c4aSTejun Heo 		atomic_set(&wq->nr_cwqs_to_flush, 1);
132273f53c4aSTejun Heo 	}
132373f53c4aSTejun Heo 
132473f53c4aSTejun Heo 	for_each_possible_cpu(cpu) {
132573f53c4aSTejun Heo 		struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
13268b03ae3cSTejun Heo 		struct global_cwq *gcwq = cwq->gcwq;
13272355b70fSLai Jiangshan 
13288b03ae3cSTejun Heo 		spin_lock_irq(&gcwq->lock);
132973f53c4aSTejun Heo 
133073f53c4aSTejun Heo 		if (flush_color >= 0) {
133173f53c4aSTejun Heo 			BUG_ON(cwq->flush_color != -1);
133273f53c4aSTejun Heo 
133373f53c4aSTejun Heo 			if (cwq->nr_in_flight[flush_color]) {
133473f53c4aSTejun Heo 				cwq->flush_color = flush_color;
133573f53c4aSTejun Heo 				atomic_inc(&wq->nr_cwqs_to_flush);
133673f53c4aSTejun Heo 				wait = true;
133783c22520SOleg Nesterov 			}
133873f53c4aSTejun Heo 		}
133973f53c4aSTejun Heo 
134073f53c4aSTejun Heo 		if (work_color >= 0) {
134173f53c4aSTejun Heo 			BUG_ON(work_color != work_next_color(cwq->work_color));
134273f53c4aSTejun Heo 			cwq->work_color = work_color;
134373f53c4aSTejun Heo 		}
134473f53c4aSTejun Heo 
13458b03ae3cSTejun Heo 		spin_unlock_irq(&gcwq->lock);
1346dc186ad7SThomas Gleixner 	}
134714441960SOleg Nesterov 
134873f53c4aSTejun Heo 	if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
134973f53c4aSTejun Heo 		complete(&wq->first_flusher->done);
135073f53c4aSTejun Heo 
135173f53c4aSTejun Heo 	return wait;
135283c22520SOleg Nesterov }
13531da177e4SLinus Torvalds 
13540fcb78c2SRolf Eike Beer /**
13551da177e4SLinus Torvalds  * flush_workqueue - ensure that any scheduled work has run to completion.
13560fcb78c2SRolf Eike Beer  * @wq: workqueue to flush
13571da177e4SLinus Torvalds  *
13581da177e4SLinus Torvalds  * Forces execution of the workqueue and blocks until its completion.
13591da177e4SLinus Torvalds  * This is typically used in driver shutdown handlers.
13601da177e4SLinus Torvalds  *
1361fc2e4d70SOleg Nesterov  * We sleep until all works which were queued on entry have been handled,
1362fc2e4d70SOleg Nesterov  * but we are not livelocked by new incoming ones.
13631da177e4SLinus Torvalds  */
13647ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq)
13651da177e4SLinus Torvalds {
136673f53c4aSTejun Heo 	struct wq_flusher this_flusher = {
136773f53c4aSTejun Heo 		.list = LIST_HEAD_INIT(this_flusher.list),
136873f53c4aSTejun Heo 		.flush_color = -1,
136973f53c4aSTejun Heo 		.done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
137073f53c4aSTejun Heo 	};
137173f53c4aSTejun Heo 	int next_color;
1372b1f4ec17SOleg Nesterov 
13733295f0efSIngo Molnar 	lock_map_acquire(&wq->lockdep_map);
13743295f0efSIngo Molnar 	lock_map_release(&wq->lockdep_map);
137573f53c4aSTejun Heo 
137673f53c4aSTejun Heo 	mutex_lock(&wq->flush_mutex);
137773f53c4aSTejun Heo 
137873f53c4aSTejun Heo 	/*
137973f53c4aSTejun Heo 	 * Start-to-wait phase
138073f53c4aSTejun Heo 	 */
138173f53c4aSTejun Heo 	next_color = work_next_color(wq->work_color);
138273f53c4aSTejun Heo 
138373f53c4aSTejun Heo 	if (next_color != wq->flush_color) {
138473f53c4aSTejun Heo 		/*
138573f53c4aSTejun Heo 		 * Color space is not full.  The current work_color
138673f53c4aSTejun Heo 		 * becomes our flush_color and work_color is advanced
138773f53c4aSTejun Heo 		 * by one.
138873f53c4aSTejun Heo 		 */
138973f53c4aSTejun Heo 		BUG_ON(!list_empty(&wq->flusher_overflow));
139073f53c4aSTejun Heo 		this_flusher.flush_color = wq->work_color;
139173f53c4aSTejun Heo 		wq->work_color = next_color;
139273f53c4aSTejun Heo 
139373f53c4aSTejun Heo 		if (!wq->first_flusher) {
139473f53c4aSTejun Heo 			/* no flush in progress, become the first flusher */
139573f53c4aSTejun Heo 			BUG_ON(wq->flush_color != this_flusher.flush_color);
139673f53c4aSTejun Heo 
139773f53c4aSTejun Heo 			wq->first_flusher = &this_flusher;
139873f53c4aSTejun Heo 
139973f53c4aSTejun Heo 			if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
140073f53c4aSTejun Heo 						       wq->work_color)) {
140173f53c4aSTejun Heo 				/* nothing to flush, done */
140273f53c4aSTejun Heo 				wq->flush_color = next_color;
140373f53c4aSTejun Heo 				wq->first_flusher = NULL;
140473f53c4aSTejun Heo 				goto out_unlock;
140573f53c4aSTejun Heo 			}
140673f53c4aSTejun Heo 		} else {
140773f53c4aSTejun Heo 			/* wait in queue */
140873f53c4aSTejun Heo 			BUG_ON(wq->flush_color == this_flusher.flush_color);
140973f53c4aSTejun Heo 			list_add_tail(&this_flusher.list, &wq->flusher_queue);
141073f53c4aSTejun Heo 			flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
141173f53c4aSTejun Heo 		}
141273f53c4aSTejun Heo 	} else {
141373f53c4aSTejun Heo 		/*
141473f53c4aSTejun Heo 		 * Oops, color space is full, wait on overflow queue.
141573f53c4aSTejun Heo 		 * The next flush completion will assign us
141673f53c4aSTejun Heo 		 * flush_color and transfer to flusher_queue.
141773f53c4aSTejun Heo 		 */
141873f53c4aSTejun Heo 		list_add_tail(&this_flusher.list, &wq->flusher_overflow);
141973f53c4aSTejun Heo 	}
142073f53c4aSTejun Heo 
142173f53c4aSTejun Heo 	mutex_unlock(&wq->flush_mutex);
142273f53c4aSTejun Heo 
142373f53c4aSTejun Heo 	wait_for_completion(&this_flusher.done);
142473f53c4aSTejun Heo 
142573f53c4aSTejun Heo 	/*
142673f53c4aSTejun Heo 	 * Wake-up-and-cascade phase
142773f53c4aSTejun Heo 	 *
142873f53c4aSTejun Heo 	 * First flushers are responsible for cascading flushes and
142973f53c4aSTejun Heo 	 * handling overflow.  Non-first flushers can simply return.
143073f53c4aSTejun Heo 	 */
143173f53c4aSTejun Heo 	if (wq->first_flusher != &this_flusher)
143273f53c4aSTejun Heo 		return;
143373f53c4aSTejun Heo 
143473f53c4aSTejun Heo 	mutex_lock(&wq->flush_mutex);
143573f53c4aSTejun Heo 
143673f53c4aSTejun Heo 	wq->first_flusher = NULL;
143773f53c4aSTejun Heo 
143873f53c4aSTejun Heo 	BUG_ON(!list_empty(&this_flusher.list));
143973f53c4aSTejun Heo 	BUG_ON(wq->flush_color != this_flusher.flush_color);
144073f53c4aSTejun Heo 
144173f53c4aSTejun Heo 	while (true) {
144273f53c4aSTejun Heo 		struct wq_flusher *next, *tmp;
144373f53c4aSTejun Heo 
144473f53c4aSTejun Heo 		/* complete all the flushers sharing the current flush color */
144573f53c4aSTejun Heo 		list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
144673f53c4aSTejun Heo 			if (next->flush_color != wq->flush_color)
144773f53c4aSTejun Heo 				break;
144873f53c4aSTejun Heo 			list_del_init(&next->list);
144973f53c4aSTejun Heo 			complete(&next->done);
145073f53c4aSTejun Heo 		}
145173f53c4aSTejun Heo 
145273f53c4aSTejun Heo 		BUG_ON(!list_empty(&wq->flusher_overflow) &&
145373f53c4aSTejun Heo 		       wq->flush_color != work_next_color(wq->work_color));
145473f53c4aSTejun Heo 
145573f53c4aSTejun Heo 		/* this flush_color is finished, advance by one */
145673f53c4aSTejun Heo 		wq->flush_color = work_next_color(wq->flush_color);
145773f53c4aSTejun Heo 
145873f53c4aSTejun Heo 		/* one color has been freed, handle overflow queue */
145973f53c4aSTejun Heo 		if (!list_empty(&wq->flusher_overflow)) {
146073f53c4aSTejun Heo 			/*
146173f53c4aSTejun Heo 			 * Assign the same color to all overflowed
146273f53c4aSTejun Heo 			 * flushers, advance work_color and append to
146373f53c4aSTejun Heo 			 * flusher_queue.  This is the start-to-wait
146473f53c4aSTejun Heo 			 * phase for these overflowed flushers.
146573f53c4aSTejun Heo 			 */
146673f53c4aSTejun Heo 			list_for_each_entry(tmp, &wq->flusher_overflow, list)
146773f53c4aSTejun Heo 				tmp->flush_color = wq->work_color;
146873f53c4aSTejun Heo 
146973f53c4aSTejun Heo 			wq->work_color = work_next_color(wq->work_color);
147073f53c4aSTejun Heo 
147173f53c4aSTejun Heo 			list_splice_tail_init(&wq->flusher_overflow,
147273f53c4aSTejun Heo 					      &wq->flusher_queue);
147373f53c4aSTejun Heo 			flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
147473f53c4aSTejun Heo 		}
147573f53c4aSTejun Heo 
147673f53c4aSTejun Heo 		if (list_empty(&wq->flusher_queue)) {
147773f53c4aSTejun Heo 			BUG_ON(wq->flush_color != wq->work_color);
147873f53c4aSTejun Heo 			break;
147973f53c4aSTejun Heo 		}
148073f53c4aSTejun Heo 
148173f53c4aSTejun Heo 		/*
148273f53c4aSTejun Heo 		 * Need to flush more colors.  Make the next flusher
148373f53c4aSTejun Heo 		 * the new first flusher and arm cwqs.
148473f53c4aSTejun Heo 		 */
148573f53c4aSTejun Heo 		BUG_ON(wq->flush_color == wq->work_color);
148673f53c4aSTejun Heo 		BUG_ON(wq->flush_color != next->flush_color);
148773f53c4aSTejun Heo 
148873f53c4aSTejun Heo 		list_del_init(&next->list);
148973f53c4aSTejun Heo 		wq->first_flusher = next;
149073f53c4aSTejun Heo 
149173f53c4aSTejun Heo 		if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
149273f53c4aSTejun Heo 			break;
149373f53c4aSTejun Heo 
149473f53c4aSTejun Heo 		/*
149573f53c4aSTejun Heo 		 * Meh... this color is already done, clear first
149673f53c4aSTejun Heo 		 * flusher and repeat cascading.
149773f53c4aSTejun Heo 		 */
149873f53c4aSTejun Heo 		wq->first_flusher = NULL;
149973f53c4aSTejun Heo 	}
150073f53c4aSTejun Heo 
150173f53c4aSTejun Heo out_unlock:
150273f53c4aSTejun Heo 	mutex_unlock(&wq->flush_mutex);
15031da177e4SLinus Torvalds }
1504ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(flush_workqueue);
15051da177e4SLinus Torvalds 
1506db700897SOleg Nesterov /**
1507db700897SOleg Nesterov  * flush_work - block until a work_struct's callback has terminated
1508db700897SOleg Nesterov  * @work: the work which is to be flushed
1509db700897SOleg Nesterov  *
1510a67da70dSOleg Nesterov  * Returns false if @work has already terminated.
1511a67da70dSOleg Nesterov  *
1512db700897SOleg Nesterov  * It is expected that, prior to calling flush_work(), the caller has
1513db700897SOleg Nesterov  * arranged for the work to not be requeued, otherwise it doesn't make
1514db700897SOleg Nesterov  * sense to use this function.
1515db700897SOleg Nesterov  */
1516db700897SOleg Nesterov int flush_work(struct work_struct *work)
1517db700897SOleg Nesterov {
1518affee4b2STejun Heo 	struct worker *worker = NULL;
15198b03ae3cSTejun Heo 	struct global_cwq *gcwq;
15207a22ad75STejun Heo 	struct cpu_workqueue_struct *cwq;
1521db700897SOleg Nesterov 	struct wq_barrier barr;
1522db700897SOleg Nesterov 
1523db700897SOleg Nesterov 	might_sleep();
15247a22ad75STejun Heo 	gcwq = get_work_gcwq(work);
15257a22ad75STejun Heo 	if (!gcwq)
1526db700897SOleg Nesterov 		return 0;
1527a67da70dSOleg Nesterov 
15288b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
1529db700897SOleg Nesterov 	if (!list_empty(&work->entry)) {
1530db700897SOleg Nesterov 		/*
1531db700897SOleg Nesterov 		 * See the comment near try_to_grab_pending()->smp_rmb().
15327a22ad75STejun Heo 		 * If it was re-queued to a different gcwq under us, we
15337a22ad75STejun Heo 		 * are not going to wait.
1534db700897SOleg Nesterov 		 */
1535db700897SOleg Nesterov 		smp_rmb();
15367a22ad75STejun Heo 		cwq = get_work_cwq(work);
15377a22ad75STejun Heo 		if (unlikely(!cwq || gcwq != cwq->gcwq))
15384690c4abSTejun Heo 			goto already_gone;
1539db700897SOleg Nesterov 	} else {
15407a22ad75STejun Heo 		worker = find_worker_executing_work(gcwq, work);
1541affee4b2STejun Heo 		if (!worker)
15424690c4abSTejun Heo 			goto already_gone;
15437a22ad75STejun Heo 		cwq = worker->current_cwq;
1544db700897SOleg Nesterov 	}
1545db700897SOleg Nesterov 
1546affee4b2STejun Heo 	insert_wq_barrier(cwq, &barr, work, worker);
15478b03ae3cSTejun Heo 	spin_unlock_irq(&gcwq->lock);
15487a22ad75STejun Heo 
15497a22ad75STejun Heo 	lock_map_acquire(&cwq->wq->lockdep_map);
15507a22ad75STejun Heo 	lock_map_release(&cwq->wq->lockdep_map);
15517a22ad75STejun Heo 
1552db700897SOleg Nesterov 	wait_for_completion(&barr.done);
1553dc186ad7SThomas Gleixner 	destroy_work_on_stack(&barr.work);
1554db700897SOleg Nesterov 	return 1;
15554690c4abSTejun Heo already_gone:
15568b03ae3cSTejun Heo 	spin_unlock_irq(&gcwq->lock);
15574690c4abSTejun Heo 	return 0;
1558db700897SOleg Nesterov }
1559db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work);
1560db700897SOleg Nesterov 
15616e84d644SOleg Nesterov /*
15621f1f642eSOleg Nesterov  * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
15636e84d644SOleg Nesterov  * so this work can't be re-armed in any way.
15646e84d644SOleg Nesterov  */
15656e84d644SOleg Nesterov static int try_to_grab_pending(struct work_struct *work)
15666e84d644SOleg Nesterov {
15678b03ae3cSTejun Heo 	struct global_cwq *gcwq;
15681f1f642eSOleg Nesterov 	int ret = -1;
15696e84d644SOleg Nesterov 
157022df02bbSTejun Heo 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
15711f1f642eSOleg Nesterov 		return 0;
15726e84d644SOleg Nesterov 
15736e84d644SOleg Nesterov 	/*
15746e84d644SOleg Nesterov 	 * The queueing is in progress, or it is already queued. Try to
15756e84d644SOleg Nesterov 	 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
15766e84d644SOleg Nesterov 	 */
15777a22ad75STejun Heo 	gcwq = get_work_gcwq(work);
15787a22ad75STejun Heo 	if (!gcwq)
15796e84d644SOleg Nesterov 		return ret;
15806e84d644SOleg Nesterov 
15818b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
15826e84d644SOleg Nesterov 	if (!list_empty(&work->entry)) {
15836e84d644SOleg Nesterov 		/*
15847a22ad75STejun Heo 		 * This work is queued, but perhaps we locked the wrong gcwq.
15856e84d644SOleg Nesterov 		 * In that case we must see the new value after rmb(), see
15866e84d644SOleg Nesterov 		 * insert_work()->wmb().
15876e84d644SOleg Nesterov 		 */
15886e84d644SOleg Nesterov 		smp_rmb();
15897a22ad75STejun Heo 		if (gcwq == get_work_gcwq(work)) {
1590dc186ad7SThomas Gleixner 			debug_work_deactivate(work);
15916e84d644SOleg Nesterov 			list_del_init(&work->entry);
15927a22ad75STejun Heo 			cwq_dec_nr_in_flight(get_work_cwq(work),
15937a22ad75STejun Heo 					     get_work_color(work));
15946e84d644SOleg Nesterov 			ret = 1;
15956e84d644SOleg Nesterov 		}
15966e84d644SOleg Nesterov 	}
15978b03ae3cSTejun Heo 	spin_unlock_irq(&gcwq->lock);
15986e84d644SOleg Nesterov 
15996e84d644SOleg Nesterov 	return ret;
16006e84d644SOleg Nesterov }
16016e84d644SOleg Nesterov 
16027a22ad75STejun Heo static void wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
1603b89deed3SOleg Nesterov {
1604b89deed3SOleg Nesterov 	struct wq_barrier barr;
1605affee4b2STejun Heo 	struct worker *worker;
1606b89deed3SOleg Nesterov 
16078b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
1608affee4b2STejun Heo 
16097a22ad75STejun Heo 	worker = find_worker_executing_work(gcwq, work);
16107a22ad75STejun Heo 	if (unlikely(worker))
16117a22ad75STejun Heo 		insert_wq_barrier(worker->current_cwq, &barr, work, worker);
1612affee4b2STejun Heo 
16138b03ae3cSTejun Heo 	spin_unlock_irq(&gcwq->lock);
1614b89deed3SOleg Nesterov 
1615affee4b2STejun Heo 	if (unlikely(worker)) {
1616b89deed3SOleg Nesterov 		wait_for_completion(&barr.done);
1617dc186ad7SThomas Gleixner 		destroy_work_on_stack(&barr.work);
1618dc186ad7SThomas Gleixner 	}
1619b89deed3SOleg Nesterov }
1620b89deed3SOleg Nesterov 
16216e84d644SOleg Nesterov static void wait_on_work(struct work_struct *work)
1622b89deed3SOleg Nesterov {
1623b1f4ec17SOleg Nesterov 	int cpu;
1624b89deed3SOleg Nesterov 
1625f293ea92SOleg Nesterov 	might_sleep();
1626f293ea92SOleg Nesterov 
16273295f0efSIngo Molnar 	lock_map_acquire(&work->lockdep_map);
16283295f0efSIngo Molnar 	lock_map_release(&work->lockdep_map);
16294e6045f1SJohannes Berg 
16301537663fSTejun Heo 	for_each_possible_cpu(cpu)
16317a22ad75STejun Heo 		wait_on_cpu_work(get_gcwq(cpu), work);
16326e84d644SOleg Nesterov }
16336e84d644SOleg Nesterov 
16341f1f642eSOleg Nesterov static int __cancel_work_timer(struct work_struct *work,
16351f1f642eSOleg Nesterov 				struct timer_list* timer)
16361f1f642eSOleg Nesterov {
16371f1f642eSOleg Nesterov 	int ret;
16381f1f642eSOleg Nesterov 
16391f1f642eSOleg Nesterov 	do {
16401f1f642eSOleg Nesterov 		ret = (timer && likely(del_timer(timer)));
16411f1f642eSOleg Nesterov 		if (!ret)
16421f1f642eSOleg Nesterov 			ret = try_to_grab_pending(work);
16431f1f642eSOleg Nesterov 		wait_on_work(work);
16441f1f642eSOleg Nesterov 	} while (unlikely(ret < 0));
16451f1f642eSOleg Nesterov 
16467a22ad75STejun Heo 	clear_work_data(work);
16471f1f642eSOleg Nesterov 	return ret;
16481f1f642eSOleg Nesterov }
16491f1f642eSOleg Nesterov 
16506e84d644SOleg Nesterov /**
16516e84d644SOleg Nesterov  * cancel_work_sync - block until a work_struct's callback has terminated
16526e84d644SOleg Nesterov  * @work: the work which is to be flushed
16536e84d644SOleg Nesterov  *
16541f1f642eSOleg Nesterov  * Returns true if @work was pending.
16551f1f642eSOleg Nesterov  *
16566e84d644SOleg Nesterov  * cancel_work_sync() will cancel the work if it is queued. If the work's
16576e84d644SOleg Nesterov  * callback appears to be running, cancel_work_sync() will block until it
16586e84d644SOleg Nesterov  * has completed.
16596e84d644SOleg Nesterov  *
16606e84d644SOleg Nesterov  * It is possible to use this function if the work re-queues itself. It can
16616e84d644SOleg Nesterov  * cancel the work even if it migrates to another workqueue, however in that
16626e84d644SOleg Nesterov  * case it only guarantees that work->func() has completed on the last queued
16636e84d644SOleg Nesterov  * workqueue.
16646e84d644SOleg Nesterov  *
16656e84d644SOleg Nesterov  * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
16666e84d644SOleg Nesterov  * pending, otherwise it goes into a busy-wait loop until the timer expires.
16676e84d644SOleg Nesterov  *
16686e84d644SOleg Nesterov  * The caller must ensure that workqueue_struct on which this work was last
16696e84d644SOleg Nesterov  * queued can't be destroyed before this function returns.
16706e84d644SOleg Nesterov  */
16711f1f642eSOleg Nesterov int cancel_work_sync(struct work_struct *work)
16726e84d644SOleg Nesterov {
16731f1f642eSOleg Nesterov 	return __cancel_work_timer(work, NULL);
1674b89deed3SOleg Nesterov }
167528e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync);
1676b89deed3SOleg Nesterov 
16776e84d644SOleg Nesterov /**
1678f5a421a4SOleg Nesterov  * cancel_delayed_work_sync - reliably kill off a delayed work.
16796e84d644SOleg Nesterov  * @dwork: the delayed work struct
16806e84d644SOleg Nesterov  *
16811f1f642eSOleg Nesterov  * Returns true if @dwork was pending.
16821f1f642eSOleg Nesterov  *
16836e84d644SOleg Nesterov  * It is possible to use this function if @dwork rearms itself via queue_work()
16846e84d644SOleg Nesterov  * or queue_delayed_work(). See also the comment for cancel_work_sync().
16856e84d644SOleg Nesterov  */
16861f1f642eSOleg Nesterov int cancel_delayed_work_sync(struct delayed_work *dwork)
16876e84d644SOleg Nesterov {
16881f1f642eSOleg Nesterov 	return __cancel_work_timer(&dwork->work, &dwork->timer);
16896e84d644SOleg Nesterov }
1690f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync);
16911da177e4SLinus Torvalds 
16926e84d644SOleg Nesterov static struct workqueue_struct *keventd_wq __read_mostly;
16931da177e4SLinus Torvalds 
16940fcb78c2SRolf Eike Beer /**
16950fcb78c2SRolf Eike Beer  * schedule_work - put work task in global workqueue
16960fcb78c2SRolf Eike Beer  * @work: job to be done
16970fcb78c2SRolf Eike Beer  *
16985b0f437dSBart Van Assche  * Returns zero if @work was already on the kernel-global workqueue and
16995b0f437dSBart Van Assche  * non-zero otherwise.
17005b0f437dSBart Van Assche  *
17015b0f437dSBart Van Assche  * This puts a job in the kernel-global workqueue if it was not already
17025b0f437dSBart Van Assche  * queued and leaves it in the same position on the kernel-global
17035b0f437dSBart Van Assche  * workqueue otherwise.
17040fcb78c2SRolf Eike Beer  */
17057ad5b3a5SHarvey Harrison int schedule_work(struct work_struct *work)
17061da177e4SLinus Torvalds {
17071da177e4SLinus Torvalds 	return queue_work(keventd_wq, work);
17081da177e4SLinus Torvalds }
1709ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_work);
17101da177e4SLinus Torvalds 
1711c1a220e7SZhang Rui /*
1712c1a220e7SZhang Rui  * schedule_work_on - put work task on a specific cpu
1713c1a220e7SZhang Rui  * @cpu: cpu to put the work task on
1714c1a220e7SZhang Rui  * @work: job to be done
1715c1a220e7SZhang Rui  *
1716c1a220e7SZhang Rui  * This puts a job on a specific cpu
1717c1a220e7SZhang Rui  */
1718c1a220e7SZhang Rui int schedule_work_on(int cpu, struct work_struct *work)
1719c1a220e7SZhang Rui {
1720c1a220e7SZhang Rui 	return queue_work_on(cpu, keventd_wq, work);
1721c1a220e7SZhang Rui }
1722c1a220e7SZhang Rui EXPORT_SYMBOL(schedule_work_on);
1723c1a220e7SZhang Rui 
17240fcb78c2SRolf Eike Beer /**
17250fcb78c2SRolf Eike Beer  * schedule_delayed_work - put work task in global workqueue after delay
172652bad64dSDavid Howells  * @dwork: job to be done
172752bad64dSDavid Howells  * @delay: number of jiffies to wait or 0 for immediate execution
17280fcb78c2SRolf Eike Beer  *
17290fcb78c2SRolf Eike Beer  * After waiting for a given time this puts a job in the kernel-global
17300fcb78c2SRolf Eike Beer  * workqueue.
17310fcb78c2SRolf Eike Beer  */
17327ad5b3a5SHarvey Harrison int schedule_delayed_work(struct delayed_work *dwork,
173382f67cd9SIngo Molnar 					unsigned long delay)
17341da177e4SLinus Torvalds {
173552bad64dSDavid Howells 	return queue_delayed_work(keventd_wq, dwork, delay);
17361da177e4SLinus Torvalds }
1737ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work);
17381da177e4SLinus Torvalds 
17390fcb78c2SRolf Eike Beer /**
17408c53e463SLinus Torvalds  * flush_delayed_work - block until a dwork_struct's callback has terminated
17418c53e463SLinus Torvalds  * @dwork: the delayed work which is to be flushed
17428c53e463SLinus Torvalds  *
17438c53e463SLinus Torvalds  * Any timeout is cancelled, and any pending work is run immediately.
17448c53e463SLinus Torvalds  */
17458c53e463SLinus Torvalds void flush_delayed_work(struct delayed_work *dwork)
17468c53e463SLinus Torvalds {
17478c53e463SLinus Torvalds 	if (del_timer_sync(&dwork->timer)) {
17487a22ad75STejun Heo 		__queue_work(get_cpu(), get_work_cwq(&dwork->work)->wq,
17494690c4abSTejun Heo 			     &dwork->work);
17508c53e463SLinus Torvalds 		put_cpu();
17518c53e463SLinus Torvalds 	}
17528c53e463SLinus Torvalds 	flush_work(&dwork->work);
17538c53e463SLinus Torvalds }
17548c53e463SLinus Torvalds EXPORT_SYMBOL(flush_delayed_work);
17558c53e463SLinus Torvalds 
17568c53e463SLinus Torvalds /**
17570fcb78c2SRolf Eike Beer  * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
17580fcb78c2SRolf Eike Beer  * @cpu: cpu to use
175952bad64dSDavid Howells  * @dwork: job to be done
17600fcb78c2SRolf Eike Beer  * @delay: number of jiffies to wait
17610fcb78c2SRolf Eike Beer  *
17620fcb78c2SRolf Eike Beer  * After waiting for a given time this puts a job in the kernel-global
17630fcb78c2SRolf Eike Beer  * workqueue on the specified CPU.
17640fcb78c2SRolf Eike Beer  */
17651da177e4SLinus Torvalds int schedule_delayed_work_on(int cpu,
176652bad64dSDavid Howells 			struct delayed_work *dwork, unsigned long delay)
17671da177e4SLinus Torvalds {
176852bad64dSDavid Howells 	return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
17691da177e4SLinus Torvalds }
1770ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work_on);
17711da177e4SLinus Torvalds 
1772b6136773SAndrew Morton /**
1773b6136773SAndrew Morton  * schedule_on_each_cpu - call a function on each online CPU from keventd
1774b6136773SAndrew Morton  * @func: the function to call
1775b6136773SAndrew Morton  *
1776b6136773SAndrew Morton  * Returns zero on success.
1777b6136773SAndrew Morton  * Returns -ve errno on failure.
1778b6136773SAndrew Morton  *
1779b6136773SAndrew Morton  * schedule_on_each_cpu() is very slow.
1780b6136773SAndrew Morton  */
178165f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func)
178215316ba8SChristoph Lameter {
178315316ba8SChristoph Lameter 	int cpu;
178465a64464SAndi Kleen 	int orig = -1;
1785b6136773SAndrew Morton 	struct work_struct *works;
178615316ba8SChristoph Lameter 
1787b6136773SAndrew Morton 	works = alloc_percpu(struct work_struct);
1788b6136773SAndrew Morton 	if (!works)
178915316ba8SChristoph Lameter 		return -ENOMEM;
1790b6136773SAndrew Morton 
179195402b38SGautham R Shenoy 	get_online_cpus();
179293981800STejun Heo 
179393981800STejun Heo 	/*
179493981800STejun Heo 	 * When running in keventd don't schedule a work item on
179593981800STejun Heo 	 * itself.  Can just call directly because the work queue is
179693981800STejun Heo 	 * already bound.  This also is faster.
179793981800STejun Heo 	 */
179893981800STejun Heo 	if (current_is_keventd())
179993981800STejun Heo 		orig = raw_smp_processor_id();
180093981800STejun Heo 
180115316ba8SChristoph Lameter 	for_each_online_cpu(cpu) {
18029bfb1839SIngo Molnar 		struct work_struct *work = per_cpu_ptr(works, cpu);
18039bfb1839SIngo Molnar 
18049bfb1839SIngo Molnar 		INIT_WORK(work, func);
180593981800STejun Heo 		if (cpu != orig)
18068de6d308SOleg Nesterov 			schedule_work_on(cpu, work);
180715316ba8SChristoph Lameter 	}
180893981800STejun Heo 	if (orig >= 0)
180993981800STejun Heo 		func(per_cpu_ptr(works, orig));
181093981800STejun Heo 
181193981800STejun Heo 	for_each_online_cpu(cpu)
18128616a89aSOleg Nesterov 		flush_work(per_cpu_ptr(works, cpu));
181393981800STejun Heo 
181495402b38SGautham R Shenoy 	put_online_cpus();
1815b6136773SAndrew Morton 	free_percpu(works);
181615316ba8SChristoph Lameter 	return 0;
181715316ba8SChristoph Lameter }
181815316ba8SChristoph Lameter 
1819eef6a7d5SAlan Stern /**
1820eef6a7d5SAlan Stern  * flush_scheduled_work - ensure that any scheduled work has run to completion.
1821eef6a7d5SAlan Stern  *
1822eef6a7d5SAlan Stern  * Forces execution of the kernel-global workqueue and blocks until its
1823eef6a7d5SAlan Stern  * completion.
1824eef6a7d5SAlan Stern  *
1825eef6a7d5SAlan Stern  * Think twice before calling this function!  It's very easy to get into
1826eef6a7d5SAlan Stern  * trouble if you don't take great care.  Either of the following situations
1827eef6a7d5SAlan Stern  * will lead to deadlock:
1828eef6a7d5SAlan Stern  *
1829eef6a7d5SAlan Stern  *	One of the work items currently on the workqueue needs to acquire
1830eef6a7d5SAlan Stern  *	a lock held by your code or its caller.
1831eef6a7d5SAlan Stern  *
1832eef6a7d5SAlan Stern  *	Your code is running in the context of a work routine.
1833eef6a7d5SAlan Stern  *
1834eef6a7d5SAlan Stern  * They will be detected by lockdep when they occur, but the first might not
1835eef6a7d5SAlan Stern  * occur very often.  It depends on what work items are on the workqueue and
1836eef6a7d5SAlan Stern  * what locks they need, which you have no control over.
1837eef6a7d5SAlan Stern  *
1838eef6a7d5SAlan Stern  * In most situations flushing the entire workqueue is overkill; you merely
1839eef6a7d5SAlan Stern  * need to know that a particular work item isn't queued and isn't running.
1840eef6a7d5SAlan Stern  * In such cases you should use cancel_delayed_work_sync() or
1841eef6a7d5SAlan Stern  * cancel_work_sync() instead.
1842eef6a7d5SAlan Stern  */
18431da177e4SLinus Torvalds void flush_scheduled_work(void)
18441da177e4SLinus Torvalds {
18451da177e4SLinus Torvalds 	flush_workqueue(keventd_wq);
18461da177e4SLinus Torvalds }
1847ae90dd5dSDave Jones EXPORT_SYMBOL(flush_scheduled_work);
18481da177e4SLinus Torvalds 
18491da177e4SLinus Torvalds /**
18501fa44ecaSJames Bottomley  * execute_in_process_context - reliably execute the routine with user context
18511fa44ecaSJames Bottomley  * @fn:		the function to execute
18521fa44ecaSJames Bottomley  * @ew:		guaranteed storage for the execute work structure (must
18531fa44ecaSJames Bottomley  *		be available when the work executes)
18541fa44ecaSJames Bottomley  *
18551fa44ecaSJames Bottomley  * Executes the function immediately if process context is available,
18561fa44ecaSJames Bottomley  * otherwise schedules the function for delayed execution.
18571fa44ecaSJames Bottomley  *
18581fa44ecaSJames Bottomley  * Returns:	0 - function was executed
18591fa44ecaSJames Bottomley  *		1 - function was scheduled for execution
18601fa44ecaSJames Bottomley  */
186165f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew)
18621fa44ecaSJames Bottomley {
18631fa44ecaSJames Bottomley 	if (!in_interrupt()) {
186465f27f38SDavid Howells 		fn(&ew->work);
18651fa44ecaSJames Bottomley 		return 0;
18661fa44ecaSJames Bottomley 	}
18671fa44ecaSJames Bottomley 
186865f27f38SDavid Howells 	INIT_WORK(&ew->work, fn);
18691fa44ecaSJames Bottomley 	schedule_work(&ew->work);
18701fa44ecaSJames Bottomley 
18711fa44ecaSJames Bottomley 	return 1;
18721fa44ecaSJames Bottomley }
18731fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context);
18741fa44ecaSJames Bottomley 
18751da177e4SLinus Torvalds int keventd_up(void)
18761da177e4SLinus Torvalds {
18771da177e4SLinus Torvalds 	return keventd_wq != NULL;
18781da177e4SLinus Torvalds }
18791da177e4SLinus Torvalds 
18801da177e4SLinus Torvalds int current_is_keventd(void)
18811da177e4SLinus Torvalds {
1882*7e11629dSTejun Heo 	bool found = false;
1883*7e11629dSTejun Heo 	unsigned int cpu;
18841da177e4SLinus Torvalds 
1885*7e11629dSTejun Heo 	/*
1886*7e11629dSTejun Heo 	 * There no longer is one-to-one relation between worker and
1887*7e11629dSTejun Heo 	 * work queue and a worker task might be unbound from its cpu
1888*7e11629dSTejun Heo 	 * if the cpu was offlined.  Match all busy workers.  This
1889*7e11629dSTejun Heo 	 * function will go away once dynamic pool is implemented.
1890*7e11629dSTejun Heo 	 */
1891*7e11629dSTejun Heo 	for_each_possible_cpu(cpu) {
1892*7e11629dSTejun Heo 		struct global_cwq *gcwq = get_gcwq(cpu);
1893*7e11629dSTejun Heo 		struct worker *worker;
1894*7e11629dSTejun Heo 		struct hlist_node *pos;
1895*7e11629dSTejun Heo 		unsigned long flags;
1896*7e11629dSTejun Heo 		int i;
18971da177e4SLinus Torvalds 
1898*7e11629dSTejun Heo 		spin_lock_irqsave(&gcwq->lock, flags);
18991da177e4SLinus Torvalds 
1900*7e11629dSTejun Heo 		for_each_busy_worker(worker, i, pos, gcwq) {
1901*7e11629dSTejun Heo 			if (worker->task == current) {
1902*7e11629dSTejun Heo 				found = true;
1903*7e11629dSTejun Heo 				break;
1904*7e11629dSTejun Heo 			}
1905*7e11629dSTejun Heo 		}
19061da177e4SLinus Torvalds 
1907*7e11629dSTejun Heo 		spin_unlock_irqrestore(&gcwq->lock, flags);
1908*7e11629dSTejun Heo 		if (found)
1909*7e11629dSTejun Heo 			break;
1910*7e11629dSTejun Heo 	}
1911*7e11629dSTejun Heo 
1912*7e11629dSTejun Heo 	return found;
19131da177e4SLinus Torvalds }
19141da177e4SLinus Torvalds 
19150f900049STejun Heo static struct cpu_workqueue_struct *alloc_cwqs(void)
19160f900049STejun Heo {
19170f900049STejun Heo 	/*
19180f900049STejun Heo 	 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
19190f900049STejun Heo 	 * Make sure that the alignment isn't lower than that of
19200f900049STejun Heo 	 * unsigned long long.
19210f900049STejun Heo 	 */
19220f900049STejun Heo 	const size_t size = sizeof(struct cpu_workqueue_struct);
19230f900049STejun Heo 	const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
19240f900049STejun Heo 				   __alignof__(unsigned long long));
19250f900049STejun Heo 	struct cpu_workqueue_struct *cwqs;
19260f900049STejun Heo #ifndef CONFIG_SMP
19270f900049STejun Heo 	void *ptr;
19280f900049STejun Heo 
19290f900049STejun Heo 	/*
19300f900049STejun Heo 	 * On UP, percpu allocator doesn't honor alignment parameter
19310f900049STejun Heo 	 * and simply uses arch-dependent default.  Allocate enough
19320f900049STejun Heo 	 * room to align cwq and put an extra pointer at the end
19330f900049STejun Heo 	 * pointing back to the originally allocated pointer which
19340f900049STejun Heo 	 * will be used for free.
19350f900049STejun Heo 	 *
19360f900049STejun Heo 	 * FIXME: This really belongs to UP percpu code.  Update UP
19370f900049STejun Heo 	 * percpu code to honor alignment and remove this ugliness.
19380f900049STejun Heo 	 */
19390f900049STejun Heo 	ptr = __alloc_percpu(size + align + sizeof(void *), 1);
19400f900049STejun Heo 	cwqs = PTR_ALIGN(ptr, align);
19410f900049STejun Heo 	*(void **)per_cpu_ptr(cwqs + 1, 0) = ptr;
19420f900049STejun Heo #else
19430f900049STejun Heo 	/* On SMP, percpu allocator can do it itself */
19440f900049STejun Heo 	cwqs = __alloc_percpu(size, align);
19450f900049STejun Heo #endif
19460f900049STejun Heo 	/* just in case, make sure it's actually aligned */
19470f900049STejun Heo 	BUG_ON(!IS_ALIGNED((unsigned long)cwqs, align));
19480f900049STejun Heo 	return cwqs;
19490f900049STejun Heo }
19500f900049STejun Heo 
19510f900049STejun Heo static void free_cwqs(struct cpu_workqueue_struct *cwqs)
19520f900049STejun Heo {
19530f900049STejun Heo #ifndef CONFIG_SMP
19540f900049STejun Heo 	/* on UP, the pointer to free is stored right after the cwq */
19550f900049STejun Heo 	if (cwqs)
19560f900049STejun Heo 		free_percpu(*(void **)per_cpu_ptr(cwqs + 1, 0));
19570f900049STejun Heo #else
19580f900049STejun Heo 	free_percpu(cwqs);
19590f900049STejun Heo #endif
19600f900049STejun Heo }
19610f900049STejun Heo 
19624e6045f1SJohannes Berg struct workqueue_struct *__create_workqueue_key(const char *name,
196397e37d7bSTejun Heo 						unsigned int flags,
19641e19ffc6STejun Heo 						int max_active,
1965eb13ba87SJohannes Berg 						struct lock_class_key *key,
1966eb13ba87SJohannes Berg 						const char *lock_name)
19673af24433SOleg Nesterov {
19683af24433SOleg Nesterov 	struct workqueue_struct *wq;
1969c34056a3STejun Heo 	bool failed = false;
1970c34056a3STejun Heo 	unsigned int cpu;
19713af24433SOleg Nesterov 
19721e19ffc6STejun Heo 	max_active = clamp_val(max_active, 1, INT_MAX);
19731e19ffc6STejun Heo 
19743af24433SOleg Nesterov 	wq = kzalloc(sizeof(*wq), GFP_KERNEL);
19753af24433SOleg Nesterov 	if (!wq)
19764690c4abSTejun Heo 		goto err;
19773af24433SOleg Nesterov 
19780f900049STejun Heo 	wq->cpu_wq = alloc_cwqs();
19794690c4abSTejun Heo 	if (!wq->cpu_wq)
19804690c4abSTejun Heo 		goto err;
19813af24433SOleg Nesterov 
198297e37d7bSTejun Heo 	wq->flags = flags;
1983a0a1a5fdSTejun Heo 	wq->saved_max_active = max_active;
198473f53c4aSTejun Heo 	mutex_init(&wq->flush_mutex);
198573f53c4aSTejun Heo 	atomic_set(&wq->nr_cwqs_to_flush, 0);
198673f53c4aSTejun Heo 	INIT_LIST_HEAD(&wq->flusher_queue);
198773f53c4aSTejun Heo 	INIT_LIST_HEAD(&wq->flusher_overflow);
1988502ca9d8STejun Heo 	wq->single_cpu = NR_CPUS;
1989502ca9d8STejun Heo 
19903af24433SOleg Nesterov 	wq->name = name;
1991eb13ba87SJohannes Berg 	lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
1992cce1a165SOleg Nesterov 	INIT_LIST_HEAD(&wq->list);
19933af24433SOleg Nesterov 
19943da1c84cSOleg Nesterov 	cpu_maps_update_begin();
19956af8bf3dSOleg Nesterov 	/*
19966af8bf3dSOleg Nesterov 	 * We must initialize cwqs for each possible cpu even if we
19976af8bf3dSOleg Nesterov 	 * are going to call destroy_workqueue() finally. Otherwise
19986af8bf3dSOleg Nesterov 	 * cpu_up() can hit the uninitialized cwq once we drop the
19996af8bf3dSOleg Nesterov 	 * lock.
20006af8bf3dSOleg Nesterov 	 */
20013af24433SOleg Nesterov 	for_each_possible_cpu(cpu) {
20021537663fSTejun Heo 		struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
20038b03ae3cSTejun Heo 		struct global_cwq *gcwq = get_gcwq(cpu);
20041537663fSTejun Heo 
20050f900049STejun Heo 		BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
20068b03ae3cSTejun Heo 		cwq->gcwq = gcwq;
2007c34056a3STejun Heo 		cwq->wq = wq;
200873f53c4aSTejun Heo 		cwq->flush_color = -1;
20091e19ffc6STejun Heo 		cwq->max_active = max_active;
20101e19ffc6STejun Heo 		INIT_LIST_HEAD(&cwq->delayed_works);
20111537663fSTejun Heo 
2012c34056a3STejun Heo 		if (failed)
20133af24433SOleg Nesterov 			continue;
2014*7e11629dSTejun Heo 		cwq->worker = create_worker(gcwq, cpu_online(cpu));
2015c34056a3STejun Heo 		if (cwq->worker)
2016c34056a3STejun Heo 			start_worker(cwq->worker);
20171537663fSTejun Heo 		else
2018c34056a3STejun Heo 			failed = true;
20193af24433SOleg Nesterov 	}
20201537663fSTejun Heo 
2021a0a1a5fdSTejun Heo 	/*
2022a0a1a5fdSTejun Heo 	 * workqueue_lock protects global freeze state and workqueues
2023a0a1a5fdSTejun Heo 	 * list.  Grab it, set max_active accordingly and add the new
2024a0a1a5fdSTejun Heo 	 * workqueue to workqueues list.
2025a0a1a5fdSTejun Heo 	 */
20261537663fSTejun Heo 	spin_lock(&workqueue_lock);
2027a0a1a5fdSTejun Heo 
2028a0a1a5fdSTejun Heo 	if (workqueue_freezing && wq->flags & WQ_FREEZEABLE)
2029a0a1a5fdSTejun Heo 		for_each_possible_cpu(cpu)
2030a0a1a5fdSTejun Heo 			get_cwq(cpu, wq)->max_active = 0;
2031a0a1a5fdSTejun Heo 
20321537663fSTejun Heo 	list_add(&wq->list, &workqueues);
2033a0a1a5fdSTejun Heo 
20341537663fSTejun Heo 	spin_unlock(&workqueue_lock);
20351537663fSTejun Heo 
20363da1c84cSOleg Nesterov 	cpu_maps_update_done();
20373af24433SOleg Nesterov 
2038c34056a3STejun Heo 	if (failed) {
20393af24433SOleg Nesterov 		destroy_workqueue(wq);
20403af24433SOleg Nesterov 		wq = NULL;
20413af24433SOleg Nesterov 	}
20423af24433SOleg Nesterov 	return wq;
20434690c4abSTejun Heo err:
20444690c4abSTejun Heo 	if (wq) {
20450f900049STejun Heo 		free_cwqs(wq->cpu_wq);
20464690c4abSTejun Heo 		kfree(wq);
20474690c4abSTejun Heo 	}
20484690c4abSTejun Heo 	return NULL;
20493af24433SOleg Nesterov }
20504e6045f1SJohannes Berg EXPORT_SYMBOL_GPL(__create_workqueue_key);
20513af24433SOleg Nesterov 
20523af24433SOleg Nesterov /**
20533af24433SOleg Nesterov  * destroy_workqueue - safely terminate a workqueue
20543af24433SOleg Nesterov  * @wq: target workqueue
20553af24433SOleg Nesterov  *
20563af24433SOleg Nesterov  * Safely destroy a workqueue. All work currently pending will be done first.
20573af24433SOleg Nesterov  */
20583af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq)
20593af24433SOleg Nesterov {
2060c8e55f36STejun Heo 	unsigned int cpu;
20613af24433SOleg Nesterov 
2062a0a1a5fdSTejun Heo 	flush_workqueue(wq);
2063a0a1a5fdSTejun Heo 
2064a0a1a5fdSTejun Heo 	/*
2065a0a1a5fdSTejun Heo 	 * wq list is used to freeze wq, remove from list after
2066a0a1a5fdSTejun Heo 	 * flushing is complete in case freeze races us.
2067a0a1a5fdSTejun Heo 	 */
20683da1c84cSOleg Nesterov 	cpu_maps_update_begin();
206995402b38SGautham R Shenoy 	spin_lock(&workqueue_lock);
20703af24433SOleg Nesterov 	list_del(&wq->list);
207195402b38SGautham R Shenoy 	spin_unlock(&workqueue_lock);
20723da1c84cSOleg Nesterov 	cpu_maps_update_done();
20733af24433SOleg Nesterov 
207473f53c4aSTejun Heo 	for_each_possible_cpu(cpu) {
207573f53c4aSTejun Heo 		struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
2076*7e11629dSTejun Heo 		struct global_cwq *gcwq = cwq->gcwq;
207773f53c4aSTejun Heo 		int i;
207873f53c4aSTejun Heo 
2079c34056a3STejun Heo 		if (cwq->worker) {
2080*7e11629dSTejun Heo 		retry:
2081*7e11629dSTejun Heo 			spin_lock_irq(&gcwq->lock);
2082*7e11629dSTejun Heo 			/*
2083*7e11629dSTejun Heo 			 * Worker can only be destroyed while idle.
2084*7e11629dSTejun Heo 			 * Wait till it becomes idle.  This is ugly
2085*7e11629dSTejun Heo 			 * and prone to starvation.  It will go away
2086*7e11629dSTejun Heo 			 * once dynamic worker pool is implemented.
2087*7e11629dSTejun Heo 			 */
2088*7e11629dSTejun Heo 			if (!(cwq->worker->flags & WORKER_IDLE)) {
2089*7e11629dSTejun Heo 				spin_unlock_irq(&gcwq->lock);
2090*7e11629dSTejun Heo 				msleep(100);
2091*7e11629dSTejun Heo 				goto retry;
2092*7e11629dSTejun Heo 			}
2093c34056a3STejun Heo 			destroy_worker(cwq->worker);
2094c34056a3STejun Heo 			cwq->worker = NULL;
2095*7e11629dSTejun Heo 			spin_unlock_irq(&gcwq->lock);
209673f53c4aSTejun Heo 		}
209773f53c4aSTejun Heo 
209873f53c4aSTejun Heo 		for (i = 0; i < WORK_NR_COLORS; i++)
209973f53c4aSTejun Heo 			BUG_ON(cwq->nr_in_flight[i]);
21001e19ffc6STejun Heo 		BUG_ON(cwq->nr_active);
21011e19ffc6STejun Heo 		BUG_ON(!list_empty(&cwq->delayed_works));
210273f53c4aSTejun Heo 	}
21031537663fSTejun Heo 
21040f900049STejun Heo 	free_cwqs(wq->cpu_wq);
21053af24433SOleg Nesterov 	kfree(wq);
21063af24433SOleg Nesterov }
21073af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue);
21083af24433SOleg Nesterov 
2109db7bccf4STejun Heo /*
2110db7bccf4STejun Heo  * CPU hotplug.
2111db7bccf4STejun Heo  *
2112db7bccf4STejun Heo  * CPU hotplug is implemented by allowing cwqs to be detached from
2113db7bccf4STejun Heo  * CPU, running with unbound workers and allowing them to be
2114db7bccf4STejun Heo  * reattached later if the cpu comes back online.  A separate thread
2115db7bccf4STejun Heo  * is created to govern cwqs in such state and is called the trustee.
2116db7bccf4STejun Heo  *
2117db7bccf4STejun Heo  * Trustee states and their descriptions.
2118db7bccf4STejun Heo  *
2119db7bccf4STejun Heo  * START	Command state used on startup.  On CPU_DOWN_PREPARE, a
2120db7bccf4STejun Heo  *		new trustee is started with this state.
2121db7bccf4STejun Heo  *
2122db7bccf4STejun Heo  * IN_CHARGE	Once started, trustee will enter this state after
2123db7bccf4STejun Heo  *		making all existing workers rogue.  DOWN_PREPARE waits
2124db7bccf4STejun Heo  *		for trustee to enter this state.  After reaching
2125db7bccf4STejun Heo  *		IN_CHARGE, trustee tries to execute the pending
2126db7bccf4STejun Heo  *		worklist until it's empty and the state is set to
2127db7bccf4STejun Heo  *		BUTCHER, or the state is set to RELEASE.
2128db7bccf4STejun Heo  *
2129db7bccf4STejun Heo  * BUTCHER	Command state which is set by the cpu callback after
2130db7bccf4STejun Heo  *		the cpu has went down.  Once this state is set trustee
2131db7bccf4STejun Heo  *		knows that there will be no new works on the worklist
2132db7bccf4STejun Heo  *		and once the worklist is empty it can proceed to
2133db7bccf4STejun Heo  *		killing idle workers.
2134db7bccf4STejun Heo  *
2135db7bccf4STejun Heo  * RELEASE	Command state which is set by the cpu callback if the
2136db7bccf4STejun Heo  *		cpu down has been canceled or it has come online
2137db7bccf4STejun Heo  *		again.  After recognizing this state, trustee stops
2138db7bccf4STejun Heo  *		trying to drain or butcher and transits to DONE.
2139db7bccf4STejun Heo  *
2140db7bccf4STejun Heo  * DONE		Trustee will enter this state after BUTCHER or RELEASE
2141db7bccf4STejun Heo  *		is complete.
2142db7bccf4STejun Heo  *
2143db7bccf4STejun Heo  *          trustee                 CPU                draining
2144db7bccf4STejun Heo  *         took over                down               complete
2145db7bccf4STejun Heo  * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
2146db7bccf4STejun Heo  *                        |                     |                  ^
2147db7bccf4STejun Heo  *                        | CPU is back online  v   return workers |
2148db7bccf4STejun Heo  *                         ----------------> RELEASE --------------
2149db7bccf4STejun Heo  */
2150db7bccf4STejun Heo 
2151db7bccf4STejun Heo /**
2152db7bccf4STejun Heo  * trustee_wait_event_timeout - timed event wait for trustee
2153db7bccf4STejun Heo  * @cond: condition to wait for
2154db7bccf4STejun Heo  * @timeout: timeout in jiffies
2155db7bccf4STejun Heo  *
2156db7bccf4STejun Heo  * wait_event_timeout() for trustee to use.  Handles locking and
2157db7bccf4STejun Heo  * checks for RELEASE request.
2158db7bccf4STejun Heo  *
2159db7bccf4STejun Heo  * CONTEXT:
2160db7bccf4STejun Heo  * spin_lock_irq(gcwq->lock) which may be released and regrabbed
2161db7bccf4STejun Heo  * multiple times.  To be used by trustee.
2162db7bccf4STejun Heo  *
2163db7bccf4STejun Heo  * RETURNS:
2164db7bccf4STejun Heo  * Positive indicating left time if @cond is satisfied, 0 if timed
2165db7bccf4STejun Heo  * out, -1 if canceled.
2166db7bccf4STejun Heo  */
2167db7bccf4STejun Heo #define trustee_wait_event_timeout(cond, timeout) ({			\
2168db7bccf4STejun Heo 	long __ret = (timeout);						\
2169db7bccf4STejun Heo 	while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) &&	\
2170db7bccf4STejun Heo 	       __ret) {							\
2171db7bccf4STejun Heo 		spin_unlock_irq(&gcwq->lock);				\
2172db7bccf4STejun Heo 		__wait_event_timeout(gcwq->trustee_wait, (cond) ||	\
2173db7bccf4STejun Heo 			(gcwq->trustee_state == TRUSTEE_RELEASE),	\
2174db7bccf4STejun Heo 			__ret);						\
2175db7bccf4STejun Heo 		spin_lock_irq(&gcwq->lock);				\
2176db7bccf4STejun Heo 	}								\
2177db7bccf4STejun Heo 	gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret);		\
2178db7bccf4STejun Heo })
2179db7bccf4STejun Heo 
2180db7bccf4STejun Heo /**
2181db7bccf4STejun Heo  * trustee_wait_event - event wait for trustee
2182db7bccf4STejun Heo  * @cond: condition to wait for
2183db7bccf4STejun Heo  *
2184db7bccf4STejun Heo  * wait_event() for trustee to use.  Automatically handles locking and
2185db7bccf4STejun Heo  * checks for CANCEL request.
2186db7bccf4STejun Heo  *
2187db7bccf4STejun Heo  * CONTEXT:
2188db7bccf4STejun Heo  * spin_lock_irq(gcwq->lock) which may be released and regrabbed
2189db7bccf4STejun Heo  * multiple times.  To be used by trustee.
2190db7bccf4STejun Heo  *
2191db7bccf4STejun Heo  * RETURNS:
2192db7bccf4STejun Heo  * 0 if @cond is satisfied, -1 if canceled.
2193db7bccf4STejun Heo  */
2194db7bccf4STejun Heo #define trustee_wait_event(cond) ({					\
2195db7bccf4STejun Heo 	long __ret1;							\
2196db7bccf4STejun Heo 	__ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
2197db7bccf4STejun Heo 	__ret1 < 0 ? -1 : 0;						\
2198db7bccf4STejun Heo })
2199db7bccf4STejun Heo 
2200db7bccf4STejun Heo static int __cpuinit trustee_thread(void *__gcwq)
2201db7bccf4STejun Heo {
2202db7bccf4STejun Heo 	struct global_cwq *gcwq = __gcwq;
2203db7bccf4STejun Heo 	struct worker *worker;
2204db7bccf4STejun Heo 	struct hlist_node *pos;
2205db7bccf4STejun Heo 	int i;
2206db7bccf4STejun Heo 
2207db7bccf4STejun Heo 	BUG_ON(gcwq->cpu != smp_processor_id());
2208db7bccf4STejun Heo 
2209db7bccf4STejun Heo 	spin_lock_irq(&gcwq->lock);
2210db7bccf4STejun Heo 	/*
2211502ca9d8STejun Heo 	 * Make all workers rogue.  Trustee must be bound to the
2212502ca9d8STejun Heo 	 * target cpu and can't be cancelled.
2213db7bccf4STejun Heo 	 */
2214db7bccf4STejun Heo 	BUG_ON(gcwq->cpu != smp_processor_id());
2215db7bccf4STejun Heo 
2216db7bccf4STejun Heo 	list_for_each_entry(worker, &gcwq->idle_list, entry)
2217db7bccf4STejun Heo 		worker->flags |= WORKER_ROGUE;
2218db7bccf4STejun Heo 
2219db7bccf4STejun Heo 	for_each_busy_worker(worker, i, pos, gcwq)
2220db7bccf4STejun Heo 		worker->flags |= WORKER_ROGUE;
2221db7bccf4STejun Heo 
2222db7bccf4STejun Heo 	/*
2223db7bccf4STejun Heo 	 * We're now in charge.  Notify and proceed to drain.  We need
2224db7bccf4STejun Heo 	 * to keep the gcwq running during the whole CPU down
2225db7bccf4STejun Heo 	 * procedure as other cpu hotunplug callbacks may need to
2226db7bccf4STejun Heo 	 * flush currently running tasks.
2227db7bccf4STejun Heo 	 */
2228db7bccf4STejun Heo 	gcwq->trustee_state = TRUSTEE_IN_CHARGE;
2229db7bccf4STejun Heo 	wake_up_all(&gcwq->trustee_wait);
2230db7bccf4STejun Heo 
2231db7bccf4STejun Heo 	/*
2232db7bccf4STejun Heo 	 * The original cpu is in the process of dying and may go away
2233db7bccf4STejun Heo 	 * anytime now.  When that happens, we and all workers would
2234db7bccf4STejun Heo 	 * be migrated to other cpus.  Try draining any left work.
2235db7bccf4STejun Heo 	 * Note that if the gcwq is frozen, there may be frozen works
2236db7bccf4STejun Heo 	 * in freezeable cwqs.  Don't declare completion while frozen.
2237db7bccf4STejun Heo 	 */
2238db7bccf4STejun Heo 	while (gcwq->nr_workers != gcwq->nr_idle ||
2239db7bccf4STejun Heo 	       gcwq->flags & GCWQ_FREEZING ||
2240db7bccf4STejun Heo 	       gcwq->trustee_state == TRUSTEE_IN_CHARGE) {
2241db7bccf4STejun Heo 		/* give a breather */
2242db7bccf4STejun Heo 		if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
2243db7bccf4STejun Heo 			break;
2244db7bccf4STejun Heo 	}
2245db7bccf4STejun Heo 
2246db7bccf4STejun Heo 	/* notify completion */
2247db7bccf4STejun Heo 	gcwq->trustee = NULL;
2248db7bccf4STejun Heo 	gcwq->trustee_state = TRUSTEE_DONE;
2249db7bccf4STejun Heo 	wake_up_all(&gcwq->trustee_wait);
2250db7bccf4STejun Heo 	spin_unlock_irq(&gcwq->lock);
2251db7bccf4STejun Heo 	return 0;
2252db7bccf4STejun Heo }
2253db7bccf4STejun Heo 
2254db7bccf4STejun Heo /**
2255db7bccf4STejun Heo  * wait_trustee_state - wait for trustee to enter the specified state
2256db7bccf4STejun Heo  * @gcwq: gcwq the trustee of interest belongs to
2257db7bccf4STejun Heo  * @state: target state to wait for
2258db7bccf4STejun Heo  *
2259db7bccf4STejun Heo  * Wait for the trustee to reach @state.  DONE is already matched.
2260db7bccf4STejun Heo  *
2261db7bccf4STejun Heo  * CONTEXT:
2262db7bccf4STejun Heo  * spin_lock_irq(gcwq->lock) which may be released and regrabbed
2263db7bccf4STejun Heo  * multiple times.  To be used by cpu_callback.
2264db7bccf4STejun Heo  */
2265db7bccf4STejun Heo static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
2266db7bccf4STejun Heo {
2267db7bccf4STejun Heo 	if (!(gcwq->trustee_state == state ||
2268db7bccf4STejun Heo 	      gcwq->trustee_state == TRUSTEE_DONE)) {
2269db7bccf4STejun Heo 		spin_unlock_irq(&gcwq->lock);
2270db7bccf4STejun Heo 		__wait_event(gcwq->trustee_wait,
2271db7bccf4STejun Heo 			     gcwq->trustee_state == state ||
2272db7bccf4STejun Heo 			     gcwq->trustee_state == TRUSTEE_DONE);
2273db7bccf4STejun Heo 		spin_lock_irq(&gcwq->lock);
2274db7bccf4STejun Heo 	}
2275db7bccf4STejun Heo }
2276db7bccf4STejun Heo 
22779c7b216dSChandra Seetharaman static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
22781da177e4SLinus Torvalds 						unsigned long action,
22791da177e4SLinus Torvalds 						void *hcpu)
22801da177e4SLinus Torvalds {
22813af24433SOleg Nesterov 	unsigned int cpu = (unsigned long)hcpu;
2282db7bccf4STejun Heo 	struct global_cwq *gcwq = get_gcwq(cpu);
2283db7bccf4STejun Heo 	struct task_struct *new_trustee = NULL;
2284db7bccf4STejun Heo 	struct worker *worker;
2285db7bccf4STejun Heo 	struct hlist_node *pos;
2286db7bccf4STejun Heo 	unsigned long flags;
2287db7bccf4STejun Heo 	int i;
22881da177e4SLinus Torvalds 
22898bb78442SRafael J. Wysocki 	action &= ~CPU_TASKS_FROZEN;
22908bb78442SRafael J. Wysocki 
2291db7bccf4STejun Heo 	switch (action) {
2292db7bccf4STejun Heo 	case CPU_DOWN_PREPARE:
2293db7bccf4STejun Heo 		new_trustee = kthread_create(trustee_thread, gcwq,
2294db7bccf4STejun Heo 					     "workqueue_trustee/%d\n", cpu);
2295db7bccf4STejun Heo 		if (IS_ERR(new_trustee))
2296db7bccf4STejun Heo 			return notifier_from_errno(PTR_ERR(new_trustee));
2297db7bccf4STejun Heo 		kthread_bind(new_trustee, cpu);
2298db7bccf4STejun Heo 	}
22991537663fSTejun Heo 
2300db7bccf4STejun Heo 	/* some are called w/ irq disabled, don't disturb irq status */
2301db7bccf4STejun Heo 	spin_lock_irqsave(&gcwq->lock, flags);
23023af24433SOleg Nesterov 
23033af24433SOleg Nesterov 	switch (action) {
2304db7bccf4STejun Heo 	case CPU_DOWN_PREPARE:
2305db7bccf4STejun Heo 		/* initialize trustee and tell it to acquire the gcwq */
2306db7bccf4STejun Heo 		BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
2307db7bccf4STejun Heo 		gcwq->trustee = new_trustee;
2308db7bccf4STejun Heo 		gcwq->trustee_state = TRUSTEE_START;
2309db7bccf4STejun Heo 		wake_up_process(gcwq->trustee);
2310db7bccf4STejun Heo 		wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
2311db7bccf4STejun Heo 		break;
2312db7bccf4STejun Heo 
23133da1c84cSOleg Nesterov 	case CPU_POST_DEAD:
2314db7bccf4STejun Heo 		gcwq->trustee_state = TRUSTEE_BUTCHER;
2315db7bccf4STejun Heo 		break;
2316db7bccf4STejun Heo 
2317db7bccf4STejun Heo 	case CPU_DOWN_FAILED:
2318db7bccf4STejun Heo 	case CPU_ONLINE:
2319db7bccf4STejun Heo 		if (gcwq->trustee_state != TRUSTEE_DONE) {
2320db7bccf4STejun Heo 			gcwq->trustee_state = TRUSTEE_RELEASE;
2321db7bccf4STejun Heo 			wake_up_process(gcwq->trustee);
2322db7bccf4STejun Heo 			wait_trustee_state(gcwq, TRUSTEE_DONE);
2323db7bccf4STejun Heo 		}
2324db7bccf4STejun Heo 
2325502ca9d8STejun Heo 		/* clear ROGUE from all workers */
2326db7bccf4STejun Heo 		list_for_each_entry(worker, &gcwq->idle_list, entry)
2327db7bccf4STejun Heo 			worker->flags &= ~WORKER_ROGUE;
2328db7bccf4STejun Heo 
2329db7bccf4STejun Heo 		for_each_busy_worker(worker, i, pos, gcwq)
2330db7bccf4STejun Heo 			worker->flags &= ~WORKER_ROGUE;
23311da177e4SLinus Torvalds 		break;
23321da177e4SLinus Torvalds 	}
2333db7bccf4STejun Heo 
2334db7bccf4STejun Heo 	spin_unlock_irqrestore(&gcwq->lock, flags);
23351da177e4SLinus Torvalds 
23361537663fSTejun Heo 	return notifier_from_errno(0);
23371da177e4SLinus Torvalds }
23381da177e4SLinus Torvalds 
23392d3854a3SRusty Russell #ifdef CONFIG_SMP
23408ccad40dSRusty Russell 
23412d3854a3SRusty Russell struct work_for_cpu {
23426b44003eSAndrew Morton 	struct completion completion;
23432d3854a3SRusty Russell 	long (*fn)(void *);
23442d3854a3SRusty Russell 	void *arg;
23452d3854a3SRusty Russell 	long ret;
23462d3854a3SRusty Russell };
23472d3854a3SRusty Russell 
23486b44003eSAndrew Morton static int do_work_for_cpu(void *_wfc)
23492d3854a3SRusty Russell {
23506b44003eSAndrew Morton 	struct work_for_cpu *wfc = _wfc;
23512d3854a3SRusty Russell 	wfc->ret = wfc->fn(wfc->arg);
23526b44003eSAndrew Morton 	complete(&wfc->completion);
23536b44003eSAndrew Morton 	return 0;
23542d3854a3SRusty Russell }
23552d3854a3SRusty Russell 
23562d3854a3SRusty Russell /**
23572d3854a3SRusty Russell  * work_on_cpu - run a function in user context on a particular cpu
23582d3854a3SRusty Russell  * @cpu: the cpu to run on
23592d3854a3SRusty Russell  * @fn: the function to run
23602d3854a3SRusty Russell  * @arg: the function arg
23612d3854a3SRusty Russell  *
236231ad9081SRusty Russell  * This will return the value @fn returns.
236331ad9081SRusty Russell  * It is up to the caller to ensure that the cpu doesn't go offline.
23646b44003eSAndrew Morton  * The caller must not hold any locks which would prevent @fn from completing.
23652d3854a3SRusty Russell  */
23662d3854a3SRusty Russell long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
23672d3854a3SRusty Russell {
23686b44003eSAndrew Morton 	struct task_struct *sub_thread;
23696b44003eSAndrew Morton 	struct work_for_cpu wfc = {
23706b44003eSAndrew Morton 		.completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
23716b44003eSAndrew Morton 		.fn = fn,
23726b44003eSAndrew Morton 		.arg = arg,
23736b44003eSAndrew Morton 	};
23742d3854a3SRusty Russell 
23756b44003eSAndrew Morton 	sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
23766b44003eSAndrew Morton 	if (IS_ERR(sub_thread))
23776b44003eSAndrew Morton 		return PTR_ERR(sub_thread);
23786b44003eSAndrew Morton 	kthread_bind(sub_thread, cpu);
23796b44003eSAndrew Morton 	wake_up_process(sub_thread);
23806b44003eSAndrew Morton 	wait_for_completion(&wfc.completion);
23812d3854a3SRusty Russell 	return wfc.ret;
23822d3854a3SRusty Russell }
23832d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu);
23842d3854a3SRusty Russell #endif /* CONFIG_SMP */
23852d3854a3SRusty Russell 
2386a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER
2387a0a1a5fdSTejun Heo 
2388a0a1a5fdSTejun Heo /**
2389a0a1a5fdSTejun Heo  * freeze_workqueues_begin - begin freezing workqueues
2390a0a1a5fdSTejun Heo  *
2391a0a1a5fdSTejun Heo  * Start freezing workqueues.  After this function returns, all
2392a0a1a5fdSTejun Heo  * freezeable workqueues will queue new works to their frozen_works
2393*7e11629dSTejun Heo  * list instead of gcwq->worklist.
2394a0a1a5fdSTejun Heo  *
2395a0a1a5fdSTejun Heo  * CONTEXT:
23968b03ae3cSTejun Heo  * Grabs and releases workqueue_lock and gcwq->lock's.
2397a0a1a5fdSTejun Heo  */
2398a0a1a5fdSTejun Heo void freeze_workqueues_begin(void)
2399a0a1a5fdSTejun Heo {
2400a0a1a5fdSTejun Heo 	struct workqueue_struct *wq;
2401a0a1a5fdSTejun Heo 	unsigned int cpu;
2402a0a1a5fdSTejun Heo 
2403a0a1a5fdSTejun Heo 	spin_lock(&workqueue_lock);
2404a0a1a5fdSTejun Heo 
2405a0a1a5fdSTejun Heo 	BUG_ON(workqueue_freezing);
2406a0a1a5fdSTejun Heo 	workqueue_freezing = true;
2407a0a1a5fdSTejun Heo 
2408a0a1a5fdSTejun Heo 	for_each_possible_cpu(cpu) {
24098b03ae3cSTejun Heo 		struct global_cwq *gcwq = get_gcwq(cpu);
24108b03ae3cSTejun Heo 
24118b03ae3cSTejun Heo 		spin_lock_irq(&gcwq->lock);
24128b03ae3cSTejun Heo 
2413db7bccf4STejun Heo 		BUG_ON(gcwq->flags & GCWQ_FREEZING);
2414db7bccf4STejun Heo 		gcwq->flags |= GCWQ_FREEZING;
2415db7bccf4STejun Heo 
2416a0a1a5fdSTejun Heo 		list_for_each_entry(wq, &workqueues, list) {
2417a0a1a5fdSTejun Heo 			struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
2418a0a1a5fdSTejun Heo 
2419a0a1a5fdSTejun Heo 			if (wq->flags & WQ_FREEZEABLE)
2420a0a1a5fdSTejun Heo 				cwq->max_active = 0;
2421a0a1a5fdSTejun Heo 		}
24228b03ae3cSTejun Heo 
24238b03ae3cSTejun Heo 		spin_unlock_irq(&gcwq->lock);
2424a0a1a5fdSTejun Heo 	}
2425a0a1a5fdSTejun Heo 
2426a0a1a5fdSTejun Heo 	spin_unlock(&workqueue_lock);
2427a0a1a5fdSTejun Heo }
2428a0a1a5fdSTejun Heo 
2429a0a1a5fdSTejun Heo /**
2430a0a1a5fdSTejun Heo  * freeze_workqueues_busy - are freezeable workqueues still busy?
2431a0a1a5fdSTejun Heo  *
2432a0a1a5fdSTejun Heo  * Check whether freezing is complete.  This function must be called
2433a0a1a5fdSTejun Heo  * between freeze_workqueues_begin() and thaw_workqueues().
2434a0a1a5fdSTejun Heo  *
2435a0a1a5fdSTejun Heo  * CONTEXT:
2436a0a1a5fdSTejun Heo  * Grabs and releases workqueue_lock.
2437a0a1a5fdSTejun Heo  *
2438a0a1a5fdSTejun Heo  * RETURNS:
2439a0a1a5fdSTejun Heo  * %true if some freezeable workqueues are still busy.  %false if
2440a0a1a5fdSTejun Heo  * freezing is complete.
2441a0a1a5fdSTejun Heo  */
2442a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void)
2443a0a1a5fdSTejun Heo {
2444a0a1a5fdSTejun Heo 	struct workqueue_struct *wq;
2445a0a1a5fdSTejun Heo 	unsigned int cpu;
2446a0a1a5fdSTejun Heo 	bool busy = false;
2447a0a1a5fdSTejun Heo 
2448a0a1a5fdSTejun Heo 	spin_lock(&workqueue_lock);
2449a0a1a5fdSTejun Heo 
2450a0a1a5fdSTejun Heo 	BUG_ON(!workqueue_freezing);
2451a0a1a5fdSTejun Heo 
2452a0a1a5fdSTejun Heo 	for_each_possible_cpu(cpu) {
2453a0a1a5fdSTejun Heo 		/*
2454a0a1a5fdSTejun Heo 		 * nr_active is monotonically decreasing.  It's safe
2455a0a1a5fdSTejun Heo 		 * to peek without lock.
2456a0a1a5fdSTejun Heo 		 */
2457a0a1a5fdSTejun Heo 		list_for_each_entry(wq, &workqueues, list) {
2458a0a1a5fdSTejun Heo 			struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
2459a0a1a5fdSTejun Heo 
2460a0a1a5fdSTejun Heo 			if (!(wq->flags & WQ_FREEZEABLE))
2461a0a1a5fdSTejun Heo 				continue;
2462a0a1a5fdSTejun Heo 
2463a0a1a5fdSTejun Heo 			BUG_ON(cwq->nr_active < 0);
2464a0a1a5fdSTejun Heo 			if (cwq->nr_active) {
2465a0a1a5fdSTejun Heo 				busy = true;
2466a0a1a5fdSTejun Heo 				goto out_unlock;
2467a0a1a5fdSTejun Heo 			}
2468a0a1a5fdSTejun Heo 		}
2469a0a1a5fdSTejun Heo 	}
2470a0a1a5fdSTejun Heo out_unlock:
2471a0a1a5fdSTejun Heo 	spin_unlock(&workqueue_lock);
2472a0a1a5fdSTejun Heo 	return busy;
2473a0a1a5fdSTejun Heo }
2474a0a1a5fdSTejun Heo 
2475a0a1a5fdSTejun Heo /**
2476a0a1a5fdSTejun Heo  * thaw_workqueues - thaw workqueues
2477a0a1a5fdSTejun Heo  *
2478a0a1a5fdSTejun Heo  * Thaw workqueues.  Normal queueing is restored and all collected
2479*7e11629dSTejun Heo  * frozen works are transferred to their respective gcwq worklists.
2480a0a1a5fdSTejun Heo  *
2481a0a1a5fdSTejun Heo  * CONTEXT:
24828b03ae3cSTejun Heo  * Grabs and releases workqueue_lock and gcwq->lock's.
2483a0a1a5fdSTejun Heo  */
2484a0a1a5fdSTejun Heo void thaw_workqueues(void)
2485a0a1a5fdSTejun Heo {
2486a0a1a5fdSTejun Heo 	struct workqueue_struct *wq;
2487a0a1a5fdSTejun Heo 	unsigned int cpu;
2488a0a1a5fdSTejun Heo 
2489a0a1a5fdSTejun Heo 	spin_lock(&workqueue_lock);
2490a0a1a5fdSTejun Heo 
2491a0a1a5fdSTejun Heo 	if (!workqueue_freezing)
2492a0a1a5fdSTejun Heo 		goto out_unlock;
2493a0a1a5fdSTejun Heo 
2494a0a1a5fdSTejun Heo 	for_each_possible_cpu(cpu) {
24958b03ae3cSTejun Heo 		struct global_cwq *gcwq = get_gcwq(cpu);
24968b03ae3cSTejun Heo 
24978b03ae3cSTejun Heo 		spin_lock_irq(&gcwq->lock);
24988b03ae3cSTejun Heo 
2499db7bccf4STejun Heo 		BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
2500db7bccf4STejun Heo 		gcwq->flags &= ~GCWQ_FREEZING;
2501db7bccf4STejun Heo 
2502a0a1a5fdSTejun Heo 		list_for_each_entry(wq, &workqueues, list) {
2503a0a1a5fdSTejun Heo 			struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
2504a0a1a5fdSTejun Heo 
2505a0a1a5fdSTejun Heo 			if (!(wq->flags & WQ_FREEZEABLE))
2506a0a1a5fdSTejun Heo 				continue;
2507a0a1a5fdSTejun Heo 
2508a0a1a5fdSTejun Heo 			/* restore max_active and repopulate worklist */
2509a0a1a5fdSTejun Heo 			cwq->max_active = wq->saved_max_active;
2510a0a1a5fdSTejun Heo 
2511a0a1a5fdSTejun Heo 			while (!list_empty(&cwq->delayed_works) &&
2512a0a1a5fdSTejun Heo 			       cwq->nr_active < cwq->max_active)
2513a0a1a5fdSTejun Heo 				cwq_activate_first_delayed(cwq);
2514a0a1a5fdSTejun Heo 
2515502ca9d8STejun Heo 			/* perform delayed unbind from single cpu if empty */
2516502ca9d8STejun Heo 			if (wq->single_cpu == gcwq->cpu &&
2517502ca9d8STejun Heo 			    !cwq->nr_active && list_empty(&cwq->delayed_works))
2518502ca9d8STejun Heo 				cwq_unbind_single_cpu(cwq);
2519502ca9d8STejun Heo 
2520c8e55f36STejun Heo 			wake_up_process(cwq->worker->task);
2521a0a1a5fdSTejun Heo 		}
25228b03ae3cSTejun Heo 
25238b03ae3cSTejun Heo 		spin_unlock_irq(&gcwq->lock);
2524a0a1a5fdSTejun Heo 	}
2525a0a1a5fdSTejun Heo 
2526a0a1a5fdSTejun Heo 	workqueue_freezing = false;
2527a0a1a5fdSTejun Heo out_unlock:
2528a0a1a5fdSTejun Heo 	spin_unlock(&workqueue_lock);
2529a0a1a5fdSTejun Heo }
2530a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */
2531a0a1a5fdSTejun Heo 
2532c12920d1SOleg Nesterov void __init init_workqueues(void)
25331da177e4SLinus Torvalds {
2534c34056a3STejun Heo 	unsigned int cpu;
2535c8e55f36STejun Heo 	int i;
2536c34056a3STejun Heo 
25377a22ad75STejun Heo 	/*
25387a22ad75STejun Heo 	 * The pointer part of work->data is either pointing to the
25397a22ad75STejun Heo 	 * cwq or contains the cpu number the work ran last on.  Make
25407a22ad75STejun Heo 	 * sure cpu number won't overflow into kernel pointer area so
25417a22ad75STejun Heo 	 * that they can be distinguished.
25427a22ad75STejun Heo 	 */
25437a22ad75STejun Heo 	BUILD_BUG_ON(NR_CPUS << WORK_STRUCT_FLAG_BITS >= PAGE_OFFSET);
25447a22ad75STejun Heo 
2545db7bccf4STejun Heo 	hotcpu_notifier(workqueue_cpu_callback, CPU_PRI_WORKQUEUE);
25468b03ae3cSTejun Heo 
25478b03ae3cSTejun Heo 	/* initialize gcwqs */
25488b03ae3cSTejun Heo 	for_each_possible_cpu(cpu) {
25498b03ae3cSTejun Heo 		struct global_cwq *gcwq = get_gcwq(cpu);
25508b03ae3cSTejun Heo 
25518b03ae3cSTejun Heo 		spin_lock_init(&gcwq->lock);
2552*7e11629dSTejun Heo 		INIT_LIST_HEAD(&gcwq->worklist);
25538b03ae3cSTejun Heo 		gcwq->cpu = cpu;
25548b03ae3cSTejun Heo 
2555c8e55f36STejun Heo 		INIT_LIST_HEAD(&gcwq->idle_list);
2556c8e55f36STejun Heo 		for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
2557c8e55f36STejun Heo 			INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
2558c8e55f36STejun Heo 
25598b03ae3cSTejun Heo 		ida_init(&gcwq->worker_ida);
2560db7bccf4STejun Heo 
2561db7bccf4STejun Heo 		gcwq->trustee_state = TRUSTEE_DONE;
2562db7bccf4STejun Heo 		init_waitqueue_head(&gcwq->trustee_wait);
25638b03ae3cSTejun Heo 	}
25648b03ae3cSTejun Heo 
25651da177e4SLinus Torvalds 	keventd_wq = create_workqueue("events");
25661da177e4SLinus Torvalds 	BUG_ON(!keventd_wq);
25671da177e4SLinus Torvalds }
2568