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