blob: 2f445833ae371a58d35c56907e2eeb638e75a453 [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>
Francois Camie1f8e872008-10-15 22:01:59 -070012 * Andrew Morton
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * 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 */
Heiko Carstens0d557dc2008-10-13 23:50:09 +020065 int rt;
Johannes Berg4e6045f2007-10-18 23:39:55 -070066#ifdef CONFIG_LOCKDEP
67 struct lockdep_map lockdep_map;
68#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070069};
70
Gautham R Shenoy95402b32008-01-25 21:08:02 +010071/* Serializes the accesses to the list of workqueues. */
72static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static LIST_HEAD(workqueues);
74
Oleg Nesterov3af244332007-05-09 02:34:09 -070075static int singlethread_cpu __read_mostly;
Rusty Russelle7577c52009-01-01 10:12:25 +103076static const struct cpumask *cpu_singlethread_map __read_mostly;
Oleg Nesterov14441962007-05-23 13:57:57 -070077/*
78 * _cpu_down() first removes CPU from cpu_online_map, then CPU_DEAD
79 * flushes cwq->worklist. This means that flush_workqueue/wait_on_work
80 * which comes in between can't use for_each_online_cpu(). We could
81 * use cpu_possible_map, the cpumask below is more a documentation
82 * than optimization.
83 */
Rusty Russelle7577c52009-01-01 10:12:25 +103084static cpumask_var_t cpu_populated_map __read_mostly;
Nathan Lynchf756d5e2006-01-08 01:05:12 -080085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/* If it's single threaded, it isn't in the list of workqueues. */
David Howells6cc88bc2008-11-14 10:39:21 +110087static inline int is_wq_single_threaded(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Oleg Nesterovcce1a162007-05-09 02:34:13 -070089 return wq->singlethread;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
Rusty Russelle7577c52009-01-01 10:12:25 +103092static const struct cpumask *wq_cpu_map(struct workqueue_struct *wq)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -070093{
David Howells6cc88bc2008-11-14 10:39:21 +110094 return is_wq_single_threaded(wq)
Rusty Russelle7577c52009-01-01 10:12:25 +103095 ? cpu_singlethread_map : cpu_populated_map;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -070096}
97
Oleg Nesterova848e3b2007-05-09 02:34:17 -070098static
99struct cpu_workqueue_struct *wq_per_cpu(struct workqueue_struct *wq, int cpu)
100{
David Howells6cc88bc2008-11-14 10:39:21 +1100101 if (unlikely(is_wq_single_threaded(wq)))
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700102 cpu = singlethread_cpu;
103 return per_cpu_ptr(wq->cpu_wq, cpu);
104}
105
David Howells4594bf12006-12-07 11:33:26 +0000106/*
107 * Set the workqueue on which a work item is to be run
108 * - Must *only* be called if the pending flag is set
109 */
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700110static inline void set_wq_data(struct work_struct *work,
111 struct cpu_workqueue_struct *cwq)
David Howells365970a2006-11-22 14:54:49 +0000112{
David Howells4594bf12006-12-07 11:33:26 +0000113 unsigned long new;
David Howells365970a2006-11-22 14:54:49 +0000114
David Howells4594bf12006-12-07 11:33:26 +0000115 BUG_ON(!work_pending(work));
116
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700117 new = (unsigned long) cwq | (1UL << WORK_STRUCT_PENDING);
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800118 new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work);
119 atomic_long_set(&work->data, new);
David Howells365970a2006-11-22 14:54:49 +0000120}
121
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700122static inline
123struct cpu_workqueue_struct *get_wq_data(struct work_struct *work)
David Howells365970a2006-11-22 14:54:49 +0000124{
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800125 return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK);
David Howells365970a2006-11-22 14:54:49 +0000126}
127
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700128static void insert_work(struct cpu_workqueue_struct *cwq,
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700129 struct work_struct *work, struct list_head *head)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700130{
131 set_wq_data(work, cwq);
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700132 /*
133 * Ensure that we get the right work->data if we see the
134 * result of list_add() below, see try_to_grab_pending().
135 */
136 smp_wmb();
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700137 list_add_tail(&work->entry, head);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700138 wake_up(&cwq->more_work);
139}
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141static void __queue_work(struct cpu_workqueue_struct *cwq,
142 struct work_struct *work)
143{
144 unsigned long flags;
145
146 spin_lock_irqsave(&cwq->lock, flags);
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700147 insert_work(cwq, work, &cwq->worklist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 spin_unlock_irqrestore(&cwq->lock, flags);
149}
150
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700151/**
152 * queue_work - queue work on a workqueue
153 * @wq: workqueue to use
154 * @work: work to queue
155 *
Alan Stern057647f2006-10-28 10:38:58 -0700156 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -0700158 * We queue the work to the CPU on which it was submitted, but if the CPU dies
159 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800161int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Oleg Nesterovef1ca232008-07-25 01:47:53 -0700163 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Oleg Nesterovef1ca232008-07-25 01:47:53 -0700165 ret = queue_work_on(get_cpu(), wq, work);
166 put_cpu();
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return ret;
169}
Dave Jonesae90dd52006-06-30 01:40:45 -0400170EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Zhang Ruic1a220e2008-07-23 21:28:39 -0700172/**
173 * queue_work_on - queue work on specific cpu
174 * @cpu: CPU number to execute work on
175 * @wq: workqueue to use
176 * @work: work to queue
177 *
178 * Returns 0 if @work was already on a queue, non-zero otherwise.
179 *
180 * We queue the work to a specific CPU, the caller must ensure it
181 * can't go away.
182 */
183int
184queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
185{
186 int ret = 0;
187
188 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
189 BUG_ON(!list_empty(&work->entry));
190 __queue_work(wq_per_cpu(wq, cpu), work);
191 ret = 1;
192 }
193 return ret;
194}
195EXPORT_SYMBOL_GPL(queue_work_on);
196
Li Zefan6d141c32008-02-08 04:21:09 -0800197static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
David Howells52bad642006-11-22 14:54:01 +0000199 struct delayed_work *dwork = (struct delayed_work *)__data;
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700200 struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
201 struct workqueue_struct *wq = cwq->wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700203 __queue_work(wq_per_cpu(wq, smp_processor_id()), &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700206/**
207 * queue_delayed_work - queue work on a workqueue after delay
208 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800209 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700210 * @delay: number of jiffies to wait before queueing
211 *
Alan Stern057647f2006-10-28 10:38:58 -0700212 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700213 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800214int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000215 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
David Howells52bad642006-11-22 14:54:01 +0000217 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -0700218 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Oleg Nesterov63bc0362007-05-09 02:34:16 -0700220 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
Dave Jonesae90dd52006-06-30 01:40:45 -0400222EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700224/**
225 * queue_delayed_work_on - queue work on specific CPU after delay
226 * @cpu: CPU number to execute work on
227 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800228 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700229 * @delay: number of jiffies to wait before queueing
230 *
Alan Stern057647f2006-10-28 10:38:58 -0700231 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700232 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700233int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000234 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700235{
236 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +0000237 struct timer_list *timer = &dwork->timer;
238 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700239
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800240 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700241 BUG_ON(timer_pending(timer));
242 BUG_ON(!list_empty(&work->entry));
243
Andrew Liu8a3e77c2008-05-01 04:35:14 -0700244 timer_stats_timer_set_start_info(&dwork->timer);
245
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700246 /* This stores cwq for the moment, for the timer_fn */
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700247 set_wq_data(work, wq_per_cpu(wq, raw_smp_processor_id()));
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700248 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +0000249 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700250 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -0700251
252 if (unlikely(cpu >= 0))
253 add_timer_on(timer, cpu);
254 else
255 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700256 ret = 1;
257 }
258 return ret;
259}
Dave Jonesae90dd52006-06-30 01:40:45 -0400260EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Arjan van de Ven858119e2006-01-14 13:20:43 -0800262static void run_workqueue(struct cpu_workqueue_struct *cwq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700264 spin_lock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 cwq->run_depth++;
266 if (cwq->run_depth > 3) {
267 /* morton gets to eat his hat */
268 printk("%s: recursion depth exceeded: %d\n",
Harvey Harrisonaf1f16d2008-04-30 00:55:08 -0700269 __func__, cwq->run_depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 dump_stack();
271 }
272 while (!list_empty(&cwq->worklist)) {
273 struct work_struct *work = list_entry(cwq->worklist.next,
274 struct work_struct, entry);
David Howells6bb49e52006-11-22 14:54:45 +0000275 work_func_t f = work->func;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700276#ifdef CONFIG_LOCKDEP
277 /*
278 * It is permissible to free the struct work_struct
279 * from inside the function that is called from it,
280 * this we need to take into account for lockdep too.
281 * To avoid bogus "held lock freed" warnings as well
282 * as problems when looking into work->lockdep_map,
283 * make a copy and use that here.
284 */
285 struct lockdep_map lockdep_map = work->lockdep_map;
286#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700288 cwq->current_work = work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 list_del_init(cwq->worklist.next);
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700290 spin_unlock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
David Howells365970a2006-11-22 14:54:49 +0000292 BUG_ON(get_wq_data(work) != cwq);
Oleg Nesterov23b2e592007-05-09 02:34:19 -0700293 work_clear_pending(work);
Ingo Molnar3295f0e2008-08-11 10:30:30 +0200294 lock_map_acquire(&cwq->wq->lockdep_map);
295 lock_map_acquire(&lockdep_map);
David Howells65f27f32006-11-22 14:55:48 +0000296 f(work);
Ingo Molnar3295f0e2008-08-11 10:30:30 +0200297 lock_map_release(&lockdep_map);
298 lock_map_release(&cwq->wq->lockdep_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Peter Zijlstrad5abe662006-12-06 20:37:26 -0800300 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
301 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
302 "%s/0x%08x/%d\n",
303 current->comm, preempt_count(),
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700304 task_pid_nr(current));
Peter Zijlstrad5abe662006-12-06 20:37:26 -0800305 printk(KERN_ERR " last function: ");
306 print_symbol("%s\n", (unsigned long)f);
307 debug_show_held_locks(current);
308 dump_stack();
309 }
310
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700311 spin_lock_irq(&cwq->lock);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700312 cwq->current_work = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314 cwq->run_depth--;
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700315 spin_unlock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
318static int worker_thread(void *__cwq)
319{
320 struct cpu_workqueue_struct *cwq = __cwq;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700321 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700323 if (cwq->wq->freezeable)
324 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 set_user_nice(current, -5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Oleg Nesterov3af244332007-05-09 02:34:09 -0700328 for (;;) {
Oleg Nesterov3af244332007-05-09 02:34:09 -0700329 prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
Oleg Nesterov14441962007-05-23 13:57:57 -0700330 if (!freezing(current) &&
331 !kthread_should_stop() &&
332 list_empty(&cwq->worklist))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 schedule();
Oleg Nesterov3af244332007-05-09 02:34:09 -0700334 finish_wait(&cwq->more_work, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Oleg Nesterov85f41862007-05-09 02:34:20 -0700336 try_to_freeze();
337
Oleg Nesterov14441962007-05-23 13:57:57 -0700338 if (kthread_should_stop())
Oleg Nesterov3af244332007-05-09 02:34:09 -0700339 break;
340
341 run_workqueue(cwq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
Oleg Nesterov3af244332007-05-09 02:34:09 -0700343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return 0;
345}
346
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700347struct wq_barrier {
348 struct work_struct work;
349 struct completion done;
350};
351
352static void wq_barrier_func(struct work_struct *work)
353{
354 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
355 complete(&barr->done);
356}
357
Oleg Nesterov83c22522007-05-09 02:33:54 -0700358static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700359 struct wq_barrier *barr, struct list_head *head)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700360{
361 INIT_WORK(&barr->work, wq_barrier_func);
362 __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work));
363
364 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -0700365
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700366 insert_work(cwq, &barr->work, head);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700367}
368
Oleg Nesterov14441962007-05-23 13:57:57 -0700369static int flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Oleg Nesterov14441962007-05-23 13:57:57 -0700371 int active;
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (cwq->thread == current) {
374 /*
375 * Probably keventd trying to flush its own queue. So simply run
376 * it by hand rather than deadlocking.
377 */
378 run_workqueue(cwq);
Oleg Nesterov14441962007-05-23 13:57:57 -0700379 active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 } else {
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700381 struct wq_barrier barr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Oleg Nesterov14441962007-05-23 13:57:57 -0700383 active = 0;
Oleg Nesterov83c22522007-05-09 02:33:54 -0700384 spin_lock_irq(&cwq->lock);
385 if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700386 insert_wq_barrier(cwq, &barr, &cwq->worklist);
Oleg Nesterov83c22522007-05-09 02:33:54 -0700387 active = 1;
388 }
389 spin_unlock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Oleg Nesterovd7213042007-05-09 02:34:07 -0700391 if (active)
Oleg Nesterov83c22522007-05-09 02:33:54 -0700392 wait_for_completion(&barr.done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
Oleg Nesterov14441962007-05-23 13:57:57 -0700394
395 return active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700398/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700400 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 *
402 * Forces execution of the workqueue and blocks until its completion.
403 * This is typically used in driver shutdown handlers.
404 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700405 * We sleep until all works which were queued on entry have been handled,
406 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 *
408 * This function used to run the workqueues itself. Now we just wait for the
409 * helper threads to do it.
410 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800411void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Rusty Russelle7577c52009-01-01 10:12:25 +1030413 const struct cpumask *cpu_map = wq_cpu_map(wq);
Oleg Nesterovcce1a162007-05-09 02:34:13 -0700414 int cpu;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700415
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700416 might_sleep();
Ingo Molnar3295f0e2008-08-11 10:30:30 +0200417 lock_map_acquire(&wq->lockdep_map);
418 lock_map_release(&wq->lockdep_map);
Mike Travis363ab6f2008-05-12 21:21:13 +0200419 for_each_cpu_mask_nr(cpu, *cpu_map)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700420 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
Dave Jonesae90dd52006-06-30 01:40:45 -0400422EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Oleg Nesterovdb700892008-07-25 01:47:49 -0700424/**
425 * flush_work - block until a work_struct's callback has terminated
426 * @work: the work which is to be flushed
427 *
Oleg Nesterova67da702008-07-25 01:47:52 -0700428 * Returns false if @work has already terminated.
429 *
Oleg Nesterovdb700892008-07-25 01:47:49 -0700430 * 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
Ingo Molnar3295f0e2008-08-11 10:30:30 +0200445 lock_map_acquire(&cwq->wq->lockdep_map);
446 lock_map_release(&cwq->wq->lockdep_map);
Oleg Nesterova67da702008-07-25 01:47:52 -0700447
Oleg Nesterovdb700892008-07-25 01:47:49 -0700448 prev = NULL;
449 spin_lock_irq(&cwq->lock);
450 if (!list_empty(&work->entry)) {
451 /*
452 * See the comment near try_to_grab_pending()->smp_rmb().
453 * If it was re-queued under us we are not going to wait.
454 */
455 smp_rmb();
456 if (unlikely(cwq != get_wq_data(work)))
457 goto out;
458 prev = &work->entry;
459 } else {
460 if (cwq->current_work != work)
461 goto out;
462 prev = &cwq->worklist;
463 }
464 insert_wq_barrier(cwq, &barr, prev->next);
465out:
466 spin_unlock_irq(&cwq->lock);
467 if (!prev)
468 return 0;
469
470 wait_for_completion(&barr.done);
471 return 1;
472}
473EXPORT_SYMBOL_GPL(flush_work);
474
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700475/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700476 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700477 * so this work can't be re-armed in any way.
478 */
479static int try_to_grab_pending(struct work_struct *work)
480{
481 struct cpu_workqueue_struct *cwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700482 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700483
484 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700485 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700486
487 /*
488 * The queueing is in progress, or it is already queued. Try to
489 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
490 */
491
492 cwq = get_wq_data(work);
493 if (!cwq)
494 return ret;
495
496 spin_lock_irq(&cwq->lock);
497 if (!list_empty(&work->entry)) {
498 /*
499 * This work is queued, but perhaps we locked the wrong cwq.
500 * In that case we must see the new value after rmb(), see
501 * insert_work()->wmb().
502 */
503 smp_rmb();
504 if (cwq == get_wq_data(work)) {
505 list_del_init(&work->entry);
506 ret = 1;
507 }
508 }
509 spin_unlock_irq(&cwq->lock);
510
511 return ret;
512}
513
514static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq,
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700515 struct work_struct *work)
516{
517 struct wq_barrier barr;
518 int running = 0;
519
520 spin_lock_irq(&cwq->lock);
521 if (unlikely(cwq->current_work == work)) {
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700522 insert_wq_barrier(cwq, &barr, cwq->worklist.next);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700523 running = 1;
524 }
525 spin_unlock_irq(&cwq->lock);
526
Oleg Nesterov3af244332007-05-09 02:34:09 -0700527 if (unlikely(running))
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700528 wait_for_completion(&barr.done);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700529}
530
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700531static void wait_on_work(struct work_struct *work)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700532{
533 struct cpu_workqueue_struct *cwq;
Oleg Nesterov28e53bd2007-05-09 02:34:22 -0700534 struct workqueue_struct *wq;
Rusty Russelle7577c52009-01-01 10:12:25 +1030535 const struct cpumask *cpu_map;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700536 int cpu;
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700537
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700538 might_sleep();
539
Ingo Molnar3295f0e2008-08-11 10:30:30 +0200540 lock_map_acquire(&work->lockdep_map);
541 lock_map_release(&work->lockdep_map);
Johannes Berg4e6045f2007-10-18 23:39:55 -0700542
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700543 cwq = get_wq_data(work);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700544 if (!cwq)
Oleg Nesterov3af244332007-05-09 02:34:09 -0700545 return;
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700546
Oleg Nesterov28e53bd2007-05-09 02:34:22 -0700547 wq = cwq->wq;
548 cpu_map = wq_cpu_map(wq);
549
Mike Travis363ab6f2008-05-12 21:21:13 +0200550 for_each_cpu_mask_nr(cpu, *cpu_map)
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700551 wait_on_cpu_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
552}
553
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700554static int __cancel_work_timer(struct work_struct *work,
555 struct timer_list* timer)
556{
557 int ret;
558
559 do {
560 ret = (timer && likely(del_timer(timer)));
561 if (!ret)
562 ret = try_to_grab_pending(work);
563 wait_on_work(work);
564 } while (unlikely(ret < 0));
565
566 work_clear_pending(work);
567 return ret;
568}
569
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700570/**
571 * cancel_work_sync - block until a work_struct's callback has terminated
572 * @work: the work which is to be flushed
573 *
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700574 * Returns true if @work was pending.
575 *
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700576 * cancel_work_sync() will cancel the work if it is queued. If the work's
577 * callback appears to be running, cancel_work_sync() will block until it
578 * has completed.
579 *
580 * It is possible to use this function if the work re-queues itself. It can
581 * cancel the work even if it migrates to another workqueue, however in that
582 * case it only guarantees that work->func() has completed on the last queued
583 * workqueue.
584 *
585 * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
586 * pending, otherwise it goes into a busy-wait loop until the timer expires.
587 *
588 * The caller must ensure that workqueue_struct on which this work was last
589 * queued can't be destroyed before this function returns.
590 */
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700591int cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700592{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700593 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700594}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -0700595EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700596
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700597/**
Oleg Nesterovf5a421a2007-07-15 23:41:44 -0700598 * cancel_delayed_work_sync - reliably kill off a delayed work.
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700599 * @dwork: the delayed work struct
600 *
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700601 * Returns true if @dwork was pending.
602 *
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700603 * It is possible to use this function if @dwork rearms itself via queue_work()
604 * or queue_delayed_work(). See also the comment for cancel_work_sync().
605 */
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700606int cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700607{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700608 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700609}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -0700610EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700612static struct workqueue_struct *keventd_wq __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700614/**
615 * schedule_work - put work task in global workqueue
616 * @work: job to be done
617 *
618 * This puts a job in the kernel-global workqueue.
619 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800620int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
622 return queue_work(keventd_wq, work);
623}
Dave Jonesae90dd52006-06-30 01:40:45 -0400624EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Zhang Ruic1a220e2008-07-23 21:28:39 -0700626/*
627 * schedule_work_on - put work task on a specific cpu
628 * @cpu: cpu to put the work task on
629 * @work: job to be done
630 *
631 * This puts a job on a specific cpu
632 */
633int schedule_work_on(int cpu, struct work_struct *work)
634{
635 return queue_work_on(cpu, keventd_wq, work);
636}
637EXPORT_SYMBOL(schedule_work_on);
638
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700639/**
640 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +0000641 * @dwork: job to be done
642 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700643 *
644 * After waiting for a given time this puts a job in the kernel-global
645 * workqueue.
646 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800647int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800648 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
David Howells52bad642006-11-22 14:54:01 +0000650 return queue_delayed_work(keventd_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
Dave Jonesae90dd52006-06-30 01:40:45 -0400652EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700654/**
655 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
656 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +0000657 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700658 * @delay: number of jiffies to wait
659 *
660 * After waiting for a given time this puts a job in the kernel-global
661 * workqueue on the specified CPU.
662 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +0000664 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
David Howells52bad642006-11-22 14:54:01 +0000666 return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
Dave Jonesae90dd52006-06-30 01:40:45 -0400668EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Andrew Mortonb6136772006-06-25 05:47:49 -0700670/**
671 * schedule_on_each_cpu - call a function on each online CPU from keventd
672 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -0700673 *
674 * Returns zero on success.
675 * Returns -ve errno on failure.
676 *
Andrew Mortonb6136772006-06-25 05:47:49 -0700677 * schedule_on_each_cpu() is very slow.
678 */
David Howells65f27f32006-11-22 14:55:48 +0000679int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -0800680{
681 int cpu;
Andrew Mortonb6136772006-06-25 05:47:49 -0700682 struct work_struct *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -0800683
Andrew Mortonb6136772006-06-25 05:47:49 -0700684 works = alloc_percpu(struct work_struct);
685 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -0800686 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -0700687
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100688 get_online_cpus();
Christoph Lameter15316ba2006-01-08 01:00:43 -0800689 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +0100690 struct work_struct *work = per_cpu_ptr(works, cpu);
691
692 INIT_WORK(work, func);
Oleg Nesterov8de6d302008-07-25 01:47:53 -0700693 schedule_work_on(cpu, work);
Christoph Lameter15316ba2006-01-08 01:00:43 -0800694 }
Oleg Nesterov8616a892008-07-25 01:47:49 -0700695 for_each_online_cpu(cpu)
696 flush_work(per_cpu_ptr(works, cpu));
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100697 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -0700698 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -0800699 return 0;
700}
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702void flush_scheduled_work(void)
703{
704 flush_workqueue(keventd_wq);
705}
Dave Jonesae90dd52006-06-30 01:40:45 -0400706EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708/**
James Bottomley1fa44ec2006-02-23 12:43:43 -0600709 * execute_in_process_context - reliably execute the routine with user context
710 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -0600711 * @ew: guaranteed storage for the execute work structure (must
712 * be available when the work executes)
713 *
714 * Executes the function immediately if process context is available,
715 * otherwise schedules the function for delayed execution.
716 *
717 * Returns: 0 - function was executed
718 * 1 - function was scheduled for execution
719 */
David Howells65f27f32006-11-22 14:55:48 +0000720int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -0600721{
722 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +0000723 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -0600724 return 0;
725 }
726
David Howells65f27f32006-11-22 14:55:48 +0000727 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -0600728 schedule_work(&ew->work);
729
730 return 1;
731}
732EXPORT_SYMBOL_GPL(execute_in_process_context);
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734int keventd_up(void)
735{
736 return keventd_wq != NULL;
737}
738
739int current_is_keventd(void)
740{
741 struct cpu_workqueue_struct *cwq;
Hugh Dickinsd2437692007-08-27 16:06:19 +0100742 int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 int ret = 0;
744
745 BUG_ON(!keventd_wq);
746
Christoph Lameter89ada672005-10-30 15:01:59 -0800747 cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (current == cwq->thread)
749 ret = 1;
750
751 return ret;
752
753}
754
Oleg Nesterov3af244332007-05-09 02:34:09 -0700755static struct cpu_workqueue_struct *
756init_cpu_workqueue(struct workqueue_struct *wq, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Christoph Lameter89ada672005-10-30 15:01:59 -0800758 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Oleg Nesterov3af244332007-05-09 02:34:09 -0700760 cwq->wq = wq;
761 spin_lock_init(&cwq->lock);
762 INIT_LIST_HEAD(&cwq->worklist);
763 init_waitqueue_head(&cwq->more_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Oleg Nesterov3af244332007-05-09 02:34:09 -0700765 return cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
Oleg Nesterov3af244332007-05-09 02:34:09 -0700768static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Heiko Carstens0d557dc2008-10-13 23:50:09 +0200770 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
Oleg Nesterov3af244332007-05-09 02:34:09 -0700771 struct workqueue_struct *wq = cwq->wq;
David Howells6cc88bc2008-11-14 10:39:21 +1100772 const char *fmt = is_wq_single_threaded(wq) ? "%s" : "%s/%d";
Oleg Nesterov3af244332007-05-09 02:34:09 -0700773 struct task_struct *p;
774
775 p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu);
776 /*
777 * Nobody can add the work_struct to this cwq,
778 * if (caller is __create_workqueue)
779 * nobody should see this wq
780 * else // caller is CPU_UP_PREPARE
781 * cpu is not on cpu_online_map
782 * so we can abort safely.
783 */
784 if (IS_ERR(p))
785 return PTR_ERR(p);
Heiko Carstens0d557dc2008-10-13 23:50:09 +0200786 if (cwq->wq->rt)
787 sched_setscheduler_nocheck(p, SCHED_FIFO, &param);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700788 cwq->thread = p;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700789
790 return 0;
791}
792
Oleg Nesterov06ba38a2007-05-09 02:34:15 -0700793static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
794{
795 struct task_struct *p = cwq->thread;
796
797 if (p != NULL) {
798 if (cpu >= 0)
799 kthread_bind(p, cpu);
800 wake_up_process(p);
801 }
802}
803
Johannes Berg4e6045f2007-10-18 23:39:55 -0700804struct workqueue_struct *__create_workqueue_key(const char *name,
805 int singlethread,
806 int freezeable,
Heiko Carstens0d557dc2008-10-13 23:50:09 +0200807 int rt,
Johannes Bergeb13ba82008-01-16 09:51:58 +0100808 struct lock_class_key *key,
809 const char *lock_name)
Oleg Nesterov3af244332007-05-09 02:34:09 -0700810{
811 struct workqueue_struct *wq;
812 struct cpu_workqueue_struct *cwq;
813 int err = 0, cpu;
814
815 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
816 if (!wq)
817 return NULL;
818
819 wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
820 if (!wq->cpu_wq) {
821 kfree(wq);
822 return NULL;
823 }
824
825 wq->name = name;
Johannes Bergeb13ba82008-01-16 09:51:58 +0100826 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -0700827 wq->singlethread = singlethread;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700828 wq->freezeable = freezeable;
Heiko Carstens0d557dc2008-10-13 23:50:09 +0200829 wq->rt = rt;
Oleg Nesterovcce1a162007-05-09 02:34:13 -0700830 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700831
832 if (singlethread) {
Oleg Nesterov3af244332007-05-09 02:34:09 -0700833 cwq = init_cpu_workqueue(wq, singlethread_cpu);
834 err = create_workqueue_thread(cwq, singlethread_cpu);
Oleg Nesterov06ba38a2007-05-09 02:34:15 -0700835 start_workqueue_thread(cwq, -1);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700836 } else {
Oleg Nesterov3da1c842008-07-25 01:47:50 -0700837 cpu_maps_update_begin();
Oleg Nesterov6af8bf32008-07-29 22:33:49 -0700838 /*
839 * We must place this wq on list even if the code below fails.
840 * cpu_down(cpu) can remove cpu from cpu_populated_map before
841 * destroy_workqueue() takes the lock, in that case we leak
842 * cwq[cpu]->thread.
843 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100844 spin_lock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700845 list_add(&wq->list, &workqueues);
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100846 spin_unlock(&workqueue_lock);
Oleg Nesterov6af8bf32008-07-29 22:33:49 -0700847 /*
848 * We must initialize cwqs for each possible cpu even if we
849 * are going to call destroy_workqueue() finally. Otherwise
850 * cpu_up() can hit the uninitialized cwq once we drop the
851 * lock.
852 */
Oleg Nesterov3af244332007-05-09 02:34:09 -0700853 for_each_possible_cpu(cpu) {
854 cwq = init_cpu_workqueue(wq, cpu);
855 if (err || !cpu_online(cpu))
856 continue;
857 err = create_workqueue_thread(cwq, cpu);
Oleg Nesterov06ba38a2007-05-09 02:34:15 -0700858 start_workqueue_thread(cwq, cpu);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700859 }
Oleg Nesterov3da1c842008-07-25 01:47:50 -0700860 cpu_maps_update_done();
Oleg Nesterov3af244332007-05-09 02:34:09 -0700861 }
862
863 if (err) {
864 destroy_workqueue(wq);
865 wq = NULL;
866 }
867 return wq;
868}
Johannes Berg4e6045f2007-10-18 23:39:55 -0700869EXPORT_SYMBOL_GPL(__create_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700870
Oleg Nesterov1e35eaa2008-04-29 01:00:28 -0700871static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq)
Oleg Nesterov3af244332007-05-09 02:34:09 -0700872{
Oleg Nesterov14441962007-05-23 13:57:57 -0700873 /*
Oleg Nesterov3da1c842008-07-25 01:47:50 -0700874 * Our caller is either destroy_workqueue() or CPU_POST_DEAD,
875 * cpu_add_remove_lock protects cwq->thread.
Oleg Nesterov14441962007-05-23 13:57:57 -0700876 */
877 if (cwq->thread == NULL)
878 return;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700879
Ingo Molnar3295f0e2008-08-11 10:30:30 +0200880 lock_map_acquire(&cwq->wq->lockdep_map);
881 lock_map_release(&cwq->wq->lockdep_map);
Johannes Berg4e6045f2007-10-18 23:39:55 -0700882
Oleg Nesterov13c22162007-07-17 04:03:55 -0700883 flush_cpu_workqueue(cwq);
Oleg Nesterov14441962007-05-23 13:57:57 -0700884 /*
Oleg Nesterov3da1c842008-07-25 01:47:50 -0700885 * If the caller is CPU_POST_DEAD and cwq->worklist was not empty,
Oleg Nesterov13c22162007-07-17 04:03:55 -0700886 * a concurrent flush_workqueue() can insert a barrier after us.
887 * However, in that case run_workqueue() won't return and check
888 * kthread_should_stop() until it flushes all work_struct's.
Oleg Nesterov14441962007-05-23 13:57:57 -0700889 * When ->worklist becomes empty it is safe to exit because no
890 * more work_structs can be queued on this cwq: flush_workqueue
891 * checks list_empty(), and a "normal" queue_work() can't use
892 * a dead CPU.
893 */
Oleg Nesterov14441962007-05-23 13:57:57 -0700894 kthread_stop(cwq->thread);
895 cwq->thread = NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700896}
897
898/**
899 * destroy_workqueue - safely terminate a workqueue
900 * @wq: target workqueue
901 *
902 * Safely destroy a workqueue. All work currently pending will be done first.
903 */
904void destroy_workqueue(struct workqueue_struct *wq)
905{
Rusty Russelle7577c52009-01-01 10:12:25 +1030906 const struct cpumask *cpu_map = wq_cpu_map(wq);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700907 int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700908
Oleg Nesterov3da1c842008-07-25 01:47:50 -0700909 cpu_maps_update_begin();
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100910 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700911 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100912 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700913
Mike Travis363ab6f2008-05-12 21:21:13 +0200914 for_each_cpu_mask_nr(cpu, *cpu_map)
Oleg Nesterov1e35eaa2008-04-29 01:00:28 -0700915 cleanup_workqueue_thread(per_cpu_ptr(wq->cpu_wq, cpu));
Oleg Nesterov3da1c842008-07-25 01:47:50 -0700916 cpu_maps_update_done();
Oleg Nesterov3af244332007-05-09 02:34:09 -0700917
918 free_percpu(wq->cpu_wq);
919 kfree(wq);
920}
921EXPORT_SYMBOL_GPL(destroy_workqueue);
922
923static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
924 unsigned long action,
925 void *hcpu)
926{
927 unsigned int cpu = (unsigned long)hcpu;
928 struct cpu_workqueue_struct *cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 struct workqueue_struct *wq;
Oleg Nesterov84485022008-07-25 01:47:54 -0700930 int ret = NOTIFY_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700932 action &= ~CPU_TASKS_FROZEN;
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 switch (action) {
935 case CPU_UP_PREPARE:
Rusty Russelle7577c52009-01-01 10:12:25 +1030936 cpumask_set_cpu(cpu, cpu_populated_map);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700937 }
Oleg Nesterov84485022008-07-25 01:47:54 -0700938undo:
Oleg Nesterov3af244332007-05-09 02:34:09 -0700939 list_for_each_entry(wq, &workqueues, list) {
940 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
Christoph Lameter89ada672005-10-30 15:01:59 -0800941
Oleg Nesterov3af244332007-05-09 02:34:09 -0700942 switch (action) {
943 case CPU_UP_PREPARE:
944 if (!create_workqueue_thread(cwq, cpu))
945 break;
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100946 printk(KERN_ERR "workqueue [%s] for %i failed\n",
947 wq->name, cpu);
Oleg Nesterov84485022008-07-25 01:47:54 -0700948 action = CPU_UP_CANCELED;
949 ret = NOTIFY_BAD;
950 goto undo;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700951
952 case CPU_ONLINE:
Oleg Nesterov06ba38a2007-05-09 02:34:15 -0700953 start_workqueue_thread(cwq, cpu);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700954 break;
955
956 case CPU_UP_CANCELED:
Oleg Nesterov06ba38a2007-05-09 02:34:15 -0700957 start_workqueue_thread(cwq, -1);
Oleg Nesterov3da1c842008-07-25 01:47:50 -0700958 case CPU_POST_DEAD:
Oleg Nesterov1e35eaa2008-04-29 01:00:28 -0700959 cleanup_workqueue_thread(cwq);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700960 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
963
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -0700964 switch (action) {
965 case CPU_UP_CANCELED:
Oleg Nesterov3da1c842008-07-25 01:47:50 -0700966 case CPU_POST_DEAD:
Rusty Russelle7577c52009-01-01 10:12:25 +1030967 cpumask_clear_cpu(cpu, cpu_populated_map);
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -0700968 }
969
Oleg Nesterov84485022008-07-25 01:47:54 -0700970 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Rusty Russell2d3854a2008-11-05 13:39:10 +1100973#ifdef CONFIG_SMP
974struct work_for_cpu {
975 struct work_struct work;
976 long (*fn)(void *);
977 void *arg;
978 long ret;
979};
980
981static void do_work_for_cpu(struct work_struct *w)
982{
983 struct work_for_cpu *wfc = container_of(w, struct work_for_cpu, work);
984
985 wfc->ret = wfc->fn(wfc->arg);
986}
987
988/**
989 * work_on_cpu - run a function in user context on a particular cpu
990 * @cpu: the cpu to run on
991 * @fn: the function to run
992 * @arg: the function arg
993 *
994 * This will return -EINVAL in the cpu is not online, or the return value
995 * of @fn otherwise.
996 */
997long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
998{
999 struct work_for_cpu wfc;
1000
1001 INIT_WORK(&wfc.work, do_work_for_cpu);
1002 wfc.fn = fn;
1003 wfc.arg = arg;
1004 get_online_cpus();
1005 if (unlikely(!cpu_online(cpu)))
1006 wfc.ret = -EINVAL;
1007 else {
1008 schedule_work_on(cpu, &wfc.work);
1009 flush_work(&wfc.work);
1010 }
1011 put_online_cpus();
1012
1013 return wfc.ret;
1014}
1015EXPORT_SYMBOL_GPL(work_on_cpu);
1016#endif /* CONFIG_SMP */
1017
Oleg Nesterovc12920d2007-05-09 02:34:14 -07001018void __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Rusty Russelle7577c52009-01-01 10:12:25 +10301020 alloc_cpumask_var(&cpu_populated_map, GFP_KERNEL);
1021
1022 cpumask_copy(cpu_populated_map, cpu_online_mask);
1023 singlethread_cpu = cpumask_first(cpu_possible_mask);
1024 cpu_singlethread_map = cpumask_of(singlethread_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 hotcpu_notifier(workqueue_cpu_callback, 0);
1026 keventd_wq = create_workqueue("events");
1027 BUG_ON(!keventd_wq);
1028}