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