Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Christoph Lameter | 89ada67 | 2005-10-30 15:01:59 -0800 | [diff] [blame] | 15 | * |
| 16 | * Made to use alloc_percpu by Christoph Lameter <clameter@sgi.com>. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
James Bottomley | 1fa44ec | 2006-02-23 12:43:43 -0600 | [diff] [blame] | 30 | #include <linux/hardirq.h> |
Christoph Lameter | 46934023 | 2006-10-11 01:21:26 -0700 | [diff] [blame] | 31 | #include <linux/mempolicy.h> |
Rafael J. Wysocki | 341a595 | 2006-12-06 20:34:49 -0800 | [diff] [blame] | 32 | #include <linux/freezer.h> |
Peter Zijlstra | d5abe66 | 2006-12-06 20:37:26 -0800 | [diff] [blame] | 33 | #include <linux/kallsyms.h> |
| 34 | #include <linux/debug_locks.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| 36 | /* |
Nathan Lynch | f756d5e | 2006-01-08 01:05:12 -0800 | [diff] [blame] | 37 | * The per-CPU workqueue (if single thread, we always use the first |
| 38 | * possible cpu). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | */ |
| 40 | struct cpu_workqueue_struct { |
| 41 | |
| 42 | spinlock_t lock; |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | struct list_head worklist; |
| 45 | wait_queue_head_t more_work; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 46 | struct work_struct *current_work; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
| 48 | struct workqueue_struct *wq; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 49 | struct task_struct *thread; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 50 | int should_stop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 { |
Christoph Lameter | 89ada67 | 2005-10-30 15:01:59 -0800 | [diff] [blame] | 60 | struct cpu_workqueue_struct *cpu_wq; |
Oleg Nesterov | cce1a16 | 2007-05-09 02:34:13 -0700 | [diff] [blame] | 61 | struct list_head list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | const char *name; |
Oleg Nesterov | cce1a16 | 2007-05-09 02:34:13 -0700 | [diff] [blame] | 63 | int singlethread; |
Oleg Nesterov | 319c2a9 | 2007-05-09 02:34:06 -0700 | [diff] [blame] | 64 | int freezeable; /* Freeze threads during suspend */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. */ |
Andrew Morton | 9b41ea7 | 2006-08-13 23:24:26 -0700 | [diff] [blame] | 69 | static DEFINE_MUTEX(workqueue_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | static LIST_HEAD(workqueues); |
| 71 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 72 | static int singlethread_cpu __read_mostly; |
Oleg Nesterov | b1f4ec17 | 2007-05-09 02:34:12 -0700 | [diff] [blame] | 73 | static cpumask_t cpu_singlethread_map __read_mostly; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 74 | /* optimization, we could use cpu_possible_map */ |
| 75 | static cpumask_t cpu_populated_map __read_mostly; |
Nathan Lynch | f756d5e | 2006-01-08 01:05:12 -0800 | [diff] [blame] | 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | /* If it's single threaded, it isn't in the list of workqueues. */ |
| 78 | static inline int is_single_threaded(struct workqueue_struct *wq) |
| 79 | { |
Oleg Nesterov | cce1a16 | 2007-05-09 02:34:13 -0700 | [diff] [blame] | 80 | return wq->singlethread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Oleg Nesterov | b1f4ec17 | 2007-05-09 02:34:12 -0700 | [diff] [blame] | 83 | static const cpumask_t *wq_cpu_map(struct workqueue_struct *wq) |
| 84 | { |
| 85 | return is_single_threaded(wq) |
| 86 | ? &cpu_singlethread_map : &cpu_populated_map; |
| 87 | } |
| 88 | |
David Howells | 4594bf1 | 2006-12-07 11:33:26 +0000 | [diff] [blame] | 89 | /* |
| 90 | * Set the workqueue on which a work item is to be run |
| 91 | * - Must *only* be called if the pending flag is set |
| 92 | */ |
Oleg Nesterov | ed7c0fe | 2007-05-09 02:34:16 -0700 | [diff] [blame^] | 93 | static inline void set_wq_data(struct work_struct *work, |
| 94 | struct cpu_workqueue_struct *cwq) |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 95 | { |
David Howells | 4594bf1 | 2006-12-07 11:33:26 +0000 | [diff] [blame] | 96 | unsigned long new; |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 97 | |
David Howells | 4594bf1 | 2006-12-07 11:33:26 +0000 | [diff] [blame] | 98 | BUG_ON(!work_pending(work)); |
| 99 | |
Oleg Nesterov | ed7c0fe | 2007-05-09 02:34:16 -0700 | [diff] [blame^] | 100 | new = (unsigned long) cwq | (1UL << WORK_STRUCT_PENDING); |
Linus Torvalds | a08727b | 2006-12-16 09:53:50 -0800 | [diff] [blame] | 101 | new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work); |
| 102 | atomic_long_set(&work->data, new); |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Oleg Nesterov | ed7c0fe | 2007-05-09 02:34:16 -0700 | [diff] [blame^] | 105 | static inline |
| 106 | struct cpu_workqueue_struct *get_wq_data(struct work_struct *work) |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 107 | { |
Linus Torvalds | a08727b | 2006-12-16 09:53:50 -0800 | [diff] [blame] | 108 | return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK); |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 111 | static void insert_work(struct cpu_workqueue_struct *cwq, |
| 112 | struct work_struct *work, int tail) |
| 113 | { |
| 114 | set_wq_data(work, cwq); |
| 115 | if (tail) |
| 116 | list_add_tail(&work->entry, &cwq->worklist); |
| 117 | else |
| 118 | list_add(&work->entry, &cwq->worklist); |
| 119 | wake_up(&cwq->more_work); |
| 120 | } |
| 121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | /* Preempt must be disabled. */ |
| 123 | static void __queue_work(struct cpu_workqueue_struct *cwq, |
| 124 | struct work_struct *work) |
| 125 | { |
| 126 | unsigned long flags; |
| 127 | |
| 128 | spin_lock_irqsave(&cwq->lock, flags); |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 129 | insert_work(cwq, work, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | spin_unlock_irqrestore(&cwq->lock, flags); |
| 131 | } |
| 132 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 133 | /** |
| 134 | * queue_work - queue work on a workqueue |
| 135 | * @wq: workqueue to use |
| 136 | * @work: work to queue |
| 137 | * |
Alan Stern | 057647f | 2006-10-28 10:38:58 -0700 | [diff] [blame] | 138 | * Returns 0 if @work was already on a queue, non-zero otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | * |
| 140 | * We queue the work to the CPU it was submitted, but there is no |
| 141 | * guarantee that it will be processed by that CPU. |
| 142 | */ |
| 143 | int fastcall queue_work(struct workqueue_struct *wq, struct work_struct *work) |
| 144 | { |
| 145 | int ret = 0, cpu = get_cpu(); |
| 146 | |
Linus Torvalds | a08727b | 2006-12-16 09:53:50 -0800 | [diff] [blame] | 147 | if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | if (unlikely(is_single_threaded(wq))) |
Nathan Lynch | f756d5e | 2006-01-08 01:05:12 -0800 | [diff] [blame] | 149 | cpu = singlethread_cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | BUG_ON(!list_empty(&work->entry)); |
Christoph Lameter | 89ada67 | 2005-10-30 15:01:59 -0800 | [diff] [blame] | 151 | __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | ret = 1; |
| 153 | } |
| 154 | put_cpu(); |
| 155 | return ret; |
| 156 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 157 | EXPORT_SYMBOL_GPL(queue_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 159 | void delayed_work_timer_fn(unsigned long __data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | { |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 161 | struct delayed_work *dwork = (struct delayed_work *)__data; |
Oleg Nesterov | ed7c0fe | 2007-05-09 02:34:16 -0700 | [diff] [blame^] | 162 | struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work); |
| 163 | struct workqueue_struct *wq = cwq->wq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | int cpu = smp_processor_id(); |
| 165 | |
| 166 | if (unlikely(is_single_threaded(wq))) |
Nathan Lynch | f756d5e | 2006-01-08 01:05:12 -0800 | [diff] [blame] | 167 | cpu = singlethread_cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 169 | __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), &dwork->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 172 | /** |
| 173 | * queue_delayed_work - queue work on a workqueue after delay |
| 174 | * @wq: workqueue to use |
Randy Dunlap | af9997e | 2006-12-22 01:06:52 -0800 | [diff] [blame] | 175 | * @dwork: delayable work to queue |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 176 | * @delay: number of jiffies to wait before queueing |
| 177 | * |
Alan Stern | 057647f | 2006-10-28 10:38:58 -0700 | [diff] [blame] | 178 | * Returns 0 if @work was already on a queue, non-zero otherwise. |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 179 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | int fastcall queue_delayed_work(struct workqueue_struct *wq, |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 181 | struct delayed_work *dwork, unsigned long delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | { |
| 183 | int ret = 0; |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 184 | struct timer_list *timer = &dwork->timer; |
| 185 | struct work_struct *work = &dwork->work; |
| 186 | |
Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 187 | timer_stats_timer_set_start_info(timer); |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 188 | if (delay == 0) |
| 189 | return queue_work(wq, work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
Linus Torvalds | a08727b | 2006-12-16 09:53:50 -0800 | [diff] [blame] | 191 | if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | BUG_ON(timer_pending(timer)); |
| 193 | BUG_ON(!list_empty(&work->entry)); |
| 194 | |
Oleg Nesterov | ed7c0fe | 2007-05-09 02:34:16 -0700 | [diff] [blame^] | 195 | /* This stores cwq for the moment, for the timer_fn */ |
| 196 | set_wq_data(work, |
| 197 | per_cpu_ptr(wq->cpu_wq, raw_smp_processor_id())); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | timer->expires = jiffies + delay; |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 199 | timer->data = (unsigned long)dwork; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | timer->function = delayed_work_timer_fn; |
| 201 | add_timer(timer); |
| 202 | ret = 1; |
| 203 | } |
| 204 | return ret; |
| 205 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 206 | EXPORT_SYMBOL_GPL(queue_delayed_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 208 | /** |
| 209 | * queue_delayed_work_on - queue work on specific CPU after delay |
| 210 | * @cpu: CPU number to execute work on |
| 211 | * @wq: workqueue to use |
Randy Dunlap | af9997e | 2006-12-22 01:06:52 -0800 | [diff] [blame] | 212 | * @dwork: work to queue |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 213 | * @delay: number of jiffies to wait before queueing |
| 214 | * |
Alan Stern | 057647f | 2006-10-28 10:38:58 -0700 | [diff] [blame] | 215 | * Returns 0 if @work was already on a queue, non-zero otherwise. |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 216 | */ |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 217 | int queue_delayed_work_on(int cpu, struct workqueue_struct *wq, |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 218 | struct delayed_work *dwork, unsigned long delay) |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 219 | { |
| 220 | int ret = 0; |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 221 | struct timer_list *timer = &dwork->timer; |
| 222 | struct work_struct *work = &dwork->work; |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 223 | |
Linus Torvalds | a08727b | 2006-12-16 09:53:50 -0800 | [diff] [blame] | 224 | if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) { |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 225 | BUG_ON(timer_pending(timer)); |
| 226 | BUG_ON(!list_empty(&work->entry)); |
| 227 | |
Oleg Nesterov | ed7c0fe | 2007-05-09 02:34:16 -0700 | [diff] [blame^] | 228 | /* This stores cwq for the moment, for the timer_fn */ |
| 229 | set_wq_data(work, |
| 230 | per_cpu_ptr(wq->cpu_wq, raw_smp_processor_id())); |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 231 | timer->expires = jiffies + delay; |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 232 | timer->data = (unsigned long)dwork; |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 233 | timer->function = delayed_work_timer_fn; |
| 234 | add_timer_on(timer, cpu); |
| 235 | ret = 1; |
| 236 | } |
| 237 | return ret; |
| 238 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 239 | EXPORT_SYMBOL_GPL(queue_delayed_work_on); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 241 | static void run_workqueue(struct cpu_workqueue_struct *cwq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | { |
Oleg Nesterov | f293ea9 | 2007-05-09 02:34:10 -0700 | [diff] [blame] | 243 | spin_lock_irq(&cwq->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | cwq->run_depth++; |
| 245 | if (cwq->run_depth > 3) { |
| 246 | /* morton gets to eat his hat */ |
| 247 | printk("%s: recursion depth exceeded: %d\n", |
| 248 | __FUNCTION__, cwq->run_depth); |
| 249 | dump_stack(); |
| 250 | } |
| 251 | while (!list_empty(&cwq->worklist)) { |
| 252 | struct work_struct *work = list_entry(cwq->worklist.next, |
| 253 | struct work_struct, entry); |
David Howells | 6bb49e5 | 2006-11-22 14:54:45 +0000 | [diff] [blame] | 254 | work_func_t f = work->func; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 256 | cwq->current_work = work; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | list_del_init(cwq->worklist.next); |
Oleg Nesterov | f293ea9 | 2007-05-09 02:34:10 -0700 | [diff] [blame] | 258 | spin_unlock_irq(&cwq->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 260 | BUG_ON(get_wq_data(work) != cwq); |
Linus Torvalds | a08727b | 2006-12-16 09:53:50 -0800 | [diff] [blame] | 261 | if (!test_bit(WORK_STRUCT_NOAUTOREL, work_data_bits(work))) |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 262 | work_release(work); |
| 263 | f(work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
Peter Zijlstra | d5abe66 | 2006-12-06 20:37:26 -0800 | [diff] [blame] | 265 | if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { |
| 266 | printk(KERN_ERR "BUG: workqueue leaked lock or atomic: " |
| 267 | "%s/0x%08x/%d\n", |
| 268 | current->comm, preempt_count(), |
| 269 | current->pid); |
| 270 | printk(KERN_ERR " last function: "); |
| 271 | print_symbol("%s\n", (unsigned long)f); |
| 272 | debug_show_held_locks(current); |
| 273 | dump_stack(); |
| 274 | } |
| 275 | |
Oleg Nesterov | f293ea9 | 2007-05-09 02:34:10 -0700 | [diff] [blame] | 276 | spin_lock_irq(&cwq->lock); |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 277 | cwq->current_work = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } |
| 279 | cwq->run_depth--; |
Oleg Nesterov | f293ea9 | 2007-05-09 02:34:10 -0700 | [diff] [blame] | 280 | spin_unlock_irq(&cwq->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 283 | /* |
| 284 | * NOTE: the caller must not touch *cwq if this func returns true |
| 285 | */ |
| 286 | static int cwq_should_stop(struct cpu_workqueue_struct *cwq) |
| 287 | { |
| 288 | int should_stop = cwq->should_stop; |
| 289 | |
| 290 | if (unlikely(should_stop)) { |
| 291 | spin_lock_irq(&cwq->lock); |
| 292 | should_stop = cwq->should_stop && list_empty(&cwq->worklist); |
| 293 | if (should_stop) |
| 294 | cwq->thread = NULL; |
| 295 | spin_unlock_irq(&cwq->lock); |
| 296 | } |
| 297 | |
| 298 | return should_stop; |
| 299 | } |
| 300 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | static int worker_thread(void *__cwq) |
| 302 | { |
| 303 | struct cpu_workqueue_struct *cwq = __cwq; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 304 | DEFINE_WAIT(wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | struct k_sigaction sa; |
| 306 | sigset_t blocked; |
| 307 | |
Oleg Nesterov | 319c2a9 | 2007-05-09 02:34:06 -0700 | [diff] [blame] | 308 | if (!cwq->wq->freezeable) |
Rafael J. Wysocki | 341a595 | 2006-12-06 20:34:49 -0800 | [diff] [blame] | 309 | current->flags |= PF_NOFREEZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
| 311 | set_user_nice(current, -5); |
| 312 | |
| 313 | /* Block and flush all signals */ |
| 314 | sigfillset(&blocked); |
| 315 | sigprocmask(SIG_BLOCK, &blocked, NULL); |
| 316 | flush_signals(current); |
| 317 | |
Christoph Lameter | 46934023 | 2006-10-11 01:21:26 -0700 | [diff] [blame] | 318 | /* |
| 319 | * We inherited MPOL_INTERLEAVE from the booting kernel. |
| 320 | * Set MPOL_DEFAULT to insure node local allocations. |
| 321 | */ |
| 322 | numa_default_policy(); |
| 323 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | /* SIG_IGN makes children autoreap: see do_notify_parent(). */ |
| 325 | sa.sa.sa_handler = SIG_IGN; |
| 326 | sa.sa.sa_flags = 0; |
| 327 | siginitset(&sa.sa.sa_mask, sigmask(SIGCHLD)); |
| 328 | do_sigaction(SIGCHLD, &sa, (struct k_sigaction *)0); |
| 329 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 330 | for (;;) { |
Oleg Nesterov | 319c2a9 | 2007-05-09 02:34:06 -0700 | [diff] [blame] | 331 | if (cwq->wq->freezeable) |
Rafael J. Wysocki | 341a595 | 2006-12-06 20:34:49 -0800 | [diff] [blame] | 332 | try_to_freeze(); |
| 333 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 334 | prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE); |
| 335 | if (!cwq->should_stop && list_empty(&cwq->worklist)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | schedule(); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 337 | finish_wait(&cwq->more_work, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 339 | if (cwq_should_stop(cwq)) |
| 340 | break; |
| 341 | |
| 342 | run_workqueue(cwq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | } |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 344 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | return 0; |
| 346 | } |
| 347 | |
Oleg Nesterov | fc2e4d7 | 2007-05-09 02:33:51 -0700 | [diff] [blame] | 348 | struct wq_barrier { |
| 349 | struct work_struct work; |
| 350 | struct completion done; |
| 351 | }; |
| 352 | |
| 353 | static void wq_barrier_func(struct work_struct *work) |
| 354 | { |
| 355 | struct wq_barrier *barr = container_of(work, struct wq_barrier, work); |
| 356 | complete(&barr->done); |
| 357 | } |
| 358 | |
Oleg Nesterov | 83c2252 | 2007-05-09 02:33:54 -0700 | [diff] [blame] | 359 | static void insert_wq_barrier(struct cpu_workqueue_struct *cwq, |
| 360 | struct wq_barrier *barr, int tail) |
Oleg Nesterov | fc2e4d7 | 2007-05-09 02:33:51 -0700 | [diff] [blame] | 361 | { |
| 362 | INIT_WORK(&barr->work, wq_barrier_func); |
| 363 | __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work)); |
| 364 | |
| 365 | init_completion(&barr->done); |
Oleg Nesterov | 83c2252 | 2007-05-09 02:33:54 -0700 | [diff] [blame] | 366 | |
| 367 | insert_work(cwq, &barr->work, tail); |
Oleg Nesterov | fc2e4d7 | 2007-05-09 02:33:51 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | static void flush_cpu_workqueue(struct cpu_workqueue_struct *cwq) |
| 371 | { |
| 372 | if (cwq->thread == current) { |
| 373 | /* |
| 374 | * Probably keventd trying to flush its own queue. So simply run |
| 375 | * it by hand rather than deadlocking. |
| 376 | */ |
| 377 | run_workqueue(cwq); |
| 378 | } else { |
Oleg Nesterov | fc2e4d7 | 2007-05-09 02:33:51 -0700 | [diff] [blame] | 379 | struct wq_barrier barr; |
Oleg Nesterov | 83c2252 | 2007-05-09 02:33:54 -0700 | [diff] [blame] | 380 | int active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | |
Oleg Nesterov | 83c2252 | 2007-05-09 02:33:54 -0700 | [diff] [blame] | 382 | spin_lock_irq(&cwq->lock); |
| 383 | if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) { |
| 384 | insert_wq_barrier(cwq, &barr, 1); |
| 385 | active = 1; |
| 386 | } |
| 387 | spin_unlock_irq(&cwq->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
Oleg Nesterov | d721304 | 2007-05-09 02:34:07 -0700 | [diff] [blame] | 389 | if (active) |
Oleg Nesterov | 83c2252 | 2007-05-09 02:33:54 -0700 | [diff] [blame] | 390 | wait_for_completion(&barr.done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 394 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | * flush_workqueue - ensure that any scheduled work has run to completion. |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 396 | * @wq: workqueue to flush |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | * |
| 398 | * Forces execution of the workqueue and blocks until its completion. |
| 399 | * This is typically used in driver shutdown handlers. |
| 400 | * |
Oleg Nesterov | fc2e4d7 | 2007-05-09 02:33:51 -0700 | [diff] [blame] | 401 | * We sleep until all works which were queued on entry have been handled, |
| 402 | * but we are not livelocked by new incoming ones. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | * |
| 404 | * This function used to run the workqueues itself. Now we just wait for the |
| 405 | * helper threads to do it. |
| 406 | */ |
| 407 | void fastcall flush_workqueue(struct workqueue_struct *wq) |
| 408 | { |
Oleg Nesterov | b1f4ec17 | 2007-05-09 02:34:12 -0700 | [diff] [blame] | 409 | const cpumask_t *cpu_map = wq_cpu_map(wq); |
Oleg Nesterov | cce1a16 | 2007-05-09 02:34:13 -0700 | [diff] [blame] | 410 | int cpu; |
Oleg Nesterov | b1f4ec17 | 2007-05-09 02:34:12 -0700 | [diff] [blame] | 411 | |
Oleg Nesterov | f293ea9 | 2007-05-09 02:34:10 -0700 | [diff] [blame] | 412 | might_sleep(); |
Oleg Nesterov | b1f4ec17 | 2007-05-09 02:34:12 -0700 | [diff] [blame] | 413 | for_each_cpu_mask(cpu, *cpu_map) |
| 414 | flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 416 | EXPORT_SYMBOL_GPL(flush_workqueue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 418 | static void wait_on_work(struct cpu_workqueue_struct *cwq, |
| 419 | struct work_struct *work) |
| 420 | { |
| 421 | struct wq_barrier barr; |
| 422 | int running = 0; |
| 423 | |
| 424 | spin_lock_irq(&cwq->lock); |
| 425 | if (unlikely(cwq->current_work == work)) { |
Oleg Nesterov | 83c2252 | 2007-05-09 02:33:54 -0700 | [diff] [blame] | 426 | insert_wq_barrier(cwq, &barr, 0); |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 427 | running = 1; |
| 428 | } |
| 429 | spin_unlock_irq(&cwq->lock); |
| 430 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 431 | if (unlikely(running)) |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 432 | wait_for_completion(&barr.done); |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | /** |
| 436 | * flush_work - block until a work_struct's callback has terminated |
| 437 | * @wq: the workqueue on which the work is queued |
| 438 | * @work: the work which is to be flushed |
| 439 | * |
| 440 | * flush_work() will attempt to cancel the work if it is queued. If the work's |
| 441 | * callback appears to be running, flush_work() will block until it has |
| 442 | * completed. |
| 443 | * |
| 444 | * flush_work() is designed to be used when the caller is tearing down data |
| 445 | * structures which the callback function operates upon. It is expected that, |
| 446 | * prior to calling flush_work(), the caller has arranged for the work to not |
| 447 | * be requeued. |
| 448 | */ |
| 449 | void flush_work(struct workqueue_struct *wq, struct work_struct *work) |
| 450 | { |
Oleg Nesterov | b1f4ec17 | 2007-05-09 02:34:12 -0700 | [diff] [blame] | 451 | const cpumask_t *cpu_map = wq_cpu_map(wq); |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 452 | struct cpu_workqueue_struct *cwq; |
Oleg Nesterov | b1f4ec17 | 2007-05-09 02:34:12 -0700 | [diff] [blame] | 453 | int cpu; |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 454 | |
Oleg Nesterov | f293ea9 | 2007-05-09 02:34:10 -0700 | [diff] [blame] | 455 | might_sleep(); |
| 456 | |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 457 | cwq = get_wq_data(work); |
| 458 | /* Was it ever queued ? */ |
| 459 | if (!cwq) |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 460 | return; |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 461 | |
| 462 | /* |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 463 | * This work can't be re-queued, no need to re-check that |
| 464 | * get_wq_data() is still the same when we take cwq->lock. |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 465 | */ |
| 466 | spin_lock_irq(&cwq->lock); |
| 467 | list_del_init(&work->entry); |
| 468 | work_release(work); |
| 469 | spin_unlock_irq(&cwq->lock); |
| 470 | |
Oleg Nesterov | b1f4ec17 | 2007-05-09 02:34:12 -0700 | [diff] [blame] | 471 | for_each_cpu_mask(cpu, *cpu_map) |
| 472 | wait_on_work(per_cpu_ptr(wq->cpu_wq, cpu), work); |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 473 | } |
| 474 | EXPORT_SYMBOL_GPL(flush_work); |
| 475 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | |
| 477 | static struct workqueue_struct *keventd_wq; |
| 478 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 479 | /** |
| 480 | * schedule_work - put work task in global workqueue |
| 481 | * @work: job to be done |
| 482 | * |
| 483 | * This puts a job in the kernel-global workqueue. |
| 484 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | int fastcall schedule_work(struct work_struct *work) |
| 486 | { |
| 487 | return queue_work(keventd_wq, work); |
| 488 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 489 | EXPORT_SYMBOL(schedule_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 491 | /** |
| 492 | * schedule_delayed_work - put work task in global workqueue after delay |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 493 | * @dwork: job to be done |
| 494 | * @delay: number of jiffies to wait or 0 for immediate execution |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 495 | * |
| 496 | * After waiting for a given time this puts a job in the kernel-global |
| 497 | * workqueue. |
| 498 | */ |
Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 499 | int fastcall schedule_delayed_work(struct delayed_work *dwork, |
| 500 | unsigned long delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | { |
Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 502 | timer_stats_timer_set_start_info(&dwork->timer); |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 503 | return queue_delayed_work(keventd_wq, dwork, delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 505 | EXPORT_SYMBOL(schedule_delayed_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 507 | /** |
| 508 | * schedule_delayed_work_on - queue work in global workqueue on CPU after delay |
| 509 | * @cpu: cpu to use |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 510 | * @dwork: job to be done |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 511 | * @delay: number of jiffies to wait |
| 512 | * |
| 513 | * After waiting for a given time this puts a job in the kernel-global |
| 514 | * workqueue on the specified CPU. |
| 515 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | int schedule_delayed_work_on(int cpu, |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 517 | struct delayed_work *dwork, unsigned long delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | { |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 519 | return queue_delayed_work_on(cpu, keventd_wq, dwork, delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 521 | EXPORT_SYMBOL(schedule_delayed_work_on); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 523 | /** |
| 524 | * schedule_on_each_cpu - call a function on each online CPU from keventd |
| 525 | * @func: the function to call |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 526 | * |
| 527 | * Returns zero on success. |
| 528 | * Returns -ve errno on failure. |
| 529 | * |
| 530 | * Appears to be racy against CPU hotplug. |
| 531 | * |
| 532 | * schedule_on_each_cpu() is very slow. |
| 533 | */ |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 534 | int schedule_on_each_cpu(work_func_t func) |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 535 | { |
| 536 | int cpu; |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 537 | struct work_struct *works; |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 538 | |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 539 | works = alloc_percpu(struct work_struct); |
| 540 | if (!works) |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 541 | return -ENOMEM; |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 542 | |
Andrew Morton | e18f3ff | 2007-05-09 02:33:50 -0700 | [diff] [blame] | 543 | preempt_disable(); /* CPU hotplug */ |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 544 | for_each_online_cpu(cpu) { |
Ingo Molnar | 9bfb183 | 2006-12-18 20:05:09 +0100 | [diff] [blame] | 545 | struct work_struct *work = per_cpu_ptr(works, cpu); |
| 546 | |
| 547 | INIT_WORK(work, func); |
| 548 | set_bit(WORK_STRUCT_PENDING, work_data_bits(work)); |
| 549 | __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), work); |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 550 | } |
Andrew Morton | e18f3ff | 2007-05-09 02:33:50 -0700 | [diff] [blame] | 551 | preempt_enable(); |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 552 | flush_workqueue(keventd_wq); |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 553 | free_percpu(works); |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 554 | return 0; |
| 555 | } |
| 556 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | void flush_scheduled_work(void) |
| 558 | { |
| 559 | flush_workqueue(keventd_wq); |
| 560 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 561 | EXPORT_SYMBOL(flush_scheduled_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 563 | void flush_work_keventd(struct work_struct *work) |
| 564 | { |
| 565 | flush_work(keventd_wq, work); |
| 566 | } |
| 567 | EXPORT_SYMBOL(flush_work_keventd); |
| 568 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | /** |
Oleg Nesterov | ed7c0fe | 2007-05-09 02:34:16 -0700 | [diff] [blame^] | 570 | * cancel_rearming_delayed_workqueue - kill off a delayed work whose handler rearms the delayed work. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | * @wq: the controlling workqueue structure |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 572 | * @dwork: the delayed work struct |
Oleg Nesterov | ed7c0fe | 2007-05-09 02:34:16 -0700 | [diff] [blame^] | 573 | * |
| 574 | * Note that the work callback function may still be running on return from |
| 575 | * cancel_delayed_work(). Run flush_workqueue() or flush_work() to wait on it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | */ |
James Bottomley | 81ddef7 | 2005-04-16 15:23:59 -0700 | [diff] [blame] | 577 | void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq, |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 578 | struct delayed_work *dwork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | { |
Oleg Nesterov | dfb4b82 | 2007-05-09 02:34:11 -0700 | [diff] [blame] | 580 | /* Was it ever queued ? */ |
| 581 | if (!get_wq_data(&dwork->work)) |
| 582 | return; |
| 583 | |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 584 | while (!cancel_delayed_work(dwork)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | flush_workqueue(wq); |
| 586 | } |
James Bottomley | 81ddef7 | 2005-04-16 15:23:59 -0700 | [diff] [blame] | 587 | EXPORT_SYMBOL(cancel_rearming_delayed_workqueue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | |
| 589 | /** |
Oleg Nesterov | ed7c0fe | 2007-05-09 02:34:16 -0700 | [diff] [blame^] | 590 | * cancel_rearming_delayed_work - kill off a delayed keventd work whose handler rearms the delayed work. |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 591 | * @dwork: the delayed work struct |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | */ |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 593 | void cancel_rearming_delayed_work(struct delayed_work *dwork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | { |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 595 | cancel_rearming_delayed_workqueue(keventd_wq, dwork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | } |
| 597 | EXPORT_SYMBOL(cancel_rearming_delayed_work); |
| 598 | |
James Bottomley | 1fa44ec | 2006-02-23 12:43:43 -0600 | [diff] [blame] | 599 | /** |
| 600 | * execute_in_process_context - reliably execute the routine with user context |
| 601 | * @fn: the function to execute |
James Bottomley | 1fa44ec | 2006-02-23 12:43:43 -0600 | [diff] [blame] | 602 | * @ew: guaranteed storage for the execute work structure (must |
| 603 | * be available when the work executes) |
| 604 | * |
| 605 | * Executes the function immediately if process context is available, |
| 606 | * otherwise schedules the function for delayed execution. |
| 607 | * |
| 608 | * Returns: 0 - function was executed |
| 609 | * 1 - function was scheduled for execution |
| 610 | */ |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 611 | int execute_in_process_context(work_func_t fn, struct execute_work *ew) |
James Bottomley | 1fa44ec | 2006-02-23 12:43:43 -0600 | [diff] [blame] | 612 | { |
| 613 | if (!in_interrupt()) { |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 614 | fn(&ew->work); |
James Bottomley | 1fa44ec | 2006-02-23 12:43:43 -0600 | [diff] [blame] | 615 | return 0; |
| 616 | } |
| 617 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 618 | INIT_WORK(&ew->work, fn); |
James Bottomley | 1fa44ec | 2006-02-23 12:43:43 -0600 | [diff] [blame] | 619 | schedule_work(&ew->work); |
| 620 | |
| 621 | return 1; |
| 622 | } |
| 623 | EXPORT_SYMBOL_GPL(execute_in_process_context); |
| 624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | int keventd_up(void) |
| 626 | { |
| 627 | return keventd_wq != NULL; |
| 628 | } |
| 629 | |
| 630 | int current_is_keventd(void) |
| 631 | { |
| 632 | struct cpu_workqueue_struct *cwq; |
| 633 | int cpu = smp_processor_id(); /* preempt-safe: keventd is per-cpu */ |
| 634 | int ret = 0; |
| 635 | |
| 636 | BUG_ON(!keventd_wq); |
| 637 | |
Christoph Lameter | 89ada67 | 2005-10-30 15:01:59 -0800 | [diff] [blame] | 638 | cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | if (current == cwq->thread) |
| 640 | ret = 1; |
| 641 | |
| 642 | return ret; |
| 643 | |
| 644 | } |
| 645 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 646 | static struct cpu_workqueue_struct * |
| 647 | init_cpu_workqueue(struct workqueue_struct *wq, int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | { |
Christoph Lameter | 89ada67 | 2005-10-30 15:01:59 -0800 | [diff] [blame] | 649 | struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 651 | cwq->wq = wq; |
| 652 | spin_lock_init(&cwq->lock); |
| 653 | INIT_LIST_HEAD(&cwq->worklist); |
| 654 | init_waitqueue_head(&cwq->more_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 656 | return cwq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | } |
| 658 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 659 | static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | { |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 661 | struct workqueue_struct *wq = cwq->wq; |
| 662 | const char *fmt = is_single_threaded(wq) ? "%s" : "%s/%d"; |
| 663 | struct task_struct *p; |
| 664 | |
| 665 | p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu); |
| 666 | /* |
| 667 | * Nobody can add the work_struct to this cwq, |
| 668 | * if (caller is __create_workqueue) |
| 669 | * nobody should see this wq |
| 670 | * else // caller is CPU_UP_PREPARE |
| 671 | * cpu is not on cpu_online_map |
| 672 | * so we can abort safely. |
| 673 | */ |
| 674 | if (IS_ERR(p)) |
| 675 | return PTR_ERR(p); |
| 676 | |
| 677 | cwq->thread = p; |
| 678 | cwq->should_stop = 0; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 679 | |
| 680 | return 0; |
| 681 | } |
| 682 | |
Oleg Nesterov | 06ba38a | 2007-05-09 02:34:15 -0700 | [diff] [blame] | 683 | static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu) |
| 684 | { |
| 685 | struct task_struct *p = cwq->thread; |
| 686 | |
| 687 | if (p != NULL) { |
| 688 | if (cpu >= 0) |
| 689 | kthread_bind(p, cpu); |
| 690 | wake_up_process(p); |
| 691 | } |
| 692 | } |
| 693 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 694 | struct workqueue_struct *__create_workqueue(const char *name, |
| 695 | int singlethread, int freezeable) |
| 696 | { |
| 697 | struct workqueue_struct *wq; |
| 698 | struct cpu_workqueue_struct *cwq; |
| 699 | int err = 0, cpu; |
| 700 | |
| 701 | wq = kzalloc(sizeof(*wq), GFP_KERNEL); |
| 702 | if (!wq) |
| 703 | return NULL; |
| 704 | |
| 705 | wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct); |
| 706 | if (!wq->cpu_wq) { |
| 707 | kfree(wq); |
| 708 | return NULL; |
| 709 | } |
| 710 | |
| 711 | wq->name = name; |
Oleg Nesterov | cce1a16 | 2007-05-09 02:34:13 -0700 | [diff] [blame] | 712 | wq->singlethread = singlethread; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 713 | wq->freezeable = freezeable; |
Oleg Nesterov | cce1a16 | 2007-05-09 02:34:13 -0700 | [diff] [blame] | 714 | INIT_LIST_HEAD(&wq->list); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 715 | |
| 716 | if (singlethread) { |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 717 | cwq = init_cpu_workqueue(wq, singlethread_cpu); |
| 718 | err = create_workqueue_thread(cwq, singlethread_cpu); |
Oleg Nesterov | 06ba38a | 2007-05-09 02:34:15 -0700 | [diff] [blame] | 719 | start_workqueue_thread(cwq, -1); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 720 | } else { |
| 721 | mutex_lock(&workqueue_mutex); |
| 722 | list_add(&wq->list, &workqueues); |
| 723 | |
| 724 | for_each_possible_cpu(cpu) { |
| 725 | cwq = init_cpu_workqueue(wq, cpu); |
| 726 | if (err || !cpu_online(cpu)) |
| 727 | continue; |
| 728 | err = create_workqueue_thread(cwq, cpu); |
Oleg Nesterov | 06ba38a | 2007-05-09 02:34:15 -0700 | [diff] [blame] | 729 | start_workqueue_thread(cwq, cpu); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 730 | } |
| 731 | mutex_unlock(&workqueue_mutex); |
| 732 | } |
| 733 | |
| 734 | if (err) { |
| 735 | destroy_workqueue(wq); |
| 736 | wq = NULL; |
| 737 | } |
| 738 | return wq; |
| 739 | } |
| 740 | EXPORT_SYMBOL_GPL(__create_workqueue); |
| 741 | |
| 742 | static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu) |
| 743 | { |
| 744 | struct wq_barrier barr; |
| 745 | int alive = 0; |
| 746 | |
| 747 | spin_lock_irq(&cwq->lock); |
| 748 | if (cwq->thread != NULL) { |
| 749 | insert_wq_barrier(cwq, &barr, 1); |
| 750 | cwq->should_stop = 1; |
| 751 | alive = 1; |
| 752 | } |
| 753 | spin_unlock_irq(&cwq->lock); |
| 754 | |
| 755 | if (alive) { |
| 756 | wait_for_completion(&barr.done); |
| 757 | |
| 758 | while (unlikely(cwq->thread != NULL)) |
| 759 | cpu_relax(); |
| 760 | /* |
| 761 | * Wait until cwq->thread unlocks cwq->lock, |
| 762 | * it won't touch *cwq after that. |
| 763 | */ |
| 764 | smp_rmb(); |
| 765 | spin_unlock_wait(&cwq->lock); |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | /** |
| 770 | * destroy_workqueue - safely terminate a workqueue |
| 771 | * @wq: target workqueue |
| 772 | * |
| 773 | * Safely destroy a workqueue. All work currently pending will be done first. |
| 774 | */ |
| 775 | void destroy_workqueue(struct workqueue_struct *wq) |
| 776 | { |
Oleg Nesterov | b1f4ec17 | 2007-05-09 02:34:12 -0700 | [diff] [blame] | 777 | const cpumask_t *cpu_map = wq_cpu_map(wq); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 778 | struct cpu_workqueue_struct *cwq; |
Oleg Nesterov | b1f4ec17 | 2007-05-09 02:34:12 -0700 | [diff] [blame] | 779 | int cpu; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 780 | |
Oleg Nesterov | b1f4ec17 | 2007-05-09 02:34:12 -0700 | [diff] [blame] | 781 | mutex_lock(&workqueue_mutex); |
| 782 | list_del(&wq->list); |
| 783 | mutex_unlock(&workqueue_mutex); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 784 | |
Oleg Nesterov | b1f4ec17 | 2007-05-09 02:34:12 -0700 | [diff] [blame] | 785 | for_each_cpu_mask(cpu, *cpu_map) { |
| 786 | cwq = per_cpu_ptr(wq->cpu_wq, cpu); |
| 787 | cleanup_workqueue_thread(cwq, cpu); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | free_percpu(wq->cpu_wq); |
| 791 | kfree(wq); |
| 792 | } |
| 793 | EXPORT_SYMBOL_GPL(destroy_workqueue); |
| 794 | |
| 795 | static int __devinit workqueue_cpu_callback(struct notifier_block *nfb, |
| 796 | unsigned long action, |
| 797 | void *hcpu) |
| 798 | { |
| 799 | unsigned int cpu = (unsigned long)hcpu; |
| 800 | struct cpu_workqueue_struct *cwq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | struct workqueue_struct *wq; |
| 802 | |
| 803 | switch (action) { |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 804 | case CPU_LOCK_ACQUIRE: |
| 805 | mutex_lock(&workqueue_mutex); |
| 806 | return NOTIFY_OK; |
| 807 | |
| 808 | case CPU_LOCK_RELEASE: |
| 809 | mutex_unlock(&workqueue_mutex); |
| 810 | return NOTIFY_OK; |
| 811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | case CPU_UP_PREPARE: |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 813 | cpu_set(cpu, cpu_populated_map); |
| 814 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 816 | list_for_each_entry(wq, &workqueues, list) { |
| 817 | cwq = per_cpu_ptr(wq->cpu_wq, cpu); |
Christoph Lameter | 89ada67 | 2005-10-30 15:01:59 -0800 | [diff] [blame] | 818 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 819 | switch (action) { |
| 820 | case CPU_UP_PREPARE: |
| 821 | if (!create_workqueue_thread(cwq, cpu)) |
| 822 | break; |
| 823 | printk(KERN_ERR "workqueue for %i failed\n", cpu); |
| 824 | return NOTIFY_BAD; |
| 825 | |
| 826 | case CPU_ONLINE: |
Oleg Nesterov | 06ba38a | 2007-05-09 02:34:15 -0700 | [diff] [blame] | 827 | start_workqueue_thread(cwq, cpu); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 828 | break; |
| 829 | |
| 830 | case CPU_UP_CANCELED: |
Oleg Nesterov | 06ba38a | 2007-05-09 02:34:15 -0700 | [diff] [blame] | 831 | start_workqueue_thread(cwq, -1); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 832 | case CPU_DEAD: |
| 833 | cleanup_workqueue_thread(cwq, cpu); |
| 834 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | return NOTIFY_OK; |
| 839 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | |
Oleg Nesterov | c12920d | 2007-05-09 02:34:14 -0700 | [diff] [blame] | 841 | void __init init_workqueues(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | { |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 843 | cpu_populated_map = cpu_online_map; |
Nathan Lynch | f756d5e | 2006-01-08 01:05:12 -0800 | [diff] [blame] | 844 | singlethread_cpu = first_cpu(cpu_possible_map); |
Oleg Nesterov | b1f4ec17 | 2007-05-09 02:34:12 -0700 | [diff] [blame] | 845 | cpu_singlethread_map = cpumask_of_cpu(singlethread_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | hotcpu_notifier(workqueue_cpu_callback, 0); |
| 847 | keventd_wq = create_workqueue("events"); |
| 848 | BUG_ON(!keventd_wq); |
| 849 | } |