aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/workqueue.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/workqueue.c')
-rw-r--r--kernel/workqueue.c112
1 files changed, 80 insertions, 32 deletions
diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 6fd158b21026..ec7e4f62aaff 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -125,7 +125,7 @@ struct cpu_workqueue_struct *get_wq_data(struct work_struct *work)
125} 125}
126 126
127static void insert_work(struct cpu_workqueue_struct *cwq, 127static void insert_work(struct cpu_workqueue_struct *cwq,
128 struct work_struct *work, int tail) 128 struct work_struct *work, struct list_head *head)
129{ 129{
130 set_wq_data(work, cwq); 130 set_wq_data(work, cwq);
131 /* 131 /*
@@ -133,10 +133,7 @@ static void insert_work(struct cpu_workqueue_struct *cwq,
133 * result of list_add() below, see try_to_grab_pending(). 133 * result of list_add() below, see try_to_grab_pending().
134 */ 134 */
135 smp_wmb(); 135 smp_wmb();
136 if (tail) 136 list_add_tail(&work->entry, head);
137 list_add_tail(&work->entry, &cwq->worklist);
138 else
139 list_add(&work->entry, &cwq->worklist);
140 wake_up(&cwq->more_work); 137 wake_up(&cwq->more_work);
141} 138}
142 139
@@ -146,7 +143,7 @@ static void __queue_work(struct cpu_workqueue_struct *cwq,
146 unsigned long flags; 143 unsigned long flags;
147 144
148 spin_lock_irqsave(&cwq->lock, flags); 145 spin_lock_irqsave(&cwq->lock, flags);
149 insert_work(cwq, work, 1); 146 insert_work(cwq, work, &cwq->worklist);
150 spin_unlock_irqrestore(&cwq->lock, flags); 147 spin_unlock_irqrestore(&cwq->lock, flags);
151} 148}
152 149
@@ -162,14 +159,11 @@ static void __queue_work(struct cpu_workqueue_struct *cwq,
162 */ 159 */
163int queue_work(struct workqueue_struct *wq, struct work_struct *work) 160int queue_work(struct workqueue_struct *wq, struct work_struct *work)
164{ 161{
165 int ret = 0; 162 int ret;
163
164 ret = queue_work_on(get_cpu(), wq, work);
165 put_cpu();
166 166
167 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
168 BUG_ON(!list_empty(&work->entry));
169 __queue_work(wq_per_cpu(wq, get_cpu()), work);
170 put_cpu();
171 ret = 1;
172 }
173 return ret; 167 return ret;
174} 168}
175EXPORT_SYMBOL_GPL(queue_work); 169EXPORT_SYMBOL_GPL(queue_work);
@@ -361,14 +355,14 @@ static void wq_barrier_func(struct work_struct *work)
361} 355}
362 356
363static void insert_wq_barrier(struct cpu_workqueue_struct *cwq, 357static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
364 struct wq_barrier *barr, int tail) 358 struct wq_barrier *barr, struct list_head *head)
365{ 359{
366 INIT_WORK(&barr->work, wq_barrier_func); 360 INIT_WORK(&barr->work, wq_barrier_func);
367 __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work)); 361 __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work));
368 362
369 init_completion(&barr->done); 363 init_completion(&barr->done);
370 364
371 insert_work(cwq, &barr->work, tail); 365 insert_work(cwq, &barr->work, head);
372} 366}
373 367
374static int flush_cpu_workqueue(struct cpu_workqueue_struct *cwq) 368static int flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
@@ -388,7 +382,7 @@ static int flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
388 active = 0; 382 active = 0;
389 spin_lock_irq(&cwq->lock); 383 spin_lock_irq(&cwq->lock);
390 if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) { 384 if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
391 insert_wq_barrier(cwq, &barr, 1); 385 insert_wq_barrier(cwq, &barr, &cwq->worklist);
392 active = 1; 386 active = 1;
393 } 387 }
394 spin_unlock_irq(&cwq->lock); 388 spin_unlock_irq(&cwq->lock);
@@ -426,6 +420,57 @@ void flush_workqueue(struct workqueue_struct *wq)
426} 420}
427EXPORT_SYMBOL_GPL(flush_workqueue); 421EXPORT_SYMBOL_GPL(flush_workqueue);
428 422
423/**
424 * flush_work - block until a work_struct's callback has terminated
425 * @work: the work which is to be flushed
426 *
427 * Returns false if @work has already terminated.
428 *
429 * It is expected that, prior to calling flush_work(), the caller has
430 * arranged for the work to not be requeued, otherwise it doesn't make
431 * sense to use this function.
432 */
433int flush_work(struct work_struct *work)
434{
435 struct cpu_workqueue_struct *cwq;
436 struct list_head *prev;
437 struct wq_barrier barr;
438
439 might_sleep();
440 cwq = get_wq_data(work);
441 if (!cwq)
442 return 0;
443
444 lock_acquire(&cwq->wq->lockdep_map, 0, 0, 0, 2, _THIS_IP_);
445 lock_release(&cwq->wq->lockdep_map, 1, _THIS_IP_);
446
447 prev = NULL;
448 spin_lock_irq(&cwq->lock);
449 if (!list_empty(&work->entry)) {
450 /*
451 * See the comment near try_to_grab_pending()->smp_rmb().
452 * If it was re-queued under us we are not going to wait.
453 */
454 smp_rmb();
455 if (unlikely(cwq != get_wq_data(work)))
456 goto out;
457 prev = &work->entry;
458 } else {
459 if (cwq->current_work != work)
460 goto out;
461 prev = &cwq->worklist;
462 }
463 insert_wq_barrier(cwq, &barr, prev->next);
464out:
465 spin_unlock_irq(&cwq->lock);
466 if (!prev)
467 return 0;
468
469 wait_for_completion(&barr.done);
470 return 1;
471}
472EXPORT_SYMBOL_GPL(flush_work);
473
429/* 474/*
430 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit, 475 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
431 * so this work can't be re-armed in any way. 476 * so this work can't be re-armed in any way.
@@ -473,7 +518,7 @@ static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq,
473 518
474 spin_lock_irq(&cwq->lock); 519 spin_lock_irq(&cwq->lock);
475 if (unlikely(cwq->current_work == work)) { 520 if (unlikely(cwq->current_work == work)) {
476 insert_wq_barrier(cwq, &barr, 0); 521 insert_wq_barrier(cwq, &barr, cwq->worklist.next);
477 running = 1; 522 running = 1;
478 } 523 }
479 spin_unlock_irq(&cwq->lock); 524 spin_unlock_irq(&cwq->lock);
@@ -644,10 +689,10 @@ int schedule_on_each_cpu(work_func_t func)
644 struct work_struct *work = per_cpu_ptr(works, cpu); 689 struct work_struct *work = per_cpu_ptr(works, cpu);
645 690
646 INIT_WORK(work, func); 691 INIT_WORK(work, func);
647 set_bit(WORK_STRUCT_PENDING, work_data_bits(work)); 692 schedule_work_on(cpu, work);
648 __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), work);
649 } 693 }
650 flush_workqueue(keventd_wq); 694 for_each_online_cpu(cpu)
695 flush_work(per_cpu_ptr(works, cpu));
651 put_online_cpus(); 696 put_online_cpus();
652 free_percpu(works); 697 free_percpu(works);
653 return 0; 698 return 0;
@@ -784,7 +829,7 @@ struct workqueue_struct *__create_workqueue_key(const char *name,
784 err = create_workqueue_thread(cwq, singlethread_cpu); 829 err = create_workqueue_thread(cwq, singlethread_cpu);
785 start_workqueue_thread(cwq, -1); 830 start_workqueue_thread(cwq, -1);
786 } else { 831 } else {
787 get_online_cpus(); 832 cpu_maps_update_begin();
788 spin_lock(&workqueue_lock); 833 spin_lock(&workqueue_lock);
789 list_add(&wq->list, &workqueues); 834 list_add(&wq->list, &workqueues);
790 spin_unlock(&workqueue_lock); 835 spin_unlock(&workqueue_lock);
@@ -796,7 +841,7 @@ struct workqueue_struct *__create_workqueue_key(const char *name,
796 err = create_workqueue_thread(cwq, cpu); 841 err = create_workqueue_thread(cwq, cpu);
797 start_workqueue_thread(cwq, cpu); 842 start_workqueue_thread(cwq, cpu);
798 } 843 }
799 put_online_cpus(); 844 cpu_maps_update_done();
800 } 845 }
801 846
802 if (err) { 847 if (err) {
@@ -810,8 +855,8 @@ EXPORT_SYMBOL_GPL(__create_workqueue_key);
810static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq) 855static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq)
811{ 856{
812 /* 857 /*
813 * Our caller is either destroy_workqueue() or CPU_DEAD, 858 * Our caller is either destroy_workqueue() or CPU_POST_DEAD,
814 * get_online_cpus() protects cwq->thread. 859 * cpu_add_remove_lock protects cwq->thread.
815 */ 860 */
816 if (cwq->thread == NULL) 861 if (cwq->thread == NULL)
817 return; 862 return;
@@ -821,7 +866,7 @@ static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq)
821 866
822 flush_cpu_workqueue(cwq); 867 flush_cpu_workqueue(cwq);
823 /* 868 /*
824 * If the caller is CPU_DEAD and cwq->worklist was not empty, 869 * If the caller is CPU_POST_DEAD and cwq->worklist was not empty,
825 * a concurrent flush_workqueue() can insert a barrier after us. 870 * a concurrent flush_workqueue() can insert a barrier after us.
826 * However, in that case run_workqueue() won't return and check 871 * However, in that case run_workqueue() won't return and check
827 * kthread_should_stop() until it flushes all work_struct's. 872 * kthread_should_stop() until it flushes all work_struct's.
@@ -845,14 +890,14 @@ void destroy_workqueue(struct workqueue_struct *wq)
845 const cpumask_t *cpu_map = wq_cpu_map(wq); 890 const cpumask_t *cpu_map = wq_cpu_map(wq);
846 int cpu; 891 int cpu;
847 892
848 get_online_cpus(); 893 cpu_maps_update_begin();
849 spin_lock(&workqueue_lock); 894 spin_lock(&workqueue_lock);
850 list_del(&wq->list); 895 list_del(&wq->list);
851 spin_unlock(&workqueue_lock); 896 spin_unlock(&workqueue_lock);
852 897
853 for_each_cpu_mask_nr(cpu, *cpu_map) 898 for_each_cpu_mask_nr(cpu, *cpu_map)
854 cleanup_workqueue_thread(per_cpu_ptr(wq->cpu_wq, cpu)); 899 cleanup_workqueue_thread(per_cpu_ptr(wq->cpu_wq, cpu));
855 put_online_cpus(); 900 cpu_maps_update_done();
856 901
857 free_percpu(wq->cpu_wq); 902 free_percpu(wq->cpu_wq);
858 kfree(wq); 903 kfree(wq);
@@ -866,6 +911,7 @@ static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
866 unsigned int cpu = (unsigned long)hcpu; 911 unsigned int cpu = (unsigned long)hcpu;
867 struct cpu_workqueue_struct *cwq; 912 struct cpu_workqueue_struct *cwq;
868 struct workqueue_struct *wq; 913 struct workqueue_struct *wq;
914 int ret = NOTIFY_OK;
869 915
870 action &= ~CPU_TASKS_FROZEN; 916 action &= ~CPU_TASKS_FROZEN;
871 917
@@ -873,7 +919,7 @@ static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
873 case CPU_UP_PREPARE: 919 case CPU_UP_PREPARE:
874 cpu_set(cpu, cpu_populated_map); 920 cpu_set(cpu, cpu_populated_map);
875 } 921 }
876 922undo:
877 list_for_each_entry(wq, &workqueues, list) { 923 list_for_each_entry(wq, &workqueues, list) {
878 cwq = per_cpu_ptr(wq->cpu_wq, cpu); 924 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
879 925
@@ -883,7 +929,9 @@ static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
883 break; 929 break;
884 printk(KERN_ERR "workqueue [%s] for %i failed\n", 930 printk(KERN_ERR "workqueue [%s] for %i failed\n",
885 wq->name, cpu); 931 wq->name, cpu);
886 return NOTIFY_BAD; 932 action = CPU_UP_CANCELED;
933 ret = NOTIFY_BAD;
934 goto undo;
887 935
888 case CPU_ONLINE: 936 case CPU_ONLINE:
889 start_workqueue_thread(cwq, cpu); 937 start_workqueue_thread(cwq, cpu);
@@ -891,7 +939,7 @@ static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
891 939
892 case CPU_UP_CANCELED: 940 case CPU_UP_CANCELED:
893 start_workqueue_thread(cwq, -1); 941 start_workqueue_thread(cwq, -1);
894 case CPU_DEAD: 942 case CPU_POST_DEAD:
895 cleanup_workqueue_thread(cwq); 943 cleanup_workqueue_thread(cwq);
896 break; 944 break;
897 } 945 }
@@ -899,11 +947,11 @@ static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
899 947
900 switch (action) { 948 switch (action) {
901 case CPU_UP_CANCELED: 949 case CPU_UP_CANCELED:
902 case CPU_DEAD: 950 case CPU_POST_DEAD:
903 cpu_clear(cpu, cpu_populated_map); 951 cpu_clear(cpu, cpu_populated_map);
904 } 952 }
905 953
906 return NOTIFY_OK; 954 return ret;
907} 955}
908 956
909void __init init_workqueues(void) 957void __init init_workqueues(void)