xref: /linux-6.15/kernel/workqueue.c (revision 18aa9eff)
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>
371da177e4SLinus Torvalds 
38c8e55f36STejun Heo enum {
39db7bccf4STejun Heo 	/* global_cwq flags */
40db7bccf4STejun Heo 	GCWQ_FREEZING		= 1 << 3,	/* freeze in progress */
41db7bccf4STejun Heo 
42c8e55f36STejun Heo 	/* worker flags */
43c8e55f36STejun Heo 	WORKER_STARTED		= 1 << 0,	/* started */
44c8e55f36STejun Heo 	WORKER_DIE		= 1 << 1,	/* die die die */
45c8e55f36STejun Heo 	WORKER_IDLE		= 1 << 2,	/* is idle */
46db7bccf4STejun Heo 	WORKER_ROGUE		= 1 << 4,	/* not bound to any cpu */
47db7bccf4STejun Heo 
48db7bccf4STejun Heo 	/* gcwq->trustee_state */
49db7bccf4STejun Heo 	TRUSTEE_START		= 0,		/* start */
50db7bccf4STejun Heo 	TRUSTEE_IN_CHARGE	= 1,		/* trustee in charge of gcwq */
51db7bccf4STejun Heo 	TRUSTEE_BUTCHER		= 2,		/* butcher workers */
52db7bccf4STejun Heo 	TRUSTEE_RELEASE		= 3,		/* release workers */
53db7bccf4STejun Heo 	TRUSTEE_DONE		= 4,		/* trustee is done */
54c8e55f36STejun Heo 
55c8e55f36STejun Heo 	BUSY_WORKER_HASH_ORDER	= 6,		/* 64 pointers */
56c8e55f36STejun Heo 	BUSY_WORKER_HASH_SIZE	= 1 << BUSY_WORKER_HASH_ORDER,
57c8e55f36STejun Heo 	BUSY_WORKER_HASH_MASK	= BUSY_WORKER_HASH_SIZE - 1,
58db7bccf4STejun Heo 
59db7bccf4STejun Heo 	TRUSTEE_COOLDOWN	= HZ / 10,	/* for trustee draining */
60c8e55f36STejun Heo };
61c8e55f36STejun Heo 
621da177e4SLinus Torvalds /*
634690c4abSTejun Heo  * Structure fields follow one of the following exclusion rules.
644690c4abSTejun Heo  *
654690c4abSTejun Heo  * I: Set during initialization and read-only afterwards.
664690c4abSTejun Heo  *
678b03ae3cSTejun Heo  * L: gcwq->lock protected.  Access with gcwq->lock held.
684690c4abSTejun Heo  *
6973f53c4aSTejun Heo  * F: wq->flush_mutex protected.
7073f53c4aSTejun Heo  *
714690c4abSTejun Heo  * W: workqueue_lock protected.
724690c4abSTejun Heo  */
734690c4abSTejun Heo 
748b03ae3cSTejun Heo struct global_cwq;
75c34056a3STejun Heo struct cpu_workqueue_struct;
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 */
89c34056a3STejun Heo 	struct cpu_workqueue_struct *cwq;	/* I: the associated cwq */
90c8e55f36STejun Heo 	unsigned int		flags;		/* L: flags */
91c34056a3STejun Heo 	int			id;		/* I: worker id */
92c34056a3STejun Heo };
93c34056a3STejun Heo 
944690c4abSTejun Heo /*
958b03ae3cSTejun Heo  * Global per-cpu workqueue.
968b03ae3cSTejun Heo  */
978b03ae3cSTejun Heo struct global_cwq {
988b03ae3cSTejun Heo 	spinlock_t		lock;		/* the gcwq lock */
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 */
1241da177e4SLinus Torvalds 	struct list_head worklist;
125c34056a3STejun Heo 	struct worker		*worker;
1264690c4abSTejun Heo 	struct workqueue_struct *wq;		/* I: the owning workqueue */
12773f53c4aSTejun Heo 	int			work_color;	/* L: current color */
12873f53c4aSTejun Heo 	int			flush_color;	/* L: flushing color */
12973f53c4aSTejun Heo 	int			nr_in_flight[WORK_NR_COLORS];
13073f53c4aSTejun Heo 						/* L: nr of in_flight works */
1311e19ffc6STejun Heo 	int			nr_active;	/* L: nr of active works */
132a0a1a5fdSTejun Heo 	int			max_active;	/* L: max active works */
1331e19ffc6STejun Heo 	struct list_head	delayed_works;	/* L: delayed works */
1340f900049STejun Heo };
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds /*
13773f53c4aSTejun Heo  * Structure used to wait for workqueue flush.
13873f53c4aSTejun Heo  */
13973f53c4aSTejun Heo struct wq_flusher {
14073f53c4aSTejun Heo 	struct list_head	list;		/* F: list of flushers */
14173f53c4aSTejun Heo 	int			flush_color;	/* F: flush color waiting for */
14273f53c4aSTejun Heo 	struct completion	done;		/* flush completion */
14373f53c4aSTejun Heo };
14473f53c4aSTejun Heo 
14573f53c4aSTejun Heo /*
1461da177e4SLinus Torvalds  * The externally visible workqueue abstraction is an array of
1471da177e4SLinus Torvalds  * per-CPU workqueues:
1481da177e4SLinus Torvalds  */
1491da177e4SLinus Torvalds struct workqueue_struct {
15097e37d7bSTejun Heo 	unsigned int		flags;		/* I: WQ_* flags */
1514690c4abSTejun Heo 	struct cpu_workqueue_struct *cpu_wq;	/* I: cwq's */
1524690c4abSTejun Heo 	struct list_head	list;		/* W: list of all workqueues */
15373f53c4aSTejun Heo 
15473f53c4aSTejun Heo 	struct mutex		flush_mutex;	/* protects wq flushing */
15573f53c4aSTejun Heo 	int			work_color;	/* F: current work color */
15673f53c4aSTejun Heo 	int			flush_color;	/* F: current flush color */
15773f53c4aSTejun Heo 	atomic_t		nr_cwqs_to_flush; /* flush in progress */
15873f53c4aSTejun Heo 	struct wq_flusher	*first_flusher;	/* F: first flusher */
15973f53c4aSTejun Heo 	struct list_head	flusher_queue;	/* F: flush waiters */
16073f53c4aSTejun Heo 	struct list_head	flusher_overflow; /* F: flush overflow list */
16173f53c4aSTejun Heo 
162502ca9d8STejun Heo 	unsigned long		single_cpu;	/* cpu for single cpu wq */
163502ca9d8STejun Heo 
164a0a1a5fdSTejun Heo 	int			saved_max_active; /* I: saved cwq max_active */
1654690c4abSTejun Heo 	const char		*name;		/* I: workqueue name */
1664e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
1674e6045f1SJohannes Berg 	struct lockdep_map	lockdep_map;
1684e6045f1SJohannes Berg #endif
1691da177e4SLinus Torvalds };
1701da177e4SLinus Torvalds 
171db7bccf4STejun Heo #define for_each_busy_worker(worker, i, pos, gcwq)			\
172db7bccf4STejun Heo 	for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)			\
173db7bccf4STejun Heo 		hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
174db7bccf4STejun Heo 
175dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK
176dc186ad7SThomas Gleixner 
177dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr;
178dc186ad7SThomas Gleixner 
179dc186ad7SThomas Gleixner /*
180dc186ad7SThomas Gleixner  * fixup_init is called when:
181dc186ad7SThomas Gleixner  * - an active object is initialized
182dc186ad7SThomas Gleixner  */
183dc186ad7SThomas Gleixner static int work_fixup_init(void *addr, enum debug_obj_state state)
184dc186ad7SThomas Gleixner {
185dc186ad7SThomas Gleixner 	struct work_struct *work = addr;
186dc186ad7SThomas Gleixner 
187dc186ad7SThomas Gleixner 	switch (state) {
188dc186ad7SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
189dc186ad7SThomas Gleixner 		cancel_work_sync(work);
190dc186ad7SThomas Gleixner 		debug_object_init(work, &work_debug_descr);
191dc186ad7SThomas Gleixner 		return 1;
192dc186ad7SThomas Gleixner 	default:
193dc186ad7SThomas Gleixner 		return 0;
194dc186ad7SThomas Gleixner 	}
195dc186ad7SThomas Gleixner }
196dc186ad7SThomas Gleixner 
197dc186ad7SThomas Gleixner /*
198dc186ad7SThomas Gleixner  * fixup_activate is called when:
199dc186ad7SThomas Gleixner  * - an active object is activated
200dc186ad7SThomas Gleixner  * - an unknown object is activated (might be a statically initialized object)
201dc186ad7SThomas Gleixner  */
202dc186ad7SThomas Gleixner static int work_fixup_activate(void *addr, enum debug_obj_state state)
203dc186ad7SThomas Gleixner {
204dc186ad7SThomas Gleixner 	struct work_struct *work = addr;
205dc186ad7SThomas Gleixner 
206dc186ad7SThomas Gleixner 	switch (state) {
207dc186ad7SThomas Gleixner 
208dc186ad7SThomas Gleixner 	case ODEBUG_STATE_NOTAVAILABLE:
209dc186ad7SThomas Gleixner 		/*
210dc186ad7SThomas Gleixner 		 * This is not really a fixup. The work struct was
211dc186ad7SThomas Gleixner 		 * statically initialized. We just make sure that it
212dc186ad7SThomas Gleixner 		 * is tracked in the object tracker.
213dc186ad7SThomas Gleixner 		 */
21422df02bbSTejun Heo 		if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
215dc186ad7SThomas Gleixner 			debug_object_init(work, &work_debug_descr);
216dc186ad7SThomas Gleixner 			debug_object_activate(work, &work_debug_descr);
217dc186ad7SThomas Gleixner 			return 0;
218dc186ad7SThomas Gleixner 		}
219dc186ad7SThomas Gleixner 		WARN_ON_ONCE(1);
220dc186ad7SThomas Gleixner 		return 0;
221dc186ad7SThomas Gleixner 
222dc186ad7SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
223dc186ad7SThomas Gleixner 		WARN_ON(1);
224dc186ad7SThomas Gleixner 
225dc186ad7SThomas Gleixner 	default:
226dc186ad7SThomas Gleixner 		return 0;
227dc186ad7SThomas Gleixner 	}
228dc186ad7SThomas Gleixner }
229dc186ad7SThomas Gleixner 
230dc186ad7SThomas Gleixner /*
231dc186ad7SThomas Gleixner  * fixup_free is called when:
232dc186ad7SThomas Gleixner  * - an active object is freed
233dc186ad7SThomas Gleixner  */
234dc186ad7SThomas Gleixner static int work_fixup_free(void *addr, enum debug_obj_state state)
235dc186ad7SThomas Gleixner {
236dc186ad7SThomas Gleixner 	struct work_struct *work = addr;
237dc186ad7SThomas Gleixner 
238dc186ad7SThomas Gleixner 	switch (state) {
239dc186ad7SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
240dc186ad7SThomas Gleixner 		cancel_work_sync(work);
241dc186ad7SThomas Gleixner 		debug_object_free(work, &work_debug_descr);
242dc186ad7SThomas Gleixner 		return 1;
243dc186ad7SThomas Gleixner 	default:
244dc186ad7SThomas Gleixner 		return 0;
245dc186ad7SThomas Gleixner 	}
246dc186ad7SThomas Gleixner }
247dc186ad7SThomas Gleixner 
248dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr = {
249dc186ad7SThomas Gleixner 	.name		= "work_struct",
250dc186ad7SThomas Gleixner 	.fixup_init	= work_fixup_init,
251dc186ad7SThomas Gleixner 	.fixup_activate	= work_fixup_activate,
252dc186ad7SThomas Gleixner 	.fixup_free	= work_fixup_free,
253dc186ad7SThomas Gleixner };
254dc186ad7SThomas Gleixner 
255dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work)
256dc186ad7SThomas Gleixner {
257dc186ad7SThomas Gleixner 	debug_object_activate(work, &work_debug_descr);
258dc186ad7SThomas Gleixner }
259dc186ad7SThomas Gleixner 
260dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work)
261dc186ad7SThomas Gleixner {
262dc186ad7SThomas Gleixner 	debug_object_deactivate(work, &work_debug_descr);
263dc186ad7SThomas Gleixner }
264dc186ad7SThomas Gleixner 
265dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack)
266dc186ad7SThomas Gleixner {
267dc186ad7SThomas Gleixner 	if (onstack)
268dc186ad7SThomas Gleixner 		debug_object_init_on_stack(work, &work_debug_descr);
269dc186ad7SThomas Gleixner 	else
270dc186ad7SThomas Gleixner 		debug_object_init(work, &work_debug_descr);
271dc186ad7SThomas Gleixner }
272dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work);
273dc186ad7SThomas Gleixner 
274dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work)
275dc186ad7SThomas Gleixner {
276dc186ad7SThomas Gleixner 	debug_object_free(work, &work_debug_descr);
277dc186ad7SThomas Gleixner }
278dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack);
279dc186ad7SThomas Gleixner 
280dc186ad7SThomas Gleixner #else
281dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { }
282dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { }
283dc186ad7SThomas Gleixner #endif
284dc186ad7SThomas Gleixner 
28595402b38SGautham R Shenoy /* Serializes the accesses to the list of workqueues. */
28695402b38SGautham R Shenoy static DEFINE_SPINLOCK(workqueue_lock);
2871da177e4SLinus Torvalds static LIST_HEAD(workqueues);
288a0a1a5fdSTejun Heo static bool workqueue_freezing;		/* W: have wqs started freezing? */
289c34056a3STejun Heo 
2908b03ae3cSTejun Heo static DEFINE_PER_CPU(struct global_cwq, global_cwq);
2918b03ae3cSTejun Heo 
292c34056a3STejun Heo static int worker_thread(void *__worker);
2931da177e4SLinus Torvalds 
2948b03ae3cSTejun Heo static struct global_cwq *get_gcwq(unsigned int cpu)
2958b03ae3cSTejun Heo {
2968b03ae3cSTejun Heo 	return &per_cpu(global_cwq, cpu);
2978b03ae3cSTejun Heo }
2988b03ae3cSTejun Heo 
2994690c4abSTejun Heo static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
3004690c4abSTejun Heo 					    struct workqueue_struct *wq)
301a848e3b6SOleg Nesterov {
302a848e3b6SOleg Nesterov 	return per_cpu_ptr(wq->cpu_wq, cpu);
303a848e3b6SOleg Nesterov }
304a848e3b6SOleg Nesterov 
30573f53c4aSTejun Heo static unsigned int work_color_to_flags(int color)
30673f53c4aSTejun Heo {
30773f53c4aSTejun Heo 	return color << WORK_STRUCT_COLOR_SHIFT;
30873f53c4aSTejun Heo }
30973f53c4aSTejun Heo 
31073f53c4aSTejun Heo static int get_work_color(struct work_struct *work)
31173f53c4aSTejun Heo {
31273f53c4aSTejun Heo 	return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
31373f53c4aSTejun Heo 		((1 << WORK_STRUCT_COLOR_BITS) - 1);
31473f53c4aSTejun Heo }
31573f53c4aSTejun Heo 
31673f53c4aSTejun Heo static int work_next_color(int color)
31773f53c4aSTejun Heo {
31873f53c4aSTejun Heo 	return (color + 1) % WORK_NR_COLORS;
31973f53c4aSTejun Heo }
32073f53c4aSTejun Heo 
3214594bf15SDavid Howells /*
3227a22ad75STejun Heo  * Work data points to the cwq while a work is on queue.  Once
3237a22ad75STejun Heo  * execution starts, it points to the cpu the work was last on.  This
3247a22ad75STejun Heo  * can be distinguished by comparing the data value against
3257a22ad75STejun Heo  * PAGE_OFFSET.
3267a22ad75STejun Heo  *
3277a22ad75STejun Heo  * set_work_{cwq|cpu}() and clear_work_data() can be used to set the
3287a22ad75STejun Heo  * cwq, cpu or clear work->data.  These functions should only be
3297a22ad75STejun Heo  * called while the work is owned - ie. while the PENDING bit is set.
3307a22ad75STejun Heo  *
3317a22ad75STejun Heo  * get_work_[g]cwq() can be used to obtain the gcwq or cwq
3327a22ad75STejun Heo  * corresponding to a work.  gcwq is available once the work has been
3337a22ad75STejun Heo  * queued anywhere after initialization.  cwq is available only from
3347a22ad75STejun Heo  * queueing until execution starts.
3354594bf15SDavid Howells  */
3367a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data,
3377a22ad75STejun Heo 				 unsigned long flags)
3387a22ad75STejun Heo {
3397a22ad75STejun Heo 	BUG_ON(!work_pending(work));
3407a22ad75STejun Heo 	atomic_long_set(&work->data, data | flags | work_static(work));
3417a22ad75STejun Heo }
3427a22ad75STejun Heo 
3437a22ad75STejun Heo static void set_work_cwq(struct work_struct *work,
3444690c4abSTejun Heo 			 struct cpu_workqueue_struct *cwq,
3454690c4abSTejun Heo 			 unsigned long extra_flags)
346365970a1SDavid Howells {
3477a22ad75STejun Heo 	set_work_data(work, (unsigned long)cwq,
34822df02bbSTejun Heo 		      WORK_STRUCT_PENDING | extra_flags);
349365970a1SDavid Howells }
350365970a1SDavid Howells 
3517a22ad75STejun Heo static void set_work_cpu(struct work_struct *work, unsigned int cpu)
3524d707b9fSOleg Nesterov {
3537a22ad75STejun Heo 	set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING);
3544d707b9fSOleg Nesterov }
3554d707b9fSOleg Nesterov 
3567a22ad75STejun Heo static void clear_work_data(struct work_struct *work)
357365970a1SDavid Howells {
3587a22ad75STejun Heo 	set_work_data(work, WORK_STRUCT_NO_CPU, 0);
3597a22ad75STejun Heo }
3607a22ad75STejun Heo 
3617a22ad75STejun Heo static inline unsigned long get_work_data(struct work_struct *work)
3627a22ad75STejun Heo {
3637a22ad75STejun Heo 	return atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK;
3647a22ad75STejun Heo }
3657a22ad75STejun Heo 
3667a22ad75STejun Heo static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
3677a22ad75STejun Heo {
3687a22ad75STejun Heo 	unsigned long data = get_work_data(work);
3697a22ad75STejun Heo 
3707a22ad75STejun Heo 	return data >= PAGE_OFFSET ? (void *)data : NULL;
3717a22ad75STejun Heo }
3727a22ad75STejun Heo 
3737a22ad75STejun Heo static struct global_cwq *get_work_gcwq(struct work_struct *work)
3747a22ad75STejun Heo {
3757a22ad75STejun Heo 	unsigned long data = get_work_data(work);
3767a22ad75STejun Heo 	unsigned int cpu;
3777a22ad75STejun Heo 
3787a22ad75STejun Heo 	if (data >= PAGE_OFFSET)
3797a22ad75STejun Heo 		return ((struct cpu_workqueue_struct *)data)->gcwq;
3807a22ad75STejun Heo 
3817a22ad75STejun Heo 	cpu = data >> WORK_STRUCT_FLAG_BITS;
3827a22ad75STejun Heo 	if (cpu == NR_CPUS)
3837a22ad75STejun Heo 		return NULL;
3847a22ad75STejun Heo 
3857a22ad75STejun Heo 	BUG_ON(cpu >= num_possible_cpus());
3867a22ad75STejun Heo 	return get_gcwq(cpu);
387365970a1SDavid Howells }
388365970a1SDavid Howells 
3894690c4abSTejun Heo /**
390c8e55f36STejun Heo  * busy_worker_head - return the busy hash head for a work
391c8e55f36STejun Heo  * @gcwq: gcwq of interest
392c8e55f36STejun Heo  * @work: work to be hashed
393c8e55f36STejun Heo  *
394c8e55f36STejun Heo  * Return hash head of @gcwq for @work.
395c8e55f36STejun Heo  *
396c8e55f36STejun Heo  * CONTEXT:
397c8e55f36STejun Heo  * spin_lock_irq(gcwq->lock).
398c8e55f36STejun Heo  *
399c8e55f36STejun Heo  * RETURNS:
400c8e55f36STejun Heo  * Pointer to the hash head.
401c8e55f36STejun Heo  */
402c8e55f36STejun Heo static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
403c8e55f36STejun Heo 					   struct work_struct *work)
404c8e55f36STejun Heo {
405c8e55f36STejun Heo 	const int base_shift = ilog2(sizeof(struct work_struct));
406c8e55f36STejun Heo 	unsigned long v = (unsigned long)work;
407c8e55f36STejun Heo 
408c8e55f36STejun Heo 	/* simple shift and fold hash, do we need something better? */
409c8e55f36STejun Heo 	v >>= base_shift;
410c8e55f36STejun Heo 	v += v >> BUSY_WORKER_HASH_ORDER;
411c8e55f36STejun Heo 	v &= BUSY_WORKER_HASH_MASK;
412c8e55f36STejun Heo 
413c8e55f36STejun Heo 	return &gcwq->busy_hash[v];
414c8e55f36STejun Heo }
415c8e55f36STejun Heo 
416c8e55f36STejun Heo /**
4178cca0eeaSTejun Heo  * __find_worker_executing_work - find worker which is executing a work
4188cca0eeaSTejun Heo  * @gcwq: gcwq of interest
4198cca0eeaSTejun Heo  * @bwh: hash head as returned by busy_worker_head()
4208cca0eeaSTejun Heo  * @work: work to find worker for
4218cca0eeaSTejun Heo  *
4228cca0eeaSTejun Heo  * Find a worker which is executing @work on @gcwq.  @bwh should be
4238cca0eeaSTejun Heo  * the hash head obtained by calling busy_worker_head() with the same
4248cca0eeaSTejun Heo  * work.
4258cca0eeaSTejun Heo  *
4268cca0eeaSTejun Heo  * CONTEXT:
4278cca0eeaSTejun Heo  * spin_lock_irq(gcwq->lock).
4288cca0eeaSTejun Heo  *
4298cca0eeaSTejun Heo  * RETURNS:
4308cca0eeaSTejun Heo  * Pointer to worker which is executing @work if found, NULL
4318cca0eeaSTejun Heo  * otherwise.
4328cca0eeaSTejun Heo  */
4338cca0eeaSTejun Heo static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
4348cca0eeaSTejun Heo 						   struct hlist_head *bwh,
4358cca0eeaSTejun Heo 						   struct work_struct *work)
4368cca0eeaSTejun Heo {
4378cca0eeaSTejun Heo 	struct worker *worker;
4388cca0eeaSTejun Heo 	struct hlist_node *tmp;
4398cca0eeaSTejun Heo 
4408cca0eeaSTejun Heo 	hlist_for_each_entry(worker, tmp, bwh, hentry)
4418cca0eeaSTejun Heo 		if (worker->current_work == work)
4428cca0eeaSTejun Heo 			return worker;
4438cca0eeaSTejun Heo 	return NULL;
4448cca0eeaSTejun Heo }
4458cca0eeaSTejun Heo 
4468cca0eeaSTejun Heo /**
4478cca0eeaSTejun Heo  * find_worker_executing_work - find worker which is executing a work
4488cca0eeaSTejun Heo  * @gcwq: gcwq of interest
4498cca0eeaSTejun Heo  * @work: work to find worker for
4508cca0eeaSTejun Heo  *
4518cca0eeaSTejun Heo  * Find a worker which is executing @work on @gcwq.  This function is
4528cca0eeaSTejun Heo  * identical to __find_worker_executing_work() except that this
4538cca0eeaSTejun Heo  * function calculates @bwh itself.
4548cca0eeaSTejun Heo  *
4558cca0eeaSTejun Heo  * CONTEXT:
4568cca0eeaSTejun Heo  * spin_lock_irq(gcwq->lock).
4578cca0eeaSTejun Heo  *
4588cca0eeaSTejun Heo  * RETURNS:
4598cca0eeaSTejun Heo  * Pointer to worker which is executing @work if found, NULL
4608cca0eeaSTejun Heo  * otherwise.
4618cca0eeaSTejun Heo  */
4628cca0eeaSTejun Heo static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
4638cca0eeaSTejun Heo 						 struct work_struct *work)
4648cca0eeaSTejun Heo {
4658cca0eeaSTejun Heo 	return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
4668cca0eeaSTejun Heo 					    work);
4678cca0eeaSTejun Heo }
4688cca0eeaSTejun Heo 
4698cca0eeaSTejun Heo /**
4704690c4abSTejun Heo  * insert_work - insert a work into cwq
4714690c4abSTejun Heo  * @cwq: cwq @work belongs to
4724690c4abSTejun Heo  * @work: work to insert
4734690c4abSTejun Heo  * @head: insertion point
4744690c4abSTejun Heo  * @extra_flags: extra WORK_STRUCT_* flags to set
4754690c4abSTejun Heo  *
4764690c4abSTejun Heo  * Insert @work into @cwq after @head.
4774690c4abSTejun Heo  *
4784690c4abSTejun Heo  * CONTEXT:
4798b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock).
4804690c4abSTejun Heo  */
481b89deed3SOleg Nesterov static void insert_work(struct cpu_workqueue_struct *cwq,
4824690c4abSTejun Heo 			struct work_struct *work, struct list_head *head,
4834690c4abSTejun Heo 			unsigned int extra_flags)
484b89deed3SOleg Nesterov {
4854690c4abSTejun Heo 	/* we own @work, set data and link */
4867a22ad75STejun Heo 	set_work_cwq(work, cwq, extra_flags);
4874690c4abSTejun Heo 
4886e84d644SOleg Nesterov 	/*
4896e84d644SOleg Nesterov 	 * Ensure that we get the right work->data if we see the
4906e84d644SOleg Nesterov 	 * result of list_add() below, see try_to_grab_pending().
4916e84d644SOleg Nesterov 	 */
4926e84d644SOleg Nesterov 	smp_wmb();
4934690c4abSTejun Heo 
4941a4d9b0aSOleg Nesterov 	list_add_tail(&work->entry, head);
495c8e55f36STejun Heo 	wake_up_process(cwq->worker->task);
496b89deed3SOleg Nesterov }
497b89deed3SOleg Nesterov 
498502ca9d8STejun Heo /**
499502ca9d8STejun Heo  * cwq_unbind_single_cpu - unbind cwq from single cpu workqueue processing
500502ca9d8STejun Heo  * @cwq: cwq to unbind
501502ca9d8STejun Heo  *
502502ca9d8STejun Heo  * Try to unbind @cwq from single cpu workqueue processing.  If
503502ca9d8STejun Heo  * @cwq->wq is frozen, unbind is delayed till the workqueue is thawed.
504502ca9d8STejun Heo  *
505502ca9d8STejun Heo  * CONTEXT:
506502ca9d8STejun Heo  * spin_lock_irq(gcwq->lock).
507502ca9d8STejun Heo  */
508502ca9d8STejun Heo static void cwq_unbind_single_cpu(struct cpu_workqueue_struct *cwq)
509502ca9d8STejun Heo {
510502ca9d8STejun Heo 	struct workqueue_struct *wq = cwq->wq;
511502ca9d8STejun Heo 	struct global_cwq *gcwq = cwq->gcwq;
512502ca9d8STejun Heo 
513502ca9d8STejun Heo 	BUG_ON(wq->single_cpu != gcwq->cpu);
514502ca9d8STejun Heo 	/*
515502ca9d8STejun Heo 	 * Unbind from workqueue if @cwq is not frozen.  If frozen,
516502ca9d8STejun Heo 	 * thaw_workqueues() will either restart processing on this
517502ca9d8STejun Heo 	 * cpu or unbind if empty.  This keeps works queued while
518502ca9d8STejun Heo 	 * frozen fully ordered and flushable.
519502ca9d8STejun Heo 	 */
520502ca9d8STejun Heo 	if (likely(!(gcwq->flags & GCWQ_FREEZING))) {
521502ca9d8STejun Heo 		smp_wmb();	/* paired with cmpxchg() in __queue_work() */
522502ca9d8STejun Heo 		wq->single_cpu = NR_CPUS;
523502ca9d8STejun Heo 	}
524502ca9d8STejun Heo }
525502ca9d8STejun Heo 
5264690c4abSTejun Heo static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
5271da177e4SLinus Torvalds 			 struct work_struct *work)
5281da177e4SLinus Torvalds {
529502ca9d8STejun Heo 	struct global_cwq *gcwq;
530502ca9d8STejun Heo 	struct cpu_workqueue_struct *cwq;
5311e19ffc6STejun Heo 	struct list_head *worklist;
5321da177e4SLinus Torvalds 	unsigned long flags;
533502ca9d8STejun Heo 	bool arbitrate;
5341da177e4SLinus Torvalds 
535dc186ad7SThomas Gleixner 	debug_work_activate(work);
5361e19ffc6STejun Heo 
537*18aa9effSTejun Heo 	/*
538*18aa9effSTejun Heo 	 * Determine gcwq to use.  SINGLE_CPU is inherently
539*18aa9effSTejun Heo 	 * NON_REENTRANT, so test it first.
540*18aa9effSTejun Heo 	 */
541502ca9d8STejun Heo 	if (!(wq->flags & WQ_SINGLE_CPU)) {
542*18aa9effSTejun Heo 		struct global_cwq *last_gcwq;
543*18aa9effSTejun Heo 
544*18aa9effSTejun Heo 		/*
545*18aa9effSTejun Heo 		 * It's multi cpu.  If @wq is non-reentrant and @work
546*18aa9effSTejun Heo 		 * was previously on a different cpu, it might still
547*18aa9effSTejun Heo 		 * be running there, in which case the work needs to
548*18aa9effSTejun Heo 		 * be queued on that cpu to guarantee non-reentrance.
549*18aa9effSTejun Heo 		 */
550502ca9d8STejun Heo 		gcwq = get_gcwq(cpu);
551*18aa9effSTejun Heo 		if (wq->flags & WQ_NON_REENTRANT &&
552*18aa9effSTejun Heo 		    (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
553*18aa9effSTejun Heo 			struct worker *worker;
554*18aa9effSTejun Heo 
555*18aa9effSTejun Heo 			spin_lock_irqsave(&last_gcwq->lock, flags);
556*18aa9effSTejun Heo 
557*18aa9effSTejun Heo 			worker = find_worker_executing_work(last_gcwq, work);
558*18aa9effSTejun Heo 
559*18aa9effSTejun Heo 			if (worker && worker->current_cwq->wq == wq)
560*18aa9effSTejun Heo 				gcwq = last_gcwq;
561*18aa9effSTejun Heo 			else {
562*18aa9effSTejun Heo 				/* meh... not running there, queue here */
563*18aa9effSTejun Heo 				spin_unlock_irqrestore(&last_gcwq->lock, flags);
564*18aa9effSTejun Heo 				spin_lock_irqsave(&gcwq->lock, flags);
565*18aa9effSTejun Heo 			}
566*18aa9effSTejun Heo 		} else
5678b03ae3cSTejun Heo 			spin_lock_irqsave(&gcwq->lock, flags);
568502ca9d8STejun Heo 	} else {
569502ca9d8STejun Heo 		unsigned int req_cpu = cpu;
570502ca9d8STejun Heo 
571502ca9d8STejun Heo 		/*
572502ca9d8STejun Heo 		 * It's a bit more complex for single cpu workqueues.
573502ca9d8STejun Heo 		 * We first need to determine which cpu is going to be
574502ca9d8STejun Heo 		 * used.  If no cpu is currently serving this
575502ca9d8STejun Heo 		 * workqueue, arbitrate using atomic accesses to
576502ca9d8STejun Heo 		 * wq->single_cpu; otherwise, use the current one.
577502ca9d8STejun Heo 		 */
578502ca9d8STejun Heo 	retry:
579502ca9d8STejun Heo 		cpu = wq->single_cpu;
580502ca9d8STejun Heo 		arbitrate = cpu == NR_CPUS;
581502ca9d8STejun Heo 		if (arbitrate)
582502ca9d8STejun Heo 			cpu = req_cpu;
583502ca9d8STejun Heo 
584502ca9d8STejun Heo 		gcwq = get_gcwq(cpu);
585502ca9d8STejun Heo 		spin_lock_irqsave(&gcwq->lock, flags);
586502ca9d8STejun Heo 
587502ca9d8STejun Heo 		/*
588502ca9d8STejun Heo 		 * The following cmpxchg() is a full barrier paired
589502ca9d8STejun Heo 		 * with smp_wmb() in cwq_unbind_single_cpu() and
590502ca9d8STejun Heo 		 * guarantees that all changes to wq->st_* fields are
591502ca9d8STejun Heo 		 * visible on the new cpu after this point.
592502ca9d8STejun Heo 		 */
593502ca9d8STejun Heo 		if (arbitrate)
594502ca9d8STejun Heo 			cmpxchg(&wq->single_cpu, NR_CPUS, cpu);
595502ca9d8STejun Heo 
596502ca9d8STejun Heo 		if (unlikely(wq->single_cpu != cpu)) {
597502ca9d8STejun Heo 			spin_unlock_irqrestore(&gcwq->lock, flags);
598502ca9d8STejun Heo 			goto retry;
599502ca9d8STejun Heo 		}
600502ca9d8STejun Heo 	}
601502ca9d8STejun Heo 
602502ca9d8STejun Heo 	/* gcwq determined, get cwq and queue */
603502ca9d8STejun Heo 	cwq = get_cwq(gcwq->cpu, wq);
604502ca9d8STejun Heo 
6054690c4abSTejun Heo 	BUG_ON(!list_empty(&work->entry));
6061e19ffc6STejun Heo 
60773f53c4aSTejun Heo 	cwq->nr_in_flight[cwq->work_color]++;
6081e19ffc6STejun Heo 
6091e19ffc6STejun Heo 	if (likely(cwq->nr_active < cwq->max_active)) {
6101e19ffc6STejun Heo 		cwq->nr_active++;
6111e19ffc6STejun Heo 		worklist = &cwq->worklist;
6121e19ffc6STejun Heo 	} else
6131e19ffc6STejun Heo 		worklist = &cwq->delayed_works;
6141e19ffc6STejun Heo 
6151e19ffc6STejun Heo 	insert_work(cwq, work, worklist, work_color_to_flags(cwq->work_color));
6161e19ffc6STejun Heo 
6178b03ae3cSTejun Heo 	spin_unlock_irqrestore(&gcwq->lock, flags);
6181da177e4SLinus Torvalds }
6191da177e4SLinus Torvalds 
6200fcb78c2SRolf Eike Beer /**
6210fcb78c2SRolf Eike Beer  * queue_work - queue work on a workqueue
6220fcb78c2SRolf Eike Beer  * @wq: workqueue to use
6230fcb78c2SRolf Eike Beer  * @work: work to queue
6240fcb78c2SRolf Eike Beer  *
625057647fcSAlan Stern  * Returns 0 if @work was already on a queue, non-zero otherwise.
6261da177e4SLinus Torvalds  *
62700dfcaf7SOleg Nesterov  * We queue the work to the CPU on which it was submitted, but if the CPU dies
62800dfcaf7SOleg Nesterov  * it can be processed by another CPU.
6291da177e4SLinus Torvalds  */
6307ad5b3a5SHarvey Harrison int queue_work(struct workqueue_struct *wq, struct work_struct *work)
6311da177e4SLinus Torvalds {
632ef1ca236SOleg Nesterov 	int ret;
6331da177e4SLinus Torvalds 
634ef1ca236SOleg Nesterov 	ret = queue_work_on(get_cpu(), wq, work);
635a848e3b6SOleg Nesterov 	put_cpu();
636ef1ca236SOleg Nesterov 
6371da177e4SLinus Torvalds 	return ret;
6381da177e4SLinus Torvalds }
639ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_work);
6401da177e4SLinus Torvalds 
641c1a220e7SZhang Rui /**
642c1a220e7SZhang Rui  * queue_work_on - queue work on specific cpu
643c1a220e7SZhang Rui  * @cpu: CPU number to execute work on
644c1a220e7SZhang Rui  * @wq: workqueue to use
645c1a220e7SZhang Rui  * @work: work to queue
646c1a220e7SZhang Rui  *
647c1a220e7SZhang Rui  * Returns 0 if @work was already on a queue, non-zero otherwise.
648c1a220e7SZhang Rui  *
649c1a220e7SZhang Rui  * We queue the work to a specific CPU, the caller must ensure it
650c1a220e7SZhang Rui  * can't go away.
651c1a220e7SZhang Rui  */
652c1a220e7SZhang Rui int
653c1a220e7SZhang Rui queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
654c1a220e7SZhang Rui {
655c1a220e7SZhang Rui 	int ret = 0;
656c1a220e7SZhang Rui 
65722df02bbSTejun Heo 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
6584690c4abSTejun Heo 		__queue_work(cpu, wq, work);
659c1a220e7SZhang Rui 		ret = 1;
660c1a220e7SZhang Rui 	}
661c1a220e7SZhang Rui 	return ret;
662c1a220e7SZhang Rui }
663c1a220e7SZhang Rui EXPORT_SYMBOL_GPL(queue_work_on);
664c1a220e7SZhang Rui 
6656d141c3fSLi Zefan static void delayed_work_timer_fn(unsigned long __data)
6661da177e4SLinus Torvalds {
66752bad64dSDavid Howells 	struct delayed_work *dwork = (struct delayed_work *)__data;
6687a22ad75STejun Heo 	struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
6691da177e4SLinus Torvalds 
6704690c4abSTejun Heo 	__queue_work(smp_processor_id(), cwq->wq, &dwork->work);
6711da177e4SLinus Torvalds }
6721da177e4SLinus Torvalds 
6730fcb78c2SRolf Eike Beer /**
6740fcb78c2SRolf Eike Beer  * queue_delayed_work - queue work on a workqueue after delay
6750fcb78c2SRolf Eike Beer  * @wq: workqueue to use
676af9997e4SRandy Dunlap  * @dwork: delayable work to queue
6770fcb78c2SRolf Eike Beer  * @delay: number of jiffies to wait before queueing
6780fcb78c2SRolf Eike Beer  *
679057647fcSAlan Stern  * Returns 0 if @work was already on a queue, non-zero otherwise.
6800fcb78c2SRolf Eike Beer  */
6817ad5b3a5SHarvey Harrison int queue_delayed_work(struct workqueue_struct *wq,
68252bad64dSDavid Howells 			struct delayed_work *dwork, unsigned long delay)
6831da177e4SLinus Torvalds {
68452bad64dSDavid Howells 	if (delay == 0)
68563bc0362SOleg Nesterov 		return queue_work(wq, &dwork->work);
6861da177e4SLinus Torvalds 
68763bc0362SOleg Nesterov 	return queue_delayed_work_on(-1, wq, dwork, delay);
6881da177e4SLinus Torvalds }
689ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work);
6901da177e4SLinus Torvalds 
6910fcb78c2SRolf Eike Beer /**
6920fcb78c2SRolf Eike Beer  * queue_delayed_work_on - queue work on specific CPU after delay
6930fcb78c2SRolf Eike Beer  * @cpu: CPU number to execute work on
6940fcb78c2SRolf Eike Beer  * @wq: workqueue to use
695af9997e4SRandy Dunlap  * @dwork: work to queue
6960fcb78c2SRolf Eike Beer  * @delay: number of jiffies to wait before queueing
6970fcb78c2SRolf Eike Beer  *
698057647fcSAlan Stern  * Returns 0 if @work was already on a queue, non-zero otherwise.
6990fcb78c2SRolf Eike Beer  */
7007a6bc1cdSVenkatesh Pallipadi int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
70152bad64dSDavid Howells 			struct delayed_work *dwork, unsigned long delay)
7027a6bc1cdSVenkatesh Pallipadi {
7037a6bc1cdSVenkatesh Pallipadi 	int ret = 0;
70452bad64dSDavid Howells 	struct timer_list *timer = &dwork->timer;
70552bad64dSDavid Howells 	struct work_struct *work = &dwork->work;
7067a6bc1cdSVenkatesh Pallipadi 
70722df02bbSTejun Heo 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
7087a22ad75STejun Heo 		struct global_cwq *gcwq = get_work_gcwq(work);
7097a22ad75STejun Heo 		unsigned int lcpu = gcwq ? gcwq->cpu : raw_smp_processor_id();
7107a22ad75STejun Heo 
7117a6bc1cdSVenkatesh Pallipadi 		BUG_ON(timer_pending(timer));
7127a6bc1cdSVenkatesh Pallipadi 		BUG_ON(!list_empty(&work->entry));
7137a6bc1cdSVenkatesh Pallipadi 
7148a3e77ccSAndrew Liu 		timer_stats_timer_set_start_info(&dwork->timer);
7157a22ad75STejun Heo 		/*
7167a22ad75STejun Heo 		 * This stores cwq for the moment, for the timer_fn.
7177a22ad75STejun Heo 		 * Note that the work's gcwq is preserved to allow
7187a22ad75STejun Heo 		 * reentrance detection for delayed works.
7197a22ad75STejun Heo 		 */
7207a22ad75STejun Heo 		set_work_cwq(work, get_cwq(lcpu, wq), 0);
7217a6bc1cdSVenkatesh Pallipadi 		timer->expires = jiffies + delay;
72252bad64dSDavid Howells 		timer->data = (unsigned long)dwork;
7237a6bc1cdSVenkatesh Pallipadi 		timer->function = delayed_work_timer_fn;
72463bc0362SOleg Nesterov 
72563bc0362SOleg Nesterov 		if (unlikely(cpu >= 0))
7267a6bc1cdSVenkatesh Pallipadi 			add_timer_on(timer, cpu);
72763bc0362SOleg Nesterov 		else
72863bc0362SOleg Nesterov 			add_timer(timer);
7297a6bc1cdSVenkatesh Pallipadi 		ret = 1;
7307a6bc1cdSVenkatesh Pallipadi 	}
7317a6bc1cdSVenkatesh Pallipadi 	return ret;
7327a6bc1cdSVenkatesh Pallipadi }
733ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work_on);
7341da177e4SLinus Torvalds 
735c8e55f36STejun Heo /**
736c8e55f36STejun Heo  * worker_enter_idle - enter idle state
737c8e55f36STejun Heo  * @worker: worker which is entering idle state
738c8e55f36STejun Heo  *
739c8e55f36STejun Heo  * @worker is entering idle state.  Update stats and idle timer if
740c8e55f36STejun Heo  * necessary.
741c8e55f36STejun Heo  *
742c8e55f36STejun Heo  * LOCKING:
743c8e55f36STejun Heo  * spin_lock_irq(gcwq->lock).
744c8e55f36STejun Heo  */
745c8e55f36STejun Heo static void worker_enter_idle(struct worker *worker)
746c8e55f36STejun Heo {
747c8e55f36STejun Heo 	struct global_cwq *gcwq = worker->gcwq;
748c8e55f36STejun Heo 
749c8e55f36STejun Heo 	BUG_ON(worker->flags & WORKER_IDLE);
750c8e55f36STejun Heo 	BUG_ON(!list_empty(&worker->entry) &&
751c8e55f36STejun Heo 	       (worker->hentry.next || worker->hentry.pprev));
752c8e55f36STejun Heo 
753c8e55f36STejun Heo 	worker->flags |= WORKER_IDLE;
754c8e55f36STejun Heo 	gcwq->nr_idle++;
755c8e55f36STejun Heo 
756c8e55f36STejun Heo 	/* idle_list is LIFO */
757c8e55f36STejun Heo 	list_add(&worker->entry, &gcwq->idle_list);
758db7bccf4STejun Heo 
759db7bccf4STejun Heo 	if (unlikely(worker->flags & WORKER_ROGUE))
760db7bccf4STejun Heo 		wake_up_all(&gcwq->trustee_wait);
761c8e55f36STejun Heo }
762c8e55f36STejun Heo 
763c8e55f36STejun Heo /**
764c8e55f36STejun Heo  * worker_leave_idle - leave idle state
765c8e55f36STejun Heo  * @worker: worker which is leaving idle state
766c8e55f36STejun Heo  *
767c8e55f36STejun Heo  * @worker is leaving idle state.  Update stats.
768c8e55f36STejun Heo  *
769c8e55f36STejun Heo  * LOCKING:
770c8e55f36STejun Heo  * spin_lock_irq(gcwq->lock).
771c8e55f36STejun Heo  */
772c8e55f36STejun Heo static void worker_leave_idle(struct worker *worker)
773c8e55f36STejun Heo {
774c8e55f36STejun Heo 	struct global_cwq *gcwq = worker->gcwq;
775c8e55f36STejun Heo 
776c8e55f36STejun Heo 	BUG_ON(!(worker->flags & WORKER_IDLE));
777c8e55f36STejun Heo 	worker->flags &= ~WORKER_IDLE;
778c8e55f36STejun Heo 	gcwq->nr_idle--;
779c8e55f36STejun Heo 	list_del_init(&worker->entry);
780c8e55f36STejun Heo }
781c8e55f36STejun Heo 
782c34056a3STejun Heo static struct worker *alloc_worker(void)
783c34056a3STejun Heo {
784c34056a3STejun Heo 	struct worker *worker;
785c34056a3STejun Heo 
786c34056a3STejun Heo 	worker = kzalloc(sizeof(*worker), GFP_KERNEL);
787c8e55f36STejun Heo 	if (worker) {
788c8e55f36STejun Heo 		INIT_LIST_HEAD(&worker->entry);
789affee4b2STejun Heo 		INIT_LIST_HEAD(&worker->scheduled);
790c8e55f36STejun Heo 	}
791c34056a3STejun Heo 	return worker;
792c34056a3STejun Heo }
793c34056a3STejun Heo 
794c34056a3STejun Heo /**
795c34056a3STejun Heo  * create_worker - create a new workqueue worker
796c34056a3STejun Heo  * @cwq: cwq the new worker will belong to
797c34056a3STejun Heo  * @bind: whether to set affinity to @cpu or not
798c34056a3STejun Heo  *
799c34056a3STejun Heo  * Create a new worker which is bound to @cwq.  The returned worker
800c34056a3STejun Heo  * can be started by calling start_worker() or destroyed using
801c34056a3STejun Heo  * destroy_worker().
802c34056a3STejun Heo  *
803c34056a3STejun Heo  * CONTEXT:
804c34056a3STejun Heo  * Might sleep.  Does GFP_KERNEL allocations.
805c34056a3STejun Heo  *
806c34056a3STejun Heo  * RETURNS:
807c34056a3STejun Heo  * Pointer to the newly created worker.
808c34056a3STejun Heo  */
809c34056a3STejun Heo static struct worker *create_worker(struct cpu_workqueue_struct *cwq, bool bind)
810c34056a3STejun Heo {
8118b03ae3cSTejun Heo 	struct global_cwq *gcwq = cwq->gcwq;
812c34056a3STejun Heo 	int id = -1;
813c34056a3STejun Heo 	struct worker *worker = NULL;
814c34056a3STejun Heo 
8158b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
8168b03ae3cSTejun Heo 	while (ida_get_new(&gcwq->worker_ida, &id)) {
8178b03ae3cSTejun Heo 		spin_unlock_irq(&gcwq->lock);
8188b03ae3cSTejun Heo 		if (!ida_pre_get(&gcwq->worker_ida, GFP_KERNEL))
819c34056a3STejun Heo 			goto fail;
8208b03ae3cSTejun Heo 		spin_lock_irq(&gcwq->lock);
821c34056a3STejun Heo 	}
8228b03ae3cSTejun Heo 	spin_unlock_irq(&gcwq->lock);
823c34056a3STejun Heo 
824c34056a3STejun Heo 	worker = alloc_worker();
825c34056a3STejun Heo 	if (!worker)
826c34056a3STejun Heo 		goto fail;
827c34056a3STejun Heo 
8288b03ae3cSTejun Heo 	worker->gcwq = gcwq;
829c34056a3STejun Heo 	worker->cwq = cwq;
830c34056a3STejun Heo 	worker->id = id;
831c34056a3STejun Heo 
832c34056a3STejun Heo 	worker->task = kthread_create(worker_thread, worker, "kworker/%u:%d",
8338b03ae3cSTejun Heo 				      gcwq->cpu, id);
834c34056a3STejun Heo 	if (IS_ERR(worker->task))
835c34056a3STejun Heo 		goto fail;
836c34056a3STejun Heo 
837db7bccf4STejun Heo 	/*
838db7bccf4STejun Heo 	 * A rogue worker will become a regular one if CPU comes
839db7bccf4STejun Heo 	 * online later on.  Make sure every worker has
840db7bccf4STejun Heo 	 * PF_THREAD_BOUND set.
841db7bccf4STejun Heo 	 */
842c34056a3STejun Heo 	if (bind)
8438b03ae3cSTejun Heo 		kthread_bind(worker->task, gcwq->cpu);
844db7bccf4STejun Heo 	else
845db7bccf4STejun Heo 		worker->task->flags |= PF_THREAD_BOUND;
846c34056a3STejun Heo 
847c34056a3STejun Heo 	return worker;
848c34056a3STejun Heo fail:
849c34056a3STejun Heo 	if (id >= 0) {
8508b03ae3cSTejun Heo 		spin_lock_irq(&gcwq->lock);
8518b03ae3cSTejun Heo 		ida_remove(&gcwq->worker_ida, id);
8528b03ae3cSTejun Heo 		spin_unlock_irq(&gcwq->lock);
853c34056a3STejun Heo 	}
854c34056a3STejun Heo 	kfree(worker);
855c34056a3STejun Heo 	return NULL;
856c34056a3STejun Heo }
857c34056a3STejun Heo 
858c34056a3STejun Heo /**
859c34056a3STejun Heo  * start_worker - start a newly created worker
860c34056a3STejun Heo  * @worker: worker to start
861c34056a3STejun Heo  *
862c8e55f36STejun Heo  * Make the gcwq aware of @worker and start it.
863c34056a3STejun Heo  *
864c34056a3STejun Heo  * CONTEXT:
8658b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock).
866c34056a3STejun Heo  */
867c34056a3STejun Heo static void start_worker(struct worker *worker)
868c34056a3STejun Heo {
869c8e55f36STejun Heo 	worker->flags |= WORKER_STARTED;
870c8e55f36STejun Heo 	worker->gcwq->nr_workers++;
871c8e55f36STejun Heo 	worker_enter_idle(worker);
872c34056a3STejun Heo 	wake_up_process(worker->task);
873c34056a3STejun Heo }
874c34056a3STejun Heo 
875c34056a3STejun Heo /**
876c34056a3STejun Heo  * destroy_worker - destroy a workqueue worker
877c34056a3STejun Heo  * @worker: worker to be destroyed
878c34056a3STejun Heo  *
879c8e55f36STejun Heo  * Destroy @worker and adjust @gcwq stats accordingly.
880c8e55f36STejun Heo  *
881c8e55f36STejun Heo  * CONTEXT:
882c8e55f36STejun Heo  * spin_lock_irq(gcwq->lock) which is released and regrabbed.
883c34056a3STejun Heo  */
884c34056a3STejun Heo static void destroy_worker(struct worker *worker)
885c34056a3STejun Heo {
8868b03ae3cSTejun Heo 	struct global_cwq *gcwq = worker->gcwq;
887c34056a3STejun Heo 	int id = worker->id;
888c34056a3STejun Heo 
889c34056a3STejun Heo 	/* sanity check frenzy */
890c34056a3STejun Heo 	BUG_ON(worker->current_work);
891affee4b2STejun Heo 	BUG_ON(!list_empty(&worker->scheduled));
892c34056a3STejun Heo 
893c8e55f36STejun Heo 	if (worker->flags & WORKER_STARTED)
894c8e55f36STejun Heo 		gcwq->nr_workers--;
895c8e55f36STejun Heo 	if (worker->flags & WORKER_IDLE)
896c8e55f36STejun Heo 		gcwq->nr_idle--;
897c8e55f36STejun Heo 
898c8e55f36STejun Heo 	list_del_init(&worker->entry);
899c8e55f36STejun Heo 	worker->flags |= WORKER_DIE;
900c8e55f36STejun Heo 
901c8e55f36STejun Heo 	spin_unlock_irq(&gcwq->lock);
902c8e55f36STejun Heo 
903c34056a3STejun Heo 	kthread_stop(worker->task);
904c34056a3STejun Heo 	kfree(worker);
905c34056a3STejun Heo 
9068b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
9078b03ae3cSTejun Heo 	ida_remove(&gcwq->worker_ida, id);
908c34056a3STejun Heo }
909c34056a3STejun Heo 
910a62428c0STejun Heo /**
911affee4b2STejun Heo  * move_linked_works - move linked works to a list
912affee4b2STejun Heo  * @work: start of series of works to be scheduled
913affee4b2STejun Heo  * @head: target list to append @work to
914affee4b2STejun Heo  * @nextp: out paramter for nested worklist walking
915affee4b2STejun Heo  *
916affee4b2STejun Heo  * Schedule linked works starting from @work to @head.  Work series to
917affee4b2STejun Heo  * be scheduled starts at @work and includes any consecutive work with
918affee4b2STejun Heo  * WORK_STRUCT_LINKED set in its predecessor.
919affee4b2STejun Heo  *
920affee4b2STejun Heo  * If @nextp is not NULL, it's updated to point to the next work of
921affee4b2STejun Heo  * the last scheduled work.  This allows move_linked_works() to be
922affee4b2STejun Heo  * nested inside outer list_for_each_entry_safe().
923affee4b2STejun Heo  *
924affee4b2STejun Heo  * CONTEXT:
9258b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock).
926affee4b2STejun Heo  */
927affee4b2STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head,
928affee4b2STejun Heo 			      struct work_struct **nextp)
929affee4b2STejun Heo {
930affee4b2STejun Heo 	struct work_struct *n;
931affee4b2STejun Heo 
932affee4b2STejun Heo 	/*
933affee4b2STejun Heo 	 * Linked worklist will always end before the end of the list,
934affee4b2STejun Heo 	 * use NULL for list head.
935affee4b2STejun Heo 	 */
936affee4b2STejun Heo 	list_for_each_entry_safe_from(work, n, NULL, entry) {
937affee4b2STejun Heo 		list_move_tail(&work->entry, head);
938affee4b2STejun Heo 		if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
939affee4b2STejun Heo 			break;
940affee4b2STejun Heo 	}
941affee4b2STejun Heo 
942affee4b2STejun Heo 	/*
943affee4b2STejun Heo 	 * If we're already inside safe list traversal and have moved
944affee4b2STejun Heo 	 * multiple works to the scheduled queue, the next position
945affee4b2STejun Heo 	 * needs to be updated.
946affee4b2STejun Heo 	 */
947affee4b2STejun Heo 	if (nextp)
948affee4b2STejun Heo 		*nextp = n;
949affee4b2STejun Heo }
950affee4b2STejun Heo 
9511e19ffc6STejun Heo static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
9521e19ffc6STejun Heo {
9531e19ffc6STejun Heo 	struct work_struct *work = list_first_entry(&cwq->delayed_works,
9541e19ffc6STejun Heo 						    struct work_struct, entry);
9551e19ffc6STejun Heo 
9561e19ffc6STejun Heo 	move_linked_works(work, &cwq->worklist, NULL);
9571e19ffc6STejun Heo 	cwq->nr_active++;
9581e19ffc6STejun Heo }
9591e19ffc6STejun Heo 
960affee4b2STejun Heo /**
96173f53c4aSTejun Heo  * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
96273f53c4aSTejun Heo  * @cwq: cwq of interest
96373f53c4aSTejun Heo  * @color: color of work which left the queue
96473f53c4aSTejun Heo  *
96573f53c4aSTejun Heo  * A work either has completed or is removed from pending queue,
96673f53c4aSTejun Heo  * decrement nr_in_flight of its cwq and handle workqueue flushing.
96773f53c4aSTejun Heo  *
96873f53c4aSTejun Heo  * CONTEXT:
9698b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock).
97073f53c4aSTejun Heo  */
97173f53c4aSTejun Heo static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color)
97273f53c4aSTejun Heo {
97373f53c4aSTejun Heo 	/* ignore uncolored works */
97473f53c4aSTejun Heo 	if (color == WORK_NO_COLOR)
97573f53c4aSTejun Heo 		return;
97673f53c4aSTejun Heo 
97773f53c4aSTejun Heo 	cwq->nr_in_flight[color]--;
9781e19ffc6STejun Heo 	cwq->nr_active--;
9791e19ffc6STejun Heo 
980502ca9d8STejun Heo 	if (!list_empty(&cwq->delayed_works)) {
9811e19ffc6STejun Heo 		/* one down, submit a delayed one */
982502ca9d8STejun Heo 		if (cwq->nr_active < cwq->max_active)
9831e19ffc6STejun Heo 			cwq_activate_first_delayed(cwq);
984502ca9d8STejun Heo 	} else if (!cwq->nr_active && cwq->wq->flags & WQ_SINGLE_CPU) {
985502ca9d8STejun Heo 		/* this was the last work, unbind from single cpu */
986502ca9d8STejun Heo 		cwq_unbind_single_cpu(cwq);
987502ca9d8STejun Heo 	}
98873f53c4aSTejun Heo 
98973f53c4aSTejun Heo 	/* is flush in progress and are we at the flushing tip? */
99073f53c4aSTejun Heo 	if (likely(cwq->flush_color != color))
99173f53c4aSTejun Heo 		return;
99273f53c4aSTejun Heo 
99373f53c4aSTejun Heo 	/* are there still in-flight works? */
99473f53c4aSTejun Heo 	if (cwq->nr_in_flight[color])
99573f53c4aSTejun Heo 		return;
99673f53c4aSTejun Heo 
99773f53c4aSTejun Heo 	/* this cwq is done, clear flush_color */
99873f53c4aSTejun Heo 	cwq->flush_color = -1;
99973f53c4aSTejun Heo 
100073f53c4aSTejun Heo 	/*
100173f53c4aSTejun Heo 	 * If this was the last cwq, wake up the first flusher.  It
100273f53c4aSTejun Heo 	 * will handle the rest.
100373f53c4aSTejun Heo 	 */
100473f53c4aSTejun Heo 	if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
100573f53c4aSTejun Heo 		complete(&cwq->wq->first_flusher->done);
100673f53c4aSTejun Heo }
100773f53c4aSTejun Heo 
100873f53c4aSTejun Heo /**
1009a62428c0STejun Heo  * process_one_work - process single work
1010c34056a3STejun Heo  * @worker: self
1011a62428c0STejun Heo  * @work: work to process
1012a62428c0STejun Heo  *
1013a62428c0STejun Heo  * Process @work.  This function contains all the logics necessary to
1014a62428c0STejun Heo  * process a single work including synchronization against and
1015a62428c0STejun Heo  * interaction with other workers on the same cpu, queueing and
1016a62428c0STejun Heo  * flushing.  As long as context requirement is met, any worker can
1017a62428c0STejun Heo  * call this function to process a work.
1018a62428c0STejun Heo  *
1019a62428c0STejun Heo  * CONTEXT:
10208b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock) which is released and regrabbed.
1021a62428c0STejun Heo  */
1022c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work)
10231da177e4SLinus Torvalds {
1024c34056a3STejun Heo 	struct cpu_workqueue_struct *cwq = worker->cwq;
10258b03ae3cSTejun Heo 	struct global_cwq *gcwq = cwq->gcwq;
1026c8e55f36STejun Heo 	struct hlist_head *bwh = busy_worker_head(gcwq, work);
10276bb49e59SDavid Howells 	work_func_t f = work->func;
102873f53c4aSTejun Heo 	int work_color;
10294e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
10304e6045f1SJohannes Berg 	/*
1031a62428c0STejun Heo 	 * It is permissible to free the struct work_struct from
1032a62428c0STejun Heo 	 * inside the function that is called from it, this we need to
1033a62428c0STejun Heo 	 * take into account for lockdep too.  To avoid bogus "held
1034a62428c0STejun Heo 	 * lock freed" warnings as well as problems when looking into
1035a62428c0STejun Heo 	 * work->lockdep_map, make a copy and use that here.
10364e6045f1SJohannes Berg 	 */
10374e6045f1SJohannes Berg 	struct lockdep_map lockdep_map = work->lockdep_map;
10384e6045f1SJohannes Berg #endif
1039a62428c0STejun Heo 	/* claim and process */
1040dc186ad7SThomas Gleixner 	debug_work_deactivate(work);
1041c8e55f36STejun Heo 	hlist_add_head(&worker->hentry, bwh);
1042c34056a3STejun Heo 	worker->current_work = work;
10438cca0eeaSTejun Heo 	worker->current_cwq = cwq;
104473f53c4aSTejun Heo 	work_color = get_work_color(work);
10457a22ad75STejun Heo 
10467a22ad75STejun Heo 	BUG_ON(get_work_cwq(work) != cwq);
10477a22ad75STejun Heo 	/* record the current cpu number in the work data and dequeue */
10487a22ad75STejun Heo 	set_work_cpu(work, gcwq->cpu);
1049a62428c0STejun Heo 	list_del_init(&work->entry);
1050a62428c0STejun Heo 
10518b03ae3cSTejun Heo 	spin_unlock_irq(&gcwq->lock);
10521da177e4SLinus Torvalds 
105323b2e599SOleg Nesterov 	work_clear_pending(work);
10543295f0efSIngo Molnar 	lock_map_acquire(&cwq->wq->lockdep_map);
10553295f0efSIngo Molnar 	lock_map_acquire(&lockdep_map);
105665f27f38SDavid Howells 	f(work);
10573295f0efSIngo Molnar 	lock_map_release(&lockdep_map);
10583295f0efSIngo Molnar 	lock_map_release(&cwq->wq->lockdep_map);
10591da177e4SLinus Torvalds 
1060d5abe669SPeter Zijlstra 	if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
1061d5abe669SPeter Zijlstra 		printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
1062d5abe669SPeter Zijlstra 		       "%s/0x%08x/%d\n",
1063a62428c0STejun Heo 		       current->comm, preempt_count(), task_pid_nr(current));
1064d5abe669SPeter Zijlstra 		printk(KERN_ERR "    last function: ");
1065d5abe669SPeter Zijlstra 		print_symbol("%s\n", (unsigned long)f);
1066d5abe669SPeter Zijlstra 		debug_show_held_locks(current);
1067d5abe669SPeter Zijlstra 		dump_stack();
1068d5abe669SPeter Zijlstra 	}
1069d5abe669SPeter Zijlstra 
10708b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
1071a62428c0STejun Heo 
1072a62428c0STejun Heo 	/* we're done with it, release */
1073c8e55f36STejun Heo 	hlist_del_init(&worker->hentry);
1074c34056a3STejun Heo 	worker->current_work = NULL;
10758cca0eeaSTejun Heo 	worker->current_cwq = NULL;
107673f53c4aSTejun Heo 	cwq_dec_nr_in_flight(cwq, work_color);
10771da177e4SLinus Torvalds }
1078a62428c0STejun Heo 
1079affee4b2STejun Heo /**
1080affee4b2STejun Heo  * process_scheduled_works - process scheduled works
1081affee4b2STejun Heo  * @worker: self
1082affee4b2STejun Heo  *
1083affee4b2STejun Heo  * Process all scheduled works.  Please note that the scheduled list
1084affee4b2STejun Heo  * may change while processing a work, so this function repeatedly
1085affee4b2STejun Heo  * fetches a work from the top and executes it.
1086affee4b2STejun Heo  *
1087affee4b2STejun Heo  * CONTEXT:
10888b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1089affee4b2STejun Heo  * multiple times.
1090affee4b2STejun Heo  */
1091affee4b2STejun Heo static void process_scheduled_works(struct worker *worker)
1092a62428c0STejun Heo {
1093affee4b2STejun Heo 	while (!list_empty(&worker->scheduled)) {
1094affee4b2STejun Heo 		struct work_struct *work = list_first_entry(&worker->scheduled,
1095a62428c0STejun Heo 						struct work_struct, entry);
1096c34056a3STejun Heo 		process_one_work(worker, work);
1097a62428c0STejun Heo 	}
10981da177e4SLinus Torvalds }
10991da177e4SLinus Torvalds 
11004690c4abSTejun Heo /**
11014690c4abSTejun Heo  * worker_thread - the worker thread function
1102c34056a3STejun Heo  * @__worker: self
11034690c4abSTejun Heo  *
11044690c4abSTejun Heo  * The cwq worker thread function.
11054690c4abSTejun Heo  */
1106c34056a3STejun Heo static int worker_thread(void *__worker)
11071da177e4SLinus Torvalds {
1108c34056a3STejun Heo 	struct worker *worker = __worker;
11098b03ae3cSTejun Heo 	struct global_cwq *gcwq = worker->gcwq;
1110c34056a3STejun Heo 	struct cpu_workqueue_struct *cwq = worker->cwq;
11111da177e4SLinus Torvalds 
1112c8e55f36STejun Heo woke_up:
11138b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
1114affee4b2STejun Heo 
1115c8e55f36STejun Heo 	/* DIE can be set only while we're idle, checking here is enough */
1116c8e55f36STejun Heo 	if (worker->flags & WORKER_DIE) {
1117c8e55f36STejun Heo 		spin_unlock_irq(&gcwq->lock);
1118c8e55f36STejun Heo 		return 0;
1119c8e55f36STejun Heo 	}
1120c8e55f36STejun Heo 
1121c8e55f36STejun Heo 	worker_leave_idle(worker);
1122db7bccf4STejun Heo recheck:
1123c8e55f36STejun Heo 	/*
1124c8e55f36STejun Heo 	 * ->scheduled list can only be filled while a worker is
1125c8e55f36STejun Heo 	 * preparing to process a work or actually processing it.
1126c8e55f36STejun Heo 	 * Make sure nobody diddled with it while I was sleeping.
1127c8e55f36STejun Heo 	 */
1128c8e55f36STejun Heo 	BUG_ON(!list_empty(&worker->scheduled));
1129c8e55f36STejun Heo 
1130affee4b2STejun Heo 	while (!list_empty(&cwq->worklist)) {
1131affee4b2STejun Heo 		struct work_struct *work =
1132affee4b2STejun Heo 			list_first_entry(&cwq->worklist,
1133affee4b2STejun Heo 					 struct work_struct, entry);
1134affee4b2STejun Heo 
1135db7bccf4STejun Heo 		/*
1136db7bccf4STejun Heo 		 * The following is a rather inefficient way to close
1137db7bccf4STejun Heo 		 * race window against cpu hotplug operations.  Will
1138db7bccf4STejun Heo 		 * be replaced soon.
1139db7bccf4STejun Heo 		 */
1140db7bccf4STejun Heo 		if (unlikely(!(worker->flags & WORKER_ROGUE) &&
1141db7bccf4STejun Heo 			     !cpumask_equal(&worker->task->cpus_allowed,
1142db7bccf4STejun Heo 					    get_cpu_mask(gcwq->cpu)))) {
1143db7bccf4STejun Heo 			spin_unlock_irq(&gcwq->lock);
1144db7bccf4STejun Heo 			set_cpus_allowed_ptr(worker->task,
1145db7bccf4STejun Heo 					     get_cpu_mask(gcwq->cpu));
1146db7bccf4STejun Heo 			cpu_relax();
1147db7bccf4STejun Heo 			spin_lock_irq(&gcwq->lock);
1148db7bccf4STejun Heo 			goto recheck;
1149db7bccf4STejun Heo 		}
1150db7bccf4STejun Heo 
1151c8e55f36STejun Heo 		if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
1152affee4b2STejun Heo 			/* optimization path, not strictly necessary */
1153affee4b2STejun Heo 			process_one_work(worker, work);
1154affee4b2STejun Heo 			if (unlikely(!list_empty(&worker->scheduled)))
1155affee4b2STejun Heo 				process_scheduled_works(worker);
1156affee4b2STejun Heo 		} else {
1157c8e55f36STejun Heo 			move_linked_works(work, &worker->scheduled, NULL);
1158affee4b2STejun Heo 			process_scheduled_works(worker);
1159affee4b2STejun Heo 		}
1160affee4b2STejun Heo 	}
1161affee4b2STejun Heo 
1162c8e55f36STejun Heo 	/*
1163c8e55f36STejun Heo 	 * gcwq->lock is held and there's no work to process, sleep.
1164c8e55f36STejun Heo 	 * Workers are woken up only while holding gcwq->lock, so
1165c8e55f36STejun Heo 	 * setting the current state before releasing gcwq->lock is
1166c8e55f36STejun Heo 	 * enough to prevent losing any event.
1167c8e55f36STejun Heo 	 */
1168c8e55f36STejun Heo 	worker_enter_idle(worker);
1169c8e55f36STejun Heo 	__set_current_state(TASK_INTERRUPTIBLE);
11708b03ae3cSTejun Heo 	spin_unlock_irq(&gcwq->lock);
1171c8e55f36STejun Heo 	schedule();
1172c8e55f36STejun Heo 	goto woke_up;
11731da177e4SLinus Torvalds }
11741da177e4SLinus Torvalds 
1175fc2e4d70SOleg Nesterov struct wq_barrier {
1176fc2e4d70SOleg Nesterov 	struct work_struct	work;
1177fc2e4d70SOleg Nesterov 	struct completion	done;
1178fc2e4d70SOleg Nesterov };
1179fc2e4d70SOleg Nesterov 
1180fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work)
1181fc2e4d70SOleg Nesterov {
1182fc2e4d70SOleg Nesterov 	struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
1183fc2e4d70SOleg Nesterov 	complete(&barr->done);
1184fc2e4d70SOleg Nesterov }
1185fc2e4d70SOleg Nesterov 
11864690c4abSTejun Heo /**
11874690c4abSTejun Heo  * insert_wq_barrier - insert a barrier work
11884690c4abSTejun Heo  * @cwq: cwq to insert barrier into
11894690c4abSTejun Heo  * @barr: wq_barrier to insert
1190affee4b2STejun Heo  * @target: target work to attach @barr to
1191affee4b2STejun Heo  * @worker: worker currently executing @target, NULL if @target is not executing
11924690c4abSTejun Heo  *
1193affee4b2STejun Heo  * @barr is linked to @target such that @barr is completed only after
1194affee4b2STejun Heo  * @target finishes execution.  Please note that the ordering
1195affee4b2STejun Heo  * guarantee is observed only with respect to @target and on the local
1196affee4b2STejun Heo  * cpu.
1197affee4b2STejun Heo  *
1198affee4b2STejun Heo  * Currently, a queued barrier can't be canceled.  This is because
1199affee4b2STejun Heo  * try_to_grab_pending() can't determine whether the work to be
1200affee4b2STejun Heo  * grabbed is at the head of the queue and thus can't clear LINKED
1201affee4b2STejun Heo  * flag of the previous work while there must be a valid next work
1202affee4b2STejun Heo  * after a work with LINKED flag set.
1203affee4b2STejun Heo  *
1204affee4b2STejun Heo  * Note that when @worker is non-NULL, @target may be modified
1205affee4b2STejun Heo  * underneath us, so we can't reliably determine cwq from @target.
12064690c4abSTejun Heo  *
12074690c4abSTejun Heo  * CONTEXT:
12088b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock).
12094690c4abSTejun Heo  */
121083c22520SOleg Nesterov static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
1211affee4b2STejun Heo 			      struct wq_barrier *barr,
1212affee4b2STejun Heo 			      struct work_struct *target, struct worker *worker)
1213fc2e4d70SOleg Nesterov {
1214affee4b2STejun Heo 	struct list_head *head;
1215affee4b2STejun Heo 	unsigned int linked = 0;
1216affee4b2STejun Heo 
1217dc186ad7SThomas Gleixner 	/*
12188b03ae3cSTejun Heo 	 * debugobject calls are safe here even with gcwq->lock locked
1219dc186ad7SThomas Gleixner 	 * as we know for sure that this will not trigger any of the
1220dc186ad7SThomas Gleixner 	 * checks and call back into the fixup functions where we
1221dc186ad7SThomas Gleixner 	 * might deadlock.
1222dc186ad7SThomas Gleixner 	 */
1223dc186ad7SThomas Gleixner 	INIT_WORK_ON_STACK(&barr->work, wq_barrier_func);
122422df02bbSTejun Heo 	__set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
1225fc2e4d70SOleg Nesterov 	init_completion(&barr->done);
122683c22520SOleg Nesterov 
1227affee4b2STejun Heo 	/*
1228affee4b2STejun Heo 	 * If @target is currently being executed, schedule the
1229affee4b2STejun Heo 	 * barrier to the worker; otherwise, put it after @target.
1230affee4b2STejun Heo 	 */
1231affee4b2STejun Heo 	if (worker)
1232affee4b2STejun Heo 		head = worker->scheduled.next;
1233affee4b2STejun Heo 	else {
1234affee4b2STejun Heo 		unsigned long *bits = work_data_bits(target);
1235affee4b2STejun Heo 
1236affee4b2STejun Heo 		head = target->entry.next;
1237affee4b2STejun Heo 		/* there can already be other linked works, inherit and set */
1238affee4b2STejun Heo 		linked = *bits & WORK_STRUCT_LINKED;
1239affee4b2STejun Heo 		__set_bit(WORK_STRUCT_LINKED_BIT, bits);
1240affee4b2STejun Heo 	}
1241affee4b2STejun Heo 
1242dc186ad7SThomas Gleixner 	debug_work_activate(&barr->work);
1243affee4b2STejun Heo 	insert_work(cwq, &barr->work, head,
1244affee4b2STejun Heo 		    work_color_to_flags(WORK_NO_COLOR) | linked);
1245fc2e4d70SOleg Nesterov }
1246fc2e4d70SOleg Nesterov 
124773f53c4aSTejun Heo /**
124873f53c4aSTejun Heo  * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
124973f53c4aSTejun Heo  * @wq: workqueue being flushed
125073f53c4aSTejun Heo  * @flush_color: new flush color, < 0 for no-op
125173f53c4aSTejun Heo  * @work_color: new work color, < 0 for no-op
125273f53c4aSTejun Heo  *
125373f53c4aSTejun Heo  * Prepare cwqs for workqueue flushing.
125473f53c4aSTejun Heo  *
125573f53c4aSTejun Heo  * If @flush_color is non-negative, flush_color on all cwqs should be
125673f53c4aSTejun Heo  * -1.  If no cwq has in-flight commands at the specified color, all
125773f53c4aSTejun Heo  * cwq->flush_color's stay at -1 and %false is returned.  If any cwq
125873f53c4aSTejun Heo  * has in flight commands, its cwq->flush_color is set to
125973f53c4aSTejun Heo  * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
126073f53c4aSTejun Heo  * wakeup logic is armed and %true is returned.
126173f53c4aSTejun Heo  *
126273f53c4aSTejun Heo  * The caller should have initialized @wq->first_flusher prior to
126373f53c4aSTejun Heo  * calling this function with non-negative @flush_color.  If
126473f53c4aSTejun Heo  * @flush_color is negative, no flush color update is done and %false
126573f53c4aSTejun Heo  * is returned.
126673f53c4aSTejun Heo  *
126773f53c4aSTejun Heo  * If @work_color is non-negative, all cwqs should have the same
126873f53c4aSTejun Heo  * work_color which is previous to @work_color and all will be
126973f53c4aSTejun Heo  * advanced to @work_color.
127073f53c4aSTejun Heo  *
127173f53c4aSTejun Heo  * CONTEXT:
127273f53c4aSTejun Heo  * mutex_lock(wq->flush_mutex).
127373f53c4aSTejun Heo  *
127473f53c4aSTejun Heo  * RETURNS:
127573f53c4aSTejun Heo  * %true if @flush_color >= 0 and there's something to flush.  %false
127673f53c4aSTejun Heo  * otherwise.
127773f53c4aSTejun Heo  */
127873f53c4aSTejun Heo static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
127973f53c4aSTejun Heo 				      int flush_color, int work_color)
12801da177e4SLinus Torvalds {
128173f53c4aSTejun Heo 	bool wait = false;
128273f53c4aSTejun Heo 	unsigned int cpu;
12831da177e4SLinus Torvalds 
128473f53c4aSTejun Heo 	if (flush_color >= 0) {
128573f53c4aSTejun Heo 		BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
128673f53c4aSTejun Heo 		atomic_set(&wq->nr_cwqs_to_flush, 1);
128773f53c4aSTejun Heo 	}
128873f53c4aSTejun Heo 
128973f53c4aSTejun Heo 	for_each_possible_cpu(cpu) {
129073f53c4aSTejun Heo 		struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
12918b03ae3cSTejun Heo 		struct global_cwq *gcwq = cwq->gcwq;
12922355b70fSLai Jiangshan 
12938b03ae3cSTejun Heo 		spin_lock_irq(&gcwq->lock);
129473f53c4aSTejun Heo 
129573f53c4aSTejun Heo 		if (flush_color >= 0) {
129673f53c4aSTejun Heo 			BUG_ON(cwq->flush_color != -1);
129773f53c4aSTejun Heo 
129873f53c4aSTejun Heo 			if (cwq->nr_in_flight[flush_color]) {
129973f53c4aSTejun Heo 				cwq->flush_color = flush_color;
130073f53c4aSTejun Heo 				atomic_inc(&wq->nr_cwqs_to_flush);
130173f53c4aSTejun Heo 				wait = true;
130283c22520SOleg Nesterov 			}
130373f53c4aSTejun Heo 		}
130473f53c4aSTejun Heo 
130573f53c4aSTejun Heo 		if (work_color >= 0) {
130673f53c4aSTejun Heo 			BUG_ON(work_color != work_next_color(cwq->work_color));
130773f53c4aSTejun Heo 			cwq->work_color = work_color;
130873f53c4aSTejun Heo 		}
130973f53c4aSTejun Heo 
13108b03ae3cSTejun Heo 		spin_unlock_irq(&gcwq->lock);
1311dc186ad7SThomas Gleixner 	}
131214441960SOleg Nesterov 
131373f53c4aSTejun Heo 	if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
131473f53c4aSTejun Heo 		complete(&wq->first_flusher->done);
131573f53c4aSTejun Heo 
131673f53c4aSTejun Heo 	return wait;
131783c22520SOleg Nesterov }
13181da177e4SLinus Torvalds 
13190fcb78c2SRolf Eike Beer /**
13201da177e4SLinus Torvalds  * flush_workqueue - ensure that any scheduled work has run to completion.
13210fcb78c2SRolf Eike Beer  * @wq: workqueue to flush
13221da177e4SLinus Torvalds  *
13231da177e4SLinus Torvalds  * Forces execution of the workqueue and blocks until its completion.
13241da177e4SLinus Torvalds  * This is typically used in driver shutdown handlers.
13251da177e4SLinus Torvalds  *
1326fc2e4d70SOleg Nesterov  * We sleep until all works which were queued on entry have been handled,
1327fc2e4d70SOleg Nesterov  * but we are not livelocked by new incoming ones.
13281da177e4SLinus Torvalds  */
13297ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq)
13301da177e4SLinus Torvalds {
133173f53c4aSTejun Heo 	struct wq_flusher this_flusher = {
133273f53c4aSTejun Heo 		.list = LIST_HEAD_INIT(this_flusher.list),
133373f53c4aSTejun Heo 		.flush_color = -1,
133473f53c4aSTejun Heo 		.done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
133573f53c4aSTejun Heo 	};
133673f53c4aSTejun Heo 	int next_color;
1337b1f4ec17SOleg Nesterov 
13383295f0efSIngo Molnar 	lock_map_acquire(&wq->lockdep_map);
13393295f0efSIngo Molnar 	lock_map_release(&wq->lockdep_map);
134073f53c4aSTejun Heo 
134173f53c4aSTejun Heo 	mutex_lock(&wq->flush_mutex);
134273f53c4aSTejun Heo 
134373f53c4aSTejun Heo 	/*
134473f53c4aSTejun Heo 	 * Start-to-wait phase
134573f53c4aSTejun Heo 	 */
134673f53c4aSTejun Heo 	next_color = work_next_color(wq->work_color);
134773f53c4aSTejun Heo 
134873f53c4aSTejun Heo 	if (next_color != wq->flush_color) {
134973f53c4aSTejun Heo 		/*
135073f53c4aSTejun Heo 		 * Color space is not full.  The current work_color
135173f53c4aSTejun Heo 		 * becomes our flush_color and work_color is advanced
135273f53c4aSTejun Heo 		 * by one.
135373f53c4aSTejun Heo 		 */
135473f53c4aSTejun Heo 		BUG_ON(!list_empty(&wq->flusher_overflow));
135573f53c4aSTejun Heo 		this_flusher.flush_color = wq->work_color;
135673f53c4aSTejun Heo 		wq->work_color = next_color;
135773f53c4aSTejun Heo 
135873f53c4aSTejun Heo 		if (!wq->first_flusher) {
135973f53c4aSTejun Heo 			/* no flush in progress, become the first flusher */
136073f53c4aSTejun Heo 			BUG_ON(wq->flush_color != this_flusher.flush_color);
136173f53c4aSTejun Heo 
136273f53c4aSTejun Heo 			wq->first_flusher = &this_flusher;
136373f53c4aSTejun Heo 
136473f53c4aSTejun Heo 			if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
136573f53c4aSTejun Heo 						       wq->work_color)) {
136673f53c4aSTejun Heo 				/* nothing to flush, done */
136773f53c4aSTejun Heo 				wq->flush_color = next_color;
136873f53c4aSTejun Heo 				wq->first_flusher = NULL;
136973f53c4aSTejun Heo 				goto out_unlock;
137073f53c4aSTejun Heo 			}
137173f53c4aSTejun Heo 		} else {
137273f53c4aSTejun Heo 			/* wait in queue */
137373f53c4aSTejun Heo 			BUG_ON(wq->flush_color == this_flusher.flush_color);
137473f53c4aSTejun Heo 			list_add_tail(&this_flusher.list, &wq->flusher_queue);
137573f53c4aSTejun Heo 			flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
137673f53c4aSTejun Heo 		}
137773f53c4aSTejun Heo 	} else {
137873f53c4aSTejun Heo 		/*
137973f53c4aSTejun Heo 		 * Oops, color space is full, wait on overflow queue.
138073f53c4aSTejun Heo 		 * The next flush completion will assign us
138173f53c4aSTejun Heo 		 * flush_color and transfer to flusher_queue.
138273f53c4aSTejun Heo 		 */
138373f53c4aSTejun Heo 		list_add_tail(&this_flusher.list, &wq->flusher_overflow);
138473f53c4aSTejun Heo 	}
138573f53c4aSTejun Heo 
138673f53c4aSTejun Heo 	mutex_unlock(&wq->flush_mutex);
138773f53c4aSTejun Heo 
138873f53c4aSTejun Heo 	wait_for_completion(&this_flusher.done);
138973f53c4aSTejun Heo 
139073f53c4aSTejun Heo 	/*
139173f53c4aSTejun Heo 	 * Wake-up-and-cascade phase
139273f53c4aSTejun Heo 	 *
139373f53c4aSTejun Heo 	 * First flushers are responsible for cascading flushes and
139473f53c4aSTejun Heo 	 * handling overflow.  Non-first flushers can simply return.
139573f53c4aSTejun Heo 	 */
139673f53c4aSTejun Heo 	if (wq->first_flusher != &this_flusher)
139773f53c4aSTejun Heo 		return;
139873f53c4aSTejun Heo 
139973f53c4aSTejun Heo 	mutex_lock(&wq->flush_mutex);
140073f53c4aSTejun Heo 
140173f53c4aSTejun Heo 	wq->first_flusher = NULL;
140273f53c4aSTejun Heo 
140373f53c4aSTejun Heo 	BUG_ON(!list_empty(&this_flusher.list));
140473f53c4aSTejun Heo 	BUG_ON(wq->flush_color != this_flusher.flush_color);
140573f53c4aSTejun Heo 
140673f53c4aSTejun Heo 	while (true) {
140773f53c4aSTejun Heo 		struct wq_flusher *next, *tmp;
140873f53c4aSTejun Heo 
140973f53c4aSTejun Heo 		/* complete all the flushers sharing the current flush color */
141073f53c4aSTejun Heo 		list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
141173f53c4aSTejun Heo 			if (next->flush_color != wq->flush_color)
141273f53c4aSTejun Heo 				break;
141373f53c4aSTejun Heo 			list_del_init(&next->list);
141473f53c4aSTejun Heo 			complete(&next->done);
141573f53c4aSTejun Heo 		}
141673f53c4aSTejun Heo 
141773f53c4aSTejun Heo 		BUG_ON(!list_empty(&wq->flusher_overflow) &&
141873f53c4aSTejun Heo 		       wq->flush_color != work_next_color(wq->work_color));
141973f53c4aSTejun Heo 
142073f53c4aSTejun Heo 		/* this flush_color is finished, advance by one */
142173f53c4aSTejun Heo 		wq->flush_color = work_next_color(wq->flush_color);
142273f53c4aSTejun Heo 
142373f53c4aSTejun Heo 		/* one color has been freed, handle overflow queue */
142473f53c4aSTejun Heo 		if (!list_empty(&wq->flusher_overflow)) {
142573f53c4aSTejun Heo 			/*
142673f53c4aSTejun Heo 			 * Assign the same color to all overflowed
142773f53c4aSTejun Heo 			 * flushers, advance work_color and append to
142873f53c4aSTejun Heo 			 * flusher_queue.  This is the start-to-wait
142973f53c4aSTejun Heo 			 * phase for these overflowed flushers.
143073f53c4aSTejun Heo 			 */
143173f53c4aSTejun Heo 			list_for_each_entry(tmp, &wq->flusher_overflow, list)
143273f53c4aSTejun Heo 				tmp->flush_color = wq->work_color;
143373f53c4aSTejun Heo 
143473f53c4aSTejun Heo 			wq->work_color = work_next_color(wq->work_color);
143573f53c4aSTejun Heo 
143673f53c4aSTejun Heo 			list_splice_tail_init(&wq->flusher_overflow,
143773f53c4aSTejun Heo 					      &wq->flusher_queue);
143873f53c4aSTejun Heo 			flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
143973f53c4aSTejun Heo 		}
144073f53c4aSTejun Heo 
144173f53c4aSTejun Heo 		if (list_empty(&wq->flusher_queue)) {
144273f53c4aSTejun Heo 			BUG_ON(wq->flush_color != wq->work_color);
144373f53c4aSTejun Heo 			break;
144473f53c4aSTejun Heo 		}
144573f53c4aSTejun Heo 
144673f53c4aSTejun Heo 		/*
144773f53c4aSTejun Heo 		 * Need to flush more colors.  Make the next flusher
144873f53c4aSTejun Heo 		 * the new first flusher and arm cwqs.
144973f53c4aSTejun Heo 		 */
145073f53c4aSTejun Heo 		BUG_ON(wq->flush_color == wq->work_color);
145173f53c4aSTejun Heo 		BUG_ON(wq->flush_color != next->flush_color);
145273f53c4aSTejun Heo 
145373f53c4aSTejun Heo 		list_del_init(&next->list);
145473f53c4aSTejun Heo 		wq->first_flusher = next;
145573f53c4aSTejun Heo 
145673f53c4aSTejun Heo 		if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
145773f53c4aSTejun Heo 			break;
145873f53c4aSTejun Heo 
145973f53c4aSTejun Heo 		/*
146073f53c4aSTejun Heo 		 * Meh... this color is already done, clear first
146173f53c4aSTejun Heo 		 * flusher and repeat cascading.
146273f53c4aSTejun Heo 		 */
146373f53c4aSTejun Heo 		wq->first_flusher = NULL;
146473f53c4aSTejun Heo 	}
146573f53c4aSTejun Heo 
146673f53c4aSTejun Heo out_unlock:
146773f53c4aSTejun Heo 	mutex_unlock(&wq->flush_mutex);
14681da177e4SLinus Torvalds }
1469ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(flush_workqueue);
14701da177e4SLinus Torvalds 
1471db700897SOleg Nesterov /**
1472db700897SOleg Nesterov  * flush_work - block until a work_struct's callback has terminated
1473db700897SOleg Nesterov  * @work: the work which is to be flushed
1474db700897SOleg Nesterov  *
1475a67da70dSOleg Nesterov  * Returns false if @work has already terminated.
1476a67da70dSOleg Nesterov  *
1477db700897SOleg Nesterov  * It is expected that, prior to calling flush_work(), the caller has
1478db700897SOleg Nesterov  * arranged for the work to not be requeued, otherwise it doesn't make
1479db700897SOleg Nesterov  * sense to use this function.
1480db700897SOleg Nesterov  */
1481db700897SOleg Nesterov int flush_work(struct work_struct *work)
1482db700897SOleg Nesterov {
1483affee4b2STejun Heo 	struct worker *worker = NULL;
14848b03ae3cSTejun Heo 	struct global_cwq *gcwq;
14857a22ad75STejun Heo 	struct cpu_workqueue_struct *cwq;
1486db700897SOleg Nesterov 	struct wq_barrier barr;
1487db700897SOleg Nesterov 
1488db700897SOleg Nesterov 	might_sleep();
14897a22ad75STejun Heo 	gcwq = get_work_gcwq(work);
14907a22ad75STejun Heo 	if (!gcwq)
1491db700897SOleg Nesterov 		return 0;
1492a67da70dSOleg Nesterov 
14938b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
1494db700897SOleg Nesterov 	if (!list_empty(&work->entry)) {
1495db700897SOleg Nesterov 		/*
1496db700897SOleg Nesterov 		 * See the comment near try_to_grab_pending()->smp_rmb().
14977a22ad75STejun Heo 		 * If it was re-queued to a different gcwq under us, we
14987a22ad75STejun Heo 		 * are not going to wait.
1499db700897SOleg Nesterov 		 */
1500db700897SOleg Nesterov 		smp_rmb();
15017a22ad75STejun Heo 		cwq = get_work_cwq(work);
15027a22ad75STejun Heo 		if (unlikely(!cwq || gcwq != cwq->gcwq))
15034690c4abSTejun Heo 			goto already_gone;
1504db700897SOleg Nesterov 	} else {
15057a22ad75STejun Heo 		worker = find_worker_executing_work(gcwq, work);
1506affee4b2STejun Heo 		if (!worker)
15074690c4abSTejun Heo 			goto already_gone;
15087a22ad75STejun Heo 		cwq = worker->current_cwq;
1509db700897SOleg Nesterov 	}
1510db700897SOleg Nesterov 
1511affee4b2STejun Heo 	insert_wq_barrier(cwq, &barr, work, worker);
15128b03ae3cSTejun Heo 	spin_unlock_irq(&gcwq->lock);
15137a22ad75STejun Heo 
15147a22ad75STejun Heo 	lock_map_acquire(&cwq->wq->lockdep_map);
15157a22ad75STejun Heo 	lock_map_release(&cwq->wq->lockdep_map);
15167a22ad75STejun Heo 
1517db700897SOleg Nesterov 	wait_for_completion(&barr.done);
1518dc186ad7SThomas Gleixner 	destroy_work_on_stack(&barr.work);
1519db700897SOleg Nesterov 	return 1;
15204690c4abSTejun Heo already_gone:
15218b03ae3cSTejun Heo 	spin_unlock_irq(&gcwq->lock);
15224690c4abSTejun Heo 	return 0;
1523db700897SOleg Nesterov }
1524db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work);
1525db700897SOleg Nesterov 
15266e84d644SOleg Nesterov /*
15271f1f642eSOleg Nesterov  * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
15286e84d644SOleg Nesterov  * so this work can't be re-armed in any way.
15296e84d644SOleg Nesterov  */
15306e84d644SOleg Nesterov static int try_to_grab_pending(struct work_struct *work)
15316e84d644SOleg Nesterov {
15328b03ae3cSTejun Heo 	struct global_cwq *gcwq;
15331f1f642eSOleg Nesterov 	int ret = -1;
15346e84d644SOleg Nesterov 
153522df02bbSTejun Heo 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
15361f1f642eSOleg Nesterov 		return 0;
15376e84d644SOleg Nesterov 
15386e84d644SOleg Nesterov 	/*
15396e84d644SOleg Nesterov 	 * The queueing is in progress, or it is already queued. Try to
15406e84d644SOleg Nesterov 	 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
15416e84d644SOleg Nesterov 	 */
15427a22ad75STejun Heo 	gcwq = get_work_gcwq(work);
15437a22ad75STejun Heo 	if (!gcwq)
15446e84d644SOleg Nesterov 		return ret;
15456e84d644SOleg Nesterov 
15468b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
15476e84d644SOleg Nesterov 	if (!list_empty(&work->entry)) {
15486e84d644SOleg Nesterov 		/*
15497a22ad75STejun Heo 		 * This work is queued, but perhaps we locked the wrong gcwq.
15506e84d644SOleg Nesterov 		 * In that case we must see the new value after rmb(), see
15516e84d644SOleg Nesterov 		 * insert_work()->wmb().
15526e84d644SOleg Nesterov 		 */
15536e84d644SOleg Nesterov 		smp_rmb();
15547a22ad75STejun Heo 		if (gcwq == get_work_gcwq(work)) {
1555dc186ad7SThomas Gleixner 			debug_work_deactivate(work);
15566e84d644SOleg Nesterov 			list_del_init(&work->entry);
15577a22ad75STejun Heo 			cwq_dec_nr_in_flight(get_work_cwq(work),
15587a22ad75STejun Heo 					     get_work_color(work));
15596e84d644SOleg Nesterov 			ret = 1;
15606e84d644SOleg Nesterov 		}
15616e84d644SOleg Nesterov 	}
15628b03ae3cSTejun Heo 	spin_unlock_irq(&gcwq->lock);
15636e84d644SOleg Nesterov 
15646e84d644SOleg Nesterov 	return ret;
15656e84d644SOleg Nesterov }
15666e84d644SOleg Nesterov 
15677a22ad75STejun Heo static void wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
1568b89deed3SOleg Nesterov {
1569b89deed3SOleg Nesterov 	struct wq_barrier barr;
1570affee4b2STejun Heo 	struct worker *worker;
1571b89deed3SOleg Nesterov 
15728b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
1573affee4b2STejun Heo 
15747a22ad75STejun Heo 	worker = find_worker_executing_work(gcwq, work);
15757a22ad75STejun Heo 	if (unlikely(worker))
15767a22ad75STejun Heo 		insert_wq_barrier(worker->current_cwq, &barr, work, worker);
1577affee4b2STejun Heo 
15788b03ae3cSTejun Heo 	spin_unlock_irq(&gcwq->lock);
1579b89deed3SOleg Nesterov 
1580affee4b2STejun Heo 	if (unlikely(worker)) {
1581b89deed3SOleg Nesterov 		wait_for_completion(&barr.done);
1582dc186ad7SThomas Gleixner 		destroy_work_on_stack(&barr.work);
1583dc186ad7SThomas Gleixner 	}
1584b89deed3SOleg Nesterov }
1585b89deed3SOleg Nesterov 
15866e84d644SOleg Nesterov static void wait_on_work(struct work_struct *work)
1587b89deed3SOleg Nesterov {
1588b1f4ec17SOleg Nesterov 	int cpu;
1589b89deed3SOleg Nesterov 
1590f293ea92SOleg Nesterov 	might_sleep();
1591f293ea92SOleg Nesterov 
15923295f0efSIngo Molnar 	lock_map_acquire(&work->lockdep_map);
15933295f0efSIngo Molnar 	lock_map_release(&work->lockdep_map);
15944e6045f1SJohannes Berg 
15951537663fSTejun Heo 	for_each_possible_cpu(cpu)
15967a22ad75STejun Heo 		wait_on_cpu_work(get_gcwq(cpu), work);
15976e84d644SOleg Nesterov }
15986e84d644SOleg Nesterov 
15991f1f642eSOleg Nesterov static int __cancel_work_timer(struct work_struct *work,
16001f1f642eSOleg Nesterov 				struct timer_list* timer)
16011f1f642eSOleg Nesterov {
16021f1f642eSOleg Nesterov 	int ret;
16031f1f642eSOleg Nesterov 
16041f1f642eSOleg Nesterov 	do {
16051f1f642eSOleg Nesterov 		ret = (timer && likely(del_timer(timer)));
16061f1f642eSOleg Nesterov 		if (!ret)
16071f1f642eSOleg Nesterov 			ret = try_to_grab_pending(work);
16081f1f642eSOleg Nesterov 		wait_on_work(work);
16091f1f642eSOleg Nesterov 	} while (unlikely(ret < 0));
16101f1f642eSOleg Nesterov 
16117a22ad75STejun Heo 	clear_work_data(work);
16121f1f642eSOleg Nesterov 	return ret;
16131f1f642eSOleg Nesterov }
16141f1f642eSOleg Nesterov 
16156e84d644SOleg Nesterov /**
16166e84d644SOleg Nesterov  * cancel_work_sync - block until a work_struct's callback has terminated
16176e84d644SOleg Nesterov  * @work: the work which is to be flushed
16186e84d644SOleg Nesterov  *
16191f1f642eSOleg Nesterov  * Returns true if @work was pending.
16201f1f642eSOleg Nesterov  *
16216e84d644SOleg Nesterov  * cancel_work_sync() will cancel the work if it is queued. If the work's
16226e84d644SOleg Nesterov  * callback appears to be running, cancel_work_sync() will block until it
16236e84d644SOleg Nesterov  * has completed.
16246e84d644SOleg Nesterov  *
16256e84d644SOleg Nesterov  * It is possible to use this function if the work re-queues itself. It can
16266e84d644SOleg Nesterov  * cancel the work even if it migrates to another workqueue, however in that
16276e84d644SOleg Nesterov  * case it only guarantees that work->func() has completed on the last queued
16286e84d644SOleg Nesterov  * workqueue.
16296e84d644SOleg Nesterov  *
16306e84d644SOleg Nesterov  * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
16316e84d644SOleg Nesterov  * pending, otherwise it goes into a busy-wait loop until the timer expires.
16326e84d644SOleg Nesterov  *
16336e84d644SOleg Nesterov  * The caller must ensure that workqueue_struct on which this work was last
16346e84d644SOleg Nesterov  * queued can't be destroyed before this function returns.
16356e84d644SOleg Nesterov  */
16361f1f642eSOleg Nesterov int cancel_work_sync(struct work_struct *work)
16376e84d644SOleg Nesterov {
16381f1f642eSOleg Nesterov 	return __cancel_work_timer(work, NULL);
1639b89deed3SOleg Nesterov }
164028e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync);
1641b89deed3SOleg Nesterov 
16426e84d644SOleg Nesterov /**
1643f5a421a4SOleg Nesterov  * cancel_delayed_work_sync - reliably kill off a delayed work.
16446e84d644SOleg Nesterov  * @dwork: the delayed work struct
16456e84d644SOleg Nesterov  *
16461f1f642eSOleg Nesterov  * Returns true if @dwork was pending.
16471f1f642eSOleg Nesterov  *
16486e84d644SOleg Nesterov  * It is possible to use this function if @dwork rearms itself via queue_work()
16496e84d644SOleg Nesterov  * or queue_delayed_work(). See also the comment for cancel_work_sync().
16506e84d644SOleg Nesterov  */
16511f1f642eSOleg Nesterov int cancel_delayed_work_sync(struct delayed_work *dwork)
16526e84d644SOleg Nesterov {
16531f1f642eSOleg Nesterov 	return __cancel_work_timer(&dwork->work, &dwork->timer);
16546e84d644SOleg Nesterov }
1655f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync);
16561da177e4SLinus Torvalds 
16576e84d644SOleg Nesterov static struct workqueue_struct *keventd_wq __read_mostly;
16581da177e4SLinus Torvalds 
16590fcb78c2SRolf Eike Beer /**
16600fcb78c2SRolf Eike Beer  * schedule_work - put work task in global workqueue
16610fcb78c2SRolf Eike Beer  * @work: job to be done
16620fcb78c2SRolf Eike Beer  *
16635b0f437dSBart Van Assche  * Returns zero if @work was already on the kernel-global workqueue and
16645b0f437dSBart Van Assche  * non-zero otherwise.
16655b0f437dSBart Van Assche  *
16665b0f437dSBart Van Assche  * This puts a job in the kernel-global workqueue if it was not already
16675b0f437dSBart Van Assche  * queued and leaves it in the same position on the kernel-global
16685b0f437dSBart Van Assche  * workqueue otherwise.
16690fcb78c2SRolf Eike Beer  */
16707ad5b3a5SHarvey Harrison int schedule_work(struct work_struct *work)
16711da177e4SLinus Torvalds {
16721da177e4SLinus Torvalds 	return queue_work(keventd_wq, work);
16731da177e4SLinus Torvalds }
1674ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_work);
16751da177e4SLinus Torvalds 
1676c1a220e7SZhang Rui /*
1677c1a220e7SZhang Rui  * schedule_work_on - put work task on a specific cpu
1678c1a220e7SZhang Rui  * @cpu: cpu to put the work task on
1679c1a220e7SZhang Rui  * @work: job to be done
1680c1a220e7SZhang Rui  *
1681c1a220e7SZhang Rui  * This puts a job on a specific cpu
1682c1a220e7SZhang Rui  */
1683c1a220e7SZhang Rui int schedule_work_on(int cpu, struct work_struct *work)
1684c1a220e7SZhang Rui {
1685c1a220e7SZhang Rui 	return queue_work_on(cpu, keventd_wq, work);
1686c1a220e7SZhang Rui }
1687c1a220e7SZhang Rui EXPORT_SYMBOL(schedule_work_on);
1688c1a220e7SZhang Rui 
16890fcb78c2SRolf Eike Beer /**
16900fcb78c2SRolf Eike Beer  * schedule_delayed_work - put work task in global workqueue after delay
169152bad64dSDavid Howells  * @dwork: job to be done
169252bad64dSDavid Howells  * @delay: number of jiffies to wait or 0 for immediate execution
16930fcb78c2SRolf Eike Beer  *
16940fcb78c2SRolf Eike Beer  * After waiting for a given time this puts a job in the kernel-global
16950fcb78c2SRolf Eike Beer  * workqueue.
16960fcb78c2SRolf Eike Beer  */
16977ad5b3a5SHarvey Harrison int schedule_delayed_work(struct delayed_work *dwork,
169882f67cd9SIngo Molnar 					unsigned long delay)
16991da177e4SLinus Torvalds {
170052bad64dSDavid Howells 	return queue_delayed_work(keventd_wq, dwork, delay);
17011da177e4SLinus Torvalds }
1702ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work);
17031da177e4SLinus Torvalds 
17040fcb78c2SRolf Eike Beer /**
17058c53e463SLinus Torvalds  * flush_delayed_work - block until a dwork_struct's callback has terminated
17068c53e463SLinus Torvalds  * @dwork: the delayed work which is to be flushed
17078c53e463SLinus Torvalds  *
17088c53e463SLinus Torvalds  * Any timeout is cancelled, and any pending work is run immediately.
17098c53e463SLinus Torvalds  */
17108c53e463SLinus Torvalds void flush_delayed_work(struct delayed_work *dwork)
17118c53e463SLinus Torvalds {
17128c53e463SLinus Torvalds 	if (del_timer_sync(&dwork->timer)) {
17137a22ad75STejun Heo 		__queue_work(get_cpu(), get_work_cwq(&dwork->work)->wq,
17144690c4abSTejun Heo 			     &dwork->work);
17158c53e463SLinus Torvalds 		put_cpu();
17168c53e463SLinus Torvalds 	}
17178c53e463SLinus Torvalds 	flush_work(&dwork->work);
17188c53e463SLinus Torvalds }
17198c53e463SLinus Torvalds EXPORT_SYMBOL(flush_delayed_work);
17208c53e463SLinus Torvalds 
17218c53e463SLinus Torvalds /**
17220fcb78c2SRolf Eike Beer  * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
17230fcb78c2SRolf Eike Beer  * @cpu: cpu to use
172452bad64dSDavid Howells  * @dwork: job to be done
17250fcb78c2SRolf Eike Beer  * @delay: number of jiffies to wait
17260fcb78c2SRolf Eike Beer  *
17270fcb78c2SRolf Eike Beer  * After waiting for a given time this puts a job in the kernel-global
17280fcb78c2SRolf Eike Beer  * workqueue on the specified CPU.
17290fcb78c2SRolf Eike Beer  */
17301da177e4SLinus Torvalds int schedule_delayed_work_on(int cpu,
173152bad64dSDavid Howells 			struct delayed_work *dwork, unsigned long delay)
17321da177e4SLinus Torvalds {
173352bad64dSDavid Howells 	return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
17341da177e4SLinus Torvalds }
1735ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work_on);
17361da177e4SLinus Torvalds 
1737b6136773SAndrew Morton /**
1738b6136773SAndrew Morton  * schedule_on_each_cpu - call a function on each online CPU from keventd
1739b6136773SAndrew Morton  * @func: the function to call
1740b6136773SAndrew Morton  *
1741b6136773SAndrew Morton  * Returns zero on success.
1742b6136773SAndrew Morton  * Returns -ve errno on failure.
1743b6136773SAndrew Morton  *
1744b6136773SAndrew Morton  * schedule_on_each_cpu() is very slow.
1745b6136773SAndrew Morton  */
174665f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func)
174715316ba8SChristoph Lameter {
174815316ba8SChristoph Lameter 	int cpu;
174965a64464SAndi Kleen 	int orig = -1;
1750b6136773SAndrew Morton 	struct work_struct *works;
175115316ba8SChristoph Lameter 
1752b6136773SAndrew Morton 	works = alloc_percpu(struct work_struct);
1753b6136773SAndrew Morton 	if (!works)
175415316ba8SChristoph Lameter 		return -ENOMEM;
1755b6136773SAndrew Morton 
175695402b38SGautham R Shenoy 	get_online_cpus();
175793981800STejun Heo 
175893981800STejun Heo 	/*
175993981800STejun Heo 	 * When running in keventd don't schedule a work item on
176093981800STejun Heo 	 * itself.  Can just call directly because the work queue is
176193981800STejun Heo 	 * already bound.  This also is faster.
176293981800STejun Heo 	 */
176393981800STejun Heo 	if (current_is_keventd())
176493981800STejun Heo 		orig = raw_smp_processor_id();
176593981800STejun Heo 
176615316ba8SChristoph Lameter 	for_each_online_cpu(cpu) {
17679bfb1839SIngo Molnar 		struct work_struct *work = per_cpu_ptr(works, cpu);
17689bfb1839SIngo Molnar 
17699bfb1839SIngo Molnar 		INIT_WORK(work, func);
177093981800STejun Heo 		if (cpu != orig)
17718de6d308SOleg Nesterov 			schedule_work_on(cpu, work);
177215316ba8SChristoph Lameter 	}
177393981800STejun Heo 	if (orig >= 0)
177493981800STejun Heo 		func(per_cpu_ptr(works, orig));
177593981800STejun Heo 
177693981800STejun Heo 	for_each_online_cpu(cpu)
17778616a89aSOleg Nesterov 		flush_work(per_cpu_ptr(works, cpu));
177893981800STejun Heo 
177995402b38SGautham R Shenoy 	put_online_cpus();
1780b6136773SAndrew Morton 	free_percpu(works);
178115316ba8SChristoph Lameter 	return 0;
178215316ba8SChristoph Lameter }
178315316ba8SChristoph Lameter 
1784eef6a7d5SAlan Stern /**
1785eef6a7d5SAlan Stern  * flush_scheduled_work - ensure that any scheduled work has run to completion.
1786eef6a7d5SAlan Stern  *
1787eef6a7d5SAlan Stern  * Forces execution of the kernel-global workqueue and blocks until its
1788eef6a7d5SAlan Stern  * completion.
1789eef6a7d5SAlan Stern  *
1790eef6a7d5SAlan Stern  * Think twice before calling this function!  It's very easy to get into
1791eef6a7d5SAlan Stern  * trouble if you don't take great care.  Either of the following situations
1792eef6a7d5SAlan Stern  * will lead to deadlock:
1793eef6a7d5SAlan Stern  *
1794eef6a7d5SAlan Stern  *	One of the work items currently on the workqueue needs to acquire
1795eef6a7d5SAlan Stern  *	a lock held by your code or its caller.
1796eef6a7d5SAlan Stern  *
1797eef6a7d5SAlan Stern  *	Your code is running in the context of a work routine.
1798eef6a7d5SAlan Stern  *
1799eef6a7d5SAlan Stern  * They will be detected by lockdep when they occur, but the first might not
1800eef6a7d5SAlan Stern  * occur very often.  It depends on what work items are on the workqueue and
1801eef6a7d5SAlan Stern  * what locks they need, which you have no control over.
1802eef6a7d5SAlan Stern  *
1803eef6a7d5SAlan Stern  * In most situations flushing the entire workqueue is overkill; you merely
1804eef6a7d5SAlan Stern  * need to know that a particular work item isn't queued and isn't running.
1805eef6a7d5SAlan Stern  * In such cases you should use cancel_delayed_work_sync() or
1806eef6a7d5SAlan Stern  * cancel_work_sync() instead.
1807eef6a7d5SAlan Stern  */
18081da177e4SLinus Torvalds void flush_scheduled_work(void)
18091da177e4SLinus Torvalds {
18101da177e4SLinus Torvalds 	flush_workqueue(keventd_wq);
18111da177e4SLinus Torvalds }
1812ae90dd5dSDave Jones EXPORT_SYMBOL(flush_scheduled_work);
18131da177e4SLinus Torvalds 
18141da177e4SLinus Torvalds /**
18151fa44ecaSJames Bottomley  * execute_in_process_context - reliably execute the routine with user context
18161fa44ecaSJames Bottomley  * @fn:		the function to execute
18171fa44ecaSJames Bottomley  * @ew:		guaranteed storage for the execute work structure (must
18181fa44ecaSJames Bottomley  *		be available when the work executes)
18191fa44ecaSJames Bottomley  *
18201fa44ecaSJames Bottomley  * Executes the function immediately if process context is available,
18211fa44ecaSJames Bottomley  * otherwise schedules the function for delayed execution.
18221fa44ecaSJames Bottomley  *
18231fa44ecaSJames Bottomley  * Returns:	0 - function was executed
18241fa44ecaSJames Bottomley  *		1 - function was scheduled for execution
18251fa44ecaSJames Bottomley  */
182665f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew)
18271fa44ecaSJames Bottomley {
18281fa44ecaSJames Bottomley 	if (!in_interrupt()) {
182965f27f38SDavid Howells 		fn(&ew->work);
18301fa44ecaSJames Bottomley 		return 0;
18311fa44ecaSJames Bottomley 	}
18321fa44ecaSJames Bottomley 
183365f27f38SDavid Howells 	INIT_WORK(&ew->work, fn);
18341fa44ecaSJames Bottomley 	schedule_work(&ew->work);
18351fa44ecaSJames Bottomley 
18361fa44ecaSJames Bottomley 	return 1;
18371fa44ecaSJames Bottomley }
18381fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context);
18391fa44ecaSJames Bottomley 
18401da177e4SLinus Torvalds int keventd_up(void)
18411da177e4SLinus Torvalds {
18421da177e4SLinus Torvalds 	return keventd_wq != NULL;
18431da177e4SLinus Torvalds }
18441da177e4SLinus Torvalds 
18451da177e4SLinus Torvalds int current_is_keventd(void)
18461da177e4SLinus Torvalds {
18471da177e4SLinus Torvalds 	struct cpu_workqueue_struct *cwq;
1848d243769dSHugh Dickins 	int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
18491da177e4SLinus Torvalds 	int ret = 0;
18501da177e4SLinus Torvalds 
18511da177e4SLinus Torvalds 	BUG_ON(!keventd_wq);
18521da177e4SLinus Torvalds 
18531537663fSTejun Heo 	cwq = get_cwq(cpu, keventd_wq);
1854c34056a3STejun Heo 	if (current == cwq->worker->task)
18551da177e4SLinus Torvalds 		ret = 1;
18561da177e4SLinus Torvalds 
18571da177e4SLinus Torvalds 	return ret;
18581da177e4SLinus Torvalds 
18591da177e4SLinus Torvalds }
18601da177e4SLinus Torvalds 
18610f900049STejun Heo static struct cpu_workqueue_struct *alloc_cwqs(void)
18620f900049STejun Heo {
18630f900049STejun Heo 	/*
18640f900049STejun Heo 	 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
18650f900049STejun Heo 	 * Make sure that the alignment isn't lower than that of
18660f900049STejun Heo 	 * unsigned long long.
18670f900049STejun Heo 	 */
18680f900049STejun Heo 	const size_t size = sizeof(struct cpu_workqueue_struct);
18690f900049STejun Heo 	const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
18700f900049STejun Heo 				   __alignof__(unsigned long long));
18710f900049STejun Heo 	struct cpu_workqueue_struct *cwqs;
18720f900049STejun Heo #ifndef CONFIG_SMP
18730f900049STejun Heo 	void *ptr;
18740f900049STejun Heo 
18750f900049STejun Heo 	/*
18760f900049STejun Heo 	 * On UP, percpu allocator doesn't honor alignment parameter
18770f900049STejun Heo 	 * and simply uses arch-dependent default.  Allocate enough
18780f900049STejun Heo 	 * room to align cwq and put an extra pointer at the end
18790f900049STejun Heo 	 * pointing back to the originally allocated pointer which
18800f900049STejun Heo 	 * will be used for free.
18810f900049STejun Heo 	 *
18820f900049STejun Heo 	 * FIXME: This really belongs to UP percpu code.  Update UP
18830f900049STejun Heo 	 * percpu code to honor alignment and remove this ugliness.
18840f900049STejun Heo 	 */
18850f900049STejun Heo 	ptr = __alloc_percpu(size + align + sizeof(void *), 1);
18860f900049STejun Heo 	cwqs = PTR_ALIGN(ptr, align);
18870f900049STejun Heo 	*(void **)per_cpu_ptr(cwqs + 1, 0) = ptr;
18880f900049STejun Heo #else
18890f900049STejun Heo 	/* On SMP, percpu allocator can do it itself */
18900f900049STejun Heo 	cwqs = __alloc_percpu(size, align);
18910f900049STejun Heo #endif
18920f900049STejun Heo 	/* just in case, make sure it's actually aligned */
18930f900049STejun Heo 	BUG_ON(!IS_ALIGNED((unsigned long)cwqs, align));
18940f900049STejun Heo 	return cwqs;
18950f900049STejun Heo }
18960f900049STejun Heo 
18970f900049STejun Heo static void free_cwqs(struct cpu_workqueue_struct *cwqs)
18980f900049STejun Heo {
18990f900049STejun Heo #ifndef CONFIG_SMP
19000f900049STejun Heo 	/* on UP, the pointer to free is stored right after the cwq */
19010f900049STejun Heo 	if (cwqs)
19020f900049STejun Heo 		free_percpu(*(void **)per_cpu_ptr(cwqs + 1, 0));
19030f900049STejun Heo #else
19040f900049STejun Heo 	free_percpu(cwqs);
19050f900049STejun Heo #endif
19060f900049STejun Heo }
19070f900049STejun Heo 
19084e6045f1SJohannes Berg struct workqueue_struct *__create_workqueue_key(const char *name,
190997e37d7bSTejun Heo 						unsigned int flags,
19101e19ffc6STejun Heo 						int max_active,
1911eb13ba87SJohannes Berg 						struct lock_class_key *key,
1912eb13ba87SJohannes Berg 						const char *lock_name)
19133af24433SOleg Nesterov {
19143af24433SOleg Nesterov 	struct workqueue_struct *wq;
1915c34056a3STejun Heo 	bool failed = false;
1916c34056a3STejun Heo 	unsigned int cpu;
19173af24433SOleg Nesterov 
19181e19ffc6STejun Heo 	max_active = clamp_val(max_active, 1, INT_MAX);
19191e19ffc6STejun Heo 
19203af24433SOleg Nesterov 	wq = kzalloc(sizeof(*wq), GFP_KERNEL);
19213af24433SOleg Nesterov 	if (!wq)
19224690c4abSTejun Heo 		goto err;
19233af24433SOleg Nesterov 
19240f900049STejun Heo 	wq->cpu_wq = alloc_cwqs();
19254690c4abSTejun Heo 	if (!wq->cpu_wq)
19264690c4abSTejun Heo 		goto err;
19273af24433SOleg Nesterov 
192897e37d7bSTejun Heo 	wq->flags = flags;
1929a0a1a5fdSTejun Heo 	wq->saved_max_active = max_active;
193073f53c4aSTejun Heo 	mutex_init(&wq->flush_mutex);
193173f53c4aSTejun Heo 	atomic_set(&wq->nr_cwqs_to_flush, 0);
193273f53c4aSTejun Heo 	INIT_LIST_HEAD(&wq->flusher_queue);
193373f53c4aSTejun Heo 	INIT_LIST_HEAD(&wq->flusher_overflow);
1934502ca9d8STejun Heo 	wq->single_cpu = NR_CPUS;
1935502ca9d8STejun Heo 
19363af24433SOleg Nesterov 	wq->name = name;
1937eb13ba87SJohannes Berg 	lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
1938cce1a165SOleg Nesterov 	INIT_LIST_HEAD(&wq->list);
19393af24433SOleg Nesterov 
19403da1c84cSOleg Nesterov 	cpu_maps_update_begin();
19416af8bf3dSOleg Nesterov 	/*
19426af8bf3dSOleg Nesterov 	 * We must initialize cwqs for each possible cpu even if we
19436af8bf3dSOleg Nesterov 	 * are going to call destroy_workqueue() finally. Otherwise
19446af8bf3dSOleg Nesterov 	 * cpu_up() can hit the uninitialized cwq once we drop the
19456af8bf3dSOleg Nesterov 	 * lock.
19466af8bf3dSOleg Nesterov 	 */
19473af24433SOleg Nesterov 	for_each_possible_cpu(cpu) {
19481537663fSTejun Heo 		struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
19498b03ae3cSTejun Heo 		struct global_cwq *gcwq = get_gcwq(cpu);
19501537663fSTejun Heo 
19510f900049STejun Heo 		BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
19528b03ae3cSTejun Heo 		cwq->gcwq = gcwq;
1953c34056a3STejun Heo 		cwq->wq = wq;
195473f53c4aSTejun Heo 		cwq->flush_color = -1;
19551e19ffc6STejun Heo 		cwq->max_active = max_active;
19561537663fSTejun Heo 		INIT_LIST_HEAD(&cwq->worklist);
19571e19ffc6STejun Heo 		INIT_LIST_HEAD(&cwq->delayed_works);
19581537663fSTejun Heo 
1959c34056a3STejun Heo 		if (failed)
19603af24433SOleg Nesterov 			continue;
1961502ca9d8STejun Heo 		cwq->worker = create_worker(cwq, cpu_online(cpu));
1962c34056a3STejun Heo 		if (cwq->worker)
1963c34056a3STejun Heo 			start_worker(cwq->worker);
19641537663fSTejun Heo 		else
1965c34056a3STejun Heo 			failed = true;
19663af24433SOleg Nesterov 	}
19671537663fSTejun Heo 
1968a0a1a5fdSTejun Heo 	/*
1969a0a1a5fdSTejun Heo 	 * workqueue_lock protects global freeze state and workqueues
1970a0a1a5fdSTejun Heo 	 * list.  Grab it, set max_active accordingly and add the new
1971a0a1a5fdSTejun Heo 	 * workqueue to workqueues list.
1972a0a1a5fdSTejun Heo 	 */
19731537663fSTejun Heo 	spin_lock(&workqueue_lock);
1974a0a1a5fdSTejun Heo 
1975a0a1a5fdSTejun Heo 	if (workqueue_freezing && wq->flags & WQ_FREEZEABLE)
1976a0a1a5fdSTejun Heo 		for_each_possible_cpu(cpu)
1977a0a1a5fdSTejun Heo 			get_cwq(cpu, wq)->max_active = 0;
1978a0a1a5fdSTejun Heo 
19791537663fSTejun Heo 	list_add(&wq->list, &workqueues);
1980a0a1a5fdSTejun Heo 
19811537663fSTejun Heo 	spin_unlock(&workqueue_lock);
19821537663fSTejun Heo 
19833da1c84cSOleg Nesterov 	cpu_maps_update_done();
19843af24433SOleg Nesterov 
1985c34056a3STejun Heo 	if (failed) {
19863af24433SOleg Nesterov 		destroy_workqueue(wq);
19873af24433SOleg Nesterov 		wq = NULL;
19883af24433SOleg Nesterov 	}
19893af24433SOleg Nesterov 	return wq;
19904690c4abSTejun Heo err:
19914690c4abSTejun Heo 	if (wq) {
19920f900049STejun Heo 		free_cwqs(wq->cpu_wq);
19934690c4abSTejun Heo 		kfree(wq);
19944690c4abSTejun Heo 	}
19954690c4abSTejun Heo 	return NULL;
19963af24433SOleg Nesterov }
19974e6045f1SJohannes Berg EXPORT_SYMBOL_GPL(__create_workqueue_key);
19983af24433SOleg Nesterov 
19993af24433SOleg Nesterov /**
20003af24433SOleg Nesterov  * destroy_workqueue - safely terminate a workqueue
20013af24433SOleg Nesterov  * @wq: target workqueue
20023af24433SOleg Nesterov  *
20033af24433SOleg Nesterov  * Safely destroy a workqueue. All work currently pending will be done first.
20043af24433SOleg Nesterov  */
20053af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq)
20063af24433SOleg Nesterov {
2007c8e55f36STejun Heo 	unsigned int cpu;
20083af24433SOleg Nesterov 
2009a0a1a5fdSTejun Heo 	flush_workqueue(wq);
2010a0a1a5fdSTejun Heo 
2011a0a1a5fdSTejun Heo 	/*
2012a0a1a5fdSTejun Heo 	 * wq list is used to freeze wq, remove from list after
2013a0a1a5fdSTejun Heo 	 * flushing is complete in case freeze races us.
2014a0a1a5fdSTejun Heo 	 */
20153da1c84cSOleg Nesterov 	cpu_maps_update_begin();
201695402b38SGautham R Shenoy 	spin_lock(&workqueue_lock);
20173af24433SOleg Nesterov 	list_del(&wq->list);
201895402b38SGautham R Shenoy 	spin_unlock(&workqueue_lock);
20193da1c84cSOleg Nesterov 	cpu_maps_update_done();
20203af24433SOleg Nesterov 
202173f53c4aSTejun Heo 	for_each_possible_cpu(cpu) {
202273f53c4aSTejun Heo 		struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
202373f53c4aSTejun Heo 		int i;
202473f53c4aSTejun Heo 
2025c34056a3STejun Heo 		if (cwq->worker) {
2026c8e55f36STejun Heo 			spin_lock_irq(&cwq->gcwq->lock);
2027c34056a3STejun Heo 			destroy_worker(cwq->worker);
2028c34056a3STejun Heo 			cwq->worker = NULL;
2029c8e55f36STejun Heo 			spin_unlock_irq(&cwq->gcwq->lock);
203073f53c4aSTejun Heo 		}
203173f53c4aSTejun Heo 
203273f53c4aSTejun Heo 		for (i = 0; i < WORK_NR_COLORS; i++)
203373f53c4aSTejun Heo 			BUG_ON(cwq->nr_in_flight[i]);
20341e19ffc6STejun Heo 		BUG_ON(cwq->nr_active);
20351e19ffc6STejun Heo 		BUG_ON(!list_empty(&cwq->delayed_works));
203673f53c4aSTejun Heo 	}
20371537663fSTejun Heo 
20380f900049STejun Heo 	free_cwqs(wq->cpu_wq);
20393af24433SOleg Nesterov 	kfree(wq);
20403af24433SOleg Nesterov }
20413af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue);
20423af24433SOleg Nesterov 
2043db7bccf4STejun Heo /*
2044db7bccf4STejun Heo  * CPU hotplug.
2045db7bccf4STejun Heo  *
2046db7bccf4STejun Heo  * CPU hotplug is implemented by allowing cwqs to be detached from
2047db7bccf4STejun Heo  * CPU, running with unbound workers and allowing them to be
2048db7bccf4STejun Heo  * reattached later if the cpu comes back online.  A separate thread
2049db7bccf4STejun Heo  * is created to govern cwqs in such state and is called the trustee.
2050db7bccf4STejun Heo  *
2051db7bccf4STejun Heo  * Trustee states and their descriptions.
2052db7bccf4STejun Heo  *
2053db7bccf4STejun Heo  * START	Command state used on startup.  On CPU_DOWN_PREPARE, a
2054db7bccf4STejun Heo  *		new trustee is started with this state.
2055db7bccf4STejun Heo  *
2056db7bccf4STejun Heo  * IN_CHARGE	Once started, trustee will enter this state after
2057db7bccf4STejun Heo  *		making all existing workers rogue.  DOWN_PREPARE waits
2058db7bccf4STejun Heo  *		for trustee to enter this state.  After reaching
2059db7bccf4STejun Heo  *		IN_CHARGE, trustee tries to execute the pending
2060db7bccf4STejun Heo  *		worklist until it's empty and the state is set to
2061db7bccf4STejun Heo  *		BUTCHER, or the state is set to RELEASE.
2062db7bccf4STejun Heo  *
2063db7bccf4STejun Heo  * BUTCHER	Command state which is set by the cpu callback after
2064db7bccf4STejun Heo  *		the cpu has went down.  Once this state is set trustee
2065db7bccf4STejun Heo  *		knows that there will be no new works on the worklist
2066db7bccf4STejun Heo  *		and once the worklist is empty it can proceed to
2067db7bccf4STejun Heo  *		killing idle workers.
2068db7bccf4STejun Heo  *
2069db7bccf4STejun Heo  * RELEASE	Command state which is set by the cpu callback if the
2070db7bccf4STejun Heo  *		cpu down has been canceled or it has come online
2071db7bccf4STejun Heo  *		again.  After recognizing this state, trustee stops
2072db7bccf4STejun Heo  *		trying to drain or butcher and transits to DONE.
2073db7bccf4STejun Heo  *
2074db7bccf4STejun Heo  * DONE		Trustee will enter this state after BUTCHER or RELEASE
2075db7bccf4STejun Heo  *		is complete.
2076db7bccf4STejun Heo  *
2077db7bccf4STejun Heo  *          trustee                 CPU                draining
2078db7bccf4STejun Heo  *         took over                down               complete
2079db7bccf4STejun Heo  * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
2080db7bccf4STejun Heo  *                        |                     |                  ^
2081db7bccf4STejun Heo  *                        | CPU is back online  v   return workers |
2082db7bccf4STejun Heo  *                         ----------------> RELEASE --------------
2083db7bccf4STejun Heo  */
2084db7bccf4STejun Heo 
2085db7bccf4STejun Heo /**
2086db7bccf4STejun Heo  * trustee_wait_event_timeout - timed event wait for trustee
2087db7bccf4STejun Heo  * @cond: condition to wait for
2088db7bccf4STejun Heo  * @timeout: timeout in jiffies
2089db7bccf4STejun Heo  *
2090db7bccf4STejun Heo  * wait_event_timeout() for trustee to use.  Handles locking and
2091db7bccf4STejun Heo  * checks for RELEASE request.
2092db7bccf4STejun Heo  *
2093db7bccf4STejun Heo  * CONTEXT:
2094db7bccf4STejun Heo  * spin_lock_irq(gcwq->lock) which may be released and regrabbed
2095db7bccf4STejun Heo  * multiple times.  To be used by trustee.
2096db7bccf4STejun Heo  *
2097db7bccf4STejun Heo  * RETURNS:
2098db7bccf4STejun Heo  * Positive indicating left time if @cond is satisfied, 0 if timed
2099db7bccf4STejun Heo  * out, -1 if canceled.
2100db7bccf4STejun Heo  */
2101db7bccf4STejun Heo #define trustee_wait_event_timeout(cond, timeout) ({			\
2102db7bccf4STejun Heo 	long __ret = (timeout);						\
2103db7bccf4STejun Heo 	while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) &&	\
2104db7bccf4STejun Heo 	       __ret) {							\
2105db7bccf4STejun Heo 		spin_unlock_irq(&gcwq->lock);				\
2106db7bccf4STejun Heo 		__wait_event_timeout(gcwq->trustee_wait, (cond) ||	\
2107db7bccf4STejun Heo 			(gcwq->trustee_state == TRUSTEE_RELEASE),	\
2108db7bccf4STejun Heo 			__ret);						\
2109db7bccf4STejun Heo 		spin_lock_irq(&gcwq->lock);				\
2110db7bccf4STejun Heo 	}								\
2111db7bccf4STejun Heo 	gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret);		\
2112db7bccf4STejun Heo })
2113db7bccf4STejun Heo 
2114db7bccf4STejun Heo /**
2115db7bccf4STejun Heo  * trustee_wait_event - event wait for trustee
2116db7bccf4STejun Heo  * @cond: condition to wait for
2117db7bccf4STejun Heo  *
2118db7bccf4STejun Heo  * wait_event() for trustee to use.  Automatically handles locking and
2119db7bccf4STejun Heo  * checks for CANCEL request.
2120db7bccf4STejun Heo  *
2121db7bccf4STejun Heo  * CONTEXT:
2122db7bccf4STejun Heo  * spin_lock_irq(gcwq->lock) which may be released and regrabbed
2123db7bccf4STejun Heo  * multiple times.  To be used by trustee.
2124db7bccf4STejun Heo  *
2125db7bccf4STejun Heo  * RETURNS:
2126db7bccf4STejun Heo  * 0 if @cond is satisfied, -1 if canceled.
2127db7bccf4STejun Heo  */
2128db7bccf4STejun Heo #define trustee_wait_event(cond) ({					\
2129db7bccf4STejun Heo 	long __ret1;							\
2130db7bccf4STejun Heo 	__ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
2131db7bccf4STejun Heo 	__ret1 < 0 ? -1 : 0;						\
2132db7bccf4STejun Heo })
2133db7bccf4STejun Heo 
2134db7bccf4STejun Heo static int __cpuinit trustee_thread(void *__gcwq)
2135db7bccf4STejun Heo {
2136db7bccf4STejun Heo 	struct global_cwq *gcwq = __gcwq;
2137db7bccf4STejun Heo 	struct worker *worker;
2138db7bccf4STejun Heo 	struct hlist_node *pos;
2139db7bccf4STejun Heo 	int i;
2140db7bccf4STejun Heo 
2141db7bccf4STejun Heo 	BUG_ON(gcwq->cpu != smp_processor_id());
2142db7bccf4STejun Heo 
2143db7bccf4STejun Heo 	spin_lock_irq(&gcwq->lock);
2144db7bccf4STejun Heo 	/*
2145502ca9d8STejun Heo 	 * Make all workers rogue.  Trustee must be bound to the
2146502ca9d8STejun Heo 	 * target cpu and can't be cancelled.
2147db7bccf4STejun Heo 	 */
2148db7bccf4STejun Heo 	BUG_ON(gcwq->cpu != smp_processor_id());
2149db7bccf4STejun Heo 
2150db7bccf4STejun Heo 	list_for_each_entry(worker, &gcwq->idle_list, entry)
2151db7bccf4STejun Heo 		worker->flags |= WORKER_ROGUE;
2152db7bccf4STejun Heo 
2153db7bccf4STejun Heo 	for_each_busy_worker(worker, i, pos, gcwq)
2154db7bccf4STejun Heo 		worker->flags |= WORKER_ROGUE;
2155db7bccf4STejun Heo 
2156db7bccf4STejun Heo 	/*
2157db7bccf4STejun Heo 	 * We're now in charge.  Notify and proceed to drain.  We need
2158db7bccf4STejun Heo 	 * to keep the gcwq running during the whole CPU down
2159db7bccf4STejun Heo 	 * procedure as other cpu hotunplug callbacks may need to
2160db7bccf4STejun Heo 	 * flush currently running tasks.
2161db7bccf4STejun Heo 	 */
2162db7bccf4STejun Heo 	gcwq->trustee_state = TRUSTEE_IN_CHARGE;
2163db7bccf4STejun Heo 	wake_up_all(&gcwq->trustee_wait);
2164db7bccf4STejun Heo 
2165db7bccf4STejun Heo 	/*
2166db7bccf4STejun Heo 	 * The original cpu is in the process of dying and may go away
2167db7bccf4STejun Heo 	 * anytime now.  When that happens, we and all workers would
2168db7bccf4STejun Heo 	 * be migrated to other cpus.  Try draining any left work.
2169db7bccf4STejun Heo 	 * Note that if the gcwq is frozen, there may be frozen works
2170db7bccf4STejun Heo 	 * in freezeable cwqs.  Don't declare completion while frozen.
2171db7bccf4STejun Heo 	 */
2172db7bccf4STejun Heo 	while (gcwq->nr_workers != gcwq->nr_idle ||
2173db7bccf4STejun Heo 	       gcwq->flags & GCWQ_FREEZING ||
2174db7bccf4STejun Heo 	       gcwq->trustee_state == TRUSTEE_IN_CHARGE) {
2175db7bccf4STejun Heo 		/* give a breather */
2176db7bccf4STejun Heo 		if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
2177db7bccf4STejun Heo 			break;
2178db7bccf4STejun Heo 	}
2179db7bccf4STejun Heo 
2180db7bccf4STejun Heo 	/* notify completion */
2181db7bccf4STejun Heo 	gcwq->trustee = NULL;
2182db7bccf4STejun Heo 	gcwq->trustee_state = TRUSTEE_DONE;
2183db7bccf4STejun Heo 	wake_up_all(&gcwq->trustee_wait);
2184db7bccf4STejun Heo 	spin_unlock_irq(&gcwq->lock);
2185db7bccf4STejun Heo 	return 0;
2186db7bccf4STejun Heo }
2187db7bccf4STejun Heo 
2188db7bccf4STejun Heo /**
2189db7bccf4STejun Heo  * wait_trustee_state - wait for trustee to enter the specified state
2190db7bccf4STejun Heo  * @gcwq: gcwq the trustee of interest belongs to
2191db7bccf4STejun Heo  * @state: target state to wait for
2192db7bccf4STejun Heo  *
2193db7bccf4STejun Heo  * Wait for the trustee to reach @state.  DONE is already matched.
2194db7bccf4STejun Heo  *
2195db7bccf4STejun Heo  * CONTEXT:
2196db7bccf4STejun Heo  * spin_lock_irq(gcwq->lock) which may be released and regrabbed
2197db7bccf4STejun Heo  * multiple times.  To be used by cpu_callback.
2198db7bccf4STejun Heo  */
2199db7bccf4STejun Heo static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
2200db7bccf4STejun Heo {
2201db7bccf4STejun Heo 	if (!(gcwq->trustee_state == state ||
2202db7bccf4STejun Heo 	      gcwq->trustee_state == TRUSTEE_DONE)) {
2203db7bccf4STejun Heo 		spin_unlock_irq(&gcwq->lock);
2204db7bccf4STejun Heo 		__wait_event(gcwq->trustee_wait,
2205db7bccf4STejun Heo 			     gcwq->trustee_state == state ||
2206db7bccf4STejun Heo 			     gcwq->trustee_state == TRUSTEE_DONE);
2207db7bccf4STejun Heo 		spin_lock_irq(&gcwq->lock);
2208db7bccf4STejun Heo 	}
2209db7bccf4STejun Heo }
2210db7bccf4STejun Heo 
22119c7b216dSChandra Seetharaman static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
22121da177e4SLinus Torvalds 						unsigned long action,
22131da177e4SLinus Torvalds 						void *hcpu)
22141da177e4SLinus Torvalds {
22153af24433SOleg Nesterov 	unsigned int cpu = (unsigned long)hcpu;
2216db7bccf4STejun Heo 	struct global_cwq *gcwq = get_gcwq(cpu);
2217db7bccf4STejun Heo 	struct task_struct *new_trustee = NULL;
2218db7bccf4STejun Heo 	struct worker *worker;
2219db7bccf4STejun Heo 	struct hlist_node *pos;
2220db7bccf4STejun Heo 	unsigned long flags;
2221db7bccf4STejun Heo 	int i;
22221da177e4SLinus Torvalds 
22238bb78442SRafael J. Wysocki 	action &= ~CPU_TASKS_FROZEN;
22248bb78442SRafael J. Wysocki 
2225db7bccf4STejun Heo 	switch (action) {
2226db7bccf4STejun Heo 	case CPU_DOWN_PREPARE:
2227db7bccf4STejun Heo 		new_trustee = kthread_create(trustee_thread, gcwq,
2228db7bccf4STejun Heo 					     "workqueue_trustee/%d\n", cpu);
2229db7bccf4STejun Heo 		if (IS_ERR(new_trustee))
2230db7bccf4STejun Heo 			return notifier_from_errno(PTR_ERR(new_trustee));
2231db7bccf4STejun Heo 		kthread_bind(new_trustee, cpu);
2232db7bccf4STejun Heo 	}
22331537663fSTejun Heo 
2234db7bccf4STejun Heo 	/* some are called w/ irq disabled, don't disturb irq status */
2235db7bccf4STejun Heo 	spin_lock_irqsave(&gcwq->lock, flags);
22363af24433SOleg Nesterov 
22373af24433SOleg Nesterov 	switch (action) {
2238db7bccf4STejun Heo 	case CPU_DOWN_PREPARE:
2239db7bccf4STejun Heo 		/* initialize trustee and tell it to acquire the gcwq */
2240db7bccf4STejun Heo 		BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
2241db7bccf4STejun Heo 		gcwq->trustee = new_trustee;
2242db7bccf4STejun Heo 		gcwq->trustee_state = TRUSTEE_START;
2243db7bccf4STejun Heo 		wake_up_process(gcwq->trustee);
2244db7bccf4STejun Heo 		wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
2245db7bccf4STejun Heo 		break;
2246db7bccf4STejun Heo 
22473da1c84cSOleg Nesterov 	case CPU_POST_DEAD:
2248db7bccf4STejun Heo 		gcwq->trustee_state = TRUSTEE_BUTCHER;
2249db7bccf4STejun Heo 		break;
2250db7bccf4STejun Heo 
2251db7bccf4STejun Heo 	case CPU_DOWN_FAILED:
2252db7bccf4STejun Heo 	case CPU_ONLINE:
2253db7bccf4STejun Heo 		if (gcwq->trustee_state != TRUSTEE_DONE) {
2254db7bccf4STejun Heo 			gcwq->trustee_state = TRUSTEE_RELEASE;
2255db7bccf4STejun Heo 			wake_up_process(gcwq->trustee);
2256db7bccf4STejun Heo 			wait_trustee_state(gcwq, TRUSTEE_DONE);
2257db7bccf4STejun Heo 		}
2258db7bccf4STejun Heo 
2259502ca9d8STejun Heo 		/* clear ROGUE from all workers */
2260db7bccf4STejun Heo 		list_for_each_entry(worker, &gcwq->idle_list, entry)
2261db7bccf4STejun Heo 			worker->flags &= ~WORKER_ROGUE;
2262db7bccf4STejun Heo 
2263db7bccf4STejun Heo 		for_each_busy_worker(worker, i, pos, gcwq)
2264db7bccf4STejun Heo 			worker->flags &= ~WORKER_ROGUE;
22651da177e4SLinus Torvalds 		break;
22661da177e4SLinus Torvalds 	}
2267db7bccf4STejun Heo 
2268db7bccf4STejun Heo 	spin_unlock_irqrestore(&gcwq->lock, flags);
22691da177e4SLinus Torvalds 
22701537663fSTejun Heo 	return notifier_from_errno(0);
22711da177e4SLinus Torvalds }
22721da177e4SLinus Torvalds 
22732d3854a3SRusty Russell #ifdef CONFIG_SMP
22748ccad40dSRusty Russell 
22752d3854a3SRusty Russell struct work_for_cpu {
22766b44003eSAndrew Morton 	struct completion completion;
22772d3854a3SRusty Russell 	long (*fn)(void *);
22782d3854a3SRusty Russell 	void *arg;
22792d3854a3SRusty Russell 	long ret;
22802d3854a3SRusty Russell };
22812d3854a3SRusty Russell 
22826b44003eSAndrew Morton static int do_work_for_cpu(void *_wfc)
22832d3854a3SRusty Russell {
22846b44003eSAndrew Morton 	struct work_for_cpu *wfc = _wfc;
22852d3854a3SRusty Russell 	wfc->ret = wfc->fn(wfc->arg);
22866b44003eSAndrew Morton 	complete(&wfc->completion);
22876b44003eSAndrew Morton 	return 0;
22882d3854a3SRusty Russell }
22892d3854a3SRusty Russell 
22902d3854a3SRusty Russell /**
22912d3854a3SRusty Russell  * work_on_cpu - run a function in user context on a particular cpu
22922d3854a3SRusty Russell  * @cpu: the cpu to run on
22932d3854a3SRusty Russell  * @fn: the function to run
22942d3854a3SRusty Russell  * @arg: the function arg
22952d3854a3SRusty Russell  *
229631ad9081SRusty Russell  * This will return the value @fn returns.
229731ad9081SRusty Russell  * It is up to the caller to ensure that the cpu doesn't go offline.
22986b44003eSAndrew Morton  * The caller must not hold any locks which would prevent @fn from completing.
22992d3854a3SRusty Russell  */
23002d3854a3SRusty Russell long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
23012d3854a3SRusty Russell {
23026b44003eSAndrew Morton 	struct task_struct *sub_thread;
23036b44003eSAndrew Morton 	struct work_for_cpu wfc = {
23046b44003eSAndrew Morton 		.completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
23056b44003eSAndrew Morton 		.fn = fn,
23066b44003eSAndrew Morton 		.arg = arg,
23076b44003eSAndrew Morton 	};
23082d3854a3SRusty Russell 
23096b44003eSAndrew Morton 	sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
23106b44003eSAndrew Morton 	if (IS_ERR(sub_thread))
23116b44003eSAndrew Morton 		return PTR_ERR(sub_thread);
23126b44003eSAndrew Morton 	kthread_bind(sub_thread, cpu);
23136b44003eSAndrew Morton 	wake_up_process(sub_thread);
23146b44003eSAndrew Morton 	wait_for_completion(&wfc.completion);
23152d3854a3SRusty Russell 	return wfc.ret;
23162d3854a3SRusty Russell }
23172d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu);
23182d3854a3SRusty Russell #endif /* CONFIG_SMP */
23192d3854a3SRusty Russell 
2320a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER
2321a0a1a5fdSTejun Heo 
2322a0a1a5fdSTejun Heo /**
2323a0a1a5fdSTejun Heo  * freeze_workqueues_begin - begin freezing workqueues
2324a0a1a5fdSTejun Heo  *
2325a0a1a5fdSTejun Heo  * Start freezing workqueues.  After this function returns, all
2326a0a1a5fdSTejun Heo  * freezeable workqueues will queue new works to their frozen_works
2327a0a1a5fdSTejun Heo  * list instead of the cwq ones.
2328a0a1a5fdSTejun Heo  *
2329a0a1a5fdSTejun Heo  * CONTEXT:
23308b03ae3cSTejun Heo  * Grabs and releases workqueue_lock and gcwq->lock's.
2331a0a1a5fdSTejun Heo  */
2332a0a1a5fdSTejun Heo void freeze_workqueues_begin(void)
2333a0a1a5fdSTejun Heo {
2334a0a1a5fdSTejun Heo 	struct workqueue_struct *wq;
2335a0a1a5fdSTejun Heo 	unsigned int cpu;
2336a0a1a5fdSTejun Heo 
2337a0a1a5fdSTejun Heo 	spin_lock(&workqueue_lock);
2338a0a1a5fdSTejun Heo 
2339a0a1a5fdSTejun Heo 	BUG_ON(workqueue_freezing);
2340a0a1a5fdSTejun Heo 	workqueue_freezing = true;
2341a0a1a5fdSTejun Heo 
2342a0a1a5fdSTejun Heo 	for_each_possible_cpu(cpu) {
23438b03ae3cSTejun Heo 		struct global_cwq *gcwq = get_gcwq(cpu);
23448b03ae3cSTejun Heo 
23458b03ae3cSTejun Heo 		spin_lock_irq(&gcwq->lock);
23468b03ae3cSTejun Heo 
2347db7bccf4STejun Heo 		BUG_ON(gcwq->flags & GCWQ_FREEZING);
2348db7bccf4STejun Heo 		gcwq->flags |= GCWQ_FREEZING;
2349db7bccf4STejun Heo 
2350a0a1a5fdSTejun Heo 		list_for_each_entry(wq, &workqueues, list) {
2351a0a1a5fdSTejun Heo 			struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
2352a0a1a5fdSTejun Heo 
2353a0a1a5fdSTejun Heo 			if (wq->flags & WQ_FREEZEABLE)
2354a0a1a5fdSTejun Heo 				cwq->max_active = 0;
2355a0a1a5fdSTejun Heo 		}
23568b03ae3cSTejun Heo 
23578b03ae3cSTejun Heo 		spin_unlock_irq(&gcwq->lock);
2358a0a1a5fdSTejun Heo 	}
2359a0a1a5fdSTejun Heo 
2360a0a1a5fdSTejun Heo 	spin_unlock(&workqueue_lock);
2361a0a1a5fdSTejun Heo }
2362a0a1a5fdSTejun Heo 
2363a0a1a5fdSTejun Heo /**
2364a0a1a5fdSTejun Heo  * freeze_workqueues_busy - are freezeable workqueues still busy?
2365a0a1a5fdSTejun Heo  *
2366a0a1a5fdSTejun Heo  * Check whether freezing is complete.  This function must be called
2367a0a1a5fdSTejun Heo  * between freeze_workqueues_begin() and thaw_workqueues().
2368a0a1a5fdSTejun Heo  *
2369a0a1a5fdSTejun Heo  * CONTEXT:
2370a0a1a5fdSTejun Heo  * Grabs and releases workqueue_lock.
2371a0a1a5fdSTejun Heo  *
2372a0a1a5fdSTejun Heo  * RETURNS:
2373a0a1a5fdSTejun Heo  * %true if some freezeable workqueues are still busy.  %false if
2374a0a1a5fdSTejun Heo  * freezing is complete.
2375a0a1a5fdSTejun Heo  */
2376a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void)
2377a0a1a5fdSTejun Heo {
2378a0a1a5fdSTejun Heo 	struct workqueue_struct *wq;
2379a0a1a5fdSTejun Heo 	unsigned int cpu;
2380a0a1a5fdSTejun Heo 	bool busy = false;
2381a0a1a5fdSTejun Heo 
2382a0a1a5fdSTejun Heo 	spin_lock(&workqueue_lock);
2383a0a1a5fdSTejun Heo 
2384a0a1a5fdSTejun Heo 	BUG_ON(!workqueue_freezing);
2385a0a1a5fdSTejun Heo 
2386a0a1a5fdSTejun Heo 	for_each_possible_cpu(cpu) {
2387a0a1a5fdSTejun Heo 		/*
2388a0a1a5fdSTejun Heo 		 * nr_active is monotonically decreasing.  It's safe
2389a0a1a5fdSTejun Heo 		 * to peek without lock.
2390a0a1a5fdSTejun Heo 		 */
2391a0a1a5fdSTejun Heo 		list_for_each_entry(wq, &workqueues, list) {
2392a0a1a5fdSTejun Heo 			struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
2393a0a1a5fdSTejun Heo 
2394a0a1a5fdSTejun Heo 			if (!(wq->flags & WQ_FREEZEABLE))
2395a0a1a5fdSTejun Heo 				continue;
2396a0a1a5fdSTejun Heo 
2397a0a1a5fdSTejun Heo 			BUG_ON(cwq->nr_active < 0);
2398a0a1a5fdSTejun Heo 			if (cwq->nr_active) {
2399a0a1a5fdSTejun Heo 				busy = true;
2400a0a1a5fdSTejun Heo 				goto out_unlock;
2401a0a1a5fdSTejun Heo 			}
2402a0a1a5fdSTejun Heo 		}
2403a0a1a5fdSTejun Heo 	}
2404a0a1a5fdSTejun Heo out_unlock:
2405a0a1a5fdSTejun Heo 	spin_unlock(&workqueue_lock);
2406a0a1a5fdSTejun Heo 	return busy;
2407a0a1a5fdSTejun Heo }
2408a0a1a5fdSTejun Heo 
2409a0a1a5fdSTejun Heo /**
2410a0a1a5fdSTejun Heo  * thaw_workqueues - thaw workqueues
2411a0a1a5fdSTejun Heo  *
2412a0a1a5fdSTejun Heo  * Thaw workqueues.  Normal queueing is restored and all collected
2413a0a1a5fdSTejun Heo  * frozen works are transferred to their respective cwq worklists.
2414a0a1a5fdSTejun Heo  *
2415a0a1a5fdSTejun Heo  * CONTEXT:
24168b03ae3cSTejun Heo  * Grabs and releases workqueue_lock and gcwq->lock's.
2417a0a1a5fdSTejun Heo  */
2418a0a1a5fdSTejun Heo void thaw_workqueues(void)
2419a0a1a5fdSTejun Heo {
2420a0a1a5fdSTejun Heo 	struct workqueue_struct *wq;
2421a0a1a5fdSTejun Heo 	unsigned int cpu;
2422a0a1a5fdSTejun Heo 
2423a0a1a5fdSTejun Heo 	spin_lock(&workqueue_lock);
2424a0a1a5fdSTejun Heo 
2425a0a1a5fdSTejun Heo 	if (!workqueue_freezing)
2426a0a1a5fdSTejun Heo 		goto out_unlock;
2427a0a1a5fdSTejun Heo 
2428a0a1a5fdSTejun Heo 	for_each_possible_cpu(cpu) {
24298b03ae3cSTejun Heo 		struct global_cwq *gcwq = get_gcwq(cpu);
24308b03ae3cSTejun Heo 
24318b03ae3cSTejun Heo 		spin_lock_irq(&gcwq->lock);
24328b03ae3cSTejun Heo 
2433db7bccf4STejun Heo 		BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
2434db7bccf4STejun Heo 		gcwq->flags &= ~GCWQ_FREEZING;
2435db7bccf4STejun Heo 
2436a0a1a5fdSTejun Heo 		list_for_each_entry(wq, &workqueues, list) {
2437a0a1a5fdSTejun Heo 			struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
2438a0a1a5fdSTejun Heo 
2439a0a1a5fdSTejun Heo 			if (!(wq->flags & WQ_FREEZEABLE))
2440a0a1a5fdSTejun Heo 				continue;
2441a0a1a5fdSTejun Heo 
2442a0a1a5fdSTejun Heo 			/* restore max_active and repopulate worklist */
2443a0a1a5fdSTejun Heo 			cwq->max_active = wq->saved_max_active;
2444a0a1a5fdSTejun Heo 
2445a0a1a5fdSTejun Heo 			while (!list_empty(&cwq->delayed_works) &&
2446a0a1a5fdSTejun Heo 			       cwq->nr_active < cwq->max_active)
2447a0a1a5fdSTejun Heo 				cwq_activate_first_delayed(cwq);
2448a0a1a5fdSTejun Heo 
2449502ca9d8STejun Heo 			/* perform delayed unbind from single cpu if empty */
2450502ca9d8STejun Heo 			if (wq->single_cpu == gcwq->cpu &&
2451502ca9d8STejun Heo 			    !cwq->nr_active && list_empty(&cwq->delayed_works))
2452502ca9d8STejun Heo 				cwq_unbind_single_cpu(cwq);
2453502ca9d8STejun Heo 
2454c8e55f36STejun Heo 			wake_up_process(cwq->worker->task);
2455a0a1a5fdSTejun Heo 		}
24568b03ae3cSTejun Heo 
24578b03ae3cSTejun Heo 		spin_unlock_irq(&gcwq->lock);
2458a0a1a5fdSTejun Heo 	}
2459a0a1a5fdSTejun Heo 
2460a0a1a5fdSTejun Heo 	workqueue_freezing = false;
2461a0a1a5fdSTejun Heo out_unlock:
2462a0a1a5fdSTejun Heo 	spin_unlock(&workqueue_lock);
2463a0a1a5fdSTejun Heo }
2464a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */
2465a0a1a5fdSTejun Heo 
2466c12920d1SOleg Nesterov void __init init_workqueues(void)
24671da177e4SLinus Torvalds {
2468c34056a3STejun Heo 	unsigned int cpu;
2469c8e55f36STejun Heo 	int i;
2470c34056a3STejun Heo 
24717a22ad75STejun Heo 	/*
24727a22ad75STejun Heo 	 * The pointer part of work->data is either pointing to the
24737a22ad75STejun Heo 	 * cwq or contains the cpu number the work ran last on.  Make
24747a22ad75STejun Heo 	 * sure cpu number won't overflow into kernel pointer area so
24757a22ad75STejun Heo 	 * that they can be distinguished.
24767a22ad75STejun Heo 	 */
24777a22ad75STejun Heo 	BUILD_BUG_ON(NR_CPUS << WORK_STRUCT_FLAG_BITS >= PAGE_OFFSET);
24787a22ad75STejun Heo 
2479db7bccf4STejun Heo 	hotcpu_notifier(workqueue_cpu_callback, CPU_PRI_WORKQUEUE);
24808b03ae3cSTejun Heo 
24818b03ae3cSTejun Heo 	/* initialize gcwqs */
24828b03ae3cSTejun Heo 	for_each_possible_cpu(cpu) {
24838b03ae3cSTejun Heo 		struct global_cwq *gcwq = get_gcwq(cpu);
24848b03ae3cSTejun Heo 
24858b03ae3cSTejun Heo 		spin_lock_init(&gcwq->lock);
24868b03ae3cSTejun Heo 		gcwq->cpu = cpu;
24878b03ae3cSTejun Heo 
2488c8e55f36STejun Heo 		INIT_LIST_HEAD(&gcwq->idle_list);
2489c8e55f36STejun Heo 		for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
2490c8e55f36STejun Heo 			INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
2491c8e55f36STejun Heo 
24928b03ae3cSTejun Heo 		ida_init(&gcwq->worker_ida);
2493db7bccf4STejun Heo 
2494db7bccf4STejun Heo 		gcwq->trustee_state = TRUSTEE_DONE;
2495db7bccf4STejun Heo 		init_waitqueue_head(&gcwq->trustee_wait);
24968b03ae3cSTejun Heo 	}
24978b03ae3cSTejun Heo 
24981da177e4SLinus Torvalds 	keventd_wq = create_workqueue("events");
24991da177e4SLinus Torvalds 	BUG_ON(!keventd_wq);
25001da177e4SLinus Torvalds }
2501