xref: /linux-6.15/kernel/workqueue.c (revision 4d707b9f)
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>
36fb39125fSZhaolei #define CREATE_TRACE_POINTS
37fb39125fSZhaolei #include <trace/events/workqueue.h>
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds /*
40f756d5e2SNathan Lynch  * The per-CPU workqueue (if single thread, we always use the first
41f756d5e2SNathan Lynch  * possible cpu).
421da177e4SLinus Torvalds  */
431da177e4SLinus Torvalds struct cpu_workqueue_struct {
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds 	spinlock_t lock;
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds 	struct list_head worklist;
481da177e4SLinus Torvalds 	wait_queue_head_t more_work;
493af24433SOleg Nesterov 	struct work_struct *current_work;
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds 	struct workqueue_struct *wq;
5236c8b586SIngo Molnar 	struct task_struct *thread;
531da177e4SLinus Torvalds } ____cacheline_aligned;
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds /*
561da177e4SLinus Torvalds  * The externally visible workqueue abstraction is an array of
571da177e4SLinus Torvalds  * per-CPU workqueues:
581da177e4SLinus Torvalds  */
591da177e4SLinus Torvalds struct workqueue_struct {
6089ada679SChristoph Lameter 	struct cpu_workqueue_struct *cpu_wq;
61cce1a165SOleg Nesterov 	struct list_head list;
621da177e4SLinus Torvalds 	const char *name;
63cce1a165SOleg Nesterov 	int singlethread;
64319c2a98SOleg Nesterov 	int freezeable;		/* Freeze threads during suspend */
650d557dc9SHeiko Carstens 	int rt;
664e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
674e6045f1SJohannes Berg 	struct lockdep_map lockdep_map;
684e6045f1SJohannes Berg #endif
691da177e4SLinus Torvalds };
701da177e4SLinus Torvalds 
71dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK
72dc186ad7SThomas Gleixner 
73dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr;
74dc186ad7SThomas Gleixner 
75dc186ad7SThomas Gleixner /*
76dc186ad7SThomas Gleixner  * fixup_init is called when:
77dc186ad7SThomas Gleixner  * - an active object is initialized
78dc186ad7SThomas Gleixner  */
79dc186ad7SThomas Gleixner static int work_fixup_init(void *addr, enum debug_obj_state state)
80dc186ad7SThomas Gleixner {
81dc186ad7SThomas Gleixner 	struct work_struct *work = addr;
82dc186ad7SThomas Gleixner 
83dc186ad7SThomas Gleixner 	switch (state) {
84dc186ad7SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
85dc186ad7SThomas Gleixner 		cancel_work_sync(work);
86dc186ad7SThomas Gleixner 		debug_object_init(work, &work_debug_descr);
87dc186ad7SThomas Gleixner 		return 1;
88dc186ad7SThomas Gleixner 	default:
89dc186ad7SThomas Gleixner 		return 0;
90dc186ad7SThomas Gleixner 	}
91dc186ad7SThomas Gleixner }
92dc186ad7SThomas Gleixner 
93dc186ad7SThomas Gleixner /*
94dc186ad7SThomas Gleixner  * fixup_activate is called when:
95dc186ad7SThomas Gleixner  * - an active object is activated
96dc186ad7SThomas Gleixner  * - an unknown object is activated (might be a statically initialized object)
97dc186ad7SThomas Gleixner  */
98dc186ad7SThomas Gleixner static int work_fixup_activate(void *addr, enum debug_obj_state state)
99dc186ad7SThomas Gleixner {
100dc186ad7SThomas Gleixner 	struct work_struct *work = addr;
101dc186ad7SThomas Gleixner 
102dc186ad7SThomas Gleixner 	switch (state) {
103dc186ad7SThomas Gleixner 
104dc186ad7SThomas Gleixner 	case ODEBUG_STATE_NOTAVAILABLE:
105dc186ad7SThomas Gleixner 		/*
106dc186ad7SThomas Gleixner 		 * This is not really a fixup. The work struct was
107dc186ad7SThomas Gleixner 		 * statically initialized. We just make sure that it
108dc186ad7SThomas Gleixner 		 * is tracked in the object tracker.
109dc186ad7SThomas Gleixner 		 */
110dc186ad7SThomas Gleixner 		if (test_bit(WORK_STRUCT_STATIC, work_data_bits(work))) {
111dc186ad7SThomas Gleixner 			debug_object_init(work, &work_debug_descr);
112dc186ad7SThomas Gleixner 			debug_object_activate(work, &work_debug_descr);
113dc186ad7SThomas Gleixner 			return 0;
114dc186ad7SThomas Gleixner 		}
115dc186ad7SThomas Gleixner 		WARN_ON_ONCE(1);
116dc186ad7SThomas Gleixner 		return 0;
117dc186ad7SThomas Gleixner 
118dc186ad7SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
119dc186ad7SThomas Gleixner 		WARN_ON(1);
120dc186ad7SThomas Gleixner 
121dc186ad7SThomas Gleixner 	default:
122dc186ad7SThomas Gleixner 		return 0;
123dc186ad7SThomas Gleixner 	}
124dc186ad7SThomas Gleixner }
125dc186ad7SThomas Gleixner 
126dc186ad7SThomas Gleixner /*
127dc186ad7SThomas Gleixner  * fixup_free is called when:
128dc186ad7SThomas Gleixner  * - an active object is freed
129dc186ad7SThomas Gleixner  */
130dc186ad7SThomas Gleixner static int work_fixup_free(void *addr, enum debug_obj_state state)
131dc186ad7SThomas Gleixner {
132dc186ad7SThomas Gleixner 	struct work_struct *work = addr;
133dc186ad7SThomas Gleixner 
134dc186ad7SThomas Gleixner 	switch (state) {
135dc186ad7SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
136dc186ad7SThomas Gleixner 		cancel_work_sync(work);
137dc186ad7SThomas Gleixner 		debug_object_free(work, &work_debug_descr);
138dc186ad7SThomas Gleixner 		return 1;
139dc186ad7SThomas Gleixner 	default:
140dc186ad7SThomas Gleixner 		return 0;
141dc186ad7SThomas Gleixner 	}
142dc186ad7SThomas Gleixner }
143dc186ad7SThomas Gleixner 
144dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr = {
145dc186ad7SThomas Gleixner 	.name		= "work_struct",
146dc186ad7SThomas Gleixner 	.fixup_init	= work_fixup_init,
147dc186ad7SThomas Gleixner 	.fixup_activate	= work_fixup_activate,
148dc186ad7SThomas Gleixner 	.fixup_free	= work_fixup_free,
149dc186ad7SThomas Gleixner };
150dc186ad7SThomas Gleixner 
151dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work)
152dc186ad7SThomas Gleixner {
153dc186ad7SThomas Gleixner 	debug_object_activate(work, &work_debug_descr);
154dc186ad7SThomas Gleixner }
155dc186ad7SThomas Gleixner 
156dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work)
157dc186ad7SThomas Gleixner {
158dc186ad7SThomas Gleixner 	debug_object_deactivate(work, &work_debug_descr);
159dc186ad7SThomas Gleixner }
160dc186ad7SThomas Gleixner 
161dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack)
162dc186ad7SThomas Gleixner {
163dc186ad7SThomas Gleixner 	if (onstack)
164dc186ad7SThomas Gleixner 		debug_object_init_on_stack(work, &work_debug_descr);
165dc186ad7SThomas Gleixner 	else
166dc186ad7SThomas Gleixner 		debug_object_init(work, &work_debug_descr);
167dc186ad7SThomas Gleixner }
168dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work);
169dc186ad7SThomas Gleixner 
170dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work)
171dc186ad7SThomas Gleixner {
172dc186ad7SThomas Gleixner 	debug_object_free(work, &work_debug_descr);
173dc186ad7SThomas Gleixner }
174dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack);
175dc186ad7SThomas Gleixner 
176dc186ad7SThomas Gleixner #else
177dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { }
178dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { }
179dc186ad7SThomas Gleixner #endif
180dc186ad7SThomas Gleixner 
18195402b38SGautham R Shenoy /* Serializes the accesses to the list of workqueues. */
18295402b38SGautham R Shenoy static DEFINE_SPINLOCK(workqueue_lock);
1831da177e4SLinus Torvalds static LIST_HEAD(workqueues);
1841da177e4SLinus Torvalds 
1853af24433SOleg Nesterov static int singlethread_cpu __read_mostly;
186e7577c50SRusty Russell static const struct cpumask *cpu_singlethread_map __read_mostly;
18714441960SOleg Nesterov /*
18814441960SOleg Nesterov  * _cpu_down() first removes CPU from cpu_online_map, then CPU_DEAD
18914441960SOleg Nesterov  * flushes cwq->worklist. This means that flush_workqueue/wait_on_work
19014441960SOleg Nesterov  * which comes in between can't use for_each_online_cpu(). We could
19114441960SOleg Nesterov  * use cpu_possible_map, the cpumask below is more a documentation
19214441960SOleg Nesterov  * than optimization.
19314441960SOleg Nesterov  */
194e7577c50SRusty Russell static cpumask_var_t cpu_populated_map __read_mostly;
195f756d5e2SNathan Lynch 
1961da177e4SLinus Torvalds /* If it's single threaded, it isn't in the list of workqueues. */
1976cc88bc4SDavid Howells static inline int is_wq_single_threaded(struct workqueue_struct *wq)
1981da177e4SLinus Torvalds {
199cce1a165SOleg Nesterov 	return wq->singlethread;
2001da177e4SLinus Torvalds }
2011da177e4SLinus Torvalds 
202e7577c50SRusty Russell static const struct cpumask *wq_cpu_map(struct workqueue_struct *wq)
203b1f4ec17SOleg Nesterov {
2046cc88bc4SDavid Howells 	return is_wq_single_threaded(wq)
205e7577c50SRusty Russell 		? cpu_singlethread_map : cpu_populated_map;
206b1f4ec17SOleg Nesterov }
207b1f4ec17SOleg Nesterov 
208a848e3b6SOleg Nesterov static
209a848e3b6SOleg Nesterov struct cpu_workqueue_struct *wq_per_cpu(struct workqueue_struct *wq, int cpu)
210a848e3b6SOleg Nesterov {
2116cc88bc4SDavid Howells 	if (unlikely(is_wq_single_threaded(wq)))
212a848e3b6SOleg Nesterov 		cpu = singlethread_cpu;
213a848e3b6SOleg Nesterov 	return per_cpu_ptr(wq->cpu_wq, cpu);
214a848e3b6SOleg Nesterov }
215a848e3b6SOleg Nesterov 
2164594bf15SDavid Howells /*
2174594bf15SDavid Howells  * Set the workqueue on which a work item is to be run
2184594bf15SDavid Howells  * - Must *only* be called if the pending flag is set
2194594bf15SDavid Howells  */
220ed7c0feeSOleg Nesterov static inline void set_wq_data(struct work_struct *work,
221ed7c0feeSOleg Nesterov 				struct cpu_workqueue_struct *cwq)
222365970a1SDavid Howells {
2234594bf15SDavid Howells 	unsigned long new;
224365970a1SDavid Howells 
2254594bf15SDavid Howells 	BUG_ON(!work_pending(work));
2264594bf15SDavid Howells 
227ed7c0feeSOleg Nesterov 	new = (unsigned long) cwq | (1UL << WORK_STRUCT_PENDING);
228a08727baSLinus Torvalds 	new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work);
229a08727baSLinus Torvalds 	atomic_long_set(&work->data, new);
230365970a1SDavid Howells }
231365970a1SDavid Howells 
232*4d707b9fSOleg Nesterov /*
233*4d707b9fSOleg Nesterov  * Clear WORK_STRUCT_PENDING and the workqueue on which it was queued.
234*4d707b9fSOleg Nesterov  */
235*4d707b9fSOleg Nesterov static inline void clear_wq_data(struct work_struct *work)
236*4d707b9fSOleg Nesterov {
237*4d707b9fSOleg Nesterov 	unsigned long flags = *work_data_bits(work) &
238*4d707b9fSOleg Nesterov 				(1UL << WORK_STRUCT_STATIC);
239*4d707b9fSOleg Nesterov 	atomic_long_set(&work->data, flags);
240*4d707b9fSOleg Nesterov }
241*4d707b9fSOleg Nesterov 
242ed7c0feeSOleg Nesterov static inline
243ed7c0feeSOleg Nesterov struct cpu_workqueue_struct *get_wq_data(struct work_struct *work)
244365970a1SDavid Howells {
245a08727baSLinus Torvalds 	return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK);
246365970a1SDavid Howells }
247365970a1SDavid Howells 
248b89deed3SOleg Nesterov static void insert_work(struct cpu_workqueue_struct *cwq,
2491a4d9b0aSOleg Nesterov 			struct work_struct *work, struct list_head *head)
250b89deed3SOleg Nesterov {
251e1d8aa9fSFrederic Weisbecker 	trace_workqueue_insertion(cwq->thread, work);
252e1d8aa9fSFrederic Weisbecker 
253b89deed3SOleg Nesterov 	set_wq_data(work, cwq);
2546e84d644SOleg Nesterov 	/*
2556e84d644SOleg Nesterov 	 * Ensure that we get the right work->data if we see the
2566e84d644SOleg Nesterov 	 * result of list_add() below, see try_to_grab_pending().
2576e84d644SOleg Nesterov 	 */
2586e84d644SOleg Nesterov 	smp_wmb();
2591a4d9b0aSOleg Nesterov 	list_add_tail(&work->entry, head);
260b89deed3SOleg Nesterov 	wake_up(&cwq->more_work);
261b89deed3SOleg Nesterov }
262b89deed3SOleg Nesterov 
2631da177e4SLinus Torvalds static void __queue_work(struct cpu_workqueue_struct *cwq,
2641da177e4SLinus Torvalds 			 struct work_struct *work)
2651da177e4SLinus Torvalds {
2661da177e4SLinus Torvalds 	unsigned long flags;
2671da177e4SLinus Torvalds 
268dc186ad7SThomas Gleixner 	debug_work_activate(work);
2691da177e4SLinus Torvalds 	spin_lock_irqsave(&cwq->lock, flags);
2701a4d9b0aSOleg Nesterov 	insert_work(cwq, work, &cwq->worklist);
2711da177e4SLinus Torvalds 	spin_unlock_irqrestore(&cwq->lock, flags);
2721da177e4SLinus Torvalds }
2731da177e4SLinus Torvalds 
2740fcb78c2SRolf Eike Beer /**
2750fcb78c2SRolf Eike Beer  * queue_work - queue work on a workqueue
2760fcb78c2SRolf Eike Beer  * @wq: workqueue to use
2770fcb78c2SRolf Eike Beer  * @work: work to queue
2780fcb78c2SRolf Eike Beer  *
279057647fcSAlan Stern  * Returns 0 if @work was already on a queue, non-zero otherwise.
2801da177e4SLinus Torvalds  *
28100dfcaf7SOleg Nesterov  * We queue the work to the CPU on which it was submitted, but if the CPU dies
28200dfcaf7SOleg Nesterov  * it can be processed by another CPU.
2831da177e4SLinus Torvalds  */
2847ad5b3a5SHarvey Harrison int queue_work(struct workqueue_struct *wq, struct work_struct *work)
2851da177e4SLinus Torvalds {
286ef1ca236SOleg Nesterov 	int ret;
2871da177e4SLinus Torvalds 
288ef1ca236SOleg Nesterov 	ret = queue_work_on(get_cpu(), wq, work);
289a848e3b6SOleg Nesterov 	put_cpu();
290ef1ca236SOleg Nesterov 
2911da177e4SLinus Torvalds 	return ret;
2921da177e4SLinus Torvalds }
293ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_work);
2941da177e4SLinus Torvalds 
295c1a220e7SZhang Rui /**
296c1a220e7SZhang Rui  * queue_work_on - queue work on specific cpu
297c1a220e7SZhang Rui  * @cpu: CPU number to execute work on
298c1a220e7SZhang Rui  * @wq: workqueue to use
299c1a220e7SZhang Rui  * @work: work to queue
300c1a220e7SZhang Rui  *
301c1a220e7SZhang Rui  * Returns 0 if @work was already on a queue, non-zero otherwise.
302c1a220e7SZhang Rui  *
303c1a220e7SZhang Rui  * We queue the work to a specific CPU, the caller must ensure it
304c1a220e7SZhang Rui  * can't go away.
305c1a220e7SZhang Rui  */
306c1a220e7SZhang Rui int
307c1a220e7SZhang Rui queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
308c1a220e7SZhang Rui {
309c1a220e7SZhang Rui 	int ret = 0;
310c1a220e7SZhang Rui 
311c1a220e7SZhang Rui 	if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
312c1a220e7SZhang Rui 		BUG_ON(!list_empty(&work->entry));
313c1a220e7SZhang Rui 		__queue_work(wq_per_cpu(wq, cpu), work);
314c1a220e7SZhang Rui 		ret = 1;
315c1a220e7SZhang Rui 	}
316c1a220e7SZhang Rui 	return ret;
317c1a220e7SZhang Rui }
318c1a220e7SZhang Rui EXPORT_SYMBOL_GPL(queue_work_on);
319c1a220e7SZhang Rui 
3206d141c3fSLi Zefan static void delayed_work_timer_fn(unsigned long __data)
3211da177e4SLinus Torvalds {
32252bad64dSDavid Howells 	struct delayed_work *dwork = (struct delayed_work *)__data;
323ed7c0feeSOleg Nesterov 	struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
324ed7c0feeSOleg Nesterov 	struct workqueue_struct *wq = cwq->wq;
3251da177e4SLinus Torvalds 
326a848e3b6SOleg Nesterov 	__queue_work(wq_per_cpu(wq, smp_processor_id()), &dwork->work);
3271da177e4SLinus Torvalds }
3281da177e4SLinus Torvalds 
3290fcb78c2SRolf Eike Beer /**
3300fcb78c2SRolf Eike Beer  * queue_delayed_work - queue work on a workqueue after delay
3310fcb78c2SRolf Eike Beer  * @wq: workqueue to use
332af9997e4SRandy Dunlap  * @dwork: delayable work to queue
3330fcb78c2SRolf Eike Beer  * @delay: number of jiffies to wait before queueing
3340fcb78c2SRolf Eike Beer  *
335057647fcSAlan Stern  * Returns 0 if @work was already on a queue, non-zero otherwise.
3360fcb78c2SRolf Eike Beer  */
3377ad5b3a5SHarvey Harrison int queue_delayed_work(struct workqueue_struct *wq,
33852bad64dSDavid Howells 			struct delayed_work *dwork, unsigned long delay)
3391da177e4SLinus Torvalds {
34052bad64dSDavid Howells 	if (delay == 0)
34163bc0362SOleg Nesterov 		return queue_work(wq, &dwork->work);
3421da177e4SLinus Torvalds 
34363bc0362SOleg Nesterov 	return queue_delayed_work_on(-1, wq, dwork, delay);
3441da177e4SLinus Torvalds }
345ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work);
3461da177e4SLinus Torvalds 
3470fcb78c2SRolf Eike Beer /**
3480fcb78c2SRolf Eike Beer  * queue_delayed_work_on - queue work on specific CPU after delay
3490fcb78c2SRolf Eike Beer  * @cpu: CPU number to execute work on
3500fcb78c2SRolf Eike Beer  * @wq: workqueue to use
351af9997e4SRandy Dunlap  * @dwork: work to queue
3520fcb78c2SRolf Eike Beer  * @delay: number of jiffies to wait before queueing
3530fcb78c2SRolf Eike Beer  *
354057647fcSAlan Stern  * Returns 0 if @work was already on a queue, non-zero otherwise.
3550fcb78c2SRolf Eike Beer  */
3567a6bc1cdSVenkatesh Pallipadi int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
35752bad64dSDavid Howells 			struct delayed_work *dwork, unsigned long delay)
3587a6bc1cdSVenkatesh Pallipadi {
3597a6bc1cdSVenkatesh Pallipadi 	int ret = 0;
36052bad64dSDavid Howells 	struct timer_list *timer = &dwork->timer;
36152bad64dSDavid Howells 	struct work_struct *work = &dwork->work;
3627a6bc1cdSVenkatesh Pallipadi 
363a08727baSLinus Torvalds 	if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
3647a6bc1cdSVenkatesh Pallipadi 		BUG_ON(timer_pending(timer));
3657a6bc1cdSVenkatesh Pallipadi 		BUG_ON(!list_empty(&work->entry));
3667a6bc1cdSVenkatesh Pallipadi 
3678a3e77ccSAndrew Liu 		timer_stats_timer_set_start_info(&dwork->timer);
3688a3e77ccSAndrew Liu 
369ed7c0feeSOleg Nesterov 		/* This stores cwq for the moment, for the timer_fn */
370a848e3b6SOleg Nesterov 		set_wq_data(work, wq_per_cpu(wq, raw_smp_processor_id()));
3717a6bc1cdSVenkatesh Pallipadi 		timer->expires = jiffies + delay;
37252bad64dSDavid Howells 		timer->data = (unsigned long)dwork;
3737a6bc1cdSVenkatesh Pallipadi 		timer->function = delayed_work_timer_fn;
37463bc0362SOleg Nesterov 
37563bc0362SOleg Nesterov 		if (unlikely(cpu >= 0))
3767a6bc1cdSVenkatesh Pallipadi 			add_timer_on(timer, cpu);
37763bc0362SOleg Nesterov 		else
37863bc0362SOleg Nesterov 			add_timer(timer);
3797a6bc1cdSVenkatesh Pallipadi 		ret = 1;
3807a6bc1cdSVenkatesh Pallipadi 	}
3817a6bc1cdSVenkatesh Pallipadi 	return ret;
3827a6bc1cdSVenkatesh Pallipadi }
383ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work_on);
3841da177e4SLinus Torvalds 
385858119e1SArjan van de Ven static void run_workqueue(struct cpu_workqueue_struct *cwq)
3861da177e4SLinus Torvalds {
387f293ea92SOleg Nesterov 	spin_lock_irq(&cwq->lock);
3881da177e4SLinus Torvalds 	while (!list_empty(&cwq->worklist)) {
3891da177e4SLinus Torvalds 		struct work_struct *work = list_entry(cwq->worklist.next,
3901da177e4SLinus Torvalds 						struct work_struct, entry);
3916bb49e59SDavid Howells 		work_func_t f = work->func;
3924e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
3934e6045f1SJohannes Berg 		/*
3944e6045f1SJohannes Berg 		 * It is permissible to free the struct work_struct
3954e6045f1SJohannes Berg 		 * from inside the function that is called from it,
3964e6045f1SJohannes Berg 		 * this we need to take into account for lockdep too.
3974e6045f1SJohannes Berg 		 * To avoid bogus "held lock freed" warnings as well
3984e6045f1SJohannes Berg 		 * as problems when looking into work->lockdep_map,
3994e6045f1SJohannes Berg 		 * make a copy and use that here.
4004e6045f1SJohannes Berg 		 */
4014e6045f1SJohannes Berg 		struct lockdep_map lockdep_map = work->lockdep_map;
4024e6045f1SJohannes Berg #endif
403e1d8aa9fSFrederic Weisbecker 		trace_workqueue_execution(cwq->thread, work);
404dc186ad7SThomas Gleixner 		debug_work_deactivate(work);
405b89deed3SOleg Nesterov 		cwq->current_work = work;
4061da177e4SLinus Torvalds 		list_del_init(cwq->worklist.next);
407f293ea92SOleg Nesterov 		spin_unlock_irq(&cwq->lock);
4081da177e4SLinus Torvalds 
409365970a1SDavid Howells 		BUG_ON(get_wq_data(work) != cwq);
41023b2e599SOleg Nesterov 		work_clear_pending(work);
4113295f0efSIngo Molnar 		lock_map_acquire(&cwq->wq->lockdep_map);
4123295f0efSIngo Molnar 		lock_map_acquire(&lockdep_map);
41365f27f38SDavid Howells 		f(work);
4143295f0efSIngo Molnar 		lock_map_release(&lockdep_map);
4153295f0efSIngo Molnar 		lock_map_release(&cwq->wq->lockdep_map);
4161da177e4SLinus Torvalds 
417d5abe669SPeter Zijlstra 		if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
418d5abe669SPeter Zijlstra 			printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
419d5abe669SPeter Zijlstra 					"%s/0x%08x/%d\n",
420d5abe669SPeter Zijlstra 					current->comm, preempt_count(),
421ba25f9dcSPavel Emelyanov 				       	task_pid_nr(current));
422d5abe669SPeter Zijlstra 			printk(KERN_ERR "    last function: ");
423d5abe669SPeter Zijlstra 			print_symbol("%s\n", (unsigned long)f);
424d5abe669SPeter Zijlstra 			debug_show_held_locks(current);
425d5abe669SPeter Zijlstra 			dump_stack();
426d5abe669SPeter Zijlstra 		}
427d5abe669SPeter Zijlstra 
428f293ea92SOleg Nesterov 		spin_lock_irq(&cwq->lock);
429b89deed3SOleg Nesterov 		cwq->current_work = NULL;
4301da177e4SLinus Torvalds 	}
431f293ea92SOleg Nesterov 	spin_unlock_irq(&cwq->lock);
4321da177e4SLinus Torvalds }
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds static int worker_thread(void *__cwq)
4351da177e4SLinus Torvalds {
4361da177e4SLinus Torvalds 	struct cpu_workqueue_struct *cwq = __cwq;
4373af24433SOleg Nesterov 	DEFINE_WAIT(wait);
4381da177e4SLinus Torvalds 
43983144186SRafael J. Wysocki 	if (cwq->wq->freezeable)
44083144186SRafael J. Wysocki 		set_freezable();
4411da177e4SLinus Torvalds 
4423af24433SOleg Nesterov 	for (;;) {
4433af24433SOleg Nesterov 		prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
44414441960SOleg Nesterov 		if (!freezing(current) &&
44514441960SOleg Nesterov 		    !kthread_should_stop() &&
44614441960SOleg Nesterov 		    list_empty(&cwq->worklist))
4471da177e4SLinus Torvalds 			schedule();
4483af24433SOleg Nesterov 		finish_wait(&cwq->more_work, &wait);
4491da177e4SLinus Torvalds 
45085f4186aSOleg Nesterov 		try_to_freeze();
45185f4186aSOleg Nesterov 
45214441960SOleg Nesterov 		if (kthread_should_stop())
4533af24433SOleg Nesterov 			break;
4543af24433SOleg Nesterov 
4551da177e4SLinus Torvalds 		run_workqueue(cwq);
4561da177e4SLinus Torvalds 	}
4573af24433SOleg Nesterov 
4581da177e4SLinus Torvalds 	return 0;
4591da177e4SLinus Torvalds }
4601da177e4SLinus Torvalds 
461fc2e4d70SOleg Nesterov struct wq_barrier {
462fc2e4d70SOleg Nesterov 	struct work_struct	work;
463fc2e4d70SOleg Nesterov 	struct completion	done;
464fc2e4d70SOleg Nesterov };
465fc2e4d70SOleg Nesterov 
466fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work)
467fc2e4d70SOleg Nesterov {
468fc2e4d70SOleg Nesterov 	struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
469fc2e4d70SOleg Nesterov 	complete(&barr->done);
470fc2e4d70SOleg Nesterov }
471fc2e4d70SOleg Nesterov 
47283c22520SOleg Nesterov static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
4731a4d9b0aSOleg Nesterov 			struct wq_barrier *barr, struct list_head *head)
474fc2e4d70SOleg Nesterov {
475dc186ad7SThomas Gleixner 	/*
476dc186ad7SThomas Gleixner 	 * debugobject calls are safe here even with cwq->lock locked
477dc186ad7SThomas Gleixner 	 * as we know for sure that this will not trigger any of the
478dc186ad7SThomas Gleixner 	 * checks and call back into the fixup functions where we
479dc186ad7SThomas Gleixner 	 * might deadlock.
480dc186ad7SThomas Gleixner 	 */
481dc186ad7SThomas Gleixner 	INIT_WORK_ON_STACK(&barr->work, wq_barrier_func);
482fc2e4d70SOleg Nesterov 	__set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work));
483fc2e4d70SOleg Nesterov 
484fc2e4d70SOleg Nesterov 	init_completion(&barr->done);
48583c22520SOleg Nesterov 
486dc186ad7SThomas Gleixner 	debug_work_activate(&barr->work);
4871a4d9b0aSOleg Nesterov 	insert_work(cwq, &barr->work, head);
488fc2e4d70SOleg Nesterov }
489fc2e4d70SOleg Nesterov 
49014441960SOleg Nesterov static int flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
4911da177e4SLinus Torvalds {
4922355b70fSLai Jiangshan 	int active = 0;
493fc2e4d70SOleg Nesterov 	struct wq_barrier barr;
4941da177e4SLinus Torvalds 
4952355b70fSLai Jiangshan 	WARN_ON(cwq->thread == current);
4962355b70fSLai Jiangshan 
49783c22520SOleg Nesterov 	spin_lock_irq(&cwq->lock);
49883c22520SOleg Nesterov 	if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
4991a4d9b0aSOleg Nesterov 		insert_wq_barrier(cwq, &barr, &cwq->worklist);
50083c22520SOleg Nesterov 		active = 1;
50183c22520SOleg Nesterov 	}
50283c22520SOleg Nesterov 	spin_unlock_irq(&cwq->lock);
5031da177e4SLinus Torvalds 
504dc186ad7SThomas Gleixner 	if (active) {
505fc2e4d70SOleg Nesterov 		wait_for_completion(&barr.done);
506dc186ad7SThomas Gleixner 		destroy_work_on_stack(&barr.work);
507dc186ad7SThomas Gleixner 	}
50814441960SOleg Nesterov 
50914441960SOleg Nesterov 	return active;
51083c22520SOleg Nesterov }
5111da177e4SLinus Torvalds 
5120fcb78c2SRolf Eike Beer /**
5131da177e4SLinus Torvalds  * flush_workqueue - ensure that any scheduled work has run to completion.
5140fcb78c2SRolf Eike Beer  * @wq: workqueue to flush
5151da177e4SLinus Torvalds  *
5161da177e4SLinus Torvalds  * Forces execution of the workqueue and blocks until its completion.
5171da177e4SLinus Torvalds  * This is typically used in driver shutdown handlers.
5181da177e4SLinus Torvalds  *
519fc2e4d70SOleg Nesterov  * We sleep until all works which were queued on entry have been handled,
520fc2e4d70SOleg Nesterov  * but we are not livelocked by new incoming ones.
5211da177e4SLinus Torvalds  *
5221da177e4SLinus Torvalds  * This function used to run the workqueues itself.  Now we just wait for the
5231da177e4SLinus Torvalds  * helper threads to do it.
5241da177e4SLinus Torvalds  */
5257ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq)
5261da177e4SLinus Torvalds {
527e7577c50SRusty Russell 	const struct cpumask *cpu_map = wq_cpu_map(wq);
528cce1a165SOleg Nesterov 	int cpu;
529b1f4ec17SOleg Nesterov 
530f293ea92SOleg Nesterov 	might_sleep();
5313295f0efSIngo Molnar 	lock_map_acquire(&wq->lockdep_map);
5323295f0efSIngo Molnar 	lock_map_release(&wq->lockdep_map);
533aa85ea5bSRusty Russell 	for_each_cpu(cpu, cpu_map)
53489ada679SChristoph Lameter 		flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
5351da177e4SLinus Torvalds }
536ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(flush_workqueue);
5371da177e4SLinus Torvalds 
538db700897SOleg Nesterov /**
539db700897SOleg Nesterov  * flush_work - block until a work_struct's callback has terminated
540db700897SOleg Nesterov  * @work: the work which is to be flushed
541db700897SOleg Nesterov  *
542a67da70dSOleg Nesterov  * Returns false if @work has already terminated.
543a67da70dSOleg Nesterov  *
544db700897SOleg Nesterov  * It is expected that, prior to calling flush_work(), the caller has
545db700897SOleg Nesterov  * arranged for the work to not be requeued, otherwise it doesn't make
546db700897SOleg Nesterov  * sense to use this function.
547db700897SOleg Nesterov  */
548db700897SOleg Nesterov int flush_work(struct work_struct *work)
549db700897SOleg Nesterov {
550db700897SOleg Nesterov 	struct cpu_workqueue_struct *cwq;
551db700897SOleg Nesterov 	struct list_head *prev;
552db700897SOleg Nesterov 	struct wq_barrier barr;
553db700897SOleg Nesterov 
554db700897SOleg Nesterov 	might_sleep();
555db700897SOleg Nesterov 	cwq = get_wq_data(work);
556db700897SOleg Nesterov 	if (!cwq)
557db700897SOleg Nesterov 		return 0;
558db700897SOleg Nesterov 
5593295f0efSIngo Molnar 	lock_map_acquire(&cwq->wq->lockdep_map);
5603295f0efSIngo Molnar 	lock_map_release(&cwq->wq->lockdep_map);
561a67da70dSOleg Nesterov 
562db700897SOleg Nesterov 	prev = NULL;
563db700897SOleg Nesterov 	spin_lock_irq(&cwq->lock);
564db700897SOleg Nesterov 	if (!list_empty(&work->entry)) {
565db700897SOleg Nesterov 		/*
566db700897SOleg Nesterov 		 * See the comment near try_to_grab_pending()->smp_rmb().
567db700897SOleg Nesterov 		 * If it was re-queued under us we are not going to wait.
568db700897SOleg Nesterov 		 */
569db700897SOleg Nesterov 		smp_rmb();
570db700897SOleg Nesterov 		if (unlikely(cwq != get_wq_data(work)))
571db700897SOleg Nesterov 			goto out;
572db700897SOleg Nesterov 		prev = &work->entry;
573db700897SOleg Nesterov 	} else {
574db700897SOleg Nesterov 		if (cwq->current_work != work)
575db700897SOleg Nesterov 			goto out;
576db700897SOleg Nesterov 		prev = &cwq->worklist;
577db700897SOleg Nesterov 	}
578db700897SOleg Nesterov 	insert_wq_barrier(cwq, &barr, prev->next);
579db700897SOleg Nesterov out:
580db700897SOleg Nesterov 	spin_unlock_irq(&cwq->lock);
581db700897SOleg Nesterov 	if (!prev)
582db700897SOleg Nesterov 		return 0;
583db700897SOleg Nesterov 
584db700897SOleg Nesterov 	wait_for_completion(&barr.done);
585dc186ad7SThomas Gleixner 	destroy_work_on_stack(&barr.work);
586db700897SOleg Nesterov 	return 1;
587db700897SOleg Nesterov }
588db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work);
589db700897SOleg Nesterov 
5906e84d644SOleg Nesterov /*
5911f1f642eSOleg Nesterov  * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
5926e84d644SOleg Nesterov  * so this work can't be re-armed in any way.
5936e84d644SOleg Nesterov  */
5946e84d644SOleg Nesterov static int try_to_grab_pending(struct work_struct *work)
5956e84d644SOleg Nesterov {
5966e84d644SOleg Nesterov 	struct cpu_workqueue_struct *cwq;
5971f1f642eSOleg Nesterov 	int ret = -1;
5986e84d644SOleg Nesterov 
5996e84d644SOleg Nesterov 	if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work)))
6001f1f642eSOleg Nesterov 		return 0;
6016e84d644SOleg Nesterov 
6026e84d644SOleg Nesterov 	/*
6036e84d644SOleg Nesterov 	 * The queueing is in progress, or it is already queued. Try to
6046e84d644SOleg Nesterov 	 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
6056e84d644SOleg Nesterov 	 */
6066e84d644SOleg Nesterov 
6076e84d644SOleg Nesterov 	cwq = get_wq_data(work);
6086e84d644SOleg Nesterov 	if (!cwq)
6096e84d644SOleg Nesterov 		return ret;
6106e84d644SOleg Nesterov 
6116e84d644SOleg Nesterov 	spin_lock_irq(&cwq->lock);
6126e84d644SOleg Nesterov 	if (!list_empty(&work->entry)) {
6136e84d644SOleg Nesterov 		/*
6146e84d644SOleg Nesterov 		 * This work is queued, but perhaps we locked the wrong cwq.
6156e84d644SOleg Nesterov 		 * In that case we must see the new value after rmb(), see
6166e84d644SOleg Nesterov 		 * insert_work()->wmb().
6176e84d644SOleg Nesterov 		 */
6186e84d644SOleg Nesterov 		smp_rmb();
6196e84d644SOleg Nesterov 		if (cwq == get_wq_data(work)) {
620dc186ad7SThomas Gleixner 			debug_work_deactivate(work);
6216e84d644SOleg Nesterov 			list_del_init(&work->entry);
6226e84d644SOleg Nesterov 			ret = 1;
6236e84d644SOleg Nesterov 		}
6246e84d644SOleg Nesterov 	}
6256e84d644SOleg Nesterov 	spin_unlock_irq(&cwq->lock);
6266e84d644SOleg Nesterov 
6276e84d644SOleg Nesterov 	return ret;
6286e84d644SOleg Nesterov }
6296e84d644SOleg Nesterov 
6306e84d644SOleg Nesterov static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq,
631b89deed3SOleg Nesterov 				struct work_struct *work)
632b89deed3SOleg Nesterov {
633b89deed3SOleg Nesterov 	struct wq_barrier barr;
634b89deed3SOleg Nesterov 	int running = 0;
635b89deed3SOleg Nesterov 
636b89deed3SOleg Nesterov 	spin_lock_irq(&cwq->lock);
637b89deed3SOleg Nesterov 	if (unlikely(cwq->current_work == work)) {
6381a4d9b0aSOleg Nesterov 		insert_wq_barrier(cwq, &barr, cwq->worklist.next);
639b89deed3SOleg Nesterov 		running = 1;
640b89deed3SOleg Nesterov 	}
641b89deed3SOleg Nesterov 	spin_unlock_irq(&cwq->lock);
642b89deed3SOleg Nesterov 
643dc186ad7SThomas Gleixner 	if (unlikely(running)) {
644b89deed3SOleg Nesterov 		wait_for_completion(&barr.done);
645dc186ad7SThomas Gleixner 		destroy_work_on_stack(&barr.work);
646dc186ad7SThomas Gleixner 	}
647b89deed3SOleg Nesterov }
648b89deed3SOleg Nesterov 
6496e84d644SOleg Nesterov static void wait_on_work(struct work_struct *work)
650b89deed3SOleg Nesterov {
651b89deed3SOleg Nesterov 	struct cpu_workqueue_struct *cwq;
65228e53bddSOleg Nesterov 	struct workqueue_struct *wq;
653e7577c50SRusty Russell 	const struct cpumask *cpu_map;
654b1f4ec17SOleg Nesterov 	int cpu;
655b89deed3SOleg Nesterov 
656f293ea92SOleg Nesterov 	might_sleep();
657f293ea92SOleg Nesterov 
6583295f0efSIngo Molnar 	lock_map_acquire(&work->lockdep_map);
6593295f0efSIngo Molnar 	lock_map_release(&work->lockdep_map);
6604e6045f1SJohannes Berg 
661b89deed3SOleg Nesterov 	cwq = get_wq_data(work);
662b89deed3SOleg Nesterov 	if (!cwq)
6633af24433SOleg Nesterov 		return;
664b89deed3SOleg Nesterov 
66528e53bddSOleg Nesterov 	wq = cwq->wq;
66628e53bddSOleg Nesterov 	cpu_map = wq_cpu_map(wq);
66728e53bddSOleg Nesterov 
668aa85ea5bSRusty Russell 	for_each_cpu(cpu, cpu_map)
6696e84d644SOleg Nesterov 		wait_on_cpu_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
6706e84d644SOleg Nesterov }
6716e84d644SOleg Nesterov 
6721f1f642eSOleg Nesterov static int __cancel_work_timer(struct work_struct *work,
6731f1f642eSOleg Nesterov 				struct timer_list* timer)
6741f1f642eSOleg Nesterov {
6751f1f642eSOleg Nesterov 	int ret;
6761f1f642eSOleg Nesterov 
6771f1f642eSOleg Nesterov 	do {
6781f1f642eSOleg Nesterov 		ret = (timer && likely(del_timer(timer)));
6791f1f642eSOleg Nesterov 		if (!ret)
6801f1f642eSOleg Nesterov 			ret = try_to_grab_pending(work);
6811f1f642eSOleg Nesterov 		wait_on_work(work);
6821f1f642eSOleg Nesterov 	} while (unlikely(ret < 0));
6831f1f642eSOleg Nesterov 
684*4d707b9fSOleg Nesterov 	clear_wq_data(work);
6851f1f642eSOleg Nesterov 	return ret;
6861f1f642eSOleg Nesterov }
6871f1f642eSOleg Nesterov 
6886e84d644SOleg Nesterov /**
6896e84d644SOleg Nesterov  * cancel_work_sync - block until a work_struct's callback has terminated
6906e84d644SOleg Nesterov  * @work: the work which is to be flushed
6916e84d644SOleg Nesterov  *
6921f1f642eSOleg Nesterov  * Returns true if @work was pending.
6931f1f642eSOleg Nesterov  *
6946e84d644SOleg Nesterov  * cancel_work_sync() will cancel the work if it is queued. If the work's
6956e84d644SOleg Nesterov  * callback appears to be running, cancel_work_sync() will block until it
6966e84d644SOleg Nesterov  * has completed.
6976e84d644SOleg Nesterov  *
6986e84d644SOleg Nesterov  * It is possible to use this function if the work re-queues itself. It can
6996e84d644SOleg Nesterov  * cancel the work even if it migrates to another workqueue, however in that
7006e84d644SOleg Nesterov  * case it only guarantees that work->func() has completed on the last queued
7016e84d644SOleg Nesterov  * workqueue.
7026e84d644SOleg Nesterov  *
7036e84d644SOleg Nesterov  * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
7046e84d644SOleg Nesterov  * pending, otherwise it goes into a busy-wait loop until the timer expires.
7056e84d644SOleg Nesterov  *
7066e84d644SOleg Nesterov  * The caller must ensure that workqueue_struct on which this work was last
7076e84d644SOleg Nesterov  * queued can't be destroyed before this function returns.
7086e84d644SOleg Nesterov  */
7091f1f642eSOleg Nesterov int cancel_work_sync(struct work_struct *work)
7106e84d644SOleg Nesterov {
7111f1f642eSOleg Nesterov 	return __cancel_work_timer(work, NULL);
712b89deed3SOleg Nesterov }
71328e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync);
714b89deed3SOleg Nesterov 
7156e84d644SOleg Nesterov /**
716f5a421a4SOleg Nesterov  * cancel_delayed_work_sync - reliably kill off a delayed work.
7176e84d644SOleg Nesterov  * @dwork: the delayed work struct
7186e84d644SOleg Nesterov  *
7191f1f642eSOleg Nesterov  * Returns true if @dwork was pending.
7201f1f642eSOleg Nesterov  *
7216e84d644SOleg Nesterov  * It is possible to use this function if @dwork rearms itself via queue_work()
7226e84d644SOleg Nesterov  * or queue_delayed_work(). See also the comment for cancel_work_sync().
7236e84d644SOleg Nesterov  */
7241f1f642eSOleg Nesterov int cancel_delayed_work_sync(struct delayed_work *dwork)
7256e84d644SOleg Nesterov {
7261f1f642eSOleg Nesterov 	return __cancel_work_timer(&dwork->work, &dwork->timer);
7276e84d644SOleg Nesterov }
728f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync);
7291da177e4SLinus Torvalds 
7306e84d644SOleg Nesterov static struct workqueue_struct *keventd_wq __read_mostly;
7311da177e4SLinus Torvalds 
7320fcb78c2SRolf Eike Beer /**
7330fcb78c2SRolf Eike Beer  * schedule_work - put work task in global workqueue
7340fcb78c2SRolf Eike Beer  * @work: job to be done
7350fcb78c2SRolf Eike Beer  *
7365b0f437dSBart Van Assche  * Returns zero if @work was already on the kernel-global workqueue and
7375b0f437dSBart Van Assche  * non-zero otherwise.
7385b0f437dSBart Van Assche  *
7395b0f437dSBart Van Assche  * This puts a job in the kernel-global workqueue if it was not already
7405b0f437dSBart Van Assche  * queued and leaves it in the same position on the kernel-global
7415b0f437dSBart Van Assche  * workqueue otherwise.
7420fcb78c2SRolf Eike Beer  */
7437ad5b3a5SHarvey Harrison int schedule_work(struct work_struct *work)
7441da177e4SLinus Torvalds {
7451da177e4SLinus Torvalds 	return queue_work(keventd_wq, work);
7461da177e4SLinus Torvalds }
747ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_work);
7481da177e4SLinus Torvalds 
749c1a220e7SZhang Rui /*
750c1a220e7SZhang Rui  * schedule_work_on - put work task on a specific cpu
751c1a220e7SZhang Rui  * @cpu: cpu to put the work task on
752c1a220e7SZhang Rui  * @work: job to be done
753c1a220e7SZhang Rui  *
754c1a220e7SZhang Rui  * This puts a job on a specific cpu
755c1a220e7SZhang Rui  */
756c1a220e7SZhang Rui int schedule_work_on(int cpu, struct work_struct *work)
757c1a220e7SZhang Rui {
758c1a220e7SZhang Rui 	return queue_work_on(cpu, keventd_wq, work);
759c1a220e7SZhang Rui }
760c1a220e7SZhang Rui EXPORT_SYMBOL(schedule_work_on);
761c1a220e7SZhang Rui 
7620fcb78c2SRolf Eike Beer /**
7630fcb78c2SRolf Eike Beer  * schedule_delayed_work - put work task in global workqueue after delay
76452bad64dSDavid Howells  * @dwork: job to be done
76552bad64dSDavid Howells  * @delay: number of jiffies to wait or 0 for immediate execution
7660fcb78c2SRolf Eike Beer  *
7670fcb78c2SRolf Eike Beer  * After waiting for a given time this puts a job in the kernel-global
7680fcb78c2SRolf Eike Beer  * workqueue.
7690fcb78c2SRolf Eike Beer  */
7707ad5b3a5SHarvey Harrison int schedule_delayed_work(struct delayed_work *dwork,
77182f67cd9SIngo Molnar 					unsigned long delay)
7721da177e4SLinus Torvalds {
77352bad64dSDavid Howells 	return queue_delayed_work(keventd_wq, dwork, delay);
7741da177e4SLinus Torvalds }
775ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work);
7761da177e4SLinus Torvalds 
7770fcb78c2SRolf Eike Beer /**
7788c53e463SLinus Torvalds  * flush_delayed_work - block until a dwork_struct's callback has terminated
7798c53e463SLinus Torvalds  * @dwork: the delayed work which is to be flushed
7808c53e463SLinus Torvalds  *
7818c53e463SLinus Torvalds  * Any timeout is cancelled, and any pending work is run immediately.
7828c53e463SLinus Torvalds  */
7838c53e463SLinus Torvalds void flush_delayed_work(struct delayed_work *dwork)
7848c53e463SLinus Torvalds {
7858c53e463SLinus Torvalds 	if (del_timer_sync(&dwork->timer)) {
7868c53e463SLinus Torvalds 		struct cpu_workqueue_struct *cwq;
78747dd5be2SOleg Nesterov 		cwq = wq_per_cpu(get_wq_data(&dwork->work)->wq, get_cpu());
7888c53e463SLinus Torvalds 		__queue_work(cwq, &dwork->work);
7898c53e463SLinus Torvalds 		put_cpu();
7908c53e463SLinus Torvalds 	}
7918c53e463SLinus Torvalds 	flush_work(&dwork->work);
7928c53e463SLinus Torvalds }
7938c53e463SLinus Torvalds EXPORT_SYMBOL(flush_delayed_work);
7948c53e463SLinus Torvalds 
7958c53e463SLinus Torvalds /**
7960fcb78c2SRolf Eike Beer  * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
7970fcb78c2SRolf Eike Beer  * @cpu: cpu to use
79852bad64dSDavid Howells  * @dwork: job to be done
7990fcb78c2SRolf Eike Beer  * @delay: number of jiffies to wait
8000fcb78c2SRolf Eike Beer  *
8010fcb78c2SRolf Eike Beer  * After waiting for a given time this puts a job in the kernel-global
8020fcb78c2SRolf Eike Beer  * workqueue on the specified CPU.
8030fcb78c2SRolf Eike Beer  */
8041da177e4SLinus Torvalds int schedule_delayed_work_on(int cpu,
80552bad64dSDavid Howells 			struct delayed_work *dwork, unsigned long delay)
8061da177e4SLinus Torvalds {
80752bad64dSDavid Howells 	return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
8081da177e4SLinus Torvalds }
809ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work_on);
8101da177e4SLinus Torvalds 
811b6136773SAndrew Morton /**
812b6136773SAndrew Morton  * schedule_on_each_cpu - call a function on each online CPU from keventd
813b6136773SAndrew Morton  * @func: the function to call
814b6136773SAndrew Morton  *
815b6136773SAndrew Morton  * Returns zero on success.
816b6136773SAndrew Morton  * Returns -ve errno on failure.
817b6136773SAndrew Morton  *
818b6136773SAndrew Morton  * schedule_on_each_cpu() is very slow.
819b6136773SAndrew Morton  */
82065f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func)
82115316ba8SChristoph Lameter {
82215316ba8SChristoph Lameter 	int cpu;
82365a64464SAndi Kleen 	int orig = -1;
824b6136773SAndrew Morton 	struct work_struct *works;
82515316ba8SChristoph Lameter 
826b6136773SAndrew Morton 	works = alloc_percpu(struct work_struct);
827b6136773SAndrew Morton 	if (!works)
82815316ba8SChristoph Lameter 		return -ENOMEM;
829b6136773SAndrew Morton 
83095402b38SGautham R Shenoy 	get_online_cpus();
83193981800STejun Heo 
83293981800STejun Heo 	/*
83393981800STejun Heo 	 * When running in keventd don't schedule a work item on
83493981800STejun Heo 	 * itself.  Can just call directly because the work queue is
83593981800STejun Heo 	 * already bound.  This also is faster.
83693981800STejun Heo 	 */
83793981800STejun Heo 	if (current_is_keventd())
83893981800STejun Heo 		orig = raw_smp_processor_id();
83993981800STejun Heo 
84015316ba8SChristoph Lameter 	for_each_online_cpu(cpu) {
8419bfb1839SIngo Molnar 		struct work_struct *work = per_cpu_ptr(works, cpu);
8429bfb1839SIngo Molnar 
8439bfb1839SIngo Molnar 		INIT_WORK(work, func);
84493981800STejun Heo 		if (cpu != orig)
8458de6d308SOleg Nesterov 			schedule_work_on(cpu, work);
84615316ba8SChristoph Lameter 	}
84793981800STejun Heo 	if (orig >= 0)
84893981800STejun Heo 		func(per_cpu_ptr(works, orig));
84993981800STejun Heo 
85093981800STejun Heo 	for_each_online_cpu(cpu)
8518616a89aSOleg Nesterov 		flush_work(per_cpu_ptr(works, cpu));
85293981800STejun Heo 
85395402b38SGautham R Shenoy 	put_online_cpus();
854b6136773SAndrew Morton 	free_percpu(works);
85515316ba8SChristoph Lameter 	return 0;
85615316ba8SChristoph Lameter }
85715316ba8SChristoph Lameter 
858eef6a7d5SAlan Stern /**
859eef6a7d5SAlan Stern  * flush_scheduled_work - ensure that any scheduled work has run to completion.
860eef6a7d5SAlan Stern  *
861eef6a7d5SAlan Stern  * Forces execution of the kernel-global workqueue and blocks until its
862eef6a7d5SAlan Stern  * completion.
863eef6a7d5SAlan Stern  *
864eef6a7d5SAlan Stern  * Think twice before calling this function!  It's very easy to get into
865eef6a7d5SAlan Stern  * trouble if you don't take great care.  Either of the following situations
866eef6a7d5SAlan Stern  * will lead to deadlock:
867eef6a7d5SAlan Stern  *
868eef6a7d5SAlan Stern  *	One of the work items currently on the workqueue needs to acquire
869eef6a7d5SAlan Stern  *	a lock held by your code or its caller.
870eef6a7d5SAlan Stern  *
871eef6a7d5SAlan Stern  *	Your code is running in the context of a work routine.
872eef6a7d5SAlan Stern  *
873eef6a7d5SAlan Stern  * They will be detected by lockdep when they occur, but the first might not
874eef6a7d5SAlan Stern  * occur very often.  It depends on what work items are on the workqueue and
875eef6a7d5SAlan Stern  * what locks they need, which you have no control over.
876eef6a7d5SAlan Stern  *
877eef6a7d5SAlan Stern  * In most situations flushing the entire workqueue is overkill; you merely
878eef6a7d5SAlan Stern  * need to know that a particular work item isn't queued and isn't running.
879eef6a7d5SAlan Stern  * In such cases you should use cancel_delayed_work_sync() or
880eef6a7d5SAlan Stern  * cancel_work_sync() instead.
881eef6a7d5SAlan Stern  */
8821da177e4SLinus Torvalds void flush_scheduled_work(void)
8831da177e4SLinus Torvalds {
8841da177e4SLinus Torvalds 	flush_workqueue(keventd_wq);
8851da177e4SLinus Torvalds }
886ae90dd5dSDave Jones EXPORT_SYMBOL(flush_scheduled_work);
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds /**
8891fa44ecaSJames Bottomley  * execute_in_process_context - reliably execute the routine with user context
8901fa44ecaSJames Bottomley  * @fn:		the function to execute
8911fa44ecaSJames Bottomley  * @ew:		guaranteed storage for the execute work structure (must
8921fa44ecaSJames Bottomley  *		be available when the work executes)
8931fa44ecaSJames Bottomley  *
8941fa44ecaSJames Bottomley  * Executes the function immediately if process context is available,
8951fa44ecaSJames Bottomley  * otherwise schedules the function for delayed execution.
8961fa44ecaSJames Bottomley  *
8971fa44ecaSJames Bottomley  * Returns:	0 - function was executed
8981fa44ecaSJames Bottomley  *		1 - function was scheduled for execution
8991fa44ecaSJames Bottomley  */
90065f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew)
9011fa44ecaSJames Bottomley {
9021fa44ecaSJames Bottomley 	if (!in_interrupt()) {
90365f27f38SDavid Howells 		fn(&ew->work);
9041fa44ecaSJames Bottomley 		return 0;
9051fa44ecaSJames Bottomley 	}
9061fa44ecaSJames Bottomley 
90765f27f38SDavid Howells 	INIT_WORK(&ew->work, fn);
9081fa44ecaSJames Bottomley 	schedule_work(&ew->work);
9091fa44ecaSJames Bottomley 
9101fa44ecaSJames Bottomley 	return 1;
9111fa44ecaSJames Bottomley }
9121fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context);
9131fa44ecaSJames Bottomley 
9141da177e4SLinus Torvalds int keventd_up(void)
9151da177e4SLinus Torvalds {
9161da177e4SLinus Torvalds 	return keventd_wq != NULL;
9171da177e4SLinus Torvalds }
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds int current_is_keventd(void)
9201da177e4SLinus Torvalds {
9211da177e4SLinus Torvalds 	struct cpu_workqueue_struct *cwq;
922d243769dSHugh Dickins 	int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
9231da177e4SLinus Torvalds 	int ret = 0;
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 	BUG_ON(!keventd_wq);
9261da177e4SLinus Torvalds 
92789ada679SChristoph Lameter 	cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
9281da177e4SLinus Torvalds 	if (current == cwq->thread)
9291da177e4SLinus Torvalds 		ret = 1;
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 	return ret;
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds }
9341da177e4SLinus Torvalds 
9353af24433SOleg Nesterov static struct cpu_workqueue_struct *
9363af24433SOleg Nesterov init_cpu_workqueue(struct workqueue_struct *wq, int cpu)
9371da177e4SLinus Torvalds {
93889ada679SChristoph Lameter 	struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
9393af24433SOleg Nesterov 
9403af24433SOleg Nesterov 	cwq->wq = wq;
9413af24433SOleg Nesterov 	spin_lock_init(&cwq->lock);
9423af24433SOleg Nesterov 	INIT_LIST_HEAD(&cwq->worklist);
9433af24433SOleg Nesterov 	init_waitqueue_head(&cwq->more_work);
9443af24433SOleg Nesterov 
9453af24433SOleg Nesterov 	return cwq;
9463af24433SOleg Nesterov }
9473af24433SOleg Nesterov 
9483af24433SOleg Nesterov static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
9493af24433SOleg Nesterov {
9500d557dc9SHeiko Carstens 	struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
9513af24433SOleg Nesterov 	struct workqueue_struct *wq = cwq->wq;
9526cc88bc4SDavid Howells 	const char *fmt = is_wq_single_threaded(wq) ? "%s" : "%s/%d";
9533af24433SOleg Nesterov 	struct task_struct *p;
9543af24433SOleg Nesterov 
9553af24433SOleg Nesterov 	p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu);
9563af24433SOleg Nesterov 	/*
9573af24433SOleg Nesterov 	 * Nobody can add the work_struct to this cwq,
9583af24433SOleg Nesterov 	 *	if (caller is __create_workqueue)
9593af24433SOleg Nesterov 	 *		nobody should see this wq
9603af24433SOleg Nesterov 	 *	else // caller is CPU_UP_PREPARE
9613af24433SOleg Nesterov 	 *		cpu is not on cpu_online_map
9623af24433SOleg Nesterov 	 * so we can abort safely.
9633af24433SOleg Nesterov 	 */
9643af24433SOleg Nesterov 	if (IS_ERR(p))
9653af24433SOleg Nesterov 		return PTR_ERR(p);
9660d557dc9SHeiko Carstens 	if (cwq->wq->rt)
9670d557dc9SHeiko Carstens 		sched_setscheduler_nocheck(p, SCHED_FIFO, &param);
9683af24433SOleg Nesterov 	cwq->thread = p;
9693af24433SOleg Nesterov 
970e1d8aa9fSFrederic Weisbecker 	trace_workqueue_creation(cwq->thread, cpu);
971e1d8aa9fSFrederic Weisbecker 
9723af24433SOleg Nesterov 	return 0;
9733af24433SOleg Nesterov }
9743af24433SOleg Nesterov 
97506ba38a9SOleg Nesterov static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
97606ba38a9SOleg Nesterov {
97706ba38a9SOleg Nesterov 	struct task_struct *p = cwq->thread;
97806ba38a9SOleg Nesterov 
97906ba38a9SOleg Nesterov 	if (p != NULL) {
98006ba38a9SOleg Nesterov 		if (cpu >= 0)
98106ba38a9SOleg Nesterov 			kthread_bind(p, cpu);
98206ba38a9SOleg Nesterov 		wake_up_process(p);
98306ba38a9SOleg Nesterov 	}
98406ba38a9SOleg Nesterov }
98506ba38a9SOleg Nesterov 
9864e6045f1SJohannes Berg struct workqueue_struct *__create_workqueue_key(const char *name,
9874e6045f1SJohannes Berg 						int singlethread,
9884e6045f1SJohannes Berg 						int freezeable,
9890d557dc9SHeiko Carstens 						int rt,
990eb13ba87SJohannes Berg 						struct lock_class_key *key,
991eb13ba87SJohannes Berg 						const char *lock_name)
9923af24433SOleg Nesterov {
9933af24433SOleg Nesterov 	struct workqueue_struct *wq;
9943af24433SOleg Nesterov 	struct cpu_workqueue_struct *cwq;
9953af24433SOleg Nesterov 	int err = 0, cpu;
9963af24433SOleg Nesterov 
9973af24433SOleg Nesterov 	wq = kzalloc(sizeof(*wq), GFP_KERNEL);
9983af24433SOleg Nesterov 	if (!wq)
9993af24433SOleg Nesterov 		return NULL;
10003af24433SOleg Nesterov 
10013af24433SOleg Nesterov 	wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
10023af24433SOleg Nesterov 	if (!wq->cpu_wq) {
10033af24433SOleg Nesterov 		kfree(wq);
10043af24433SOleg Nesterov 		return NULL;
10053af24433SOleg Nesterov 	}
10063af24433SOleg Nesterov 
10073af24433SOleg Nesterov 	wq->name = name;
1008eb13ba87SJohannes Berg 	lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
1009cce1a165SOleg Nesterov 	wq->singlethread = singlethread;
10103af24433SOleg Nesterov 	wq->freezeable = freezeable;
10110d557dc9SHeiko Carstens 	wq->rt = rt;
1012cce1a165SOleg Nesterov 	INIT_LIST_HEAD(&wq->list);
10133af24433SOleg Nesterov 
10143af24433SOleg Nesterov 	if (singlethread) {
10153af24433SOleg Nesterov 		cwq = init_cpu_workqueue(wq, singlethread_cpu);
10163af24433SOleg Nesterov 		err = create_workqueue_thread(cwq, singlethread_cpu);
101706ba38a9SOleg Nesterov 		start_workqueue_thread(cwq, -1);
10183af24433SOleg Nesterov 	} else {
10193da1c84cSOleg Nesterov 		cpu_maps_update_begin();
10206af8bf3dSOleg Nesterov 		/*
10216af8bf3dSOleg Nesterov 		 * We must place this wq on list even if the code below fails.
10226af8bf3dSOleg Nesterov 		 * cpu_down(cpu) can remove cpu from cpu_populated_map before
10236af8bf3dSOleg Nesterov 		 * destroy_workqueue() takes the lock, in that case we leak
10246af8bf3dSOleg Nesterov 		 * cwq[cpu]->thread.
10256af8bf3dSOleg Nesterov 		 */
102695402b38SGautham R Shenoy 		spin_lock(&workqueue_lock);
10273af24433SOleg Nesterov 		list_add(&wq->list, &workqueues);
102895402b38SGautham R Shenoy 		spin_unlock(&workqueue_lock);
10296af8bf3dSOleg Nesterov 		/*
10306af8bf3dSOleg Nesterov 		 * We must initialize cwqs for each possible cpu even if we
10316af8bf3dSOleg Nesterov 		 * are going to call destroy_workqueue() finally. Otherwise
10326af8bf3dSOleg Nesterov 		 * cpu_up() can hit the uninitialized cwq once we drop the
10336af8bf3dSOleg Nesterov 		 * lock.
10346af8bf3dSOleg Nesterov 		 */
10353af24433SOleg Nesterov 		for_each_possible_cpu(cpu) {
10363af24433SOleg Nesterov 			cwq = init_cpu_workqueue(wq, cpu);
10373af24433SOleg Nesterov 			if (err || !cpu_online(cpu))
10383af24433SOleg Nesterov 				continue;
10393af24433SOleg Nesterov 			err = create_workqueue_thread(cwq, cpu);
104006ba38a9SOleg Nesterov 			start_workqueue_thread(cwq, cpu);
10413af24433SOleg Nesterov 		}
10423da1c84cSOleg Nesterov 		cpu_maps_update_done();
10433af24433SOleg Nesterov 	}
10443af24433SOleg Nesterov 
10453af24433SOleg Nesterov 	if (err) {
10463af24433SOleg Nesterov 		destroy_workqueue(wq);
10473af24433SOleg Nesterov 		wq = NULL;
10483af24433SOleg Nesterov 	}
10493af24433SOleg Nesterov 	return wq;
10503af24433SOleg Nesterov }
10514e6045f1SJohannes Berg EXPORT_SYMBOL_GPL(__create_workqueue_key);
10523af24433SOleg Nesterov 
10531e35eaa2SOleg Nesterov static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq)
10543af24433SOleg Nesterov {
10553af24433SOleg Nesterov 	/*
10563da1c84cSOleg Nesterov 	 * Our caller is either destroy_workqueue() or CPU_POST_DEAD,
10573da1c84cSOleg Nesterov 	 * cpu_add_remove_lock protects cwq->thread.
10583af24433SOleg Nesterov 	 */
105914441960SOleg Nesterov 	if (cwq->thread == NULL)
106014441960SOleg Nesterov 		return;
106114441960SOleg Nesterov 
10623295f0efSIngo Molnar 	lock_map_acquire(&cwq->wq->lockdep_map);
10633295f0efSIngo Molnar 	lock_map_release(&cwq->wq->lockdep_map);
10644e6045f1SJohannes Berg 
106513c22168SOleg Nesterov 	flush_cpu_workqueue(cwq);
106614441960SOleg Nesterov 	/*
10673da1c84cSOleg Nesterov 	 * If the caller is CPU_POST_DEAD and cwq->worklist was not empty,
106813c22168SOleg Nesterov 	 * a concurrent flush_workqueue() can insert a barrier after us.
106913c22168SOleg Nesterov 	 * However, in that case run_workqueue() won't return and check
107013c22168SOleg Nesterov 	 * kthread_should_stop() until it flushes all work_struct's.
107114441960SOleg Nesterov 	 * When ->worklist becomes empty it is safe to exit because no
107214441960SOleg Nesterov 	 * more work_structs can be queued on this cwq: flush_workqueue
107314441960SOleg Nesterov 	 * checks list_empty(), and a "normal" queue_work() can't use
107414441960SOleg Nesterov 	 * a dead CPU.
107514441960SOleg Nesterov 	 */
1076e1d8aa9fSFrederic Weisbecker 	trace_workqueue_destruction(cwq->thread);
107714441960SOleg Nesterov 	kthread_stop(cwq->thread);
107814441960SOleg Nesterov 	cwq->thread = NULL;
10791da177e4SLinus Torvalds }
10801da177e4SLinus Torvalds 
10813af24433SOleg Nesterov /**
10823af24433SOleg Nesterov  * destroy_workqueue - safely terminate a workqueue
10833af24433SOleg Nesterov  * @wq: target workqueue
10843af24433SOleg Nesterov  *
10853af24433SOleg Nesterov  * Safely destroy a workqueue. All work currently pending will be done first.
10863af24433SOleg Nesterov  */
10873af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq)
10883af24433SOleg Nesterov {
1089e7577c50SRusty Russell 	const struct cpumask *cpu_map = wq_cpu_map(wq);
10903af24433SOleg Nesterov 	int cpu;
10913af24433SOleg Nesterov 
10923da1c84cSOleg Nesterov 	cpu_maps_update_begin();
109395402b38SGautham R Shenoy 	spin_lock(&workqueue_lock);
10943af24433SOleg Nesterov 	list_del(&wq->list);
109595402b38SGautham R Shenoy 	spin_unlock(&workqueue_lock);
10963af24433SOleg Nesterov 
1097aa85ea5bSRusty Russell 	for_each_cpu(cpu, cpu_map)
10981e35eaa2SOleg Nesterov 		cleanup_workqueue_thread(per_cpu_ptr(wq->cpu_wq, cpu));
10993da1c84cSOleg Nesterov  	cpu_maps_update_done();
11003af24433SOleg Nesterov 
11013af24433SOleg Nesterov 	free_percpu(wq->cpu_wq);
11023af24433SOleg Nesterov 	kfree(wq);
11033af24433SOleg Nesterov }
11043af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue);
11053af24433SOleg Nesterov 
11069c7b216dSChandra Seetharaman static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
11071da177e4SLinus Torvalds 						unsigned long action,
11081da177e4SLinus Torvalds 						void *hcpu)
11091da177e4SLinus Torvalds {
11103af24433SOleg Nesterov 	unsigned int cpu = (unsigned long)hcpu;
11113af24433SOleg Nesterov 	struct cpu_workqueue_struct *cwq;
11121da177e4SLinus Torvalds 	struct workqueue_struct *wq;
11138448502cSOleg Nesterov 	int ret = NOTIFY_OK;
11141da177e4SLinus Torvalds 
11158bb78442SRafael J. Wysocki 	action &= ~CPU_TASKS_FROZEN;
11168bb78442SRafael J. Wysocki 
11171da177e4SLinus Torvalds 	switch (action) {
11183af24433SOleg Nesterov 	case CPU_UP_PREPARE:
1119e7577c50SRusty Russell 		cpumask_set_cpu(cpu, cpu_populated_map);
11203af24433SOleg Nesterov 	}
11218448502cSOleg Nesterov undo:
11221da177e4SLinus Torvalds 	list_for_each_entry(wq, &workqueues, list) {
11233af24433SOleg Nesterov 		cwq = per_cpu_ptr(wq->cpu_wq, cpu);
11243af24433SOleg Nesterov 
11253af24433SOleg Nesterov 		switch (action) {
11263af24433SOleg Nesterov 		case CPU_UP_PREPARE:
11273af24433SOleg Nesterov 			if (!create_workqueue_thread(cwq, cpu))
11281da177e4SLinus Torvalds 				break;
112995402b38SGautham R Shenoy 			printk(KERN_ERR "workqueue [%s] for %i failed\n",
113095402b38SGautham R Shenoy 				wq->name, cpu);
11318448502cSOleg Nesterov 			action = CPU_UP_CANCELED;
11328448502cSOleg Nesterov 			ret = NOTIFY_BAD;
11338448502cSOleg Nesterov 			goto undo;
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds 		case CPU_ONLINE:
113606ba38a9SOleg Nesterov 			start_workqueue_thread(cwq, cpu);
11371da177e4SLinus Torvalds 			break;
11381da177e4SLinus Torvalds 
11391da177e4SLinus Torvalds 		case CPU_UP_CANCELED:
114006ba38a9SOleg Nesterov 			start_workqueue_thread(cwq, -1);
11413da1c84cSOleg Nesterov 		case CPU_POST_DEAD:
11421e35eaa2SOleg Nesterov 			cleanup_workqueue_thread(cwq);
11431da177e4SLinus Torvalds 			break;
11441da177e4SLinus Torvalds 		}
11453af24433SOleg Nesterov 	}
11461da177e4SLinus Torvalds 
114700dfcaf7SOleg Nesterov 	switch (action) {
114800dfcaf7SOleg Nesterov 	case CPU_UP_CANCELED:
11493da1c84cSOleg Nesterov 	case CPU_POST_DEAD:
1150e7577c50SRusty Russell 		cpumask_clear_cpu(cpu, cpu_populated_map);
115100dfcaf7SOleg Nesterov 	}
115200dfcaf7SOleg Nesterov 
11538448502cSOleg Nesterov 	return ret;
11541da177e4SLinus Torvalds }
11551da177e4SLinus Torvalds 
11562d3854a3SRusty Russell #ifdef CONFIG_SMP
11578ccad40dSRusty Russell 
11582d3854a3SRusty Russell struct work_for_cpu {
11596b44003eSAndrew Morton 	struct completion completion;
11602d3854a3SRusty Russell 	long (*fn)(void *);
11612d3854a3SRusty Russell 	void *arg;
11622d3854a3SRusty Russell 	long ret;
11632d3854a3SRusty Russell };
11642d3854a3SRusty Russell 
11656b44003eSAndrew Morton static int do_work_for_cpu(void *_wfc)
11662d3854a3SRusty Russell {
11676b44003eSAndrew Morton 	struct work_for_cpu *wfc = _wfc;
11682d3854a3SRusty Russell 	wfc->ret = wfc->fn(wfc->arg);
11696b44003eSAndrew Morton 	complete(&wfc->completion);
11706b44003eSAndrew Morton 	return 0;
11712d3854a3SRusty Russell }
11722d3854a3SRusty Russell 
11732d3854a3SRusty Russell /**
11742d3854a3SRusty Russell  * work_on_cpu - run a function in user context on a particular cpu
11752d3854a3SRusty Russell  * @cpu: the cpu to run on
11762d3854a3SRusty Russell  * @fn: the function to run
11772d3854a3SRusty Russell  * @arg: the function arg
11782d3854a3SRusty Russell  *
117931ad9081SRusty Russell  * This will return the value @fn returns.
118031ad9081SRusty Russell  * It is up to the caller to ensure that the cpu doesn't go offline.
11816b44003eSAndrew Morton  * The caller must not hold any locks which would prevent @fn from completing.
11822d3854a3SRusty Russell  */
11832d3854a3SRusty Russell long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
11842d3854a3SRusty Russell {
11856b44003eSAndrew Morton 	struct task_struct *sub_thread;
11866b44003eSAndrew Morton 	struct work_for_cpu wfc = {
11876b44003eSAndrew Morton 		.completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
11886b44003eSAndrew Morton 		.fn = fn,
11896b44003eSAndrew Morton 		.arg = arg,
11906b44003eSAndrew Morton 	};
11912d3854a3SRusty Russell 
11926b44003eSAndrew Morton 	sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
11936b44003eSAndrew Morton 	if (IS_ERR(sub_thread))
11946b44003eSAndrew Morton 		return PTR_ERR(sub_thread);
11956b44003eSAndrew Morton 	kthread_bind(sub_thread, cpu);
11966b44003eSAndrew Morton 	wake_up_process(sub_thread);
11976b44003eSAndrew Morton 	wait_for_completion(&wfc.completion);
11982d3854a3SRusty Russell 	return wfc.ret;
11992d3854a3SRusty Russell }
12002d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu);
12012d3854a3SRusty Russell #endif /* CONFIG_SMP */
12022d3854a3SRusty Russell 
1203c12920d1SOleg Nesterov void __init init_workqueues(void)
12041da177e4SLinus Torvalds {
1205e7577c50SRusty Russell 	alloc_cpumask_var(&cpu_populated_map, GFP_KERNEL);
1206e7577c50SRusty Russell 
1207e7577c50SRusty Russell 	cpumask_copy(cpu_populated_map, cpu_online_mask);
1208e7577c50SRusty Russell 	singlethread_cpu = cpumask_first(cpu_possible_mask);
1209e7577c50SRusty Russell 	cpu_singlethread_map = cpumask_of(singlethread_cpu);
12101da177e4SLinus Torvalds 	hotcpu_notifier(workqueue_cpu_callback, 0);
12111da177e4SLinus Torvalds 	keventd_wq = create_workqueue("events");
12121da177e4SLinus Torvalds 	BUG_ON(!keventd_wq);
12131da177e4SLinus Torvalds }
1214