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