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