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