blob: ee41cf857d5578da5dad1fa649aaadefbebe34ff [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/workqueue.c
3 *
4 * Generic mechanism for defining kernel helper threads for running
5 * arbitrary tasks in process context.
6 *
7 * Started by Ingo Molnar, Copyright (C) 2002
8 *
9 * Derived from the taskqueue/keventd code by:
10 *
11 * David Woodhouse <dwmw2@infradead.org>
12 * Andrew Morton <andrewm@uow.edu.au>
13 * Kai Petzke <wpp@marie.physik.tu-berlin.de>
14 * Theodore Ts'o <tytso@mit.edu>
Christoph Lameter89ada672005-10-30 15:01:59 -080015 *
Christoph Lametercde53532008-07-04 09:59:22 -070016 * Made to use alloc_percpu by Christoph Lameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18
19#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/sched.h>
22#include <linux/init.h>
23#include <linux/signal.h>
24#include <linux/completion.h>
25#include <linux/workqueue.h>
26#include <linux/slab.h>
27#include <linux/cpu.h>
28#include <linux/notifier.h>
29#include <linux/kthread.h>
James Bottomley1fa44ec2006-02-23 12:43:43 -060030#include <linux/hardirq.h>
Christoph Lameter46934022006-10-11 01:21:26 -070031#include <linux/mempolicy.h>
Rafael J. Wysocki341a5952006-12-06 20:34:49 -080032#include <linux/freezer.h>
Peter Zijlstrad5abe662006-12-06 20:37:26 -080033#include <linux/kallsyms.h>
34#include <linux/debug_locks.h>
Johannes Berg4e6045f2007-10-18 23:39:55 -070035#include <linux/lockdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/*
Nathan Lynchf756d5e2006-01-08 01:05:12 -080038 * The per-CPU workqueue (if single thread, we always use the first
39 * possible cpu).
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 */
41struct cpu_workqueue_struct {
42
43 spinlock_t lock;
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 struct list_head worklist;
46 wait_queue_head_t more_work;
Oleg Nesterov3af244332007-05-09 02:34:09 -070047 struct work_struct *current_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49 struct workqueue_struct *wq;
Ingo Molnar36c8b582006-07-03 00:25:41 -070050 struct task_struct *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52 int run_depth; /* Detect run_workqueue() recursion depth */
53} ____cacheline_aligned;
54
55/*
56 * The externally visible workqueue abstraction is an array of
57 * per-CPU workqueues:
58 */
59struct workqueue_struct {
Christoph Lameter89ada672005-10-30 15:01:59 -080060 struct cpu_workqueue_struct *cpu_wq;
Oleg Nesterovcce1a162007-05-09 02:34:13 -070061 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 const char *name;
Oleg Nesterovcce1a162007-05-09 02:34:13 -070063 int singlethread;
Oleg Nesterov319c2a92007-05-09 02:34:06 -070064 int freezeable; /* Freeze threads during suspend */
Johannes Berg4e6045f2007-10-18 23:39:55 -070065#ifdef CONFIG_LOCKDEP
66 struct lockdep_map lockdep_map;
67#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69
Gautham R Shenoy95402b32008-01-25 21:08:02 +010070/* Serializes the accesses to the list of workqueues. */
71static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static LIST_HEAD(workqueues);
73
Oleg Nesterov3af244332007-05-09 02:34:09 -070074static int singlethread_cpu __read_mostly;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -070075static cpumask_t cpu_singlethread_map __read_mostly;
Oleg Nesterov14441962007-05-23 13:57:57 -070076/*
77 * _cpu_down() first removes CPU from cpu_online_map, then CPU_DEAD
78 * flushes cwq->worklist. This means that flush_workqueue/wait_on_work
79 * which comes in between can't use for_each_online_cpu(). We could
80 * use cpu_possible_map, the cpumask below is more a documentation
81 * than optimization.
82 */
Oleg Nesterov3af244332007-05-09 02:34:09 -070083static cpumask_t cpu_populated_map __read_mostly;
Nathan Lynchf756d5e2006-01-08 01:05:12 -080084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/* If it's single threaded, it isn't in the list of workqueues. */
86static inline int is_single_threaded(struct workqueue_struct *wq)
87{
Oleg Nesterovcce1a162007-05-09 02:34:13 -070088 return wq->singlethread;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -070091static const cpumask_t *wq_cpu_map(struct workqueue_struct *wq)
92{
93 return is_single_threaded(wq)
94 ? &cpu_singlethread_map : &cpu_populated_map;
95}
96
Oleg Nesterova848e3b2007-05-09 02:34:17 -070097static
98struct cpu_workqueue_struct *wq_per_cpu(struct workqueue_struct *wq, int cpu)
99{
100 if (unlikely(is_single_threaded(wq)))
101 cpu = singlethread_cpu;
102 return per_cpu_ptr(wq->cpu_wq, cpu);
103}
104
David Howells4594bf12006-12-07 11:33:26 +0000105/*
106 * Set the workqueue on which a work item is to be run
107 * - Must *only* be called if the pending flag is set
108 */
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700109static inline void set_wq_data(struct work_struct *work,
110 struct cpu_workqueue_struct *cwq)
David Howells365970a2006-11-22 14:54:49 +0000111{
David Howells4594bf12006-12-07 11:33:26 +0000112 unsigned long new;
David Howells365970a2006-11-22 14:54:49 +0000113
David Howells4594bf12006-12-07 11:33:26 +0000114 BUG_ON(!work_pending(work));
115
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700116 new = (unsigned long) cwq | (1UL << WORK_STRUCT_PENDING);
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800117 new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work);
118 atomic_long_set(&work->data, new);
David Howells365970a2006-11-22 14:54:49 +0000119}
120
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700121static inline
122struct cpu_workqueue_struct *get_wq_data(struct work_struct *work)
David Howells365970a2006-11-22 14:54:49 +0000123{
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800124 return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK);
David Howells365970a2006-11-22 14:54:49 +0000125}
126
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700127static void insert_work(struct cpu_workqueue_struct *cwq,
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700128 struct work_struct *work, struct list_head *head)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700129{
130 set_wq_data(work, cwq);
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700131 /*
132 * Ensure that we get the right work->data if we see the
133 * result of list_add() below, see try_to_grab_pending().
134 */
135 smp_wmb();
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700136 list_add_tail(&work->entry, head);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700137 wake_up(&cwq->more_work);
138}
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140static void __queue_work(struct cpu_workqueue_struct *cwq,
141 struct work_struct *work)
142{
143 unsigned long flags;
144
145 spin_lock_irqsave(&cwq->lock, flags);
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700146 insert_work(cwq, work, &cwq->worklist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 spin_unlock_irqrestore(&cwq->lock, flags);
148}
149
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700150/**
151 * queue_work - queue work on a workqueue
152 * @wq: workqueue to use
153 * @work: work to queue
154 *
Alan Stern057647f2006-10-28 10:38:58 -0700155 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -0700157 * We queue the work to the CPU on which it was submitted, but if the CPU dies
158 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800160int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700162 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800164 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 BUG_ON(!list_empty(&work->entry));
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700166 __queue_work(wq_per_cpu(wq, get_cpu()), work);
167 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 ret = 1;
169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return ret;
171}
Dave Jonesae90dd52006-06-30 01:40:45 -0400172EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Zhang Ruic1a220e2008-07-23 21:28:39 -0700174/**
175 * queue_work_on - queue work on specific cpu
176 * @cpu: CPU number to execute work on
177 * @wq: workqueue to use
178 * @work: work to queue
179 *
180 * Returns 0 if @work was already on a queue, non-zero otherwise.
181 *
182 * We queue the work to a specific CPU, the caller must ensure it
183 * can't go away.
184 */
185int
186queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
187{
188 int ret = 0;
189
190 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
191 BUG_ON(!list_empty(&work->entry));
192 __queue_work(wq_per_cpu(wq, cpu), work);
193 ret = 1;
194 }
195 return ret;
196}
197EXPORT_SYMBOL_GPL(queue_work_on);
198
Li Zefan6d141c32008-02-08 04:21:09 -0800199static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
David Howells52bad642006-11-22 14:54:01 +0000201 struct delayed_work *dwork = (struct delayed_work *)__data;
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700202 struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
203 struct workqueue_struct *wq = cwq->wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700205 __queue_work(wq_per_cpu(wq, smp_processor_id()), &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700208/**
209 * queue_delayed_work - queue work on a workqueue after delay
210 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800211 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700212 * @delay: number of jiffies to wait before queueing
213 *
Alan Stern057647f2006-10-28 10:38:58 -0700214 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700215 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800216int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000217 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
David Howells52bad642006-11-22 14:54:01 +0000219 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -0700220 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Oleg Nesterov63bc0362007-05-09 02:34:16 -0700222 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
Dave Jonesae90dd52006-06-30 01:40:45 -0400224EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700226/**
227 * queue_delayed_work_on - queue work on specific CPU after delay
228 * @cpu: CPU number to execute work on
229 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800230 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700231 * @delay: number of jiffies to wait before queueing
232 *
Alan Stern057647f2006-10-28 10:38:58 -0700233 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700234 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700235int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000236 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700237{
238 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +0000239 struct timer_list *timer = &dwork->timer;
240 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700241
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800242 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700243 BUG_ON(timer_pending(timer));
244 BUG_ON(!list_empty(&work->entry));
245
Andrew Liu8a3e77c2008-05-01 04:35:14 -0700246 timer_stats_timer_set_start_info(&dwork->timer);
247
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700248 /* This stores cwq for the moment, for the timer_fn */
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700249 set_wq_data(work, wq_per_cpu(wq, raw_smp_processor_id()));
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700250 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +0000251 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700252 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -0700253
254 if (unlikely(cpu >= 0))
255 add_timer_on(timer, cpu);
256 else
257 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700258 ret = 1;
259 }
260 return ret;
261}
Dave Jonesae90dd52006-06-30 01:40:45 -0400262EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Arjan van de Ven858119e2006-01-14 13:20:43 -0800264static void run_workqueue(struct cpu_workqueue_struct *cwq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700266 spin_lock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 cwq->run_depth++;
268 if (cwq->run_depth > 3) {
269 /* morton gets to eat his hat */
270 printk("%s: recursion depth exceeded: %d\n",
Harvey Harrisonaf1f16d2008-04-30 00:55:08 -0700271 __func__, cwq->run_depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 dump_stack();
273 }
274 while (!list_empty(&cwq->worklist)) {
275 struct work_struct *work = list_entry(cwq->worklist.next,
276 struct work_struct, entry);
David Howells6bb49e52006-11-22 14:54:45 +0000277 work_func_t f = work->func;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700278#ifdef CONFIG_LOCKDEP
279 /*
280 * It is permissible to free the struct work_struct
281 * from inside the function that is called from it,
282 * this we need to take into account for lockdep too.
283 * To avoid bogus "held lock freed" warnings as well
284 * as problems when looking into work->lockdep_map,
285 * make a copy and use that here.
286 */
287 struct lockdep_map lockdep_map = work->lockdep_map;
288#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700290 cwq->current_work = work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 list_del_init(cwq->worklist.next);
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700292 spin_unlock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
David Howells365970a2006-11-22 14:54:49 +0000294 BUG_ON(get_wq_data(work) != cwq);
Oleg Nesterov23b2e592007-05-09 02:34:19 -0700295 work_clear_pending(work);
Johannes Berg4e6045f2007-10-18 23:39:55 -0700296 lock_acquire(&cwq->wq->lockdep_map, 0, 0, 0, 2, _THIS_IP_);
297 lock_acquire(&lockdep_map, 0, 0, 0, 2, _THIS_IP_);
David Howells65f27f32006-11-22 14:55:48 +0000298 f(work);
Johannes Berg4e6045f2007-10-18 23:39:55 -0700299 lock_release(&lockdep_map, 1, _THIS_IP_);
300 lock_release(&cwq->wq->lockdep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Peter Zijlstrad5abe662006-12-06 20:37:26 -0800302 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
303 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
304 "%s/0x%08x/%d\n",
305 current->comm, preempt_count(),
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700306 task_pid_nr(current));
Peter Zijlstrad5abe662006-12-06 20:37:26 -0800307 printk(KERN_ERR " last function: ");
308 print_symbol("%s\n", (unsigned long)f);
309 debug_show_held_locks(current);
310 dump_stack();
311 }
312
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700313 spin_lock_irq(&cwq->lock);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700314 cwq->current_work = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
316 cwq->run_depth--;
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700317 spin_unlock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
320static int worker_thread(void *__cwq)
321{
322 struct cpu_workqueue_struct *cwq = __cwq;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700323 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700325 if (cwq->wq->freezeable)
326 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 set_user_nice(current, -5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Oleg Nesterov3af244332007-05-09 02:34:09 -0700330 for (;;) {
Oleg Nesterov3af244332007-05-09 02:34:09 -0700331 prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
Oleg Nesterov14441962007-05-23 13:57:57 -0700332 if (!freezing(current) &&
333 !kthread_should_stop() &&
334 list_empty(&cwq->worklist))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 schedule();
Oleg Nesterov3af244332007-05-09 02:34:09 -0700336 finish_wait(&cwq->more_work, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Oleg Nesterov85f41862007-05-09 02:34:20 -0700338 try_to_freeze();
339
Oleg Nesterov14441962007-05-23 13:57:57 -0700340 if (kthread_should_stop())
Oleg Nesterov3af244332007-05-09 02:34:09 -0700341 break;
342
343 run_workqueue(cwq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
Oleg Nesterov3af244332007-05-09 02:34:09 -0700345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return 0;
347}
348
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700349struct wq_barrier {
350 struct work_struct work;
351 struct completion done;
352};
353
354static void wq_barrier_func(struct work_struct *work)
355{
356 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
357 complete(&barr->done);
358}
359
Oleg Nesterov83c22522007-05-09 02:33:54 -0700360static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700361 struct wq_barrier *barr, struct list_head *head)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700362{
363 INIT_WORK(&barr->work, wq_barrier_func);
364 __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work));
365
366 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -0700367
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700368 insert_work(cwq, &barr->work, head);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700369}
370
Oleg Nesterov14441962007-05-23 13:57:57 -0700371static int flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Oleg Nesterov14441962007-05-23 13:57:57 -0700373 int active;
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (cwq->thread == current) {
376 /*
377 * Probably keventd trying to flush its own queue. So simply run
378 * it by hand rather than deadlocking.
379 */
380 run_workqueue(cwq);
Oleg Nesterov14441962007-05-23 13:57:57 -0700381 active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 } else {
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700383 struct wq_barrier barr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Oleg Nesterov14441962007-05-23 13:57:57 -0700385 active = 0;
Oleg Nesterov83c22522007-05-09 02:33:54 -0700386 spin_lock_irq(&cwq->lock);
387 if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700388 insert_wq_barrier(cwq, &barr, &cwq->worklist);
Oleg Nesterov83c22522007-05-09 02:33:54 -0700389 active = 1;
390 }
391 spin_unlock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Oleg Nesterovd7213042007-05-09 02:34:07 -0700393 if (active)
Oleg Nesterov83c22522007-05-09 02:33:54 -0700394 wait_for_completion(&barr.done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
Oleg Nesterov14441962007-05-23 13:57:57 -0700396
397 return active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700400/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700402 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 *
404 * Forces execution of the workqueue and blocks until its completion.
405 * This is typically used in driver shutdown handlers.
406 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700407 * We sleep until all works which were queued on entry have been handled,
408 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 *
410 * This function used to run the workqueues itself. Now we just wait for the
411 * helper threads to do it.
412 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800413void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700415 const cpumask_t *cpu_map = wq_cpu_map(wq);
Oleg Nesterovcce1a162007-05-09 02:34:13 -0700416 int cpu;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700417
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700418 might_sleep();
Johannes Berg4e6045f2007-10-18 23:39:55 -0700419 lock_acquire(&wq->lockdep_map, 0, 0, 0, 2, _THIS_IP_);
420 lock_release(&wq->lockdep_map, 1, _THIS_IP_);
Mike Travis363ab6f2008-05-12 21:21:13 +0200421 for_each_cpu_mask_nr(cpu, *cpu_map)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700422 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
Dave Jonesae90dd52006-06-30 01:40:45 -0400424EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Oleg Nesterovdb700892008-07-25 01:47:49 -0700426/**
427 * flush_work - block until a work_struct's callback has terminated
428 * @work: the work which is to be flushed
429 *
430 * It is expected that, prior to calling flush_work(), the caller has
431 * arranged for the work to not be requeued, otherwise it doesn't make
432 * sense to use this function.
433 */
434int flush_work(struct work_struct *work)
435{
436 struct cpu_workqueue_struct *cwq;
437 struct list_head *prev;
438 struct wq_barrier barr;
439
440 might_sleep();
441 cwq = get_wq_data(work);
442 if (!cwq)
443 return 0;
444
445 prev = NULL;
446 spin_lock_irq(&cwq->lock);
447 if (!list_empty(&work->entry)) {
448 /*
449 * See the comment near try_to_grab_pending()->smp_rmb().
450 * If it was re-queued under us we are not going to wait.
451 */
452 smp_rmb();
453 if (unlikely(cwq != get_wq_data(work)))
454 goto out;
455 prev = &work->entry;
456 } else {
457 if (cwq->current_work != work)
458 goto out;
459 prev = &cwq->worklist;
460 }
461 insert_wq_barrier(cwq, &barr, prev->next);
462out:
463 spin_unlock_irq(&cwq->lock);
464 if (!prev)
465 return 0;
466
467 wait_for_completion(&barr.done);
468 return 1;
469}
470EXPORT_SYMBOL_GPL(flush_work);
471
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700472/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700473 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700474 * so this work can't be re-armed in any way.
475 */
476static int try_to_grab_pending(struct work_struct *work)
477{
478 struct cpu_workqueue_struct *cwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700479 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700480
481 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700482 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700483
484 /*
485 * The queueing is in progress, or it is already queued. Try to
486 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
487 */
488
489 cwq = get_wq_data(work);
490 if (!cwq)
491 return ret;
492
493 spin_lock_irq(&cwq->lock);
494 if (!list_empty(&work->entry)) {
495 /*
496 * This work is queued, but perhaps we locked the wrong cwq.
497 * In that case we must see the new value after rmb(), see
498 * insert_work()->wmb().
499 */
500 smp_rmb();
501 if (cwq == get_wq_data(work)) {
502 list_del_init(&work->entry);
503 ret = 1;
504 }
505 }
506 spin_unlock_irq(&cwq->lock);
507
508 return ret;
509}
510
511static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq,
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700512 struct work_struct *work)
513{
514 struct wq_barrier barr;
515 int running = 0;
516
517 spin_lock_irq(&cwq->lock);
518 if (unlikely(cwq->current_work == work)) {
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700519 insert_wq_barrier(cwq, &barr, cwq->worklist.next);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700520 running = 1;
521 }
522 spin_unlock_irq(&cwq->lock);
523
Oleg Nesterov3af244332007-05-09 02:34:09 -0700524 if (unlikely(running))
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700525 wait_for_completion(&barr.done);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700526}
527
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700528static void wait_on_work(struct work_struct *work)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700529{
530 struct cpu_workqueue_struct *cwq;
Oleg Nesterov28e53bd2007-05-09 02:34:22 -0700531 struct workqueue_struct *wq;
532 const cpumask_t *cpu_map;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700533 int cpu;
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700534
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700535 might_sleep();
536
Johannes Berg4e6045f2007-10-18 23:39:55 -0700537 lock_acquire(&work->lockdep_map, 0, 0, 0, 2, _THIS_IP_);
538 lock_release(&work->lockdep_map, 1, _THIS_IP_);
539
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700540 cwq = get_wq_data(work);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700541 if (!cwq)
Oleg Nesterov3af244332007-05-09 02:34:09 -0700542 return;
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700543
Oleg Nesterov28e53bd2007-05-09 02:34:22 -0700544 wq = cwq->wq;
545 cpu_map = wq_cpu_map(wq);
546
Mike Travis363ab6f2008-05-12 21:21:13 +0200547 for_each_cpu_mask_nr(cpu, *cpu_map)
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700548 wait_on_cpu_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
549}
550
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700551static int __cancel_work_timer(struct work_struct *work,
552 struct timer_list* timer)
553{
554 int ret;
555
556 do {
557 ret = (timer && likely(del_timer(timer)));
558 if (!ret)
559 ret = try_to_grab_pending(work);
560 wait_on_work(work);
561 } while (unlikely(ret < 0));
562
563 work_clear_pending(work);
564 return ret;
565}
566
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700567/**
568 * cancel_work_sync - block until a work_struct's callback has terminated
569 * @work: the work which is to be flushed
570 *
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700571 * Returns true if @work was pending.
572 *
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700573 * cancel_work_sync() will cancel the work if it is queued. If the work's
574 * callback appears to be running, cancel_work_sync() will block until it
575 * has completed.
576 *
577 * It is possible to use this function if the work re-queues itself. It can
578 * cancel the work even if it migrates to another workqueue, however in that
579 * case it only guarantees that work->func() has completed on the last queued
580 * workqueue.
581 *
582 * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
583 * pending, otherwise it goes into a busy-wait loop until the timer expires.
584 *
585 * The caller must ensure that workqueue_struct on which this work was last
586 * queued can't be destroyed before this function returns.
587 */
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700588int cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700589{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700590 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700591}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -0700592EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700593
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700594/**
Oleg Nesterovf5a421a2007-07-15 23:41:44 -0700595 * cancel_delayed_work_sync - reliably kill off a delayed work.
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700596 * @dwork: the delayed work struct
597 *
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700598 * Returns true if @dwork was pending.
599 *
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700600 * It is possible to use this function if @dwork rearms itself via queue_work()
601 * or queue_delayed_work(). See also the comment for cancel_work_sync().
602 */
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700603int cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700604{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700605 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700606}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -0700607EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700609static struct workqueue_struct *keventd_wq __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700611/**
612 * schedule_work - put work task in global workqueue
613 * @work: job to be done
614 *
615 * This puts a job in the kernel-global workqueue.
616 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800617int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 return queue_work(keventd_wq, work);
620}
Dave Jonesae90dd52006-06-30 01:40:45 -0400621EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Zhang Ruic1a220e2008-07-23 21:28:39 -0700623/*
624 * schedule_work_on - put work task on a specific cpu
625 * @cpu: cpu to put the work task on
626 * @work: job to be done
627 *
628 * This puts a job on a specific cpu
629 */
630int schedule_work_on(int cpu, struct work_struct *work)
631{
632 return queue_work_on(cpu, keventd_wq, work);
633}
634EXPORT_SYMBOL(schedule_work_on);
635
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700636/**
637 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +0000638 * @dwork: job to be done
639 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700640 *
641 * After waiting for a given time this puts a job in the kernel-global
642 * workqueue.
643 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800644int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800645 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
David Howells52bad642006-11-22 14:54:01 +0000647 return queue_delayed_work(keventd_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
Dave Jonesae90dd52006-06-30 01:40:45 -0400649EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700651/**
652 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
653 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +0000654 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700655 * @delay: number of jiffies to wait
656 *
657 * After waiting for a given time this puts a job in the kernel-global
658 * workqueue on the specified CPU.
659 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +0000661 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
David Howells52bad642006-11-22 14:54:01 +0000663 return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
Dave Jonesae90dd52006-06-30 01:40:45 -0400665EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Andrew Mortonb6136772006-06-25 05:47:49 -0700667/**
668 * schedule_on_each_cpu - call a function on each online CPU from keventd
669 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -0700670 *
671 * Returns zero on success.
672 * Returns -ve errno on failure.
673 *
Andrew Mortonb6136772006-06-25 05:47:49 -0700674 * schedule_on_each_cpu() is very slow.
675 */
David Howells65f27f32006-11-22 14:55:48 +0000676int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -0800677{
678 int cpu;
Andrew Mortonb6136772006-06-25 05:47:49 -0700679 struct work_struct *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -0800680
Andrew Mortonb6136772006-06-25 05:47:49 -0700681 works = alloc_percpu(struct work_struct);
682 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -0800683 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -0700684
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100685 get_online_cpus();
Christoph Lameter15316ba2006-01-08 01:00:43 -0800686 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +0100687 struct work_struct *work = per_cpu_ptr(works, cpu);
688
689 INIT_WORK(work, func);
690 set_bit(WORK_STRUCT_PENDING, work_data_bits(work));
691 __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), work);
Christoph Lameter15316ba2006-01-08 01:00:43 -0800692 }
693 flush_workqueue(keventd_wq);
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100694 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -0700695 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -0800696 return 0;
697}
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699void flush_scheduled_work(void)
700{
701 flush_workqueue(keventd_wq);
702}
Dave Jonesae90dd52006-06-30 01:40:45 -0400703EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705/**
James Bottomley1fa44ec2006-02-23 12:43:43 -0600706 * execute_in_process_context - reliably execute the routine with user context
707 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -0600708 * @ew: guaranteed storage for the execute work structure (must
709 * be available when the work executes)
710 *
711 * Executes the function immediately if process context is available,
712 * otherwise schedules the function for delayed execution.
713 *
714 * Returns: 0 - function was executed
715 * 1 - function was scheduled for execution
716 */
David Howells65f27f32006-11-22 14:55:48 +0000717int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -0600718{
719 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +0000720 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -0600721 return 0;
722 }
723
David Howells65f27f32006-11-22 14:55:48 +0000724 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -0600725 schedule_work(&ew->work);
726
727 return 1;
728}
729EXPORT_SYMBOL_GPL(execute_in_process_context);
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731int keventd_up(void)
732{
733 return keventd_wq != NULL;
734}
735
736int current_is_keventd(void)
737{
738 struct cpu_workqueue_struct *cwq;
Hugh Dickinsd2437692007-08-27 16:06:19 +0100739 int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 int ret = 0;
741
742 BUG_ON(!keventd_wq);
743
Christoph Lameter89ada672005-10-30 15:01:59 -0800744 cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (current == cwq->thread)
746 ret = 1;
747
748 return ret;
749
750}
751
Oleg Nesterov3af244332007-05-09 02:34:09 -0700752static struct cpu_workqueue_struct *
753init_cpu_workqueue(struct workqueue_struct *wq, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Christoph Lameter89ada672005-10-30 15:01:59 -0800755 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Oleg Nesterov3af244332007-05-09 02:34:09 -0700757 cwq->wq = wq;
758 spin_lock_init(&cwq->lock);
759 INIT_LIST_HEAD(&cwq->worklist);
760 init_waitqueue_head(&cwq->more_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Oleg Nesterov3af244332007-05-09 02:34:09 -0700762 return cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
Oleg Nesterov3af244332007-05-09 02:34:09 -0700765static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Oleg Nesterov3af244332007-05-09 02:34:09 -0700767 struct workqueue_struct *wq = cwq->wq;
768 const char *fmt = is_single_threaded(wq) ? "%s" : "%s/%d";
769 struct task_struct *p;
770
771 p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu);
772 /*
773 * Nobody can add the work_struct to this cwq,
774 * if (caller is __create_workqueue)
775 * nobody should see this wq
776 * else // caller is CPU_UP_PREPARE
777 * cpu is not on cpu_online_map
778 * so we can abort safely.
779 */
780 if (IS_ERR(p))
781 return PTR_ERR(p);
782
783 cwq->thread = p;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700784
785 return 0;
786}
787
Oleg Nesterov06ba38a2007-05-09 02:34:15 -0700788static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
789{
790 struct task_struct *p = cwq->thread;
791
792 if (p != NULL) {
793 if (cpu >= 0)
794 kthread_bind(p, cpu);
795 wake_up_process(p);
796 }
797}
798
Johannes Berg4e6045f2007-10-18 23:39:55 -0700799struct workqueue_struct *__create_workqueue_key(const char *name,
800 int singlethread,
801 int freezeable,
Johannes Bergeb13ba82008-01-16 09:51:58 +0100802 struct lock_class_key *key,
803 const char *lock_name)
Oleg Nesterov3af244332007-05-09 02:34:09 -0700804{
805 struct workqueue_struct *wq;
806 struct cpu_workqueue_struct *cwq;
807 int err = 0, cpu;
808
809 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
810 if (!wq)
811 return NULL;
812
813 wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
814 if (!wq->cpu_wq) {
815 kfree(wq);
816 return NULL;
817 }
818
819 wq->name = name;
Johannes Bergeb13ba82008-01-16 09:51:58 +0100820 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -0700821 wq->singlethread = singlethread;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700822 wq->freezeable = freezeable;
Oleg Nesterovcce1a162007-05-09 02:34:13 -0700823 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700824
825 if (singlethread) {
Oleg Nesterov3af244332007-05-09 02:34:09 -0700826 cwq = init_cpu_workqueue(wq, singlethread_cpu);
827 err = create_workqueue_thread(cwq, singlethread_cpu);
Oleg Nesterov06ba38a2007-05-09 02:34:15 -0700828 start_workqueue_thread(cwq, -1);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700829 } else {
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100830 get_online_cpus();
831 spin_lock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700832 list_add(&wq->list, &workqueues);
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100833 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700834
835 for_each_possible_cpu(cpu) {
836 cwq = init_cpu_workqueue(wq, cpu);
837 if (err || !cpu_online(cpu))
838 continue;
839 err = create_workqueue_thread(cwq, cpu);
Oleg Nesterov06ba38a2007-05-09 02:34:15 -0700840 start_workqueue_thread(cwq, cpu);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700841 }
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100842 put_online_cpus();
Oleg Nesterov3af244332007-05-09 02:34:09 -0700843 }
844
845 if (err) {
846 destroy_workqueue(wq);
847 wq = NULL;
848 }
849 return wq;
850}
Johannes Berg4e6045f2007-10-18 23:39:55 -0700851EXPORT_SYMBOL_GPL(__create_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700852
Oleg Nesterov1e35eaa2008-04-29 01:00:28 -0700853static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq)
Oleg Nesterov3af244332007-05-09 02:34:09 -0700854{
Oleg Nesterov14441962007-05-23 13:57:57 -0700855 /*
856 * Our caller is either destroy_workqueue() or CPU_DEAD,
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100857 * get_online_cpus() protects cwq->thread.
Oleg Nesterov14441962007-05-23 13:57:57 -0700858 */
859 if (cwq->thread == NULL)
860 return;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700861
Johannes Berg4e6045f2007-10-18 23:39:55 -0700862 lock_acquire(&cwq->wq->lockdep_map, 0, 0, 0, 2, _THIS_IP_);
863 lock_release(&cwq->wq->lockdep_map, 1, _THIS_IP_);
864
Oleg Nesterov13c22162007-07-17 04:03:55 -0700865 flush_cpu_workqueue(cwq);
Oleg Nesterov14441962007-05-23 13:57:57 -0700866 /*
Oleg Nesterov13c22162007-07-17 04:03:55 -0700867 * If the caller is CPU_DEAD and cwq->worklist was not empty,
868 * a concurrent flush_workqueue() can insert a barrier after us.
869 * However, in that case run_workqueue() won't return and check
870 * kthread_should_stop() until it flushes all work_struct's.
Oleg Nesterov14441962007-05-23 13:57:57 -0700871 * When ->worklist becomes empty it is safe to exit because no
872 * more work_structs can be queued on this cwq: flush_workqueue
873 * checks list_empty(), and a "normal" queue_work() can't use
874 * a dead CPU.
875 */
Oleg Nesterov14441962007-05-23 13:57:57 -0700876 kthread_stop(cwq->thread);
877 cwq->thread = NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700878}
879
880/**
881 * destroy_workqueue - safely terminate a workqueue
882 * @wq: target workqueue
883 *
884 * Safely destroy a workqueue. All work currently pending will be done first.
885 */
886void destroy_workqueue(struct workqueue_struct *wq)
887{
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700888 const cpumask_t *cpu_map = wq_cpu_map(wq);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700889 int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700890
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100891 get_online_cpus();
892 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700893 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100894 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700895
Mike Travis363ab6f2008-05-12 21:21:13 +0200896 for_each_cpu_mask_nr(cpu, *cpu_map)
Oleg Nesterov1e35eaa2008-04-29 01:00:28 -0700897 cleanup_workqueue_thread(per_cpu_ptr(wq->cpu_wq, cpu));
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -0700898 put_online_cpus();
Oleg Nesterov3af244332007-05-09 02:34:09 -0700899
900 free_percpu(wq->cpu_wq);
901 kfree(wq);
902}
903EXPORT_SYMBOL_GPL(destroy_workqueue);
904
905static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
906 unsigned long action,
907 void *hcpu)
908{
909 unsigned int cpu = (unsigned long)hcpu;
910 struct cpu_workqueue_struct *cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 struct workqueue_struct *wq;
912
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700913 action &= ~CPU_TASKS_FROZEN;
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 switch (action) {
916 case CPU_UP_PREPARE:
Oleg Nesterov3af244332007-05-09 02:34:09 -0700917 cpu_set(cpu, cpu_populated_map);
918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Oleg Nesterov3af244332007-05-09 02:34:09 -0700920 list_for_each_entry(wq, &workqueues, list) {
921 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
Christoph Lameter89ada672005-10-30 15:01:59 -0800922
Oleg Nesterov3af244332007-05-09 02:34:09 -0700923 switch (action) {
924 case CPU_UP_PREPARE:
925 if (!create_workqueue_thread(cwq, cpu))
926 break;
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100927 printk(KERN_ERR "workqueue [%s] for %i failed\n",
928 wq->name, cpu);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700929 return NOTIFY_BAD;
930
931 case CPU_ONLINE:
Oleg Nesterov06ba38a2007-05-09 02:34:15 -0700932 start_workqueue_thread(cwq, cpu);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700933 break;
934
935 case CPU_UP_CANCELED:
Oleg Nesterov06ba38a2007-05-09 02:34:15 -0700936 start_workqueue_thread(cwq, -1);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700937 case CPU_DEAD:
Oleg Nesterov1e35eaa2008-04-29 01:00:28 -0700938 cleanup_workqueue_thread(cwq);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700939 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
942
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -0700943 switch (action) {
944 case CPU_UP_CANCELED:
945 case CPU_DEAD:
946 cpu_clear(cpu, cpu_populated_map);
947 }
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return NOTIFY_OK;
950}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Oleg Nesterovc12920d2007-05-09 02:34:14 -0700952void __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Oleg Nesterov3af244332007-05-09 02:34:09 -0700954 cpu_populated_map = cpu_online_map;
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800955 singlethread_cpu = first_cpu(cpu_possible_map);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700956 cpu_singlethread_map = cpumask_of_cpu(singlethread_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 hotcpu_notifier(workqueue_cpu_callback, 0);
958 keventd_wq = create_workqueue("events");
959 BUG_ON(!keventd_wq);
960}