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> |
Francois Cami | e1f8e87 | 2008-10-15 22:01:59 -0700 | [diff] [blame] | 12 | * Andrew Morton |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Christoph Lameter | cde5353 | 2008-07-04 09:59:22 -0700 | [diff] [blame] | 16 | * Made to use alloc_percpu by Christoph Lameter. |
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> |
Johannes Berg | 4e6045f | 2007-10-18 23:39:55 -0700 | [diff] [blame] | 35 | #include <linux/lockdep.h> |
Frederic Weisbecker | e1d8aa9 | 2009-01-12 23:15:46 +0100 | [diff] [blame] | 36 | #include <trace/workqueue.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | /* |
Nathan Lynch | f756d5e | 2006-01-08 01:05:12 -0800 | [diff] [blame] | 39 | * The per-CPU workqueue (if single thread, we always use the first |
| 40 | * possible cpu). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | */ |
| 42 | struct cpu_workqueue_struct { |
| 43 | |
| 44 | spinlock_t lock; |
| 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | struct list_head worklist; |
| 47 | wait_queue_head_t more_work; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 48 | struct work_struct *current_work; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | struct workqueue_struct *wq; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 51 | struct task_struct *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | int run_depth; /* Detect run_workqueue() recursion depth */ |
| 54 | } ____cacheline_aligned; |
| 55 | |
| 56 | /* |
| 57 | * The externally visible workqueue abstraction is an array of |
| 58 | * per-CPU workqueues: |
| 59 | */ |
| 60 | struct workqueue_struct { |
Christoph Lameter | 89ada67 | 2005-10-30 15:01:59 -0800 | [diff] [blame] | 61 | struct cpu_workqueue_struct *cpu_wq; |
Oleg Nesterov | cce1a16 | 2007-05-09 02:34:13 -0700 | [diff] [blame] | 62 | struct list_head list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | const char *name; |
Oleg Nesterov | cce1a16 | 2007-05-09 02:34:13 -0700 | [diff] [blame] | 64 | int singlethread; |
Oleg Nesterov | 319c2a9 | 2007-05-09 02:34:06 -0700 | [diff] [blame] | 65 | int freezeable; /* Freeze threads during suspend */ |
Heiko Carstens | 0d557dc | 2008-10-13 23:50:09 +0200 | [diff] [blame] | 66 | int rt; |
Johannes Berg | 4e6045f | 2007-10-18 23:39:55 -0700 | [diff] [blame] | 67 | #ifdef CONFIG_LOCKDEP |
| 68 | struct lockdep_map lockdep_map; |
| 69 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 72 | /* Serializes the accesses to the list of workqueues. */ |
| 73 | static DEFINE_SPINLOCK(workqueue_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | static LIST_HEAD(workqueues); |
| 75 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 76 | static int singlethread_cpu __read_mostly; |
Rusty Russell | e7577c5 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 77 | static const struct cpumask *cpu_singlethread_map __read_mostly; |
Oleg Nesterov | 1444196 | 2007-05-23 13:57:57 -0700 | [diff] [blame] | 78 | /* |
| 79 | * _cpu_down() first removes CPU from cpu_online_map, then CPU_DEAD |
| 80 | * flushes cwq->worklist. This means that flush_workqueue/wait_on_work |
| 81 | * which comes in between can't use for_each_online_cpu(). We could |
| 82 | * use cpu_possible_map, the cpumask below is more a documentation |
| 83 | * than optimization. |
| 84 | */ |
Rusty Russell | e7577c5 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 85 | static cpumask_var_t cpu_populated_map __read_mostly; |
Nathan Lynch | f756d5e | 2006-01-08 01:05:12 -0800 | [diff] [blame] | 86 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | /* If it's single threaded, it isn't in the list of workqueues. */ |
David Howells | 6cc88bc | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 88 | static inline int is_wq_single_threaded(struct workqueue_struct *wq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | { |
Oleg Nesterov | cce1a16 | 2007-05-09 02:34:13 -0700 | [diff] [blame] | 90 | return wq->singlethread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Rusty Russell | e7577c5 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 93 | static const struct cpumask *wq_cpu_map(struct workqueue_struct *wq) |
Oleg Nesterov | b1f4ec1 | 2007-05-09 02:34:12 -0700 | [diff] [blame] | 94 | { |
David Howells | 6cc88bc | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 95 | return is_wq_single_threaded(wq) |
Rusty Russell | e7577c5 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 96 | ? cpu_singlethread_map : cpu_populated_map; |
Oleg Nesterov | b1f4ec1 | 2007-05-09 02:34:12 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Oleg Nesterov | a848e3b | 2007-05-09 02:34:17 -0700 | [diff] [blame] | 99 | static |
| 100 | struct cpu_workqueue_struct *wq_per_cpu(struct workqueue_struct *wq, int cpu) |
| 101 | { |
David Howells | 6cc88bc | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 102 | if (unlikely(is_wq_single_threaded(wq))) |
Oleg Nesterov | a848e3b | 2007-05-09 02:34:17 -0700 | [diff] [blame] | 103 | cpu = singlethread_cpu; |
| 104 | return per_cpu_ptr(wq->cpu_wq, cpu); |
| 105 | } |
| 106 | |
David Howells | 4594bf1 | 2006-12-07 11:33:26 +0000 | [diff] [blame] | 107 | /* |
| 108 | * Set the workqueue on which a work item is to be run |
| 109 | * - Must *only* be called if the pending flag is set |
| 110 | */ |
Oleg Nesterov | ed7c0fe | 2007-05-09 02:34:16 -0700 | [diff] [blame] | 111 | static inline void set_wq_data(struct work_struct *work, |
| 112 | struct cpu_workqueue_struct *cwq) |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 113 | { |
David Howells | 4594bf1 | 2006-12-07 11:33:26 +0000 | [diff] [blame] | 114 | unsigned long new; |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 115 | |
David Howells | 4594bf1 | 2006-12-07 11:33:26 +0000 | [diff] [blame] | 116 | BUG_ON(!work_pending(work)); |
| 117 | |
Oleg Nesterov | ed7c0fe | 2007-05-09 02:34:16 -0700 | [diff] [blame] | 118 | new = (unsigned long) cwq | (1UL << WORK_STRUCT_PENDING); |
Linus Torvalds | a08727b | 2006-12-16 09:53:50 -0800 | [diff] [blame] | 119 | new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work); |
| 120 | atomic_long_set(&work->data, new); |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Oleg Nesterov | ed7c0fe | 2007-05-09 02:34:16 -0700 | [diff] [blame] | 123 | static inline |
| 124 | struct cpu_workqueue_struct *get_wq_data(struct work_struct *work) |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 125 | { |
Linus Torvalds | a08727b | 2006-12-16 09:53:50 -0800 | [diff] [blame] | 126 | return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK); |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Frederic Weisbecker | e1d8aa9 | 2009-01-12 23:15:46 +0100 | [diff] [blame] | 129 | DEFINE_TRACE(workqueue_insertion); |
| 130 | |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 131 | static void insert_work(struct cpu_workqueue_struct *cwq, |
Oleg Nesterov | 1a4d9b0 | 2008-07-25 01:47:47 -0700 | [diff] [blame] | 132 | struct work_struct *work, struct list_head *head) |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 133 | { |
Frederic Weisbecker | e1d8aa9 | 2009-01-12 23:15:46 +0100 | [diff] [blame] | 134 | trace_workqueue_insertion(cwq->thread, work); |
| 135 | |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 136 | set_wq_data(work, cwq); |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 137 | /* |
| 138 | * Ensure that we get the right work->data if we see the |
| 139 | * result of list_add() below, see try_to_grab_pending(). |
| 140 | */ |
| 141 | smp_wmb(); |
Oleg Nesterov | 1a4d9b0 | 2008-07-25 01:47:47 -0700 | [diff] [blame] | 142 | list_add_tail(&work->entry, head); |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 143 | wake_up(&cwq->more_work); |
| 144 | } |
| 145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | static void __queue_work(struct cpu_workqueue_struct *cwq, |
| 147 | struct work_struct *work) |
| 148 | { |
| 149 | unsigned long flags; |
| 150 | |
| 151 | spin_lock_irqsave(&cwq->lock, flags); |
Oleg Nesterov | 1a4d9b0 | 2008-07-25 01:47:47 -0700 | [diff] [blame] | 152 | insert_work(cwq, work, &cwq->worklist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | spin_unlock_irqrestore(&cwq->lock, flags); |
| 154 | } |
| 155 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 156 | /** |
| 157 | * queue_work - queue work on a workqueue |
| 158 | * @wq: workqueue to use |
| 159 | * @work: work to queue |
| 160 | * |
Alan Stern | 057647f | 2006-10-28 10:38:58 -0700 | [diff] [blame] | 161 | * Returns 0 if @work was already on a queue, non-zero otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | * |
Oleg Nesterov | 00dfcaf | 2008-04-29 01:00:27 -0700 | [diff] [blame] | 163 | * We queue the work to the CPU on which it was submitted, but if the CPU dies |
| 164 | * it can be processed by another CPU. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 166 | int queue_work(struct workqueue_struct *wq, struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | { |
Oleg Nesterov | ef1ca23 | 2008-07-25 01:47:53 -0700 | [diff] [blame] | 168 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
Oleg Nesterov | ef1ca23 | 2008-07-25 01:47:53 -0700 | [diff] [blame] | 170 | ret = queue_work_on(get_cpu(), wq, work); |
| 171 | put_cpu(); |
| 172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | return ret; |
| 174 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 175 | EXPORT_SYMBOL_GPL(queue_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
Zhang Rui | c1a220e | 2008-07-23 21:28:39 -0700 | [diff] [blame] | 177 | /** |
| 178 | * queue_work_on - queue work on specific cpu |
| 179 | * @cpu: CPU number to execute work on |
| 180 | * @wq: workqueue to use |
| 181 | * @work: work to queue |
| 182 | * |
| 183 | * Returns 0 if @work was already on a queue, non-zero otherwise. |
| 184 | * |
| 185 | * We queue the work to a specific CPU, the caller must ensure it |
| 186 | * can't go away. |
| 187 | */ |
| 188 | int |
| 189 | queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work) |
| 190 | { |
| 191 | int ret = 0; |
| 192 | |
| 193 | if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) { |
| 194 | BUG_ON(!list_empty(&work->entry)); |
| 195 | __queue_work(wq_per_cpu(wq, cpu), work); |
| 196 | ret = 1; |
| 197 | } |
| 198 | return ret; |
| 199 | } |
| 200 | EXPORT_SYMBOL_GPL(queue_work_on); |
| 201 | |
Li Zefan | 6d141c3 | 2008-02-08 04:21:09 -0800 | [diff] [blame] | 202 | static void delayed_work_timer_fn(unsigned long __data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 204 | struct delayed_work *dwork = (struct delayed_work *)__data; |
Oleg Nesterov | ed7c0fe | 2007-05-09 02:34:16 -0700 | [diff] [blame] | 205 | struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work); |
| 206 | struct workqueue_struct *wq = cwq->wq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
Oleg Nesterov | a848e3b | 2007-05-09 02:34:17 -0700 | [diff] [blame] | 208 | __queue_work(wq_per_cpu(wq, smp_processor_id()), &dwork->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 211 | /** |
| 212 | * queue_delayed_work - queue work on a workqueue after delay |
| 213 | * @wq: workqueue to use |
Randy Dunlap | af9997e | 2006-12-22 01:06:52 -0800 | [diff] [blame] | 214 | * @dwork: delayable work to queue |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 215 | * @delay: number of jiffies to wait before queueing |
| 216 | * |
Alan Stern | 057647f | 2006-10-28 10:38:58 -0700 | [diff] [blame] | 217 | * 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] | 218 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 219 | int queue_delayed_work(struct workqueue_struct *wq, |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 220 | struct delayed_work *dwork, unsigned long delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | { |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 222 | if (delay == 0) |
Oleg Nesterov | 63bc036 | 2007-05-09 02:34:16 -0700 | [diff] [blame] | 223 | return queue_work(wq, &dwork->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
Oleg Nesterov | 63bc036 | 2007-05-09 02:34:16 -0700 | [diff] [blame] | 225 | return queue_delayed_work_on(-1, wq, dwork, delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 227 | EXPORT_SYMBOL_GPL(queue_delayed_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 229 | /** |
| 230 | * queue_delayed_work_on - queue work on specific CPU after delay |
| 231 | * @cpu: CPU number to execute work on |
| 232 | * @wq: workqueue to use |
Randy Dunlap | af9997e | 2006-12-22 01:06:52 -0800 | [diff] [blame] | 233 | * @dwork: work to queue |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 234 | * @delay: number of jiffies to wait before queueing |
| 235 | * |
Alan Stern | 057647f | 2006-10-28 10:38:58 -0700 | [diff] [blame] | 236 | * 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] | 237 | */ |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 238 | int queue_delayed_work_on(int cpu, struct workqueue_struct *wq, |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 239 | struct delayed_work *dwork, unsigned long delay) |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 240 | { |
| 241 | int ret = 0; |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 242 | struct timer_list *timer = &dwork->timer; |
| 243 | struct work_struct *work = &dwork->work; |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 244 | |
Linus Torvalds | a08727b | 2006-12-16 09:53:50 -0800 | [diff] [blame] | 245 | if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) { |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 246 | BUG_ON(timer_pending(timer)); |
| 247 | BUG_ON(!list_empty(&work->entry)); |
| 248 | |
Andrew Liu | 8a3e77c | 2008-05-01 04:35:14 -0700 | [diff] [blame] | 249 | timer_stats_timer_set_start_info(&dwork->timer); |
| 250 | |
Oleg Nesterov | ed7c0fe | 2007-05-09 02:34:16 -0700 | [diff] [blame] | 251 | /* This stores cwq for the moment, for the timer_fn */ |
Oleg Nesterov | a848e3b | 2007-05-09 02:34:17 -0700 | [diff] [blame] | 252 | set_wq_data(work, wq_per_cpu(wq, raw_smp_processor_id())); |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 253 | timer->expires = jiffies + delay; |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 254 | timer->data = (unsigned long)dwork; |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 255 | timer->function = delayed_work_timer_fn; |
Oleg Nesterov | 63bc036 | 2007-05-09 02:34:16 -0700 | [diff] [blame] | 256 | |
| 257 | if (unlikely(cpu >= 0)) |
| 258 | add_timer_on(timer, cpu); |
| 259 | else |
| 260 | add_timer(timer); |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 261 | ret = 1; |
| 262 | } |
| 263 | return ret; |
| 264 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 265 | EXPORT_SYMBOL_GPL(queue_delayed_work_on); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
Frederic Weisbecker | e1d8aa9 | 2009-01-12 23:15:46 +0100 | [diff] [blame] | 267 | DEFINE_TRACE(workqueue_execution); |
| 268 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 269 | static void run_workqueue(struct cpu_workqueue_struct *cwq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | { |
Oleg Nesterov | f293ea9 | 2007-05-09 02:34:10 -0700 | [diff] [blame] | 271 | spin_lock_irq(&cwq->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | cwq->run_depth++; |
| 273 | if (cwq->run_depth > 3) { |
| 274 | /* morton gets to eat his hat */ |
| 275 | printk("%s: recursion depth exceeded: %d\n", |
Harvey Harrison | af1f16d | 2008-04-30 00:55:08 -0700 | [diff] [blame] | 276 | __func__, cwq->run_depth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | dump_stack(); |
| 278 | } |
| 279 | while (!list_empty(&cwq->worklist)) { |
| 280 | struct work_struct *work = list_entry(cwq->worklist.next, |
| 281 | struct work_struct, entry); |
David Howells | 6bb49e5 | 2006-11-22 14:54:45 +0000 | [diff] [blame] | 282 | work_func_t f = work->func; |
Johannes Berg | 4e6045f | 2007-10-18 23:39:55 -0700 | [diff] [blame] | 283 | #ifdef CONFIG_LOCKDEP |
| 284 | /* |
| 285 | * It is permissible to free the struct work_struct |
| 286 | * from inside the function that is called from it, |
| 287 | * this we need to take into account for lockdep too. |
| 288 | * To avoid bogus "held lock freed" warnings as well |
| 289 | * as problems when looking into work->lockdep_map, |
| 290 | * make a copy and use that here. |
| 291 | */ |
| 292 | struct lockdep_map lockdep_map = work->lockdep_map; |
| 293 | #endif |
Frederic Weisbecker | e1d8aa9 | 2009-01-12 23:15:46 +0100 | [diff] [blame] | 294 | trace_workqueue_execution(cwq->thread, work); |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 295 | cwq->current_work = work; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | list_del_init(cwq->worklist.next); |
Oleg Nesterov | f293ea9 | 2007-05-09 02:34:10 -0700 | [diff] [blame] | 297 | spin_unlock_irq(&cwq->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 299 | BUG_ON(get_wq_data(work) != cwq); |
Oleg Nesterov | 23b2e59 | 2007-05-09 02:34:19 -0700 | [diff] [blame] | 300 | work_clear_pending(work); |
Ingo Molnar | 3295f0e | 2008-08-11 10:30:30 +0200 | [diff] [blame] | 301 | lock_map_acquire(&cwq->wq->lockdep_map); |
| 302 | lock_map_acquire(&lockdep_map); |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 303 | f(work); |
Ingo Molnar | 3295f0e | 2008-08-11 10:30:30 +0200 | [diff] [blame] | 304 | lock_map_release(&lockdep_map); |
| 305 | lock_map_release(&cwq->wq->lockdep_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
Peter Zijlstra | d5abe66 | 2006-12-06 20:37:26 -0800 | [diff] [blame] | 307 | if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { |
| 308 | printk(KERN_ERR "BUG: workqueue leaked lock or atomic: " |
| 309 | "%s/0x%08x/%d\n", |
| 310 | current->comm, preempt_count(), |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 311 | task_pid_nr(current)); |
Peter Zijlstra | d5abe66 | 2006-12-06 20:37:26 -0800 | [diff] [blame] | 312 | printk(KERN_ERR " last function: "); |
| 313 | print_symbol("%s\n", (unsigned long)f); |
| 314 | debug_show_held_locks(current); |
| 315 | dump_stack(); |
| 316 | } |
| 317 | |
Oleg Nesterov | f293ea9 | 2007-05-09 02:34:10 -0700 | [diff] [blame] | 318 | spin_lock_irq(&cwq->lock); |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 319 | cwq->current_work = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | } |
| 321 | cwq->run_depth--; |
Oleg Nesterov | f293ea9 | 2007-05-09 02:34:10 -0700 | [diff] [blame] | 322 | spin_unlock_irq(&cwq->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | static int worker_thread(void *__cwq) |
| 326 | { |
| 327 | struct cpu_workqueue_struct *cwq = __cwq; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 328 | DEFINE_WAIT(wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 330 | if (cwq->wq->freezeable) |
| 331 | set_freezable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
| 333 | set_user_nice(current, -5); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 335 | for (;;) { |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 336 | prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE); |
Oleg Nesterov | 1444196 | 2007-05-23 13:57:57 -0700 | [diff] [blame] | 337 | if (!freezing(current) && |
| 338 | !kthread_should_stop() && |
| 339 | list_empty(&cwq->worklist)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | schedule(); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 341 | finish_wait(&cwq->more_work, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
Oleg Nesterov | 85f4186 | 2007-05-09 02:34:20 -0700 | [diff] [blame] | 343 | try_to_freeze(); |
| 344 | |
Oleg Nesterov | 1444196 | 2007-05-23 13:57:57 -0700 | [diff] [blame] | 345 | if (kthread_should_stop()) |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 346 | break; |
| 347 | |
| 348 | run_workqueue(cwq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 350 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | return 0; |
| 352 | } |
| 353 | |
Oleg Nesterov | fc2e4d7 | 2007-05-09 02:33:51 -0700 | [diff] [blame] | 354 | struct wq_barrier { |
| 355 | struct work_struct work; |
| 356 | struct completion done; |
| 357 | }; |
| 358 | |
| 359 | static void wq_barrier_func(struct work_struct *work) |
| 360 | { |
| 361 | struct wq_barrier *barr = container_of(work, struct wq_barrier, work); |
| 362 | complete(&barr->done); |
| 363 | } |
| 364 | |
Oleg Nesterov | 83c2252 | 2007-05-09 02:33:54 -0700 | [diff] [blame] | 365 | static void insert_wq_barrier(struct cpu_workqueue_struct *cwq, |
Oleg Nesterov | 1a4d9b0 | 2008-07-25 01:47:47 -0700 | [diff] [blame] | 366 | struct wq_barrier *barr, struct list_head *head) |
Oleg Nesterov | fc2e4d7 | 2007-05-09 02:33:51 -0700 | [diff] [blame] | 367 | { |
| 368 | INIT_WORK(&barr->work, wq_barrier_func); |
| 369 | __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work)); |
| 370 | |
| 371 | init_completion(&barr->done); |
Oleg Nesterov | 83c2252 | 2007-05-09 02:33:54 -0700 | [diff] [blame] | 372 | |
Oleg Nesterov | 1a4d9b0 | 2008-07-25 01:47:47 -0700 | [diff] [blame] | 373 | insert_work(cwq, &barr->work, head); |
Oleg Nesterov | fc2e4d7 | 2007-05-09 02:33:51 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Oleg Nesterov | 1444196 | 2007-05-23 13:57:57 -0700 | [diff] [blame] | 376 | static int flush_cpu_workqueue(struct cpu_workqueue_struct *cwq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | { |
Oleg Nesterov | 1444196 | 2007-05-23 13:57:57 -0700 | [diff] [blame] | 378 | int active; |
| 379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | if (cwq->thread == current) { |
| 381 | /* |
| 382 | * Probably keventd trying to flush its own queue. So simply run |
| 383 | * it by hand rather than deadlocking. |
| 384 | */ |
| 385 | run_workqueue(cwq); |
Oleg Nesterov | 1444196 | 2007-05-23 13:57:57 -0700 | [diff] [blame] | 386 | active = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } else { |
Oleg Nesterov | fc2e4d7 | 2007-05-09 02:33:51 -0700 | [diff] [blame] | 388 | struct wq_barrier barr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
Oleg Nesterov | 1444196 | 2007-05-23 13:57:57 -0700 | [diff] [blame] | 390 | active = 0; |
Oleg Nesterov | 83c2252 | 2007-05-09 02:33:54 -0700 | [diff] [blame] | 391 | spin_lock_irq(&cwq->lock); |
| 392 | if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) { |
Oleg Nesterov | 1a4d9b0 | 2008-07-25 01:47:47 -0700 | [diff] [blame] | 393 | insert_wq_barrier(cwq, &barr, &cwq->worklist); |
Oleg Nesterov | 83c2252 | 2007-05-09 02:33:54 -0700 | [diff] [blame] | 394 | active = 1; |
| 395 | } |
| 396 | spin_unlock_irq(&cwq->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | |
Oleg Nesterov | d721304 | 2007-05-09 02:34:07 -0700 | [diff] [blame] | 398 | if (active) |
Oleg Nesterov | 83c2252 | 2007-05-09 02:33:54 -0700 | [diff] [blame] | 399 | wait_for_completion(&barr.done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | } |
Oleg Nesterov | 1444196 | 2007-05-23 13:57:57 -0700 | [diff] [blame] | 401 | |
| 402 | return active; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 405 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | * flush_workqueue - ensure that any scheduled work has run to completion. |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 407 | * @wq: workqueue to flush |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | * |
| 409 | * Forces execution of the workqueue and blocks until its completion. |
| 410 | * This is typically used in driver shutdown handlers. |
| 411 | * |
Oleg Nesterov | fc2e4d7 | 2007-05-09 02:33:51 -0700 | [diff] [blame] | 412 | * We sleep until all works which were queued on entry have been handled, |
| 413 | * but we are not livelocked by new incoming ones. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | * |
| 415 | * This function used to run the workqueues itself. Now we just wait for the |
| 416 | * helper threads to do it. |
| 417 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 418 | void flush_workqueue(struct workqueue_struct *wq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | { |
Rusty Russell | e7577c5 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 420 | const struct cpumask *cpu_map = wq_cpu_map(wq); |
Oleg Nesterov | cce1a16 | 2007-05-09 02:34:13 -0700 | [diff] [blame] | 421 | int cpu; |
Oleg Nesterov | b1f4ec1 | 2007-05-09 02:34:12 -0700 | [diff] [blame] | 422 | |
Oleg Nesterov | f293ea9 | 2007-05-09 02:34:10 -0700 | [diff] [blame] | 423 | might_sleep(); |
Ingo Molnar | 3295f0e | 2008-08-11 10:30:30 +0200 | [diff] [blame] | 424 | lock_map_acquire(&wq->lockdep_map); |
| 425 | lock_map_release(&wq->lockdep_map); |
Mike Travis | 363ab6f | 2008-05-12 21:21:13 +0200 | [diff] [blame] | 426 | for_each_cpu_mask_nr(cpu, *cpu_map) |
Oleg Nesterov | b1f4ec1 | 2007-05-09 02:34:12 -0700 | [diff] [blame] | 427 | flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 429 | EXPORT_SYMBOL_GPL(flush_workqueue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
Oleg Nesterov | db70089 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 431 | /** |
| 432 | * flush_work - block until a work_struct's callback has terminated |
| 433 | * @work: the work which is to be flushed |
| 434 | * |
Oleg Nesterov | a67da70 | 2008-07-25 01:47:52 -0700 | [diff] [blame] | 435 | * Returns false if @work has already terminated. |
| 436 | * |
Oleg Nesterov | db70089 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 437 | * It is expected that, prior to calling flush_work(), the caller has |
| 438 | * arranged for the work to not be requeued, otherwise it doesn't make |
| 439 | * sense to use this function. |
| 440 | */ |
| 441 | int flush_work(struct work_struct *work) |
| 442 | { |
| 443 | struct cpu_workqueue_struct *cwq; |
| 444 | struct list_head *prev; |
| 445 | struct wq_barrier barr; |
| 446 | |
| 447 | might_sleep(); |
| 448 | cwq = get_wq_data(work); |
| 449 | if (!cwq) |
| 450 | return 0; |
| 451 | |
Ingo Molnar | 3295f0e | 2008-08-11 10:30:30 +0200 | [diff] [blame] | 452 | lock_map_acquire(&cwq->wq->lockdep_map); |
| 453 | lock_map_release(&cwq->wq->lockdep_map); |
Oleg Nesterov | a67da70 | 2008-07-25 01:47:52 -0700 | [diff] [blame] | 454 | |
Oleg Nesterov | db70089 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 455 | prev = NULL; |
| 456 | spin_lock_irq(&cwq->lock); |
| 457 | if (!list_empty(&work->entry)) { |
| 458 | /* |
| 459 | * See the comment near try_to_grab_pending()->smp_rmb(). |
| 460 | * If it was re-queued under us we are not going to wait. |
| 461 | */ |
| 462 | smp_rmb(); |
| 463 | if (unlikely(cwq != get_wq_data(work))) |
| 464 | goto out; |
| 465 | prev = &work->entry; |
| 466 | } else { |
| 467 | if (cwq->current_work != work) |
| 468 | goto out; |
| 469 | prev = &cwq->worklist; |
| 470 | } |
| 471 | insert_wq_barrier(cwq, &barr, prev->next); |
| 472 | out: |
| 473 | spin_unlock_irq(&cwq->lock); |
| 474 | if (!prev) |
| 475 | return 0; |
| 476 | |
| 477 | wait_for_completion(&barr.done); |
| 478 | return 1; |
| 479 | } |
| 480 | EXPORT_SYMBOL_GPL(flush_work); |
| 481 | |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 482 | /* |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 483 | * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit, |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 484 | * so this work can't be re-armed in any way. |
| 485 | */ |
| 486 | static int try_to_grab_pending(struct work_struct *work) |
| 487 | { |
| 488 | struct cpu_workqueue_struct *cwq; |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 489 | int ret = -1; |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 490 | |
| 491 | if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 492 | return 0; |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 493 | |
| 494 | /* |
| 495 | * The queueing is in progress, or it is already queued. Try to |
| 496 | * steal it from ->worklist without clearing WORK_STRUCT_PENDING. |
| 497 | */ |
| 498 | |
| 499 | cwq = get_wq_data(work); |
| 500 | if (!cwq) |
| 501 | return ret; |
| 502 | |
| 503 | spin_lock_irq(&cwq->lock); |
| 504 | if (!list_empty(&work->entry)) { |
| 505 | /* |
| 506 | * This work is queued, but perhaps we locked the wrong cwq. |
| 507 | * In that case we must see the new value after rmb(), see |
| 508 | * insert_work()->wmb(). |
| 509 | */ |
| 510 | smp_rmb(); |
| 511 | if (cwq == get_wq_data(work)) { |
| 512 | list_del_init(&work->entry); |
| 513 | ret = 1; |
| 514 | } |
| 515 | } |
| 516 | spin_unlock_irq(&cwq->lock); |
| 517 | |
| 518 | return ret; |
| 519 | } |
| 520 | |
| 521 | static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq, |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 522 | struct work_struct *work) |
| 523 | { |
| 524 | struct wq_barrier barr; |
| 525 | int running = 0; |
| 526 | |
| 527 | spin_lock_irq(&cwq->lock); |
| 528 | if (unlikely(cwq->current_work == work)) { |
Oleg Nesterov | 1a4d9b0 | 2008-07-25 01:47:47 -0700 | [diff] [blame] | 529 | insert_wq_barrier(cwq, &barr, cwq->worklist.next); |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 530 | running = 1; |
| 531 | } |
| 532 | spin_unlock_irq(&cwq->lock); |
| 533 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 534 | if (unlikely(running)) |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 535 | wait_for_completion(&barr.done); |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 538 | static void wait_on_work(struct work_struct *work) |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 539 | { |
| 540 | struct cpu_workqueue_struct *cwq; |
Oleg Nesterov | 28e53bd | 2007-05-09 02:34:22 -0700 | [diff] [blame] | 541 | struct workqueue_struct *wq; |
Rusty Russell | e7577c5 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 542 | const struct cpumask *cpu_map; |
Oleg Nesterov | b1f4ec1 | 2007-05-09 02:34:12 -0700 | [diff] [blame] | 543 | int cpu; |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 544 | |
Oleg Nesterov | f293ea9 | 2007-05-09 02:34:10 -0700 | [diff] [blame] | 545 | might_sleep(); |
| 546 | |
Ingo Molnar | 3295f0e | 2008-08-11 10:30:30 +0200 | [diff] [blame] | 547 | lock_map_acquire(&work->lockdep_map); |
| 548 | lock_map_release(&work->lockdep_map); |
Johannes Berg | 4e6045f | 2007-10-18 23:39:55 -0700 | [diff] [blame] | 549 | |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 550 | cwq = get_wq_data(work); |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 551 | if (!cwq) |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 552 | return; |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 553 | |
Oleg Nesterov | 28e53bd | 2007-05-09 02:34:22 -0700 | [diff] [blame] | 554 | wq = cwq->wq; |
| 555 | cpu_map = wq_cpu_map(wq); |
| 556 | |
Mike Travis | 363ab6f | 2008-05-12 21:21:13 +0200 | [diff] [blame] | 557 | for_each_cpu_mask_nr(cpu, *cpu_map) |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 558 | wait_on_cpu_work(per_cpu_ptr(wq->cpu_wq, cpu), work); |
| 559 | } |
| 560 | |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 561 | static int __cancel_work_timer(struct work_struct *work, |
| 562 | struct timer_list* timer) |
| 563 | { |
| 564 | int ret; |
| 565 | |
| 566 | do { |
| 567 | ret = (timer && likely(del_timer(timer))); |
| 568 | if (!ret) |
| 569 | ret = try_to_grab_pending(work); |
| 570 | wait_on_work(work); |
| 571 | } while (unlikely(ret < 0)); |
| 572 | |
| 573 | work_clear_pending(work); |
| 574 | return ret; |
| 575 | } |
| 576 | |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 577 | /** |
| 578 | * cancel_work_sync - block until a work_struct's callback has terminated |
| 579 | * @work: the work which is to be flushed |
| 580 | * |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 581 | * Returns true if @work was pending. |
| 582 | * |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 583 | * cancel_work_sync() will cancel the work if it is queued. If the work's |
| 584 | * callback appears to be running, cancel_work_sync() will block until it |
| 585 | * has completed. |
| 586 | * |
| 587 | * It is possible to use this function if the work re-queues itself. It can |
| 588 | * cancel the work even if it migrates to another workqueue, however in that |
| 589 | * case it only guarantees that work->func() has completed on the last queued |
| 590 | * workqueue. |
| 591 | * |
| 592 | * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not |
| 593 | * pending, otherwise it goes into a busy-wait loop until the timer expires. |
| 594 | * |
| 595 | * The caller must ensure that workqueue_struct on which this work was last |
| 596 | * queued can't be destroyed before this function returns. |
| 597 | */ |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 598 | int cancel_work_sync(struct work_struct *work) |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 599 | { |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 600 | return __cancel_work_timer(work, NULL); |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 601 | } |
Oleg Nesterov | 28e53bd | 2007-05-09 02:34:22 -0700 | [diff] [blame] | 602 | EXPORT_SYMBOL_GPL(cancel_work_sync); |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 603 | |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 604 | /** |
Oleg Nesterov | f5a421a | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 605 | * cancel_delayed_work_sync - reliably kill off a delayed work. |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 606 | * @dwork: the delayed work struct |
| 607 | * |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 608 | * Returns true if @dwork was pending. |
| 609 | * |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 610 | * It is possible to use this function if @dwork rearms itself via queue_work() |
| 611 | * or queue_delayed_work(). See also the comment for cancel_work_sync(). |
| 612 | */ |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 613 | int cancel_delayed_work_sync(struct delayed_work *dwork) |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 614 | { |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 615 | return __cancel_work_timer(&dwork->work, &dwork->timer); |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 616 | } |
Oleg Nesterov | f5a421a | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 617 | EXPORT_SYMBOL(cancel_delayed_work_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 619 | static struct workqueue_struct *keventd_wq __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 621 | /** |
| 622 | * schedule_work - put work task in global workqueue |
| 623 | * @work: job to be done |
| 624 | * |
| 625 | * This puts a job in the kernel-global workqueue. |
| 626 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 627 | int schedule_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | { |
| 629 | return queue_work(keventd_wq, work); |
| 630 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 631 | EXPORT_SYMBOL(schedule_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
Zhang Rui | c1a220e | 2008-07-23 21:28:39 -0700 | [diff] [blame] | 633 | /* |
| 634 | * schedule_work_on - put work task on a specific cpu |
| 635 | * @cpu: cpu to put the work task on |
| 636 | * @work: job to be done |
| 637 | * |
| 638 | * This puts a job on a specific cpu |
| 639 | */ |
| 640 | int schedule_work_on(int cpu, struct work_struct *work) |
| 641 | { |
| 642 | return queue_work_on(cpu, keventd_wq, work); |
| 643 | } |
| 644 | EXPORT_SYMBOL(schedule_work_on); |
| 645 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 646 | /** |
| 647 | * schedule_delayed_work - put work task in global workqueue after delay |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 648 | * @dwork: job to be done |
| 649 | * @delay: number of jiffies to wait or 0 for immediate execution |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 650 | * |
| 651 | * After waiting for a given time this puts a job in the kernel-global |
| 652 | * workqueue. |
| 653 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 654 | int schedule_delayed_work(struct delayed_work *dwork, |
Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 655 | unsigned long delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | { |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 657 | return queue_delayed_work(keventd_wq, dwork, delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 659 | EXPORT_SYMBOL(schedule_delayed_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 661 | /** |
| 662 | * schedule_delayed_work_on - queue work in global workqueue on CPU after delay |
| 663 | * @cpu: cpu to use |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 664 | * @dwork: job to be done |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 665 | * @delay: number of jiffies to wait |
| 666 | * |
| 667 | * After waiting for a given time this puts a job in the kernel-global |
| 668 | * workqueue on the specified CPU. |
| 669 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | int schedule_delayed_work_on(int cpu, |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 671 | struct delayed_work *dwork, unsigned long delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | { |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 673 | return queue_delayed_work_on(cpu, keventd_wq, dwork, delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 675 | EXPORT_SYMBOL(schedule_delayed_work_on); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 677 | /** |
| 678 | * schedule_on_each_cpu - call a function on each online CPU from keventd |
| 679 | * @func: the function to call |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 680 | * |
| 681 | * Returns zero on success. |
| 682 | * Returns -ve errno on failure. |
| 683 | * |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 684 | * schedule_on_each_cpu() is very slow. |
| 685 | */ |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 686 | int schedule_on_each_cpu(work_func_t func) |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 687 | { |
| 688 | int cpu; |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 689 | struct work_struct *works; |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 690 | |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 691 | works = alloc_percpu(struct work_struct); |
| 692 | if (!works) |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 693 | return -ENOMEM; |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 694 | |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 695 | get_online_cpus(); |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 696 | for_each_online_cpu(cpu) { |
Ingo Molnar | 9bfb183 | 2006-12-18 20:05:09 +0100 | [diff] [blame] | 697 | struct work_struct *work = per_cpu_ptr(works, cpu); |
| 698 | |
| 699 | INIT_WORK(work, func); |
Oleg Nesterov | 8de6d30 | 2008-07-25 01:47:53 -0700 | [diff] [blame] | 700 | schedule_work_on(cpu, work); |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 701 | } |
Oleg Nesterov | 8616a89 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 702 | for_each_online_cpu(cpu) |
| 703 | flush_work(per_cpu_ptr(works, cpu)); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 704 | put_online_cpus(); |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 705 | free_percpu(works); |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 706 | return 0; |
| 707 | } |
| 708 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | void flush_scheduled_work(void) |
| 710 | { |
| 711 | flush_workqueue(keventd_wq); |
| 712 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 713 | EXPORT_SYMBOL(flush_scheduled_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | |
| 715 | /** |
James Bottomley | 1fa44ec | 2006-02-23 12:43:43 -0600 | [diff] [blame] | 716 | * execute_in_process_context - reliably execute the routine with user context |
| 717 | * @fn: the function to execute |
James Bottomley | 1fa44ec | 2006-02-23 12:43:43 -0600 | [diff] [blame] | 718 | * @ew: guaranteed storage for the execute work structure (must |
| 719 | * be available when the work executes) |
| 720 | * |
| 721 | * Executes the function immediately if process context is available, |
| 722 | * otherwise schedules the function for delayed execution. |
| 723 | * |
| 724 | * Returns: 0 - function was executed |
| 725 | * 1 - function was scheduled for execution |
| 726 | */ |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 727 | 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] | 728 | { |
| 729 | if (!in_interrupt()) { |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 730 | fn(&ew->work); |
James Bottomley | 1fa44ec | 2006-02-23 12:43:43 -0600 | [diff] [blame] | 731 | return 0; |
| 732 | } |
| 733 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 734 | INIT_WORK(&ew->work, fn); |
James Bottomley | 1fa44ec | 2006-02-23 12:43:43 -0600 | [diff] [blame] | 735 | schedule_work(&ew->work); |
| 736 | |
| 737 | return 1; |
| 738 | } |
| 739 | EXPORT_SYMBOL_GPL(execute_in_process_context); |
| 740 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | int keventd_up(void) |
| 742 | { |
| 743 | return keventd_wq != NULL; |
| 744 | } |
| 745 | |
| 746 | int current_is_keventd(void) |
| 747 | { |
| 748 | struct cpu_workqueue_struct *cwq; |
Hugh Dickins | d243769 | 2007-08-27 16:06:19 +0100 | [diff] [blame] | 749 | int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | int ret = 0; |
| 751 | |
| 752 | BUG_ON(!keventd_wq); |
| 753 | |
Christoph Lameter | 89ada67 | 2005-10-30 15:01:59 -0800 | [diff] [blame] | 754 | cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | if (current == cwq->thread) |
| 756 | ret = 1; |
| 757 | |
| 758 | return ret; |
| 759 | |
| 760 | } |
| 761 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 762 | static struct cpu_workqueue_struct * |
| 763 | init_cpu_workqueue(struct workqueue_struct *wq, int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | { |
Christoph Lameter | 89ada67 | 2005-10-30 15:01:59 -0800 | [diff] [blame] | 765 | struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 767 | cwq->wq = wq; |
| 768 | spin_lock_init(&cwq->lock); |
| 769 | INIT_LIST_HEAD(&cwq->worklist); |
| 770 | init_waitqueue_head(&cwq->more_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 772 | return cwq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Frederic Weisbecker | e1d8aa9 | 2009-01-12 23:15:46 +0100 | [diff] [blame] | 775 | DEFINE_TRACE(workqueue_creation); |
| 776 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 777 | static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | { |
Heiko Carstens | 0d557dc | 2008-10-13 23:50:09 +0200 | [diff] [blame] | 779 | struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 }; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 780 | struct workqueue_struct *wq = cwq->wq; |
David Howells | 6cc88bc | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 781 | const char *fmt = is_wq_single_threaded(wq) ? "%s" : "%s/%d"; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 782 | struct task_struct *p; |
| 783 | |
| 784 | p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu); |
| 785 | /* |
| 786 | * Nobody can add the work_struct to this cwq, |
| 787 | * if (caller is __create_workqueue) |
| 788 | * nobody should see this wq |
| 789 | * else // caller is CPU_UP_PREPARE |
| 790 | * cpu is not on cpu_online_map |
| 791 | * so we can abort safely. |
| 792 | */ |
| 793 | if (IS_ERR(p)) |
| 794 | return PTR_ERR(p); |
Heiko Carstens | 0d557dc | 2008-10-13 23:50:09 +0200 | [diff] [blame] | 795 | if (cwq->wq->rt) |
| 796 | sched_setscheduler_nocheck(p, SCHED_FIFO, ¶m); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 797 | cwq->thread = p; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 798 | |
Frederic Weisbecker | e1d8aa9 | 2009-01-12 23:15:46 +0100 | [diff] [blame] | 799 | trace_workqueue_creation(cwq->thread, cpu); |
| 800 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 801 | return 0; |
| 802 | } |
| 803 | |
Oleg Nesterov | 06ba38a | 2007-05-09 02:34:15 -0700 | [diff] [blame] | 804 | static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu) |
| 805 | { |
| 806 | struct task_struct *p = cwq->thread; |
| 807 | |
| 808 | if (p != NULL) { |
| 809 | if (cpu >= 0) |
| 810 | kthread_bind(p, cpu); |
| 811 | wake_up_process(p); |
| 812 | } |
| 813 | } |
| 814 | |
Johannes Berg | 4e6045f | 2007-10-18 23:39:55 -0700 | [diff] [blame] | 815 | struct workqueue_struct *__create_workqueue_key(const char *name, |
| 816 | int singlethread, |
| 817 | int freezeable, |
Heiko Carstens | 0d557dc | 2008-10-13 23:50:09 +0200 | [diff] [blame] | 818 | int rt, |
Johannes Berg | eb13ba8 | 2008-01-16 09:51:58 +0100 | [diff] [blame] | 819 | struct lock_class_key *key, |
| 820 | const char *lock_name) |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 821 | { |
| 822 | struct workqueue_struct *wq; |
| 823 | struct cpu_workqueue_struct *cwq; |
| 824 | int err = 0, cpu; |
| 825 | |
| 826 | wq = kzalloc(sizeof(*wq), GFP_KERNEL); |
| 827 | if (!wq) |
| 828 | return NULL; |
| 829 | |
| 830 | wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct); |
| 831 | if (!wq->cpu_wq) { |
| 832 | kfree(wq); |
| 833 | return NULL; |
| 834 | } |
| 835 | |
| 836 | wq->name = name; |
Johannes Berg | eb13ba8 | 2008-01-16 09:51:58 +0100 | [diff] [blame] | 837 | lockdep_init_map(&wq->lockdep_map, lock_name, key, 0); |
Oleg Nesterov | cce1a16 | 2007-05-09 02:34:13 -0700 | [diff] [blame] | 838 | wq->singlethread = singlethread; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 839 | wq->freezeable = freezeable; |
Heiko Carstens | 0d557dc | 2008-10-13 23:50:09 +0200 | [diff] [blame] | 840 | wq->rt = rt; |
Oleg Nesterov | cce1a16 | 2007-05-09 02:34:13 -0700 | [diff] [blame] | 841 | INIT_LIST_HEAD(&wq->list); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 842 | |
| 843 | if (singlethread) { |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 844 | cwq = init_cpu_workqueue(wq, singlethread_cpu); |
| 845 | err = create_workqueue_thread(cwq, singlethread_cpu); |
Oleg Nesterov | 06ba38a | 2007-05-09 02:34:15 -0700 | [diff] [blame] | 846 | start_workqueue_thread(cwq, -1); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 847 | } else { |
Oleg Nesterov | 3da1c84 | 2008-07-25 01:47:50 -0700 | [diff] [blame] | 848 | cpu_maps_update_begin(); |
Oleg Nesterov | 6af8bf3 | 2008-07-29 22:33:49 -0700 | [diff] [blame] | 849 | /* |
| 850 | * We must place this wq on list even if the code below fails. |
| 851 | * cpu_down(cpu) can remove cpu from cpu_populated_map before |
| 852 | * destroy_workqueue() takes the lock, in that case we leak |
| 853 | * cwq[cpu]->thread. |
| 854 | */ |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 855 | spin_lock(&workqueue_lock); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 856 | list_add(&wq->list, &workqueues); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 857 | spin_unlock(&workqueue_lock); |
Oleg Nesterov | 6af8bf3 | 2008-07-29 22:33:49 -0700 | [diff] [blame] | 858 | /* |
| 859 | * We must initialize cwqs for each possible cpu even if we |
| 860 | * are going to call destroy_workqueue() finally. Otherwise |
| 861 | * cpu_up() can hit the uninitialized cwq once we drop the |
| 862 | * lock. |
| 863 | */ |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 864 | for_each_possible_cpu(cpu) { |
| 865 | cwq = init_cpu_workqueue(wq, cpu); |
| 866 | if (err || !cpu_online(cpu)) |
| 867 | continue; |
| 868 | err = create_workqueue_thread(cwq, cpu); |
Oleg Nesterov | 06ba38a | 2007-05-09 02:34:15 -0700 | [diff] [blame] | 869 | start_workqueue_thread(cwq, cpu); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 870 | } |
Oleg Nesterov | 3da1c84 | 2008-07-25 01:47:50 -0700 | [diff] [blame] | 871 | cpu_maps_update_done(); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | if (err) { |
| 875 | destroy_workqueue(wq); |
| 876 | wq = NULL; |
| 877 | } |
| 878 | return wq; |
| 879 | } |
Johannes Berg | 4e6045f | 2007-10-18 23:39:55 -0700 | [diff] [blame] | 880 | EXPORT_SYMBOL_GPL(__create_workqueue_key); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 881 | |
Frederic Weisbecker | e1d8aa9 | 2009-01-12 23:15:46 +0100 | [diff] [blame] | 882 | DEFINE_TRACE(workqueue_destruction); |
| 883 | |
Oleg Nesterov | 1e35eaa | 2008-04-29 01:00:28 -0700 | [diff] [blame] | 884 | static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq) |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 885 | { |
Oleg Nesterov | 1444196 | 2007-05-23 13:57:57 -0700 | [diff] [blame] | 886 | /* |
Oleg Nesterov | 3da1c84 | 2008-07-25 01:47:50 -0700 | [diff] [blame] | 887 | * Our caller is either destroy_workqueue() or CPU_POST_DEAD, |
| 888 | * cpu_add_remove_lock protects cwq->thread. |
Oleg Nesterov | 1444196 | 2007-05-23 13:57:57 -0700 | [diff] [blame] | 889 | */ |
| 890 | if (cwq->thread == NULL) |
| 891 | return; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 892 | |
Ingo Molnar | 3295f0e | 2008-08-11 10:30:30 +0200 | [diff] [blame] | 893 | lock_map_acquire(&cwq->wq->lockdep_map); |
| 894 | lock_map_release(&cwq->wq->lockdep_map); |
Johannes Berg | 4e6045f | 2007-10-18 23:39:55 -0700 | [diff] [blame] | 895 | |
Oleg Nesterov | 13c2216 | 2007-07-17 04:03:55 -0700 | [diff] [blame] | 896 | flush_cpu_workqueue(cwq); |
Oleg Nesterov | 1444196 | 2007-05-23 13:57:57 -0700 | [diff] [blame] | 897 | /* |
Oleg Nesterov | 3da1c84 | 2008-07-25 01:47:50 -0700 | [diff] [blame] | 898 | * If the caller is CPU_POST_DEAD and cwq->worklist was not empty, |
Oleg Nesterov | 13c2216 | 2007-07-17 04:03:55 -0700 | [diff] [blame] | 899 | * a concurrent flush_workqueue() can insert a barrier after us. |
| 900 | * However, in that case run_workqueue() won't return and check |
| 901 | * kthread_should_stop() until it flushes all work_struct's. |
Oleg Nesterov | 1444196 | 2007-05-23 13:57:57 -0700 | [diff] [blame] | 902 | * When ->worklist becomes empty it is safe to exit because no |
| 903 | * more work_structs can be queued on this cwq: flush_workqueue |
| 904 | * checks list_empty(), and a "normal" queue_work() can't use |
| 905 | * a dead CPU. |
| 906 | */ |
Frederic Weisbecker | e1d8aa9 | 2009-01-12 23:15:46 +0100 | [diff] [blame] | 907 | trace_workqueue_destruction(cwq->thread); |
Oleg Nesterov | 1444196 | 2007-05-23 13:57:57 -0700 | [diff] [blame] | 908 | kthread_stop(cwq->thread); |
| 909 | cwq->thread = NULL; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | /** |
| 913 | * destroy_workqueue - safely terminate a workqueue |
| 914 | * @wq: target workqueue |
| 915 | * |
| 916 | * Safely destroy a workqueue. All work currently pending will be done first. |
| 917 | */ |
| 918 | void destroy_workqueue(struct workqueue_struct *wq) |
| 919 | { |
Rusty Russell | e7577c5 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 920 | const struct cpumask *cpu_map = wq_cpu_map(wq); |
Oleg Nesterov | b1f4ec1 | 2007-05-09 02:34:12 -0700 | [diff] [blame] | 921 | int cpu; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 922 | |
Oleg Nesterov | 3da1c84 | 2008-07-25 01:47:50 -0700 | [diff] [blame] | 923 | cpu_maps_update_begin(); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 924 | spin_lock(&workqueue_lock); |
Oleg Nesterov | b1f4ec1 | 2007-05-09 02:34:12 -0700 | [diff] [blame] | 925 | list_del(&wq->list); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 926 | spin_unlock(&workqueue_lock); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 927 | |
Mike Travis | 363ab6f | 2008-05-12 21:21:13 +0200 | [diff] [blame] | 928 | for_each_cpu_mask_nr(cpu, *cpu_map) |
Oleg Nesterov | 1e35eaa | 2008-04-29 01:00:28 -0700 | [diff] [blame] | 929 | cleanup_workqueue_thread(per_cpu_ptr(wq->cpu_wq, cpu)); |
Oleg Nesterov | 3da1c84 | 2008-07-25 01:47:50 -0700 | [diff] [blame] | 930 | cpu_maps_update_done(); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 931 | |
| 932 | free_percpu(wq->cpu_wq); |
| 933 | kfree(wq); |
| 934 | } |
| 935 | EXPORT_SYMBOL_GPL(destroy_workqueue); |
| 936 | |
| 937 | static int __devinit workqueue_cpu_callback(struct notifier_block *nfb, |
| 938 | unsigned long action, |
| 939 | void *hcpu) |
| 940 | { |
| 941 | unsigned int cpu = (unsigned long)hcpu; |
| 942 | struct cpu_workqueue_struct *cwq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | struct workqueue_struct *wq; |
Oleg Nesterov | 8448502 | 2008-07-25 01:47:54 -0700 | [diff] [blame] | 944 | int ret = NOTIFY_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 946 | action &= ~CPU_TASKS_FROZEN; |
| 947 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | switch (action) { |
| 949 | case CPU_UP_PREPARE: |
Rusty Russell | e7577c5 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 950 | cpumask_set_cpu(cpu, cpu_populated_map); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 951 | } |
Oleg Nesterov | 8448502 | 2008-07-25 01:47:54 -0700 | [diff] [blame] | 952 | undo: |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 953 | list_for_each_entry(wq, &workqueues, list) { |
| 954 | cwq = per_cpu_ptr(wq->cpu_wq, cpu); |
Christoph Lameter | 89ada67 | 2005-10-30 15:01:59 -0800 | [diff] [blame] | 955 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 956 | switch (action) { |
| 957 | case CPU_UP_PREPARE: |
| 958 | if (!create_workqueue_thread(cwq, cpu)) |
| 959 | break; |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 960 | printk(KERN_ERR "workqueue [%s] for %i failed\n", |
| 961 | wq->name, cpu); |
Oleg Nesterov | 8448502 | 2008-07-25 01:47:54 -0700 | [diff] [blame] | 962 | action = CPU_UP_CANCELED; |
| 963 | ret = NOTIFY_BAD; |
| 964 | goto undo; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 965 | |
| 966 | case CPU_ONLINE: |
Oleg Nesterov | 06ba38a | 2007-05-09 02:34:15 -0700 | [diff] [blame] | 967 | start_workqueue_thread(cwq, cpu); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 968 | break; |
| 969 | |
| 970 | case CPU_UP_CANCELED: |
Oleg Nesterov | 06ba38a | 2007-05-09 02:34:15 -0700 | [diff] [blame] | 971 | start_workqueue_thread(cwq, -1); |
Oleg Nesterov | 3da1c84 | 2008-07-25 01:47:50 -0700 | [diff] [blame] | 972 | case CPU_POST_DEAD: |
Oleg Nesterov | 1e35eaa | 2008-04-29 01:00:28 -0700 | [diff] [blame] | 973 | cleanup_workqueue_thread(cwq); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 974 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | } |
| 977 | |
Oleg Nesterov | 00dfcaf | 2008-04-29 01:00:27 -0700 | [diff] [blame] | 978 | switch (action) { |
| 979 | case CPU_UP_CANCELED: |
Oleg Nesterov | 3da1c84 | 2008-07-25 01:47:50 -0700 | [diff] [blame] | 980 | case CPU_POST_DEAD: |
Rusty Russell | e7577c5 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 981 | cpumask_clear_cpu(cpu, cpu_populated_map); |
Oleg Nesterov | 00dfcaf | 2008-04-29 01:00:27 -0700 | [diff] [blame] | 982 | } |
| 983 | |
Oleg Nesterov | 8448502 | 2008-07-25 01:47:54 -0700 | [diff] [blame] | 984 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 987 | #ifdef CONFIG_SMP |
Rusty Russell | 8ccad40 | 2009-01-16 15:31:15 -0800 | [diff] [blame] | 988 | static struct workqueue_struct *work_on_cpu_wq __read_mostly; |
| 989 | |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 990 | struct work_for_cpu { |
| 991 | struct work_struct work; |
| 992 | long (*fn)(void *); |
| 993 | void *arg; |
| 994 | long ret; |
| 995 | }; |
| 996 | |
| 997 | static void do_work_for_cpu(struct work_struct *w) |
| 998 | { |
| 999 | struct work_for_cpu *wfc = container_of(w, struct work_for_cpu, work); |
| 1000 | |
| 1001 | wfc->ret = wfc->fn(wfc->arg); |
| 1002 | } |
| 1003 | |
| 1004 | /** |
| 1005 | * work_on_cpu - run a function in user context on a particular cpu |
| 1006 | * @cpu: the cpu to run on |
| 1007 | * @fn: the function to run |
| 1008 | * @arg: the function arg |
| 1009 | * |
Rusty Russell | 31ad908 | 2009-01-16 15:31:15 -0800 | [diff] [blame] | 1010 | * This will return the value @fn returns. |
| 1011 | * It is up to the caller to ensure that the cpu doesn't go offline. |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 1012 | */ |
| 1013 | long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg) |
| 1014 | { |
| 1015 | struct work_for_cpu wfc; |
| 1016 | |
| 1017 | INIT_WORK(&wfc.work, do_work_for_cpu); |
| 1018 | wfc.fn = fn; |
| 1019 | wfc.arg = arg; |
Rusty Russell | 8ccad40 | 2009-01-16 15:31:15 -0800 | [diff] [blame] | 1020 | queue_work_on(cpu, work_on_cpu_wq, &wfc.work); |
Rusty Russell | 31ad908 | 2009-01-16 15:31:15 -0800 | [diff] [blame] | 1021 | flush_work(&wfc.work); |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 1022 | |
| 1023 | return wfc.ret; |
| 1024 | } |
| 1025 | EXPORT_SYMBOL_GPL(work_on_cpu); |
| 1026 | #endif /* CONFIG_SMP */ |
| 1027 | |
Oleg Nesterov | c12920d | 2007-05-09 02:34:14 -0700 | [diff] [blame] | 1028 | void __init init_workqueues(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | { |
Rusty Russell | e7577c5 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 1030 | alloc_cpumask_var(&cpu_populated_map, GFP_KERNEL); |
| 1031 | |
| 1032 | cpumask_copy(cpu_populated_map, cpu_online_mask); |
| 1033 | singlethread_cpu = cpumask_first(cpu_possible_mask); |
| 1034 | cpu_singlethread_map = cpumask_of(singlethread_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | hotcpu_notifier(workqueue_cpu_callback, 0); |
| 1036 | keventd_wq = create_workqueue("events"); |
| 1037 | BUG_ON(!keventd_wq); |
Rusty Russell | 8ccad40 | 2009-01-16 15:31:15 -0800 | [diff] [blame] | 1038 | #ifdef CONFIG_SMP |
| 1039 | work_on_cpu_wq = create_workqueue("work_on_cpu"); |
| 1040 | BUG_ON(!work_on_cpu_wq); |
| 1041 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | } |