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