2 * linux/kernel/workqueue.c
4 * Generic mechanism for defining kernel helper threads for running
5 * arbitrary tasks in process context.
7 * Started by Ingo Molnar, Copyright (C) 2002
9 * Derived from the taskqueue/keventd code by:
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>
16 * Made to use alloc_percpu by Christoph Lameter <clameter@sgi.com>.
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 #include <linux/hardirq.h>
33 * The per-CPU workqueue (if single thread, we always use the first
36 * The sequence counters are for flush_scheduled_work(). It wants to wait
37 * until until all currently-scheduled works are completed, but it doesn't
38 * want to be livelocked by new, incoming ones. So it waits until
39 * remove_sequence is >= the insert_sequence which pertained when
40 * flush_scheduled_work() was called.
42 struct cpu_workqueue_struct {
46 long remove_sequence; /* Least-recently added (next to run) */
47 long insert_sequence; /* Next to add */
49 struct list_head worklist;
50 wait_queue_head_t more_work;
51 wait_queue_head_t work_done;
53 struct workqueue_struct *wq;
56 int run_depth; /* Detect run_workqueue() recursion depth */
57 } ____cacheline_aligned;
60 * The externally visible workqueue abstraction is an array of
63 struct workqueue_struct {
64 struct cpu_workqueue_struct *cpu_wq;
66 struct list_head list; /* Empty if single thread */
69 /* All the per-cpu workqueues on the system, for hotplug cpu to add/remove
70 threads to each one as cpus come/go. */
71 static DEFINE_SPINLOCK(workqueue_lock);
72 static LIST_HEAD(workqueues);
74 static int singlethread_cpu;
76 /* If it's single threaded, it isn't in the list of workqueues. */
77 static inline int is_single_threaded(struct workqueue_struct *wq)
79 return list_empty(&wq->list);
82 /* Preempt must be disabled. */
83 static void __queue_work(struct cpu_workqueue_struct *cwq,
84 struct work_struct *work)
88 spin_lock_irqsave(&cwq->lock, flags);
90 list_add_tail(&work->entry, &cwq->worklist);
91 cwq->insert_sequence++;
92 wake_up(&cwq->more_work);
93 spin_unlock_irqrestore(&cwq->lock, flags);
97 * Queue work on a workqueue. Return non-zero if it was successfully
100 * We queue the work to the CPU it was submitted, but there is no
101 * guarantee that it will be processed by that CPU.
103 int fastcall queue_work(struct workqueue_struct *wq, struct work_struct *work)
105 int ret = 0, cpu = get_cpu();
107 if (!test_and_set_bit(0, &work->pending)) {
108 if (unlikely(is_single_threaded(wq)))
109 cpu = singlethread_cpu;
110 BUG_ON(!list_empty(&work->entry));
111 __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
118 static void delayed_work_timer_fn(unsigned long __data)
120 struct work_struct *work = (struct work_struct *)__data;
121 struct workqueue_struct *wq = work->wq_data;
122 int cpu = smp_processor_id();
124 if (unlikely(is_single_threaded(wq)))
125 cpu = singlethread_cpu;
127 __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
130 int fastcall queue_delayed_work(struct workqueue_struct *wq,
131 struct work_struct *work, unsigned long delay)
134 struct timer_list *timer = &work->timer;
136 if (!test_and_set_bit(0, &work->pending)) {
137 BUG_ON(timer_pending(timer));
138 BUG_ON(!list_empty(&work->entry));
140 /* This stores wq for the moment, for the timer_fn */
142 timer->expires = jiffies + delay;
143 timer->data = (unsigned long)work;
144 timer->function = delayed_work_timer_fn;
151 static void run_workqueue(struct cpu_workqueue_struct *cwq)
156 * Keep taking off work from the queue until
159 spin_lock_irqsave(&cwq->lock, flags);
161 if (cwq->run_depth > 3) {
162 /* morton gets to eat his hat */
163 printk("%s: recursion depth exceeded: %d\n",
164 __FUNCTION__, cwq->run_depth);
167 while (!list_empty(&cwq->worklist)) {
168 struct work_struct *work = list_entry(cwq->worklist.next,
169 struct work_struct, entry);
170 void (*f) (void *) = work->func;
171 void *data = work->data;
173 list_del_init(cwq->worklist.next);
174 spin_unlock_irqrestore(&cwq->lock, flags);
176 BUG_ON(work->wq_data != cwq);
177 clear_bit(0, &work->pending);
180 spin_lock_irqsave(&cwq->lock, flags);
181 cwq->remove_sequence++;
182 wake_up(&cwq->work_done);
185 spin_unlock_irqrestore(&cwq->lock, flags);
188 static int worker_thread(void *__cwq)
190 struct cpu_workqueue_struct *cwq = __cwq;
191 DECLARE_WAITQUEUE(wait, current);
192 struct k_sigaction sa;
195 current->flags |= PF_NOFREEZE;
197 set_user_nice(current, -5);
199 /* Block and flush all signals */
200 sigfillset(&blocked);
201 sigprocmask(SIG_BLOCK, &blocked, NULL);
202 flush_signals(current);
204 /* SIG_IGN makes children autoreap: see do_notify_parent(). */
205 sa.sa.sa_handler = SIG_IGN;
207 siginitset(&sa.sa.sa_mask, sigmask(SIGCHLD));
208 do_sigaction(SIGCHLD, &sa, (struct k_sigaction *)0);
210 set_current_state(TASK_INTERRUPTIBLE);
211 while (!kthread_should_stop()) {
212 add_wait_queue(&cwq->more_work, &wait);
213 if (list_empty(&cwq->worklist))
216 __set_current_state(TASK_RUNNING);
217 remove_wait_queue(&cwq->more_work, &wait);
219 if (!list_empty(&cwq->worklist))
221 set_current_state(TASK_INTERRUPTIBLE);
223 __set_current_state(TASK_RUNNING);
227 static void flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
229 if (cwq->thread == current) {
231 * Probably keventd trying to flush its own queue. So simply run
232 * it by hand rather than deadlocking.
237 long sequence_needed;
239 spin_lock_irq(&cwq->lock);
240 sequence_needed = cwq->insert_sequence;
242 while (sequence_needed - cwq->remove_sequence > 0) {
243 prepare_to_wait(&cwq->work_done, &wait,
244 TASK_UNINTERRUPTIBLE);
245 spin_unlock_irq(&cwq->lock);
247 spin_lock_irq(&cwq->lock);
249 finish_wait(&cwq->work_done, &wait);
250 spin_unlock_irq(&cwq->lock);
255 * flush_workqueue - ensure that any scheduled work has run to completion.
257 * Forces execution of the workqueue and blocks until its completion.
258 * This is typically used in driver shutdown handlers.
260 * This function will sample each workqueue's current insert_sequence number and
261 * will sleep until the head sequence is greater than or equal to that. This
262 * means that we sleep until all works which were queued on entry have been
263 * handled, but we are not livelocked by new incoming ones.
265 * This function used to run the workqueues itself. Now we just wait for the
266 * helper threads to do it.
268 void fastcall flush_workqueue(struct workqueue_struct *wq)
272 if (is_single_threaded(wq)) {
273 /* Always use first cpu's area. */
274 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, singlethread_cpu));
279 for_each_online_cpu(cpu)
280 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
281 unlock_cpu_hotplug();
285 static struct task_struct *create_workqueue_thread(struct workqueue_struct *wq,
288 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
289 struct task_struct *p;
291 spin_lock_init(&cwq->lock);
294 cwq->insert_sequence = 0;
295 cwq->remove_sequence = 0;
296 INIT_LIST_HEAD(&cwq->worklist);
297 init_waitqueue_head(&cwq->more_work);
298 init_waitqueue_head(&cwq->work_done);
300 if (is_single_threaded(wq))
301 p = kthread_create(worker_thread, cwq, "%s", wq->name);
303 p = kthread_create(worker_thread, cwq, "%s/%d", wq->name, cpu);
310 struct workqueue_struct *__create_workqueue(const char *name,
313 int cpu, destroy = 0;
314 struct workqueue_struct *wq;
315 struct task_struct *p;
317 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
321 wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
328 /* We don't need the distraction of CPUs appearing and vanishing. */
331 INIT_LIST_HEAD(&wq->list);
332 p = create_workqueue_thread(wq, singlethread_cpu);
338 spin_lock(&workqueue_lock);
339 list_add(&wq->list, &workqueues);
340 spin_unlock(&workqueue_lock);
341 for_each_online_cpu(cpu) {
342 p = create_workqueue_thread(wq, cpu);
344 kthread_bind(p, cpu);
350 unlock_cpu_hotplug();
353 * Was there any error during startup? If yes then clean up:
356 destroy_workqueue(wq);
362 static void cleanup_workqueue_thread(struct workqueue_struct *wq, int cpu)
364 struct cpu_workqueue_struct *cwq;
366 struct task_struct *p;
368 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
369 spin_lock_irqsave(&cwq->lock, flags);
372 spin_unlock_irqrestore(&cwq->lock, flags);
377 void destroy_workqueue(struct workqueue_struct *wq)
383 /* We don't need the distraction of CPUs appearing and vanishing. */
385 if (is_single_threaded(wq))
386 cleanup_workqueue_thread(wq, singlethread_cpu);
388 for_each_online_cpu(cpu)
389 cleanup_workqueue_thread(wq, cpu);
390 spin_lock(&workqueue_lock);
392 spin_unlock(&workqueue_lock);
394 unlock_cpu_hotplug();
395 free_percpu(wq->cpu_wq);
399 static struct workqueue_struct *keventd_wq;
401 int fastcall schedule_work(struct work_struct *work)
403 return queue_work(keventd_wq, work);
406 int fastcall schedule_delayed_work(struct work_struct *work, unsigned long delay)
408 return queue_delayed_work(keventd_wq, work, delay);
411 int schedule_delayed_work_on(int cpu,
412 struct work_struct *work, unsigned long delay)
415 struct timer_list *timer = &work->timer;
417 if (!test_and_set_bit(0, &work->pending)) {
418 BUG_ON(timer_pending(timer));
419 BUG_ON(!list_empty(&work->entry));
420 /* This stores keventd_wq for the moment, for the timer_fn */
421 work->wq_data = keventd_wq;
422 timer->expires = jiffies + delay;
423 timer->data = (unsigned long)work;
424 timer->function = delayed_work_timer_fn;
425 add_timer_on(timer, cpu);
431 int schedule_on_each_cpu(void (*func) (void *info), void *info)
434 struct work_struct *work;
436 work = kmalloc(NR_CPUS * sizeof(struct work_struct), GFP_KERNEL);
440 for_each_online_cpu(cpu) {
441 INIT_WORK(work + cpu, func, info);
442 __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu),
445 flush_workqueue(keventd_wq);
450 void flush_scheduled_work(void)
452 flush_workqueue(keventd_wq);
456 * cancel_rearming_delayed_workqueue - reliably kill off a delayed
457 * work whose handler rearms the delayed work.
458 * @wq: the controlling workqueue structure
459 * @work: the delayed work struct
461 void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq,
462 struct work_struct *work)
464 while (!cancel_delayed_work(work))
467 EXPORT_SYMBOL(cancel_rearming_delayed_workqueue);
470 * cancel_rearming_delayed_work - reliably kill off a delayed keventd
471 * work whose handler rearms the delayed work.
472 * @work: the delayed work struct
474 void cancel_rearming_delayed_work(struct work_struct *work)
476 cancel_rearming_delayed_workqueue(keventd_wq, work);
478 EXPORT_SYMBOL(cancel_rearming_delayed_work);
481 * execute_in_process_context - reliably execute the routine with user context
482 * @fn: the function to execute
483 * @data: data to pass to the function
484 * @ew: guaranteed storage for the execute work structure (must
485 * be available when the work executes)
487 * Executes the function immediately if process context is available,
488 * otherwise schedules the function for delayed execution.
490 * Returns: 0 - function was executed
491 * 1 - function was scheduled for execution
493 int execute_in_process_context(void (*fn)(void *data), void *data,
494 struct execute_work *ew)
496 if (!in_interrupt()) {
501 INIT_WORK(&ew->work, fn, data);
502 schedule_work(&ew->work);
506 EXPORT_SYMBOL_GPL(execute_in_process_context);
510 return keventd_wq != NULL;
513 int current_is_keventd(void)
515 struct cpu_workqueue_struct *cwq;
516 int cpu = smp_processor_id(); /* preempt-safe: keventd is per-cpu */
521 cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
522 if (current == cwq->thread)
529 #ifdef CONFIG_HOTPLUG_CPU
530 /* Take the work from this (downed) CPU. */
531 static void take_over_work(struct workqueue_struct *wq, unsigned int cpu)
533 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
535 struct work_struct *work;
537 spin_lock_irq(&cwq->lock);
538 list_splice_init(&cwq->worklist, &list);
540 while (!list_empty(&list)) {
541 printk("Taking work for %s\n", wq->name);
542 work = list_entry(list.next,struct work_struct,entry);
543 list_del(&work->entry);
544 __queue_work(per_cpu_ptr(wq->cpu_wq, smp_processor_id()), work);
546 spin_unlock_irq(&cwq->lock);
549 /* We're holding the cpucontrol mutex here */
550 static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
551 unsigned long action,
554 unsigned int hotcpu = (unsigned long)hcpu;
555 struct workqueue_struct *wq;
559 /* Create a new workqueue thread for it. */
560 list_for_each_entry(wq, &workqueues, list) {
561 if (!create_workqueue_thread(wq, hotcpu)) {
562 printk("workqueue for %i failed\n", hotcpu);
569 /* Kick off worker threads. */
570 list_for_each_entry(wq, &workqueues, list) {
571 struct cpu_workqueue_struct *cwq;
573 cwq = per_cpu_ptr(wq->cpu_wq, hotcpu);
574 kthread_bind(cwq->thread, hotcpu);
575 wake_up_process(cwq->thread);
579 case CPU_UP_CANCELED:
580 list_for_each_entry(wq, &workqueues, list) {
581 /* Unbind so it can run. */
582 kthread_bind(per_cpu_ptr(wq->cpu_wq, hotcpu)->thread,
583 any_online_cpu(cpu_online_map));
584 cleanup_workqueue_thread(wq, hotcpu);
589 list_for_each_entry(wq, &workqueues, list)
590 cleanup_workqueue_thread(wq, hotcpu);
591 list_for_each_entry(wq, &workqueues, list)
592 take_over_work(wq, hotcpu);
600 void init_workqueues(void)
602 singlethread_cpu = first_cpu(cpu_possible_map);
603 hotcpu_notifier(workqueue_cpu_callback, 0);
604 keventd_wq = create_workqueue("events");
608 EXPORT_SYMBOL_GPL(__create_workqueue);
609 EXPORT_SYMBOL_GPL(queue_work);
610 EXPORT_SYMBOL_GPL(queue_delayed_work);
611 EXPORT_SYMBOL_GPL(flush_workqueue);
612 EXPORT_SYMBOL_GPL(destroy_workqueue);
614 EXPORT_SYMBOL(schedule_work);
615 EXPORT_SYMBOL(schedule_delayed_work);
616 EXPORT_SYMBOL(schedule_delayed_work_on);
617 EXPORT_SYMBOL(flush_scheduled_work);