]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/workqueue.c
[PATCH] drivers/block: Use ARRAY_SIZE macro
[net-next-2.6.git] / kernel / workqueue.c
CommitLineData
1da177e4
LT
1/*
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>
89ada679
CL
15 *
16 * Made to use alloc_percpu by Christoph Lameter <clameter@sgi.com>.
1da177e4
LT
17 */
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>
30
31/*
32 * The per-CPU workqueue (if single thread, we always use cpu 0's).
33 *
34 * The sequence counters are for flush_scheduled_work(). It wants to wait
35 * until until all currently-scheduled works are completed, but it doesn't
36 * want to be livelocked by new, incoming ones. So it waits until
37 * remove_sequence is >= the insert_sequence which pertained when
38 * flush_scheduled_work() was called.
39 */
40struct cpu_workqueue_struct {
41
42 spinlock_t lock;
43
44 long remove_sequence; /* Least-recently added (next to run) */
45 long insert_sequence; /* Next to add */
46
47 struct list_head worklist;
48 wait_queue_head_t more_work;
49 wait_queue_head_t work_done;
50
51 struct workqueue_struct *wq;
52 task_t *thread;
53
54 int run_depth; /* Detect run_workqueue() recursion depth */
55} ____cacheline_aligned;
56
57/*
58 * The externally visible workqueue abstraction is an array of
59 * per-CPU workqueues:
60 */
61struct workqueue_struct {
89ada679 62 struct cpu_workqueue_struct *cpu_wq;
1da177e4
LT
63 const char *name;
64 struct list_head list; /* Empty if single thread */
65};
66
67/* All the per-cpu workqueues on the system, for hotplug cpu to add/remove
68 threads to each one as cpus come/go. */
69static DEFINE_SPINLOCK(workqueue_lock);
70static LIST_HEAD(workqueues);
71
72/* If it's single threaded, it isn't in the list of workqueues. */
73static inline int is_single_threaded(struct workqueue_struct *wq)
74{
75 return list_empty(&wq->list);
76}
77
78/* Preempt must be disabled. */
79static void __queue_work(struct cpu_workqueue_struct *cwq,
80 struct work_struct *work)
81{
82 unsigned long flags;
83
84 spin_lock_irqsave(&cwq->lock, flags);
85 work->wq_data = cwq;
86 list_add_tail(&work->entry, &cwq->worklist);
87 cwq->insert_sequence++;
88 wake_up(&cwq->more_work);
89 spin_unlock_irqrestore(&cwq->lock, flags);
90}
91
92/*
93 * Queue work on a workqueue. Return non-zero if it was successfully
94 * added.
95 *
96 * We queue the work to the CPU it was submitted, but there is no
97 * guarantee that it will be processed by that CPU.
98 */
99int fastcall queue_work(struct workqueue_struct *wq, struct work_struct *work)
100{
101 int ret = 0, cpu = get_cpu();
102
103 if (!test_and_set_bit(0, &work->pending)) {
104 if (unlikely(is_single_threaded(wq)))
bce61dd4 105 cpu = any_online_cpu(cpu_online_map);
1da177e4 106 BUG_ON(!list_empty(&work->entry));
89ada679 107 __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
1da177e4
LT
108 ret = 1;
109 }
110 put_cpu();
111 return ret;
112}
113
114static void delayed_work_timer_fn(unsigned long __data)
115{
116 struct work_struct *work = (struct work_struct *)__data;
117 struct workqueue_struct *wq = work->wq_data;
118 int cpu = smp_processor_id();
119
120 if (unlikely(is_single_threaded(wq)))
bce61dd4 121 cpu = any_online_cpu(cpu_online_map);
1da177e4 122
89ada679 123 __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
1da177e4
LT
124}
125
126int fastcall queue_delayed_work(struct workqueue_struct *wq,
127 struct work_struct *work, unsigned long delay)
128{
129 int ret = 0;
130 struct timer_list *timer = &work->timer;
131
132 if (!test_and_set_bit(0, &work->pending)) {
133 BUG_ON(timer_pending(timer));
134 BUG_ON(!list_empty(&work->entry));
135
136 /* This stores wq for the moment, for the timer_fn */
137 work->wq_data = wq;
138 timer->expires = jiffies + delay;
139 timer->data = (unsigned long)work;
140 timer->function = delayed_work_timer_fn;
141 add_timer(timer);
142 ret = 1;
143 }
144 return ret;
145}
146
147static inline void run_workqueue(struct cpu_workqueue_struct *cwq)
148{
149 unsigned long flags;
150
151 /*
152 * Keep taking off work from the queue until
153 * done.
154 */
155 spin_lock_irqsave(&cwq->lock, flags);
156 cwq->run_depth++;
157 if (cwq->run_depth > 3) {
158 /* morton gets to eat his hat */
159 printk("%s: recursion depth exceeded: %d\n",
160 __FUNCTION__, cwq->run_depth);
161 dump_stack();
162 }
163 while (!list_empty(&cwq->worklist)) {
164 struct work_struct *work = list_entry(cwq->worklist.next,
165 struct work_struct, entry);
166 void (*f) (void *) = work->func;
167 void *data = work->data;
168
169 list_del_init(cwq->worklist.next);
170 spin_unlock_irqrestore(&cwq->lock, flags);
171
172 BUG_ON(work->wq_data != cwq);
173 clear_bit(0, &work->pending);
174 f(data);
175
176 spin_lock_irqsave(&cwq->lock, flags);
177 cwq->remove_sequence++;
178 wake_up(&cwq->work_done);
179 }
180 cwq->run_depth--;
181 spin_unlock_irqrestore(&cwq->lock, flags);
182}
183
184static int worker_thread(void *__cwq)
185{
186 struct cpu_workqueue_struct *cwq = __cwq;
187 DECLARE_WAITQUEUE(wait, current);
188 struct k_sigaction sa;
189 sigset_t blocked;
190
191 current->flags |= PF_NOFREEZE;
192
193 set_user_nice(current, -5);
194
195 /* Block and flush all signals */
196 sigfillset(&blocked);
197 sigprocmask(SIG_BLOCK, &blocked, NULL);
198 flush_signals(current);
199
200 /* SIG_IGN makes children autoreap: see do_notify_parent(). */
201 sa.sa.sa_handler = SIG_IGN;
202 sa.sa.sa_flags = 0;
203 siginitset(&sa.sa.sa_mask, sigmask(SIGCHLD));
204 do_sigaction(SIGCHLD, &sa, (struct k_sigaction *)0);
205
206 set_current_state(TASK_INTERRUPTIBLE);
207 while (!kthread_should_stop()) {
208 add_wait_queue(&cwq->more_work, &wait);
209 if (list_empty(&cwq->worklist))
210 schedule();
211 else
212 __set_current_state(TASK_RUNNING);
213 remove_wait_queue(&cwq->more_work, &wait);
214
215 if (!list_empty(&cwq->worklist))
216 run_workqueue(cwq);
217 set_current_state(TASK_INTERRUPTIBLE);
218 }
219 __set_current_state(TASK_RUNNING);
220 return 0;
221}
222
223static void flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
224{
225 if (cwq->thread == current) {
226 /*
227 * Probably keventd trying to flush its own queue. So simply run
228 * it by hand rather than deadlocking.
229 */
230 run_workqueue(cwq);
231 } else {
232 DEFINE_WAIT(wait);
233 long sequence_needed;
234
235 spin_lock_irq(&cwq->lock);
236 sequence_needed = cwq->insert_sequence;
237
238 while (sequence_needed - cwq->remove_sequence > 0) {
239 prepare_to_wait(&cwq->work_done, &wait,
240 TASK_UNINTERRUPTIBLE);
241 spin_unlock_irq(&cwq->lock);
242 schedule();
243 spin_lock_irq(&cwq->lock);
244 }
245 finish_wait(&cwq->work_done, &wait);
246 spin_unlock_irq(&cwq->lock);
247 }
248}
249
250/*
251 * flush_workqueue - ensure that any scheduled work has run to completion.
252 *
253 * Forces execution of the workqueue and blocks until its completion.
254 * This is typically used in driver shutdown handlers.
255 *
256 * This function will sample each workqueue's current insert_sequence number and
257 * will sleep until the head sequence is greater than or equal to that. This
258 * means that we sleep until all works which were queued on entry have been
259 * handled, but we are not livelocked by new incoming ones.
260 *
261 * This function used to run the workqueues itself. Now we just wait for the
262 * helper threads to do it.
263 */
264void fastcall flush_workqueue(struct workqueue_struct *wq)
265{
266 might_sleep();
267
268 if (is_single_threaded(wq)) {
bce61dd4
BC
269 /* Always use first cpu's area. */
270 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, any_online_cpu(cpu_online_map)));
1da177e4
LT
271 } else {
272 int cpu;
273
274 lock_cpu_hotplug();
275 for_each_online_cpu(cpu)
89ada679 276 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
1da177e4
LT
277 unlock_cpu_hotplug();
278 }
279}
280
281static struct task_struct *create_workqueue_thread(struct workqueue_struct *wq,
282 int cpu)
283{
89ada679 284 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
1da177e4
LT
285 struct task_struct *p;
286
287 spin_lock_init(&cwq->lock);
288 cwq->wq = wq;
289 cwq->thread = NULL;
290 cwq->insert_sequence = 0;
291 cwq->remove_sequence = 0;
292 INIT_LIST_HEAD(&cwq->worklist);
293 init_waitqueue_head(&cwq->more_work);
294 init_waitqueue_head(&cwq->work_done);
295
296 if (is_single_threaded(wq))
297 p = kthread_create(worker_thread, cwq, "%s", wq->name);
298 else
299 p = kthread_create(worker_thread, cwq, "%s/%d", wq->name, cpu);
300 if (IS_ERR(p))
301 return NULL;
302 cwq->thread = p;
303 return p;
304}
305
306struct workqueue_struct *__create_workqueue(const char *name,
307 int singlethread)
308{
309 int cpu, destroy = 0;
310 struct workqueue_struct *wq;
311 struct task_struct *p;
312
dd392710 313 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
1da177e4
LT
314 if (!wq)
315 return NULL;
1da177e4 316
89ada679 317 wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
676121fc
BC
318 if (!wq->cpu_wq) {
319 kfree(wq);
320 return NULL;
321 }
322
1da177e4
LT
323 wq->name = name;
324 /* We don't need the distraction of CPUs appearing and vanishing. */
325 lock_cpu_hotplug();
326 if (singlethread) {
327 INIT_LIST_HEAD(&wq->list);
bce61dd4 328 p = create_workqueue_thread(wq, any_online_cpu(cpu_online_map));
1da177e4
LT
329 if (!p)
330 destroy = 1;
331 else
332 wake_up_process(p);
333 } else {
334 spin_lock(&workqueue_lock);
335 list_add(&wq->list, &workqueues);
336 spin_unlock(&workqueue_lock);
337 for_each_online_cpu(cpu) {
338 p = create_workqueue_thread(wq, cpu);
339 if (p) {
340 kthread_bind(p, cpu);
341 wake_up_process(p);
342 } else
343 destroy = 1;
344 }
345 }
346 unlock_cpu_hotplug();
347
348 /*
349 * Was there any error during startup? If yes then clean up:
350 */
351 if (destroy) {
352 destroy_workqueue(wq);
353 wq = NULL;
354 }
355 return wq;
356}
357
358static void cleanup_workqueue_thread(struct workqueue_struct *wq, int cpu)
359{
360 struct cpu_workqueue_struct *cwq;
361 unsigned long flags;
362 struct task_struct *p;
363
89ada679 364 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
1da177e4
LT
365 spin_lock_irqsave(&cwq->lock, flags);
366 p = cwq->thread;
367 cwq->thread = NULL;
368 spin_unlock_irqrestore(&cwq->lock, flags);
369 if (p)
370 kthread_stop(p);
371}
372
373void destroy_workqueue(struct workqueue_struct *wq)
374{
375 int cpu;
376
377 flush_workqueue(wq);
378
379 /* We don't need the distraction of CPUs appearing and vanishing. */
380 lock_cpu_hotplug();
381 if (is_single_threaded(wq))
bce61dd4 382 cleanup_workqueue_thread(wq, any_online_cpu(cpu_online_map));
1da177e4
LT
383 else {
384 for_each_online_cpu(cpu)
385 cleanup_workqueue_thread(wq, cpu);
386 spin_lock(&workqueue_lock);
387 list_del(&wq->list);
388 spin_unlock(&workqueue_lock);
389 }
390 unlock_cpu_hotplug();
89ada679 391 free_percpu(wq->cpu_wq);
1da177e4
LT
392 kfree(wq);
393}
394
395static struct workqueue_struct *keventd_wq;
396
397int fastcall schedule_work(struct work_struct *work)
398{
399 return queue_work(keventd_wq, work);
400}
401
402int fastcall schedule_delayed_work(struct work_struct *work, unsigned long delay)
403{
404 return queue_delayed_work(keventd_wq, work, delay);
405}
406
407int schedule_delayed_work_on(int cpu,
408 struct work_struct *work, unsigned long delay)
409{
410 int ret = 0;
411 struct timer_list *timer = &work->timer;
412
413 if (!test_and_set_bit(0, &work->pending)) {
414 BUG_ON(timer_pending(timer));
415 BUG_ON(!list_empty(&work->entry));
416 /* This stores keventd_wq for the moment, for the timer_fn */
417 work->wq_data = keventd_wq;
418 timer->expires = jiffies + delay;
419 timer->data = (unsigned long)work;
420 timer->function = delayed_work_timer_fn;
421 add_timer_on(timer, cpu);
422 ret = 1;
423 }
424 return ret;
425}
426
15316ba8
CL
427int schedule_on_each_cpu(void (*func) (void *info), void *info)
428{
429 int cpu;
430 struct work_struct *work;
431
432 work = kmalloc(NR_CPUS * sizeof(struct work_struct), GFP_KERNEL);
433
434 if (!work)
435 return -ENOMEM;
436 for_each_online_cpu(cpu) {
437 INIT_WORK(work + cpu, func, info);
438 __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu),
439 work + cpu);
440 }
441 flush_workqueue(keventd_wq);
442 kfree(work);
443 return 0;
444}
445
1da177e4
LT
446void flush_scheduled_work(void)
447{
448 flush_workqueue(keventd_wq);
449}
450
451/**
452 * cancel_rearming_delayed_workqueue - reliably kill off a delayed
453 * work whose handler rearms the delayed work.
454 * @wq: the controlling workqueue structure
455 * @work: the delayed work struct
456 */
81ddef77
JB
457void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq,
458 struct work_struct *work)
1da177e4
LT
459{
460 while (!cancel_delayed_work(work))
461 flush_workqueue(wq);
462}
81ddef77 463EXPORT_SYMBOL(cancel_rearming_delayed_workqueue);
1da177e4
LT
464
465/**
466 * cancel_rearming_delayed_work - reliably kill off a delayed keventd
467 * work whose handler rearms the delayed work.
468 * @work: the delayed work struct
469 */
470void cancel_rearming_delayed_work(struct work_struct *work)
471{
472 cancel_rearming_delayed_workqueue(keventd_wq, work);
473}
474EXPORT_SYMBOL(cancel_rearming_delayed_work);
475
476int keventd_up(void)
477{
478 return keventd_wq != NULL;
479}
480
481int current_is_keventd(void)
482{
483 struct cpu_workqueue_struct *cwq;
484 int cpu = smp_processor_id(); /* preempt-safe: keventd is per-cpu */
485 int ret = 0;
486
487 BUG_ON(!keventd_wq);
488
89ada679 489 cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
1da177e4
LT
490 if (current == cwq->thread)
491 ret = 1;
492
493 return ret;
494
495}
496
497#ifdef CONFIG_HOTPLUG_CPU
498/* Take the work from this (downed) CPU. */
499static void take_over_work(struct workqueue_struct *wq, unsigned int cpu)
500{
89ada679 501 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
1da177e4
LT
502 LIST_HEAD(list);
503 struct work_struct *work;
504
505 spin_lock_irq(&cwq->lock);
506 list_splice_init(&cwq->worklist, &list);
507
508 while (!list_empty(&list)) {
509 printk("Taking work for %s\n", wq->name);
510 work = list_entry(list.next,struct work_struct,entry);
511 list_del(&work->entry);
89ada679 512 __queue_work(per_cpu_ptr(wq->cpu_wq, smp_processor_id()), work);
1da177e4
LT
513 }
514 spin_unlock_irq(&cwq->lock);
515}
516
517/* We're holding the cpucontrol mutex here */
518static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
519 unsigned long action,
520 void *hcpu)
521{
522 unsigned int hotcpu = (unsigned long)hcpu;
523 struct workqueue_struct *wq;
524
525 switch (action) {
526 case CPU_UP_PREPARE:
527 /* Create a new workqueue thread for it. */
528 list_for_each_entry(wq, &workqueues, list) {
230649da 529 if (!create_workqueue_thread(wq, hotcpu)) {
1da177e4
LT
530 printk("workqueue for %i failed\n", hotcpu);
531 return NOTIFY_BAD;
532 }
533 }
534 break;
535
536 case CPU_ONLINE:
537 /* Kick off worker threads. */
538 list_for_each_entry(wq, &workqueues, list) {
89ada679
CL
539 struct cpu_workqueue_struct *cwq;
540
541 cwq = per_cpu_ptr(wq->cpu_wq, hotcpu);
542 kthread_bind(cwq->thread, hotcpu);
543 wake_up_process(cwq->thread);
1da177e4
LT
544 }
545 break;
546
547 case CPU_UP_CANCELED:
548 list_for_each_entry(wq, &workqueues, list) {
549 /* Unbind so it can run. */
89ada679 550 kthread_bind(per_cpu_ptr(wq->cpu_wq, hotcpu)->thread,
a4c4af7c 551 any_online_cpu(cpu_online_map));
1da177e4
LT
552 cleanup_workqueue_thread(wq, hotcpu);
553 }
554 break;
555
556 case CPU_DEAD:
557 list_for_each_entry(wq, &workqueues, list)
558 cleanup_workqueue_thread(wq, hotcpu);
559 list_for_each_entry(wq, &workqueues, list)
560 take_over_work(wq, hotcpu);
561 break;
562 }
563
564 return NOTIFY_OK;
565}
566#endif
567
568void init_workqueues(void)
569{
570 hotcpu_notifier(workqueue_cpu_callback, 0);
571 keventd_wq = create_workqueue("events");
572 BUG_ON(!keventd_wq);
573}
574
575EXPORT_SYMBOL_GPL(__create_workqueue);
576EXPORT_SYMBOL_GPL(queue_work);
577EXPORT_SYMBOL_GPL(queue_delayed_work);
578EXPORT_SYMBOL_GPL(flush_workqueue);
579EXPORT_SYMBOL_GPL(destroy_workqueue);
580
581EXPORT_SYMBOL(schedule_work);
582EXPORT_SYMBOL(schedule_delayed_work);
583EXPORT_SYMBOL(schedule_delayed_work_on);
584EXPORT_SYMBOL(flush_scheduled_work);