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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | /* |
Nathan Lynch | f756d5e | 2006-01-08 01:05:12 -0800 | [diff] [blame] | 34 | * The per-CPU workqueue (if single thread, we always use the first |
| 35 | * possible cpu). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | * |
| 37 | * The sequence counters are for flush_scheduled_work(). It wants to wait |
Rolf Eike Beer | 9f5d785 | 2006-10-03 23:07:31 +0200 | [diff] [blame] | 38 | * until all currently-scheduled works are completed, but it doesn't |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | * want to be livelocked by new, incoming ones. So it waits until |
| 40 | * remove_sequence is >= the insert_sequence which pertained when |
| 41 | * flush_scheduled_work() was called. |
| 42 | */ |
| 43 | struct cpu_workqueue_struct { |
| 44 | |
| 45 | spinlock_t lock; |
| 46 | |
| 47 | long remove_sequence; /* Least-recently added (next to run) */ |
| 48 | long insert_sequence; /* Next to add */ |
| 49 | |
| 50 | struct list_head worklist; |
| 51 | wait_queue_head_t more_work; |
| 52 | wait_queue_head_t work_done; |
| 53 | |
| 54 | struct workqueue_struct *wq; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 55 | struct task_struct *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | int run_depth; /* Detect run_workqueue() recursion depth */ |
| 58 | } ____cacheline_aligned; |
| 59 | |
| 60 | /* |
| 61 | * The externally visible workqueue abstraction is an array of |
| 62 | * per-CPU workqueues: |
| 63 | */ |
| 64 | struct workqueue_struct { |
Christoph Lameter | 89ada67 | 2005-10-30 15:01:59 -0800 | [diff] [blame] | 65 | struct cpu_workqueue_struct *cpu_wq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | const char *name; |
| 67 | struct list_head list; /* Empty if single thread */ |
| 68 | }; |
| 69 | |
| 70 | /* All the per-cpu workqueues on the system, for hotplug cpu to add/remove |
| 71 | threads to each one as cpus come/go. */ |
Andrew Morton | 9b41ea7 | 2006-08-13 23:24:26 -0700 | [diff] [blame] | 72 | static DEFINE_MUTEX(workqueue_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | static LIST_HEAD(workqueues); |
| 74 | |
Nathan Lynch | f756d5e | 2006-01-08 01:05:12 -0800 | [diff] [blame] | 75 | static int singlethread_cpu; |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | /* If it's single threaded, it isn't in the list of workqueues. */ |
| 78 | static inline int is_single_threaded(struct workqueue_struct *wq) |
| 79 | { |
| 80 | return list_empty(&wq->list); |
| 81 | } |
| 82 | |
| 83 | /* Preempt must be disabled. */ |
| 84 | static void __queue_work(struct cpu_workqueue_struct *cwq, |
| 85 | struct work_struct *work) |
| 86 | { |
| 87 | unsigned long flags; |
| 88 | |
| 89 | spin_lock_irqsave(&cwq->lock, flags); |
| 90 | work->wq_data = cwq; |
| 91 | list_add_tail(&work->entry, &cwq->worklist); |
| 92 | cwq->insert_sequence++; |
| 93 | wake_up(&cwq->more_work); |
| 94 | spin_unlock_irqrestore(&cwq->lock, flags); |
| 95 | } |
| 96 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 97 | /** |
| 98 | * queue_work - queue work on a workqueue |
| 99 | * @wq: workqueue to use |
| 100 | * @work: work to queue |
| 101 | * |
Alan Stern | 057647f | 2006-10-28 10:38:58 -0700 | [diff] [blame] | 102 | * Returns 0 if @work was already on a queue, non-zero otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | * |
| 104 | * We queue the work to the CPU it was submitted, but there is no |
| 105 | * guarantee that it will be processed by that CPU. |
| 106 | */ |
| 107 | int fastcall queue_work(struct workqueue_struct *wq, struct work_struct *work) |
| 108 | { |
| 109 | int ret = 0, cpu = get_cpu(); |
| 110 | |
| 111 | if (!test_and_set_bit(0, &work->pending)) { |
| 112 | if (unlikely(is_single_threaded(wq))) |
Nathan Lynch | f756d5e | 2006-01-08 01:05:12 -0800 | [diff] [blame] | 113 | cpu = singlethread_cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | BUG_ON(!list_empty(&work->entry)); |
Christoph Lameter | 89ada67 | 2005-10-30 15:01:59 -0800 | [diff] [blame] | 115 | __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | ret = 1; |
| 117 | } |
| 118 | put_cpu(); |
| 119 | return ret; |
| 120 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 121 | EXPORT_SYMBOL_GPL(queue_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
| 123 | static void delayed_work_timer_fn(unsigned long __data) |
| 124 | { |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 125 | struct delayed_work *dwork = (struct delayed_work *)__data; |
| 126 | struct workqueue_struct *wq = dwork->work.wq_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | int cpu = smp_processor_id(); |
| 128 | |
| 129 | if (unlikely(is_single_threaded(wq))) |
Nathan Lynch | f756d5e | 2006-01-08 01:05:12 -0800 | [diff] [blame] | 130 | cpu = singlethread_cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 132 | __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), &dwork->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 135 | /** |
| 136 | * queue_delayed_work - queue work on a workqueue after delay |
| 137 | * @wq: workqueue to use |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 138 | * @work: delayable work to queue |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 139 | * @delay: number of jiffies to wait before queueing |
| 140 | * |
Alan Stern | 057647f | 2006-10-28 10:38:58 -0700 | [diff] [blame] | 141 | * 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] | 142 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | int fastcall queue_delayed_work(struct workqueue_struct *wq, |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 144 | struct delayed_work *dwork, unsigned long delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | { |
| 146 | int ret = 0; |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 147 | struct timer_list *timer = &dwork->timer; |
| 148 | struct work_struct *work = &dwork->work; |
| 149 | |
| 150 | if (delay == 0) |
| 151 | return queue_work(wq, work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
| 153 | if (!test_and_set_bit(0, &work->pending)) { |
| 154 | BUG_ON(timer_pending(timer)); |
| 155 | BUG_ON(!list_empty(&work->entry)); |
| 156 | |
| 157 | /* This stores wq for the moment, for the timer_fn */ |
| 158 | work->wq_data = wq; |
| 159 | timer->expires = jiffies + delay; |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 160 | timer->data = (unsigned long)dwork; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | timer->function = delayed_work_timer_fn; |
| 162 | add_timer(timer); |
| 163 | ret = 1; |
| 164 | } |
| 165 | return ret; |
| 166 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 167 | EXPORT_SYMBOL_GPL(queue_delayed_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 169 | /** |
| 170 | * queue_delayed_work_on - queue work on specific CPU after delay |
| 171 | * @cpu: CPU number to execute work on |
| 172 | * @wq: workqueue to use |
| 173 | * @work: work to queue |
| 174 | * @delay: number of jiffies to wait before queueing |
| 175 | * |
Alan Stern | 057647f | 2006-10-28 10:38:58 -0700 | [diff] [blame] | 176 | * 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] | 177 | */ |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 178 | int queue_delayed_work_on(int cpu, struct workqueue_struct *wq, |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 179 | struct delayed_work *dwork, unsigned long delay) |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 180 | { |
| 181 | int ret = 0; |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 182 | struct timer_list *timer = &dwork->timer; |
| 183 | struct work_struct *work = &dwork->work; |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 184 | |
| 185 | if (!test_and_set_bit(0, &work->pending)) { |
| 186 | BUG_ON(timer_pending(timer)); |
| 187 | BUG_ON(!list_empty(&work->entry)); |
| 188 | |
| 189 | /* This stores wq for the moment, for the timer_fn */ |
| 190 | work->wq_data = wq; |
| 191 | timer->expires = jiffies + delay; |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 192 | timer->data = (unsigned long)dwork; |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 193 | timer->function = delayed_work_timer_fn; |
| 194 | add_timer_on(timer, cpu); |
| 195 | ret = 1; |
| 196 | } |
| 197 | return ret; |
| 198 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 199 | EXPORT_SYMBOL_GPL(queue_delayed_work_on); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 201 | static void run_workqueue(struct cpu_workqueue_struct *cwq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | { |
| 203 | unsigned long flags; |
| 204 | |
| 205 | /* |
| 206 | * Keep taking off work from the queue until |
| 207 | * done. |
| 208 | */ |
| 209 | spin_lock_irqsave(&cwq->lock, flags); |
| 210 | cwq->run_depth++; |
| 211 | if (cwq->run_depth > 3) { |
| 212 | /* morton gets to eat his hat */ |
| 213 | printk("%s: recursion depth exceeded: %d\n", |
| 214 | __FUNCTION__, cwq->run_depth); |
| 215 | dump_stack(); |
| 216 | } |
| 217 | while (!list_empty(&cwq->worklist)) { |
| 218 | struct work_struct *work = list_entry(cwq->worklist.next, |
| 219 | struct work_struct, entry); |
David Howells | 6bb49e5 | 2006-11-22 14:54:45 +0000 | [diff] [blame^] | 220 | work_func_t f = work->func; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | void *data = work->data; |
| 222 | |
| 223 | list_del_init(cwq->worklist.next); |
| 224 | spin_unlock_irqrestore(&cwq->lock, flags); |
| 225 | |
| 226 | BUG_ON(work->wq_data != cwq); |
| 227 | clear_bit(0, &work->pending); |
| 228 | f(data); |
| 229 | |
| 230 | spin_lock_irqsave(&cwq->lock, flags); |
| 231 | cwq->remove_sequence++; |
| 232 | wake_up(&cwq->work_done); |
| 233 | } |
| 234 | cwq->run_depth--; |
| 235 | spin_unlock_irqrestore(&cwq->lock, flags); |
| 236 | } |
| 237 | |
| 238 | static int worker_thread(void *__cwq) |
| 239 | { |
| 240 | struct cpu_workqueue_struct *cwq = __cwq; |
| 241 | DECLARE_WAITQUEUE(wait, current); |
| 242 | struct k_sigaction sa; |
| 243 | sigset_t blocked; |
| 244 | |
| 245 | current->flags |= PF_NOFREEZE; |
| 246 | |
| 247 | set_user_nice(current, -5); |
| 248 | |
| 249 | /* Block and flush all signals */ |
| 250 | sigfillset(&blocked); |
| 251 | sigprocmask(SIG_BLOCK, &blocked, NULL); |
| 252 | flush_signals(current); |
| 253 | |
Christoph Lameter | 4693402 | 2006-10-11 01:21:26 -0700 | [diff] [blame] | 254 | /* |
| 255 | * We inherited MPOL_INTERLEAVE from the booting kernel. |
| 256 | * Set MPOL_DEFAULT to insure node local allocations. |
| 257 | */ |
| 258 | numa_default_policy(); |
| 259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | /* SIG_IGN makes children autoreap: see do_notify_parent(). */ |
| 261 | sa.sa.sa_handler = SIG_IGN; |
| 262 | sa.sa.sa_flags = 0; |
| 263 | siginitset(&sa.sa.sa_mask, sigmask(SIGCHLD)); |
| 264 | do_sigaction(SIGCHLD, &sa, (struct k_sigaction *)0); |
| 265 | |
| 266 | set_current_state(TASK_INTERRUPTIBLE); |
| 267 | while (!kthread_should_stop()) { |
| 268 | add_wait_queue(&cwq->more_work, &wait); |
| 269 | if (list_empty(&cwq->worklist)) |
| 270 | schedule(); |
| 271 | else |
| 272 | __set_current_state(TASK_RUNNING); |
| 273 | remove_wait_queue(&cwq->more_work, &wait); |
| 274 | |
| 275 | if (!list_empty(&cwq->worklist)) |
| 276 | run_workqueue(cwq); |
| 277 | set_current_state(TASK_INTERRUPTIBLE); |
| 278 | } |
| 279 | __set_current_state(TASK_RUNNING); |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | static void flush_cpu_workqueue(struct cpu_workqueue_struct *cwq) |
| 284 | { |
| 285 | if (cwq->thread == current) { |
| 286 | /* |
| 287 | * Probably keventd trying to flush its own queue. So simply run |
| 288 | * it by hand rather than deadlocking. |
| 289 | */ |
| 290 | run_workqueue(cwq); |
| 291 | } else { |
| 292 | DEFINE_WAIT(wait); |
| 293 | long sequence_needed; |
| 294 | |
| 295 | spin_lock_irq(&cwq->lock); |
| 296 | sequence_needed = cwq->insert_sequence; |
| 297 | |
| 298 | while (sequence_needed - cwq->remove_sequence > 0) { |
| 299 | prepare_to_wait(&cwq->work_done, &wait, |
| 300 | TASK_UNINTERRUPTIBLE); |
| 301 | spin_unlock_irq(&cwq->lock); |
| 302 | schedule(); |
| 303 | spin_lock_irq(&cwq->lock); |
| 304 | } |
| 305 | finish_wait(&cwq->work_done, &wait); |
| 306 | spin_unlock_irq(&cwq->lock); |
| 307 | } |
| 308 | } |
| 309 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 310 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | * flush_workqueue - ensure that any scheduled work has run to completion. |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 312 | * @wq: workqueue to flush |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | * |
| 314 | * Forces execution of the workqueue and blocks until its completion. |
| 315 | * This is typically used in driver shutdown handlers. |
| 316 | * |
| 317 | * This function will sample each workqueue's current insert_sequence number and |
| 318 | * will sleep until the head sequence is greater than or equal to that. This |
| 319 | * means that we sleep until all works which were queued on entry have been |
| 320 | * handled, but we are not livelocked by new incoming ones. |
| 321 | * |
| 322 | * This function used to run the workqueues itself. Now we just wait for the |
| 323 | * helper threads to do it. |
| 324 | */ |
| 325 | void fastcall flush_workqueue(struct workqueue_struct *wq) |
| 326 | { |
| 327 | might_sleep(); |
| 328 | |
| 329 | if (is_single_threaded(wq)) { |
Ben Collins | bce61dd | 2005-11-28 13:43:56 -0800 | [diff] [blame] | 330 | /* Always use first cpu's area. */ |
Nathan Lynch | f756d5e | 2006-01-08 01:05:12 -0800 | [diff] [blame] | 331 | flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, singlethread_cpu)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | } else { |
| 333 | int cpu; |
| 334 | |
Andrew Morton | 9b41ea7 | 2006-08-13 23:24:26 -0700 | [diff] [blame] | 335 | mutex_lock(&workqueue_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | for_each_online_cpu(cpu) |
Christoph Lameter | 89ada67 | 2005-10-30 15:01:59 -0800 | [diff] [blame] | 337 | flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu)); |
Andrew Morton | 9b41ea7 | 2006-08-13 23:24:26 -0700 | [diff] [blame] | 338 | mutex_unlock(&workqueue_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | } |
| 340 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 341 | EXPORT_SYMBOL_GPL(flush_workqueue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
| 343 | static struct task_struct *create_workqueue_thread(struct workqueue_struct *wq, |
| 344 | int cpu) |
| 345 | { |
Christoph Lameter | 89ada67 | 2005-10-30 15:01:59 -0800 | [diff] [blame] | 346 | struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | struct task_struct *p; |
| 348 | |
| 349 | spin_lock_init(&cwq->lock); |
| 350 | cwq->wq = wq; |
| 351 | cwq->thread = NULL; |
| 352 | cwq->insert_sequence = 0; |
| 353 | cwq->remove_sequence = 0; |
| 354 | INIT_LIST_HEAD(&cwq->worklist); |
| 355 | init_waitqueue_head(&cwq->more_work); |
| 356 | init_waitqueue_head(&cwq->work_done); |
| 357 | |
| 358 | if (is_single_threaded(wq)) |
| 359 | p = kthread_create(worker_thread, cwq, "%s", wq->name); |
| 360 | else |
| 361 | p = kthread_create(worker_thread, cwq, "%s/%d", wq->name, cpu); |
| 362 | if (IS_ERR(p)) |
| 363 | return NULL; |
| 364 | cwq->thread = p; |
| 365 | return p; |
| 366 | } |
| 367 | |
| 368 | struct workqueue_struct *__create_workqueue(const char *name, |
| 369 | int singlethread) |
| 370 | { |
| 371 | int cpu, destroy = 0; |
| 372 | struct workqueue_struct *wq; |
| 373 | struct task_struct *p; |
| 374 | |
Pekka J Enberg | dd39271 | 2005-09-06 15:18:31 -0700 | [diff] [blame] | 375 | wq = kzalloc(sizeof(*wq), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | if (!wq) |
| 377 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
Christoph Lameter | 89ada67 | 2005-10-30 15:01:59 -0800 | [diff] [blame] | 379 | wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct); |
Ben Collins | 676121f | 2006-01-08 01:03:04 -0800 | [diff] [blame] | 380 | if (!wq->cpu_wq) { |
| 381 | kfree(wq); |
| 382 | return NULL; |
| 383 | } |
| 384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | wq->name = name; |
Andrew Morton | 9b41ea7 | 2006-08-13 23:24:26 -0700 | [diff] [blame] | 386 | mutex_lock(&workqueue_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | if (singlethread) { |
| 388 | INIT_LIST_HEAD(&wq->list); |
Nathan Lynch | f756d5e | 2006-01-08 01:05:12 -0800 | [diff] [blame] | 389 | p = create_workqueue_thread(wq, singlethread_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | if (!p) |
| 391 | destroy = 1; |
| 392 | else |
| 393 | wake_up_process(p); |
| 394 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | list_add(&wq->list, &workqueues); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | for_each_online_cpu(cpu) { |
| 397 | p = create_workqueue_thread(wq, cpu); |
| 398 | if (p) { |
| 399 | kthread_bind(p, cpu); |
| 400 | wake_up_process(p); |
| 401 | } else |
| 402 | destroy = 1; |
| 403 | } |
| 404 | } |
Andrew Morton | 9b41ea7 | 2006-08-13 23:24:26 -0700 | [diff] [blame] | 405 | mutex_unlock(&workqueue_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | |
| 407 | /* |
| 408 | * Was there any error during startup? If yes then clean up: |
| 409 | */ |
| 410 | if (destroy) { |
| 411 | destroy_workqueue(wq); |
| 412 | wq = NULL; |
| 413 | } |
| 414 | return wq; |
| 415 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 416 | EXPORT_SYMBOL_GPL(__create_workqueue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | |
| 418 | static void cleanup_workqueue_thread(struct workqueue_struct *wq, int cpu) |
| 419 | { |
| 420 | struct cpu_workqueue_struct *cwq; |
| 421 | unsigned long flags; |
| 422 | struct task_struct *p; |
| 423 | |
Christoph Lameter | 89ada67 | 2005-10-30 15:01:59 -0800 | [diff] [blame] | 424 | cwq = per_cpu_ptr(wq->cpu_wq, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | spin_lock_irqsave(&cwq->lock, flags); |
| 426 | p = cwq->thread; |
| 427 | cwq->thread = NULL; |
| 428 | spin_unlock_irqrestore(&cwq->lock, flags); |
| 429 | if (p) |
| 430 | kthread_stop(p); |
| 431 | } |
| 432 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 433 | /** |
| 434 | * destroy_workqueue - safely terminate a workqueue |
| 435 | * @wq: target workqueue |
| 436 | * |
| 437 | * Safely destroy a workqueue. All work currently pending will be done first. |
| 438 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | void destroy_workqueue(struct workqueue_struct *wq) |
| 440 | { |
| 441 | int cpu; |
| 442 | |
| 443 | flush_workqueue(wq); |
| 444 | |
| 445 | /* We don't need the distraction of CPUs appearing and vanishing. */ |
Andrew Morton | 9b41ea7 | 2006-08-13 23:24:26 -0700 | [diff] [blame] | 446 | mutex_lock(&workqueue_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | if (is_single_threaded(wq)) |
Nathan Lynch | f756d5e | 2006-01-08 01:05:12 -0800 | [diff] [blame] | 448 | cleanup_workqueue_thread(wq, singlethread_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | else { |
| 450 | for_each_online_cpu(cpu) |
| 451 | cleanup_workqueue_thread(wq, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | list_del(&wq->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | } |
Andrew Morton | 9b41ea7 | 2006-08-13 23:24:26 -0700 | [diff] [blame] | 454 | mutex_unlock(&workqueue_mutex); |
Christoph Lameter | 89ada67 | 2005-10-30 15:01:59 -0800 | [diff] [blame] | 455 | free_percpu(wq->cpu_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | kfree(wq); |
| 457 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 458 | EXPORT_SYMBOL_GPL(destroy_workqueue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | |
| 460 | static struct workqueue_struct *keventd_wq; |
| 461 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 462 | /** |
| 463 | * schedule_work - put work task in global workqueue |
| 464 | * @work: job to be done |
| 465 | * |
| 466 | * This puts a job in the kernel-global workqueue. |
| 467 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | int fastcall schedule_work(struct work_struct *work) |
| 469 | { |
| 470 | return queue_work(keventd_wq, work); |
| 471 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 472 | EXPORT_SYMBOL(schedule_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 474 | /** |
| 475 | * schedule_delayed_work - put work task in global workqueue after delay |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 476 | * @dwork: job to be done |
| 477 | * @delay: number of jiffies to wait or 0 for immediate execution |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 478 | * |
| 479 | * After waiting for a given time this puts a job in the kernel-global |
| 480 | * workqueue. |
| 481 | */ |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 482 | int fastcall schedule_delayed_work(struct delayed_work *dwork, unsigned long delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | { |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 484 | return queue_delayed_work(keventd_wq, dwork, delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 486 | EXPORT_SYMBOL(schedule_delayed_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 488 | /** |
| 489 | * schedule_delayed_work_on - queue work in global workqueue on CPU after delay |
| 490 | * @cpu: cpu to use |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 491 | * @dwork: job to be done |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 492 | * @delay: number of jiffies to wait |
| 493 | * |
| 494 | * After waiting for a given time this puts a job in the kernel-global |
| 495 | * workqueue on the specified CPU. |
| 496 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | int schedule_delayed_work_on(int cpu, |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 498 | struct delayed_work *dwork, unsigned long delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | { |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 500 | return queue_delayed_work_on(cpu, keventd_wq, dwork, delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 502 | EXPORT_SYMBOL(schedule_delayed_work_on); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 504 | /** |
| 505 | * schedule_on_each_cpu - call a function on each online CPU from keventd |
| 506 | * @func: the function to call |
| 507 | * @info: a pointer to pass to func() |
| 508 | * |
| 509 | * Returns zero on success. |
| 510 | * Returns -ve errno on failure. |
| 511 | * |
| 512 | * Appears to be racy against CPU hotplug. |
| 513 | * |
| 514 | * schedule_on_each_cpu() is very slow. |
| 515 | */ |
David Howells | 6bb49e5 | 2006-11-22 14:54:45 +0000 | [diff] [blame^] | 516 | int schedule_on_each_cpu(work_func_t func, void *info) |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 517 | { |
| 518 | int cpu; |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 519 | struct work_struct *works; |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 520 | |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 521 | works = alloc_percpu(struct work_struct); |
| 522 | if (!works) |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 523 | return -ENOMEM; |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 524 | |
Andrew Morton | 9b41ea7 | 2006-08-13 23:24:26 -0700 | [diff] [blame] | 525 | mutex_lock(&workqueue_mutex); |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 526 | for_each_online_cpu(cpu) { |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 527 | INIT_WORK(per_cpu_ptr(works, cpu), func, info); |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 528 | __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 529 | per_cpu_ptr(works, cpu)); |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 530 | } |
Andrew Morton | 9b41ea7 | 2006-08-13 23:24:26 -0700 | [diff] [blame] | 531 | mutex_unlock(&workqueue_mutex); |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 532 | flush_workqueue(keventd_wq); |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 533 | free_percpu(works); |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 534 | return 0; |
| 535 | } |
| 536 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | void flush_scheduled_work(void) |
| 538 | { |
| 539 | flush_workqueue(keventd_wq); |
| 540 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 541 | EXPORT_SYMBOL(flush_scheduled_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | |
| 543 | /** |
| 544 | * cancel_rearming_delayed_workqueue - reliably kill off a delayed |
| 545 | * work whose handler rearms the delayed work. |
| 546 | * @wq: the controlling workqueue structure |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 547 | * @dwork: the delayed work struct |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | */ |
James Bottomley | 81ddef7 | 2005-04-16 15:23:59 -0700 | [diff] [blame] | 549 | void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq, |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 550 | struct delayed_work *dwork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | { |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 552 | while (!cancel_delayed_work(dwork)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | flush_workqueue(wq); |
| 554 | } |
James Bottomley | 81ddef7 | 2005-04-16 15:23:59 -0700 | [diff] [blame] | 555 | EXPORT_SYMBOL(cancel_rearming_delayed_workqueue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | |
| 557 | /** |
| 558 | * cancel_rearming_delayed_work - reliably kill off a delayed keventd |
| 559 | * work whose handler rearms the delayed work. |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 560 | * @dwork: the delayed work struct |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | */ |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 562 | void cancel_rearming_delayed_work(struct delayed_work *dwork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | { |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 564 | cancel_rearming_delayed_workqueue(keventd_wq, dwork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | } |
| 566 | EXPORT_SYMBOL(cancel_rearming_delayed_work); |
| 567 | |
James Bottomley | 1fa44ec | 2006-02-23 12:43:43 -0600 | [diff] [blame] | 568 | /** |
| 569 | * execute_in_process_context - reliably execute the routine with user context |
| 570 | * @fn: the function to execute |
| 571 | * @data: data to pass to the function |
| 572 | * @ew: guaranteed storage for the execute work structure (must |
| 573 | * be available when the work executes) |
| 574 | * |
| 575 | * Executes the function immediately if process context is available, |
| 576 | * otherwise schedules the function for delayed execution. |
| 577 | * |
| 578 | * Returns: 0 - function was executed |
| 579 | * 1 - function was scheduled for execution |
| 580 | */ |
David Howells | 6bb49e5 | 2006-11-22 14:54:45 +0000 | [diff] [blame^] | 581 | int execute_in_process_context(work_func_t fn, void *data, |
James Bottomley | 1fa44ec | 2006-02-23 12:43:43 -0600 | [diff] [blame] | 582 | struct execute_work *ew) |
| 583 | { |
| 584 | if (!in_interrupt()) { |
| 585 | fn(data); |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | INIT_WORK(&ew->work, fn, data); |
| 590 | schedule_work(&ew->work); |
| 591 | |
| 592 | return 1; |
| 593 | } |
| 594 | EXPORT_SYMBOL_GPL(execute_in_process_context); |
| 595 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | int keventd_up(void) |
| 597 | { |
| 598 | return keventd_wq != NULL; |
| 599 | } |
| 600 | |
| 601 | int current_is_keventd(void) |
| 602 | { |
| 603 | struct cpu_workqueue_struct *cwq; |
| 604 | int cpu = smp_processor_id(); /* preempt-safe: keventd is per-cpu */ |
| 605 | int ret = 0; |
| 606 | |
| 607 | BUG_ON(!keventd_wq); |
| 608 | |
Christoph Lameter | 89ada67 | 2005-10-30 15:01:59 -0800 | [diff] [blame] | 609 | cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | if (current == cwq->thread) |
| 611 | ret = 1; |
| 612 | |
| 613 | return ret; |
| 614 | |
| 615 | } |
| 616 | |
| 617 | #ifdef CONFIG_HOTPLUG_CPU |
| 618 | /* Take the work from this (downed) CPU. */ |
| 619 | static void take_over_work(struct workqueue_struct *wq, unsigned int cpu) |
| 620 | { |
Christoph Lameter | 89ada67 | 2005-10-30 15:01:59 -0800 | [diff] [blame] | 621 | struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu); |
Oleg Nesterov | 626ab0e | 2006-06-23 02:05:55 -0700 | [diff] [blame] | 622 | struct list_head list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | struct work_struct *work; |
| 624 | |
| 625 | spin_lock_irq(&cwq->lock); |
Oleg Nesterov | 626ab0e | 2006-06-23 02:05:55 -0700 | [diff] [blame] | 626 | list_replace_init(&cwq->worklist, &list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | |
| 628 | while (!list_empty(&list)) { |
| 629 | printk("Taking work for %s\n", wq->name); |
| 630 | work = list_entry(list.next,struct work_struct,entry); |
| 631 | list_del(&work->entry); |
Christoph Lameter | 89ada67 | 2005-10-30 15:01:59 -0800 | [diff] [blame] | 632 | __queue_work(per_cpu_ptr(wq->cpu_wq, smp_processor_id()), work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | } |
| 634 | spin_unlock_irq(&cwq->lock); |
| 635 | } |
| 636 | |
| 637 | /* We're holding the cpucontrol mutex here */ |
Chandra Seetharaman | 9c7b216 | 2006-06-27 02:54:07 -0700 | [diff] [blame] | 638 | static int __devinit workqueue_cpu_callback(struct notifier_block *nfb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | unsigned long action, |
| 640 | void *hcpu) |
| 641 | { |
| 642 | unsigned int hotcpu = (unsigned long)hcpu; |
| 643 | struct workqueue_struct *wq; |
| 644 | |
| 645 | switch (action) { |
| 646 | case CPU_UP_PREPARE: |
Andrew Morton | 9b41ea7 | 2006-08-13 23:24:26 -0700 | [diff] [blame] | 647 | mutex_lock(&workqueue_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | /* Create a new workqueue thread for it. */ |
| 649 | list_for_each_entry(wq, &workqueues, list) { |
Mika Kukkonen | 230649d | 2005-09-06 15:17:17 -0700 | [diff] [blame] | 650 | if (!create_workqueue_thread(wq, hotcpu)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | printk("workqueue for %i failed\n", hotcpu); |
| 652 | return NOTIFY_BAD; |
| 653 | } |
| 654 | } |
| 655 | break; |
| 656 | |
| 657 | case CPU_ONLINE: |
| 658 | /* Kick off worker threads. */ |
| 659 | list_for_each_entry(wq, &workqueues, list) { |
Christoph Lameter | 89ada67 | 2005-10-30 15:01:59 -0800 | [diff] [blame] | 660 | struct cpu_workqueue_struct *cwq; |
| 661 | |
| 662 | cwq = per_cpu_ptr(wq->cpu_wq, hotcpu); |
| 663 | kthread_bind(cwq->thread, hotcpu); |
| 664 | wake_up_process(cwq->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | } |
Andrew Morton | 9b41ea7 | 2006-08-13 23:24:26 -0700 | [diff] [blame] | 666 | mutex_unlock(&workqueue_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | break; |
| 668 | |
| 669 | case CPU_UP_CANCELED: |
| 670 | list_for_each_entry(wq, &workqueues, list) { |
Heiko Carstens | fc75cdf | 2006-06-25 05:49:10 -0700 | [diff] [blame] | 671 | if (!per_cpu_ptr(wq->cpu_wq, hotcpu)->thread) |
| 672 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | /* Unbind so it can run. */ |
Christoph Lameter | 89ada67 | 2005-10-30 15:01:59 -0800 | [diff] [blame] | 674 | kthread_bind(per_cpu_ptr(wq->cpu_wq, hotcpu)->thread, |
Heiko Carstens | a4c4af7 | 2005-11-07 00:58:38 -0800 | [diff] [blame] | 675 | any_online_cpu(cpu_online_map)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | cleanup_workqueue_thread(wq, hotcpu); |
| 677 | } |
Andrew Morton | 9b41ea7 | 2006-08-13 23:24:26 -0700 | [diff] [blame] | 678 | mutex_unlock(&workqueue_mutex); |
| 679 | break; |
| 680 | |
| 681 | case CPU_DOWN_PREPARE: |
| 682 | mutex_lock(&workqueue_mutex); |
| 683 | break; |
| 684 | |
| 685 | case CPU_DOWN_FAILED: |
| 686 | mutex_unlock(&workqueue_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | break; |
| 688 | |
| 689 | case CPU_DEAD: |
| 690 | list_for_each_entry(wq, &workqueues, list) |
| 691 | cleanup_workqueue_thread(wq, hotcpu); |
| 692 | list_for_each_entry(wq, &workqueues, list) |
| 693 | take_over_work(wq, hotcpu); |
Andrew Morton | 9b41ea7 | 2006-08-13 23:24:26 -0700 | [diff] [blame] | 694 | mutex_unlock(&workqueue_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | break; |
| 696 | } |
| 697 | |
| 698 | return NOTIFY_OK; |
| 699 | } |
| 700 | #endif |
| 701 | |
| 702 | void init_workqueues(void) |
| 703 | { |
Nathan Lynch | f756d5e | 2006-01-08 01:05:12 -0800 | [diff] [blame] | 704 | singlethread_cpu = first_cpu(cpu_possible_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | hotcpu_notifier(workqueue_cpu_callback, 0); |
| 706 | keventd_wq = create_workqueue("events"); |
| 707 | BUG_ON(!keventd_wq); |
| 708 | } |
| 709 | |