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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | /* |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 38 | * Structure fields follow one of the following exclusion rules. |
| 39 | * |
| 40 | * I: Set during initialization and read-only afterwards. |
| 41 | * |
| 42 | * L: cwq->lock protected. Access with cwq->lock held. |
| 43 | * |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame^] | 44 | * F: wq->flush_mutex protected. |
| 45 | * |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 46 | * W: workqueue_lock protected. |
| 47 | */ |
| 48 | |
| 49 | /* |
Nathan Lynch | f756d5e | 2006-01-08 01:05:12 -0800 | [diff] [blame] | 50 | * The per-CPU workqueue (if single thread, we always use the first |
Tejun Heo | 0f90004 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 51 | * possible cpu). The lower WORK_STRUCT_FLAG_BITS of |
| 52 | * work_struct->data are used for flags and thus cwqs need to be |
| 53 | * aligned at two's power of the number of flag bits. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | */ |
| 55 | struct cpu_workqueue_struct { |
| 56 | |
| 57 | spinlock_t lock; |
| 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | struct list_head worklist; |
| 60 | wait_queue_head_t more_work; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 61 | struct work_struct *current_work; |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 62 | unsigned int cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 64 | struct workqueue_struct *wq; /* I: the owning workqueue */ |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame^] | 65 | int work_color; /* L: current color */ |
| 66 | int flush_color; /* L: flushing color */ |
| 67 | int nr_in_flight[WORK_NR_COLORS]; |
| 68 | /* L: nr of in_flight works */ |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 69 | struct task_struct *thread; |
Tejun Heo | 0f90004 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 70 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | /* |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame^] | 73 | * Structure used to wait for workqueue flush. |
| 74 | */ |
| 75 | struct wq_flusher { |
| 76 | struct list_head list; /* F: list of flushers */ |
| 77 | int flush_color; /* F: flush color waiting for */ |
| 78 | struct completion done; /* flush completion */ |
| 79 | }; |
| 80 | |
| 81 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | * The externally visible workqueue abstraction is an array of |
| 83 | * per-CPU workqueues: |
| 84 | */ |
| 85 | struct workqueue_struct { |
Tejun Heo | 97e37d7 | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 86 | unsigned int flags; /* I: WQ_* flags */ |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 87 | struct cpu_workqueue_struct *cpu_wq; /* I: cwq's */ |
| 88 | struct list_head list; /* W: list of all workqueues */ |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame^] | 89 | |
| 90 | struct mutex flush_mutex; /* protects wq flushing */ |
| 91 | int work_color; /* F: current work color */ |
| 92 | int flush_color; /* F: current flush color */ |
| 93 | atomic_t nr_cwqs_to_flush; /* flush in progress */ |
| 94 | struct wq_flusher *first_flusher; /* F: first flusher */ |
| 95 | struct list_head flusher_queue; /* F: flush waiters */ |
| 96 | struct list_head flusher_overflow; /* F: flush overflow list */ |
| 97 | |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 98 | const char *name; /* I: workqueue name */ |
Johannes Berg | 4e6045f | 2007-10-18 23:39:55 -0700 | [diff] [blame] | 99 | #ifdef CONFIG_LOCKDEP |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 100 | struct lockdep_map lockdep_map; |
Johannes Berg | 4e6045f | 2007-10-18 23:39:55 -0700 | [diff] [blame] | 101 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
Thomas Gleixner | dc186ad | 2009-11-16 01:09:48 +0900 | [diff] [blame] | 104 | #ifdef CONFIG_DEBUG_OBJECTS_WORK |
| 105 | |
| 106 | static struct debug_obj_descr work_debug_descr; |
| 107 | |
| 108 | /* |
| 109 | * fixup_init is called when: |
| 110 | * - an active object is initialized |
| 111 | */ |
| 112 | static int work_fixup_init(void *addr, enum debug_obj_state state) |
| 113 | { |
| 114 | struct work_struct *work = addr; |
| 115 | |
| 116 | switch (state) { |
| 117 | case ODEBUG_STATE_ACTIVE: |
| 118 | cancel_work_sync(work); |
| 119 | debug_object_init(work, &work_debug_descr); |
| 120 | return 1; |
| 121 | default: |
| 122 | return 0; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * fixup_activate is called when: |
| 128 | * - an active object is activated |
| 129 | * - an unknown object is activated (might be a statically initialized object) |
| 130 | */ |
| 131 | static int work_fixup_activate(void *addr, enum debug_obj_state state) |
| 132 | { |
| 133 | struct work_struct *work = addr; |
| 134 | |
| 135 | switch (state) { |
| 136 | |
| 137 | case ODEBUG_STATE_NOTAVAILABLE: |
| 138 | /* |
| 139 | * This is not really a fixup. The work struct was |
| 140 | * statically initialized. We just make sure that it |
| 141 | * is tracked in the object tracker. |
| 142 | */ |
Tejun Heo | 22df02b | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 143 | if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) { |
Thomas Gleixner | dc186ad | 2009-11-16 01:09:48 +0900 | [diff] [blame] | 144 | debug_object_init(work, &work_debug_descr); |
| 145 | debug_object_activate(work, &work_debug_descr); |
| 146 | return 0; |
| 147 | } |
| 148 | WARN_ON_ONCE(1); |
| 149 | return 0; |
| 150 | |
| 151 | case ODEBUG_STATE_ACTIVE: |
| 152 | WARN_ON(1); |
| 153 | |
| 154 | default: |
| 155 | return 0; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * fixup_free is called when: |
| 161 | * - an active object is freed |
| 162 | */ |
| 163 | static int work_fixup_free(void *addr, enum debug_obj_state state) |
| 164 | { |
| 165 | struct work_struct *work = addr; |
| 166 | |
| 167 | switch (state) { |
| 168 | case ODEBUG_STATE_ACTIVE: |
| 169 | cancel_work_sync(work); |
| 170 | debug_object_free(work, &work_debug_descr); |
| 171 | return 1; |
| 172 | default: |
| 173 | return 0; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | static struct debug_obj_descr work_debug_descr = { |
| 178 | .name = "work_struct", |
| 179 | .fixup_init = work_fixup_init, |
| 180 | .fixup_activate = work_fixup_activate, |
| 181 | .fixup_free = work_fixup_free, |
| 182 | }; |
| 183 | |
| 184 | static inline void debug_work_activate(struct work_struct *work) |
| 185 | { |
| 186 | debug_object_activate(work, &work_debug_descr); |
| 187 | } |
| 188 | |
| 189 | static inline void debug_work_deactivate(struct work_struct *work) |
| 190 | { |
| 191 | debug_object_deactivate(work, &work_debug_descr); |
| 192 | } |
| 193 | |
| 194 | void __init_work(struct work_struct *work, int onstack) |
| 195 | { |
| 196 | if (onstack) |
| 197 | debug_object_init_on_stack(work, &work_debug_descr); |
| 198 | else |
| 199 | debug_object_init(work, &work_debug_descr); |
| 200 | } |
| 201 | EXPORT_SYMBOL_GPL(__init_work); |
| 202 | |
| 203 | void destroy_work_on_stack(struct work_struct *work) |
| 204 | { |
| 205 | debug_object_free(work, &work_debug_descr); |
| 206 | } |
| 207 | EXPORT_SYMBOL_GPL(destroy_work_on_stack); |
| 208 | |
| 209 | #else |
| 210 | static inline void debug_work_activate(struct work_struct *work) { } |
| 211 | static inline void debug_work_deactivate(struct work_struct *work) { } |
| 212 | #endif |
| 213 | |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 214 | /* Serializes the accesses to the list of workqueues. */ |
| 215 | static DEFINE_SPINLOCK(workqueue_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | static LIST_HEAD(workqueues); |
| 217 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 218 | static int singlethread_cpu __read_mostly; |
Oleg Nesterov | b1f4ec1 | 2007-05-09 02:34:12 -0700 | [diff] [blame] | 219 | |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 220 | static struct cpu_workqueue_struct *get_cwq(unsigned int cpu, |
| 221 | struct workqueue_struct *wq) |
Oleg Nesterov | a848e3b | 2007-05-09 02:34:17 -0700 | [diff] [blame] | 222 | { |
Oleg Nesterov | a848e3b | 2007-05-09 02:34:17 -0700 | [diff] [blame] | 223 | return per_cpu_ptr(wq->cpu_wq, cpu); |
| 224 | } |
| 225 | |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 226 | static struct cpu_workqueue_struct *target_cwq(unsigned int cpu, |
| 227 | struct workqueue_struct *wq) |
| 228 | { |
| 229 | if (unlikely(wq->flags & WQ_SINGLE_THREAD)) |
| 230 | cpu = singlethread_cpu; |
| 231 | return get_cwq(cpu, wq); |
| 232 | } |
| 233 | |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame^] | 234 | static unsigned int work_color_to_flags(int color) |
| 235 | { |
| 236 | return color << WORK_STRUCT_COLOR_SHIFT; |
| 237 | } |
| 238 | |
| 239 | static int get_work_color(struct work_struct *work) |
| 240 | { |
| 241 | return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) & |
| 242 | ((1 << WORK_STRUCT_COLOR_BITS) - 1); |
| 243 | } |
| 244 | |
| 245 | static int work_next_color(int color) |
| 246 | { |
| 247 | return (color + 1) % WORK_NR_COLORS; |
| 248 | } |
| 249 | |
David Howells | 4594bf1 | 2006-12-07 11:33:26 +0000 | [diff] [blame] | 250 | /* |
| 251 | * Set the workqueue on which a work item is to be run |
| 252 | * - Must *only* be called if the pending flag is set |
| 253 | */ |
Oleg Nesterov | ed7c0fe | 2007-05-09 02:34:16 -0700 | [diff] [blame] | 254 | static inline void set_wq_data(struct work_struct *work, |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 255 | struct cpu_workqueue_struct *cwq, |
| 256 | unsigned long extra_flags) |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 257 | { |
David Howells | 4594bf1 | 2006-12-07 11:33:26 +0000 | [diff] [blame] | 258 | BUG_ON(!work_pending(work)); |
| 259 | |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 260 | atomic_long_set(&work->data, (unsigned long)cwq | work_static(work) | |
Tejun Heo | 22df02b | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 261 | WORK_STRUCT_PENDING | extra_flags); |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Oleg Nesterov | 4d707b9 | 2010-04-23 17:40:40 +0200 | [diff] [blame] | 264 | /* |
| 265 | * Clear WORK_STRUCT_PENDING and the workqueue on which it was queued. |
| 266 | */ |
| 267 | static inline void clear_wq_data(struct work_struct *work) |
| 268 | { |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 269 | atomic_long_set(&work->data, work_static(work)); |
Oleg Nesterov | 4d707b9 | 2010-04-23 17:40:40 +0200 | [diff] [blame] | 270 | } |
| 271 | |
Tejun Heo | 6416669 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 272 | static inline struct cpu_workqueue_struct *get_wq_data(struct work_struct *work) |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 273 | { |
Tejun Heo | 6416669 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 274 | return (void *)(atomic_long_read(&work->data) & |
| 275 | WORK_STRUCT_WQ_DATA_MASK); |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 278 | /** |
| 279 | * insert_work - insert a work into cwq |
| 280 | * @cwq: cwq @work belongs to |
| 281 | * @work: work to insert |
| 282 | * @head: insertion point |
| 283 | * @extra_flags: extra WORK_STRUCT_* flags to set |
| 284 | * |
| 285 | * Insert @work into @cwq after @head. |
| 286 | * |
| 287 | * CONTEXT: |
| 288 | * spin_lock_irq(cwq->lock). |
| 289 | */ |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 290 | static void insert_work(struct cpu_workqueue_struct *cwq, |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 291 | struct work_struct *work, struct list_head *head, |
| 292 | unsigned int extra_flags) |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 293 | { |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 294 | /* we own @work, set data and link */ |
| 295 | set_wq_data(work, cwq, extra_flags); |
| 296 | |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 297 | /* |
| 298 | * Ensure that we get the right work->data if we see the |
| 299 | * result of list_add() below, see try_to_grab_pending(). |
| 300 | */ |
| 301 | smp_wmb(); |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 302 | |
Oleg Nesterov | 1a4d9b0 | 2008-07-25 01:47:47 -0700 | [diff] [blame] | 303 | list_add_tail(&work->entry, head); |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 304 | wake_up(&cwq->more_work); |
| 305 | } |
| 306 | |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 307 | static void __queue_work(unsigned int cpu, struct workqueue_struct *wq, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | struct work_struct *work) |
| 309 | { |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 310 | struct cpu_workqueue_struct *cwq = target_cwq(cpu, wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | unsigned long flags; |
| 312 | |
Thomas Gleixner | dc186ad | 2009-11-16 01:09:48 +0900 | [diff] [blame] | 313 | debug_work_activate(work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | spin_lock_irqsave(&cwq->lock, flags); |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 315 | BUG_ON(!list_empty(&work->entry)); |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame^] | 316 | cwq->nr_in_flight[cwq->work_color]++; |
| 317 | insert_work(cwq, work, &cwq->worklist, |
| 318 | work_color_to_flags(cwq->work_color)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | spin_unlock_irqrestore(&cwq->lock, flags); |
| 320 | } |
| 321 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 322 | /** |
| 323 | * queue_work - queue work on a workqueue |
| 324 | * @wq: workqueue to use |
| 325 | * @work: work to queue |
| 326 | * |
Alan Stern | 057647f | 2006-10-28 10:38:58 -0700 | [diff] [blame] | 327 | * Returns 0 if @work was already on a queue, non-zero otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | * |
Oleg Nesterov | 00dfcaf | 2008-04-29 01:00:27 -0700 | [diff] [blame] | 329 | * We queue the work to the CPU on which it was submitted, but if the CPU dies |
| 330 | * it can be processed by another CPU. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 332 | int queue_work(struct workqueue_struct *wq, struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | { |
Oleg Nesterov | ef1ca23 | 2008-07-25 01:47:53 -0700 | [diff] [blame] | 334 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | |
Oleg Nesterov | ef1ca23 | 2008-07-25 01:47:53 -0700 | [diff] [blame] | 336 | ret = queue_work_on(get_cpu(), wq, work); |
| 337 | put_cpu(); |
| 338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | return ret; |
| 340 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 341 | EXPORT_SYMBOL_GPL(queue_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
Zhang Rui | c1a220e | 2008-07-23 21:28:39 -0700 | [diff] [blame] | 343 | /** |
| 344 | * queue_work_on - queue work on specific cpu |
| 345 | * @cpu: CPU number to execute work on |
| 346 | * @wq: workqueue to use |
| 347 | * @work: work to queue |
| 348 | * |
| 349 | * Returns 0 if @work was already on a queue, non-zero otherwise. |
| 350 | * |
| 351 | * We queue the work to a specific CPU, the caller must ensure it |
| 352 | * can't go away. |
| 353 | */ |
| 354 | int |
| 355 | queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work) |
| 356 | { |
| 357 | int ret = 0; |
| 358 | |
Tejun Heo | 22df02b | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 359 | if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 360 | __queue_work(cpu, wq, work); |
Zhang Rui | c1a220e | 2008-07-23 21:28:39 -0700 | [diff] [blame] | 361 | ret = 1; |
| 362 | } |
| 363 | return ret; |
| 364 | } |
| 365 | EXPORT_SYMBOL_GPL(queue_work_on); |
| 366 | |
Li Zefan | 6d141c3 | 2008-02-08 04:21:09 -0800 | [diff] [blame] | 367 | static void delayed_work_timer_fn(unsigned long __data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | { |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 369 | struct delayed_work *dwork = (struct delayed_work *)__data; |
Oleg Nesterov | ed7c0fe | 2007-05-09 02:34:16 -0700 | [diff] [blame] | 370 | struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 372 | __queue_work(smp_processor_id(), cwq->wq, &dwork->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 375 | /** |
| 376 | * queue_delayed_work - queue work on a workqueue after delay |
| 377 | * @wq: workqueue to use |
Randy Dunlap | af9997e | 2006-12-22 01:06:52 -0800 | [diff] [blame] | 378 | * @dwork: delayable work to queue |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 379 | * @delay: number of jiffies to wait before queueing |
| 380 | * |
Alan Stern | 057647f | 2006-10-28 10:38:58 -0700 | [diff] [blame] | 381 | * 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] | 382 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 383 | int queue_delayed_work(struct workqueue_struct *wq, |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 384 | struct delayed_work *dwork, unsigned long delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | { |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 386 | if (delay == 0) |
Oleg Nesterov | 63bc036 | 2007-05-09 02:34:16 -0700 | [diff] [blame] | 387 | return queue_work(wq, &dwork->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
Oleg Nesterov | 63bc036 | 2007-05-09 02:34:16 -0700 | [diff] [blame] | 389 | return queue_delayed_work_on(-1, wq, dwork, delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 391 | EXPORT_SYMBOL_GPL(queue_delayed_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 393 | /** |
| 394 | * queue_delayed_work_on - queue work on specific CPU after delay |
| 395 | * @cpu: CPU number to execute work on |
| 396 | * @wq: workqueue to use |
Randy Dunlap | af9997e | 2006-12-22 01:06:52 -0800 | [diff] [blame] | 397 | * @dwork: work to queue |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 398 | * @delay: number of jiffies to wait before queueing |
| 399 | * |
Alan Stern | 057647f | 2006-10-28 10:38:58 -0700 | [diff] [blame] | 400 | * 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] | 401 | */ |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 402 | int queue_delayed_work_on(int cpu, struct workqueue_struct *wq, |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 403 | struct delayed_work *dwork, unsigned long delay) |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 404 | { |
| 405 | int ret = 0; |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 406 | struct timer_list *timer = &dwork->timer; |
| 407 | struct work_struct *work = &dwork->work; |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 408 | |
Tejun Heo | 22df02b | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 409 | if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 410 | BUG_ON(timer_pending(timer)); |
| 411 | BUG_ON(!list_empty(&work->entry)); |
| 412 | |
Andrew Liu | 8a3e77c | 2008-05-01 04:35:14 -0700 | [diff] [blame] | 413 | timer_stats_timer_set_start_info(&dwork->timer); |
| 414 | |
Oleg Nesterov | ed7c0fe | 2007-05-09 02:34:16 -0700 | [diff] [blame] | 415 | /* This stores cwq for the moment, for the timer_fn */ |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 416 | set_wq_data(work, target_cwq(raw_smp_processor_id(), wq), 0); |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 417 | timer->expires = jiffies + delay; |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 418 | timer->data = (unsigned long)dwork; |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 419 | timer->function = delayed_work_timer_fn; |
Oleg Nesterov | 63bc036 | 2007-05-09 02:34:16 -0700 | [diff] [blame] | 420 | |
| 421 | if (unlikely(cpu >= 0)) |
| 422 | add_timer_on(timer, cpu); |
| 423 | else |
| 424 | add_timer(timer); |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 425 | ret = 1; |
| 426 | } |
| 427 | return ret; |
| 428 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 429 | EXPORT_SYMBOL_GPL(queue_delayed_work_on); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 431 | /** |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame^] | 432 | * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight |
| 433 | * @cwq: cwq of interest |
| 434 | * @color: color of work which left the queue |
| 435 | * |
| 436 | * A work either has completed or is removed from pending queue, |
| 437 | * decrement nr_in_flight of its cwq and handle workqueue flushing. |
| 438 | * |
| 439 | * CONTEXT: |
| 440 | * spin_lock_irq(cwq->lock). |
| 441 | */ |
| 442 | static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color) |
| 443 | { |
| 444 | /* ignore uncolored works */ |
| 445 | if (color == WORK_NO_COLOR) |
| 446 | return; |
| 447 | |
| 448 | cwq->nr_in_flight[color]--; |
| 449 | |
| 450 | /* is flush in progress and are we at the flushing tip? */ |
| 451 | if (likely(cwq->flush_color != color)) |
| 452 | return; |
| 453 | |
| 454 | /* are there still in-flight works? */ |
| 455 | if (cwq->nr_in_flight[color]) |
| 456 | return; |
| 457 | |
| 458 | /* this cwq is done, clear flush_color */ |
| 459 | cwq->flush_color = -1; |
| 460 | |
| 461 | /* |
| 462 | * If this was the last cwq, wake up the first flusher. It |
| 463 | * will handle the rest. |
| 464 | */ |
| 465 | if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush)) |
| 466 | complete(&cwq->wq->first_flusher->done); |
| 467 | } |
| 468 | |
| 469 | /** |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 470 | * process_one_work - process single work |
| 471 | * @cwq: cwq to process work for |
| 472 | * @work: work to process |
| 473 | * |
| 474 | * Process @work. This function contains all the logics necessary to |
| 475 | * process a single work including synchronization against and |
| 476 | * interaction with other workers on the same cpu, queueing and |
| 477 | * flushing. As long as context requirement is met, any worker can |
| 478 | * call this function to process a work. |
| 479 | * |
| 480 | * CONTEXT: |
| 481 | * spin_lock_irq(cwq->lock) which is released and regrabbed. |
| 482 | */ |
| 483 | static void process_one_work(struct cpu_workqueue_struct *cwq, |
| 484 | struct work_struct *work) |
| 485 | { |
| 486 | work_func_t f = work->func; |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame^] | 487 | int work_color; |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 488 | #ifdef CONFIG_LOCKDEP |
| 489 | /* |
| 490 | * It is permissible to free the struct work_struct from |
| 491 | * inside the function that is called from it, this we need to |
| 492 | * take into account for lockdep too. To avoid bogus "held |
| 493 | * lock freed" warnings as well as problems when looking into |
| 494 | * work->lockdep_map, make a copy and use that here. |
| 495 | */ |
| 496 | struct lockdep_map lockdep_map = work->lockdep_map; |
| 497 | #endif |
| 498 | /* claim and process */ |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 499 | debug_work_deactivate(work); |
| 500 | cwq->current_work = work; |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame^] | 501 | work_color = get_work_color(work); |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 502 | list_del_init(&work->entry); |
| 503 | |
| 504 | spin_unlock_irq(&cwq->lock); |
| 505 | |
| 506 | BUG_ON(get_wq_data(work) != cwq); |
| 507 | work_clear_pending(work); |
| 508 | lock_map_acquire(&cwq->wq->lockdep_map); |
| 509 | lock_map_acquire(&lockdep_map); |
| 510 | f(work); |
| 511 | lock_map_release(&lockdep_map); |
| 512 | lock_map_release(&cwq->wq->lockdep_map); |
| 513 | |
| 514 | if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { |
| 515 | printk(KERN_ERR "BUG: workqueue leaked lock or atomic: " |
| 516 | "%s/0x%08x/%d\n", |
| 517 | current->comm, preempt_count(), task_pid_nr(current)); |
| 518 | printk(KERN_ERR " last function: "); |
| 519 | print_symbol("%s\n", (unsigned long)f); |
| 520 | debug_show_held_locks(current); |
| 521 | dump_stack(); |
| 522 | } |
| 523 | |
| 524 | spin_lock_irq(&cwq->lock); |
| 525 | |
| 526 | /* we're done with it, release */ |
| 527 | cwq->current_work = NULL; |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame^] | 528 | cwq_dec_nr_in_flight(cwq, work_color); |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 529 | } |
| 530 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 531 | static void run_workqueue(struct cpu_workqueue_struct *cwq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | { |
Oleg Nesterov | f293ea9 | 2007-05-09 02:34:10 -0700 | [diff] [blame] | 533 | spin_lock_irq(&cwq->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | while (!list_empty(&cwq->worklist)) { |
| 535 | struct work_struct *work = list_entry(cwq->worklist.next, |
| 536 | struct work_struct, entry); |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 537 | process_one_work(cwq, work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | } |
Oleg Nesterov | f293ea9 | 2007-05-09 02:34:10 -0700 | [diff] [blame] | 539 | spin_unlock_irq(&cwq->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 542 | /** |
| 543 | * worker_thread - the worker thread function |
| 544 | * @__cwq: cwq to serve |
| 545 | * |
| 546 | * The cwq worker thread function. |
| 547 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | static int worker_thread(void *__cwq) |
| 549 | { |
| 550 | struct cpu_workqueue_struct *cwq = __cwq; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 551 | DEFINE_WAIT(wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | |
Tejun Heo | 97e37d7 | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 553 | if (cwq->wq->flags & WQ_FREEZEABLE) |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 554 | set_freezable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 556 | for (;;) { |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 557 | prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE); |
Oleg Nesterov | 1444196 | 2007-05-23 13:57:57 -0700 | [diff] [blame] | 558 | if (!freezing(current) && |
| 559 | !kthread_should_stop() && |
| 560 | list_empty(&cwq->worklist)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | schedule(); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 562 | finish_wait(&cwq->more_work, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
Oleg Nesterov | 85f4186 | 2007-05-09 02:34:20 -0700 | [diff] [blame] | 564 | try_to_freeze(); |
| 565 | |
Oleg Nesterov | 1444196 | 2007-05-23 13:57:57 -0700 | [diff] [blame] | 566 | if (kthread_should_stop()) |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 567 | break; |
| 568 | |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 569 | if (unlikely(!cpumask_equal(&cwq->thread->cpus_allowed, |
| 570 | get_cpu_mask(cwq->cpu)))) |
| 571 | set_cpus_allowed_ptr(cwq->thread, |
| 572 | get_cpu_mask(cwq->cpu)); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 573 | run_workqueue(cwq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | } |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | return 0; |
| 577 | } |
| 578 | |
Oleg Nesterov | fc2e4d7 | 2007-05-09 02:33:51 -0700 | [diff] [blame] | 579 | struct wq_barrier { |
| 580 | struct work_struct work; |
| 581 | struct completion done; |
| 582 | }; |
| 583 | |
| 584 | static void wq_barrier_func(struct work_struct *work) |
| 585 | { |
| 586 | struct wq_barrier *barr = container_of(work, struct wq_barrier, work); |
| 587 | complete(&barr->done); |
| 588 | } |
| 589 | |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 590 | /** |
| 591 | * insert_wq_barrier - insert a barrier work |
| 592 | * @cwq: cwq to insert barrier into |
| 593 | * @barr: wq_barrier to insert |
| 594 | * @head: insertion point |
| 595 | * |
| 596 | * Insert barrier @barr into @cwq before @head. |
| 597 | * |
| 598 | * CONTEXT: |
| 599 | * spin_lock_irq(cwq->lock). |
| 600 | */ |
Oleg Nesterov | 83c2252 | 2007-05-09 02:33:54 -0700 | [diff] [blame] | 601 | static void insert_wq_barrier(struct cpu_workqueue_struct *cwq, |
Oleg Nesterov | 1a4d9b0 | 2008-07-25 01:47:47 -0700 | [diff] [blame] | 602 | struct wq_barrier *barr, struct list_head *head) |
Oleg Nesterov | fc2e4d7 | 2007-05-09 02:33:51 -0700 | [diff] [blame] | 603 | { |
Thomas Gleixner | dc186ad | 2009-11-16 01:09:48 +0900 | [diff] [blame] | 604 | /* |
| 605 | * debugobject calls are safe here even with cwq->lock locked |
| 606 | * as we know for sure that this will not trigger any of the |
| 607 | * checks and call back into the fixup functions where we |
| 608 | * might deadlock. |
| 609 | */ |
| 610 | INIT_WORK_ON_STACK(&barr->work, wq_barrier_func); |
Tejun Heo | 22df02b | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 611 | __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); |
Oleg Nesterov | fc2e4d7 | 2007-05-09 02:33:51 -0700 | [diff] [blame] | 612 | init_completion(&barr->done); |
Oleg Nesterov | 83c2252 | 2007-05-09 02:33:54 -0700 | [diff] [blame] | 613 | |
Thomas Gleixner | dc186ad | 2009-11-16 01:09:48 +0900 | [diff] [blame] | 614 | debug_work_activate(&barr->work); |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame^] | 615 | insert_work(cwq, &barr->work, head, work_color_to_flags(WORK_NO_COLOR)); |
Oleg Nesterov | fc2e4d7 | 2007-05-09 02:33:51 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame^] | 618 | /** |
| 619 | * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing |
| 620 | * @wq: workqueue being flushed |
| 621 | * @flush_color: new flush color, < 0 for no-op |
| 622 | * @work_color: new work color, < 0 for no-op |
| 623 | * |
| 624 | * Prepare cwqs for workqueue flushing. |
| 625 | * |
| 626 | * If @flush_color is non-negative, flush_color on all cwqs should be |
| 627 | * -1. If no cwq has in-flight commands at the specified color, all |
| 628 | * cwq->flush_color's stay at -1 and %false is returned. If any cwq |
| 629 | * has in flight commands, its cwq->flush_color is set to |
| 630 | * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq |
| 631 | * wakeup logic is armed and %true is returned. |
| 632 | * |
| 633 | * The caller should have initialized @wq->first_flusher prior to |
| 634 | * calling this function with non-negative @flush_color. If |
| 635 | * @flush_color is negative, no flush color update is done and %false |
| 636 | * is returned. |
| 637 | * |
| 638 | * If @work_color is non-negative, all cwqs should have the same |
| 639 | * work_color which is previous to @work_color and all will be |
| 640 | * advanced to @work_color. |
| 641 | * |
| 642 | * CONTEXT: |
| 643 | * mutex_lock(wq->flush_mutex). |
| 644 | * |
| 645 | * RETURNS: |
| 646 | * %true if @flush_color >= 0 and there's something to flush. %false |
| 647 | * otherwise. |
| 648 | */ |
| 649 | static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq, |
| 650 | int flush_color, int work_color) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | { |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame^] | 652 | bool wait = false; |
| 653 | unsigned int cpu; |
Oleg Nesterov | 1444196 | 2007-05-23 13:57:57 -0700 | [diff] [blame] | 654 | |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame^] | 655 | if (flush_color >= 0) { |
| 656 | BUG_ON(atomic_read(&wq->nr_cwqs_to_flush)); |
| 657 | atomic_set(&wq->nr_cwqs_to_flush, 1); |
Thomas Gleixner | dc186ad | 2009-11-16 01:09:48 +0900 | [diff] [blame] | 658 | } |
Oleg Nesterov | 1444196 | 2007-05-23 13:57:57 -0700 | [diff] [blame] | 659 | |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame^] | 660 | for_each_possible_cpu(cpu) { |
| 661 | struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); |
| 662 | |
| 663 | spin_lock_irq(&cwq->lock); |
| 664 | |
| 665 | if (flush_color >= 0) { |
| 666 | BUG_ON(cwq->flush_color != -1); |
| 667 | |
| 668 | if (cwq->nr_in_flight[flush_color]) { |
| 669 | cwq->flush_color = flush_color; |
| 670 | atomic_inc(&wq->nr_cwqs_to_flush); |
| 671 | wait = true; |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | if (work_color >= 0) { |
| 676 | BUG_ON(work_color != work_next_color(cwq->work_color)); |
| 677 | cwq->work_color = work_color; |
| 678 | } |
| 679 | |
| 680 | spin_unlock_irq(&cwq->lock); |
| 681 | } |
| 682 | |
| 683 | if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush)) |
| 684 | complete(&wq->first_flusher->done); |
| 685 | |
| 686 | return wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | } |
| 688 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 689 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | * flush_workqueue - ensure that any scheduled work has run to completion. |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 691 | * @wq: workqueue to flush |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | * |
| 693 | * Forces execution of the workqueue and blocks until its completion. |
| 694 | * This is typically used in driver shutdown handlers. |
| 695 | * |
Oleg Nesterov | fc2e4d7 | 2007-05-09 02:33:51 -0700 | [diff] [blame] | 696 | * We sleep until all works which were queued on entry have been handled, |
| 697 | * but we are not livelocked by new incoming ones. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 699 | void flush_workqueue(struct workqueue_struct *wq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | { |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame^] | 701 | struct wq_flusher this_flusher = { |
| 702 | .list = LIST_HEAD_INIT(this_flusher.list), |
| 703 | .flush_color = -1, |
| 704 | .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done), |
| 705 | }; |
| 706 | int next_color; |
Oleg Nesterov | b1f4ec1 | 2007-05-09 02:34:12 -0700 | [diff] [blame] | 707 | |
Ingo Molnar | 3295f0e | 2008-08-11 10:30:30 +0200 | [diff] [blame] | 708 | lock_map_acquire(&wq->lockdep_map); |
| 709 | lock_map_release(&wq->lockdep_map); |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame^] | 710 | |
| 711 | mutex_lock(&wq->flush_mutex); |
| 712 | |
| 713 | /* |
| 714 | * Start-to-wait phase |
| 715 | */ |
| 716 | next_color = work_next_color(wq->work_color); |
| 717 | |
| 718 | if (next_color != wq->flush_color) { |
| 719 | /* |
| 720 | * Color space is not full. The current work_color |
| 721 | * becomes our flush_color and work_color is advanced |
| 722 | * by one. |
| 723 | */ |
| 724 | BUG_ON(!list_empty(&wq->flusher_overflow)); |
| 725 | this_flusher.flush_color = wq->work_color; |
| 726 | wq->work_color = next_color; |
| 727 | |
| 728 | if (!wq->first_flusher) { |
| 729 | /* no flush in progress, become the first flusher */ |
| 730 | BUG_ON(wq->flush_color != this_flusher.flush_color); |
| 731 | |
| 732 | wq->first_flusher = &this_flusher; |
| 733 | |
| 734 | if (!flush_workqueue_prep_cwqs(wq, wq->flush_color, |
| 735 | wq->work_color)) { |
| 736 | /* nothing to flush, done */ |
| 737 | wq->flush_color = next_color; |
| 738 | wq->first_flusher = NULL; |
| 739 | goto out_unlock; |
| 740 | } |
| 741 | } else { |
| 742 | /* wait in queue */ |
| 743 | BUG_ON(wq->flush_color == this_flusher.flush_color); |
| 744 | list_add_tail(&this_flusher.list, &wq->flusher_queue); |
| 745 | flush_workqueue_prep_cwqs(wq, -1, wq->work_color); |
| 746 | } |
| 747 | } else { |
| 748 | /* |
| 749 | * Oops, color space is full, wait on overflow queue. |
| 750 | * The next flush completion will assign us |
| 751 | * flush_color and transfer to flusher_queue. |
| 752 | */ |
| 753 | list_add_tail(&this_flusher.list, &wq->flusher_overflow); |
| 754 | } |
| 755 | |
| 756 | mutex_unlock(&wq->flush_mutex); |
| 757 | |
| 758 | wait_for_completion(&this_flusher.done); |
| 759 | |
| 760 | /* |
| 761 | * Wake-up-and-cascade phase |
| 762 | * |
| 763 | * First flushers are responsible for cascading flushes and |
| 764 | * handling overflow. Non-first flushers can simply return. |
| 765 | */ |
| 766 | if (wq->first_flusher != &this_flusher) |
| 767 | return; |
| 768 | |
| 769 | mutex_lock(&wq->flush_mutex); |
| 770 | |
| 771 | wq->first_flusher = NULL; |
| 772 | |
| 773 | BUG_ON(!list_empty(&this_flusher.list)); |
| 774 | BUG_ON(wq->flush_color != this_flusher.flush_color); |
| 775 | |
| 776 | while (true) { |
| 777 | struct wq_flusher *next, *tmp; |
| 778 | |
| 779 | /* complete all the flushers sharing the current flush color */ |
| 780 | list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { |
| 781 | if (next->flush_color != wq->flush_color) |
| 782 | break; |
| 783 | list_del_init(&next->list); |
| 784 | complete(&next->done); |
| 785 | } |
| 786 | |
| 787 | BUG_ON(!list_empty(&wq->flusher_overflow) && |
| 788 | wq->flush_color != work_next_color(wq->work_color)); |
| 789 | |
| 790 | /* this flush_color is finished, advance by one */ |
| 791 | wq->flush_color = work_next_color(wq->flush_color); |
| 792 | |
| 793 | /* one color has been freed, handle overflow queue */ |
| 794 | if (!list_empty(&wq->flusher_overflow)) { |
| 795 | /* |
| 796 | * Assign the same color to all overflowed |
| 797 | * flushers, advance work_color and append to |
| 798 | * flusher_queue. This is the start-to-wait |
| 799 | * phase for these overflowed flushers. |
| 800 | */ |
| 801 | list_for_each_entry(tmp, &wq->flusher_overflow, list) |
| 802 | tmp->flush_color = wq->work_color; |
| 803 | |
| 804 | wq->work_color = work_next_color(wq->work_color); |
| 805 | |
| 806 | list_splice_tail_init(&wq->flusher_overflow, |
| 807 | &wq->flusher_queue); |
| 808 | flush_workqueue_prep_cwqs(wq, -1, wq->work_color); |
| 809 | } |
| 810 | |
| 811 | if (list_empty(&wq->flusher_queue)) { |
| 812 | BUG_ON(wq->flush_color != wq->work_color); |
| 813 | break; |
| 814 | } |
| 815 | |
| 816 | /* |
| 817 | * Need to flush more colors. Make the next flusher |
| 818 | * the new first flusher and arm cwqs. |
| 819 | */ |
| 820 | BUG_ON(wq->flush_color == wq->work_color); |
| 821 | BUG_ON(wq->flush_color != next->flush_color); |
| 822 | |
| 823 | list_del_init(&next->list); |
| 824 | wq->first_flusher = next; |
| 825 | |
| 826 | if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1)) |
| 827 | break; |
| 828 | |
| 829 | /* |
| 830 | * Meh... this color is already done, clear first |
| 831 | * flusher and repeat cascading. |
| 832 | */ |
| 833 | wq->first_flusher = NULL; |
| 834 | } |
| 835 | |
| 836 | out_unlock: |
| 837 | mutex_unlock(&wq->flush_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 839 | EXPORT_SYMBOL_GPL(flush_workqueue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | |
Oleg Nesterov | db70089 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 841 | /** |
| 842 | * flush_work - block until a work_struct's callback has terminated |
| 843 | * @work: the work which is to be flushed |
| 844 | * |
Oleg Nesterov | a67da70 | 2008-07-25 01:47:52 -0700 | [diff] [blame] | 845 | * Returns false if @work has already terminated. |
| 846 | * |
Oleg Nesterov | db70089 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 847 | * It is expected that, prior to calling flush_work(), the caller has |
| 848 | * arranged for the work to not be requeued, otherwise it doesn't make |
| 849 | * sense to use this function. |
| 850 | */ |
| 851 | int flush_work(struct work_struct *work) |
| 852 | { |
| 853 | struct cpu_workqueue_struct *cwq; |
| 854 | struct list_head *prev; |
| 855 | struct wq_barrier barr; |
| 856 | |
| 857 | might_sleep(); |
| 858 | cwq = get_wq_data(work); |
| 859 | if (!cwq) |
| 860 | return 0; |
| 861 | |
Ingo Molnar | 3295f0e | 2008-08-11 10:30:30 +0200 | [diff] [blame] | 862 | lock_map_acquire(&cwq->wq->lockdep_map); |
| 863 | lock_map_release(&cwq->wq->lockdep_map); |
Oleg Nesterov | a67da70 | 2008-07-25 01:47:52 -0700 | [diff] [blame] | 864 | |
Oleg Nesterov | db70089 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 865 | spin_lock_irq(&cwq->lock); |
| 866 | if (!list_empty(&work->entry)) { |
| 867 | /* |
| 868 | * See the comment near try_to_grab_pending()->smp_rmb(). |
| 869 | * If it was re-queued under us we are not going to wait. |
| 870 | */ |
| 871 | smp_rmb(); |
| 872 | if (unlikely(cwq != get_wq_data(work))) |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 873 | goto already_gone; |
Oleg Nesterov | db70089 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 874 | prev = &work->entry; |
| 875 | } else { |
| 876 | if (cwq->current_work != work) |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 877 | goto already_gone; |
Oleg Nesterov | db70089 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 878 | prev = &cwq->worklist; |
| 879 | } |
| 880 | insert_wq_barrier(cwq, &barr, prev->next); |
Oleg Nesterov | db70089 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 881 | |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 882 | spin_unlock_irq(&cwq->lock); |
Oleg Nesterov | db70089 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 883 | wait_for_completion(&barr.done); |
Thomas Gleixner | dc186ad | 2009-11-16 01:09:48 +0900 | [diff] [blame] | 884 | destroy_work_on_stack(&barr.work); |
Oleg Nesterov | db70089 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 885 | return 1; |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 886 | already_gone: |
| 887 | spin_unlock_irq(&cwq->lock); |
| 888 | return 0; |
Oleg Nesterov | db70089 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 889 | } |
| 890 | EXPORT_SYMBOL_GPL(flush_work); |
| 891 | |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 892 | /* |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 893 | * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit, |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 894 | * so this work can't be re-armed in any way. |
| 895 | */ |
| 896 | static int try_to_grab_pending(struct work_struct *work) |
| 897 | { |
| 898 | struct cpu_workqueue_struct *cwq; |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 899 | int ret = -1; |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 900 | |
Tejun Heo | 22df02b | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 901 | if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 902 | return 0; |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 903 | |
| 904 | /* |
| 905 | * The queueing is in progress, or it is already queued. Try to |
| 906 | * steal it from ->worklist without clearing WORK_STRUCT_PENDING. |
| 907 | */ |
| 908 | |
| 909 | cwq = get_wq_data(work); |
| 910 | if (!cwq) |
| 911 | return ret; |
| 912 | |
| 913 | spin_lock_irq(&cwq->lock); |
| 914 | if (!list_empty(&work->entry)) { |
| 915 | /* |
| 916 | * This work is queued, but perhaps we locked the wrong cwq. |
| 917 | * In that case we must see the new value after rmb(), see |
| 918 | * insert_work()->wmb(). |
| 919 | */ |
| 920 | smp_rmb(); |
| 921 | if (cwq == get_wq_data(work)) { |
Thomas Gleixner | dc186ad | 2009-11-16 01:09:48 +0900 | [diff] [blame] | 922 | debug_work_deactivate(work); |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 923 | list_del_init(&work->entry); |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame^] | 924 | cwq_dec_nr_in_flight(cwq, get_work_color(work)); |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 925 | ret = 1; |
| 926 | } |
| 927 | } |
| 928 | spin_unlock_irq(&cwq->lock); |
| 929 | |
| 930 | return ret; |
| 931 | } |
| 932 | |
| 933 | static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq, |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 934 | struct work_struct *work) |
| 935 | { |
| 936 | struct wq_barrier barr; |
| 937 | int running = 0; |
| 938 | |
| 939 | spin_lock_irq(&cwq->lock); |
| 940 | if (unlikely(cwq->current_work == work)) { |
Oleg Nesterov | 1a4d9b0 | 2008-07-25 01:47:47 -0700 | [diff] [blame] | 941 | insert_wq_barrier(cwq, &barr, cwq->worklist.next); |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 942 | running = 1; |
| 943 | } |
| 944 | spin_unlock_irq(&cwq->lock); |
| 945 | |
Thomas Gleixner | dc186ad | 2009-11-16 01:09:48 +0900 | [diff] [blame] | 946 | if (unlikely(running)) { |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 947 | wait_for_completion(&barr.done); |
Thomas Gleixner | dc186ad | 2009-11-16 01:09:48 +0900 | [diff] [blame] | 948 | destroy_work_on_stack(&barr.work); |
| 949 | } |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 950 | } |
| 951 | |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 952 | static void wait_on_work(struct work_struct *work) |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 953 | { |
| 954 | struct cpu_workqueue_struct *cwq; |
Oleg Nesterov | 28e53bd | 2007-05-09 02:34:22 -0700 | [diff] [blame] | 955 | struct workqueue_struct *wq; |
Oleg Nesterov | b1f4ec1 | 2007-05-09 02:34:12 -0700 | [diff] [blame] | 956 | int cpu; |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 957 | |
Oleg Nesterov | f293ea9 | 2007-05-09 02:34:10 -0700 | [diff] [blame] | 958 | might_sleep(); |
| 959 | |
Ingo Molnar | 3295f0e | 2008-08-11 10:30:30 +0200 | [diff] [blame] | 960 | lock_map_acquire(&work->lockdep_map); |
| 961 | lock_map_release(&work->lockdep_map); |
Johannes Berg | 4e6045f | 2007-10-18 23:39:55 -0700 | [diff] [blame] | 962 | |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 963 | cwq = get_wq_data(work); |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 964 | if (!cwq) |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 965 | return; |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 966 | |
Oleg Nesterov | 28e53bd | 2007-05-09 02:34:22 -0700 | [diff] [blame] | 967 | wq = cwq->wq; |
Oleg Nesterov | 28e53bd | 2007-05-09 02:34:22 -0700 | [diff] [blame] | 968 | |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 969 | for_each_possible_cpu(cpu) |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 970 | wait_on_cpu_work(get_cwq(cpu, wq), work); |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 971 | } |
| 972 | |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 973 | static int __cancel_work_timer(struct work_struct *work, |
| 974 | struct timer_list* timer) |
| 975 | { |
| 976 | int ret; |
| 977 | |
| 978 | do { |
| 979 | ret = (timer && likely(del_timer(timer))); |
| 980 | if (!ret) |
| 981 | ret = try_to_grab_pending(work); |
| 982 | wait_on_work(work); |
| 983 | } while (unlikely(ret < 0)); |
| 984 | |
Oleg Nesterov | 4d707b9 | 2010-04-23 17:40:40 +0200 | [diff] [blame] | 985 | clear_wq_data(work); |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 986 | return ret; |
| 987 | } |
| 988 | |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 989 | /** |
| 990 | * cancel_work_sync - block until a work_struct's callback has terminated |
| 991 | * @work: the work which is to be flushed |
| 992 | * |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 993 | * Returns true if @work was pending. |
| 994 | * |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 995 | * cancel_work_sync() will cancel the work if it is queued. If the work's |
| 996 | * callback appears to be running, cancel_work_sync() will block until it |
| 997 | * has completed. |
| 998 | * |
| 999 | * It is possible to use this function if the work re-queues itself. It can |
| 1000 | * cancel the work even if it migrates to another workqueue, however in that |
| 1001 | * case it only guarantees that work->func() has completed on the last queued |
| 1002 | * workqueue. |
| 1003 | * |
| 1004 | * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not |
| 1005 | * pending, otherwise it goes into a busy-wait loop until the timer expires. |
| 1006 | * |
| 1007 | * The caller must ensure that workqueue_struct on which this work was last |
| 1008 | * queued can't be destroyed before this function returns. |
| 1009 | */ |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 1010 | int cancel_work_sync(struct work_struct *work) |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 1011 | { |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 1012 | return __cancel_work_timer(work, NULL); |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 1013 | } |
Oleg Nesterov | 28e53bd | 2007-05-09 02:34:22 -0700 | [diff] [blame] | 1014 | EXPORT_SYMBOL_GPL(cancel_work_sync); |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 1015 | |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 1016 | /** |
Oleg Nesterov | f5a421a | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 1017 | * cancel_delayed_work_sync - reliably kill off a delayed work. |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 1018 | * @dwork: the delayed work struct |
| 1019 | * |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 1020 | * Returns true if @dwork was pending. |
| 1021 | * |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 1022 | * It is possible to use this function if @dwork rearms itself via queue_work() |
| 1023 | * or queue_delayed_work(). See also the comment for cancel_work_sync(). |
| 1024 | */ |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 1025 | int cancel_delayed_work_sync(struct delayed_work *dwork) |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 1026 | { |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 1027 | return __cancel_work_timer(&dwork->work, &dwork->timer); |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 1028 | } |
Oleg Nesterov | f5a421a | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 1029 | EXPORT_SYMBOL(cancel_delayed_work_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 1031 | static struct workqueue_struct *keventd_wq __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 1033 | /** |
| 1034 | * schedule_work - put work task in global workqueue |
| 1035 | * @work: job to be done |
| 1036 | * |
Bart Van Assche | 5b0f437d | 2009-07-30 19:00:53 +0200 | [diff] [blame] | 1037 | * Returns zero if @work was already on the kernel-global workqueue and |
| 1038 | * non-zero otherwise. |
| 1039 | * |
| 1040 | * This puts a job in the kernel-global workqueue if it was not already |
| 1041 | * queued and leaves it in the same position on the kernel-global |
| 1042 | * workqueue otherwise. |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 1043 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 1044 | int schedule_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | { |
| 1046 | return queue_work(keventd_wq, work); |
| 1047 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 1048 | EXPORT_SYMBOL(schedule_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | |
Zhang Rui | c1a220e | 2008-07-23 21:28:39 -0700 | [diff] [blame] | 1050 | /* |
| 1051 | * schedule_work_on - put work task on a specific cpu |
| 1052 | * @cpu: cpu to put the work task on |
| 1053 | * @work: job to be done |
| 1054 | * |
| 1055 | * This puts a job on a specific cpu |
| 1056 | */ |
| 1057 | int schedule_work_on(int cpu, struct work_struct *work) |
| 1058 | { |
| 1059 | return queue_work_on(cpu, keventd_wq, work); |
| 1060 | } |
| 1061 | EXPORT_SYMBOL(schedule_work_on); |
| 1062 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 1063 | /** |
| 1064 | * schedule_delayed_work - put work task in global workqueue after delay |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 1065 | * @dwork: job to be done |
| 1066 | * @delay: number of jiffies to wait or 0 for immediate execution |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 1067 | * |
| 1068 | * After waiting for a given time this puts a job in the kernel-global |
| 1069 | * workqueue. |
| 1070 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 1071 | int schedule_delayed_work(struct delayed_work *dwork, |
Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 1072 | unsigned long delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | { |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 1074 | return queue_delayed_work(keventd_wq, dwork, delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 1076 | EXPORT_SYMBOL(schedule_delayed_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 1078 | /** |
Linus Torvalds | 8c53e46 | 2009-10-14 09:16:42 -0700 | [diff] [blame] | 1079 | * flush_delayed_work - block until a dwork_struct's callback has terminated |
| 1080 | * @dwork: the delayed work which is to be flushed |
| 1081 | * |
| 1082 | * Any timeout is cancelled, and any pending work is run immediately. |
| 1083 | */ |
| 1084 | void flush_delayed_work(struct delayed_work *dwork) |
| 1085 | { |
| 1086 | if (del_timer_sync(&dwork->timer)) { |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1087 | __queue_work(get_cpu(), get_wq_data(&dwork->work)->wq, |
| 1088 | &dwork->work); |
Linus Torvalds | 8c53e46 | 2009-10-14 09:16:42 -0700 | [diff] [blame] | 1089 | put_cpu(); |
| 1090 | } |
| 1091 | flush_work(&dwork->work); |
| 1092 | } |
| 1093 | EXPORT_SYMBOL(flush_delayed_work); |
| 1094 | |
| 1095 | /** |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 1096 | * schedule_delayed_work_on - queue work in global workqueue on CPU after delay |
| 1097 | * @cpu: cpu to use |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 1098 | * @dwork: job to be done |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 1099 | * @delay: number of jiffies to wait |
| 1100 | * |
| 1101 | * After waiting for a given time this puts a job in the kernel-global |
| 1102 | * workqueue on the specified CPU. |
| 1103 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | int schedule_delayed_work_on(int cpu, |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 1105 | struct delayed_work *dwork, unsigned long delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | { |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 1107 | return queue_delayed_work_on(cpu, keventd_wq, dwork, delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 1109 | EXPORT_SYMBOL(schedule_delayed_work_on); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 1111 | /** |
| 1112 | * schedule_on_each_cpu - call a function on each online CPU from keventd |
| 1113 | * @func: the function to call |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 1114 | * |
| 1115 | * Returns zero on success. |
| 1116 | * Returns -ve errno on failure. |
| 1117 | * |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 1118 | * schedule_on_each_cpu() is very slow. |
| 1119 | */ |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 1120 | int schedule_on_each_cpu(work_func_t func) |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 1121 | { |
| 1122 | int cpu; |
Andi Kleen | 65a6446 | 2009-10-14 06:22:47 +0200 | [diff] [blame] | 1123 | int orig = -1; |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 1124 | struct work_struct *works; |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 1125 | |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 1126 | works = alloc_percpu(struct work_struct); |
| 1127 | if (!works) |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 1128 | return -ENOMEM; |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 1129 | |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 1130 | get_online_cpus(); |
Tejun Heo | 9398180 | 2009-11-17 14:06:20 -0800 | [diff] [blame] | 1131 | |
| 1132 | /* |
| 1133 | * When running in keventd don't schedule a work item on |
| 1134 | * itself. Can just call directly because the work queue is |
| 1135 | * already bound. This also is faster. |
| 1136 | */ |
| 1137 | if (current_is_keventd()) |
| 1138 | orig = raw_smp_processor_id(); |
| 1139 | |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 1140 | for_each_online_cpu(cpu) { |
Ingo Molnar | 9bfb183 | 2006-12-18 20:05:09 +0100 | [diff] [blame] | 1141 | struct work_struct *work = per_cpu_ptr(works, cpu); |
| 1142 | |
| 1143 | INIT_WORK(work, func); |
Andi Kleen | 65a6446 | 2009-10-14 06:22:47 +0200 | [diff] [blame] | 1144 | if (cpu != orig) |
Tejun Heo | 9398180 | 2009-11-17 14:06:20 -0800 | [diff] [blame] | 1145 | schedule_work_on(cpu, work); |
Andi Kleen | 65a6446 | 2009-10-14 06:22:47 +0200 | [diff] [blame] | 1146 | } |
Tejun Heo | 9398180 | 2009-11-17 14:06:20 -0800 | [diff] [blame] | 1147 | if (orig >= 0) |
| 1148 | func(per_cpu_ptr(works, orig)); |
| 1149 | |
| 1150 | for_each_online_cpu(cpu) |
| 1151 | flush_work(per_cpu_ptr(works, cpu)); |
| 1152 | |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 1153 | put_online_cpus(); |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 1154 | free_percpu(works); |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 1155 | return 0; |
| 1156 | } |
| 1157 | |
Alan Stern | eef6a7d | 2010-02-12 17:39:21 +0900 | [diff] [blame] | 1158 | /** |
| 1159 | * flush_scheduled_work - ensure that any scheduled work has run to completion. |
| 1160 | * |
| 1161 | * Forces execution of the kernel-global workqueue and blocks until its |
| 1162 | * completion. |
| 1163 | * |
| 1164 | * Think twice before calling this function! It's very easy to get into |
| 1165 | * trouble if you don't take great care. Either of the following situations |
| 1166 | * will lead to deadlock: |
| 1167 | * |
| 1168 | * One of the work items currently on the workqueue needs to acquire |
| 1169 | * a lock held by your code or its caller. |
| 1170 | * |
| 1171 | * Your code is running in the context of a work routine. |
| 1172 | * |
| 1173 | * They will be detected by lockdep when they occur, but the first might not |
| 1174 | * occur very often. It depends on what work items are on the workqueue and |
| 1175 | * what locks they need, which you have no control over. |
| 1176 | * |
| 1177 | * In most situations flushing the entire workqueue is overkill; you merely |
| 1178 | * need to know that a particular work item isn't queued and isn't running. |
| 1179 | * In such cases you should use cancel_delayed_work_sync() or |
| 1180 | * cancel_work_sync() instead. |
| 1181 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | void flush_scheduled_work(void) |
| 1183 | { |
| 1184 | flush_workqueue(keventd_wq); |
| 1185 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 1186 | EXPORT_SYMBOL(flush_scheduled_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | |
| 1188 | /** |
James Bottomley | 1fa44ec | 2006-02-23 12:43:43 -0600 | [diff] [blame] | 1189 | * execute_in_process_context - reliably execute the routine with user context |
| 1190 | * @fn: the function to execute |
James Bottomley | 1fa44ec | 2006-02-23 12:43:43 -0600 | [diff] [blame] | 1191 | * @ew: guaranteed storage for the execute work structure (must |
| 1192 | * be available when the work executes) |
| 1193 | * |
| 1194 | * Executes the function immediately if process context is available, |
| 1195 | * otherwise schedules the function for delayed execution. |
| 1196 | * |
| 1197 | * Returns: 0 - function was executed |
| 1198 | * 1 - function was scheduled for execution |
| 1199 | */ |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 1200 | 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] | 1201 | { |
| 1202 | if (!in_interrupt()) { |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 1203 | fn(&ew->work); |
James Bottomley | 1fa44ec | 2006-02-23 12:43:43 -0600 | [diff] [blame] | 1204 | return 0; |
| 1205 | } |
| 1206 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 1207 | INIT_WORK(&ew->work, fn); |
James Bottomley | 1fa44ec | 2006-02-23 12:43:43 -0600 | [diff] [blame] | 1208 | schedule_work(&ew->work); |
| 1209 | |
| 1210 | return 1; |
| 1211 | } |
| 1212 | EXPORT_SYMBOL_GPL(execute_in_process_context); |
| 1213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | int keventd_up(void) |
| 1215 | { |
| 1216 | return keventd_wq != NULL; |
| 1217 | } |
| 1218 | |
| 1219 | int current_is_keventd(void) |
| 1220 | { |
| 1221 | struct cpu_workqueue_struct *cwq; |
Hugh Dickins | d243769 | 2007-08-27 16:06:19 +0100 | [diff] [blame] | 1222 | int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | int ret = 0; |
| 1224 | |
| 1225 | BUG_ON(!keventd_wq); |
| 1226 | |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1227 | cwq = get_cwq(cpu, keventd_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | if (current == cwq->thread) |
| 1229 | ret = 1; |
| 1230 | |
| 1231 | return ret; |
| 1232 | |
| 1233 | } |
| 1234 | |
Tejun Heo | 0f90004 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1235 | static struct cpu_workqueue_struct *alloc_cwqs(void) |
| 1236 | { |
| 1237 | /* |
| 1238 | * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS. |
| 1239 | * Make sure that the alignment isn't lower than that of |
| 1240 | * unsigned long long. |
| 1241 | */ |
| 1242 | const size_t size = sizeof(struct cpu_workqueue_struct); |
| 1243 | const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS, |
| 1244 | __alignof__(unsigned long long)); |
| 1245 | struct cpu_workqueue_struct *cwqs; |
| 1246 | #ifndef CONFIG_SMP |
| 1247 | void *ptr; |
| 1248 | |
| 1249 | /* |
| 1250 | * On UP, percpu allocator doesn't honor alignment parameter |
| 1251 | * and simply uses arch-dependent default. Allocate enough |
| 1252 | * room to align cwq and put an extra pointer at the end |
| 1253 | * pointing back to the originally allocated pointer which |
| 1254 | * will be used for free. |
| 1255 | * |
| 1256 | * FIXME: This really belongs to UP percpu code. Update UP |
| 1257 | * percpu code to honor alignment and remove this ugliness. |
| 1258 | */ |
| 1259 | ptr = __alloc_percpu(size + align + sizeof(void *), 1); |
| 1260 | cwqs = PTR_ALIGN(ptr, align); |
| 1261 | *(void **)per_cpu_ptr(cwqs + 1, 0) = ptr; |
| 1262 | #else |
| 1263 | /* On SMP, percpu allocator can do it itself */ |
| 1264 | cwqs = __alloc_percpu(size, align); |
| 1265 | #endif |
| 1266 | /* just in case, make sure it's actually aligned */ |
| 1267 | BUG_ON(!IS_ALIGNED((unsigned long)cwqs, align)); |
| 1268 | return cwqs; |
| 1269 | } |
| 1270 | |
| 1271 | static void free_cwqs(struct cpu_workqueue_struct *cwqs) |
| 1272 | { |
| 1273 | #ifndef CONFIG_SMP |
| 1274 | /* on UP, the pointer to free is stored right after the cwq */ |
| 1275 | if (cwqs) |
| 1276 | free_percpu(*(void **)per_cpu_ptr(cwqs + 1, 0)); |
| 1277 | #else |
| 1278 | free_percpu(cwqs); |
| 1279 | #endif |
| 1280 | } |
| 1281 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1282 | static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | { |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1284 | struct workqueue_struct *wq = cwq->wq; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1285 | struct task_struct *p; |
| 1286 | |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1287 | p = kthread_create(worker_thread, cwq, "%s/%d", wq->name, cpu); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1288 | /* |
| 1289 | * Nobody can add the work_struct to this cwq, |
| 1290 | * if (caller is __create_workqueue) |
| 1291 | * nobody should see this wq |
| 1292 | * else // caller is CPU_UP_PREPARE |
| 1293 | * cpu is not on cpu_online_map |
| 1294 | * so we can abort safely. |
| 1295 | */ |
| 1296 | if (IS_ERR(p)) |
| 1297 | return PTR_ERR(p); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1298 | cwq->thread = p; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1299 | |
| 1300 | return 0; |
| 1301 | } |
| 1302 | |
Oleg Nesterov | 06ba38a | 2007-05-09 02:34:15 -0700 | [diff] [blame] | 1303 | static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu) |
| 1304 | { |
| 1305 | struct task_struct *p = cwq->thread; |
| 1306 | |
| 1307 | if (p != NULL) { |
| 1308 | if (cpu >= 0) |
| 1309 | kthread_bind(p, cpu); |
| 1310 | wake_up_process(p); |
| 1311 | } |
| 1312 | } |
| 1313 | |
Johannes Berg | 4e6045f | 2007-10-18 23:39:55 -0700 | [diff] [blame] | 1314 | struct workqueue_struct *__create_workqueue_key(const char *name, |
Tejun Heo | 97e37d7 | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1315 | unsigned int flags, |
Johannes Berg | eb13ba8 | 2008-01-16 09:51:58 +0100 | [diff] [blame] | 1316 | struct lock_class_key *key, |
| 1317 | const char *lock_name) |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1318 | { |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1319 | bool singlethread = flags & WQ_SINGLE_THREAD; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1320 | struct workqueue_struct *wq; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1321 | int err = 0, cpu; |
| 1322 | |
| 1323 | wq = kzalloc(sizeof(*wq), GFP_KERNEL); |
| 1324 | if (!wq) |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1325 | goto err; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1326 | |
Tejun Heo | 0f90004 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1327 | wq->cpu_wq = alloc_cwqs(); |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1328 | if (!wq->cpu_wq) |
| 1329 | goto err; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1330 | |
Tejun Heo | 97e37d7 | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1331 | wq->flags = flags; |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame^] | 1332 | mutex_init(&wq->flush_mutex); |
| 1333 | atomic_set(&wq->nr_cwqs_to_flush, 0); |
| 1334 | INIT_LIST_HEAD(&wq->flusher_queue); |
| 1335 | INIT_LIST_HEAD(&wq->flusher_overflow); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1336 | wq->name = name; |
Johannes Berg | eb13ba8 | 2008-01-16 09:51:58 +0100 | [diff] [blame] | 1337 | lockdep_init_map(&wq->lockdep_map, lock_name, key, 0); |
Oleg Nesterov | cce1a16 | 2007-05-09 02:34:13 -0700 | [diff] [blame] | 1338 | INIT_LIST_HEAD(&wq->list); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1339 | |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1340 | cpu_maps_update_begin(); |
| 1341 | /* |
| 1342 | * We must initialize cwqs for each possible cpu even if we |
| 1343 | * are going to call destroy_workqueue() finally. Otherwise |
| 1344 | * cpu_up() can hit the uninitialized cwq once we drop the |
| 1345 | * lock. |
| 1346 | */ |
| 1347 | for_each_possible_cpu(cpu) { |
| 1348 | struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); |
| 1349 | |
Tejun Heo | 0f90004 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1350 | BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK); |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1351 | cwq->wq = wq; |
| 1352 | cwq->cpu = cpu; |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame^] | 1353 | cwq->flush_color = -1; |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1354 | spin_lock_init(&cwq->lock); |
| 1355 | INIT_LIST_HEAD(&cwq->worklist); |
| 1356 | init_waitqueue_head(&cwq->more_work); |
| 1357 | |
| 1358 | if (err) |
| 1359 | continue; |
| 1360 | err = create_workqueue_thread(cwq, cpu); |
| 1361 | if (cpu_online(cpu) && !singlethread) |
Oleg Nesterov | 06ba38a | 2007-05-09 02:34:15 -0700 | [diff] [blame] | 1362 | start_workqueue_thread(cwq, cpu); |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1363 | else |
| 1364 | start_workqueue_thread(cwq, -1); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1367 | spin_lock(&workqueue_lock); |
| 1368 | list_add(&wq->list, &workqueues); |
| 1369 | spin_unlock(&workqueue_lock); |
| 1370 | |
| 1371 | cpu_maps_update_done(); |
| 1372 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1373 | if (err) { |
| 1374 | destroy_workqueue(wq); |
| 1375 | wq = NULL; |
| 1376 | } |
| 1377 | return wq; |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1378 | err: |
| 1379 | if (wq) { |
Tejun Heo | 0f90004 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1380 | free_cwqs(wq->cpu_wq); |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1381 | kfree(wq); |
| 1382 | } |
| 1383 | return NULL; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1384 | } |
Johannes Berg | 4e6045f | 2007-10-18 23:39:55 -0700 | [diff] [blame] | 1385 | EXPORT_SYMBOL_GPL(__create_workqueue_key); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1386 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1387 | /** |
| 1388 | * destroy_workqueue - safely terminate a workqueue |
| 1389 | * @wq: target workqueue |
| 1390 | * |
| 1391 | * Safely destroy a workqueue. All work currently pending will be done first. |
| 1392 | */ |
| 1393 | void destroy_workqueue(struct workqueue_struct *wq) |
| 1394 | { |
Oleg Nesterov | b1f4ec1 | 2007-05-09 02:34:12 -0700 | [diff] [blame] | 1395 | int cpu; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1396 | |
Oleg Nesterov | 3da1c84 | 2008-07-25 01:47:50 -0700 | [diff] [blame] | 1397 | cpu_maps_update_begin(); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 1398 | spin_lock(&workqueue_lock); |
Oleg Nesterov | b1f4ec1 | 2007-05-09 02:34:12 -0700 | [diff] [blame] | 1399 | list_del(&wq->list); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 1400 | spin_unlock(&workqueue_lock); |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1401 | cpu_maps_update_done(); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1402 | |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame^] | 1403 | flush_workqueue(wq); |
| 1404 | |
| 1405 | for_each_possible_cpu(cpu) { |
| 1406 | struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); |
| 1407 | int i; |
| 1408 | |
| 1409 | if (cwq->thread) { |
| 1410 | kthread_stop(cwq->thread); |
| 1411 | cwq->thread = NULL; |
| 1412 | } |
| 1413 | |
| 1414 | for (i = 0; i < WORK_NR_COLORS; i++) |
| 1415 | BUG_ON(cwq->nr_in_flight[i]); |
| 1416 | } |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1417 | |
Tejun Heo | 0f90004 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1418 | free_cwqs(wq->cpu_wq); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1419 | kfree(wq); |
| 1420 | } |
| 1421 | EXPORT_SYMBOL_GPL(destroy_workqueue); |
| 1422 | |
| 1423 | static int __devinit workqueue_cpu_callback(struct notifier_block *nfb, |
| 1424 | unsigned long action, |
| 1425 | void *hcpu) |
| 1426 | { |
| 1427 | unsigned int cpu = (unsigned long)hcpu; |
| 1428 | struct cpu_workqueue_struct *cwq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | struct workqueue_struct *wq; |
| 1430 | |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 1431 | action &= ~CPU_TASKS_FROZEN; |
| 1432 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1433 | list_for_each_entry(wq, &workqueues, list) { |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1434 | if (wq->flags & WQ_SINGLE_THREAD) |
| 1435 | continue; |
| 1436 | |
| 1437 | cwq = get_cwq(cpu, wq); |
Christoph Lameter | 89ada67 | 2005-10-30 15:01:59 -0800 | [diff] [blame] | 1438 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1439 | switch (action) { |
Oleg Nesterov | 3da1c84 | 2008-07-25 01:47:50 -0700 | [diff] [blame] | 1440 | case CPU_POST_DEAD: |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame^] | 1441 | flush_workqueue(wq); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1442 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | } |
| 1445 | |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1446 | return notifier_from_errno(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 1449 | #ifdef CONFIG_SMP |
Rusty Russell | 8ccad40 | 2009-01-16 15:31:15 -0800 | [diff] [blame] | 1450 | |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 1451 | struct work_for_cpu { |
Andrew Morton | 6b44003 | 2009-04-09 09:50:37 -0600 | [diff] [blame] | 1452 | struct completion completion; |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 1453 | long (*fn)(void *); |
| 1454 | void *arg; |
| 1455 | long ret; |
| 1456 | }; |
| 1457 | |
Andrew Morton | 6b44003 | 2009-04-09 09:50:37 -0600 | [diff] [blame] | 1458 | static int do_work_for_cpu(void *_wfc) |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 1459 | { |
Andrew Morton | 6b44003 | 2009-04-09 09:50:37 -0600 | [diff] [blame] | 1460 | struct work_for_cpu *wfc = _wfc; |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 1461 | wfc->ret = wfc->fn(wfc->arg); |
Andrew Morton | 6b44003 | 2009-04-09 09:50:37 -0600 | [diff] [blame] | 1462 | complete(&wfc->completion); |
| 1463 | return 0; |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | /** |
| 1467 | * work_on_cpu - run a function in user context on a particular cpu |
| 1468 | * @cpu: the cpu to run on |
| 1469 | * @fn: the function to run |
| 1470 | * @arg: the function arg |
| 1471 | * |
Rusty Russell | 31ad908 | 2009-01-16 15:31:15 -0800 | [diff] [blame] | 1472 | * This will return the value @fn returns. |
| 1473 | * It is up to the caller to ensure that the cpu doesn't go offline. |
Andrew Morton | 6b44003 | 2009-04-09 09:50:37 -0600 | [diff] [blame] | 1474 | * The caller must not hold any locks which would prevent @fn from completing. |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 1475 | */ |
| 1476 | long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg) |
| 1477 | { |
Andrew Morton | 6b44003 | 2009-04-09 09:50:37 -0600 | [diff] [blame] | 1478 | struct task_struct *sub_thread; |
| 1479 | struct work_for_cpu wfc = { |
| 1480 | .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion), |
| 1481 | .fn = fn, |
| 1482 | .arg = arg, |
| 1483 | }; |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 1484 | |
Andrew Morton | 6b44003 | 2009-04-09 09:50:37 -0600 | [diff] [blame] | 1485 | sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu"); |
| 1486 | if (IS_ERR(sub_thread)) |
| 1487 | return PTR_ERR(sub_thread); |
| 1488 | kthread_bind(sub_thread, cpu); |
| 1489 | wake_up_process(sub_thread); |
| 1490 | wait_for_completion(&wfc.completion); |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 1491 | return wfc.ret; |
| 1492 | } |
| 1493 | EXPORT_SYMBOL_GPL(work_on_cpu); |
| 1494 | #endif /* CONFIG_SMP */ |
| 1495 | |
Oleg Nesterov | c12920d | 2007-05-09 02:34:14 -0700 | [diff] [blame] | 1496 | void __init init_workqueues(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | { |
Rusty Russell | e7577c5 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 1498 | singlethread_cpu = cpumask_first(cpu_possible_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | hotcpu_notifier(workqueue_cpu_callback, 0); |
| 1500 | keventd_wq = create_workqueue("events"); |
| 1501 | BUG_ON(!keventd_wq); |
| 1502 | } |