xref: /linux-6.15/kernel/workqueue.c (revision e1d8aa9f)
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*e1d8aa9fSFrederic Weisbecker #include <trace/workqueue.h>
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds /*
39f756d5e2SNathan Lynch  * The per-CPU workqueue (if single thread, we always use the first
40f756d5e2SNathan Lynch  * possible cpu).
411da177e4SLinus Torvalds  */
421da177e4SLinus Torvalds struct cpu_workqueue_struct {
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds 	spinlock_t lock;
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds 	struct list_head worklist;
471da177e4SLinus Torvalds 	wait_queue_head_t more_work;
483af24433SOleg Nesterov 	struct work_struct *current_work;
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds 	struct workqueue_struct *wq;
5136c8b586SIngo Molnar 	struct task_struct *thread;
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds 	int run_depth;		/* Detect run_workqueue() recursion depth */
541da177e4SLinus Torvalds } ____cacheline_aligned;
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds /*
571da177e4SLinus Torvalds  * The externally visible workqueue abstraction is an array of
581da177e4SLinus Torvalds  * per-CPU workqueues:
591da177e4SLinus Torvalds  */
601da177e4SLinus Torvalds struct workqueue_struct {
6189ada679SChristoph Lameter 	struct cpu_workqueue_struct *cpu_wq;
62cce1a165SOleg Nesterov 	struct list_head list;
631da177e4SLinus Torvalds 	const char *name;
64cce1a165SOleg Nesterov 	int singlethread;
65319c2a98SOleg Nesterov 	int freezeable;		/* Freeze threads during suspend */
660d557dc9SHeiko Carstens 	int rt;
674e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
684e6045f1SJohannes Berg 	struct lockdep_map lockdep_map;
694e6045f1SJohannes Berg #endif
701da177e4SLinus Torvalds };
711da177e4SLinus Torvalds 
7295402b38SGautham R Shenoy /* Serializes the accesses to the list of workqueues. */
7395402b38SGautham R Shenoy static DEFINE_SPINLOCK(workqueue_lock);
741da177e4SLinus Torvalds static LIST_HEAD(workqueues);
751da177e4SLinus Torvalds 
763af24433SOleg Nesterov static int singlethread_cpu __read_mostly;
77e7577c50SRusty Russell static const struct cpumask *cpu_singlethread_map __read_mostly;
7814441960SOleg Nesterov /*
7914441960SOleg Nesterov  * _cpu_down() first removes CPU from cpu_online_map, then CPU_DEAD
8014441960SOleg Nesterov  * flushes cwq->worklist. This means that flush_workqueue/wait_on_work
8114441960SOleg Nesterov  * which comes in between can't use for_each_online_cpu(). We could
8214441960SOleg Nesterov  * use cpu_possible_map, the cpumask below is more a documentation
8314441960SOleg Nesterov  * than optimization.
8414441960SOleg Nesterov  */
85e7577c50SRusty Russell static cpumask_var_t cpu_populated_map __read_mostly;
86f756d5e2SNathan Lynch 
871da177e4SLinus Torvalds /* If it's single threaded, it isn't in the list of workqueues. */
886cc88bc4SDavid Howells static inline int is_wq_single_threaded(struct workqueue_struct *wq)
891da177e4SLinus Torvalds {
90cce1a165SOleg Nesterov 	return wq->singlethread;
911da177e4SLinus Torvalds }
921da177e4SLinus Torvalds 
93e7577c50SRusty Russell static const struct cpumask *wq_cpu_map(struct workqueue_struct *wq)
94b1f4ec17SOleg Nesterov {
956cc88bc4SDavid Howells 	return is_wq_single_threaded(wq)
96e7577c50SRusty Russell 		? cpu_singlethread_map : cpu_populated_map;
97b1f4ec17SOleg Nesterov }
98b1f4ec17SOleg Nesterov 
99a848e3b6SOleg Nesterov static
100a848e3b6SOleg Nesterov struct cpu_workqueue_struct *wq_per_cpu(struct workqueue_struct *wq, int cpu)
101a848e3b6SOleg Nesterov {
1026cc88bc4SDavid Howells 	if (unlikely(is_wq_single_threaded(wq)))
103a848e3b6SOleg Nesterov 		cpu = singlethread_cpu;
104a848e3b6SOleg Nesterov 	return per_cpu_ptr(wq->cpu_wq, cpu);
105a848e3b6SOleg Nesterov }
106a848e3b6SOleg Nesterov 
1074594bf15SDavid Howells /*
1084594bf15SDavid Howells  * Set the workqueue on which a work item is to be run
1094594bf15SDavid Howells  * - Must *only* be called if the pending flag is set
1104594bf15SDavid Howells  */
111ed7c0feeSOleg Nesterov static inline void set_wq_data(struct work_struct *work,
112ed7c0feeSOleg Nesterov 				struct cpu_workqueue_struct *cwq)
113365970a1SDavid Howells {
1144594bf15SDavid Howells 	unsigned long new;
115365970a1SDavid Howells 
1164594bf15SDavid Howells 	BUG_ON(!work_pending(work));
1174594bf15SDavid Howells 
118ed7c0feeSOleg Nesterov 	new = (unsigned long) cwq | (1UL << WORK_STRUCT_PENDING);
119a08727baSLinus Torvalds 	new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work);
120a08727baSLinus Torvalds 	atomic_long_set(&work->data, new);
121365970a1SDavid Howells }
122365970a1SDavid Howells 
123ed7c0feeSOleg Nesterov static inline
124ed7c0feeSOleg Nesterov struct cpu_workqueue_struct *get_wq_data(struct work_struct *work)
125365970a1SDavid Howells {
126a08727baSLinus Torvalds 	return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK);
127365970a1SDavid Howells }
128365970a1SDavid Howells 
129*e1d8aa9fSFrederic Weisbecker DEFINE_TRACE(workqueue_insertion);
130*e1d8aa9fSFrederic Weisbecker 
131b89deed3SOleg Nesterov static void insert_work(struct cpu_workqueue_struct *cwq,
1321a4d9b0aSOleg Nesterov 			struct work_struct *work, struct list_head *head)
133b89deed3SOleg Nesterov {
134*e1d8aa9fSFrederic Weisbecker 	trace_workqueue_insertion(cwq->thread, work);
135*e1d8aa9fSFrederic Weisbecker 
136b89deed3SOleg Nesterov 	set_wq_data(work, cwq);
1376e84d644SOleg Nesterov 	/*
1386e84d644SOleg Nesterov 	 * Ensure that we get the right work->data if we see the
1396e84d644SOleg Nesterov 	 * result of list_add() below, see try_to_grab_pending().
1406e84d644SOleg Nesterov 	 */
1416e84d644SOleg Nesterov 	smp_wmb();
1421a4d9b0aSOleg Nesterov 	list_add_tail(&work->entry, head);
143b89deed3SOleg Nesterov 	wake_up(&cwq->more_work);
144b89deed3SOleg Nesterov }
145b89deed3SOleg Nesterov 
1461da177e4SLinus Torvalds static void __queue_work(struct cpu_workqueue_struct *cwq,
1471da177e4SLinus Torvalds 			 struct work_struct *work)
1481da177e4SLinus Torvalds {
1491da177e4SLinus Torvalds 	unsigned long flags;
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 	spin_lock_irqsave(&cwq->lock, flags);
1521a4d9b0aSOleg Nesterov 	insert_work(cwq, work, &cwq->worklist);
1531da177e4SLinus Torvalds 	spin_unlock_irqrestore(&cwq->lock, flags);
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds 
1560fcb78c2SRolf Eike Beer /**
1570fcb78c2SRolf Eike Beer  * queue_work - queue work on a workqueue
1580fcb78c2SRolf Eike Beer  * @wq: workqueue to use
1590fcb78c2SRolf Eike Beer  * @work: work to queue
1600fcb78c2SRolf Eike Beer  *
161057647fcSAlan Stern  * Returns 0 if @work was already on a queue, non-zero otherwise.
1621da177e4SLinus Torvalds  *
16300dfcaf7SOleg Nesterov  * We queue the work to the CPU on which it was submitted, but if the CPU dies
16400dfcaf7SOleg Nesterov  * it can be processed by another CPU.
1651da177e4SLinus Torvalds  */
1667ad5b3a5SHarvey Harrison int queue_work(struct workqueue_struct *wq, struct work_struct *work)
1671da177e4SLinus Torvalds {
168ef1ca236SOleg Nesterov 	int ret;
1691da177e4SLinus Torvalds 
170ef1ca236SOleg Nesterov 	ret = queue_work_on(get_cpu(), wq, work);
171a848e3b6SOleg Nesterov 	put_cpu();
172ef1ca236SOleg Nesterov 
1731da177e4SLinus Torvalds 	return ret;
1741da177e4SLinus Torvalds }
175ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_work);
1761da177e4SLinus Torvalds 
177c1a220e7SZhang Rui /**
178c1a220e7SZhang Rui  * queue_work_on - queue work on specific cpu
179c1a220e7SZhang Rui  * @cpu: CPU number to execute work on
180c1a220e7SZhang Rui  * @wq: workqueue to use
181c1a220e7SZhang Rui  * @work: work to queue
182c1a220e7SZhang Rui  *
183c1a220e7SZhang Rui  * Returns 0 if @work was already on a queue, non-zero otherwise.
184c1a220e7SZhang Rui  *
185c1a220e7SZhang Rui  * We queue the work to a specific CPU, the caller must ensure it
186c1a220e7SZhang Rui  * can't go away.
187c1a220e7SZhang Rui  */
188c1a220e7SZhang Rui int
189c1a220e7SZhang Rui queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
190c1a220e7SZhang Rui {
191c1a220e7SZhang Rui 	int ret = 0;
192c1a220e7SZhang Rui 
193c1a220e7SZhang Rui 	if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
194c1a220e7SZhang Rui 		BUG_ON(!list_empty(&work->entry));
195c1a220e7SZhang Rui 		__queue_work(wq_per_cpu(wq, cpu), work);
196c1a220e7SZhang Rui 		ret = 1;
197c1a220e7SZhang Rui 	}
198c1a220e7SZhang Rui 	return ret;
199c1a220e7SZhang Rui }
200c1a220e7SZhang Rui EXPORT_SYMBOL_GPL(queue_work_on);
201c1a220e7SZhang Rui 
2026d141c3fSLi Zefan static void delayed_work_timer_fn(unsigned long __data)
2031da177e4SLinus Torvalds {
20452bad64dSDavid Howells 	struct delayed_work *dwork = (struct delayed_work *)__data;
205ed7c0feeSOleg Nesterov 	struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
206ed7c0feeSOleg Nesterov 	struct workqueue_struct *wq = cwq->wq;
2071da177e4SLinus Torvalds 
208a848e3b6SOleg Nesterov 	__queue_work(wq_per_cpu(wq, smp_processor_id()), &dwork->work);
2091da177e4SLinus Torvalds }
2101da177e4SLinus Torvalds 
2110fcb78c2SRolf Eike Beer /**
2120fcb78c2SRolf Eike Beer  * queue_delayed_work - queue work on a workqueue after delay
2130fcb78c2SRolf Eike Beer  * @wq: workqueue to use
214af9997e4SRandy Dunlap  * @dwork: delayable work to queue
2150fcb78c2SRolf Eike Beer  * @delay: number of jiffies to wait before queueing
2160fcb78c2SRolf Eike Beer  *
217057647fcSAlan Stern  * Returns 0 if @work was already on a queue, non-zero otherwise.
2180fcb78c2SRolf Eike Beer  */
2197ad5b3a5SHarvey Harrison int queue_delayed_work(struct workqueue_struct *wq,
22052bad64dSDavid Howells 			struct delayed_work *dwork, unsigned long delay)
2211da177e4SLinus Torvalds {
22252bad64dSDavid Howells 	if (delay == 0)
22363bc0362SOleg Nesterov 		return queue_work(wq, &dwork->work);
2241da177e4SLinus Torvalds 
22563bc0362SOleg Nesterov 	return queue_delayed_work_on(-1, wq, dwork, delay);
2261da177e4SLinus Torvalds }
227ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work);
2281da177e4SLinus Torvalds 
2290fcb78c2SRolf Eike Beer /**
2300fcb78c2SRolf Eike Beer  * queue_delayed_work_on - queue work on specific CPU after delay
2310fcb78c2SRolf Eike Beer  * @cpu: CPU number to execute work on
2320fcb78c2SRolf Eike Beer  * @wq: workqueue to use
233af9997e4SRandy Dunlap  * @dwork: work to queue
2340fcb78c2SRolf Eike Beer  * @delay: number of jiffies to wait before queueing
2350fcb78c2SRolf Eike Beer  *
236057647fcSAlan Stern  * Returns 0 if @work was already on a queue, non-zero otherwise.
2370fcb78c2SRolf Eike Beer  */
2387a6bc1cdSVenkatesh Pallipadi int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
23952bad64dSDavid Howells 			struct delayed_work *dwork, unsigned long delay)
2407a6bc1cdSVenkatesh Pallipadi {
2417a6bc1cdSVenkatesh Pallipadi 	int ret = 0;
24252bad64dSDavid Howells 	struct timer_list *timer = &dwork->timer;
24352bad64dSDavid Howells 	struct work_struct *work = &dwork->work;
2447a6bc1cdSVenkatesh Pallipadi 
245a08727baSLinus Torvalds 	if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
2467a6bc1cdSVenkatesh Pallipadi 		BUG_ON(timer_pending(timer));
2477a6bc1cdSVenkatesh Pallipadi 		BUG_ON(!list_empty(&work->entry));
2487a6bc1cdSVenkatesh Pallipadi 
2498a3e77ccSAndrew Liu 		timer_stats_timer_set_start_info(&dwork->timer);
2508a3e77ccSAndrew Liu 
251ed7c0feeSOleg Nesterov 		/* This stores cwq for the moment, for the timer_fn */
252a848e3b6SOleg Nesterov 		set_wq_data(work, wq_per_cpu(wq, raw_smp_processor_id()));
2537a6bc1cdSVenkatesh Pallipadi 		timer->expires = jiffies + delay;
25452bad64dSDavid Howells 		timer->data = (unsigned long)dwork;
2557a6bc1cdSVenkatesh Pallipadi 		timer->function = delayed_work_timer_fn;
25663bc0362SOleg Nesterov 
25763bc0362SOleg Nesterov 		if (unlikely(cpu >= 0))
2587a6bc1cdSVenkatesh Pallipadi 			add_timer_on(timer, cpu);
25963bc0362SOleg Nesterov 		else
26063bc0362SOleg Nesterov 			add_timer(timer);
2617a6bc1cdSVenkatesh Pallipadi 		ret = 1;
2627a6bc1cdSVenkatesh Pallipadi 	}
2637a6bc1cdSVenkatesh Pallipadi 	return ret;
2647a6bc1cdSVenkatesh Pallipadi }
265ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work_on);
2661da177e4SLinus Torvalds 
267*e1d8aa9fSFrederic Weisbecker DEFINE_TRACE(workqueue_execution);
268*e1d8aa9fSFrederic Weisbecker 
269858119e1SArjan van de Ven static void run_workqueue(struct cpu_workqueue_struct *cwq)
2701da177e4SLinus Torvalds {
271f293ea92SOleg Nesterov 	spin_lock_irq(&cwq->lock);
2721da177e4SLinus Torvalds 	cwq->run_depth++;
2731da177e4SLinus Torvalds 	if (cwq->run_depth > 3) {
2741da177e4SLinus Torvalds 		/* morton gets to eat his hat */
2751da177e4SLinus Torvalds 		printk("%s: recursion depth exceeded: %d\n",
276af1f16d0SHarvey Harrison 			__func__, cwq->run_depth);
2771da177e4SLinus Torvalds 		dump_stack();
2781da177e4SLinus Torvalds 	}
2791da177e4SLinus Torvalds 	while (!list_empty(&cwq->worklist)) {
2801da177e4SLinus Torvalds 		struct work_struct *work = list_entry(cwq->worklist.next,
2811da177e4SLinus Torvalds 						struct work_struct, entry);
2826bb49e59SDavid Howells 		work_func_t f = work->func;
2834e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
2844e6045f1SJohannes Berg 		/*
2854e6045f1SJohannes Berg 		 * It is permissible to free the struct work_struct
2864e6045f1SJohannes Berg 		 * from inside the function that is called from it,
2874e6045f1SJohannes Berg 		 * this we need to take into account for lockdep too.
2884e6045f1SJohannes Berg 		 * To avoid bogus "held lock freed" warnings as well
2894e6045f1SJohannes Berg 		 * as problems when looking into work->lockdep_map,
2904e6045f1SJohannes Berg 		 * make a copy and use that here.
2914e6045f1SJohannes Berg 		 */
2924e6045f1SJohannes Berg 		struct lockdep_map lockdep_map = work->lockdep_map;
2934e6045f1SJohannes Berg #endif
294*e1d8aa9fSFrederic Weisbecker 		trace_workqueue_execution(cwq->thread, work);
295b89deed3SOleg Nesterov 		cwq->current_work = work;
2961da177e4SLinus Torvalds 		list_del_init(cwq->worklist.next);
297f293ea92SOleg Nesterov 		spin_unlock_irq(&cwq->lock);
2981da177e4SLinus Torvalds 
299365970a1SDavid Howells 		BUG_ON(get_wq_data(work) != cwq);
30023b2e599SOleg Nesterov 		work_clear_pending(work);
3013295f0efSIngo Molnar 		lock_map_acquire(&cwq->wq->lockdep_map);
3023295f0efSIngo Molnar 		lock_map_acquire(&lockdep_map);
30365f27f38SDavid Howells 		f(work);
3043295f0efSIngo Molnar 		lock_map_release(&lockdep_map);
3053295f0efSIngo Molnar 		lock_map_release(&cwq->wq->lockdep_map);
3061da177e4SLinus Torvalds 
307d5abe669SPeter Zijlstra 		if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
308d5abe669SPeter Zijlstra 			printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
309d5abe669SPeter Zijlstra 					"%s/0x%08x/%d\n",
310d5abe669SPeter Zijlstra 					current->comm, preempt_count(),
311ba25f9dcSPavel Emelyanov 				       	task_pid_nr(current));
312d5abe669SPeter Zijlstra 			printk(KERN_ERR "    last function: ");
313d5abe669SPeter Zijlstra 			print_symbol("%s\n", (unsigned long)f);
314d5abe669SPeter Zijlstra 			debug_show_held_locks(current);
315d5abe669SPeter Zijlstra 			dump_stack();
316d5abe669SPeter Zijlstra 		}
317d5abe669SPeter Zijlstra 
318f293ea92SOleg Nesterov 		spin_lock_irq(&cwq->lock);
319b89deed3SOleg Nesterov 		cwq->current_work = NULL;
3201da177e4SLinus Torvalds 	}
3211da177e4SLinus Torvalds 	cwq->run_depth--;
322f293ea92SOleg Nesterov 	spin_unlock_irq(&cwq->lock);
3231da177e4SLinus Torvalds }
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds static int worker_thread(void *__cwq)
3261da177e4SLinus Torvalds {
3271da177e4SLinus Torvalds 	struct cpu_workqueue_struct *cwq = __cwq;
3283af24433SOleg Nesterov 	DEFINE_WAIT(wait);
3291da177e4SLinus Torvalds 
33083144186SRafael J. Wysocki 	if (cwq->wq->freezeable)
33183144186SRafael J. Wysocki 		set_freezable();
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 	set_user_nice(current, -5);
3341da177e4SLinus Torvalds 
3353af24433SOleg Nesterov 	for (;;) {
3363af24433SOleg Nesterov 		prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
33714441960SOleg Nesterov 		if (!freezing(current) &&
33814441960SOleg Nesterov 		    !kthread_should_stop() &&
33914441960SOleg Nesterov 		    list_empty(&cwq->worklist))
3401da177e4SLinus Torvalds 			schedule();
3413af24433SOleg Nesterov 		finish_wait(&cwq->more_work, &wait);
3421da177e4SLinus Torvalds 
34385f4186aSOleg Nesterov 		try_to_freeze();
34485f4186aSOleg Nesterov 
34514441960SOleg Nesterov 		if (kthread_should_stop())
3463af24433SOleg Nesterov 			break;
3473af24433SOleg Nesterov 
3481da177e4SLinus Torvalds 		run_workqueue(cwq);
3491da177e4SLinus Torvalds 	}
3503af24433SOleg Nesterov 
3511da177e4SLinus Torvalds 	return 0;
3521da177e4SLinus Torvalds }
3531da177e4SLinus Torvalds 
354fc2e4d70SOleg Nesterov struct wq_barrier {
355fc2e4d70SOleg Nesterov 	struct work_struct	work;
356fc2e4d70SOleg Nesterov 	struct completion	done;
357fc2e4d70SOleg Nesterov };
358fc2e4d70SOleg Nesterov 
359fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work)
360fc2e4d70SOleg Nesterov {
361fc2e4d70SOleg Nesterov 	struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
362fc2e4d70SOleg Nesterov 	complete(&barr->done);
363fc2e4d70SOleg Nesterov }
364fc2e4d70SOleg Nesterov 
36583c22520SOleg Nesterov static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
3661a4d9b0aSOleg Nesterov 			struct wq_barrier *barr, struct list_head *head)
367fc2e4d70SOleg Nesterov {
368fc2e4d70SOleg Nesterov 	INIT_WORK(&barr->work, wq_barrier_func);
369fc2e4d70SOleg Nesterov 	__set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work));
370fc2e4d70SOleg Nesterov 
371fc2e4d70SOleg Nesterov 	init_completion(&barr->done);
37283c22520SOleg Nesterov 
3731a4d9b0aSOleg Nesterov 	insert_work(cwq, &barr->work, head);
374fc2e4d70SOleg Nesterov }
375fc2e4d70SOleg Nesterov 
37614441960SOleg Nesterov static int flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
3771da177e4SLinus Torvalds {
37814441960SOleg Nesterov 	int active;
37914441960SOleg Nesterov 
3801da177e4SLinus Torvalds 	if (cwq->thread == current) {
3811da177e4SLinus Torvalds 		/*
3821da177e4SLinus Torvalds 		 * Probably keventd trying to flush its own queue. So simply run
3831da177e4SLinus Torvalds 		 * it by hand rather than deadlocking.
3841da177e4SLinus Torvalds 		 */
3851da177e4SLinus Torvalds 		run_workqueue(cwq);
38614441960SOleg Nesterov 		active = 1;
3871da177e4SLinus Torvalds 	} else {
388fc2e4d70SOleg Nesterov 		struct wq_barrier barr;
3891da177e4SLinus Torvalds 
39014441960SOleg Nesterov 		active = 0;
39183c22520SOleg Nesterov 		spin_lock_irq(&cwq->lock);
39283c22520SOleg Nesterov 		if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
3931a4d9b0aSOleg Nesterov 			insert_wq_barrier(cwq, &barr, &cwq->worklist);
39483c22520SOleg Nesterov 			active = 1;
39583c22520SOleg Nesterov 		}
39683c22520SOleg Nesterov 		spin_unlock_irq(&cwq->lock);
3971da177e4SLinus Torvalds 
398d721304dSOleg Nesterov 		if (active)
399fc2e4d70SOleg Nesterov 			wait_for_completion(&barr.done);
4001da177e4SLinus Torvalds 	}
40114441960SOleg Nesterov 
40214441960SOleg Nesterov 	return active;
40383c22520SOleg Nesterov }
4041da177e4SLinus Torvalds 
4050fcb78c2SRolf Eike Beer /**
4061da177e4SLinus Torvalds  * flush_workqueue - ensure that any scheduled work has run to completion.
4070fcb78c2SRolf Eike Beer  * @wq: workqueue to flush
4081da177e4SLinus Torvalds  *
4091da177e4SLinus Torvalds  * Forces execution of the workqueue and blocks until its completion.
4101da177e4SLinus Torvalds  * This is typically used in driver shutdown handlers.
4111da177e4SLinus Torvalds  *
412fc2e4d70SOleg Nesterov  * We sleep until all works which were queued on entry have been handled,
413fc2e4d70SOleg Nesterov  * but we are not livelocked by new incoming ones.
4141da177e4SLinus Torvalds  *
4151da177e4SLinus Torvalds  * This function used to run the workqueues itself.  Now we just wait for the
4161da177e4SLinus Torvalds  * helper threads to do it.
4171da177e4SLinus Torvalds  */
4187ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq)
4191da177e4SLinus Torvalds {
420e7577c50SRusty Russell 	const struct cpumask *cpu_map = wq_cpu_map(wq);
421cce1a165SOleg Nesterov 	int cpu;
422b1f4ec17SOleg Nesterov 
423f293ea92SOleg Nesterov 	might_sleep();
4243295f0efSIngo Molnar 	lock_map_acquire(&wq->lockdep_map);
4253295f0efSIngo Molnar 	lock_map_release(&wq->lockdep_map);
426363ab6f1SMike Travis 	for_each_cpu_mask_nr(cpu, *cpu_map)
42789ada679SChristoph Lameter 		flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
4281da177e4SLinus Torvalds }
429ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(flush_workqueue);
4301da177e4SLinus Torvalds 
431db700897SOleg Nesterov /**
432db700897SOleg Nesterov  * flush_work - block until a work_struct's callback has terminated
433db700897SOleg Nesterov  * @work: the work which is to be flushed
434db700897SOleg Nesterov  *
435a67da70dSOleg Nesterov  * Returns false if @work has already terminated.
436a67da70dSOleg Nesterov  *
437db700897SOleg Nesterov  * It is expected that, prior to calling flush_work(), the caller has
438db700897SOleg Nesterov  * arranged for the work to not be requeued, otherwise it doesn't make
439db700897SOleg Nesterov  * sense to use this function.
440db700897SOleg Nesterov  */
441db700897SOleg Nesterov int flush_work(struct work_struct *work)
442db700897SOleg Nesterov {
443db700897SOleg Nesterov 	struct cpu_workqueue_struct *cwq;
444db700897SOleg Nesterov 	struct list_head *prev;
445db700897SOleg Nesterov 	struct wq_barrier barr;
446db700897SOleg Nesterov 
447db700897SOleg Nesterov 	might_sleep();
448db700897SOleg Nesterov 	cwq = get_wq_data(work);
449db700897SOleg Nesterov 	if (!cwq)
450db700897SOleg Nesterov 		return 0;
451db700897SOleg Nesterov 
4523295f0efSIngo Molnar 	lock_map_acquire(&cwq->wq->lockdep_map);
4533295f0efSIngo Molnar 	lock_map_release(&cwq->wq->lockdep_map);
454a67da70dSOleg Nesterov 
455db700897SOleg Nesterov 	prev = NULL;
456db700897SOleg Nesterov 	spin_lock_irq(&cwq->lock);
457db700897SOleg Nesterov 	if (!list_empty(&work->entry)) {
458db700897SOleg Nesterov 		/*
459db700897SOleg Nesterov 		 * See the comment near try_to_grab_pending()->smp_rmb().
460db700897SOleg Nesterov 		 * If it was re-queued under us we are not going to wait.
461db700897SOleg Nesterov 		 */
462db700897SOleg Nesterov 		smp_rmb();
463db700897SOleg Nesterov 		if (unlikely(cwq != get_wq_data(work)))
464db700897SOleg Nesterov 			goto out;
465db700897SOleg Nesterov 		prev = &work->entry;
466db700897SOleg Nesterov 	} else {
467db700897SOleg Nesterov 		if (cwq->current_work != work)
468db700897SOleg Nesterov 			goto out;
469db700897SOleg Nesterov 		prev = &cwq->worklist;
470db700897SOleg Nesterov 	}
471db700897SOleg Nesterov 	insert_wq_barrier(cwq, &barr, prev->next);
472db700897SOleg Nesterov out:
473db700897SOleg Nesterov 	spin_unlock_irq(&cwq->lock);
474db700897SOleg Nesterov 	if (!prev)
475db700897SOleg Nesterov 		return 0;
476db700897SOleg Nesterov 
477db700897SOleg Nesterov 	wait_for_completion(&barr.done);
478db700897SOleg Nesterov 	return 1;
479db700897SOleg Nesterov }
480db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work);
481db700897SOleg Nesterov 
4826e84d644SOleg Nesterov /*
4831f1f642eSOleg Nesterov  * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
4846e84d644SOleg Nesterov  * so this work can't be re-armed in any way.
4856e84d644SOleg Nesterov  */
4866e84d644SOleg Nesterov static int try_to_grab_pending(struct work_struct *work)
4876e84d644SOleg Nesterov {
4886e84d644SOleg Nesterov 	struct cpu_workqueue_struct *cwq;
4891f1f642eSOleg Nesterov 	int ret = -1;
4906e84d644SOleg Nesterov 
4916e84d644SOleg Nesterov 	if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work)))
4921f1f642eSOleg Nesterov 		return 0;
4936e84d644SOleg Nesterov 
4946e84d644SOleg Nesterov 	/*
4956e84d644SOleg Nesterov 	 * The queueing is in progress, or it is already queued. Try to
4966e84d644SOleg Nesterov 	 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
4976e84d644SOleg Nesterov 	 */
4986e84d644SOleg Nesterov 
4996e84d644SOleg Nesterov 	cwq = get_wq_data(work);
5006e84d644SOleg Nesterov 	if (!cwq)
5016e84d644SOleg Nesterov 		return ret;
5026e84d644SOleg Nesterov 
5036e84d644SOleg Nesterov 	spin_lock_irq(&cwq->lock);
5046e84d644SOleg Nesterov 	if (!list_empty(&work->entry)) {
5056e84d644SOleg Nesterov 		/*
5066e84d644SOleg Nesterov 		 * This work is queued, but perhaps we locked the wrong cwq.
5076e84d644SOleg Nesterov 		 * In that case we must see the new value after rmb(), see
5086e84d644SOleg Nesterov 		 * insert_work()->wmb().
5096e84d644SOleg Nesterov 		 */
5106e84d644SOleg Nesterov 		smp_rmb();
5116e84d644SOleg Nesterov 		if (cwq == get_wq_data(work)) {
5126e84d644SOleg Nesterov 			list_del_init(&work->entry);
5136e84d644SOleg Nesterov 			ret = 1;
5146e84d644SOleg Nesterov 		}
5156e84d644SOleg Nesterov 	}
5166e84d644SOleg Nesterov 	spin_unlock_irq(&cwq->lock);
5176e84d644SOleg Nesterov 
5186e84d644SOleg Nesterov 	return ret;
5196e84d644SOleg Nesterov }
5206e84d644SOleg Nesterov 
5216e84d644SOleg Nesterov static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq,
522b89deed3SOleg Nesterov 				struct work_struct *work)
523b89deed3SOleg Nesterov {
524b89deed3SOleg Nesterov 	struct wq_barrier barr;
525b89deed3SOleg Nesterov 	int running = 0;
526b89deed3SOleg Nesterov 
527b89deed3SOleg Nesterov 	spin_lock_irq(&cwq->lock);
528b89deed3SOleg Nesterov 	if (unlikely(cwq->current_work == work)) {
5291a4d9b0aSOleg Nesterov 		insert_wq_barrier(cwq, &barr, cwq->worklist.next);
530b89deed3SOleg Nesterov 		running = 1;
531b89deed3SOleg Nesterov 	}
532b89deed3SOleg Nesterov 	spin_unlock_irq(&cwq->lock);
533b89deed3SOleg Nesterov 
5343af24433SOleg Nesterov 	if (unlikely(running))
535b89deed3SOleg Nesterov 		wait_for_completion(&barr.done);
536b89deed3SOleg Nesterov }
537b89deed3SOleg Nesterov 
5386e84d644SOleg Nesterov static void wait_on_work(struct work_struct *work)
539b89deed3SOleg Nesterov {
540b89deed3SOleg Nesterov 	struct cpu_workqueue_struct *cwq;
54128e53bddSOleg Nesterov 	struct workqueue_struct *wq;
542e7577c50SRusty Russell 	const struct cpumask *cpu_map;
543b1f4ec17SOleg Nesterov 	int cpu;
544b89deed3SOleg Nesterov 
545f293ea92SOleg Nesterov 	might_sleep();
546f293ea92SOleg Nesterov 
5473295f0efSIngo Molnar 	lock_map_acquire(&work->lockdep_map);
5483295f0efSIngo Molnar 	lock_map_release(&work->lockdep_map);
5494e6045f1SJohannes Berg 
550b89deed3SOleg Nesterov 	cwq = get_wq_data(work);
551b89deed3SOleg Nesterov 	if (!cwq)
5523af24433SOleg Nesterov 		return;
553b89deed3SOleg Nesterov 
55428e53bddSOleg Nesterov 	wq = cwq->wq;
55528e53bddSOleg Nesterov 	cpu_map = wq_cpu_map(wq);
55628e53bddSOleg Nesterov 
557363ab6f1SMike Travis 	for_each_cpu_mask_nr(cpu, *cpu_map)
5586e84d644SOleg Nesterov 		wait_on_cpu_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
5596e84d644SOleg Nesterov }
5606e84d644SOleg Nesterov 
5611f1f642eSOleg Nesterov static int __cancel_work_timer(struct work_struct *work,
5621f1f642eSOleg Nesterov 				struct timer_list* timer)
5631f1f642eSOleg Nesterov {
5641f1f642eSOleg Nesterov 	int ret;
5651f1f642eSOleg Nesterov 
5661f1f642eSOleg Nesterov 	do {
5671f1f642eSOleg Nesterov 		ret = (timer && likely(del_timer(timer)));
5681f1f642eSOleg Nesterov 		if (!ret)
5691f1f642eSOleg Nesterov 			ret = try_to_grab_pending(work);
5701f1f642eSOleg Nesterov 		wait_on_work(work);
5711f1f642eSOleg Nesterov 	} while (unlikely(ret < 0));
5721f1f642eSOleg Nesterov 
5731f1f642eSOleg Nesterov 	work_clear_pending(work);
5741f1f642eSOleg Nesterov 	return ret;
5751f1f642eSOleg Nesterov }
5761f1f642eSOleg Nesterov 
5776e84d644SOleg Nesterov /**
5786e84d644SOleg Nesterov  * cancel_work_sync - block until a work_struct's callback has terminated
5796e84d644SOleg Nesterov  * @work: the work which is to be flushed
5806e84d644SOleg Nesterov  *
5811f1f642eSOleg Nesterov  * Returns true if @work was pending.
5821f1f642eSOleg Nesterov  *
5836e84d644SOleg Nesterov  * cancel_work_sync() will cancel the work if it is queued. If the work's
5846e84d644SOleg Nesterov  * callback appears to be running, cancel_work_sync() will block until it
5856e84d644SOleg Nesterov  * has completed.
5866e84d644SOleg Nesterov  *
5876e84d644SOleg Nesterov  * It is possible to use this function if the work re-queues itself. It can
5886e84d644SOleg Nesterov  * cancel the work even if it migrates to another workqueue, however in that
5896e84d644SOleg Nesterov  * case it only guarantees that work->func() has completed on the last queued
5906e84d644SOleg Nesterov  * workqueue.
5916e84d644SOleg Nesterov  *
5926e84d644SOleg Nesterov  * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
5936e84d644SOleg Nesterov  * pending, otherwise it goes into a busy-wait loop until the timer expires.
5946e84d644SOleg Nesterov  *
5956e84d644SOleg Nesterov  * The caller must ensure that workqueue_struct on which this work was last
5966e84d644SOleg Nesterov  * queued can't be destroyed before this function returns.
5976e84d644SOleg Nesterov  */
5981f1f642eSOleg Nesterov int cancel_work_sync(struct work_struct *work)
5996e84d644SOleg Nesterov {
6001f1f642eSOleg Nesterov 	return __cancel_work_timer(work, NULL);
601b89deed3SOleg Nesterov }
60228e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync);
603b89deed3SOleg Nesterov 
6046e84d644SOleg Nesterov /**
605f5a421a4SOleg Nesterov  * cancel_delayed_work_sync - reliably kill off a delayed work.
6066e84d644SOleg Nesterov  * @dwork: the delayed work struct
6076e84d644SOleg Nesterov  *
6081f1f642eSOleg Nesterov  * Returns true if @dwork was pending.
6091f1f642eSOleg Nesterov  *
6106e84d644SOleg Nesterov  * It is possible to use this function if @dwork rearms itself via queue_work()
6116e84d644SOleg Nesterov  * or queue_delayed_work(). See also the comment for cancel_work_sync().
6126e84d644SOleg Nesterov  */
6131f1f642eSOleg Nesterov int cancel_delayed_work_sync(struct delayed_work *dwork)
6146e84d644SOleg Nesterov {
6151f1f642eSOleg Nesterov 	return __cancel_work_timer(&dwork->work, &dwork->timer);
6166e84d644SOleg Nesterov }
617f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync);
6181da177e4SLinus Torvalds 
6196e84d644SOleg Nesterov static struct workqueue_struct *keventd_wq __read_mostly;
6201da177e4SLinus Torvalds 
6210fcb78c2SRolf Eike Beer /**
6220fcb78c2SRolf Eike Beer  * schedule_work - put work task in global workqueue
6230fcb78c2SRolf Eike Beer  * @work: job to be done
6240fcb78c2SRolf Eike Beer  *
6250fcb78c2SRolf Eike Beer  * This puts a job in the kernel-global workqueue.
6260fcb78c2SRolf Eike Beer  */
6277ad5b3a5SHarvey Harrison int schedule_work(struct work_struct *work)
6281da177e4SLinus Torvalds {
6291da177e4SLinus Torvalds 	return queue_work(keventd_wq, work);
6301da177e4SLinus Torvalds }
631ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_work);
6321da177e4SLinus Torvalds 
633c1a220e7SZhang Rui /*
634c1a220e7SZhang Rui  * schedule_work_on - put work task on a specific cpu
635c1a220e7SZhang Rui  * @cpu: cpu to put the work task on
636c1a220e7SZhang Rui  * @work: job to be done
637c1a220e7SZhang Rui  *
638c1a220e7SZhang Rui  * This puts a job on a specific cpu
639c1a220e7SZhang Rui  */
640c1a220e7SZhang Rui int schedule_work_on(int cpu, struct work_struct *work)
641c1a220e7SZhang Rui {
642c1a220e7SZhang Rui 	return queue_work_on(cpu, keventd_wq, work);
643c1a220e7SZhang Rui }
644c1a220e7SZhang Rui EXPORT_SYMBOL(schedule_work_on);
645c1a220e7SZhang Rui 
6460fcb78c2SRolf Eike Beer /**
6470fcb78c2SRolf Eike Beer  * schedule_delayed_work - put work task in global workqueue after delay
64852bad64dSDavid Howells  * @dwork: job to be done
64952bad64dSDavid Howells  * @delay: number of jiffies to wait or 0 for immediate execution
6500fcb78c2SRolf Eike Beer  *
6510fcb78c2SRolf Eike Beer  * After waiting for a given time this puts a job in the kernel-global
6520fcb78c2SRolf Eike Beer  * workqueue.
6530fcb78c2SRolf Eike Beer  */
6547ad5b3a5SHarvey Harrison int schedule_delayed_work(struct delayed_work *dwork,
65582f67cd9SIngo Molnar 					unsigned long delay)
6561da177e4SLinus Torvalds {
65752bad64dSDavid Howells 	return queue_delayed_work(keventd_wq, dwork, delay);
6581da177e4SLinus Torvalds }
659ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work);
6601da177e4SLinus Torvalds 
6610fcb78c2SRolf Eike Beer /**
6620fcb78c2SRolf Eike Beer  * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
6630fcb78c2SRolf Eike Beer  * @cpu: cpu to use
66452bad64dSDavid Howells  * @dwork: job to be done
6650fcb78c2SRolf Eike Beer  * @delay: number of jiffies to wait
6660fcb78c2SRolf Eike Beer  *
6670fcb78c2SRolf Eike Beer  * After waiting for a given time this puts a job in the kernel-global
6680fcb78c2SRolf Eike Beer  * workqueue on the specified CPU.
6690fcb78c2SRolf Eike Beer  */
6701da177e4SLinus Torvalds int schedule_delayed_work_on(int cpu,
67152bad64dSDavid Howells 			struct delayed_work *dwork, unsigned long delay)
6721da177e4SLinus Torvalds {
67352bad64dSDavid Howells 	return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
6741da177e4SLinus Torvalds }
675ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work_on);
6761da177e4SLinus Torvalds 
677b6136773SAndrew Morton /**
678b6136773SAndrew Morton  * schedule_on_each_cpu - call a function on each online CPU from keventd
679b6136773SAndrew Morton  * @func: the function to call
680b6136773SAndrew Morton  *
681b6136773SAndrew Morton  * Returns zero on success.
682b6136773SAndrew Morton  * Returns -ve errno on failure.
683b6136773SAndrew Morton  *
684b6136773SAndrew Morton  * schedule_on_each_cpu() is very slow.
685b6136773SAndrew Morton  */
68665f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func)
68715316ba8SChristoph Lameter {
68815316ba8SChristoph Lameter 	int cpu;
689b6136773SAndrew Morton 	struct work_struct *works;
69015316ba8SChristoph Lameter 
691b6136773SAndrew Morton 	works = alloc_percpu(struct work_struct);
692b6136773SAndrew Morton 	if (!works)
69315316ba8SChristoph Lameter 		return -ENOMEM;
694b6136773SAndrew Morton 
69595402b38SGautham R Shenoy 	get_online_cpus();
69615316ba8SChristoph Lameter 	for_each_online_cpu(cpu) {
6979bfb1839SIngo Molnar 		struct work_struct *work = per_cpu_ptr(works, cpu);
6989bfb1839SIngo Molnar 
6999bfb1839SIngo Molnar 		INIT_WORK(work, func);
7008de6d308SOleg Nesterov 		schedule_work_on(cpu, work);
70115316ba8SChristoph Lameter 	}
7028616a89aSOleg Nesterov 	for_each_online_cpu(cpu)
7038616a89aSOleg Nesterov 		flush_work(per_cpu_ptr(works, cpu));
70495402b38SGautham R Shenoy 	put_online_cpus();
705b6136773SAndrew Morton 	free_percpu(works);
70615316ba8SChristoph Lameter 	return 0;
70715316ba8SChristoph Lameter }
70815316ba8SChristoph Lameter 
7091da177e4SLinus Torvalds void flush_scheduled_work(void)
7101da177e4SLinus Torvalds {
7111da177e4SLinus Torvalds 	flush_workqueue(keventd_wq);
7121da177e4SLinus Torvalds }
713ae90dd5dSDave Jones EXPORT_SYMBOL(flush_scheduled_work);
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds /**
7161fa44ecaSJames Bottomley  * execute_in_process_context - reliably execute the routine with user context
7171fa44ecaSJames Bottomley  * @fn:		the function to execute
7181fa44ecaSJames Bottomley  * @ew:		guaranteed storage for the execute work structure (must
7191fa44ecaSJames Bottomley  *		be available when the work executes)
7201fa44ecaSJames Bottomley  *
7211fa44ecaSJames Bottomley  * Executes the function immediately if process context is available,
7221fa44ecaSJames Bottomley  * otherwise schedules the function for delayed execution.
7231fa44ecaSJames Bottomley  *
7241fa44ecaSJames Bottomley  * Returns:	0 - function was executed
7251fa44ecaSJames Bottomley  *		1 - function was scheduled for execution
7261fa44ecaSJames Bottomley  */
72765f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew)
7281fa44ecaSJames Bottomley {
7291fa44ecaSJames Bottomley 	if (!in_interrupt()) {
73065f27f38SDavid Howells 		fn(&ew->work);
7311fa44ecaSJames Bottomley 		return 0;
7321fa44ecaSJames Bottomley 	}
7331fa44ecaSJames Bottomley 
73465f27f38SDavid Howells 	INIT_WORK(&ew->work, fn);
7351fa44ecaSJames Bottomley 	schedule_work(&ew->work);
7361fa44ecaSJames Bottomley 
7371fa44ecaSJames Bottomley 	return 1;
7381fa44ecaSJames Bottomley }
7391fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context);
7401fa44ecaSJames Bottomley 
7411da177e4SLinus Torvalds int keventd_up(void)
7421da177e4SLinus Torvalds {
7431da177e4SLinus Torvalds 	return keventd_wq != NULL;
7441da177e4SLinus Torvalds }
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds int current_is_keventd(void)
7471da177e4SLinus Torvalds {
7481da177e4SLinus Torvalds 	struct cpu_workqueue_struct *cwq;
749d243769dSHugh Dickins 	int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
7501da177e4SLinus Torvalds 	int ret = 0;
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds 	BUG_ON(!keventd_wq);
7531da177e4SLinus Torvalds 
75489ada679SChristoph Lameter 	cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
7551da177e4SLinus Torvalds 	if (current == cwq->thread)
7561da177e4SLinus Torvalds 		ret = 1;
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds 	return ret;
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds }
7611da177e4SLinus Torvalds 
7623af24433SOleg Nesterov static struct cpu_workqueue_struct *
7633af24433SOleg Nesterov init_cpu_workqueue(struct workqueue_struct *wq, int cpu)
7641da177e4SLinus Torvalds {
76589ada679SChristoph Lameter 	struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
7663af24433SOleg Nesterov 
7673af24433SOleg Nesterov 	cwq->wq = wq;
7683af24433SOleg Nesterov 	spin_lock_init(&cwq->lock);
7693af24433SOleg Nesterov 	INIT_LIST_HEAD(&cwq->worklist);
7703af24433SOleg Nesterov 	init_waitqueue_head(&cwq->more_work);
7713af24433SOleg Nesterov 
7723af24433SOleg Nesterov 	return cwq;
7733af24433SOleg Nesterov }
7743af24433SOleg Nesterov 
775*e1d8aa9fSFrederic Weisbecker DEFINE_TRACE(workqueue_creation);
776*e1d8aa9fSFrederic Weisbecker 
7773af24433SOleg Nesterov static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
7783af24433SOleg Nesterov {
7790d557dc9SHeiko Carstens 	struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
7803af24433SOleg Nesterov 	struct workqueue_struct *wq = cwq->wq;
7816cc88bc4SDavid Howells 	const char *fmt = is_wq_single_threaded(wq) ? "%s" : "%s/%d";
7823af24433SOleg Nesterov 	struct task_struct *p;
7833af24433SOleg Nesterov 
7843af24433SOleg Nesterov 	p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu);
7853af24433SOleg Nesterov 	/*
7863af24433SOleg Nesterov 	 * Nobody can add the work_struct to this cwq,
7873af24433SOleg Nesterov 	 *	if (caller is __create_workqueue)
7883af24433SOleg Nesterov 	 *		nobody should see this wq
7893af24433SOleg Nesterov 	 *	else // caller is CPU_UP_PREPARE
7903af24433SOleg Nesterov 	 *		cpu is not on cpu_online_map
7913af24433SOleg Nesterov 	 * so we can abort safely.
7923af24433SOleg Nesterov 	 */
7933af24433SOleg Nesterov 	if (IS_ERR(p))
7943af24433SOleg Nesterov 		return PTR_ERR(p);
7950d557dc9SHeiko Carstens 	if (cwq->wq->rt)
7960d557dc9SHeiko Carstens 		sched_setscheduler_nocheck(p, SCHED_FIFO, &param);
7973af24433SOleg Nesterov 	cwq->thread = p;
7983af24433SOleg Nesterov 
799*e1d8aa9fSFrederic Weisbecker 	trace_workqueue_creation(cwq->thread, cpu);
800*e1d8aa9fSFrederic Weisbecker 
8013af24433SOleg Nesterov 	return 0;
8023af24433SOleg Nesterov }
8033af24433SOleg Nesterov 
80406ba38a9SOleg Nesterov static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
80506ba38a9SOleg Nesterov {
80606ba38a9SOleg Nesterov 	struct task_struct *p = cwq->thread;
80706ba38a9SOleg Nesterov 
80806ba38a9SOleg Nesterov 	if (p != NULL) {
80906ba38a9SOleg Nesterov 		if (cpu >= 0)
81006ba38a9SOleg Nesterov 			kthread_bind(p, cpu);
81106ba38a9SOleg Nesterov 		wake_up_process(p);
81206ba38a9SOleg Nesterov 	}
81306ba38a9SOleg Nesterov }
81406ba38a9SOleg Nesterov 
8154e6045f1SJohannes Berg struct workqueue_struct *__create_workqueue_key(const char *name,
8164e6045f1SJohannes Berg 						int singlethread,
8174e6045f1SJohannes Berg 						int freezeable,
8180d557dc9SHeiko Carstens 						int rt,
819eb13ba87SJohannes Berg 						struct lock_class_key *key,
820eb13ba87SJohannes Berg 						const char *lock_name)
8213af24433SOleg Nesterov {
8223af24433SOleg Nesterov 	struct workqueue_struct *wq;
8233af24433SOleg Nesterov 	struct cpu_workqueue_struct *cwq;
8243af24433SOleg Nesterov 	int err = 0, cpu;
8253af24433SOleg Nesterov 
8263af24433SOleg Nesterov 	wq = kzalloc(sizeof(*wq), GFP_KERNEL);
8273af24433SOleg Nesterov 	if (!wq)
8283af24433SOleg Nesterov 		return NULL;
8293af24433SOleg Nesterov 
8303af24433SOleg Nesterov 	wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
8313af24433SOleg Nesterov 	if (!wq->cpu_wq) {
8323af24433SOleg Nesterov 		kfree(wq);
8333af24433SOleg Nesterov 		return NULL;
8343af24433SOleg Nesterov 	}
8353af24433SOleg Nesterov 
8363af24433SOleg Nesterov 	wq->name = name;
837eb13ba87SJohannes Berg 	lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
838cce1a165SOleg Nesterov 	wq->singlethread = singlethread;
8393af24433SOleg Nesterov 	wq->freezeable = freezeable;
8400d557dc9SHeiko Carstens 	wq->rt = rt;
841cce1a165SOleg Nesterov 	INIT_LIST_HEAD(&wq->list);
8423af24433SOleg Nesterov 
8433af24433SOleg Nesterov 	if (singlethread) {
8443af24433SOleg Nesterov 		cwq = init_cpu_workqueue(wq, singlethread_cpu);
8453af24433SOleg Nesterov 		err = create_workqueue_thread(cwq, singlethread_cpu);
84606ba38a9SOleg Nesterov 		start_workqueue_thread(cwq, -1);
8473af24433SOleg Nesterov 	} else {
8483da1c84cSOleg Nesterov 		cpu_maps_update_begin();
8496af8bf3dSOleg Nesterov 		/*
8506af8bf3dSOleg Nesterov 		 * We must place this wq on list even if the code below fails.
8516af8bf3dSOleg Nesterov 		 * cpu_down(cpu) can remove cpu from cpu_populated_map before
8526af8bf3dSOleg Nesterov 		 * destroy_workqueue() takes the lock, in that case we leak
8536af8bf3dSOleg Nesterov 		 * cwq[cpu]->thread.
8546af8bf3dSOleg Nesterov 		 */
85595402b38SGautham R Shenoy 		spin_lock(&workqueue_lock);
8563af24433SOleg Nesterov 		list_add(&wq->list, &workqueues);
85795402b38SGautham R Shenoy 		spin_unlock(&workqueue_lock);
8586af8bf3dSOleg Nesterov 		/*
8596af8bf3dSOleg Nesterov 		 * We must initialize cwqs for each possible cpu even if we
8606af8bf3dSOleg Nesterov 		 * are going to call destroy_workqueue() finally. Otherwise
8616af8bf3dSOleg Nesterov 		 * cpu_up() can hit the uninitialized cwq once we drop the
8626af8bf3dSOleg Nesterov 		 * lock.
8636af8bf3dSOleg Nesterov 		 */
8643af24433SOleg Nesterov 		for_each_possible_cpu(cpu) {
8653af24433SOleg Nesterov 			cwq = init_cpu_workqueue(wq, cpu);
8663af24433SOleg Nesterov 			if (err || !cpu_online(cpu))
8673af24433SOleg Nesterov 				continue;
8683af24433SOleg Nesterov 			err = create_workqueue_thread(cwq, cpu);
86906ba38a9SOleg Nesterov 			start_workqueue_thread(cwq, cpu);
8703af24433SOleg Nesterov 		}
8713da1c84cSOleg Nesterov 		cpu_maps_update_done();
8723af24433SOleg Nesterov 	}
8733af24433SOleg Nesterov 
8743af24433SOleg Nesterov 	if (err) {
8753af24433SOleg Nesterov 		destroy_workqueue(wq);
8763af24433SOleg Nesterov 		wq = NULL;
8773af24433SOleg Nesterov 	}
8783af24433SOleg Nesterov 	return wq;
8793af24433SOleg Nesterov }
8804e6045f1SJohannes Berg EXPORT_SYMBOL_GPL(__create_workqueue_key);
8813af24433SOleg Nesterov 
882*e1d8aa9fSFrederic Weisbecker DEFINE_TRACE(workqueue_destruction);
883*e1d8aa9fSFrederic Weisbecker 
8841e35eaa2SOleg Nesterov static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq)
8853af24433SOleg Nesterov {
8863af24433SOleg Nesterov 	/*
8873da1c84cSOleg Nesterov 	 * Our caller is either destroy_workqueue() or CPU_POST_DEAD,
8883da1c84cSOleg Nesterov 	 * cpu_add_remove_lock protects cwq->thread.
8893af24433SOleg Nesterov 	 */
89014441960SOleg Nesterov 	if (cwq->thread == NULL)
89114441960SOleg Nesterov 		return;
89214441960SOleg Nesterov 
8933295f0efSIngo Molnar 	lock_map_acquire(&cwq->wq->lockdep_map);
8943295f0efSIngo Molnar 	lock_map_release(&cwq->wq->lockdep_map);
8954e6045f1SJohannes Berg 
89613c22168SOleg Nesterov 	flush_cpu_workqueue(cwq);
89714441960SOleg Nesterov 	/*
8983da1c84cSOleg Nesterov 	 * If the caller is CPU_POST_DEAD and cwq->worklist was not empty,
89913c22168SOleg Nesterov 	 * a concurrent flush_workqueue() can insert a barrier after us.
90013c22168SOleg Nesterov 	 * However, in that case run_workqueue() won't return and check
90113c22168SOleg Nesterov 	 * kthread_should_stop() until it flushes all work_struct's.
90214441960SOleg Nesterov 	 * When ->worklist becomes empty it is safe to exit because no
90314441960SOleg Nesterov 	 * more work_structs can be queued on this cwq: flush_workqueue
90414441960SOleg Nesterov 	 * checks list_empty(), and a "normal" queue_work() can't use
90514441960SOleg Nesterov 	 * a dead CPU.
90614441960SOleg Nesterov 	 */
907*e1d8aa9fSFrederic Weisbecker 	trace_workqueue_destruction(cwq->thread);
90814441960SOleg Nesterov 	kthread_stop(cwq->thread);
90914441960SOleg Nesterov 	cwq->thread = NULL;
9101da177e4SLinus Torvalds }
9111da177e4SLinus Torvalds 
9123af24433SOleg Nesterov /**
9133af24433SOleg Nesterov  * destroy_workqueue - safely terminate a workqueue
9143af24433SOleg Nesterov  * @wq: target workqueue
9153af24433SOleg Nesterov  *
9163af24433SOleg Nesterov  * Safely destroy a workqueue. All work currently pending will be done first.
9173af24433SOleg Nesterov  */
9183af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq)
9193af24433SOleg Nesterov {
920e7577c50SRusty Russell 	const struct cpumask *cpu_map = wq_cpu_map(wq);
9213af24433SOleg Nesterov 	int cpu;
9223af24433SOleg Nesterov 
9233da1c84cSOleg Nesterov 	cpu_maps_update_begin();
92495402b38SGautham R Shenoy 	spin_lock(&workqueue_lock);
9253af24433SOleg Nesterov 	list_del(&wq->list);
92695402b38SGautham R Shenoy 	spin_unlock(&workqueue_lock);
9273af24433SOleg Nesterov 
928363ab6f1SMike Travis 	for_each_cpu_mask_nr(cpu, *cpu_map)
9291e35eaa2SOleg Nesterov 		cleanup_workqueue_thread(per_cpu_ptr(wq->cpu_wq, cpu));
9303da1c84cSOleg Nesterov  	cpu_maps_update_done();
9313af24433SOleg Nesterov 
9323af24433SOleg Nesterov 	free_percpu(wq->cpu_wq);
9333af24433SOleg Nesterov 	kfree(wq);
9343af24433SOleg Nesterov }
9353af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue);
9363af24433SOleg Nesterov 
9379c7b216dSChandra Seetharaman static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
9381da177e4SLinus Torvalds 						unsigned long action,
9391da177e4SLinus Torvalds 						void *hcpu)
9401da177e4SLinus Torvalds {
9413af24433SOleg Nesterov 	unsigned int cpu = (unsigned long)hcpu;
9423af24433SOleg Nesterov 	struct cpu_workqueue_struct *cwq;
9431da177e4SLinus Torvalds 	struct workqueue_struct *wq;
9448448502cSOleg Nesterov 	int ret = NOTIFY_OK;
9451da177e4SLinus Torvalds 
9468bb78442SRafael J. Wysocki 	action &= ~CPU_TASKS_FROZEN;
9478bb78442SRafael J. Wysocki 
9481da177e4SLinus Torvalds 	switch (action) {
9493af24433SOleg Nesterov 	case CPU_UP_PREPARE:
950e7577c50SRusty Russell 		cpumask_set_cpu(cpu, cpu_populated_map);
9513af24433SOleg Nesterov 	}
9528448502cSOleg Nesterov undo:
9531da177e4SLinus Torvalds 	list_for_each_entry(wq, &workqueues, list) {
9543af24433SOleg Nesterov 		cwq = per_cpu_ptr(wq->cpu_wq, cpu);
9553af24433SOleg Nesterov 
9563af24433SOleg Nesterov 		switch (action) {
9573af24433SOleg Nesterov 		case CPU_UP_PREPARE:
9583af24433SOleg Nesterov 			if (!create_workqueue_thread(cwq, cpu))
9591da177e4SLinus Torvalds 				break;
96095402b38SGautham R Shenoy 			printk(KERN_ERR "workqueue [%s] for %i failed\n",
96195402b38SGautham R Shenoy 				wq->name, cpu);
9628448502cSOleg Nesterov 			action = CPU_UP_CANCELED;
9638448502cSOleg Nesterov 			ret = NOTIFY_BAD;
9648448502cSOleg Nesterov 			goto undo;
9651da177e4SLinus Torvalds 
9661da177e4SLinus Torvalds 		case CPU_ONLINE:
96706ba38a9SOleg Nesterov 			start_workqueue_thread(cwq, cpu);
9681da177e4SLinus Torvalds 			break;
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 		case CPU_UP_CANCELED:
97106ba38a9SOleg Nesterov 			start_workqueue_thread(cwq, -1);
9723da1c84cSOleg Nesterov 		case CPU_POST_DEAD:
9731e35eaa2SOleg Nesterov 			cleanup_workqueue_thread(cwq);
9741da177e4SLinus Torvalds 			break;
9751da177e4SLinus Torvalds 		}
9763af24433SOleg Nesterov 	}
9771da177e4SLinus Torvalds 
97800dfcaf7SOleg Nesterov 	switch (action) {
97900dfcaf7SOleg Nesterov 	case CPU_UP_CANCELED:
9803da1c84cSOleg Nesterov 	case CPU_POST_DEAD:
981e7577c50SRusty Russell 		cpumask_clear_cpu(cpu, cpu_populated_map);
98200dfcaf7SOleg Nesterov 	}
98300dfcaf7SOleg Nesterov 
9848448502cSOleg Nesterov 	return ret;
9851da177e4SLinus Torvalds }
9861da177e4SLinus Torvalds 
9872d3854a3SRusty Russell #ifdef CONFIG_SMP
9882d3854a3SRusty Russell struct work_for_cpu {
9892d3854a3SRusty Russell 	struct work_struct work;
9902d3854a3SRusty Russell 	long (*fn)(void *);
9912d3854a3SRusty Russell 	void *arg;
9922d3854a3SRusty Russell 	long ret;
9932d3854a3SRusty Russell };
9942d3854a3SRusty Russell 
9952d3854a3SRusty Russell static void do_work_for_cpu(struct work_struct *w)
9962d3854a3SRusty Russell {
9972d3854a3SRusty Russell 	struct work_for_cpu *wfc = container_of(w, struct work_for_cpu, work);
9982d3854a3SRusty Russell 
9992d3854a3SRusty Russell 	wfc->ret = wfc->fn(wfc->arg);
10002d3854a3SRusty Russell }
10012d3854a3SRusty Russell 
10022d3854a3SRusty Russell /**
10032d3854a3SRusty Russell  * work_on_cpu - run a function in user context on a particular cpu
10042d3854a3SRusty Russell  * @cpu: the cpu to run on
10052d3854a3SRusty Russell  * @fn: the function to run
10062d3854a3SRusty Russell  * @arg: the function arg
10072d3854a3SRusty Russell  *
10082d3854a3SRusty Russell  * This will return -EINVAL in the cpu is not online, or the return value
10092d3854a3SRusty Russell  * of @fn otherwise.
10102d3854a3SRusty Russell  */
10112d3854a3SRusty Russell long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
10122d3854a3SRusty Russell {
10132d3854a3SRusty Russell 	struct work_for_cpu wfc;
10142d3854a3SRusty Russell 
10152d3854a3SRusty Russell 	INIT_WORK(&wfc.work, do_work_for_cpu);
10162d3854a3SRusty Russell 	wfc.fn = fn;
10172d3854a3SRusty Russell 	wfc.arg = arg;
10182d3854a3SRusty Russell 	get_online_cpus();
10192d3854a3SRusty Russell 	if (unlikely(!cpu_online(cpu)))
10202d3854a3SRusty Russell 		wfc.ret = -EINVAL;
10212d3854a3SRusty Russell 	else {
10222d3854a3SRusty Russell 		schedule_work_on(cpu, &wfc.work);
10232d3854a3SRusty Russell 		flush_work(&wfc.work);
10242d3854a3SRusty Russell 	}
10252d3854a3SRusty Russell 	put_online_cpus();
10262d3854a3SRusty Russell 
10272d3854a3SRusty Russell 	return wfc.ret;
10282d3854a3SRusty Russell }
10292d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu);
10302d3854a3SRusty Russell #endif /* CONFIG_SMP */
10312d3854a3SRusty Russell 
1032c12920d1SOleg Nesterov void __init init_workqueues(void)
10331da177e4SLinus Torvalds {
1034e7577c50SRusty Russell 	alloc_cpumask_var(&cpu_populated_map, GFP_KERNEL);
1035e7577c50SRusty Russell 
1036e7577c50SRusty Russell 	cpumask_copy(cpu_populated_map, cpu_online_mask);
1037e7577c50SRusty Russell 	singlethread_cpu = cpumask_first(cpu_possible_mask);
1038e7577c50SRusty Russell 	cpu_singlethread_map = cpumask_of(singlethread_cpu);
10391da177e4SLinus Torvalds 	hotcpu_notifier(workqueue_cpu_callback, 0);
10401da177e4SLinus Torvalds 	keventd_wq = create_workqueue("events");
10411da177e4SLinus Torvalds 	BUG_ON(!keventd_wq);
10421da177e4SLinus Torvalds }
1043