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