xref: /linux-6.15/kernel/workqueue.c (revision c34056a3)
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>
36*c34056a3STejun Heo #include <linux/idr.h>
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds /*
394690c4abSTejun Heo  * Structure fields follow one of the following exclusion rules.
404690c4abSTejun Heo  *
414690c4abSTejun Heo  * I: Set during initialization and read-only afterwards.
424690c4abSTejun Heo  *
434690c4abSTejun Heo  * L: cwq->lock protected.  Access with cwq->lock held.
444690c4abSTejun Heo  *
4573f53c4aSTejun Heo  * F: wq->flush_mutex protected.
4673f53c4aSTejun Heo  *
474690c4abSTejun Heo  * W: workqueue_lock protected.
484690c4abSTejun Heo  */
494690c4abSTejun Heo 
50*c34056a3STejun Heo struct cpu_workqueue_struct;
51*c34056a3STejun Heo 
52*c34056a3STejun Heo struct worker {
53*c34056a3STejun Heo 	struct work_struct	*current_work;	/* L: work being processed */
54*c34056a3STejun Heo 	struct task_struct	*task;		/* I: worker task */
55*c34056a3STejun Heo 	struct cpu_workqueue_struct *cwq;	/* I: the associated cwq */
56*c34056a3STejun Heo 	int			id;		/* I: worker id */
57*c34056a3STejun Heo };
58*c34056a3STejun Heo 
594690c4abSTejun Heo /*
60f756d5e2SNathan Lynch  * The per-CPU workqueue (if single thread, we always use the first
610f900049STejun Heo  * possible cpu).  The lower WORK_STRUCT_FLAG_BITS of
620f900049STejun Heo  * work_struct->data are used for flags and thus cwqs need to be
630f900049STejun Heo  * aligned at two's power of the number of flag bits.
641da177e4SLinus Torvalds  */
651da177e4SLinus Torvalds struct cpu_workqueue_struct {
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds 	spinlock_t lock;
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds 	struct list_head worklist;
701da177e4SLinus Torvalds 	wait_queue_head_t more_work;
711537663fSTejun Heo 	unsigned int		cpu;
72*c34056a3STejun Heo 	struct worker		*worker;
731da177e4SLinus Torvalds 
744690c4abSTejun Heo 	struct workqueue_struct *wq;		/* I: the owning workqueue */
7573f53c4aSTejun Heo 	int			work_color;	/* L: current color */
7673f53c4aSTejun Heo 	int			flush_color;	/* L: flushing color */
7773f53c4aSTejun Heo 	int			nr_in_flight[WORK_NR_COLORS];
7873f53c4aSTejun Heo 						/* L: nr of in_flight works */
790f900049STejun Heo };
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds /*
8273f53c4aSTejun Heo  * Structure used to wait for workqueue flush.
8373f53c4aSTejun Heo  */
8473f53c4aSTejun Heo struct wq_flusher {
8573f53c4aSTejun Heo 	struct list_head	list;		/* F: list of flushers */
8673f53c4aSTejun Heo 	int			flush_color;	/* F: flush color waiting for */
8773f53c4aSTejun Heo 	struct completion	done;		/* flush completion */
8873f53c4aSTejun Heo };
8973f53c4aSTejun Heo 
9073f53c4aSTejun Heo /*
911da177e4SLinus Torvalds  * The externally visible workqueue abstraction is an array of
921da177e4SLinus Torvalds  * per-CPU workqueues:
931da177e4SLinus Torvalds  */
941da177e4SLinus Torvalds struct workqueue_struct {
9597e37d7bSTejun Heo 	unsigned int		flags;		/* I: WQ_* flags */
964690c4abSTejun Heo 	struct cpu_workqueue_struct *cpu_wq;	/* I: cwq's */
974690c4abSTejun Heo 	struct list_head	list;		/* W: list of all workqueues */
9873f53c4aSTejun Heo 
9973f53c4aSTejun Heo 	struct mutex		flush_mutex;	/* protects wq flushing */
10073f53c4aSTejun Heo 	int			work_color;	/* F: current work color */
10173f53c4aSTejun Heo 	int			flush_color;	/* F: current flush color */
10273f53c4aSTejun Heo 	atomic_t		nr_cwqs_to_flush; /* flush in progress */
10373f53c4aSTejun Heo 	struct wq_flusher	*first_flusher;	/* F: first flusher */
10473f53c4aSTejun Heo 	struct list_head	flusher_queue;	/* F: flush waiters */
10573f53c4aSTejun Heo 	struct list_head	flusher_overflow; /* F: flush overflow list */
10673f53c4aSTejun Heo 
1074690c4abSTejun Heo 	const char		*name;		/* I: workqueue name */
1084e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
1094e6045f1SJohannes Berg 	struct lockdep_map	lockdep_map;
1104e6045f1SJohannes Berg #endif
1111da177e4SLinus Torvalds };
1121da177e4SLinus Torvalds 
113dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK
114dc186ad7SThomas Gleixner 
115dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr;
116dc186ad7SThomas Gleixner 
117dc186ad7SThomas Gleixner /*
118dc186ad7SThomas Gleixner  * fixup_init is called when:
119dc186ad7SThomas Gleixner  * - an active object is initialized
120dc186ad7SThomas Gleixner  */
121dc186ad7SThomas Gleixner static int work_fixup_init(void *addr, enum debug_obj_state state)
122dc186ad7SThomas Gleixner {
123dc186ad7SThomas Gleixner 	struct work_struct *work = addr;
124dc186ad7SThomas Gleixner 
125dc186ad7SThomas Gleixner 	switch (state) {
126dc186ad7SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
127dc186ad7SThomas Gleixner 		cancel_work_sync(work);
128dc186ad7SThomas Gleixner 		debug_object_init(work, &work_debug_descr);
129dc186ad7SThomas Gleixner 		return 1;
130dc186ad7SThomas Gleixner 	default:
131dc186ad7SThomas Gleixner 		return 0;
132dc186ad7SThomas Gleixner 	}
133dc186ad7SThomas Gleixner }
134dc186ad7SThomas Gleixner 
135dc186ad7SThomas Gleixner /*
136dc186ad7SThomas Gleixner  * fixup_activate is called when:
137dc186ad7SThomas Gleixner  * - an active object is activated
138dc186ad7SThomas Gleixner  * - an unknown object is activated (might be a statically initialized object)
139dc186ad7SThomas Gleixner  */
140dc186ad7SThomas Gleixner static int work_fixup_activate(void *addr, enum debug_obj_state state)
141dc186ad7SThomas Gleixner {
142dc186ad7SThomas Gleixner 	struct work_struct *work = addr;
143dc186ad7SThomas Gleixner 
144dc186ad7SThomas Gleixner 	switch (state) {
145dc186ad7SThomas Gleixner 
146dc186ad7SThomas Gleixner 	case ODEBUG_STATE_NOTAVAILABLE:
147dc186ad7SThomas Gleixner 		/*
148dc186ad7SThomas Gleixner 		 * This is not really a fixup. The work struct was
149dc186ad7SThomas Gleixner 		 * statically initialized. We just make sure that it
150dc186ad7SThomas Gleixner 		 * is tracked in the object tracker.
151dc186ad7SThomas Gleixner 		 */
15222df02bbSTejun Heo 		if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
153dc186ad7SThomas Gleixner 			debug_object_init(work, &work_debug_descr);
154dc186ad7SThomas Gleixner 			debug_object_activate(work, &work_debug_descr);
155dc186ad7SThomas Gleixner 			return 0;
156dc186ad7SThomas Gleixner 		}
157dc186ad7SThomas Gleixner 		WARN_ON_ONCE(1);
158dc186ad7SThomas Gleixner 		return 0;
159dc186ad7SThomas Gleixner 
160dc186ad7SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
161dc186ad7SThomas Gleixner 		WARN_ON(1);
162dc186ad7SThomas Gleixner 
163dc186ad7SThomas Gleixner 	default:
164dc186ad7SThomas Gleixner 		return 0;
165dc186ad7SThomas Gleixner 	}
166dc186ad7SThomas Gleixner }
167dc186ad7SThomas Gleixner 
168dc186ad7SThomas Gleixner /*
169dc186ad7SThomas Gleixner  * fixup_free is called when:
170dc186ad7SThomas Gleixner  * - an active object is freed
171dc186ad7SThomas Gleixner  */
172dc186ad7SThomas Gleixner static int work_fixup_free(void *addr, enum debug_obj_state state)
173dc186ad7SThomas Gleixner {
174dc186ad7SThomas Gleixner 	struct work_struct *work = addr;
175dc186ad7SThomas Gleixner 
176dc186ad7SThomas Gleixner 	switch (state) {
177dc186ad7SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
178dc186ad7SThomas Gleixner 		cancel_work_sync(work);
179dc186ad7SThomas Gleixner 		debug_object_free(work, &work_debug_descr);
180dc186ad7SThomas Gleixner 		return 1;
181dc186ad7SThomas Gleixner 	default:
182dc186ad7SThomas Gleixner 		return 0;
183dc186ad7SThomas Gleixner 	}
184dc186ad7SThomas Gleixner }
185dc186ad7SThomas Gleixner 
186dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr = {
187dc186ad7SThomas Gleixner 	.name		= "work_struct",
188dc186ad7SThomas Gleixner 	.fixup_init	= work_fixup_init,
189dc186ad7SThomas Gleixner 	.fixup_activate	= work_fixup_activate,
190dc186ad7SThomas Gleixner 	.fixup_free	= work_fixup_free,
191dc186ad7SThomas Gleixner };
192dc186ad7SThomas Gleixner 
193dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work)
194dc186ad7SThomas Gleixner {
195dc186ad7SThomas Gleixner 	debug_object_activate(work, &work_debug_descr);
196dc186ad7SThomas Gleixner }
197dc186ad7SThomas Gleixner 
198dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work)
199dc186ad7SThomas Gleixner {
200dc186ad7SThomas Gleixner 	debug_object_deactivate(work, &work_debug_descr);
201dc186ad7SThomas Gleixner }
202dc186ad7SThomas Gleixner 
203dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack)
204dc186ad7SThomas Gleixner {
205dc186ad7SThomas Gleixner 	if (onstack)
206dc186ad7SThomas Gleixner 		debug_object_init_on_stack(work, &work_debug_descr);
207dc186ad7SThomas Gleixner 	else
208dc186ad7SThomas Gleixner 		debug_object_init(work, &work_debug_descr);
209dc186ad7SThomas Gleixner }
210dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work);
211dc186ad7SThomas Gleixner 
212dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work)
213dc186ad7SThomas Gleixner {
214dc186ad7SThomas Gleixner 	debug_object_free(work, &work_debug_descr);
215dc186ad7SThomas Gleixner }
216dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack);
217dc186ad7SThomas Gleixner 
218dc186ad7SThomas Gleixner #else
219dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { }
220dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { }
221dc186ad7SThomas Gleixner #endif
222dc186ad7SThomas Gleixner 
22395402b38SGautham R Shenoy /* Serializes the accesses to the list of workqueues. */
22495402b38SGautham R Shenoy static DEFINE_SPINLOCK(workqueue_lock);
2251da177e4SLinus Torvalds static LIST_HEAD(workqueues);
226*c34056a3STejun Heo static DEFINE_PER_CPU(struct ida, worker_ida);
227*c34056a3STejun Heo 
228*c34056a3STejun Heo static int worker_thread(void *__worker);
2291da177e4SLinus Torvalds 
2303af24433SOleg Nesterov static int singlethread_cpu __read_mostly;
231b1f4ec17SOleg Nesterov 
2324690c4abSTejun Heo static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
2334690c4abSTejun Heo 					    struct workqueue_struct *wq)
234a848e3b6SOleg Nesterov {
235a848e3b6SOleg Nesterov 	return per_cpu_ptr(wq->cpu_wq, cpu);
236a848e3b6SOleg Nesterov }
237a848e3b6SOleg Nesterov 
2381537663fSTejun Heo static struct cpu_workqueue_struct *target_cwq(unsigned int cpu,
2391537663fSTejun Heo 					       struct workqueue_struct *wq)
2401537663fSTejun Heo {
2411537663fSTejun Heo 	if (unlikely(wq->flags & WQ_SINGLE_THREAD))
2421537663fSTejun Heo 		cpu = singlethread_cpu;
2431537663fSTejun Heo 	return get_cwq(cpu, wq);
2441537663fSTejun Heo }
2451537663fSTejun Heo 
24673f53c4aSTejun Heo static unsigned int work_color_to_flags(int color)
24773f53c4aSTejun Heo {
24873f53c4aSTejun Heo 	return color << WORK_STRUCT_COLOR_SHIFT;
24973f53c4aSTejun Heo }
25073f53c4aSTejun Heo 
25173f53c4aSTejun Heo static int get_work_color(struct work_struct *work)
25273f53c4aSTejun Heo {
25373f53c4aSTejun Heo 	return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
25473f53c4aSTejun Heo 		((1 << WORK_STRUCT_COLOR_BITS) - 1);
25573f53c4aSTejun Heo }
25673f53c4aSTejun Heo 
25773f53c4aSTejun Heo static int work_next_color(int color)
25873f53c4aSTejun Heo {
25973f53c4aSTejun Heo 	return (color + 1) % WORK_NR_COLORS;
26073f53c4aSTejun Heo }
26173f53c4aSTejun Heo 
2624594bf15SDavid Howells /*
2634594bf15SDavid Howells  * Set the workqueue on which a work item is to be run
2644594bf15SDavid Howells  * - Must *only* be called if the pending flag is set
2654594bf15SDavid Howells  */
266ed7c0feeSOleg Nesterov static inline void set_wq_data(struct work_struct *work,
2674690c4abSTejun Heo 			       struct cpu_workqueue_struct *cwq,
2684690c4abSTejun Heo 			       unsigned long extra_flags)
269365970a1SDavid Howells {
2704594bf15SDavid Howells 	BUG_ON(!work_pending(work));
2714594bf15SDavid Howells 
2724690c4abSTejun Heo 	atomic_long_set(&work->data, (unsigned long)cwq | work_static(work) |
27322df02bbSTejun Heo 			WORK_STRUCT_PENDING | extra_flags);
274365970a1SDavid Howells }
275365970a1SDavid Howells 
2764d707b9fSOleg Nesterov /*
2774d707b9fSOleg Nesterov  * Clear WORK_STRUCT_PENDING and the workqueue on which it was queued.
2784d707b9fSOleg Nesterov  */
2794d707b9fSOleg Nesterov static inline void clear_wq_data(struct work_struct *work)
2804d707b9fSOleg Nesterov {
2814690c4abSTejun Heo 	atomic_long_set(&work->data, work_static(work));
2824d707b9fSOleg Nesterov }
2834d707b9fSOleg Nesterov 
28464166699STejun Heo static inline struct cpu_workqueue_struct *get_wq_data(struct work_struct *work)
285365970a1SDavid Howells {
28664166699STejun Heo 	return (void *)(atomic_long_read(&work->data) &
28764166699STejun Heo 			WORK_STRUCT_WQ_DATA_MASK);
288365970a1SDavid Howells }
289365970a1SDavid Howells 
2904690c4abSTejun Heo /**
2914690c4abSTejun Heo  * insert_work - insert a work into cwq
2924690c4abSTejun Heo  * @cwq: cwq @work belongs to
2934690c4abSTejun Heo  * @work: work to insert
2944690c4abSTejun Heo  * @head: insertion point
2954690c4abSTejun Heo  * @extra_flags: extra WORK_STRUCT_* flags to set
2964690c4abSTejun Heo  *
2974690c4abSTejun Heo  * Insert @work into @cwq after @head.
2984690c4abSTejun Heo  *
2994690c4abSTejun Heo  * CONTEXT:
3004690c4abSTejun Heo  * spin_lock_irq(cwq->lock).
3014690c4abSTejun Heo  */
302b89deed3SOleg Nesterov static void insert_work(struct cpu_workqueue_struct *cwq,
3034690c4abSTejun Heo 			struct work_struct *work, struct list_head *head,
3044690c4abSTejun Heo 			unsigned int extra_flags)
305b89deed3SOleg Nesterov {
3064690c4abSTejun Heo 	/* we own @work, set data and link */
3074690c4abSTejun Heo 	set_wq_data(work, cwq, extra_flags);
3084690c4abSTejun Heo 
3096e84d644SOleg Nesterov 	/*
3106e84d644SOleg Nesterov 	 * Ensure that we get the right work->data if we see the
3116e84d644SOleg Nesterov 	 * result of list_add() below, see try_to_grab_pending().
3126e84d644SOleg Nesterov 	 */
3136e84d644SOleg Nesterov 	smp_wmb();
3144690c4abSTejun Heo 
3151a4d9b0aSOleg Nesterov 	list_add_tail(&work->entry, head);
316b89deed3SOleg Nesterov 	wake_up(&cwq->more_work);
317b89deed3SOleg Nesterov }
318b89deed3SOleg Nesterov 
3194690c4abSTejun Heo static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
3201da177e4SLinus Torvalds 			 struct work_struct *work)
3211da177e4SLinus Torvalds {
3221537663fSTejun Heo 	struct cpu_workqueue_struct *cwq = target_cwq(cpu, wq);
3231da177e4SLinus Torvalds 	unsigned long flags;
3241da177e4SLinus Torvalds 
325dc186ad7SThomas Gleixner 	debug_work_activate(work);
3261da177e4SLinus Torvalds 	spin_lock_irqsave(&cwq->lock, flags);
3274690c4abSTejun Heo 	BUG_ON(!list_empty(&work->entry));
32873f53c4aSTejun Heo 	cwq->nr_in_flight[cwq->work_color]++;
32973f53c4aSTejun Heo 	insert_work(cwq, work, &cwq->worklist,
33073f53c4aSTejun Heo 		    work_color_to_flags(cwq->work_color));
3311da177e4SLinus Torvalds 	spin_unlock_irqrestore(&cwq->lock, flags);
3321da177e4SLinus Torvalds }
3331da177e4SLinus Torvalds 
3340fcb78c2SRolf Eike Beer /**
3350fcb78c2SRolf Eike Beer  * queue_work - queue work on a workqueue
3360fcb78c2SRolf Eike Beer  * @wq: workqueue to use
3370fcb78c2SRolf Eike Beer  * @work: work to queue
3380fcb78c2SRolf Eike Beer  *
339057647fcSAlan Stern  * Returns 0 if @work was already on a queue, non-zero otherwise.
3401da177e4SLinus Torvalds  *
34100dfcaf7SOleg Nesterov  * We queue the work to the CPU on which it was submitted, but if the CPU dies
34200dfcaf7SOleg Nesterov  * it can be processed by another CPU.
3431da177e4SLinus Torvalds  */
3447ad5b3a5SHarvey Harrison int queue_work(struct workqueue_struct *wq, struct work_struct *work)
3451da177e4SLinus Torvalds {
346ef1ca236SOleg Nesterov 	int ret;
3471da177e4SLinus Torvalds 
348ef1ca236SOleg Nesterov 	ret = queue_work_on(get_cpu(), wq, work);
349a848e3b6SOleg Nesterov 	put_cpu();
350ef1ca236SOleg Nesterov 
3511da177e4SLinus Torvalds 	return ret;
3521da177e4SLinus Torvalds }
353ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_work);
3541da177e4SLinus Torvalds 
355c1a220e7SZhang Rui /**
356c1a220e7SZhang Rui  * queue_work_on - queue work on specific cpu
357c1a220e7SZhang Rui  * @cpu: CPU number to execute work on
358c1a220e7SZhang Rui  * @wq: workqueue to use
359c1a220e7SZhang Rui  * @work: work to queue
360c1a220e7SZhang Rui  *
361c1a220e7SZhang Rui  * Returns 0 if @work was already on a queue, non-zero otherwise.
362c1a220e7SZhang Rui  *
363c1a220e7SZhang Rui  * We queue the work to a specific CPU, the caller must ensure it
364c1a220e7SZhang Rui  * can't go away.
365c1a220e7SZhang Rui  */
366c1a220e7SZhang Rui int
367c1a220e7SZhang Rui queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
368c1a220e7SZhang Rui {
369c1a220e7SZhang Rui 	int ret = 0;
370c1a220e7SZhang Rui 
37122df02bbSTejun Heo 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
3724690c4abSTejun Heo 		__queue_work(cpu, wq, work);
373c1a220e7SZhang Rui 		ret = 1;
374c1a220e7SZhang Rui 	}
375c1a220e7SZhang Rui 	return ret;
376c1a220e7SZhang Rui }
377c1a220e7SZhang Rui EXPORT_SYMBOL_GPL(queue_work_on);
378c1a220e7SZhang Rui 
3796d141c3fSLi Zefan static void delayed_work_timer_fn(unsigned long __data)
3801da177e4SLinus Torvalds {
38152bad64dSDavid Howells 	struct delayed_work *dwork = (struct delayed_work *)__data;
382ed7c0feeSOleg Nesterov 	struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
3831da177e4SLinus Torvalds 
3844690c4abSTejun Heo 	__queue_work(smp_processor_id(), cwq->wq, &dwork->work);
3851da177e4SLinus Torvalds }
3861da177e4SLinus Torvalds 
3870fcb78c2SRolf Eike Beer /**
3880fcb78c2SRolf Eike Beer  * queue_delayed_work - queue work on a workqueue after delay
3890fcb78c2SRolf Eike Beer  * @wq: workqueue to use
390af9997e4SRandy Dunlap  * @dwork: delayable work to queue
3910fcb78c2SRolf Eike Beer  * @delay: number of jiffies to wait before queueing
3920fcb78c2SRolf Eike Beer  *
393057647fcSAlan Stern  * Returns 0 if @work was already on a queue, non-zero otherwise.
3940fcb78c2SRolf Eike Beer  */
3957ad5b3a5SHarvey Harrison int queue_delayed_work(struct workqueue_struct *wq,
39652bad64dSDavid Howells 			struct delayed_work *dwork, unsigned long delay)
3971da177e4SLinus Torvalds {
39852bad64dSDavid Howells 	if (delay == 0)
39963bc0362SOleg Nesterov 		return queue_work(wq, &dwork->work);
4001da177e4SLinus Torvalds 
40163bc0362SOleg Nesterov 	return queue_delayed_work_on(-1, wq, dwork, delay);
4021da177e4SLinus Torvalds }
403ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work);
4041da177e4SLinus Torvalds 
4050fcb78c2SRolf Eike Beer /**
4060fcb78c2SRolf Eike Beer  * queue_delayed_work_on - queue work on specific CPU after delay
4070fcb78c2SRolf Eike Beer  * @cpu: CPU number to execute work on
4080fcb78c2SRolf Eike Beer  * @wq: workqueue to use
409af9997e4SRandy Dunlap  * @dwork: work to queue
4100fcb78c2SRolf Eike Beer  * @delay: number of jiffies to wait before queueing
4110fcb78c2SRolf Eike Beer  *
412057647fcSAlan Stern  * Returns 0 if @work was already on a queue, non-zero otherwise.
4130fcb78c2SRolf Eike Beer  */
4147a6bc1cdSVenkatesh Pallipadi int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
41552bad64dSDavid Howells 			struct delayed_work *dwork, unsigned long delay)
4167a6bc1cdSVenkatesh Pallipadi {
4177a6bc1cdSVenkatesh Pallipadi 	int ret = 0;
41852bad64dSDavid Howells 	struct timer_list *timer = &dwork->timer;
41952bad64dSDavid Howells 	struct work_struct *work = &dwork->work;
4207a6bc1cdSVenkatesh Pallipadi 
42122df02bbSTejun Heo 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
4227a6bc1cdSVenkatesh Pallipadi 		BUG_ON(timer_pending(timer));
4237a6bc1cdSVenkatesh Pallipadi 		BUG_ON(!list_empty(&work->entry));
4247a6bc1cdSVenkatesh Pallipadi 
4258a3e77ccSAndrew Liu 		timer_stats_timer_set_start_info(&dwork->timer);
4268a3e77ccSAndrew Liu 
427ed7c0feeSOleg Nesterov 		/* This stores cwq for the moment, for the timer_fn */
4281537663fSTejun Heo 		set_wq_data(work, target_cwq(raw_smp_processor_id(), wq), 0);
4297a6bc1cdSVenkatesh Pallipadi 		timer->expires = jiffies + delay;
43052bad64dSDavid Howells 		timer->data = (unsigned long)dwork;
4317a6bc1cdSVenkatesh Pallipadi 		timer->function = delayed_work_timer_fn;
43263bc0362SOleg Nesterov 
43363bc0362SOleg Nesterov 		if (unlikely(cpu >= 0))
4347a6bc1cdSVenkatesh Pallipadi 			add_timer_on(timer, cpu);
43563bc0362SOleg Nesterov 		else
43663bc0362SOleg Nesterov 			add_timer(timer);
4377a6bc1cdSVenkatesh Pallipadi 		ret = 1;
4387a6bc1cdSVenkatesh Pallipadi 	}
4397a6bc1cdSVenkatesh Pallipadi 	return ret;
4407a6bc1cdSVenkatesh Pallipadi }
441ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work_on);
4421da177e4SLinus Torvalds 
443*c34056a3STejun Heo static struct worker *alloc_worker(void)
444*c34056a3STejun Heo {
445*c34056a3STejun Heo 	struct worker *worker;
446*c34056a3STejun Heo 
447*c34056a3STejun Heo 	worker = kzalloc(sizeof(*worker), GFP_KERNEL);
448*c34056a3STejun Heo 	return worker;
449*c34056a3STejun Heo }
450*c34056a3STejun Heo 
451*c34056a3STejun Heo /**
452*c34056a3STejun Heo  * create_worker - create a new workqueue worker
453*c34056a3STejun Heo  * @cwq: cwq the new worker will belong to
454*c34056a3STejun Heo  * @bind: whether to set affinity to @cpu or not
455*c34056a3STejun Heo  *
456*c34056a3STejun Heo  * Create a new worker which is bound to @cwq.  The returned worker
457*c34056a3STejun Heo  * can be started by calling start_worker() or destroyed using
458*c34056a3STejun Heo  * destroy_worker().
459*c34056a3STejun Heo  *
460*c34056a3STejun Heo  * CONTEXT:
461*c34056a3STejun Heo  * Might sleep.  Does GFP_KERNEL allocations.
462*c34056a3STejun Heo  *
463*c34056a3STejun Heo  * RETURNS:
464*c34056a3STejun Heo  * Pointer to the newly created worker.
465*c34056a3STejun Heo  */
466*c34056a3STejun Heo static struct worker *create_worker(struct cpu_workqueue_struct *cwq, bool bind)
467*c34056a3STejun Heo {
468*c34056a3STejun Heo 	int id = -1;
469*c34056a3STejun Heo 	struct worker *worker = NULL;
470*c34056a3STejun Heo 
471*c34056a3STejun Heo 	spin_lock(&workqueue_lock);
472*c34056a3STejun Heo 	while (ida_get_new(&per_cpu(worker_ida, cwq->cpu), &id)) {
473*c34056a3STejun Heo 		spin_unlock(&workqueue_lock);
474*c34056a3STejun Heo 		if (!ida_pre_get(&per_cpu(worker_ida, cwq->cpu), GFP_KERNEL))
475*c34056a3STejun Heo 			goto fail;
476*c34056a3STejun Heo 		spin_lock(&workqueue_lock);
477*c34056a3STejun Heo 	}
478*c34056a3STejun Heo 	spin_unlock(&workqueue_lock);
479*c34056a3STejun Heo 
480*c34056a3STejun Heo 	worker = alloc_worker();
481*c34056a3STejun Heo 	if (!worker)
482*c34056a3STejun Heo 		goto fail;
483*c34056a3STejun Heo 
484*c34056a3STejun Heo 	worker->cwq = cwq;
485*c34056a3STejun Heo 	worker->id = id;
486*c34056a3STejun Heo 
487*c34056a3STejun Heo 	worker->task = kthread_create(worker_thread, worker, "kworker/%u:%d",
488*c34056a3STejun Heo 				      cwq->cpu, id);
489*c34056a3STejun Heo 	if (IS_ERR(worker->task))
490*c34056a3STejun Heo 		goto fail;
491*c34056a3STejun Heo 
492*c34056a3STejun Heo 	if (bind)
493*c34056a3STejun Heo 		kthread_bind(worker->task, cwq->cpu);
494*c34056a3STejun Heo 
495*c34056a3STejun Heo 	return worker;
496*c34056a3STejun Heo fail:
497*c34056a3STejun Heo 	if (id >= 0) {
498*c34056a3STejun Heo 		spin_lock(&workqueue_lock);
499*c34056a3STejun Heo 		ida_remove(&per_cpu(worker_ida, cwq->cpu), id);
500*c34056a3STejun Heo 		spin_unlock(&workqueue_lock);
501*c34056a3STejun Heo 	}
502*c34056a3STejun Heo 	kfree(worker);
503*c34056a3STejun Heo 	return NULL;
504*c34056a3STejun Heo }
505*c34056a3STejun Heo 
506*c34056a3STejun Heo /**
507*c34056a3STejun Heo  * start_worker - start a newly created worker
508*c34056a3STejun Heo  * @worker: worker to start
509*c34056a3STejun Heo  *
510*c34056a3STejun Heo  * Start @worker.
511*c34056a3STejun Heo  *
512*c34056a3STejun Heo  * CONTEXT:
513*c34056a3STejun Heo  * spin_lock_irq(cwq->lock).
514*c34056a3STejun Heo  */
515*c34056a3STejun Heo static void start_worker(struct worker *worker)
516*c34056a3STejun Heo {
517*c34056a3STejun Heo 	wake_up_process(worker->task);
518*c34056a3STejun Heo }
519*c34056a3STejun Heo 
520*c34056a3STejun Heo /**
521*c34056a3STejun Heo  * destroy_worker - destroy a workqueue worker
522*c34056a3STejun Heo  * @worker: worker to be destroyed
523*c34056a3STejun Heo  *
524*c34056a3STejun Heo  * Destroy @worker.
525*c34056a3STejun Heo  */
526*c34056a3STejun Heo static void destroy_worker(struct worker *worker)
527*c34056a3STejun Heo {
528*c34056a3STejun Heo 	int cpu = worker->cwq->cpu;
529*c34056a3STejun Heo 	int id = worker->id;
530*c34056a3STejun Heo 
531*c34056a3STejun Heo 	/* sanity check frenzy */
532*c34056a3STejun Heo 	BUG_ON(worker->current_work);
533*c34056a3STejun Heo 
534*c34056a3STejun Heo 	kthread_stop(worker->task);
535*c34056a3STejun Heo 	kfree(worker);
536*c34056a3STejun Heo 
537*c34056a3STejun Heo 	spin_lock(&workqueue_lock);
538*c34056a3STejun Heo 	ida_remove(&per_cpu(worker_ida, cpu), id);
539*c34056a3STejun Heo 	spin_unlock(&workqueue_lock);
540*c34056a3STejun Heo }
541*c34056a3STejun Heo 
542a62428c0STejun Heo /**
54373f53c4aSTejun Heo  * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
54473f53c4aSTejun Heo  * @cwq: cwq of interest
54573f53c4aSTejun Heo  * @color: color of work which left the queue
54673f53c4aSTejun Heo  *
54773f53c4aSTejun Heo  * A work either has completed or is removed from pending queue,
54873f53c4aSTejun Heo  * decrement nr_in_flight of its cwq and handle workqueue flushing.
54973f53c4aSTejun Heo  *
55073f53c4aSTejun Heo  * CONTEXT:
55173f53c4aSTejun Heo  * spin_lock_irq(cwq->lock).
55273f53c4aSTejun Heo  */
55373f53c4aSTejun Heo static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color)
55473f53c4aSTejun Heo {
55573f53c4aSTejun Heo 	/* ignore uncolored works */
55673f53c4aSTejun Heo 	if (color == WORK_NO_COLOR)
55773f53c4aSTejun Heo 		return;
55873f53c4aSTejun Heo 
55973f53c4aSTejun Heo 	cwq->nr_in_flight[color]--;
56073f53c4aSTejun Heo 
56173f53c4aSTejun Heo 	/* is flush in progress and are we at the flushing tip? */
56273f53c4aSTejun Heo 	if (likely(cwq->flush_color != color))
56373f53c4aSTejun Heo 		return;
56473f53c4aSTejun Heo 
56573f53c4aSTejun Heo 	/* are there still in-flight works? */
56673f53c4aSTejun Heo 	if (cwq->nr_in_flight[color])
56773f53c4aSTejun Heo 		return;
56873f53c4aSTejun Heo 
56973f53c4aSTejun Heo 	/* this cwq is done, clear flush_color */
57073f53c4aSTejun Heo 	cwq->flush_color = -1;
57173f53c4aSTejun Heo 
57273f53c4aSTejun Heo 	/*
57373f53c4aSTejun Heo 	 * If this was the last cwq, wake up the first flusher.  It
57473f53c4aSTejun Heo 	 * will handle the rest.
57573f53c4aSTejun Heo 	 */
57673f53c4aSTejun Heo 	if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
57773f53c4aSTejun Heo 		complete(&cwq->wq->first_flusher->done);
57873f53c4aSTejun Heo }
57973f53c4aSTejun Heo 
58073f53c4aSTejun Heo /**
581a62428c0STejun Heo  * process_one_work - process single work
582*c34056a3STejun Heo  * @worker: self
583a62428c0STejun Heo  * @work: work to process
584a62428c0STejun Heo  *
585a62428c0STejun Heo  * Process @work.  This function contains all the logics necessary to
586a62428c0STejun Heo  * process a single work including synchronization against and
587a62428c0STejun Heo  * interaction with other workers on the same cpu, queueing and
588a62428c0STejun Heo  * flushing.  As long as context requirement is met, any worker can
589a62428c0STejun Heo  * call this function to process a work.
590a62428c0STejun Heo  *
591a62428c0STejun Heo  * CONTEXT:
592a62428c0STejun Heo  * spin_lock_irq(cwq->lock) which is released and regrabbed.
593a62428c0STejun Heo  */
594*c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work)
5951da177e4SLinus Torvalds {
596*c34056a3STejun Heo 	struct cpu_workqueue_struct *cwq = worker->cwq;
5976bb49e59SDavid Howells 	work_func_t f = work->func;
59873f53c4aSTejun Heo 	int work_color;
5994e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
6004e6045f1SJohannes Berg 	/*
601a62428c0STejun Heo 	 * It is permissible to free the struct work_struct from
602a62428c0STejun Heo 	 * inside the function that is called from it, this we need to
603a62428c0STejun Heo 	 * take into account for lockdep too.  To avoid bogus "held
604a62428c0STejun Heo 	 * lock freed" warnings as well as problems when looking into
605a62428c0STejun Heo 	 * work->lockdep_map, make a copy and use that here.
6064e6045f1SJohannes Berg 	 */
6074e6045f1SJohannes Berg 	struct lockdep_map lockdep_map = work->lockdep_map;
6084e6045f1SJohannes Berg #endif
609a62428c0STejun Heo 	/* claim and process */
610dc186ad7SThomas Gleixner 	debug_work_deactivate(work);
611*c34056a3STejun Heo 	worker->current_work = work;
61273f53c4aSTejun Heo 	work_color = get_work_color(work);
613a62428c0STejun Heo 	list_del_init(&work->entry);
614a62428c0STejun Heo 
615f293ea92SOleg Nesterov 	spin_unlock_irq(&cwq->lock);
6161da177e4SLinus Torvalds 
617365970a1SDavid Howells 	BUG_ON(get_wq_data(work) != cwq);
61823b2e599SOleg Nesterov 	work_clear_pending(work);
6193295f0efSIngo Molnar 	lock_map_acquire(&cwq->wq->lockdep_map);
6203295f0efSIngo Molnar 	lock_map_acquire(&lockdep_map);
62165f27f38SDavid Howells 	f(work);
6223295f0efSIngo Molnar 	lock_map_release(&lockdep_map);
6233295f0efSIngo Molnar 	lock_map_release(&cwq->wq->lockdep_map);
6241da177e4SLinus Torvalds 
625d5abe669SPeter Zijlstra 	if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
626d5abe669SPeter Zijlstra 		printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
627d5abe669SPeter Zijlstra 		       "%s/0x%08x/%d\n",
628a62428c0STejun Heo 		       current->comm, preempt_count(), task_pid_nr(current));
629d5abe669SPeter Zijlstra 		printk(KERN_ERR "    last function: ");
630d5abe669SPeter Zijlstra 		print_symbol("%s\n", (unsigned long)f);
631d5abe669SPeter Zijlstra 		debug_show_held_locks(current);
632d5abe669SPeter Zijlstra 		dump_stack();
633d5abe669SPeter Zijlstra 	}
634d5abe669SPeter Zijlstra 
635f293ea92SOleg Nesterov 	spin_lock_irq(&cwq->lock);
636a62428c0STejun Heo 
637a62428c0STejun Heo 	/* we're done with it, release */
638*c34056a3STejun Heo 	worker->current_work = NULL;
63973f53c4aSTejun Heo 	cwq_dec_nr_in_flight(cwq, work_color);
6401da177e4SLinus Torvalds }
641a62428c0STejun Heo 
642*c34056a3STejun Heo static void run_workqueue(struct worker *worker)
643a62428c0STejun Heo {
644*c34056a3STejun Heo 	struct cpu_workqueue_struct *cwq = worker->cwq;
645*c34056a3STejun Heo 
646a62428c0STejun Heo 	spin_lock_irq(&cwq->lock);
647a62428c0STejun Heo 	while (!list_empty(&cwq->worklist)) {
648a62428c0STejun Heo 		struct work_struct *work = list_entry(cwq->worklist.next,
649a62428c0STejun Heo 						struct work_struct, entry);
650*c34056a3STejun Heo 		process_one_work(worker, work);
651a62428c0STejun Heo 	}
652f293ea92SOleg Nesterov 	spin_unlock_irq(&cwq->lock);
6531da177e4SLinus Torvalds }
6541da177e4SLinus Torvalds 
6554690c4abSTejun Heo /**
6564690c4abSTejun Heo  * worker_thread - the worker thread function
657*c34056a3STejun Heo  * @__worker: self
6584690c4abSTejun Heo  *
6594690c4abSTejun Heo  * The cwq worker thread function.
6604690c4abSTejun Heo  */
661*c34056a3STejun Heo static int worker_thread(void *__worker)
6621da177e4SLinus Torvalds {
663*c34056a3STejun Heo 	struct worker *worker = __worker;
664*c34056a3STejun Heo 	struct cpu_workqueue_struct *cwq = worker->cwq;
6653af24433SOleg Nesterov 	DEFINE_WAIT(wait);
6661da177e4SLinus Torvalds 
66797e37d7bSTejun Heo 	if (cwq->wq->flags & WQ_FREEZEABLE)
66883144186SRafael J. Wysocki 		set_freezable();
6691da177e4SLinus Torvalds 
6703af24433SOleg Nesterov 	for (;;) {
6713af24433SOleg Nesterov 		prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
67214441960SOleg Nesterov 		if (!freezing(current) &&
67314441960SOleg Nesterov 		    !kthread_should_stop() &&
67414441960SOleg Nesterov 		    list_empty(&cwq->worklist))
6751da177e4SLinus Torvalds 			schedule();
6763af24433SOleg Nesterov 		finish_wait(&cwq->more_work, &wait);
6771da177e4SLinus Torvalds 
67885f4186aSOleg Nesterov 		try_to_freeze();
67985f4186aSOleg Nesterov 
68014441960SOleg Nesterov 		if (kthread_should_stop())
6813af24433SOleg Nesterov 			break;
6823af24433SOleg Nesterov 
683*c34056a3STejun Heo 		if (unlikely(!cpumask_equal(&worker->task->cpus_allowed,
6841537663fSTejun Heo 					    get_cpu_mask(cwq->cpu))))
685*c34056a3STejun Heo 			set_cpus_allowed_ptr(worker->task,
6861537663fSTejun Heo 					     get_cpu_mask(cwq->cpu));
687*c34056a3STejun Heo 		run_workqueue(worker);
6881da177e4SLinus Torvalds 	}
6893af24433SOleg Nesterov 
6901da177e4SLinus Torvalds 	return 0;
6911da177e4SLinus Torvalds }
6921da177e4SLinus Torvalds 
693fc2e4d70SOleg Nesterov struct wq_barrier {
694fc2e4d70SOleg Nesterov 	struct work_struct	work;
695fc2e4d70SOleg Nesterov 	struct completion	done;
696fc2e4d70SOleg Nesterov };
697fc2e4d70SOleg Nesterov 
698fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work)
699fc2e4d70SOleg Nesterov {
700fc2e4d70SOleg Nesterov 	struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
701fc2e4d70SOleg Nesterov 	complete(&barr->done);
702fc2e4d70SOleg Nesterov }
703fc2e4d70SOleg Nesterov 
7044690c4abSTejun Heo /**
7054690c4abSTejun Heo  * insert_wq_barrier - insert a barrier work
7064690c4abSTejun Heo  * @cwq: cwq to insert barrier into
7074690c4abSTejun Heo  * @barr: wq_barrier to insert
7084690c4abSTejun Heo  * @head: insertion point
7094690c4abSTejun Heo  *
7104690c4abSTejun Heo  * Insert barrier @barr into @cwq before @head.
7114690c4abSTejun Heo  *
7124690c4abSTejun Heo  * CONTEXT:
7134690c4abSTejun Heo  * spin_lock_irq(cwq->lock).
7144690c4abSTejun Heo  */
71583c22520SOleg Nesterov static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
7161a4d9b0aSOleg Nesterov 			struct wq_barrier *barr, struct list_head *head)
717fc2e4d70SOleg Nesterov {
718dc186ad7SThomas Gleixner 	/*
719dc186ad7SThomas Gleixner 	 * debugobject calls are safe here even with cwq->lock locked
720dc186ad7SThomas Gleixner 	 * as we know for sure that this will not trigger any of the
721dc186ad7SThomas Gleixner 	 * checks and call back into the fixup functions where we
722dc186ad7SThomas Gleixner 	 * might deadlock.
723dc186ad7SThomas Gleixner 	 */
724dc186ad7SThomas Gleixner 	INIT_WORK_ON_STACK(&barr->work, wq_barrier_func);
72522df02bbSTejun Heo 	__set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
726fc2e4d70SOleg Nesterov 	init_completion(&barr->done);
72783c22520SOleg Nesterov 
728dc186ad7SThomas Gleixner 	debug_work_activate(&barr->work);
72973f53c4aSTejun Heo 	insert_work(cwq, &barr->work, head, work_color_to_flags(WORK_NO_COLOR));
730fc2e4d70SOleg Nesterov }
731fc2e4d70SOleg Nesterov 
73273f53c4aSTejun Heo /**
73373f53c4aSTejun Heo  * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
73473f53c4aSTejun Heo  * @wq: workqueue being flushed
73573f53c4aSTejun Heo  * @flush_color: new flush color, < 0 for no-op
73673f53c4aSTejun Heo  * @work_color: new work color, < 0 for no-op
73773f53c4aSTejun Heo  *
73873f53c4aSTejun Heo  * Prepare cwqs for workqueue flushing.
73973f53c4aSTejun Heo  *
74073f53c4aSTejun Heo  * If @flush_color is non-negative, flush_color on all cwqs should be
74173f53c4aSTejun Heo  * -1.  If no cwq has in-flight commands at the specified color, all
74273f53c4aSTejun Heo  * cwq->flush_color's stay at -1 and %false is returned.  If any cwq
74373f53c4aSTejun Heo  * has in flight commands, its cwq->flush_color is set to
74473f53c4aSTejun Heo  * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
74573f53c4aSTejun Heo  * wakeup logic is armed and %true is returned.
74673f53c4aSTejun Heo  *
74773f53c4aSTejun Heo  * The caller should have initialized @wq->first_flusher prior to
74873f53c4aSTejun Heo  * calling this function with non-negative @flush_color.  If
74973f53c4aSTejun Heo  * @flush_color is negative, no flush color update is done and %false
75073f53c4aSTejun Heo  * is returned.
75173f53c4aSTejun Heo  *
75273f53c4aSTejun Heo  * If @work_color is non-negative, all cwqs should have the same
75373f53c4aSTejun Heo  * work_color which is previous to @work_color and all will be
75473f53c4aSTejun Heo  * advanced to @work_color.
75573f53c4aSTejun Heo  *
75673f53c4aSTejun Heo  * CONTEXT:
75773f53c4aSTejun Heo  * mutex_lock(wq->flush_mutex).
75873f53c4aSTejun Heo  *
75973f53c4aSTejun Heo  * RETURNS:
76073f53c4aSTejun Heo  * %true if @flush_color >= 0 and there's something to flush.  %false
76173f53c4aSTejun Heo  * otherwise.
76273f53c4aSTejun Heo  */
76373f53c4aSTejun Heo static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
76473f53c4aSTejun Heo 				      int flush_color, int work_color)
7651da177e4SLinus Torvalds {
76673f53c4aSTejun Heo 	bool wait = false;
76773f53c4aSTejun Heo 	unsigned int cpu;
7681da177e4SLinus Torvalds 
76973f53c4aSTejun Heo 	if (flush_color >= 0) {
77073f53c4aSTejun Heo 		BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
77173f53c4aSTejun Heo 		atomic_set(&wq->nr_cwqs_to_flush, 1);
77273f53c4aSTejun Heo 	}
77373f53c4aSTejun Heo 
77473f53c4aSTejun Heo 	for_each_possible_cpu(cpu) {
77573f53c4aSTejun Heo 		struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
7762355b70fSLai Jiangshan 
77783c22520SOleg Nesterov 		spin_lock_irq(&cwq->lock);
77873f53c4aSTejun Heo 
77973f53c4aSTejun Heo 		if (flush_color >= 0) {
78073f53c4aSTejun Heo 			BUG_ON(cwq->flush_color != -1);
78173f53c4aSTejun Heo 
78273f53c4aSTejun Heo 			if (cwq->nr_in_flight[flush_color]) {
78373f53c4aSTejun Heo 				cwq->flush_color = flush_color;
78473f53c4aSTejun Heo 				atomic_inc(&wq->nr_cwqs_to_flush);
78573f53c4aSTejun Heo 				wait = true;
78683c22520SOleg Nesterov 			}
78773f53c4aSTejun Heo 		}
78873f53c4aSTejun Heo 
78973f53c4aSTejun Heo 		if (work_color >= 0) {
79073f53c4aSTejun Heo 			BUG_ON(work_color != work_next_color(cwq->work_color));
79173f53c4aSTejun Heo 			cwq->work_color = work_color;
79273f53c4aSTejun Heo 		}
79373f53c4aSTejun Heo 
79483c22520SOleg Nesterov 		spin_unlock_irq(&cwq->lock);
795dc186ad7SThomas Gleixner 	}
79614441960SOleg Nesterov 
79773f53c4aSTejun Heo 	if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
79873f53c4aSTejun Heo 		complete(&wq->first_flusher->done);
79973f53c4aSTejun Heo 
80073f53c4aSTejun Heo 	return wait;
80183c22520SOleg Nesterov }
8021da177e4SLinus Torvalds 
8030fcb78c2SRolf Eike Beer /**
8041da177e4SLinus Torvalds  * flush_workqueue - ensure that any scheduled work has run to completion.
8050fcb78c2SRolf Eike Beer  * @wq: workqueue to flush
8061da177e4SLinus Torvalds  *
8071da177e4SLinus Torvalds  * Forces execution of the workqueue and blocks until its completion.
8081da177e4SLinus Torvalds  * This is typically used in driver shutdown handlers.
8091da177e4SLinus Torvalds  *
810fc2e4d70SOleg Nesterov  * We sleep until all works which were queued on entry have been handled,
811fc2e4d70SOleg Nesterov  * but we are not livelocked by new incoming ones.
8121da177e4SLinus Torvalds  */
8137ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq)
8141da177e4SLinus Torvalds {
81573f53c4aSTejun Heo 	struct wq_flusher this_flusher = {
81673f53c4aSTejun Heo 		.list = LIST_HEAD_INIT(this_flusher.list),
81773f53c4aSTejun Heo 		.flush_color = -1,
81873f53c4aSTejun Heo 		.done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
81973f53c4aSTejun Heo 	};
82073f53c4aSTejun Heo 	int next_color;
821b1f4ec17SOleg Nesterov 
8223295f0efSIngo Molnar 	lock_map_acquire(&wq->lockdep_map);
8233295f0efSIngo Molnar 	lock_map_release(&wq->lockdep_map);
82473f53c4aSTejun Heo 
82573f53c4aSTejun Heo 	mutex_lock(&wq->flush_mutex);
82673f53c4aSTejun Heo 
82773f53c4aSTejun Heo 	/*
82873f53c4aSTejun Heo 	 * Start-to-wait phase
82973f53c4aSTejun Heo 	 */
83073f53c4aSTejun Heo 	next_color = work_next_color(wq->work_color);
83173f53c4aSTejun Heo 
83273f53c4aSTejun Heo 	if (next_color != wq->flush_color) {
83373f53c4aSTejun Heo 		/*
83473f53c4aSTejun Heo 		 * Color space is not full.  The current work_color
83573f53c4aSTejun Heo 		 * becomes our flush_color and work_color is advanced
83673f53c4aSTejun Heo 		 * by one.
83773f53c4aSTejun Heo 		 */
83873f53c4aSTejun Heo 		BUG_ON(!list_empty(&wq->flusher_overflow));
83973f53c4aSTejun Heo 		this_flusher.flush_color = wq->work_color;
84073f53c4aSTejun Heo 		wq->work_color = next_color;
84173f53c4aSTejun Heo 
84273f53c4aSTejun Heo 		if (!wq->first_flusher) {
84373f53c4aSTejun Heo 			/* no flush in progress, become the first flusher */
84473f53c4aSTejun Heo 			BUG_ON(wq->flush_color != this_flusher.flush_color);
84573f53c4aSTejun Heo 
84673f53c4aSTejun Heo 			wq->first_flusher = &this_flusher;
84773f53c4aSTejun Heo 
84873f53c4aSTejun Heo 			if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
84973f53c4aSTejun Heo 						       wq->work_color)) {
85073f53c4aSTejun Heo 				/* nothing to flush, done */
85173f53c4aSTejun Heo 				wq->flush_color = next_color;
85273f53c4aSTejun Heo 				wq->first_flusher = NULL;
85373f53c4aSTejun Heo 				goto out_unlock;
85473f53c4aSTejun Heo 			}
85573f53c4aSTejun Heo 		} else {
85673f53c4aSTejun Heo 			/* wait in queue */
85773f53c4aSTejun Heo 			BUG_ON(wq->flush_color == this_flusher.flush_color);
85873f53c4aSTejun Heo 			list_add_tail(&this_flusher.list, &wq->flusher_queue);
85973f53c4aSTejun Heo 			flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
86073f53c4aSTejun Heo 		}
86173f53c4aSTejun Heo 	} else {
86273f53c4aSTejun Heo 		/*
86373f53c4aSTejun Heo 		 * Oops, color space is full, wait on overflow queue.
86473f53c4aSTejun Heo 		 * The next flush completion will assign us
86573f53c4aSTejun Heo 		 * flush_color and transfer to flusher_queue.
86673f53c4aSTejun Heo 		 */
86773f53c4aSTejun Heo 		list_add_tail(&this_flusher.list, &wq->flusher_overflow);
86873f53c4aSTejun Heo 	}
86973f53c4aSTejun Heo 
87073f53c4aSTejun Heo 	mutex_unlock(&wq->flush_mutex);
87173f53c4aSTejun Heo 
87273f53c4aSTejun Heo 	wait_for_completion(&this_flusher.done);
87373f53c4aSTejun Heo 
87473f53c4aSTejun Heo 	/*
87573f53c4aSTejun Heo 	 * Wake-up-and-cascade phase
87673f53c4aSTejun Heo 	 *
87773f53c4aSTejun Heo 	 * First flushers are responsible for cascading flushes and
87873f53c4aSTejun Heo 	 * handling overflow.  Non-first flushers can simply return.
87973f53c4aSTejun Heo 	 */
88073f53c4aSTejun Heo 	if (wq->first_flusher != &this_flusher)
88173f53c4aSTejun Heo 		return;
88273f53c4aSTejun Heo 
88373f53c4aSTejun Heo 	mutex_lock(&wq->flush_mutex);
88473f53c4aSTejun Heo 
88573f53c4aSTejun Heo 	wq->first_flusher = NULL;
88673f53c4aSTejun Heo 
88773f53c4aSTejun Heo 	BUG_ON(!list_empty(&this_flusher.list));
88873f53c4aSTejun Heo 	BUG_ON(wq->flush_color != this_flusher.flush_color);
88973f53c4aSTejun Heo 
89073f53c4aSTejun Heo 	while (true) {
89173f53c4aSTejun Heo 		struct wq_flusher *next, *tmp;
89273f53c4aSTejun Heo 
89373f53c4aSTejun Heo 		/* complete all the flushers sharing the current flush color */
89473f53c4aSTejun Heo 		list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
89573f53c4aSTejun Heo 			if (next->flush_color != wq->flush_color)
89673f53c4aSTejun Heo 				break;
89773f53c4aSTejun Heo 			list_del_init(&next->list);
89873f53c4aSTejun Heo 			complete(&next->done);
89973f53c4aSTejun Heo 		}
90073f53c4aSTejun Heo 
90173f53c4aSTejun Heo 		BUG_ON(!list_empty(&wq->flusher_overflow) &&
90273f53c4aSTejun Heo 		       wq->flush_color != work_next_color(wq->work_color));
90373f53c4aSTejun Heo 
90473f53c4aSTejun Heo 		/* this flush_color is finished, advance by one */
90573f53c4aSTejun Heo 		wq->flush_color = work_next_color(wq->flush_color);
90673f53c4aSTejun Heo 
90773f53c4aSTejun Heo 		/* one color has been freed, handle overflow queue */
90873f53c4aSTejun Heo 		if (!list_empty(&wq->flusher_overflow)) {
90973f53c4aSTejun Heo 			/*
91073f53c4aSTejun Heo 			 * Assign the same color to all overflowed
91173f53c4aSTejun Heo 			 * flushers, advance work_color and append to
91273f53c4aSTejun Heo 			 * flusher_queue.  This is the start-to-wait
91373f53c4aSTejun Heo 			 * phase for these overflowed flushers.
91473f53c4aSTejun Heo 			 */
91573f53c4aSTejun Heo 			list_for_each_entry(tmp, &wq->flusher_overflow, list)
91673f53c4aSTejun Heo 				tmp->flush_color = wq->work_color;
91773f53c4aSTejun Heo 
91873f53c4aSTejun Heo 			wq->work_color = work_next_color(wq->work_color);
91973f53c4aSTejun Heo 
92073f53c4aSTejun Heo 			list_splice_tail_init(&wq->flusher_overflow,
92173f53c4aSTejun Heo 					      &wq->flusher_queue);
92273f53c4aSTejun Heo 			flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
92373f53c4aSTejun Heo 		}
92473f53c4aSTejun Heo 
92573f53c4aSTejun Heo 		if (list_empty(&wq->flusher_queue)) {
92673f53c4aSTejun Heo 			BUG_ON(wq->flush_color != wq->work_color);
92773f53c4aSTejun Heo 			break;
92873f53c4aSTejun Heo 		}
92973f53c4aSTejun Heo 
93073f53c4aSTejun Heo 		/*
93173f53c4aSTejun Heo 		 * Need to flush more colors.  Make the next flusher
93273f53c4aSTejun Heo 		 * the new first flusher and arm cwqs.
93373f53c4aSTejun Heo 		 */
93473f53c4aSTejun Heo 		BUG_ON(wq->flush_color == wq->work_color);
93573f53c4aSTejun Heo 		BUG_ON(wq->flush_color != next->flush_color);
93673f53c4aSTejun Heo 
93773f53c4aSTejun Heo 		list_del_init(&next->list);
93873f53c4aSTejun Heo 		wq->first_flusher = next;
93973f53c4aSTejun Heo 
94073f53c4aSTejun Heo 		if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
94173f53c4aSTejun Heo 			break;
94273f53c4aSTejun Heo 
94373f53c4aSTejun Heo 		/*
94473f53c4aSTejun Heo 		 * Meh... this color is already done, clear first
94573f53c4aSTejun Heo 		 * flusher and repeat cascading.
94673f53c4aSTejun Heo 		 */
94773f53c4aSTejun Heo 		wq->first_flusher = NULL;
94873f53c4aSTejun Heo 	}
94973f53c4aSTejun Heo 
95073f53c4aSTejun Heo out_unlock:
95173f53c4aSTejun Heo 	mutex_unlock(&wq->flush_mutex);
9521da177e4SLinus Torvalds }
953ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(flush_workqueue);
9541da177e4SLinus Torvalds 
955db700897SOleg Nesterov /**
956db700897SOleg Nesterov  * flush_work - block until a work_struct's callback has terminated
957db700897SOleg Nesterov  * @work: the work which is to be flushed
958db700897SOleg Nesterov  *
959a67da70dSOleg Nesterov  * Returns false if @work has already terminated.
960a67da70dSOleg Nesterov  *
961db700897SOleg Nesterov  * It is expected that, prior to calling flush_work(), the caller has
962db700897SOleg Nesterov  * arranged for the work to not be requeued, otherwise it doesn't make
963db700897SOleg Nesterov  * sense to use this function.
964db700897SOleg Nesterov  */
965db700897SOleg Nesterov int flush_work(struct work_struct *work)
966db700897SOleg Nesterov {
967db700897SOleg Nesterov 	struct cpu_workqueue_struct *cwq;
968db700897SOleg Nesterov 	struct list_head *prev;
969db700897SOleg Nesterov 	struct wq_barrier barr;
970db700897SOleg Nesterov 
971db700897SOleg Nesterov 	might_sleep();
972db700897SOleg Nesterov 	cwq = get_wq_data(work);
973db700897SOleg Nesterov 	if (!cwq)
974db700897SOleg Nesterov 		return 0;
975db700897SOleg Nesterov 
9763295f0efSIngo Molnar 	lock_map_acquire(&cwq->wq->lockdep_map);
9773295f0efSIngo Molnar 	lock_map_release(&cwq->wq->lockdep_map);
978a67da70dSOleg Nesterov 
979db700897SOleg Nesterov 	spin_lock_irq(&cwq->lock);
980db700897SOleg Nesterov 	if (!list_empty(&work->entry)) {
981db700897SOleg Nesterov 		/*
982db700897SOleg Nesterov 		 * See the comment near try_to_grab_pending()->smp_rmb().
983db700897SOleg Nesterov 		 * If it was re-queued under us we are not going to wait.
984db700897SOleg Nesterov 		 */
985db700897SOleg Nesterov 		smp_rmb();
986db700897SOleg Nesterov 		if (unlikely(cwq != get_wq_data(work)))
9874690c4abSTejun Heo 			goto already_gone;
988db700897SOleg Nesterov 		prev = &work->entry;
989db700897SOleg Nesterov 	} else {
990*c34056a3STejun Heo 		if (!cwq->worker || cwq->worker->current_work != work)
9914690c4abSTejun Heo 			goto already_gone;
992db700897SOleg Nesterov 		prev = &cwq->worklist;
993db700897SOleg Nesterov 	}
994db700897SOleg Nesterov 	insert_wq_barrier(cwq, &barr, prev->next);
995db700897SOleg Nesterov 
9964690c4abSTejun Heo 	spin_unlock_irq(&cwq->lock);
997db700897SOleg Nesterov 	wait_for_completion(&barr.done);
998dc186ad7SThomas Gleixner 	destroy_work_on_stack(&barr.work);
999db700897SOleg Nesterov 	return 1;
10004690c4abSTejun Heo already_gone:
10014690c4abSTejun Heo 	spin_unlock_irq(&cwq->lock);
10024690c4abSTejun Heo 	return 0;
1003db700897SOleg Nesterov }
1004db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work);
1005db700897SOleg Nesterov 
10066e84d644SOleg Nesterov /*
10071f1f642eSOleg Nesterov  * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
10086e84d644SOleg Nesterov  * so this work can't be re-armed in any way.
10096e84d644SOleg Nesterov  */
10106e84d644SOleg Nesterov static int try_to_grab_pending(struct work_struct *work)
10116e84d644SOleg Nesterov {
10126e84d644SOleg Nesterov 	struct cpu_workqueue_struct *cwq;
10131f1f642eSOleg Nesterov 	int ret = -1;
10146e84d644SOleg Nesterov 
101522df02bbSTejun Heo 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
10161f1f642eSOleg Nesterov 		return 0;
10176e84d644SOleg Nesterov 
10186e84d644SOleg Nesterov 	/*
10196e84d644SOleg Nesterov 	 * The queueing is in progress, or it is already queued. Try to
10206e84d644SOleg Nesterov 	 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
10216e84d644SOleg Nesterov 	 */
10226e84d644SOleg Nesterov 
10236e84d644SOleg Nesterov 	cwq = get_wq_data(work);
10246e84d644SOleg Nesterov 	if (!cwq)
10256e84d644SOleg Nesterov 		return ret;
10266e84d644SOleg Nesterov 
10276e84d644SOleg Nesterov 	spin_lock_irq(&cwq->lock);
10286e84d644SOleg Nesterov 	if (!list_empty(&work->entry)) {
10296e84d644SOleg Nesterov 		/*
10306e84d644SOleg Nesterov 		 * This work is queued, but perhaps we locked the wrong cwq.
10316e84d644SOleg Nesterov 		 * In that case we must see the new value after rmb(), see
10326e84d644SOleg Nesterov 		 * insert_work()->wmb().
10336e84d644SOleg Nesterov 		 */
10346e84d644SOleg Nesterov 		smp_rmb();
10356e84d644SOleg Nesterov 		if (cwq == get_wq_data(work)) {
1036dc186ad7SThomas Gleixner 			debug_work_deactivate(work);
10376e84d644SOleg Nesterov 			list_del_init(&work->entry);
103873f53c4aSTejun Heo 			cwq_dec_nr_in_flight(cwq, get_work_color(work));
10396e84d644SOleg Nesterov 			ret = 1;
10406e84d644SOleg Nesterov 		}
10416e84d644SOleg Nesterov 	}
10426e84d644SOleg Nesterov 	spin_unlock_irq(&cwq->lock);
10436e84d644SOleg Nesterov 
10446e84d644SOleg Nesterov 	return ret;
10456e84d644SOleg Nesterov }
10466e84d644SOleg Nesterov 
10476e84d644SOleg Nesterov static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq,
1048b89deed3SOleg Nesterov 				struct work_struct *work)
1049b89deed3SOleg Nesterov {
1050b89deed3SOleg Nesterov 	struct wq_barrier barr;
1051b89deed3SOleg Nesterov 	int running = 0;
1052b89deed3SOleg Nesterov 
1053b89deed3SOleg Nesterov 	spin_lock_irq(&cwq->lock);
1054*c34056a3STejun Heo 	if (unlikely(cwq->worker && cwq->worker->current_work == work)) {
10551a4d9b0aSOleg Nesterov 		insert_wq_barrier(cwq, &barr, cwq->worklist.next);
1056b89deed3SOleg Nesterov 		running = 1;
1057b89deed3SOleg Nesterov 	}
1058b89deed3SOleg Nesterov 	spin_unlock_irq(&cwq->lock);
1059b89deed3SOleg Nesterov 
1060dc186ad7SThomas Gleixner 	if (unlikely(running)) {
1061b89deed3SOleg Nesterov 		wait_for_completion(&barr.done);
1062dc186ad7SThomas Gleixner 		destroy_work_on_stack(&barr.work);
1063dc186ad7SThomas Gleixner 	}
1064b89deed3SOleg Nesterov }
1065b89deed3SOleg Nesterov 
10666e84d644SOleg Nesterov static void wait_on_work(struct work_struct *work)
1067b89deed3SOleg Nesterov {
1068b89deed3SOleg Nesterov 	struct cpu_workqueue_struct *cwq;
106928e53bddSOleg Nesterov 	struct workqueue_struct *wq;
1070b1f4ec17SOleg Nesterov 	int cpu;
1071b89deed3SOleg Nesterov 
1072f293ea92SOleg Nesterov 	might_sleep();
1073f293ea92SOleg Nesterov 
10743295f0efSIngo Molnar 	lock_map_acquire(&work->lockdep_map);
10753295f0efSIngo Molnar 	lock_map_release(&work->lockdep_map);
10764e6045f1SJohannes Berg 
1077b89deed3SOleg Nesterov 	cwq = get_wq_data(work);
1078b89deed3SOleg Nesterov 	if (!cwq)
10793af24433SOleg Nesterov 		return;
1080b89deed3SOleg Nesterov 
108128e53bddSOleg Nesterov 	wq = cwq->wq;
108228e53bddSOleg Nesterov 
10831537663fSTejun Heo 	for_each_possible_cpu(cpu)
10844690c4abSTejun Heo 		wait_on_cpu_work(get_cwq(cpu, wq), work);
10856e84d644SOleg Nesterov }
10866e84d644SOleg Nesterov 
10871f1f642eSOleg Nesterov static int __cancel_work_timer(struct work_struct *work,
10881f1f642eSOleg Nesterov 				struct timer_list* timer)
10891f1f642eSOleg Nesterov {
10901f1f642eSOleg Nesterov 	int ret;
10911f1f642eSOleg Nesterov 
10921f1f642eSOleg Nesterov 	do {
10931f1f642eSOleg Nesterov 		ret = (timer && likely(del_timer(timer)));
10941f1f642eSOleg Nesterov 		if (!ret)
10951f1f642eSOleg Nesterov 			ret = try_to_grab_pending(work);
10961f1f642eSOleg Nesterov 		wait_on_work(work);
10971f1f642eSOleg Nesterov 	} while (unlikely(ret < 0));
10981f1f642eSOleg Nesterov 
10994d707b9fSOleg Nesterov 	clear_wq_data(work);
11001f1f642eSOleg Nesterov 	return ret;
11011f1f642eSOleg Nesterov }
11021f1f642eSOleg Nesterov 
11036e84d644SOleg Nesterov /**
11046e84d644SOleg Nesterov  * cancel_work_sync - block until a work_struct's callback has terminated
11056e84d644SOleg Nesterov  * @work: the work which is to be flushed
11066e84d644SOleg Nesterov  *
11071f1f642eSOleg Nesterov  * Returns true if @work was pending.
11081f1f642eSOleg Nesterov  *
11096e84d644SOleg Nesterov  * cancel_work_sync() will cancel the work if it is queued. If the work's
11106e84d644SOleg Nesterov  * callback appears to be running, cancel_work_sync() will block until it
11116e84d644SOleg Nesterov  * has completed.
11126e84d644SOleg Nesterov  *
11136e84d644SOleg Nesterov  * It is possible to use this function if the work re-queues itself. It can
11146e84d644SOleg Nesterov  * cancel the work even if it migrates to another workqueue, however in that
11156e84d644SOleg Nesterov  * case it only guarantees that work->func() has completed on the last queued
11166e84d644SOleg Nesterov  * workqueue.
11176e84d644SOleg Nesterov  *
11186e84d644SOleg Nesterov  * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
11196e84d644SOleg Nesterov  * pending, otherwise it goes into a busy-wait loop until the timer expires.
11206e84d644SOleg Nesterov  *
11216e84d644SOleg Nesterov  * The caller must ensure that workqueue_struct on which this work was last
11226e84d644SOleg Nesterov  * queued can't be destroyed before this function returns.
11236e84d644SOleg Nesterov  */
11241f1f642eSOleg Nesterov int cancel_work_sync(struct work_struct *work)
11256e84d644SOleg Nesterov {
11261f1f642eSOleg Nesterov 	return __cancel_work_timer(work, NULL);
1127b89deed3SOleg Nesterov }
112828e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync);
1129b89deed3SOleg Nesterov 
11306e84d644SOleg Nesterov /**
1131f5a421a4SOleg Nesterov  * cancel_delayed_work_sync - reliably kill off a delayed work.
11326e84d644SOleg Nesterov  * @dwork: the delayed work struct
11336e84d644SOleg Nesterov  *
11341f1f642eSOleg Nesterov  * Returns true if @dwork was pending.
11351f1f642eSOleg Nesterov  *
11366e84d644SOleg Nesterov  * It is possible to use this function if @dwork rearms itself via queue_work()
11376e84d644SOleg Nesterov  * or queue_delayed_work(). See also the comment for cancel_work_sync().
11386e84d644SOleg Nesterov  */
11391f1f642eSOleg Nesterov int cancel_delayed_work_sync(struct delayed_work *dwork)
11406e84d644SOleg Nesterov {
11411f1f642eSOleg Nesterov 	return __cancel_work_timer(&dwork->work, &dwork->timer);
11426e84d644SOleg Nesterov }
1143f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync);
11441da177e4SLinus Torvalds 
11456e84d644SOleg Nesterov static struct workqueue_struct *keventd_wq __read_mostly;
11461da177e4SLinus Torvalds 
11470fcb78c2SRolf Eike Beer /**
11480fcb78c2SRolf Eike Beer  * schedule_work - put work task in global workqueue
11490fcb78c2SRolf Eike Beer  * @work: job to be done
11500fcb78c2SRolf Eike Beer  *
11515b0f437dSBart Van Assche  * Returns zero if @work was already on the kernel-global workqueue and
11525b0f437dSBart Van Assche  * non-zero otherwise.
11535b0f437dSBart Van Assche  *
11545b0f437dSBart Van Assche  * This puts a job in the kernel-global workqueue if it was not already
11555b0f437dSBart Van Assche  * queued and leaves it in the same position on the kernel-global
11565b0f437dSBart Van Assche  * workqueue otherwise.
11570fcb78c2SRolf Eike Beer  */
11587ad5b3a5SHarvey Harrison int schedule_work(struct work_struct *work)
11591da177e4SLinus Torvalds {
11601da177e4SLinus Torvalds 	return queue_work(keventd_wq, work);
11611da177e4SLinus Torvalds }
1162ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_work);
11631da177e4SLinus Torvalds 
1164c1a220e7SZhang Rui /*
1165c1a220e7SZhang Rui  * schedule_work_on - put work task on a specific cpu
1166c1a220e7SZhang Rui  * @cpu: cpu to put the work task on
1167c1a220e7SZhang Rui  * @work: job to be done
1168c1a220e7SZhang Rui  *
1169c1a220e7SZhang Rui  * This puts a job on a specific cpu
1170c1a220e7SZhang Rui  */
1171c1a220e7SZhang Rui int schedule_work_on(int cpu, struct work_struct *work)
1172c1a220e7SZhang Rui {
1173c1a220e7SZhang Rui 	return queue_work_on(cpu, keventd_wq, work);
1174c1a220e7SZhang Rui }
1175c1a220e7SZhang Rui EXPORT_SYMBOL(schedule_work_on);
1176c1a220e7SZhang Rui 
11770fcb78c2SRolf Eike Beer /**
11780fcb78c2SRolf Eike Beer  * schedule_delayed_work - put work task in global workqueue after delay
117952bad64dSDavid Howells  * @dwork: job to be done
118052bad64dSDavid Howells  * @delay: number of jiffies to wait or 0 for immediate execution
11810fcb78c2SRolf Eike Beer  *
11820fcb78c2SRolf Eike Beer  * After waiting for a given time this puts a job in the kernel-global
11830fcb78c2SRolf Eike Beer  * workqueue.
11840fcb78c2SRolf Eike Beer  */
11857ad5b3a5SHarvey Harrison int schedule_delayed_work(struct delayed_work *dwork,
118682f67cd9SIngo Molnar 					unsigned long delay)
11871da177e4SLinus Torvalds {
118852bad64dSDavid Howells 	return queue_delayed_work(keventd_wq, dwork, delay);
11891da177e4SLinus Torvalds }
1190ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work);
11911da177e4SLinus Torvalds 
11920fcb78c2SRolf Eike Beer /**
11938c53e463SLinus Torvalds  * flush_delayed_work - block until a dwork_struct's callback has terminated
11948c53e463SLinus Torvalds  * @dwork: the delayed work which is to be flushed
11958c53e463SLinus Torvalds  *
11968c53e463SLinus Torvalds  * Any timeout is cancelled, and any pending work is run immediately.
11978c53e463SLinus Torvalds  */
11988c53e463SLinus Torvalds void flush_delayed_work(struct delayed_work *dwork)
11998c53e463SLinus Torvalds {
12008c53e463SLinus Torvalds 	if (del_timer_sync(&dwork->timer)) {
12014690c4abSTejun Heo 		__queue_work(get_cpu(), get_wq_data(&dwork->work)->wq,
12024690c4abSTejun Heo 			     &dwork->work);
12038c53e463SLinus Torvalds 		put_cpu();
12048c53e463SLinus Torvalds 	}
12058c53e463SLinus Torvalds 	flush_work(&dwork->work);
12068c53e463SLinus Torvalds }
12078c53e463SLinus Torvalds EXPORT_SYMBOL(flush_delayed_work);
12088c53e463SLinus Torvalds 
12098c53e463SLinus Torvalds /**
12100fcb78c2SRolf Eike Beer  * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
12110fcb78c2SRolf Eike Beer  * @cpu: cpu to use
121252bad64dSDavid Howells  * @dwork: job to be done
12130fcb78c2SRolf Eike Beer  * @delay: number of jiffies to wait
12140fcb78c2SRolf Eike Beer  *
12150fcb78c2SRolf Eike Beer  * After waiting for a given time this puts a job in the kernel-global
12160fcb78c2SRolf Eike Beer  * workqueue on the specified CPU.
12170fcb78c2SRolf Eike Beer  */
12181da177e4SLinus Torvalds int schedule_delayed_work_on(int cpu,
121952bad64dSDavid Howells 			struct delayed_work *dwork, unsigned long delay)
12201da177e4SLinus Torvalds {
122152bad64dSDavid Howells 	return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
12221da177e4SLinus Torvalds }
1223ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work_on);
12241da177e4SLinus Torvalds 
1225b6136773SAndrew Morton /**
1226b6136773SAndrew Morton  * schedule_on_each_cpu - call a function on each online CPU from keventd
1227b6136773SAndrew Morton  * @func: the function to call
1228b6136773SAndrew Morton  *
1229b6136773SAndrew Morton  * Returns zero on success.
1230b6136773SAndrew Morton  * Returns -ve errno on failure.
1231b6136773SAndrew Morton  *
1232b6136773SAndrew Morton  * schedule_on_each_cpu() is very slow.
1233b6136773SAndrew Morton  */
123465f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func)
123515316ba8SChristoph Lameter {
123615316ba8SChristoph Lameter 	int cpu;
123765a64464SAndi Kleen 	int orig = -1;
1238b6136773SAndrew Morton 	struct work_struct *works;
123915316ba8SChristoph Lameter 
1240b6136773SAndrew Morton 	works = alloc_percpu(struct work_struct);
1241b6136773SAndrew Morton 	if (!works)
124215316ba8SChristoph Lameter 		return -ENOMEM;
1243b6136773SAndrew Morton 
124495402b38SGautham R Shenoy 	get_online_cpus();
124593981800STejun Heo 
124693981800STejun Heo 	/*
124793981800STejun Heo 	 * When running in keventd don't schedule a work item on
124893981800STejun Heo 	 * itself.  Can just call directly because the work queue is
124993981800STejun Heo 	 * already bound.  This also is faster.
125093981800STejun Heo 	 */
125193981800STejun Heo 	if (current_is_keventd())
125293981800STejun Heo 		orig = raw_smp_processor_id();
125393981800STejun Heo 
125415316ba8SChristoph Lameter 	for_each_online_cpu(cpu) {
12559bfb1839SIngo Molnar 		struct work_struct *work = per_cpu_ptr(works, cpu);
12569bfb1839SIngo Molnar 
12579bfb1839SIngo Molnar 		INIT_WORK(work, func);
125893981800STejun Heo 		if (cpu != orig)
12598de6d308SOleg Nesterov 			schedule_work_on(cpu, work);
126015316ba8SChristoph Lameter 	}
126193981800STejun Heo 	if (orig >= 0)
126293981800STejun Heo 		func(per_cpu_ptr(works, orig));
126393981800STejun Heo 
126493981800STejun Heo 	for_each_online_cpu(cpu)
12658616a89aSOleg Nesterov 		flush_work(per_cpu_ptr(works, cpu));
126693981800STejun Heo 
126795402b38SGautham R Shenoy 	put_online_cpus();
1268b6136773SAndrew Morton 	free_percpu(works);
126915316ba8SChristoph Lameter 	return 0;
127015316ba8SChristoph Lameter }
127115316ba8SChristoph Lameter 
1272eef6a7d5SAlan Stern /**
1273eef6a7d5SAlan Stern  * flush_scheduled_work - ensure that any scheduled work has run to completion.
1274eef6a7d5SAlan Stern  *
1275eef6a7d5SAlan Stern  * Forces execution of the kernel-global workqueue and blocks until its
1276eef6a7d5SAlan Stern  * completion.
1277eef6a7d5SAlan Stern  *
1278eef6a7d5SAlan Stern  * Think twice before calling this function!  It's very easy to get into
1279eef6a7d5SAlan Stern  * trouble if you don't take great care.  Either of the following situations
1280eef6a7d5SAlan Stern  * will lead to deadlock:
1281eef6a7d5SAlan Stern  *
1282eef6a7d5SAlan Stern  *	One of the work items currently on the workqueue needs to acquire
1283eef6a7d5SAlan Stern  *	a lock held by your code or its caller.
1284eef6a7d5SAlan Stern  *
1285eef6a7d5SAlan Stern  *	Your code is running in the context of a work routine.
1286eef6a7d5SAlan Stern  *
1287eef6a7d5SAlan Stern  * They will be detected by lockdep when they occur, but the first might not
1288eef6a7d5SAlan Stern  * occur very often.  It depends on what work items are on the workqueue and
1289eef6a7d5SAlan Stern  * what locks they need, which you have no control over.
1290eef6a7d5SAlan Stern  *
1291eef6a7d5SAlan Stern  * In most situations flushing the entire workqueue is overkill; you merely
1292eef6a7d5SAlan Stern  * need to know that a particular work item isn't queued and isn't running.
1293eef6a7d5SAlan Stern  * In such cases you should use cancel_delayed_work_sync() or
1294eef6a7d5SAlan Stern  * cancel_work_sync() instead.
1295eef6a7d5SAlan Stern  */
12961da177e4SLinus Torvalds void flush_scheduled_work(void)
12971da177e4SLinus Torvalds {
12981da177e4SLinus Torvalds 	flush_workqueue(keventd_wq);
12991da177e4SLinus Torvalds }
1300ae90dd5dSDave Jones EXPORT_SYMBOL(flush_scheduled_work);
13011da177e4SLinus Torvalds 
13021da177e4SLinus Torvalds /**
13031fa44ecaSJames Bottomley  * execute_in_process_context - reliably execute the routine with user context
13041fa44ecaSJames Bottomley  * @fn:		the function to execute
13051fa44ecaSJames Bottomley  * @ew:		guaranteed storage for the execute work structure (must
13061fa44ecaSJames Bottomley  *		be available when the work executes)
13071fa44ecaSJames Bottomley  *
13081fa44ecaSJames Bottomley  * Executes the function immediately if process context is available,
13091fa44ecaSJames Bottomley  * otherwise schedules the function for delayed execution.
13101fa44ecaSJames Bottomley  *
13111fa44ecaSJames Bottomley  * Returns:	0 - function was executed
13121fa44ecaSJames Bottomley  *		1 - function was scheduled for execution
13131fa44ecaSJames Bottomley  */
131465f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew)
13151fa44ecaSJames Bottomley {
13161fa44ecaSJames Bottomley 	if (!in_interrupt()) {
131765f27f38SDavid Howells 		fn(&ew->work);
13181fa44ecaSJames Bottomley 		return 0;
13191fa44ecaSJames Bottomley 	}
13201fa44ecaSJames Bottomley 
132165f27f38SDavid Howells 	INIT_WORK(&ew->work, fn);
13221fa44ecaSJames Bottomley 	schedule_work(&ew->work);
13231fa44ecaSJames Bottomley 
13241fa44ecaSJames Bottomley 	return 1;
13251fa44ecaSJames Bottomley }
13261fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context);
13271fa44ecaSJames Bottomley 
13281da177e4SLinus Torvalds int keventd_up(void)
13291da177e4SLinus Torvalds {
13301da177e4SLinus Torvalds 	return keventd_wq != NULL;
13311da177e4SLinus Torvalds }
13321da177e4SLinus Torvalds 
13331da177e4SLinus Torvalds int current_is_keventd(void)
13341da177e4SLinus Torvalds {
13351da177e4SLinus Torvalds 	struct cpu_workqueue_struct *cwq;
1336d243769dSHugh Dickins 	int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
13371da177e4SLinus Torvalds 	int ret = 0;
13381da177e4SLinus Torvalds 
13391da177e4SLinus Torvalds 	BUG_ON(!keventd_wq);
13401da177e4SLinus Torvalds 
13411537663fSTejun Heo 	cwq = get_cwq(cpu, keventd_wq);
1342*c34056a3STejun Heo 	if (current == cwq->worker->task)
13431da177e4SLinus Torvalds 		ret = 1;
13441da177e4SLinus Torvalds 
13451da177e4SLinus Torvalds 	return ret;
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds }
13481da177e4SLinus Torvalds 
13490f900049STejun Heo static struct cpu_workqueue_struct *alloc_cwqs(void)
13500f900049STejun Heo {
13510f900049STejun Heo 	/*
13520f900049STejun Heo 	 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
13530f900049STejun Heo 	 * Make sure that the alignment isn't lower than that of
13540f900049STejun Heo 	 * unsigned long long.
13550f900049STejun Heo 	 */
13560f900049STejun Heo 	const size_t size = sizeof(struct cpu_workqueue_struct);
13570f900049STejun Heo 	const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
13580f900049STejun Heo 				   __alignof__(unsigned long long));
13590f900049STejun Heo 	struct cpu_workqueue_struct *cwqs;
13600f900049STejun Heo #ifndef CONFIG_SMP
13610f900049STejun Heo 	void *ptr;
13620f900049STejun Heo 
13630f900049STejun Heo 	/*
13640f900049STejun Heo 	 * On UP, percpu allocator doesn't honor alignment parameter
13650f900049STejun Heo 	 * and simply uses arch-dependent default.  Allocate enough
13660f900049STejun Heo 	 * room to align cwq and put an extra pointer at the end
13670f900049STejun Heo 	 * pointing back to the originally allocated pointer which
13680f900049STejun Heo 	 * will be used for free.
13690f900049STejun Heo 	 *
13700f900049STejun Heo 	 * FIXME: This really belongs to UP percpu code.  Update UP
13710f900049STejun Heo 	 * percpu code to honor alignment and remove this ugliness.
13720f900049STejun Heo 	 */
13730f900049STejun Heo 	ptr = __alloc_percpu(size + align + sizeof(void *), 1);
13740f900049STejun Heo 	cwqs = PTR_ALIGN(ptr, align);
13750f900049STejun Heo 	*(void **)per_cpu_ptr(cwqs + 1, 0) = ptr;
13760f900049STejun Heo #else
13770f900049STejun Heo 	/* On SMP, percpu allocator can do it itself */
13780f900049STejun Heo 	cwqs = __alloc_percpu(size, align);
13790f900049STejun Heo #endif
13800f900049STejun Heo 	/* just in case, make sure it's actually aligned */
13810f900049STejun Heo 	BUG_ON(!IS_ALIGNED((unsigned long)cwqs, align));
13820f900049STejun Heo 	return cwqs;
13830f900049STejun Heo }
13840f900049STejun Heo 
13850f900049STejun Heo static void free_cwqs(struct cpu_workqueue_struct *cwqs)
13860f900049STejun Heo {
13870f900049STejun Heo #ifndef CONFIG_SMP
13880f900049STejun Heo 	/* on UP, the pointer to free is stored right after the cwq */
13890f900049STejun Heo 	if (cwqs)
13900f900049STejun Heo 		free_percpu(*(void **)per_cpu_ptr(cwqs + 1, 0));
13910f900049STejun Heo #else
13920f900049STejun Heo 	free_percpu(cwqs);
13930f900049STejun Heo #endif
13940f900049STejun Heo }
13950f900049STejun Heo 
13964e6045f1SJohannes Berg struct workqueue_struct *__create_workqueue_key(const char *name,
139797e37d7bSTejun Heo 						unsigned int flags,
1398eb13ba87SJohannes Berg 						struct lock_class_key *key,
1399eb13ba87SJohannes Berg 						const char *lock_name)
14003af24433SOleg Nesterov {
14011537663fSTejun Heo 	bool singlethread = flags & WQ_SINGLE_THREAD;
14023af24433SOleg Nesterov 	struct workqueue_struct *wq;
1403*c34056a3STejun Heo 	bool failed = false;
1404*c34056a3STejun Heo 	unsigned int cpu;
14053af24433SOleg Nesterov 
14063af24433SOleg Nesterov 	wq = kzalloc(sizeof(*wq), GFP_KERNEL);
14073af24433SOleg Nesterov 	if (!wq)
14084690c4abSTejun Heo 		goto err;
14093af24433SOleg Nesterov 
14100f900049STejun Heo 	wq->cpu_wq = alloc_cwqs();
14114690c4abSTejun Heo 	if (!wq->cpu_wq)
14124690c4abSTejun Heo 		goto err;
14133af24433SOleg Nesterov 
141497e37d7bSTejun Heo 	wq->flags = flags;
141573f53c4aSTejun Heo 	mutex_init(&wq->flush_mutex);
141673f53c4aSTejun Heo 	atomic_set(&wq->nr_cwqs_to_flush, 0);
141773f53c4aSTejun Heo 	INIT_LIST_HEAD(&wq->flusher_queue);
141873f53c4aSTejun Heo 	INIT_LIST_HEAD(&wq->flusher_overflow);
14193af24433SOleg Nesterov 	wq->name = name;
1420eb13ba87SJohannes Berg 	lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
1421cce1a165SOleg Nesterov 	INIT_LIST_HEAD(&wq->list);
14223af24433SOleg Nesterov 
14233da1c84cSOleg Nesterov 	cpu_maps_update_begin();
14246af8bf3dSOleg Nesterov 	/*
14256af8bf3dSOleg Nesterov 	 * We must initialize cwqs for each possible cpu even if we
14266af8bf3dSOleg Nesterov 	 * are going to call destroy_workqueue() finally. Otherwise
14276af8bf3dSOleg Nesterov 	 * cpu_up() can hit the uninitialized cwq once we drop the
14286af8bf3dSOleg Nesterov 	 * lock.
14296af8bf3dSOleg Nesterov 	 */
14303af24433SOleg Nesterov 	for_each_possible_cpu(cpu) {
14311537663fSTejun Heo 		struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
14321537663fSTejun Heo 
14330f900049STejun Heo 		BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
14341537663fSTejun Heo 		cwq->cpu = cpu;
1435*c34056a3STejun Heo 		cwq->wq = wq;
143673f53c4aSTejun Heo 		cwq->flush_color = -1;
14371537663fSTejun Heo 		spin_lock_init(&cwq->lock);
14381537663fSTejun Heo 		INIT_LIST_HEAD(&cwq->worklist);
14391537663fSTejun Heo 		init_waitqueue_head(&cwq->more_work);
14401537663fSTejun Heo 
1441*c34056a3STejun Heo 		if (failed)
14423af24433SOleg Nesterov 			continue;
1443*c34056a3STejun Heo 		cwq->worker = create_worker(cwq,
1444*c34056a3STejun Heo 					    cpu_online(cpu) && !singlethread);
1445*c34056a3STejun Heo 		if (cwq->worker)
1446*c34056a3STejun Heo 			start_worker(cwq->worker);
14471537663fSTejun Heo 		else
1448*c34056a3STejun Heo 			failed = true;
14493af24433SOleg Nesterov 	}
14501537663fSTejun Heo 
14511537663fSTejun Heo 	spin_lock(&workqueue_lock);
14521537663fSTejun Heo 	list_add(&wq->list, &workqueues);
14531537663fSTejun Heo 	spin_unlock(&workqueue_lock);
14541537663fSTejun Heo 
14553da1c84cSOleg Nesterov 	cpu_maps_update_done();
14563af24433SOleg Nesterov 
1457*c34056a3STejun Heo 	if (failed) {
14583af24433SOleg Nesterov 		destroy_workqueue(wq);
14593af24433SOleg Nesterov 		wq = NULL;
14603af24433SOleg Nesterov 	}
14613af24433SOleg Nesterov 	return wq;
14624690c4abSTejun Heo err:
14634690c4abSTejun Heo 	if (wq) {
14640f900049STejun Heo 		free_cwqs(wq->cpu_wq);
14654690c4abSTejun Heo 		kfree(wq);
14664690c4abSTejun Heo 	}
14674690c4abSTejun Heo 	return NULL;
14683af24433SOleg Nesterov }
14694e6045f1SJohannes Berg EXPORT_SYMBOL_GPL(__create_workqueue_key);
14703af24433SOleg Nesterov 
14713af24433SOleg Nesterov /**
14723af24433SOleg Nesterov  * destroy_workqueue - safely terminate a workqueue
14733af24433SOleg Nesterov  * @wq: target workqueue
14743af24433SOleg Nesterov  *
14753af24433SOleg Nesterov  * Safely destroy a workqueue. All work currently pending will be done first.
14763af24433SOleg Nesterov  */
14773af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq)
14783af24433SOleg Nesterov {
14793af24433SOleg Nesterov 	int cpu;
14803af24433SOleg Nesterov 
14813da1c84cSOleg Nesterov 	cpu_maps_update_begin();
148295402b38SGautham R Shenoy 	spin_lock(&workqueue_lock);
14833af24433SOleg Nesterov 	list_del(&wq->list);
148495402b38SGautham R Shenoy 	spin_unlock(&workqueue_lock);
14853da1c84cSOleg Nesterov 	cpu_maps_update_done();
14863af24433SOleg Nesterov 
148773f53c4aSTejun Heo 	flush_workqueue(wq);
148873f53c4aSTejun Heo 
148973f53c4aSTejun Heo 	for_each_possible_cpu(cpu) {
149073f53c4aSTejun Heo 		struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
149173f53c4aSTejun Heo 		int i;
149273f53c4aSTejun Heo 
1493*c34056a3STejun Heo 		if (cwq->worker) {
1494*c34056a3STejun Heo 			destroy_worker(cwq->worker);
1495*c34056a3STejun Heo 			cwq->worker = NULL;
149673f53c4aSTejun Heo 		}
149773f53c4aSTejun Heo 
149873f53c4aSTejun Heo 		for (i = 0; i < WORK_NR_COLORS; i++)
149973f53c4aSTejun Heo 			BUG_ON(cwq->nr_in_flight[i]);
150073f53c4aSTejun Heo 	}
15011537663fSTejun Heo 
15020f900049STejun Heo 	free_cwqs(wq->cpu_wq);
15033af24433SOleg Nesterov 	kfree(wq);
15043af24433SOleg Nesterov }
15053af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue);
15063af24433SOleg Nesterov 
15079c7b216dSChandra Seetharaman static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
15081da177e4SLinus Torvalds 						unsigned long action,
15091da177e4SLinus Torvalds 						void *hcpu)
15101da177e4SLinus Torvalds {
15113af24433SOleg Nesterov 	unsigned int cpu = (unsigned long)hcpu;
15123af24433SOleg Nesterov 	struct cpu_workqueue_struct *cwq;
15131da177e4SLinus Torvalds 	struct workqueue_struct *wq;
15141da177e4SLinus Torvalds 
15158bb78442SRafael J. Wysocki 	action &= ~CPU_TASKS_FROZEN;
15168bb78442SRafael J. Wysocki 
15171da177e4SLinus Torvalds 	list_for_each_entry(wq, &workqueues, list) {
15181537663fSTejun Heo 		if (wq->flags & WQ_SINGLE_THREAD)
15191537663fSTejun Heo 			continue;
15201537663fSTejun Heo 
15211537663fSTejun Heo 		cwq = get_cwq(cpu, wq);
15223af24433SOleg Nesterov 
15233af24433SOleg Nesterov 		switch (action) {
15243da1c84cSOleg Nesterov 		case CPU_POST_DEAD:
152573f53c4aSTejun Heo 			flush_workqueue(wq);
15261da177e4SLinus Torvalds 			break;
15271da177e4SLinus Torvalds 		}
15283af24433SOleg Nesterov 	}
15291da177e4SLinus Torvalds 
15301537663fSTejun Heo 	return notifier_from_errno(0);
15311da177e4SLinus Torvalds }
15321da177e4SLinus Torvalds 
15332d3854a3SRusty Russell #ifdef CONFIG_SMP
15348ccad40dSRusty Russell 
15352d3854a3SRusty Russell struct work_for_cpu {
15366b44003eSAndrew Morton 	struct completion completion;
15372d3854a3SRusty Russell 	long (*fn)(void *);
15382d3854a3SRusty Russell 	void *arg;
15392d3854a3SRusty Russell 	long ret;
15402d3854a3SRusty Russell };
15412d3854a3SRusty Russell 
15426b44003eSAndrew Morton static int do_work_for_cpu(void *_wfc)
15432d3854a3SRusty Russell {
15446b44003eSAndrew Morton 	struct work_for_cpu *wfc = _wfc;
15452d3854a3SRusty Russell 	wfc->ret = wfc->fn(wfc->arg);
15466b44003eSAndrew Morton 	complete(&wfc->completion);
15476b44003eSAndrew Morton 	return 0;
15482d3854a3SRusty Russell }
15492d3854a3SRusty Russell 
15502d3854a3SRusty Russell /**
15512d3854a3SRusty Russell  * work_on_cpu - run a function in user context on a particular cpu
15522d3854a3SRusty Russell  * @cpu: the cpu to run on
15532d3854a3SRusty Russell  * @fn: the function to run
15542d3854a3SRusty Russell  * @arg: the function arg
15552d3854a3SRusty Russell  *
155631ad9081SRusty Russell  * This will return the value @fn returns.
155731ad9081SRusty Russell  * It is up to the caller to ensure that the cpu doesn't go offline.
15586b44003eSAndrew Morton  * The caller must not hold any locks which would prevent @fn from completing.
15592d3854a3SRusty Russell  */
15602d3854a3SRusty Russell long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
15612d3854a3SRusty Russell {
15626b44003eSAndrew Morton 	struct task_struct *sub_thread;
15636b44003eSAndrew Morton 	struct work_for_cpu wfc = {
15646b44003eSAndrew Morton 		.completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
15656b44003eSAndrew Morton 		.fn = fn,
15666b44003eSAndrew Morton 		.arg = arg,
15676b44003eSAndrew Morton 	};
15682d3854a3SRusty Russell 
15696b44003eSAndrew Morton 	sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
15706b44003eSAndrew Morton 	if (IS_ERR(sub_thread))
15716b44003eSAndrew Morton 		return PTR_ERR(sub_thread);
15726b44003eSAndrew Morton 	kthread_bind(sub_thread, cpu);
15736b44003eSAndrew Morton 	wake_up_process(sub_thread);
15746b44003eSAndrew Morton 	wait_for_completion(&wfc.completion);
15752d3854a3SRusty Russell 	return wfc.ret;
15762d3854a3SRusty Russell }
15772d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu);
15782d3854a3SRusty Russell #endif /* CONFIG_SMP */
15792d3854a3SRusty Russell 
1580c12920d1SOleg Nesterov void __init init_workqueues(void)
15811da177e4SLinus Torvalds {
1582*c34056a3STejun Heo 	unsigned int cpu;
1583*c34056a3STejun Heo 
1584*c34056a3STejun Heo 	for_each_possible_cpu(cpu)
1585*c34056a3STejun Heo 		ida_init(&per_cpu(worker_ida, cpu));
1586*c34056a3STejun Heo 
1587e7577c50SRusty Russell 	singlethread_cpu = cpumask_first(cpu_possible_mask);
15881da177e4SLinus Torvalds 	hotcpu_notifier(workqueue_cpu_callback, 0);
15891da177e4SLinus Torvalds 	keventd_wq = create_workqueue("events");
15901da177e4SLinus Torvalds 	BUG_ON(!keventd_wq);
15911da177e4SLinus Torvalds }
1592