xref: /linux-6.15/kernel/workqueue.c (revision 8cca0eea)
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 */
85*8cca0eeaSTejun 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 /*
3224594bf15SDavid Howells  * Set the workqueue on which a work item is to be run
3234594bf15SDavid Howells  * - Must *only* be called if the pending flag is set
3244594bf15SDavid Howells  */
325ed7c0feeSOleg Nesterov static inline void set_wq_data(struct work_struct *work,
3264690c4abSTejun Heo 			       struct cpu_workqueue_struct *cwq,
3274690c4abSTejun Heo 			       unsigned long extra_flags)
328365970a1SDavid Howells {
3294594bf15SDavid Howells 	BUG_ON(!work_pending(work));
3304594bf15SDavid Howells 
3314690c4abSTejun Heo 	atomic_long_set(&work->data, (unsigned long)cwq | work_static(work) |
33222df02bbSTejun Heo 			WORK_STRUCT_PENDING | extra_flags);
333365970a1SDavid Howells }
334365970a1SDavid Howells 
3354d707b9fSOleg Nesterov /*
3364d707b9fSOleg Nesterov  * Clear WORK_STRUCT_PENDING and the workqueue on which it was queued.
3374d707b9fSOleg Nesterov  */
3384d707b9fSOleg Nesterov static inline void clear_wq_data(struct work_struct *work)
3394d707b9fSOleg Nesterov {
3404690c4abSTejun Heo 	atomic_long_set(&work->data, work_static(work));
3414d707b9fSOleg Nesterov }
3424d707b9fSOleg Nesterov 
34364166699STejun Heo static inline struct cpu_workqueue_struct *get_wq_data(struct work_struct *work)
344365970a1SDavid Howells {
34564166699STejun Heo 	return (void *)(atomic_long_read(&work->data) &
34664166699STejun Heo 			WORK_STRUCT_WQ_DATA_MASK);
347365970a1SDavid Howells }
348365970a1SDavid Howells 
3494690c4abSTejun Heo /**
350c8e55f36STejun Heo  * busy_worker_head - return the busy hash head for a work
351c8e55f36STejun Heo  * @gcwq: gcwq of interest
352c8e55f36STejun Heo  * @work: work to be hashed
353c8e55f36STejun Heo  *
354c8e55f36STejun Heo  * Return hash head of @gcwq for @work.
355c8e55f36STejun Heo  *
356c8e55f36STejun Heo  * CONTEXT:
357c8e55f36STejun Heo  * spin_lock_irq(gcwq->lock).
358c8e55f36STejun Heo  *
359c8e55f36STejun Heo  * RETURNS:
360c8e55f36STejun Heo  * Pointer to the hash head.
361c8e55f36STejun Heo  */
362c8e55f36STejun Heo static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
363c8e55f36STejun Heo 					   struct work_struct *work)
364c8e55f36STejun Heo {
365c8e55f36STejun Heo 	const int base_shift = ilog2(sizeof(struct work_struct));
366c8e55f36STejun Heo 	unsigned long v = (unsigned long)work;
367c8e55f36STejun Heo 
368c8e55f36STejun Heo 	/* simple shift and fold hash, do we need something better? */
369c8e55f36STejun Heo 	v >>= base_shift;
370c8e55f36STejun Heo 	v += v >> BUSY_WORKER_HASH_ORDER;
371c8e55f36STejun Heo 	v &= BUSY_WORKER_HASH_MASK;
372c8e55f36STejun Heo 
373c8e55f36STejun Heo 	return &gcwq->busy_hash[v];
374c8e55f36STejun Heo }
375c8e55f36STejun Heo 
376c8e55f36STejun Heo /**
377*8cca0eeaSTejun Heo  * __find_worker_executing_work - find worker which is executing a work
378*8cca0eeaSTejun Heo  * @gcwq: gcwq of interest
379*8cca0eeaSTejun Heo  * @bwh: hash head as returned by busy_worker_head()
380*8cca0eeaSTejun Heo  * @work: work to find worker for
381*8cca0eeaSTejun Heo  *
382*8cca0eeaSTejun Heo  * Find a worker which is executing @work on @gcwq.  @bwh should be
383*8cca0eeaSTejun Heo  * the hash head obtained by calling busy_worker_head() with the same
384*8cca0eeaSTejun Heo  * work.
385*8cca0eeaSTejun Heo  *
386*8cca0eeaSTejun Heo  * CONTEXT:
387*8cca0eeaSTejun Heo  * spin_lock_irq(gcwq->lock).
388*8cca0eeaSTejun Heo  *
389*8cca0eeaSTejun Heo  * RETURNS:
390*8cca0eeaSTejun Heo  * Pointer to worker which is executing @work if found, NULL
391*8cca0eeaSTejun Heo  * otherwise.
392*8cca0eeaSTejun Heo  */
393*8cca0eeaSTejun Heo static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
394*8cca0eeaSTejun Heo 						   struct hlist_head *bwh,
395*8cca0eeaSTejun Heo 						   struct work_struct *work)
396*8cca0eeaSTejun Heo {
397*8cca0eeaSTejun Heo 	struct worker *worker;
398*8cca0eeaSTejun Heo 	struct hlist_node *tmp;
399*8cca0eeaSTejun Heo 
400*8cca0eeaSTejun Heo 	hlist_for_each_entry(worker, tmp, bwh, hentry)
401*8cca0eeaSTejun Heo 		if (worker->current_work == work)
402*8cca0eeaSTejun Heo 			return worker;
403*8cca0eeaSTejun Heo 	return NULL;
404*8cca0eeaSTejun Heo }
405*8cca0eeaSTejun Heo 
406*8cca0eeaSTejun Heo /**
407*8cca0eeaSTejun Heo  * find_worker_executing_work - find worker which is executing a work
408*8cca0eeaSTejun Heo  * @gcwq: gcwq of interest
409*8cca0eeaSTejun Heo  * @work: work to find worker for
410*8cca0eeaSTejun Heo  *
411*8cca0eeaSTejun Heo  * Find a worker which is executing @work on @gcwq.  This function is
412*8cca0eeaSTejun Heo  * identical to __find_worker_executing_work() except that this
413*8cca0eeaSTejun Heo  * function calculates @bwh itself.
414*8cca0eeaSTejun Heo  *
415*8cca0eeaSTejun Heo  * CONTEXT:
416*8cca0eeaSTejun Heo  * spin_lock_irq(gcwq->lock).
417*8cca0eeaSTejun Heo  *
418*8cca0eeaSTejun Heo  * RETURNS:
419*8cca0eeaSTejun Heo  * Pointer to worker which is executing @work if found, NULL
420*8cca0eeaSTejun Heo  * otherwise.
421*8cca0eeaSTejun Heo  */
422*8cca0eeaSTejun Heo static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
423*8cca0eeaSTejun Heo 						 struct work_struct *work)
424*8cca0eeaSTejun Heo {
425*8cca0eeaSTejun Heo 	return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
426*8cca0eeaSTejun Heo 					    work);
427*8cca0eeaSTejun Heo }
428*8cca0eeaSTejun Heo 
429*8cca0eeaSTejun Heo /**
4304690c4abSTejun Heo  * insert_work - insert a work into cwq
4314690c4abSTejun Heo  * @cwq: cwq @work belongs to
4324690c4abSTejun Heo  * @work: work to insert
4334690c4abSTejun Heo  * @head: insertion point
4344690c4abSTejun Heo  * @extra_flags: extra WORK_STRUCT_* flags to set
4354690c4abSTejun Heo  *
4364690c4abSTejun Heo  * Insert @work into @cwq after @head.
4374690c4abSTejun Heo  *
4384690c4abSTejun Heo  * CONTEXT:
4398b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock).
4404690c4abSTejun Heo  */
441b89deed3SOleg Nesterov static void insert_work(struct cpu_workqueue_struct *cwq,
4424690c4abSTejun Heo 			struct work_struct *work, struct list_head *head,
4434690c4abSTejun Heo 			unsigned int extra_flags)
444b89deed3SOleg Nesterov {
4454690c4abSTejun Heo 	/* we own @work, set data and link */
4464690c4abSTejun Heo 	set_wq_data(work, cwq, extra_flags);
4474690c4abSTejun Heo 
4486e84d644SOleg Nesterov 	/*
4496e84d644SOleg Nesterov 	 * Ensure that we get the right work->data if we see the
4506e84d644SOleg Nesterov 	 * result of list_add() below, see try_to_grab_pending().
4516e84d644SOleg Nesterov 	 */
4526e84d644SOleg Nesterov 	smp_wmb();
4534690c4abSTejun Heo 
4541a4d9b0aSOleg Nesterov 	list_add_tail(&work->entry, head);
455c8e55f36STejun Heo 	wake_up_process(cwq->worker->task);
456b89deed3SOleg Nesterov }
457b89deed3SOleg Nesterov 
458502ca9d8STejun Heo /**
459502ca9d8STejun Heo  * cwq_unbind_single_cpu - unbind cwq from single cpu workqueue processing
460502ca9d8STejun Heo  * @cwq: cwq to unbind
461502ca9d8STejun Heo  *
462502ca9d8STejun Heo  * Try to unbind @cwq from single cpu workqueue processing.  If
463502ca9d8STejun Heo  * @cwq->wq is frozen, unbind is delayed till the workqueue is thawed.
464502ca9d8STejun Heo  *
465502ca9d8STejun Heo  * CONTEXT:
466502ca9d8STejun Heo  * spin_lock_irq(gcwq->lock).
467502ca9d8STejun Heo  */
468502ca9d8STejun Heo static void cwq_unbind_single_cpu(struct cpu_workqueue_struct *cwq)
469502ca9d8STejun Heo {
470502ca9d8STejun Heo 	struct workqueue_struct *wq = cwq->wq;
471502ca9d8STejun Heo 	struct global_cwq *gcwq = cwq->gcwq;
472502ca9d8STejun Heo 
473502ca9d8STejun Heo 	BUG_ON(wq->single_cpu != gcwq->cpu);
474502ca9d8STejun Heo 	/*
475502ca9d8STejun Heo 	 * Unbind from workqueue if @cwq is not frozen.  If frozen,
476502ca9d8STejun Heo 	 * thaw_workqueues() will either restart processing on this
477502ca9d8STejun Heo 	 * cpu or unbind if empty.  This keeps works queued while
478502ca9d8STejun Heo 	 * frozen fully ordered and flushable.
479502ca9d8STejun Heo 	 */
480502ca9d8STejun Heo 	if (likely(!(gcwq->flags & GCWQ_FREEZING))) {
481502ca9d8STejun Heo 		smp_wmb();	/* paired with cmpxchg() in __queue_work() */
482502ca9d8STejun Heo 		wq->single_cpu = NR_CPUS;
483502ca9d8STejun Heo 	}
484502ca9d8STejun Heo }
485502ca9d8STejun Heo 
4864690c4abSTejun Heo static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
4871da177e4SLinus Torvalds 			 struct work_struct *work)
4881da177e4SLinus Torvalds {
489502ca9d8STejun Heo 	struct global_cwq *gcwq;
490502ca9d8STejun Heo 	struct cpu_workqueue_struct *cwq;
4911e19ffc6STejun Heo 	struct list_head *worklist;
4921da177e4SLinus Torvalds 	unsigned long flags;
493502ca9d8STejun Heo 	bool arbitrate;
4941da177e4SLinus Torvalds 
495dc186ad7SThomas Gleixner 	debug_work_activate(work);
4961e19ffc6STejun Heo 
497502ca9d8STejun Heo 	/* determine gcwq to use */
498502ca9d8STejun Heo 	if (!(wq->flags & WQ_SINGLE_CPU)) {
499502ca9d8STejun Heo 		/* just use the requested cpu for multicpu workqueues */
500502ca9d8STejun Heo 		gcwq = get_gcwq(cpu);
5018b03ae3cSTejun Heo 		spin_lock_irqsave(&gcwq->lock, flags);
502502ca9d8STejun Heo 	} else {
503502ca9d8STejun Heo 		unsigned int req_cpu = cpu;
504502ca9d8STejun Heo 
505502ca9d8STejun Heo 		/*
506502ca9d8STejun Heo 		 * It's a bit more complex for single cpu workqueues.
507502ca9d8STejun Heo 		 * We first need to determine which cpu is going to be
508502ca9d8STejun Heo 		 * used.  If no cpu is currently serving this
509502ca9d8STejun Heo 		 * workqueue, arbitrate using atomic accesses to
510502ca9d8STejun Heo 		 * wq->single_cpu; otherwise, use the current one.
511502ca9d8STejun Heo 		 */
512502ca9d8STejun Heo 	retry:
513502ca9d8STejun Heo 		cpu = wq->single_cpu;
514502ca9d8STejun Heo 		arbitrate = cpu == NR_CPUS;
515502ca9d8STejun Heo 		if (arbitrate)
516502ca9d8STejun Heo 			cpu = req_cpu;
517502ca9d8STejun Heo 
518502ca9d8STejun Heo 		gcwq = get_gcwq(cpu);
519502ca9d8STejun Heo 		spin_lock_irqsave(&gcwq->lock, flags);
520502ca9d8STejun Heo 
521502ca9d8STejun Heo 		/*
522502ca9d8STejun Heo 		 * The following cmpxchg() is a full barrier paired
523502ca9d8STejun Heo 		 * with smp_wmb() in cwq_unbind_single_cpu() and
524502ca9d8STejun Heo 		 * guarantees that all changes to wq->st_* fields are
525502ca9d8STejun Heo 		 * visible on the new cpu after this point.
526502ca9d8STejun Heo 		 */
527502ca9d8STejun Heo 		if (arbitrate)
528502ca9d8STejun Heo 			cmpxchg(&wq->single_cpu, NR_CPUS, cpu);
529502ca9d8STejun Heo 
530502ca9d8STejun Heo 		if (unlikely(wq->single_cpu != cpu)) {
531502ca9d8STejun Heo 			spin_unlock_irqrestore(&gcwq->lock, flags);
532502ca9d8STejun Heo 			goto retry;
533502ca9d8STejun Heo 		}
534502ca9d8STejun Heo 	}
535502ca9d8STejun Heo 
536502ca9d8STejun Heo 	/* gcwq determined, get cwq and queue */
537502ca9d8STejun Heo 	cwq = get_cwq(gcwq->cpu, wq);
538502ca9d8STejun Heo 
5394690c4abSTejun Heo 	BUG_ON(!list_empty(&work->entry));
5401e19ffc6STejun Heo 
54173f53c4aSTejun Heo 	cwq->nr_in_flight[cwq->work_color]++;
5421e19ffc6STejun Heo 
5431e19ffc6STejun Heo 	if (likely(cwq->nr_active < cwq->max_active)) {
5441e19ffc6STejun Heo 		cwq->nr_active++;
5451e19ffc6STejun Heo 		worklist = &cwq->worklist;
5461e19ffc6STejun Heo 	} else
5471e19ffc6STejun Heo 		worklist = &cwq->delayed_works;
5481e19ffc6STejun Heo 
5491e19ffc6STejun Heo 	insert_work(cwq, work, worklist, work_color_to_flags(cwq->work_color));
5501e19ffc6STejun Heo 
5518b03ae3cSTejun Heo 	spin_unlock_irqrestore(&gcwq->lock, flags);
5521da177e4SLinus Torvalds }
5531da177e4SLinus Torvalds 
5540fcb78c2SRolf Eike Beer /**
5550fcb78c2SRolf Eike Beer  * queue_work - queue work on a workqueue
5560fcb78c2SRolf Eike Beer  * @wq: workqueue to use
5570fcb78c2SRolf Eike Beer  * @work: work to queue
5580fcb78c2SRolf Eike Beer  *
559057647fcSAlan Stern  * Returns 0 if @work was already on a queue, non-zero otherwise.
5601da177e4SLinus Torvalds  *
56100dfcaf7SOleg Nesterov  * We queue the work to the CPU on which it was submitted, but if the CPU dies
56200dfcaf7SOleg Nesterov  * it can be processed by another CPU.
5631da177e4SLinus Torvalds  */
5647ad5b3a5SHarvey Harrison int queue_work(struct workqueue_struct *wq, struct work_struct *work)
5651da177e4SLinus Torvalds {
566ef1ca236SOleg Nesterov 	int ret;
5671da177e4SLinus Torvalds 
568ef1ca236SOleg Nesterov 	ret = queue_work_on(get_cpu(), wq, work);
569a848e3b6SOleg Nesterov 	put_cpu();
570ef1ca236SOleg Nesterov 
5711da177e4SLinus Torvalds 	return ret;
5721da177e4SLinus Torvalds }
573ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_work);
5741da177e4SLinus Torvalds 
575c1a220e7SZhang Rui /**
576c1a220e7SZhang Rui  * queue_work_on - queue work on specific cpu
577c1a220e7SZhang Rui  * @cpu: CPU number to execute work on
578c1a220e7SZhang Rui  * @wq: workqueue to use
579c1a220e7SZhang Rui  * @work: work to queue
580c1a220e7SZhang Rui  *
581c1a220e7SZhang Rui  * Returns 0 if @work was already on a queue, non-zero otherwise.
582c1a220e7SZhang Rui  *
583c1a220e7SZhang Rui  * We queue the work to a specific CPU, the caller must ensure it
584c1a220e7SZhang Rui  * can't go away.
585c1a220e7SZhang Rui  */
586c1a220e7SZhang Rui int
587c1a220e7SZhang Rui queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
588c1a220e7SZhang Rui {
589c1a220e7SZhang Rui 	int ret = 0;
590c1a220e7SZhang Rui 
59122df02bbSTejun Heo 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
5924690c4abSTejun Heo 		__queue_work(cpu, wq, work);
593c1a220e7SZhang Rui 		ret = 1;
594c1a220e7SZhang Rui 	}
595c1a220e7SZhang Rui 	return ret;
596c1a220e7SZhang Rui }
597c1a220e7SZhang Rui EXPORT_SYMBOL_GPL(queue_work_on);
598c1a220e7SZhang Rui 
5996d141c3fSLi Zefan static void delayed_work_timer_fn(unsigned long __data)
6001da177e4SLinus Torvalds {
60152bad64dSDavid Howells 	struct delayed_work *dwork = (struct delayed_work *)__data;
602ed7c0feeSOleg Nesterov 	struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
6031da177e4SLinus Torvalds 
6044690c4abSTejun Heo 	__queue_work(smp_processor_id(), cwq->wq, &dwork->work);
6051da177e4SLinus Torvalds }
6061da177e4SLinus Torvalds 
6070fcb78c2SRolf Eike Beer /**
6080fcb78c2SRolf Eike Beer  * queue_delayed_work - queue work on a workqueue after delay
6090fcb78c2SRolf Eike Beer  * @wq: workqueue to use
610af9997e4SRandy Dunlap  * @dwork: delayable work to queue
6110fcb78c2SRolf Eike Beer  * @delay: number of jiffies to wait before queueing
6120fcb78c2SRolf Eike Beer  *
613057647fcSAlan Stern  * Returns 0 if @work was already on a queue, non-zero otherwise.
6140fcb78c2SRolf Eike Beer  */
6157ad5b3a5SHarvey Harrison int queue_delayed_work(struct workqueue_struct *wq,
61652bad64dSDavid Howells 			struct delayed_work *dwork, unsigned long delay)
6171da177e4SLinus Torvalds {
61852bad64dSDavid Howells 	if (delay == 0)
61963bc0362SOleg Nesterov 		return queue_work(wq, &dwork->work);
6201da177e4SLinus Torvalds 
62163bc0362SOleg Nesterov 	return queue_delayed_work_on(-1, wq, dwork, delay);
6221da177e4SLinus Torvalds }
623ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work);
6241da177e4SLinus Torvalds 
6250fcb78c2SRolf Eike Beer /**
6260fcb78c2SRolf Eike Beer  * queue_delayed_work_on - queue work on specific CPU after delay
6270fcb78c2SRolf Eike Beer  * @cpu: CPU number to execute work on
6280fcb78c2SRolf Eike Beer  * @wq: workqueue to use
629af9997e4SRandy Dunlap  * @dwork: work to queue
6300fcb78c2SRolf Eike Beer  * @delay: number of jiffies to wait before queueing
6310fcb78c2SRolf Eike Beer  *
632057647fcSAlan Stern  * Returns 0 if @work was already on a queue, non-zero otherwise.
6330fcb78c2SRolf Eike Beer  */
6347a6bc1cdSVenkatesh Pallipadi int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
63552bad64dSDavid Howells 			struct delayed_work *dwork, unsigned long delay)
6367a6bc1cdSVenkatesh Pallipadi {
6377a6bc1cdSVenkatesh Pallipadi 	int ret = 0;
63852bad64dSDavid Howells 	struct timer_list *timer = &dwork->timer;
63952bad64dSDavid Howells 	struct work_struct *work = &dwork->work;
6407a6bc1cdSVenkatesh Pallipadi 
64122df02bbSTejun Heo 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
6427a6bc1cdSVenkatesh Pallipadi 		BUG_ON(timer_pending(timer));
6437a6bc1cdSVenkatesh Pallipadi 		BUG_ON(!list_empty(&work->entry));
6447a6bc1cdSVenkatesh Pallipadi 
6458a3e77ccSAndrew Liu 		timer_stats_timer_set_start_info(&dwork->timer);
6468a3e77ccSAndrew Liu 
647ed7c0feeSOleg Nesterov 		/* This stores cwq for the moment, for the timer_fn */
648502ca9d8STejun Heo 		set_wq_data(work, get_cwq(raw_smp_processor_id(), wq), 0);
6497a6bc1cdSVenkatesh Pallipadi 		timer->expires = jiffies + delay;
65052bad64dSDavid Howells 		timer->data = (unsigned long)dwork;
6517a6bc1cdSVenkatesh Pallipadi 		timer->function = delayed_work_timer_fn;
65263bc0362SOleg Nesterov 
65363bc0362SOleg Nesterov 		if (unlikely(cpu >= 0))
6547a6bc1cdSVenkatesh Pallipadi 			add_timer_on(timer, cpu);
65563bc0362SOleg Nesterov 		else
65663bc0362SOleg Nesterov 			add_timer(timer);
6577a6bc1cdSVenkatesh Pallipadi 		ret = 1;
6587a6bc1cdSVenkatesh Pallipadi 	}
6597a6bc1cdSVenkatesh Pallipadi 	return ret;
6607a6bc1cdSVenkatesh Pallipadi }
661ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work_on);
6621da177e4SLinus Torvalds 
663c8e55f36STejun Heo /**
664c8e55f36STejun Heo  * worker_enter_idle - enter idle state
665c8e55f36STejun Heo  * @worker: worker which is entering idle state
666c8e55f36STejun Heo  *
667c8e55f36STejun Heo  * @worker is entering idle state.  Update stats and idle timer if
668c8e55f36STejun Heo  * necessary.
669c8e55f36STejun Heo  *
670c8e55f36STejun Heo  * LOCKING:
671c8e55f36STejun Heo  * spin_lock_irq(gcwq->lock).
672c8e55f36STejun Heo  */
673c8e55f36STejun Heo static void worker_enter_idle(struct worker *worker)
674c8e55f36STejun Heo {
675c8e55f36STejun Heo 	struct global_cwq *gcwq = worker->gcwq;
676c8e55f36STejun Heo 
677c8e55f36STejun Heo 	BUG_ON(worker->flags & WORKER_IDLE);
678c8e55f36STejun Heo 	BUG_ON(!list_empty(&worker->entry) &&
679c8e55f36STejun Heo 	       (worker->hentry.next || worker->hentry.pprev));
680c8e55f36STejun Heo 
681c8e55f36STejun Heo 	worker->flags |= WORKER_IDLE;
682c8e55f36STejun Heo 	gcwq->nr_idle++;
683c8e55f36STejun Heo 
684c8e55f36STejun Heo 	/* idle_list is LIFO */
685c8e55f36STejun Heo 	list_add(&worker->entry, &gcwq->idle_list);
686db7bccf4STejun Heo 
687db7bccf4STejun Heo 	if (unlikely(worker->flags & WORKER_ROGUE))
688db7bccf4STejun Heo 		wake_up_all(&gcwq->trustee_wait);
689c8e55f36STejun Heo }
690c8e55f36STejun Heo 
691c8e55f36STejun Heo /**
692c8e55f36STejun Heo  * worker_leave_idle - leave idle state
693c8e55f36STejun Heo  * @worker: worker which is leaving idle state
694c8e55f36STejun Heo  *
695c8e55f36STejun Heo  * @worker is leaving idle state.  Update stats.
696c8e55f36STejun Heo  *
697c8e55f36STejun Heo  * LOCKING:
698c8e55f36STejun Heo  * spin_lock_irq(gcwq->lock).
699c8e55f36STejun Heo  */
700c8e55f36STejun Heo static void worker_leave_idle(struct worker *worker)
701c8e55f36STejun Heo {
702c8e55f36STejun Heo 	struct global_cwq *gcwq = worker->gcwq;
703c8e55f36STejun Heo 
704c8e55f36STejun Heo 	BUG_ON(!(worker->flags & WORKER_IDLE));
705c8e55f36STejun Heo 	worker->flags &= ~WORKER_IDLE;
706c8e55f36STejun Heo 	gcwq->nr_idle--;
707c8e55f36STejun Heo 	list_del_init(&worker->entry);
708c8e55f36STejun Heo }
709c8e55f36STejun Heo 
710c34056a3STejun Heo static struct worker *alloc_worker(void)
711c34056a3STejun Heo {
712c34056a3STejun Heo 	struct worker *worker;
713c34056a3STejun Heo 
714c34056a3STejun Heo 	worker = kzalloc(sizeof(*worker), GFP_KERNEL);
715c8e55f36STejun Heo 	if (worker) {
716c8e55f36STejun Heo 		INIT_LIST_HEAD(&worker->entry);
717affee4b2STejun Heo 		INIT_LIST_HEAD(&worker->scheduled);
718c8e55f36STejun Heo 	}
719c34056a3STejun Heo 	return worker;
720c34056a3STejun Heo }
721c34056a3STejun Heo 
722c34056a3STejun Heo /**
723c34056a3STejun Heo  * create_worker - create a new workqueue worker
724c34056a3STejun Heo  * @cwq: cwq the new worker will belong to
725c34056a3STejun Heo  * @bind: whether to set affinity to @cpu or not
726c34056a3STejun Heo  *
727c34056a3STejun Heo  * Create a new worker which is bound to @cwq.  The returned worker
728c34056a3STejun Heo  * can be started by calling start_worker() or destroyed using
729c34056a3STejun Heo  * destroy_worker().
730c34056a3STejun Heo  *
731c34056a3STejun Heo  * CONTEXT:
732c34056a3STejun Heo  * Might sleep.  Does GFP_KERNEL allocations.
733c34056a3STejun Heo  *
734c34056a3STejun Heo  * RETURNS:
735c34056a3STejun Heo  * Pointer to the newly created worker.
736c34056a3STejun Heo  */
737c34056a3STejun Heo static struct worker *create_worker(struct cpu_workqueue_struct *cwq, bool bind)
738c34056a3STejun Heo {
7398b03ae3cSTejun Heo 	struct global_cwq *gcwq = cwq->gcwq;
740c34056a3STejun Heo 	int id = -1;
741c34056a3STejun Heo 	struct worker *worker = NULL;
742c34056a3STejun Heo 
7438b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
7448b03ae3cSTejun Heo 	while (ida_get_new(&gcwq->worker_ida, &id)) {
7458b03ae3cSTejun Heo 		spin_unlock_irq(&gcwq->lock);
7468b03ae3cSTejun Heo 		if (!ida_pre_get(&gcwq->worker_ida, GFP_KERNEL))
747c34056a3STejun Heo 			goto fail;
7488b03ae3cSTejun Heo 		spin_lock_irq(&gcwq->lock);
749c34056a3STejun Heo 	}
7508b03ae3cSTejun Heo 	spin_unlock_irq(&gcwq->lock);
751c34056a3STejun Heo 
752c34056a3STejun Heo 	worker = alloc_worker();
753c34056a3STejun Heo 	if (!worker)
754c34056a3STejun Heo 		goto fail;
755c34056a3STejun Heo 
7568b03ae3cSTejun Heo 	worker->gcwq = gcwq;
757c34056a3STejun Heo 	worker->cwq = cwq;
758c34056a3STejun Heo 	worker->id = id;
759c34056a3STejun Heo 
760c34056a3STejun Heo 	worker->task = kthread_create(worker_thread, worker, "kworker/%u:%d",
7618b03ae3cSTejun Heo 				      gcwq->cpu, id);
762c34056a3STejun Heo 	if (IS_ERR(worker->task))
763c34056a3STejun Heo 		goto fail;
764c34056a3STejun Heo 
765db7bccf4STejun Heo 	/*
766db7bccf4STejun Heo 	 * A rogue worker will become a regular one if CPU comes
767db7bccf4STejun Heo 	 * online later on.  Make sure every worker has
768db7bccf4STejun Heo 	 * PF_THREAD_BOUND set.
769db7bccf4STejun Heo 	 */
770c34056a3STejun Heo 	if (bind)
7718b03ae3cSTejun Heo 		kthread_bind(worker->task, gcwq->cpu);
772db7bccf4STejun Heo 	else
773db7bccf4STejun Heo 		worker->task->flags |= PF_THREAD_BOUND;
774c34056a3STejun Heo 
775c34056a3STejun Heo 	return worker;
776c34056a3STejun Heo fail:
777c34056a3STejun Heo 	if (id >= 0) {
7788b03ae3cSTejun Heo 		spin_lock_irq(&gcwq->lock);
7798b03ae3cSTejun Heo 		ida_remove(&gcwq->worker_ida, id);
7808b03ae3cSTejun Heo 		spin_unlock_irq(&gcwq->lock);
781c34056a3STejun Heo 	}
782c34056a3STejun Heo 	kfree(worker);
783c34056a3STejun Heo 	return NULL;
784c34056a3STejun Heo }
785c34056a3STejun Heo 
786c34056a3STejun Heo /**
787c34056a3STejun Heo  * start_worker - start a newly created worker
788c34056a3STejun Heo  * @worker: worker to start
789c34056a3STejun Heo  *
790c8e55f36STejun Heo  * Make the gcwq aware of @worker and start it.
791c34056a3STejun Heo  *
792c34056a3STejun Heo  * CONTEXT:
7938b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock).
794c34056a3STejun Heo  */
795c34056a3STejun Heo static void start_worker(struct worker *worker)
796c34056a3STejun Heo {
797c8e55f36STejun Heo 	worker->flags |= WORKER_STARTED;
798c8e55f36STejun Heo 	worker->gcwq->nr_workers++;
799c8e55f36STejun Heo 	worker_enter_idle(worker);
800c34056a3STejun Heo 	wake_up_process(worker->task);
801c34056a3STejun Heo }
802c34056a3STejun Heo 
803c34056a3STejun Heo /**
804c34056a3STejun Heo  * destroy_worker - destroy a workqueue worker
805c34056a3STejun Heo  * @worker: worker to be destroyed
806c34056a3STejun Heo  *
807c8e55f36STejun Heo  * Destroy @worker and adjust @gcwq stats accordingly.
808c8e55f36STejun Heo  *
809c8e55f36STejun Heo  * CONTEXT:
810c8e55f36STejun Heo  * spin_lock_irq(gcwq->lock) which is released and regrabbed.
811c34056a3STejun Heo  */
812c34056a3STejun Heo static void destroy_worker(struct worker *worker)
813c34056a3STejun Heo {
8148b03ae3cSTejun Heo 	struct global_cwq *gcwq = worker->gcwq;
815c34056a3STejun Heo 	int id = worker->id;
816c34056a3STejun Heo 
817c34056a3STejun Heo 	/* sanity check frenzy */
818c34056a3STejun Heo 	BUG_ON(worker->current_work);
819affee4b2STejun Heo 	BUG_ON(!list_empty(&worker->scheduled));
820c34056a3STejun Heo 
821c8e55f36STejun Heo 	if (worker->flags & WORKER_STARTED)
822c8e55f36STejun Heo 		gcwq->nr_workers--;
823c8e55f36STejun Heo 	if (worker->flags & WORKER_IDLE)
824c8e55f36STejun Heo 		gcwq->nr_idle--;
825c8e55f36STejun Heo 
826c8e55f36STejun Heo 	list_del_init(&worker->entry);
827c8e55f36STejun Heo 	worker->flags |= WORKER_DIE;
828c8e55f36STejun Heo 
829c8e55f36STejun Heo 	spin_unlock_irq(&gcwq->lock);
830c8e55f36STejun Heo 
831c34056a3STejun Heo 	kthread_stop(worker->task);
832c34056a3STejun Heo 	kfree(worker);
833c34056a3STejun Heo 
8348b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
8358b03ae3cSTejun Heo 	ida_remove(&gcwq->worker_ida, id);
836c34056a3STejun Heo }
837c34056a3STejun Heo 
838a62428c0STejun Heo /**
839affee4b2STejun Heo  * move_linked_works - move linked works to a list
840affee4b2STejun Heo  * @work: start of series of works to be scheduled
841affee4b2STejun Heo  * @head: target list to append @work to
842affee4b2STejun Heo  * @nextp: out paramter for nested worklist walking
843affee4b2STejun Heo  *
844affee4b2STejun Heo  * Schedule linked works starting from @work to @head.  Work series to
845affee4b2STejun Heo  * be scheduled starts at @work and includes any consecutive work with
846affee4b2STejun Heo  * WORK_STRUCT_LINKED set in its predecessor.
847affee4b2STejun Heo  *
848affee4b2STejun Heo  * If @nextp is not NULL, it's updated to point to the next work of
849affee4b2STejun Heo  * the last scheduled work.  This allows move_linked_works() to be
850affee4b2STejun Heo  * nested inside outer list_for_each_entry_safe().
851affee4b2STejun Heo  *
852affee4b2STejun Heo  * CONTEXT:
8538b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock).
854affee4b2STejun Heo  */
855affee4b2STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head,
856affee4b2STejun Heo 			      struct work_struct **nextp)
857affee4b2STejun Heo {
858affee4b2STejun Heo 	struct work_struct *n;
859affee4b2STejun Heo 
860affee4b2STejun Heo 	/*
861affee4b2STejun Heo 	 * Linked worklist will always end before the end of the list,
862affee4b2STejun Heo 	 * use NULL for list head.
863affee4b2STejun Heo 	 */
864affee4b2STejun Heo 	list_for_each_entry_safe_from(work, n, NULL, entry) {
865affee4b2STejun Heo 		list_move_tail(&work->entry, head);
866affee4b2STejun Heo 		if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
867affee4b2STejun Heo 			break;
868affee4b2STejun Heo 	}
869affee4b2STejun Heo 
870affee4b2STejun Heo 	/*
871affee4b2STejun Heo 	 * If we're already inside safe list traversal and have moved
872affee4b2STejun Heo 	 * multiple works to the scheduled queue, the next position
873affee4b2STejun Heo 	 * needs to be updated.
874affee4b2STejun Heo 	 */
875affee4b2STejun Heo 	if (nextp)
876affee4b2STejun Heo 		*nextp = n;
877affee4b2STejun Heo }
878affee4b2STejun Heo 
8791e19ffc6STejun Heo static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
8801e19ffc6STejun Heo {
8811e19ffc6STejun Heo 	struct work_struct *work = list_first_entry(&cwq->delayed_works,
8821e19ffc6STejun Heo 						    struct work_struct, entry);
8831e19ffc6STejun Heo 
8841e19ffc6STejun Heo 	move_linked_works(work, &cwq->worklist, NULL);
8851e19ffc6STejun Heo 	cwq->nr_active++;
8861e19ffc6STejun Heo }
8871e19ffc6STejun Heo 
888affee4b2STejun Heo /**
88973f53c4aSTejun Heo  * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
89073f53c4aSTejun Heo  * @cwq: cwq of interest
89173f53c4aSTejun Heo  * @color: color of work which left the queue
89273f53c4aSTejun Heo  *
89373f53c4aSTejun Heo  * A work either has completed or is removed from pending queue,
89473f53c4aSTejun Heo  * decrement nr_in_flight of its cwq and handle workqueue flushing.
89573f53c4aSTejun Heo  *
89673f53c4aSTejun Heo  * CONTEXT:
8978b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock).
89873f53c4aSTejun Heo  */
89973f53c4aSTejun Heo static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color)
90073f53c4aSTejun Heo {
90173f53c4aSTejun Heo 	/* ignore uncolored works */
90273f53c4aSTejun Heo 	if (color == WORK_NO_COLOR)
90373f53c4aSTejun Heo 		return;
90473f53c4aSTejun Heo 
90573f53c4aSTejun Heo 	cwq->nr_in_flight[color]--;
9061e19ffc6STejun Heo 	cwq->nr_active--;
9071e19ffc6STejun Heo 
908502ca9d8STejun Heo 	if (!list_empty(&cwq->delayed_works)) {
9091e19ffc6STejun Heo 		/* one down, submit a delayed one */
910502ca9d8STejun Heo 		if (cwq->nr_active < cwq->max_active)
9111e19ffc6STejun Heo 			cwq_activate_first_delayed(cwq);
912502ca9d8STejun Heo 	} else if (!cwq->nr_active && cwq->wq->flags & WQ_SINGLE_CPU) {
913502ca9d8STejun Heo 		/* this was the last work, unbind from single cpu */
914502ca9d8STejun Heo 		cwq_unbind_single_cpu(cwq);
915502ca9d8STejun Heo 	}
91673f53c4aSTejun Heo 
91773f53c4aSTejun Heo 	/* is flush in progress and are we at the flushing tip? */
91873f53c4aSTejun Heo 	if (likely(cwq->flush_color != color))
91973f53c4aSTejun Heo 		return;
92073f53c4aSTejun Heo 
92173f53c4aSTejun Heo 	/* are there still in-flight works? */
92273f53c4aSTejun Heo 	if (cwq->nr_in_flight[color])
92373f53c4aSTejun Heo 		return;
92473f53c4aSTejun Heo 
92573f53c4aSTejun Heo 	/* this cwq is done, clear flush_color */
92673f53c4aSTejun Heo 	cwq->flush_color = -1;
92773f53c4aSTejun Heo 
92873f53c4aSTejun Heo 	/*
92973f53c4aSTejun Heo 	 * If this was the last cwq, wake up the first flusher.  It
93073f53c4aSTejun Heo 	 * will handle the rest.
93173f53c4aSTejun Heo 	 */
93273f53c4aSTejun Heo 	if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
93373f53c4aSTejun Heo 		complete(&cwq->wq->first_flusher->done);
93473f53c4aSTejun Heo }
93573f53c4aSTejun Heo 
93673f53c4aSTejun Heo /**
937a62428c0STejun Heo  * process_one_work - process single work
938c34056a3STejun Heo  * @worker: self
939a62428c0STejun Heo  * @work: work to process
940a62428c0STejun Heo  *
941a62428c0STejun Heo  * Process @work.  This function contains all the logics necessary to
942a62428c0STejun Heo  * process a single work including synchronization against and
943a62428c0STejun Heo  * interaction with other workers on the same cpu, queueing and
944a62428c0STejun Heo  * flushing.  As long as context requirement is met, any worker can
945a62428c0STejun Heo  * call this function to process a work.
946a62428c0STejun Heo  *
947a62428c0STejun Heo  * CONTEXT:
9488b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock) which is released and regrabbed.
949a62428c0STejun Heo  */
950c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work)
9511da177e4SLinus Torvalds {
952c34056a3STejun Heo 	struct cpu_workqueue_struct *cwq = worker->cwq;
9538b03ae3cSTejun Heo 	struct global_cwq *gcwq = cwq->gcwq;
954c8e55f36STejun Heo 	struct hlist_head *bwh = busy_worker_head(gcwq, work);
9556bb49e59SDavid Howells 	work_func_t f = work->func;
95673f53c4aSTejun Heo 	int work_color;
9574e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
9584e6045f1SJohannes Berg 	/*
959a62428c0STejun Heo 	 * It is permissible to free the struct work_struct from
960a62428c0STejun Heo 	 * inside the function that is called from it, this we need to
961a62428c0STejun Heo 	 * take into account for lockdep too.  To avoid bogus "held
962a62428c0STejun Heo 	 * lock freed" warnings as well as problems when looking into
963a62428c0STejun Heo 	 * work->lockdep_map, make a copy and use that here.
9644e6045f1SJohannes Berg 	 */
9654e6045f1SJohannes Berg 	struct lockdep_map lockdep_map = work->lockdep_map;
9664e6045f1SJohannes Berg #endif
967a62428c0STejun Heo 	/* claim and process */
968dc186ad7SThomas Gleixner 	debug_work_deactivate(work);
969c8e55f36STejun Heo 	hlist_add_head(&worker->hentry, bwh);
970c34056a3STejun Heo 	worker->current_work = work;
971*8cca0eeaSTejun Heo 	worker->current_cwq = cwq;
97273f53c4aSTejun Heo 	work_color = get_work_color(work);
973a62428c0STejun Heo 	list_del_init(&work->entry);
974a62428c0STejun Heo 
9758b03ae3cSTejun Heo 	spin_unlock_irq(&gcwq->lock);
9761da177e4SLinus Torvalds 
977365970a1SDavid Howells 	BUG_ON(get_wq_data(work) != cwq);
97823b2e599SOleg Nesterov 	work_clear_pending(work);
9793295f0efSIngo Molnar 	lock_map_acquire(&cwq->wq->lockdep_map);
9803295f0efSIngo Molnar 	lock_map_acquire(&lockdep_map);
98165f27f38SDavid Howells 	f(work);
9823295f0efSIngo Molnar 	lock_map_release(&lockdep_map);
9833295f0efSIngo Molnar 	lock_map_release(&cwq->wq->lockdep_map);
9841da177e4SLinus Torvalds 
985d5abe669SPeter Zijlstra 	if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
986d5abe669SPeter Zijlstra 		printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
987d5abe669SPeter Zijlstra 		       "%s/0x%08x/%d\n",
988a62428c0STejun Heo 		       current->comm, preempt_count(), task_pid_nr(current));
989d5abe669SPeter Zijlstra 		printk(KERN_ERR "    last function: ");
990d5abe669SPeter Zijlstra 		print_symbol("%s\n", (unsigned long)f);
991d5abe669SPeter Zijlstra 		debug_show_held_locks(current);
992d5abe669SPeter Zijlstra 		dump_stack();
993d5abe669SPeter Zijlstra 	}
994d5abe669SPeter Zijlstra 
9958b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
996a62428c0STejun Heo 
997a62428c0STejun Heo 	/* we're done with it, release */
998c8e55f36STejun Heo 	hlist_del_init(&worker->hentry);
999c34056a3STejun Heo 	worker->current_work = NULL;
1000*8cca0eeaSTejun Heo 	worker->current_cwq = NULL;
100173f53c4aSTejun Heo 	cwq_dec_nr_in_flight(cwq, work_color);
10021da177e4SLinus Torvalds }
1003a62428c0STejun Heo 
1004affee4b2STejun Heo /**
1005affee4b2STejun Heo  * process_scheduled_works - process scheduled works
1006affee4b2STejun Heo  * @worker: self
1007affee4b2STejun Heo  *
1008affee4b2STejun Heo  * Process all scheduled works.  Please note that the scheduled list
1009affee4b2STejun Heo  * may change while processing a work, so this function repeatedly
1010affee4b2STejun Heo  * fetches a work from the top and executes it.
1011affee4b2STejun Heo  *
1012affee4b2STejun Heo  * CONTEXT:
10138b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1014affee4b2STejun Heo  * multiple times.
1015affee4b2STejun Heo  */
1016affee4b2STejun Heo static void process_scheduled_works(struct worker *worker)
1017a62428c0STejun Heo {
1018affee4b2STejun Heo 	while (!list_empty(&worker->scheduled)) {
1019affee4b2STejun Heo 		struct work_struct *work = list_first_entry(&worker->scheduled,
1020a62428c0STejun Heo 						struct work_struct, entry);
1021c34056a3STejun Heo 		process_one_work(worker, work);
1022a62428c0STejun Heo 	}
10231da177e4SLinus Torvalds }
10241da177e4SLinus Torvalds 
10254690c4abSTejun Heo /**
10264690c4abSTejun Heo  * worker_thread - the worker thread function
1027c34056a3STejun Heo  * @__worker: self
10284690c4abSTejun Heo  *
10294690c4abSTejun Heo  * The cwq worker thread function.
10304690c4abSTejun Heo  */
1031c34056a3STejun Heo static int worker_thread(void *__worker)
10321da177e4SLinus Torvalds {
1033c34056a3STejun Heo 	struct worker *worker = __worker;
10348b03ae3cSTejun Heo 	struct global_cwq *gcwq = worker->gcwq;
1035c34056a3STejun Heo 	struct cpu_workqueue_struct *cwq = worker->cwq;
10361da177e4SLinus Torvalds 
1037c8e55f36STejun Heo woke_up:
10388b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
1039affee4b2STejun Heo 
1040c8e55f36STejun Heo 	/* DIE can be set only while we're idle, checking here is enough */
1041c8e55f36STejun Heo 	if (worker->flags & WORKER_DIE) {
1042c8e55f36STejun Heo 		spin_unlock_irq(&gcwq->lock);
1043c8e55f36STejun Heo 		return 0;
1044c8e55f36STejun Heo 	}
1045c8e55f36STejun Heo 
1046c8e55f36STejun Heo 	worker_leave_idle(worker);
1047db7bccf4STejun Heo recheck:
1048c8e55f36STejun Heo 	/*
1049c8e55f36STejun Heo 	 * ->scheduled list can only be filled while a worker is
1050c8e55f36STejun Heo 	 * preparing to process a work or actually processing it.
1051c8e55f36STejun Heo 	 * Make sure nobody diddled with it while I was sleeping.
1052c8e55f36STejun Heo 	 */
1053c8e55f36STejun Heo 	BUG_ON(!list_empty(&worker->scheduled));
1054c8e55f36STejun Heo 
1055affee4b2STejun Heo 	while (!list_empty(&cwq->worklist)) {
1056affee4b2STejun Heo 		struct work_struct *work =
1057affee4b2STejun Heo 			list_first_entry(&cwq->worklist,
1058affee4b2STejun Heo 					 struct work_struct, entry);
1059affee4b2STejun Heo 
1060db7bccf4STejun Heo 		/*
1061db7bccf4STejun Heo 		 * The following is a rather inefficient way to close
1062db7bccf4STejun Heo 		 * race window against cpu hotplug operations.  Will
1063db7bccf4STejun Heo 		 * be replaced soon.
1064db7bccf4STejun Heo 		 */
1065db7bccf4STejun Heo 		if (unlikely(!(worker->flags & WORKER_ROGUE) &&
1066db7bccf4STejun Heo 			     !cpumask_equal(&worker->task->cpus_allowed,
1067db7bccf4STejun Heo 					    get_cpu_mask(gcwq->cpu)))) {
1068db7bccf4STejun Heo 			spin_unlock_irq(&gcwq->lock);
1069db7bccf4STejun Heo 			set_cpus_allowed_ptr(worker->task,
1070db7bccf4STejun Heo 					     get_cpu_mask(gcwq->cpu));
1071db7bccf4STejun Heo 			cpu_relax();
1072db7bccf4STejun Heo 			spin_lock_irq(&gcwq->lock);
1073db7bccf4STejun Heo 			goto recheck;
1074db7bccf4STejun Heo 		}
1075db7bccf4STejun Heo 
1076c8e55f36STejun Heo 		if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
1077affee4b2STejun Heo 			/* optimization path, not strictly necessary */
1078affee4b2STejun Heo 			process_one_work(worker, work);
1079affee4b2STejun Heo 			if (unlikely(!list_empty(&worker->scheduled)))
1080affee4b2STejun Heo 				process_scheduled_works(worker);
1081affee4b2STejun Heo 		} else {
1082c8e55f36STejun Heo 			move_linked_works(work, &worker->scheduled, NULL);
1083affee4b2STejun Heo 			process_scheduled_works(worker);
1084affee4b2STejun Heo 		}
1085affee4b2STejun Heo 	}
1086affee4b2STejun Heo 
1087c8e55f36STejun Heo 	/*
1088c8e55f36STejun Heo 	 * gcwq->lock is held and there's no work to process, sleep.
1089c8e55f36STejun Heo 	 * Workers are woken up only while holding gcwq->lock, so
1090c8e55f36STejun Heo 	 * setting the current state before releasing gcwq->lock is
1091c8e55f36STejun Heo 	 * enough to prevent losing any event.
1092c8e55f36STejun Heo 	 */
1093c8e55f36STejun Heo 	worker_enter_idle(worker);
1094c8e55f36STejun Heo 	__set_current_state(TASK_INTERRUPTIBLE);
10958b03ae3cSTejun Heo 	spin_unlock_irq(&gcwq->lock);
1096c8e55f36STejun Heo 	schedule();
1097c8e55f36STejun Heo 	goto woke_up;
10981da177e4SLinus Torvalds }
10991da177e4SLinus Torvalds 
1100fc2e4d70SOleg Nesterov struct wq_barrier {
1101fc2e4d70SOleg Nesterov 	struct work_struct	work;
1102fc2e4d70SOleg Nesterov 	struct completion	done;
1103fc2e4d70SOleg Nesterov };
1104fc2e4d70SOleg Nesterov 
1105fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work)
1106fc2e4d70SOleg Nesterov {
1107fc2e4d70SOleg Nesterov 	struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
1108fc2e4d70SOleg Nesterov 	complete(&barr->done);
1109fc2e4d70SOleg Nesterov }
1110fc2e4d70SOleg Nesterov 
11114690c4abSTejun Heo /**
11124690c4abSTejun Heo  * insert_wq_barrier - insert a barrier work
11134690c4abSTejun Heo  * @cwq: cwq to insert barrier into
11144690c4abSTejun Heo  * @barr: wq_barrier to insert
1115affee4b2STejun Heo  * @target: target work to attach @barr to
1116affee4b2STejun Heo  * @worker: worker currently executing @target, NULL if @target is not executing
11174690c4abSTejun Heo  *
1118affee4b2STejun Heo  * @barr is linked to @target such that @barr is completed only after
1119affee4b2STejun Heo  * @target finishes execution.  Please note that the ordering
1120affee4b2STejun Heo  * guarantee is observed only with respect to @target and on the local
1121affee4b2STejun Heo  * cpu.
1122affee4b2STejun Heo  *
1123affee4b2STejun Heo  * Currently, a queued barrier can't be canceled.  This is because
1124affee4b2STejun Heo  * try_to_grab_pending() can't determine whether the work to be
1125affee4b2STejun Heo  * grabbed is at the head of the queue and thus can't clear LINKED
1126affee4b2STejun Heo  * flag of the previous work while there must be a valid next work
1127affee4b2STejun Heo  * after a work with LINKED flag set.
1128affee4b2STejun Heo  *
1129affee4b2STejun Heo  * Note that when @worker is non-NULL, @target may be modified
1130affee4b2STejun Heo  * underneath us, so we can't reliably determine cwq from @target.
11314690c4abSTejun Heo  *
11324690c4abSTejun Heo  * CONTEXT:
11338b03ae3cSTejun Heo  * spin_lock_irq(gcwq->lock).
11344690c4abSTejun Heo  */
113583c22520SOleg Nesterov static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
1136affee4b2STejun Heo 			      struct wq_barrier *barr,
1137affee4b2STejun Heo 			      struct work_struct *target, struct worker *worker)
1138fc2e4d70SOleg Nesterov {
1139affee4b2STejun Heo 	struct list_head *head;
1140affee4b2STejun Heo 	unsigned int linked = 0;
1141affee4b2STejun Heo 
1142dc186ad7SThomas Gleixner 	/*
11438b03ae3cSTejun Heo 	 * debugobject calls are safe here even with gcwq->lock locked
1144dc186ad7SThomas Gleixner 	 * as we know for sure that this will not trigger any of the
1145dc186ad7SThomas Gleixner 	 * checks and call back into the fixup functions where we
1146dc186ad7SThomas Gleixner 	 * might deadlock.
1147dc186ad7SThomas Gleixner 	 */
1148dc186ad7SThomas Gleixner 	INIT_WORK_ON_STACK(&barr->work, wq_barrier_func);
114922df02bbSTejun Heo 	__set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
1150fc2e4d70SOleg Nesterov 	init_completion(&barr->done);
115183c22520SOleg Nesterov 
1152affee4b2STejun Heo 	/*
1153affee4b2STejun Heo 	 * If @target is currently being executed, schedule the
1154affee4b2STejun Heo 	 * barrier to the worker; otherwise, put it after @target.
1155affee4b2STejun Heo 	 */
1156affee4b2STejun Heo 	if (worker)
1157affee4b2STejun Heo 		head = worker->scheduled.next;
1158affee4b2STejun Heo 	else {
1159affee4b2STejun Heo 		unsigned long *bits = work_data_bits(target);
1160affee4b2STejun Heo 
1161affee4b2STejun Heo 		head = target->entry.next;
1162affee4b2STejun Heo 		/* there can already be other linked works, inherit and set */
1163affee4b2STejun Heo 		linked = *bits & WORK_STRUCT_LINKED;
1164affee4b2STejun Heo 		__set_bit(WORK_STRUCT_LINKED_BIT, bits);
1165affee4b2STejun Heo 	}
1166affee4b2STejun Heo 
1167dc186ad7SThomas Gleixner 	debug_work_activate(&barr->work);
1168affee4b2STejun Heo 	insert_work(cwq, &barr->work, head,
1169affee4b2STejun Heo 		    work_color_to_flags(WORK_NO_COLOR) | linked);
1170fc2e4d70SOleg Nesterov }
1171fc2e4d70SOleg Nesterov 
117273f53c4aSTejun Heo /**
117373f53c4aSTejun Heo  * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
117473f53c4aSTejun Heo  * @wq: workqueue being flushed
117573f53c4aSTejun Heo  * @flush_color: new flush color, < 0 for no-op
117673f53c4aSTejun Heo  * @work_color: new work color, < 0 for no-op
117773f53c4aSTejun Heo  *
117873f53c4aSTejun Heo  * Prepare cwqs for workqueue flushing.
117973f53c4aSTejun Heo  *
118073f53c4aSTejun Heo  * If @flush_color is non-negative, flush_color on all cwqs should be
118173f53c4aSTejun Heo  * -1.  If no cwq has in-flight commands at the specified color, all
118273f53c4aSTejun Heo  * cwq->flush_color's stay at -1 and %false is returned.  If any cwq
118373f53c4aSTejun Heo  * has in flight commands, its cwq->flush_color is set to
118473f53c4aSTejun Heo  * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
118573f53c4aSTejun Heo  * wakeup logic is armed and %true is returned.
118673f53c4aSTejun Heo  *
118773f53c4aSTejun Heo  * The caller should have initialized @wq->first_flusher prior to
118873f53c4aSTejun Heo  * calling this function with non-negative @flush_color.  If
118973f53c4aSTejun Heo  * @flush_color is negative, no flush color update is done and %false
119073f53c4aSTejun Heo  * is returned.
119173f53c4aSTejun Heo  *
119273f53c4aSTejun Heo  * If @work_color is non-negative, all cwqs should have the same
119373f53c4aSTejun Heo  * work_color which is previous to @work_color and all will be
119473f53c4aSTejun Heo  * advanced to @work_color.
119573f53c4aSTejun Heo  *
119673f53c4aSTejun Heo  * CONTEXT:
119773f53c4aSTejun Heo  * mutex_lock(wq->flush_mutex).
119873f53c4aSTejun Heo  *
119973f53c4aSTejun Heo  * RETURNS:
120073f53c4aSTejun Heo  * %true if @flush_color >= 0 and there's something to flush.  %false
120173f53c4aSTejun Heo  * otherwise.
120273f53c4aSTejun Heo  */
120373f53c4aSTejun Heo static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
120473f53c4aSTejun Heo 				      int flush_color, int work_color)
12051da177e4SLinus Torvalds {
120673f53c4aSTejun Heo 	bool wait = false;
120773f53c4aSTejun Heo 	unsigned int cpu;
12081da177e4SLinus Torvalds 
120973f53c4aSTejun Heo 	if (flush_color >= 0) {
121073f53c4aSTejun Heo 		BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
121173f53c4aSTejun Heo 		atomic_set(&wq->nr_cwqs_to_flush, 1);
121273f53c4aSTejun Heo 	}
121373f53c4aSTejun Heo 
121473f53c4aSTejun Heo 	for_each_possible_cpu(cpu) {
121573f53c4aSTejun Heo 		struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
12168b03ae3cSTejun Heo 		struct global_cwq *gcwq = cwq->gcwq;
12172355b70fSLai Jiangshan 
12188b03ae3cSTejun Heo 		spin_lock_irq(&gcwq->lock);
121973f53c4aSTejun Heo 
122073f53c4aSTejun Heo 		if (flush_color >= 0) {
122173f53c4aSTejun Heo 			BUG_ON(cwq->flush_color != -1);
122273f53c4aSTejun Heo 
122373f53c4aSTejun Heo 			if (cwq->nr_in_flight[flush_color]) {
122473f53c4aSTejun Heo 				cwq->flush_color = flush_color;
122573f53c4aSTejun Heo 				atomic_inc(&wq->nr_cwqs_to_flush);
122673f53c4aSTejun Heo 				wait = true;
122783c22520SOleg Nesterov 			}
122873f53c4aSTejun Heo 		}
122973f53c4aSTejun Heo 
123073f53c4aSTejun Heo 		if (work_color >= 0) {
123173f53c4aSTejun Heo 			BUG_ON(work_color != work_next_color(cwq->work_color));
123273f53c4aSTejun Heo 			cwq->work_color = work_color;
123373f53c4aSTejun Heo 		}
123473f53c4aSTejun Heo 
12358b03ae3cSTejun Heo 		spin_unlock_irq(&gcwq->lock);
1236dc186ad7SThomas Gleixner 	}
123714441960SOleg Nesterov 
123873f53c4aSTejun Heo 	if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
123973f53c4aSTejun Heo 		complete(&wq->first_flusher->done);
124073f53c4aSTejun Heo 
124173f53c4aSTejun Heo 	return wait;
124283c22520SOleg Nesterov }
12431da177e4SLinus Torvalds 
12440fcb78c2SRolf Eike Beer /**
12451da177e4SLinus Torvalds  * flush_workqueue - ensure that any scheduled work has run to completion.
12460fcb78c2SRolf Eike Beer  * @wq: workqueue to flush
12471da177e4SLinus Torvalds  *
12481da177e4SLinus Torvalds  * Forces execution of the workqueue and blocks until its completion.
12491da177e4SLinus Torvalds  * This is typically used in driver shutdown handlers.
12501da177e4SLinus Torvalds  *
1251fc2e4d70SOleg Nesterov  * We sleep until all works which were queued on entry have been handled,
1252fc2e4d70SOleg Nesterov  * but we are not livelocked by new incoming ones.
12531da177e4SLinus Torvalds  */
12547ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq)
12551da177e4SLinus Torvalds {
125673f53c4aSTejun Heo 	struct wq_flusher this_flusher = {
125773f53c4aSTejun Heo 		.list = LIST_HEAD_INIT(this_flusher.list),
125873f53c4aSTejun Heo 		.flush_color = -1,
125973f53c4aSTejun Heo 		.done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
126073f53c4aSTejun Heo 	};
126173f53c4aSTejun Heo 	int next_color;
1262b1f4ec17SOleg Nesterov 
12633295f0efSIngo Molnar 	lock_map_acquire(&wq->lockdep_map);
12643295f0efSIngo Molnar 	lock_map_release(&wq->lockdep_map);
126573f53c4aSTejun Heo 
126673f53c4aSTejun Heo 	mutex_lock(&wq->flush_mutex);
126773f53c4aSTejun Heo 
126873f53c4aSTejun Heo 	/*
126973f53c4aSTejun Heo 	 * Start-to-wait phase
127073f53c4aSTejun Heo 	 */
127173f53c4aSTejun Heo 	next_color = work_next_color(wq->work_color);
127273f53c4aSTejun Heo 
127373f53c4aSTejun Heo 	if (next_color != wq->flush_color) {
127473f53c4aSTejun Heo 		/*
127573f53c4aSTejun Heo 		 * Color space is not full.  The current work_color
127673f53c4aSTejun Heo 		 * becomes our flush_color and work_color is advanced
127773f53c4aSTejun Heo 		 * by one.
127873f53c4aSTejun Heo 		 */
127973f53c4aSTejun Heo 		BUG_ON(!list_empty(&wq->flusher_overflow));
128073f53c4aSTejun Heo 		this_flusher.flush_color = wq->work_color;
128173f53c4aSTejun Heo 		wq->work_color = next_color;
128273f53c4aSTejun Heo 
128373f53c4aSTejun Heo 		if (!wq->first_flusher) {
128473f53c4aSTejun Heo 			/* no flush in progress, become the first flusher */
128573f53c4aSTejun Heo 			BUG_ON(wq->flush_color != this_flusher.flush_color);
128673f53c4aSTejun Heo 
128773f53c4aSTejun Heo 			wq->first_flusher = &this_flusher;
128873f53c4aSTejun Heo 
128973f53c4aSTejun Heo 			if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
129073f53c4aSTejun Heo 						       wq->work_color)) {
129173f53c4aSTejun Heo 				/* nothing to flush, done */
129273f53c4aSTejun Heo 				wq->flush_color = next_color;
129373f53c4aSTejun Heo 				wq->first_flusher = NULL;
129473f53c4aSTejun Heo 				goto out_unlock;
129573f53c4aSTejun Heo 			}
129673f53c4aSTejun Heo 		} else {
129773f53c4aSTejun Heo 			/* wait in queue */
129873f53c4aSTejun Heo 			BUG_ON(wq->flush_color == this_flusher.flush_color);
129973f53c4aSTejun Heo 			list_add_tail(&this_flusher.list, &wq->flusher_queue);
130073f53c4aSTejun Heo 			flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
130173f53c4aSTejun Heo 		}
130273f53c4aSTejun Heo 	} else {
130373f53c4aSTejun Heo 		/*
130473f53c4aSTejun Heo 		 * Oops, color space is full, wait on overflow queue.
130573f53c4aSTejun Heo 		 * The next flush completion will assign us
130673f53c4aSTejun Heo 		 * flush_color and transfer to flusher_queue.
130773f53c4aSTejun Heo 		 */
130873f53c4aSTejun Heo 		list_add_tail(&this_flusher.list, &wq->flusher_overflow);
130973f53c4aSTejun Heo 	}
131073f53c4aSTejun Heo 
131173f53c4aSTejun Heo 	mutex_unlock(&wq->flush_mutex);
131273f53c4aSTejun Heo 
131373f53c4aSTejun Heo 	wait_for_completion(&this_flusher.done);
131473f53c4aSTejun Heo 
131573f53c4aSTejun Heo 	/*
131673f53c4aSTejun Heo 	 * Wake-up-and-cascade phase
131773f53c4aSTejun Heo 	 *
131873f53c4aSTejun Heo 	 * First flushers are responsible for cascading flushes and
131973f53c4aSTejun Heo 	 * handling overflow.  Non-first flushers can simply return.
132073f53c4aSTejun Heo 	 */
132173f53c4aSTejun Heo 	if (wq->first_flusher != &this_flusher)
132273f53c4aSTejun Heo 		return;
132373f53c4aSTejun Heo 
132473f53c4aSTejun Heo 	mutex_lock(&wq->flush_mutex);
132573f53c4aSTejun Heo 
132673f53c4aSTejun Heo 	wq->first_flusher = NULL;
132773f53c4aSTejun Heo 
132873f53c4aSTejun Heo 	BUG_ON(!list_empty(&this_flusher.list));
132973f53c4aSTejun Heo 	BUG_ON(wq->flush_color != this_flusher.flush_color);
133073f53c4aSTejun Heo 
133173f53c4aSTejun Heo 	while (true) {
133273f53c4aSTejun Heo 		struct wq_flusher *next, *tmp;
133373f53c4aSTejun Heo 
133473f53c4aSTejun Heo 		/* complete all the flushers sharing the current flush color */
133573f53c4aSTejun Heo 		list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
133673f53c4aSTejun Heo 			if (next->flush_color != wq->flush_color)
133773f53c4aSTejun Heo 				break;
133873f53c4aSTejun Heo 			list_del_init(&next->list);
133973f53c4aSTejun Heo 			complete(&next->done);
134073f53c4aSTejun Heo 		}
134173f53c4aSTejun Heo 
134273f53c4aSTejun Heo 		BUG_ON(!list_empty(&wq->flusher_overflow) &&
134373f53c4aSTejun Heo 		       wq->flush_color != work_next_color(wq->work_color));
134473f53c4aSTejun Heo 
134573f53c4aSTejun Heo 		/* this flush_color is finished, advance by one */
134673f53c4aSTejun Heo 		wq->flush_color = work_next_color(wq->flush_color);
134773f53c4aSTejun Heo 
134873f53c4aSTejun Heo 		/* one color has been freed, handle overflow queue */
134973f53c4aSTejun Heo 		if (!list_empty(&wq->flusher_overflow)) {
135073f53c4aSTejun Heo 			/*
135173f53c4aSTejun Heo 			 * Assign the same color to all overflowed
135273f53c4aSTejun Heo 			 * flushers, advance work_color and append to
135373f53c4aSTejun Heo 			 * flusher_queue.  This is the start-to-wait
135473f53c4aSTejun Heo 			 * phase for these overflowed flushers.
135573f53c4aSTejun Heo 			 */
135673f53c4aSTejun Heo 			list_for_each_entry(tmp, &wq->flusher_overflow, list)
135773f53c4aSTejun Heo 				tmp->flush_color = wq->work_color;
135873f53c4aSTejun Heo 
135973f53c4aSTejun Heo 			wq->work_color = work_next_color(wq->work_color);
136073f53c4aSTejun Heo 
136173f53c4aSTejun Heo 			list_splice_tail_init(&wq->flusher_overflow,
136273f53c4aSTejun Heo 					      &wq->flusher_queue);
136373f53c4aSTejun Heo 			flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
136473f53c4aSTejun Heo 		}
136573f53c4aSTejun Heo 
136673f53c4aSTejun Heo 		if (list_empty(&wq->flusher_queue)) {
136773f53c4aSTejun Heo 			BUG_ON(wq->flush_color != wq->work_color);
136873f53c4aSTejun Heo 			break;
136973f53c4aSTejun Heo 		}
137073f53c4aSTejun Heo 
137173f53c4aSTejun Heo 		/*
137273f53c4aSTejun Heo 		 * Need to flush more colors.  Make the next flusher
137373f53c4aSTejun Heo 		 * the new first flusher and arm cwqs.
137473f53c4aSTejun Heo 		 */
137573f53c4aSTejun Heo 		BUG_ON(wq->flush_color == wq->work_color);
137673f53c4aSTejun Heo 		BUG_ON(wq->flush_color != next->flush_color);
137773f53c4aSTejun Heo 
137873f53c4aSTejun Heo 		list_del_init(&next->list);
137973f53c4aSTejun Heo 		wq->first_flusher = next;
138073f53c4aSTejun Heo 
138173f53c4aSTejun Heo 		if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
138273f53c4aSTejun Heo 			break;
138373f53c4aSTejun Heo 
138473f53c4aSTejun Heo 		/*
138573f53c4aSTejun Heo 		 * Meh... this color is already done, clear first
138673f53c4aSTejun Heo 		 * flusher and repeat cascading.
138773f53c4aSTejun Heo 		 */
138873f53c4aSTejun Heo 		wq->first_flusher = NULL;
138973f53c4aSTejun Heo 	}
139073f53c4aSTejun Heo 
139173f53c4aSTejun Heo out_unlock:
139273f53c4aSTejun Heo 	mutex_unlock(&wq->flush_mutex);
13931da177e4SLinus Torvalds }
1394ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(flush_workqueue);
13951da177e4SLinus Torvalds 
1396db700897SOleg Nesterov /**
1397db700897SOleg Nesterov  * flush_work - block until a work_struct's callback has terminated
1398db700897SOleg Nesterov  * @work: the work which is to be flushed
1399db700897SOleg Nesterov  *
1400a67da70dSOleg Nesterov  * Returns false if @work has already terminated.
1401a67da70dSOleg Nesterov  *
1402db700897SOleg Nesterov  * It is expected that, prior to calling flush_work(), the caller has
1403db700897SOleg Nesterov  * arranged for the work to not be requeued, otherwise it doesn't make
1404db700897SOleg Nesterov  * sense to use this function.
1405db700897SOleg Nesterov  */
1406db700897SOleg Nesterov int flush_work(struct work_struct *work)
1407db700897SOleg Nesterov {
1408affee4b2STejun Heo 	struct worker *worker = NULL;
1409db700897SOleg Nesterov 	struct cpu_workqueue_struct *cwq;
14108b03ae3cSTejun Heo 	struct global_cwq *gcwq;
1411db700897SOleg Nesterov 	struct wq_barrier barr;
1412db700897SOleg Nesterov 
1413db700897SOleg Nesterov 	might_sleep();
1414db700897SOleg Nesterov 	cwq = get_wq_data(work);
1415db700897SOleg Nesterov 	if (!cwq)
1416db700897SOleg Nesterov 		return 0;
14178b03ae3cSTejun Heo 	gcwq = cwq->gcwq;
1418db700897SOleg Nesterov 
14193295f0efSIngo Molnar 	lock_map_acquire(&cwq->wq->lockdep_map);
14203295f0efSIngo Molnar 	lock_map_release(&cwq->wq->lockdep_map);
1421a67da70dSOleg Nesterov 
14228b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
1423db700897SOleg Nesterov 	if (!list_empty(&work->entry)) {
1424db700897SOleg Nesterov 		/*
1425db700897SOleg Nesterov 		 * See the comment near try_to_grab_pending()->smp_rmb().
1426db700897SOleg Nesterov 		 * If it was re-queued under us we are not going to wait.
1427db700897SOleg Nesterov 		 */
1428db700897SOleg Nesterov 		smp_rmb();
1429db700897SOleg Nesterov 		if (unlikely(cwq != get_wq_data(work)))
14304690c4abSTejun Heo 			goto already_gone;
1431db700897SOleg Nesterov 	} else {
1432affee4b2STejun Heo 		if (cwq->worker && cwq->worker->current_work == work)
1433affee4b2STejun Heo 			worker = cwq->worker;
1434affee4b2STejun Heo 		if (!worker)
14354690c4abSTejun Heo 			goto already_gone;
1436db700897SOleg Nesterov 	}
1437db700897SOleg Nesterov 
1438affee4b2STejun Heo 	insert_wq_barrier(cwq, &barr, work, worker);
14398b03ae3cSTejun Heo 	spin_unlock_irq(&gcwq->lock);
1440db700897SOleg Nesterov 	wait_for_completion(&barr.done);
1441dc186ad7SThomas Gleixner 	destroy_work_on_stack(&barr.work);
1442db700897SOleg Nesterov 	return 1;
14434690c4abSTejun Heo already_gone:
14448b03ae3cSTejun Heo 	spin_unlock_irq(&gcwq->lock);
14454690c4abSTejun Heo 	return 0;
1446db700897SOleg Nesterov }
1447db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work);
1448db700897SOleg Nesterov 
14496e84d644SOleg Nesterov /*
14501f1f642eSOleg Nesterov  * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
14516e84d644SOleg Nesterov  * so this work can't be re-armed in any way.
14526e84d644SOleg Nesterov  */
14536e84d644SOleg Nesterov static int try_to_grab_pending(struct work_struct *work)
14546e84d644SOleg Nesterov {
14558b03ae3cSTejun Heo 	struct global_cwq *gcwq;
14566e84d644SOleg Nesterov 	struct cpu_workqueue_struct *cwq;
14571f1f642eSOleg Nesterov 	int ret = -1;
14586e84d644SOleg Nesterov 
145922df02bbSTejun Heo 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
14601f1f642eSOleg Nesterov 		return 0;
14616e84d644SOleg Nesterov 
14626e84d644SOleg Nesterov 	/*
14636e84d644SOleg Nesterov 	 * The queueing is in progress, or it is already queued. Try to
14646e84d644SOleg Nesterov 	 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
14656e84d644SOleg Nesterov 	 */
14666e84d644SOleg Nesterov 
14676e84d644SOleg Nesterov 	cwq = get_wq_data(work);
14686e84d644SOleg Nesterov 	if (!cwq)
14696e84d644SOleg Nesterov 		return ret;
14708b03ae3cSTejun Heo 	gcwq = cwq->gcwq;
14716e84d644SOleg Nesterov 
14728b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
14736e84d644SOleg Nesterov 	if (!list_empty(&work->entry)) {
14746e84d644SOleg Nesterov 		/*
14756e84d644SOleg Nesterov 		 * This work is queued, but perhaps we locked the wrong cwq.
14766e84d644SOleg Nesterov 		 * In that case we must see the new value after rmb(), see
14776e84d644SOleg Nesterov 		 * insert_work()->wmb().
14786e84d644SOleg Nesterov 		 */
14796e84d644SOleg Nesterov 		smp_rmb();
14806e84d644SOleg Nesterov 		if (cwq == get_wq_data(work)) {
1481dc186ad7SThomas Gleixner 			debug_work_deactivate(work);
14826e84d644SOleg Nesterov 			list_del_init(&work->entry);
148373f53c4aSTejun Heo 			cwq_dec_nr_in_flight(cwq, get_work_color(work));
14846e84d644SOleg Nesterov 			ret = 1;
14856e84d644SOleg Nesterov 		}
14866e84d644SOleg Nesterov 	}
14878b03ae3cSTejun Heo 	spin_unlock_irq(&gcwq->lock);
14886e84d644SOleg Nesterov 
14896e84d644SOleg Nesterov 	return ret;
14906e84d644SOleg Nesterov }
14916e84d644SOleg Nesterov 
14926e84d644SOleg Nesterov static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq,
1493b89deed3SOleg Nesterov 				struct work_struct *work)
1494b89deed3SOleg Nesterov {
14958b03ae3cSTejun Heo 	struct global_cwq *gcwq = cwq->gcwq;
1496b89deed3SOleg Nesterov 	struct wq_barrier barr;
1497affee4b2STejun Heo 	struct worker *worker;
1498b89deed3SOleg Nesterov 
14998b03ae3cSTejun Heo 	spin_lock_irq(&gcwq->lock);
1500affee4b2STejun Heo 
1501affee4b2STejun Heo 	worker = NULL;
1502c34056a3STejun Heo 	if (unlikely(cwq->worker && cwq->worker->current_work == work)) {
1503affee4b2STejun Heo 		worker = cwq->worker;
1504affee4b2STejun Heo 		insert_wq_barrier(cwq, &barr, work, worker);
1505b89deed3SOleg Nesterov 	}
1506affee4b2STejun Heo 
15078b03ae3cSTejun Heo 	spin_unlock_irq(&gcwq->lock);
1508b89deed3SOleg Nesterov 
1509affee4b2STejun Heo 	if (unlikely(worker)) {
1510b89deed3SOleg Nesterov 		wait_for_completion(&barr.done);
1511dc186ad7SThomas Gleixner 		destroy_work_on_stack(&barr.work);
1512dc186ad7SThomas Gleixner 	}
1513b89deed3SOleg Nesterov }
1514b89deed3SOleg Nesterov 
15156e84d644SOleg Nesterov static void wait_on_work(struct work_struct *work)
1516b89deed3SOleg Nesterov {
1517b89deed3SOleg Nesterov 	struct cpu_workqueue_struct *cwq;
151828e53bddSOleg Nesterov 	struct workqueue_struct *wq;
1519b1f4ec17SOleg Nesterov 	int cpu;
1520b89deed3SOleg Nesterov 
1521f293ea92SOleg Nesterov 	might_sleep();
1522f293ea92SOleg Nesterov 
15233295f0efSIngo Molnar 	lock_map_acquire(&work->lockdep_map);
15243295f0efSIngo Molnar 	lock_map_release(&work->lockdep_map);
15254e6045f1SJohannes Berg 
1526b89deed3SOleg Nesterov 	cwq = get_wq_data(work);
1527b89deed3SOleg Nesterov 	if (!cwq)
15283af24433SOleg Nesterov 		return;
1529b89deed3SOleg Nesterov 
153028e53bddSOleg Nesterov 	wq = cwq->wq;
153128e53bddSOleg Nesterov 
15321537663fSTejun Heo 	for_each_possible_cpu(cpu)
15334690c4abSTejun Heo 		wait_on_cpu_work(get_cwq(cpu, wq), work);
15346e84d644SOleg Nesterov }
15356e84d644SOleg Nesterov 
15361f1f642eSOleg Nesterov static int __cancel_work_timer(struct work_struct *work,
15371f1f642eSOleg Nesterov 				struct timer_list* timer)
15381f1f642eSOleg Nesterov {
15391f1f642eSOleg Nesterov 	int ret;
15401f1f642eSOleg Nesterov 
15411f1f642eSOleg Nesterov 	do {
15421f1f642eSOleg Nesterov 		ret = (timer && likely(del_timer(timer)));
15431f1f642eSOleg Nesterov 		if (!ret)
15441f1f642eSOleg Nesterov 			ret = try_to_grab_pending(work);
15451f1f642eSOleg Nesterov 		wait_on_work(work);
15461f1f642eSOleg Nesterov 	} while (unlikely(ret < 0));
15471f1f642eSOleg Nesterov 
15484d707b9fSOleg Nesterov 	clear_wq_data(work);
15491f1f642eSOleg Nesterov 	return ret;
15501f1f642eSOleg Nesterov }
15511f1f642eSOleg Nesterov 
15526e84d644SOleg Nesterov /**
15536e84d644SOleg Nesterov  * cancel_work_sync - block until a work_struct's callback has terminated
15546e84d644SOleg Nesterov  * @work: the work which is to be flushed
15556e84d644SOleg Nesterov  *
15561f1f642eSOleg Nesterov  * Returns true if @work was pending.
15571f1f642eSOleg Nesterov  *
15586e84d644SOleg Nesterov  * cancel_work_sync() will cancel the work if it is queued. If the work's
15596e84d644SOleg Nesterov  * callback appears to be running, cancel_work_sync() will block until it
15606e84d644SOleg Nesterov  * has completed.
15616e84d644SOleg Nesterov  *
15626e84d644SOleg Nesterov  * It is possible to use this function if the work re-queues itself. It can
15636e84d644SOleg Nesterov  * cancel the work even if it migrates to another workqueue, however in that
15646e84d644SOleg Nesterov  * case it only guarantees that work->func() has completed on the last queued
15656e84d644SOleg Nesterov  * workqueue.
15666e84d644SOleg Nesterov  *
15676e84d644SOleg Nesterov  * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
15686e84d644SOleg Nesterov  * pending, otherwise it goes into a busy-wait loop until the timer expires.
15696e84d644SOleg Nesterov  *
15706e84d644SOleg Nesterov  * The caller must ensure that workqueue_struct on which this work was last
15716e84d644SOleg Nesterov  * queued can't be destroyed before this function returns.
15726e84d644SOleg Nesterov  */
15731f1f642eSOleg Nesterov int cancel_work_sync(struct work_struct *work)
15746e84d644SOleg Nesterov {
15751f1f642eSOleg Nesterov 	return __cancel_work_timer(work, NULL);
1576b89deed3SOleg Nesterov }
157728e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync);
1578b89deed3SOleg Nesterov 
15796e84d644SOleg Nesterov /**
1580f5a421a4SOleg Nesterov  * cancel_delayed_work_sync - reliably kill off a delayed work.
15816e84d644SOleg Nesterov  * @dwork: the delayed work struct
15826e84d644SOleg Nesterov  *
15831f1f642eSOleg Nesterov  * Returns true if @dwork was pending.
15841f1f642eSOleg Nesterov  *
15856e84d644SOleg Nesterov  * It is possible to use this function if @dwork rearms itself via queue_work()
15866e84d644SOleg Nesterov  * or queue_delayed_work(). See also the comment for cancel_work_sync().
15876e84d644SOleg Nesterov  */
15881f1f642eSOleg Nesterov int cancel_delayed_work_sync(struct delayed_work *dwork)
15896e84d644SOleg Nesterov {
15901f1f642eSOleg Nesterov 	return __cancel_work_timer(&dwork->work, &dwork->timer);
15916e84d644SOleg Nesterov }
1592f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync);
15931da177e4SLinus Torvalds 
15946e84d644SOleg Nesterov static struct workqueue_struct *keventd_wq __read_mostly;
15951da177e4SLinus Torvalds 
15960fcb78c2SRolf Eike Beer /**
15970fcb78c2SRolf Eike Beer  * schedule_work - put work task in global workqueue
15980fcb78c2SRolf Eike Beer  * @work: job to be done
15990fcb78c2SRolf Eike Beer  *
16005b0f437dSBart Van Assche  * Returns zero if @work was already on the kernel-global workqueue and
16015b0f437dSBart Van Assche  * non-zero otherwise.
16025b0f437dSBart Van Assche  *
16035b0f437dSBart Van Assche  * This puts a job in the kernel-global workqueue if it was not already
16045b0f437dSBart Van Assche  * queued and leaves it in the same position on the kernel-global
16055b0f437dSBart Van Assche  * workqueue otherwise.
16060fcb78c2SRolf Eike Beer  */
16077ad5b3a5SHarvey Harrison int schedule_work(struct work_struct *work)
16081da177e4SLinus Torvalds {
16091da177e4SLinus Torvalds 	return queue_work(keventd_wq, work);
16101da177e4SLinus Torvalds }
1611ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_work);
16121da177e4SLinus Torvalds 
1613c1a220e7SZhang Rui /*
1614c1a220e7SZhang Rui  * schedule_work_on - put work task on a specific cpu
1615c1a220e7SZhang Rui  * @cpu: cpu to put the work task on
1616c1a220e7SZhang Rui  * @work: job to be done
1617c1a220e7SZhang Rui  *
1618c1a220e7SZhang Rui  * This puts a job on a specific cpu
1619c1a220e7SZhang Rui  */
1620c1a220e7SZhang Rui int schedule_work_on(int cpu, struct work_struct *work)
1621c1a220e7SZhang Rui {
1622c1a220e7SZhang Rui 	return queue_work_on(cpu, keventd_wq, work);
1623c1a220e7SZhang Rui }
1624c1a220e7SZhang Rui EXPORT_SYMBOL(schedule_work_on);
1625c1a220e7SZhang Rui 
16260fcb78c2SRolf Eike Beer /**
16270fcb78c2SRolf Eike Beer  * schedule_delayed_work - put work task in global workqueue after delay
162852bad64dSDavid Howells  * @dwork: job to be done
162952bad64dSDavid Howells  * @delay: number of jiffies to wait or 0 for immediate execution
16300fcb78c2SRolf Eike Beer  *
16310fcb78c2SRolf Eike Beer  * After waiting for a given time this puts a job in the kernel-global
16320fcb78c2SRolf Eike Beer  * workqueue.
16330fcb78c2SRolf Eike Beer  */
16347ad5b3a5SHarvey Harrison int schedule_delayed_work(struct delayed_work *dwork,
163582f67cd9SIngo Molnar 					unsigned long delay)
16361da177e4SLinus Torvalds {
163752bad64dSDavid Howells 	return queue_delayed_work(keventd_wq, dwork, delay);
16381da177e4SLinus Torvalds }
1639ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work);
16401da177e4SLinus Torvalds 
16410fcb78c2SRolf Eike Beer /**
16428c53e463SLinus Torvalds  * flush_delayed_work - block until a dwork_struct's callback has terminated
16438c53e463SLinus Torvalds  * @dwork: the delayed work which is to be flushed
16448c53e463SLinus Torvalds  *
16458c53e463SLinus Torvalds  * Any timeout is cancelled, and any pending work is run immediately.
16468c53e463SLinus Torvalds  */
16478c53e463SLinus Torvalds void flush_delayed_work(struct delayed_work *dwork)
16488c53e463SLinus Torvalds {
16498c53e463SLinus Torvalds 	if (del_timer_sync(&dwork->timer)) {
16504690c4abSTejun Heo 		__queue_work(get_cpu(), get_wq_data(&dwork->work)->wq,
16514690c4abSTejun Heo 			     &dwork->work);
16528c53e463SLinus Torvalds 		put_cpu();
16538c53e463SLinus Torvalds 	}
16548c53e463SLinus Torvalds 	flush_work(&dwork->work);
16558c53e463SLinus Torvalds }
16568c53e463SLinus Torvalds EXPORT_SYMBOL(flush_delayed_work);
16578c53e463SLinus Torvalds 
16588c53e463SLinus Torvalds /**
16590fcb78c2SRolf Eike Beer  * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
16600fcb78c2SRolf Eike Beer  * @cpu: cpu to use
166152bad64dSDavid Howells  * @dwork: job to be done
16620fcb78c2SRolf Eike Beer  * @delay: number of jiffies to wait
16630fcb78c2SRolf Eike Beer  *
16640fcb78c2SRolf Eike Beer  * After waiting for a given time this puts a job in the kernel-global
16650fcb78c2SRolf Eike Beer  * workqueue on the specified CPU.
16660fcb78c2SRolf Eike Beer  */
16671da177e4SLinus Torvalds int schedule_delayed_work_on(int cpu,
166852bad64dSDavid Howells 			struct delayed_work *dwork, unsigned long delay)
16691da177e4SLinus Torvalds {
167052bad64dSDavid Howells 	return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
16711da177e4SLinus Torvalds }
1672ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work_on);
16731da177e4SLinus Torvalds 
1674b6136773SAndrew Morton /**
1675b6136773SAndrew Morton  * schedule_on_each_cpu - call a function on each online CPU from keventd
1676b6136773SAndrew Morton  * @func: the function to call
1677b6136773SAndrew Morton  *
1678b6136773SAndrew Morton  * Returns zero on success.
1679b6136773SAndrew Morton  * Returns -ve errno on failure.
1680b6136773SAndrew Morton  *
1681b6136773SAndrew Morton  * schedule_on_each_cpu() is very slow.
1682b6136773SAndrew Morton  */
168365f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func)
168415316ba8SChristoph Lameter {
168515316ba8SChristoph Lameter 	int cpu;
168665a64464SAndi Kleen 	int orig = -1;
1687b6136773SAndrew Morton 	struct work_struct *works;
168815316ba8SChristoph Lameter 
1689b6136773SAndrew Morton 	works = alloc_percpu(struct work_struct);
1690b6136773SAndrew Morton 	if (!works)
169115316ba8SChristoph Lameter 		return -ENOMEM;
1692b6136773SAndrew Morton 
169395402b38SGautham R Shenoy 	get_online_cpus();
169493981800STejun Heo 
169593981800STejun Heo 	/*
169693981800STejun Heo 	 * When running in keventd don't schedule a work item on
169793981800STejun Heo 	 * itself.  Can just call directly because the work queue is
169893981800STejun Heo 	 * already bound.  This also is faster.
169993981800STejun Heo 	 */
170093981800STejun Heo 	if (current_is_keventd())
170193981800STejun Heo 		orig = raw_smp_processor_id();
170293981800STejun Heo 
170315316ba8SChristoph Lameter 	for_each_online_cpu(cpu) {
17049bfb1839SIngo Molnar 		struct work_struct *work = per_cpu_ptr(works, cpu);
17059bfb1839SIngo Molnar 
17069bfb1839SIngo Molnar 		INIT_WORK(work, func);
170793981800STejun Heo 		if (cpu != orig)
17088de6d308SOleg Nesterov 			schedule_work_on(cpu, work);
170915316ba8SChristoph Lameter 	}
171093981800STejun Heo 	if (orig >= 0)
171193981800STejun Heo 		func(per_cpu_ptr(works, orig));
171293981800STejun Heo 
171393981800STejun Heo 	for_each_online_cpu(cpu)
17148616a89aSOleg Nesterov 		flush_work(per_cpu_ptr(works, cpu));
171593981800STejun Heo 
171695402b38SGautham R Shenoy 	put_online_cpus();
1717b6136773SAndrew Morton 	free_percpu(works);
171815316ba8SChristoph Lameter 	return 0;
171915316ba8SChristoph Lameter }
172015316ba8SChristoph Lameter 
1721eef6a7d5SAlan Stern /**
1722eef6a7d5SAlan Stern  * flush_scheduled_work - ensure that any scheduled work has run to completion.
1723eef6a7d5SAlan Stern  *
1724eef6a7d5SAlan Stern  * Forces execution of the kernel-global workqueue and blocks until its
1725eef6a7d5SAlan Stern  * completion.
1726eef6a7d5SAlan Stern  *
1727eef6a7d5SAlan Stern  * Think twice before calling this function!  It's very easy to get into
1728eef6a7d5SAlan Stern  * trouble if you don't take great care.  Either of the following situations
1729eef6a7d5SAlan Stern  * will lead to deadlock:
1730eef6a7d5SAlan Stern  *
1731eef6a7d5SAlan Stern  *	One of the work items currently on the workqueue needs to acquire
1732eef6a7d5SAlan Stern  *	a lock held by your code or its caller.
1733eef6a7d5SAlan Stern  *
1734eef6a7d5SAlan Stern  *	Your code is running in the context of a work routine.
1735eef6a7d5SAlan Stern  *
1736eef6a7d5SAlan Stern  * They will be detected by lockdep when they occur, but the first might not
1737eef6a7d5SAlan Stern  * occur very often.  It depends on what work items are on the workqueue and
1738eef6a7d5SAlan Stern  * what locks they need, which you have no control over.
1739eef6a7d5SAlan Stern  *
1740eef6a7d5SAlan Stern  * In most situations flushing the entire workqueue is overkill; you merely
1741eef6a7d5SAlan Stern  * need to know that a particular work item isn't queued and isn't running.
1742eef6a7d5SAlan Stern  * In such cases you should use cancel_delayed_work_sync() or
1743eef6a7d5SAlan Stern  * cancel_work_sync() instead.
1744eef6a7d5SAlan Stern  */
17451da177e4SLinus Torvalds void flush_scheduled_work(void)
17461da177e4SLinus Torvalds {
17471da177e4SLinus Torvalds 	flush_workqueue(keventd_wq);
17481da177e4SLinus Torvalds }
1749ae90dd5dSDave Jones EXPORT_SYMBOL(flush_scheduled_work);
17501da177e4SLinus Torvalds 
17511da177e4SLinus Torvalds /**
17521fa44ecaSJames Bottomley  * execute_in_process_context - reliably execute the routine with user context
17531fa44ecaSJames Bottomley  * @fn:		the function to execute
17541fa44ecaSJames Bottomley  * @ew:		guaranteed storage for the execute work structure (must
17551fa44ecaSJames Bottomley  *		be available when the work executes)
17561fa44ecaSJames Bottomley  *
17571fa44ecaSJames Bottomley  * Executes the function immediately if process context is available,
17581fa44ecaSJames Bottomley  * otherwise schedules the function for delayed execution.
17591fa44ecaSJames Bottomley  *
17601fa44ecaSJames Bottomley  * Returns:	0 - function was executed
17611fa44ecaSJames Bottomley  *		1 - function was scheduled for execution
17621fa44ecaSJames Bottomley  */
176365f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew)
17641fa44ecaSJames Bottomley {
17651fa44ecaSJames Bottomley 	if (!in_interrupt()) {
176665f27f38SDavid Howells 		fn(&ew->work);
17671fa44ecaSJames Bottomley 		return 0;
17681fa44ecaSJames Bottomley 	}
17691fa44ecaSJames Bottomley 
177065f27f38SDavid Howells 	INIT_WORK(&ew->work, fn);
17711fa44ecaSJames Bottomley 	schedule_work(&ew->work);
17721fa44ecaSJames Bottomley 
17731fa44ecaSJames Bottomley 	return 1;
17741fa44ecaSJames Bottomley }
17751fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context);
17761fa44ecaSJames Bottomley 
17771da177e4SLinus Torvalds int keventd_up(void)
17781da177e4SLinus Torvalds {
17791da177e4SLinus Torvalds 	return keventd_wq != NULL;
17801da177e4SLinus Torvalds }
17811da177e4SLinus Torvalds 
17821da177e4SLinus Torvalds int current_is_keventd(void)
17831da177e4SLinus Torvalds {
17841da177e4SLinus Torvalds 	struct cpu_workqueue_struct *cwq;
1785d243769dSHugh Dickins 	int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
17861da177e4SLinus Torvalds 	int ret = 0;
17871da177e4SLinus Torvalds 
17881da177e4SLinus Torvalds 	BUG_ON(!keventd_wq);
17891da177e4SLinus Torvalds 
17901537663fSTejun Heo 	cwq = get_cwq(cpu, keventd_wq);
1791c34056a3STejun Heo 	if (current == cwq->worker->task)
17921da177e4SLinus Torvalds 		ret = 1;
17931da177e4SLinus Torvalds 
17941da177e4SLinus Torvalds 	return ret;
17951da177e4SLinus Torvalds 
17961da177e4SLinus Torvalds }
17971da177e4SLinus Torvalds 
17980f900049STejun Heo static struct cpu_workqueue_struct *alloc_cwqs(void)
17990f900049STejun Heo {
18000f900049STejun Heo 	/*
18010f900049STejun Heo 	 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
18020f900049STejun Heo 	 * Make sure that the alignment isn't lower than that of
18030f900049STejun Heo 	 * unsigned long long.
18040f900049STejun Heo 	 */
18050f900049STejun Heo 	const size_t size = sizeof(struct cpu_workqueue_struct);
18060f900049STejun Heo 	const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
18070f900049STejun Heo 				   __alignof__(unsigned long long));
18080f900049STejun Heo 	struct cpu_workqueue_struct *cwqs;
18090f900049STejun Heo #ifndef CONFIG_SMP
18100f900049STejun Heo 	void *ptr;
18110f900049STejun Heo 
18120f900049STejun Heo 	/*
18130f900049STejun Heo 	 * On UP, percpu allocator doesn't honor alignment parameter
18140f900049STejun Heo 	 * and simply uses arch-dependent default.  Allocate enough
18150f900049STejun Heo 	 * room to align cwq and put an extra pointer at the end
18160f900049STejun Heo 	 * pointing back to the originally allocated pointer which
18170f900049STejun Heo 	 * will be used for free.
18180f900049STejun Heo 	 *
18190f900049STejun Heo 	 * FIXME: This really belongs to UP percpu code.  Update UP
18200f900049STejun Heo 	 * percpu code to honor alignment and remove this ugliness.
18210f900049STejun Heo 	 */
18220f900049STejun Heo 	ptr = __alloc_percpu(size + align + sizeof(void *), 1);
18230f900049STejun Heo 	cwqs = PTR_ALIGN(ptr, align);
18240f900049STejun Heo 	*(void **)per_cpu_ptr(cwqs + 1, 0) = ptr;
18250f900049STejun Heo #else
18260f900049STejun Heo 	/* On SMP, percpu allocator can do it itself */
18270f900049STejun Heo 	cwqs = __alloc_percpu(size, align);
18280f900049STejun Heo #endif
18290f900049STejun Heo 	/* just in case, make sure it's actually aligned */
18300f900049STejun Heo 	BUG_ON(!IS_ALIGNED((unsigned long)cwqs, align));
18310f900049STejun Heo 	return cwqs;
18320f900049STejun Heo }
18330f900049STejun Heo 
18340f900049STejun Heo static void free_cwqs(struct cpu_workqueue_struct *cwqs)
18350f900049STejun Heo {
18360f900049STejun Heo #ifndef CONFIG_SMP
18370f900049STejun Heo 	/* on UP, the pointer to free is stored right after the cwq */
18380f900049STejun Heo 	if (cwqs)
18390f900049STejun Heo 		free_percpu(*(void **)per_cpu_ptr(cwqs + 1, 0));
18400f900049STejun Heo #else
18410f900049STejun Heo 	free_percpu(cwqs);
18420f900049STejun Heo #endif
18430f900049STejun Heo }
18440f900049STejun Heo 
18454e6045f1SJohannes Berg struct workqueue_struct *__create_workqueue_key(const char *name,
184697e37d7bSTejun Heo 						unsigned int flags,
18471e19ffc6STejun Heo 						int max_active,
1848eb13ba87SJohannes Berg 						struct lock_class_key *key,
1849eb13ba87SJohannes Berg 						const char *lock_name)
18503af24433SOleg Nesterov {
18513af24433SOleg Nesterov 	struct workqueue_struct *wq;
1852c34056a3STejun Heo 	bool failed = false;
1853c34056a3STejun Heo 	unsigned int cpu;
18543af24433SOleg Nesterov 
18551e19ffc6STejun Heo 	max_active = clamp_val(max_active, 1, INT_MAX);
18561e19ffc6STejun Heo 
18573af24433SOleg Nesterov 	wq = kzalloc(sizeof(*wq), GFP_KERNEL);
18583af24433SOleg Nesterov 	if (!wq)
18594690c4abSTejun Heo 		goto err;
18603af24433SOleg Nesterov 
18610f900049STejun Heo 	wq->cpu_wq = alloc_cwqs();
18624690c4abSTejun Heo 	if (!wq->cpu_wq)
18634690c4abSTejun Heo 		goto err;
18643af24433SOleg Nesterov 
186597e37d7bSTejun Heo 	wq->flags = flags;
1866a0a1a5fdSTejun Heo 	wq->saved_max_active = max_active;
186773f53c4aSTejun Heo 	mutex_init(&wq->flush_mutex);
186873f53c4aSTejun Heo 	atomic_set(&wq->nr_cwqs_to_flush, 0);
186973f53c4aSTejun Heo 	INIT_LIST_HEAD(&wq->flusher_queue);
187073f53c4aSTejun Heo 	INIT_LIST_HEAD(&wq->flusher_overflow);
1871502ca9d8STejun Heo 	wq->single_cpu = NR_CPUS;
1872502ca9d8STejun Heo 
18733af24433SOleg Nesterov 	wq->name = name;
1874eb13ba87SJohannes Berg 	lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
1875cce1a165SOleg Nesterov 	INIT_LIST_HEAD(&wq->list);
18763af24433SOleg Nesterov 
18773da1c84cSOleg Nesterov 	cpu_maps_update_begin();
18786af8bf3dSOleg Nesterov 	/*
18796af8bf3dSOleg Nesterov 	 * We must initialize cwqs for each possible cpu even if we
18806af8bf3dSOleg Nesterov 	 * are going to call destroy_workqueue() finally. Otherwise
18816af8bf3dSOleg Nesterov 	 * cpu_up() can hit the uninitialized cwq once we drop the
18826af8bf3dSOleg Nesterov 	 * lock.
18836af8bf3dSOleg Nesterov 	 */
18843af24433SOleg Nesterov 	for_each_possible_cpu(cpu) {
18851537663fSTejun Heo 		struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
18868b03ae3cSTejun Heo 		struct global_cwq *gcwq = get_gcwq(cpu);
18871537663fSTejun Heo 
18880f900049STejun Heo 		BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
18898b03ae3cSTejun Heo 		cwq->gcwq = gcwq;
1890c34056a3STejun Heo 		cwq->wq = wq;
189173f53c4aSTejun Heo 		cwq->flush_color = -1;
18921e19ffc6STejun Heo 		cwq->max_active = max_active;
18931537663fSTejun Heo 		INIT_LIST_HEAD(&cwq->worklist);
18941e19ffc6STejun Heo 		INIT_LIST_HEAD(&cwq->delayed_works);
18951537663fSTejun Heo 
1896c34056a3STejun Heo 		if (failed)
18973af24433SOleg Nesterov 			continue;
1898502ca9d8STejun Heo 		cwq->worker = create_worker(cwq, cpu_online(cpu));
1899c34056a3STejun Heo 		if (cwq->worker)
1900c34056a3STejun Heo 			start_worker(cwq->worker);
19011537663fSTejun Heo 		else
1902c34056a3STejun Heo 			failed = true;
19033af24433SOleg Nesterov 	}
19041537663fSTejun Heo 
1905a0a1a5fdSTejun Heo 	/*
1906a0a1a5fdSTejun Heo 	 * workqueue_lock protects global freeze state and workqueues
1907a0a1a5fdSTejun Heo 	 * list.  Grab it, set max_active accordingly and add the new
1908a0a1a5fdSTejun Heo 	 * workqueue to workqueues list.
1909a0a1a5fdSTejun Heo 	 */
19101537663fSTejun Heo 	spin_lock(&workqueue_lock);
1911a0a1a5fdSTejun Heo 
1912a0a1a5fdSTejun Heo 	if (workqueue_freezing && wq->flags & WQ_FREEZEABLE)
1913a0a1a5fdSTejun Heo 		for_each_possible_cpu(cpu)
1914a0a1a5fdSTejun Heo 			get_cwq(cpu, wq)->max_active = 0;
1915a0a1a5fdSTejun Heo 
19161537663fSTejun Heo 	list_add(&wq->list, &workqueues);
1917a0a1a5fdSTejun Heo 
19181537663fSTejun Heo 	spin_unlock(&workqueue_lock);
19191537663fSTejun Heo 
19203da1c84cSOleg Nesterov 	cpu_maps_update_done();
19213af24433SOleg Nesterov 
1922c34056a3STejun Heo 	if (failed) {
19233af24433SOleg Nesterov 		destroy_workqueue(wq);
19243af24433SOleg Nesterov 		wq = NULL;
19253af24433SOleg Nesterov 	}
19263af24433SOleg Nesterov 	return wq;
19274690c4abSTejun Heo err:
19284690c4abSTejun Heo 	if (wq) {
19290f900049STejun Heo 		free_cwqs(wq->cpu_wq);
19304690c4abSTejun Heo 		kfree(wq);
19314690c4abSTejun Heo 	}
19324690c4abSTejun Heo 	return NULL;
19333af24433SOleg Nesterov }
19344e6045f1SJohannes Berg EXPORT_SYMBOL_GPL(__create_workqueue_key);
19353af24433SOleg Nesterov 
19363af24433SOleg Nesterov /**
19373af24433SOleg Nesterov  * destroy_workqueue - safely terminate a workqueue
19383af24433SOleg Nesterov  * @wq: target workqueue
19393af24433SOleg Nesterov  *
19403af24433SOleg Nesterov  * Safely destroy a workqueue. All work currently pending will be done first.
19413af24433SOleg Nesterov  */
19423af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq)
19433af24433SOleg Nesterov {
1944c8e55f36STejun Heo 	unsigned int cpu;
19453af24433SOleg Nesterov 
1946a0a1a5fdSTejun Heo 	flush_workqueue(wq);
1947a0a1a5fdSTejun Heo 
1948a0a1a5fdSTejun Heo 	/*
1949a0a1a5fdSTejun Heo 	 * wq list is used to freeze wq, remove from list after
1950a0a1a5fdSTejun Heo 	 * flushing is complete in case freeze races us.
1951a0a1a5fdSTejun Heo 	 */
19523da1c84cSOleg Nesterov 	cpu_maps_update_begin();
195395402b38SGautham R Shenoy 	spin_lock(&workqueue_lock);
19543af24433SOleg Nesterov 	list_del(&wq->list);
195595402b38SGautham R Shenoy 	spin_unlock(&workqueue_lock);
19563da1c84cSOleg Nesterov 	cpu_maps_update_done();
19573af24433SOleg Nesterov 
195873f53c4aSTejun Heo 	for_each_possible_cpu(cpu) {
195973f53c4aSTejun Heo 		struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
196073f53c4aSTejun Heo 		int i;
196173f53c4aSTejun Heo 
1962c34056a3STejun Heo 		if (cwq->worker) {
1963c8e55f36STejun Heo 			spin_lock_irq(&cwq->gcwq->lock);
1964c34056a3STejun Heo 			destroy_worker(cwq->worker);
1965c34056a3STejun Heo 			cwq->worker = NULL;
1966c8e55f36STejun Heo 			spin_unlock_irq(&cwq->gcwq->lock);
196773f53c4aSTejun Heo 		}
196873f53c4aSTejun Heo 
196973f53c4aSTejun Heo 		for (i = 0; i < WORK_NR_COLORS; i++)
197073f53c4aSTejun Heo 			BUG_ON(cwq->nr_in_flight[i]);
19711e19ffc6STejun Heo 		BUG_ON(cwq->nr_active);
19721e19ffc6STejun Heo 		BUG_ON(!list_empty(&cwq->delayed_works));
197373f53c4aSTejun Heo 	}
19741537663fSTejun Heo 
19750f900049STejun Heo 	free_cwqs(wq->cpu_wq);
19763af24433SOleg Nesterov 	kfree(wq);
19773af24433SOleg Nesterov }
19783af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue);
19793af24433SOleg Nesterov 
1980db7bccf4STejun Heo /*
1981db7bccf4STejun Heo  * CPU hotplug.
1982db7bccf4STejun Heo  *
1983db7bccf4STejun Heo  * CPU hotplug is implemented by allowing cwqs to be detached from
1984db7bccf4STejun Heo  * CPU, running with unbound workers and allowing them to be
1985db7bccf4STejun Heo  * reattached later if the cpu comes back online.  A separate thread
1986db7bccf4STejun Heo  * is created to govern cwqs in such state and is called the trustee.
1987db7bccf4STejun Heo  *
1988db7bccf4STejun Heo  * Trustee states and their descriptions.
1989db7bccf4STejun Heo  *
1990db7bccf4STejun Heo  * START	Command state used on startup.  On CPU_DOWN_PREPARE, a
1991db7bccf4STejun Heo  *		new trustee is started with this state.
1992db7bccf4STejun Heo  *
1993db7bccf4STejun Heo  * IN_CHARGE	Once started, trustee will enter this state after
1994db7bccf4STejun Heo  *		making all existing workers rogue.  DOWN_PREPARE waits
1995db7bccf4STejun Heo  *		for trustee to enter this state.  After reaching
1996db7bccf4STejun Heo  *		IN_CHARGE, trustee tries to execute the pending
1997db7bccf4STejun Heo  *		worklist until it's empty and the state is set to
1998db7bccf4STejun Heo  *		BUTCHER, or the state is set to RELEASE.
1999db7bccf4STejun Heo  *
2000db7bccf4STejun Heo  * BUTCHER	Command state which is set by the cpu callback after
2001db7bccf4STejun Heo  *		the cpu has went down.  Once this state is set trustee
2002db7bccf4STejun Heo  *		knows that there will be no new works on the worklist
2003db7bccf4STejun Heo  *		and once the worklist is empty it can proceed to
2004db7bccf4STejun Heo  *		killing idle workers.
2005db7bccf4STejun Heo  *
2006db7bccf4STejun Heo  * RELEASE	Command state which is set by the cpu callback if the
2007db7bccf4STejun Heo  *		cpu down has been canceled or it has come online
2008db7bccf4STejun Heo  *		again.  After recognizing this state, trustee stops
2009db7bccf4STejun Heo  *		trying to drain or butcher and transits to DONE.
2010db7bccf4STejun Heo  *
2011db7bccf4STejun Heo  * DONE		Trustee will enter this state after BUTCHER or RELEASE
2012db7bccf4STejun Heo  *		is complete.
2013db7bccf4STejun Heo  *
2014db7bccf4STejun Heo  *          trustee                 CPU                draining
2015db7bccf4STejun Heo  *         took over                down               complete
2016db7bccf4STejun Heo  * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
2017db7bccf4STejun Heo  *                        |                     |                  ^
2018db7bccf4STejun Heo  *                        | CPU is back online  v   return workers |
2019db7bccf4STejun Heo  *                         ----------------> RELEASE --------------
2020db7bccf4STejun Heo  */
2021db7bccf4STejun Heo 
2022db7bccf4STejun Heo /**
2023db7bccf4STejun Heo  * trustee_wait_event_timeout - timed event wait for trustee
2024db7bccf4STejun Heo  * @cond: condition to wait for
2025db7bccf4STejun Heo  * @timeout: timeout in jiffies
2026db7bccf4STejun Heo  *
2027db7bccf4STejun Heo  * wait_event_timeout() for trustee to use.  Handles locking and
2028db7bccf4STejun Heo  * checks for RELEASE request.
2029db7bccf4STejun Heo  *
2030db7bccf4STejun Heo  * CONTEXT:
2031db7bccf4STejun Heo  * spin_lock_irq(gcwq->lock) which may be released and regrabbed
2032db7bccf4STejun Heo  * multiple times.  To be used by trustee.
2033db7bccf4STejun Heo  *
2034db7bccf4STejun Heo  * RETURNS:
2035db7bccf4STejun Heo  * Positive indicating left time if @cond is satisfied, 0 if timed
2036db7bccf4STejun Heo  * out, -1 if canceled.
2037db7bccf4STejun Heo  */
2038db7bccf4STejun Heo #define trustee_wait_event_timeout(cond, timeout) ({			\
2039db7bccf4STejun Heo 	long __ret = (timeout);						\
2040db7bccf4STejun Heo 	while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) &&	\
2041db7bccf4STejun Heo 	       __ret) {							\
2042db7bccf4STejun Heo 		spin_unlock_irq(&gcwq->lock);				\
2043db7bccf4STejun Heo 		__wait_event_timeout(gcwq->trustee_wait, (cond) ||	\
2044db7bccf4STejun Heo 			(gcwq->trustee_state == TRUSTEE_RELEASE),	\
2045db7bccf4STejun Heo 			__ret);						\
2046db7bccf4STejun Heo 		spin_lock_irq(&gcwq->lock);				\
2047db7bccf4STejun Heo 	}								\
2048db7bccf4STejun Heo 	gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret);		\
2049db7bccf4STejun Heo })
2050db7bccf4STejun Heo 
2051db7bccf4STejun Heo /**
2052db7bccf4STejun Heo  * trustee_wait_event - event wait for trustee
2053db7bccf4STejun Heo  * @cond: condition to wait for
2054db7bccf4STejun Heo  *
2055db7bccf4STejun Heo  * wait_event() for trustee to use.  Automatically handles locking and
2056db7bccf4STejun Heo  * checks for CANCEL request.
2057db7bccf4STejun Heo  *
2058db7bccf4STejun Heo  * CONTEXT:
2059db7bccf4STejun Heo  * spin_lock_irq(gcwq->lock) which may be released and regrabbed
2060db7bccf4STejun Heo  * multiple times.  To be used by trustee.
2061db7bccf4STejun Heo  *
2062db7bccf4STejun Heo  * RETURNS:
2063db7bccf4STejun Heo  * 0 if @cond is satisfied, -1 if canceled.
2064db7bccf4STejun Heo  */
2065db7bccf4STejun Heo #define trustee_wait_event(cond) ({					\
2066db7bccf4STejun Heo 	long __ret1;							\
2067db7bccf4STejun Heo 	__ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
2068db7bccf4STejun Heo 	__ret1 < 0 ? -1 : 0;						\
2069db7bccf4STejun Heo })
2070db7bccf4STejun Heo 
2071db7bccf4STejun Heo static int __cpuinit trustee_thread(void *__gcwq)
2072db7bccf4STejun Heo {
2073db7bccf4STejun Heo 	struct global_cwq *gcwq = __gcwq;
2074db7bccf4STejun Heo 	struct worker *worker;
2075db7bccf4STejun Heo 	struct hlist_node *pos;
2076db7bccf4STejun Heo 	int i;
2077db7bccf4STejun Heo 
2078db7bccf4STejun Heo 	BUG_ON(gcwq->cpu != smp_processor_id());
2079db7bccf4STejun Heo 
2080db7bccf4STejun Heo 	spin_lock_irq(&gcwq->lock);
2081db7bccf4STejun Heo 	/*
2082502ca9d8STejun Heo 	 * Make all workers rogue.  Trustee must be bound to the
2083502ca9d8STejun Heo 	 * target cpu and can't be cancelled.
2084db7bccf4STejun Heo 	 */
2085db7bccf4STejun Heo 	BUG_ON(gcwq->cpu != smp_processor_id());
2086db7bccf4STejun Heo 
2087db7bccf4STejun Heo 	list_for_each_entry(worker, &gcwq->idle_list, entry)
2088db7bccf4STejun Heo 		worker->flags |= WORKER_ROGUE;
2089db7bccf4STejun Heo 
2090db7bccf4STejun Heo 	for_each_busy_worker(worker, i, pos, gcwq)
2091db7bccf4STejun Heo 		worker->flags |= WORKER_ROGUE;
2092db7bccf4STejun Heo 
2093db7bccf4STejun Heo 	/*
2094db7bccf4STejun Heo 	 * We're now in charge.  Notify and proceed to drain.  We need
2095db7bccf4STejun Heo 	 * to keep the gcwq running during the whole CPU down
2096db7bccf4STejun Heo 	 * procedure as other cpu hotunplug callbacks may need to
2097db7bccf4STejun Heo 	 * flush currently running tasks.
2098db7bccf4STejun Heo 	 */
2099db7bccf4STejun Heo 	gcwq->trustee_state = TRUSTEE_IN_CHARGE;
2100db7bccf4STejun Heo 	wake_up_all(&gcwq->trustee_wait);
2101db7bccf4STejun Heo 
2102db7bccf4STejun Heo 	/*
2103db7bccf4STejun Heo 	 * The original cpu is in the process of dying and may go away
2104db7bccf4STejun Heo 	 * anytime now.  When that happens, we and all workers would
2105db7bccf4STejun Heo 	 * be migrated to other cpus.  Try draining any left work.
2106db7bccf4STejun Heo 	 * Note that if the gcwq is frozen, there may be frozen works
2107db7bccf4STejun Heo 	 * in freezeable cwqs.  Don't declare completion while frozen.
2108db7bccf4STejun Heo 	 */
2109db7bccf4STejun Heo 	while (gcwq->nr_workers != gcwq->nr_idle ||
2110db7bccf4STejun Heo 	       gcwq->flags & GCWQ_FREEZING ||
2111db7bccf4STejun Heo 	       gcwq->trustee_state == TRUSTEE_IN_CHARGE) {
2112db7bccf4STejun Heo 		/* give a breather */
2113db7bccf4STejun Heo 		if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
2114db7bccf4STejun Heo 			break;
2115db7bccf4STejun Heo 	}
2116db7bccf4STejun Heo 
2117db7bccf4STejun Heo 	/* notify completion */
2118db7bccf4STejun Heo 	gcwq->trustee = NULL;
2119db7bccf4STejun Heo 	gcwq->trustee_state = TRUSTEE_DONE;
2120db7bccf4STejun Heo 	wake_up_all(&gcwq->trustee_wait);
2121db7bccf4STejun Heo 	spin_unlock_irq(&gcwq->lock);
2122db7bccf4STejun Heo 	return 0;
2123db7bccf4STejun Heo }
2124db7bccf4STejun Heo 
2125db7bccf4STejun Heo /**
2126db7bccf4STejun Heo  * wait_trustee_state - wait for trustee to enter the specified state
2127db7bccf4STejun Heo  * @gcwq: gcwq the trustee of interest belongs to
2128db7bccf4STejun Heo  * @state: target state to wait for
2129db7bccf4STejun Heo  *
2130db7bccf4STejun Heo  * Wait for the trustee to reach @state.  DONE is already matched.
2131db7bccf4STejun Heo  *
2132db7bccf4STejun Heo  * CONTEXT:
2133db7bccf4STejun Heo  * spin_lock_irq(gcwq->lock) which may be released and regrabbed
2134db7bccf4STejun Heo  * multiple times.  To be used by cpu_callback.
2135db7bccf4STejun Heo  */
2136db7bccf4STejun Heo static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
2137db7bccf4STejun Heo {
2138db7bccf4STejun Heo 	if (!(gcwq->trustee_state == state ||
2139db7bccf4STejun Heo 	      gcwq->trustee_state == TRUSTEE_DONE)) {
2140db7bccf4STejun Heo 		spin_unlock_irq(&gcwq->lock);
2141db7bccf4STejun Heo 		__wait_event(gcwq->trustee_wait,
2142db7bccf4STejun Heo 			     gcwq->trustee_state == state ||
2143db7bccf4STejun Heo 			     gcwq->trustee_state == TRUSTEE_DONE);
2144db7bccf4STejun Heo 		spin_lock_irq(&gcwq->lock);
2145db7bccf4STejun Heo 	}
2146db7bccf4STejun Heo }
2147db7bccf4STejun Heo 
21489c7b216dSChandra Seetharaman static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
21491da177e4SLinus Torvalds 						unsigned long action,
21501da177e4SLinus Torvalds 						void *hcpu)
21511da177e4SLinus Torvalds {
21523af24433SOleg Nesterov 	unsigned int cpu = (unsigned long)hcpu;
2153db7bccf4STejun Heo 	struct global_cwq *gcwq = get_gcwq(cpu);
2154db7bccf4STejun Heo 	struct task_struct *new_trustee = NULL;
2155db7bccf4STejun Heo 	struct worker *worker;
2156db7bccf4STejun Heo 	struct hlist_node *pos;
2157db7bccf4STejun Heo 	unsigned long flags;
2158db7bccf4STejun Heo 	int i;
21591da177e4SLinus Torvalds 
21608bb78442SRafael J. Wysocki 	action &= ~CPU_TASKS_FROZEN;
21618bb78442SRafael J. Wysocki 
2162db7bccf4STejun Heo 	switch (action) {
2163db7bccf4STejun Heo 	case CPU_DOWN_PREPARE:
2164db7bccf4STejun Heo 		new_trustee = kthread_create(trustee_thread, gcwq,
2165db7bccf4STejun Heo 					     "workqueue_trustee/%d\n", cpu);
2166db7bccf4STejun Heo 		if (IS_ERR(new_trustee))
2167db7bccf4STejun Heo 			return notifier_from_errno(PTR_ERR(new_trustee));
2168db7bccf4STejun Heo 		kthread_bind(new_trustee, cpu);
2169db7bccf4STejun Heo 	}
21701537663fSTejun Heo 
2171db7bccf4STejun Heo 	/* some are called w/ irq disabled, don't disturb irq status */
2172db7bccf4STejun Heo 	spin_lock_irqsave(&gcwq->lock, flags);
21733af24433SOleg Nesterov 
21743af24433SOleg Nesterov 	switch (action) {
2175db7bccf4STejun Heo 	case CPU_DOWN_PREPARE:
2176db7bccf4STejun Heo 		/* initialize trustee and tell it to acquire the gcwq */
2177db7bccf4STejun Heo 		BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
2178db7bccf4STejun Heo 		gcwq->trustee = new_trustee;
2179db7bccf4STejun Heo 		gcwq->trustee_state = TRUSTEE_START;
2180db7bccf4STejun Heo 		wake_up_process(gcwq->trustee);
2181db7bccf4STejun Heo 		wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
2182db7bccf4STejun Heo 		break;
2183db7bccf4STejun Heo 
21843da1c84cSOleg Nesterov 	case CPU_POST_DEAD:
2185db7bccf4STejun Heo 		gcwq->trustee_state = TRUSTEE_BUTCHER;
2186db7bccf4STejun Heo 		break;
2187db7bccf4STejun Heo 
2188db7bccf4STejun Heo 	case CPU_DOWN_FAILED:
2189db7bccf4STejun Heo 	case CPU_ONLINE:
2190db7bccf4STejun Heo 		if (gcwq->trustee_state != TRUSTEE_DONE) {
2191db7bccf4STejun Heo 			gcwq->trustee_state = TRUSTEE_RELEASE;
2192db7bccf4STejun Heo 			wake_up_process(gcwq->trustee);
2193db7bccf4STejun Heo 			wait_trustee_state(gcwq, TRUSTEE_DONE);
2194db7bccf4STejun Heo 		}
2195db7bccf4STejun Heo 
2196502ca9d8STejun Heo 		/* clear ROGUE from all workers */
2197db7bccf4STejun Heo 		list_for_each_entry(worker, &gcwq->idle_list, entry)
2198db7bccf4STejun Heo 			worker->flags &= ~WORKER_ROGUE;
2199db7bccf4STejun Heo 
2200db7bccf4STejun Heo 		for_each_busy_worker(worker, i, pos, gcwq)
2201db7bccf4STejun Heo 			worker->flags &= ~WORKER_ROGUE;
22021da177e4SLinus Torvalds 		break;
22031da177e4SLinus Torvalds 	}
2204db7bccf4STejun Heo 
2205db7bccf4STejun Heo 	spin_unlock_irqrestore(&gcwq->lock, flags);
22061da177e4SLinus Torvalds 
22071537663fSTejun Heo 	return notifier_from_errno(0);
22081da177e4SLinus Torvalds }
22091da177e4SLinus Torvalds 
22102d3854a3SRusty Russell #ifdef CONFIG_SMP
22118ccad40dSRusty Russell 
22122d3854a3SRusty Russell struct work_for_cpu {
22136b44003eSAndrew Morton 	struct completion completion;
22142d3854a3SRusty Russell 	long (*fn)(void *);
22152d3854a3SRusty Russell 	void *arg;
22162d3854a3SRusty Russell 	long ret;
22172d3854a3SRusty Russell };
22182d3854a3SRusty Russell 
22196b44003eSAndrew Morton static int do_work_for_cpu(void *_wfc)
22202d3854a3SRusty Russell {
22216b44003eSAndrew Morton 	struct work_for_cpu *wfc = _wfc;
22222d3854a3SRusty Russell 	wfc->ret = wfc->fn(wfc->arg);
22236b44003eSAndrew Morton 	complete(&wfc->completion);
22246b44003eSAndrew Morton 	return 0;
22252d3854a3SRusty Russell }
22262d3854a3SRusty Russell 
22272d3854a3SRusty Russell /**
22282d3854a3SRusty Russell  * work_on_cpu - run a function in user context on a particular cpu
22292d3854a3SRusty Russell  * @cpu: the cpu to run on
22302d3854a3SRusty Russell  * @fn: the function to run
22312d3854a3SRusty Russell  * @arg: the function arg
22322d3854a3SRusty Russell  *
223331ad9081SRusty Russell  * This will return the value @fn returns.
223431ad9081SRusty Russell  * It is up to the caller to ensure that the cpu doesn't go offline.
22356b44003eSAndrew Morton  * The caller must not hold any locks which would prevent @fn from completing.
22362d3854a3SRusty Russell  */
22372d3854a3SRusty Russell long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
22382d3854a3SRusty Russell {
22396b44003eSAndrew Morton 	struct task_struct *sub_thread;
22406b44003eSAndrew Morton 	struct work_for_cpu wfc = {
22416b44003eSAndrew Morton 		.completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
22426b44003eSAndrew Morton 		.fn = fn,
22436b44003eSAndrew Morton 		.arg = arg,
22446b44003eSAndrew Morton 	};
22452d3854a3SRusty Russell 
22466b44003eSAndrew Morton 	sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
22476b44003eSAndrew Morton 	if (IS_ERR(sub_thread))
22486b44003eSAndrew Morton 		return PTR_ERR(sub_thread);
22496b44003eSAndrew Morton 	kthread_bind(sub_thread, cpu);
22506b44003eSAndrew Morton 	wake_up_process(sub_thread);
22516b44003eSAndrew Morton 	wait_for_completion(&wfc.completion);
22522d3854a3SRusty Russell 	return wfc.ret;
22532d3854a3SRusty Russell }
22542d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu);
22552d3854a3SRusty Russell #endif /* CONFIG_SMP */
22562d3854a3SRusty Russell 
2257a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER
2258a0a1a5fdSTejun Heo 
2259a0a1a5fdSTejun Heo /**
2260a0a1a5fdSTejun Heo  * freeze_workqueues_begin - begin freezing workqueues
2261a0a1a5fdSTejun Heo  *
2262a0a1a5fdSTejun Heo  * Start freezing workqueues.  After this function returns, all
2263a0a1a5fdSTejun Heo  * freezeable workqueues will queue new works to their frozen_works
2264a0a1a5fdSTejun Heo  * list instead of the cwq ones.
2265a0a1a5fdSTejun Heo  *
2266a0a1a5fdSTejun Heo  * CONTEXT:
22678b03ae3cSTejun Heo  * Grabs and releases workqueue_lock and gcwq->lock's.
2268a0a1a5fdSTejun Heo  */
2269a0a1a5fdSTejun Heo void freeze_workqueues_begin(void)
2270a0a1a5fdSTejun Heo {
2271a0a1a5fdSTejun Heo 	struct workqueue_struct *wq;
2272a0a1a5fdSTejun Heo 	unsigned int cpu;
2273a0a1a5fdSTejun Heo 
2274a0a1a5fdSTejun Heo 	spin_lock(&workqueue_lock);
2275a0a1a5fdSTejun Heo 
2276a0a1a5fdSTejun Heo 	BUG_ON(workqueue_freezing);
2277a0a1a5fdSTejun Heo 	workqueue_freezing = true;
2278a0a1a5fdSTejun Heo 
2279a0a1a5fdSTejun Heo 	for_each_possible_cpu(cpu) {
22808b03ae3cSTejun Heo 		struct global_cwq *gcwq = get_gcwq(cpu);
22818b03ae3cSTejun Heo 
22828b03ae3cSTejun Heo 		spin_lock_irq(&gcwq->lock);
22838b03ae3cSTejun Heo 
2284db7bccf4STejun Heo 		BUG_ON(gcwq->flags & GCWQ_FREEZING);
2285db7bccf4STejun Heo 		gcwq->flags |= GCWQ_FREEZING;
2286db7bccf4STejun Heo 
2287a0a1a5fdSTejun Heo 		list_for_each_entry(wq, &workqueues, list) {
2288a0a1a5fdSTejun Heo 			struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
2289a0a1a5fdSTejun Heo 
2290a0a1a5fdSTejun Heo 			if (wq->flags & WQ_FREEZEABLE)
2291a0a1a5fdSTejun Heo 				cwq->max_active = 0;
2292a0a1a5fdSTejun Heo 		}
22938b03ae3cSTejun Heo 
22948b03ae3cSTejun Heo 		spin_unlock_irq(&gcwq->lock);
2295a0a1a5fdSTejun Heo 	}
2296a0a1a5fdSTejun Heo 
2297a0a1a5fdSTejun Heo 	spin_unlock(&workqueue_lock);
2298a0a1a5fdSTejun Heo }
2299a0a1a5fdSTejun Heo 
2300a0a1a5fdSTejun Heo /**
2301a0a1a5fdSTejun Heo  * freeze_workqueues_busy - are freezeable workqueues still busy?
2302a0a1a5fdSTejun Heo  *
2303a0a1a5fdSTejun Heo  * Check whether freezing is complete.  This function must be called
2304a0a1a5fdSTejun Heo  * between freeze_workqueues_begin() and thaw_workqueues().
2305a0a1a5fdSTejun Heo  *
2306a0a1a5fdSTejun Heo  * CONTEXT:
2307a0a1a5fdSTejun Heo  * Grabs and releases workqueue_lock.
2308a0a1a5fdSTejun Heo  *
2309a0a1a5fdSTejun Heo  * RETURNS:
2310a0a1a5fdSTejun Heo  * %true if some freezeable workqueues are still busy.  %false if
2311a0a1a5fdSTejun Heo  * freezing is complete.
2312a0a1a5fdSTejun Heo  */
2313a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void)
2314a0a1a5fdSTejun Heo {
2315a0a1a5fdSTejun Heo 	struct workqueue_struct *wq;
2316a0a1a5fdSTejun Heo 	unsigned int cpu;
2317a0a1a5fdSTejun Heo 	bool busy = false;
2318a0a1a5fdSTejun Heo 
2319a0a1a5fdSTejun Heo 	spin_lock(&workqueue_lock);
2320a0a1a5fdSTejun Heo 
2321a0a1a5fdSTejun Heo 	BUG_ON(!workqueue_freezing);
2322a0a1a5fdSTejun Heo 
2323a0a1a5fdSTejun Heo 	for_each_possible_cpu(cpu) {
2324a0a1a5fdSTejun Heo 		/*
2325a0a1a5fdSTejun Heo 		 * nr_active is monotonically decreasing.  It's safe
2326a0a1a5fdSTejun Heo 		 * to peek without lock.
2327a0a1a5fdSTejun Heo 		 */
2328a0a1a5fdSTejun Heo 		list_for_each_entry(wq, &workqueues, list) {
2329a0a1a5fdSTejun Heo 			struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
2330a0a1a5fdSTejun Heo 
2331a0a1a5fdSTejun Heo 			if (!(wq->flags & WQ_FREEZEABLE))
2332a0a1a5fdSTejun Heo 				continue;
2333a0a1a5fdSTejun Heo 
2334a0a1a5fdSTejun Heo 			BUG_ON(cwq->nr_active < 0);
2335a0a1a5fdSTejun Heo 			if (cwq->nr_active) {
2336a0a1a5fdSTejun Heo 				busy = true;
2337a0a1a5fdSTejun Heo 				goto out_unlock;
2338a0a1a5fdSTejun Heo 			}
2339a0a1a5fdSTejun Heo 		}
2340a0a1a5fdSTejun Heo 	}
2341a0a1a5fdSTejun Heo out_unlock:
2342a0a1a5fdSTejun Heo 	spin_unlock(&workqueue_lock);
2343a0a1a5fdSTejun Heo 	return busy;
2344a0a1a5fdSTejun Heo }
2345a0a1a5fdSTejun Heo 
2346a0a1a5fdSTejun Heo /**
2347a0a1a5fdSTejun Heo  * thaw_workqueues - thaw workqueues
2348a0a1a5fdSTejun Heo  *
2349a0a1a5fdSTejun Heo  * Thaw workqueues.  Normal queueing is restored and all collected
2350a0a1a5fdSTejun Heo  * frozen works are transferred to their respective cwq worklists.
2351a0a1a5fdSTejun Heo  *
2352a0a1a5fdSTejun Heo  * CONTEXT:
23538b03ae3cSTejun Heo  * Grabs and releases workqueue_lock and gcwq->lock's.
2354a0a1a5fdSTejun Heo  */
2355a0a1a5fdSTejun Heo void thaw_workqueues(void)
2356a0a1a5fdSTejun Heo {
2357a0a1a5fdSTejun Heo 	struct workqueue_struct *wq;
2358a0a1a5fdSTejun Heo 	unsigned int cpu;
2359a0a1a5fdSTejun Heo 
2360a0a1a5fdSTejun Heo 	spin_lock(&workqueue_lock);
2361a0a1a5fdSTejun Heo 
2362a0a1a5fdSTejun Heo 	if (!workqueue_freezing)
2363a0a1a5fdSTejun Heo 		goto out_unlock;
2364a0a1a5fdSTejun Heo 
2365a0a1a5fdSTejun Heo 	for_each_possible_cpu(cpu) {
23668b03ae3cSTejun Heo 		struct global_cwq *gcwq = get_gcwq(cpu);
23678b03ae3cSTejun Heo 
23688b03ae3cSTejun Heo 		spin_lock_irq(&gcwq->lock);
23698b03ae3cSTejun Heo 
2370db7bccf4STejun Heo 		BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
2371db7bccf4STejun Heo 		gcwq->flags &= ~GCWQ_FREEZING;
2372db7bccf4STejun Heo 
2373a0a1a5fdSTejun Heo 		list_for_each_entry(wq, &workqueues, list) {
2374a0a1a5fdSTejun Heo 			struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
2375a0a1a5fdSTejun Heo 
2376a0a1a5fdSTejun Heo 			if (!(wq->flags & WQ_FREEZEABLE))
2377a0a1a5fdSTejun Heo 				continue;
2378a0a1a5fdSTejun Heo 
2379a0a1a5fdSTejun Heo 			/* restore max_active and repopulate worklist */
2380a0a1a5fdSTejun Heo 			cwq->max_active = wq->saved_max_active;
2381a0a1a5fdSTejun Heo 
2382a0a1a5fdSTejun Heo 			while (!list_empty(&cwq->delayed_works) &&
2383a0a1a5fdSTejun Heo 			       cwq->nr_active < cwq->max_active)
2384a0a1a5fdSTejun Heo 				cwq_activate_first_delayed(cwq);
2385a0a1a5fdSTejun Heo 
2386502ca9d8STejun Heo 			/* perform delayed unbind from single cpu if empty */
2387502ca9d8STejun Heo 			if (wq->single_cpu == gcwq->cpu &&
2388502ca9d8STejun Heo 			    !cwq->nr_active && list_empty(&cwq->delayed_works))
2389502ca9d8STejun Heo 				cwq_unbind_single_cpu(cwq);
2390502ca9d8STejun Heo 
2391c8e55f36STejun Heo 			wake_up_process(cwq->worker->task);
2392a0a1a5fdSTejun Heo 		}
23938b03ae3cSTejun Heo 
23948b03ae3cSTejun Heo 		spin_unlock_irq(&gcwq->lock);
2395a0a1a5fdSTejun Heo 	}
2396a0a1a5fdSTejun Heo 
2397a0a1a5fdSTejun Heo 	workqueue_freezing = false;
2398a0a1a5fdSTejun Heo out_unlock:
2399a0a1a5fdSTejun Heo 	spin_unlock(&workqueue_lock);
2400a0a1a5fdSTejun Heo }
2401a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */
2402a0a1a5fdSTejun Heo 
2403c12920d1SOleg Nesterov void __init init_workqueues(void)
24041da177e4SLinus Torvalds {
2405c34056a3STejun Heo 	unsigned int cpu;
2406c8e55f36STejun Heo 	int i;
2407c34056a3STejun Heo 
2408db7bccf4STejun Heo 	hotcpu_notifier(workqueue_cpu_callback, CPU_PRI_WORKQUEUE);
24098b03ae3cSTejun Heo 
24108b03ae3cSTejun Heo 	/* initialize gcwqs */
24118b03ae3cSTejun Heo 	for_each_possible_cpu(cpu) {
24128b03ae3cSTejun Heo 		struct global_cwq *gcwq = get_gcwq(cpu);
24138b03ae3cSTejun Heo 
24148b03ae3cSTejun Heo 		spin_lock_init(&gcwq->lock);
24158b03ae3cSTejun Heo 		gcwq->cpu = cpu;
24168b03ae3cSTejun Heo 
2417c8e55f36STejun Heo 		INIT_LIST_HEAD(&gcwq->idle_list);
2418c8e55f36STejun Heo 		for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
2419c8e55f36STejun Heo 			INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
2420c8e55f36STejun Heo 
24218b03ae3cSTejun Heo 		ida_init(&gcwq->worker_ida);
2422db7bccf4STejun Heo 
2423db7bccf4STejun Heo 		gcwq->trustee_state = TRUSTEE_DONE;
2424db7bccf4STejun Heo 		init_waitqueue_head(&gcwq->trustee_wait);
24258b03ae3cSTejun Heo 	}
24268b03ae3cSTejun Heo 
24271da177e4SLinus Torvalds 	keventd_wq = create_workqueue("events");
24281da177e4SLinus Torvalds 	BUG_ON(!keventd_wq);
24291da177e4SLinus Torvalds }
2430