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