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 | |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 38 | enum { |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 39 | /* global_cwq flags */ |
| 40 | GCWQ_FREEZING = 1 << 3, /* freeze in progress */ |
| 41 | |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 42 | /* worker flags */ |
| 43 | WORKER_STARTED = 1 << 0, /* started */ |
| 44 | WORKER_DIE = 1 << 1, /* die die die */ |
| 45 | WORKER_IDLE = 1 << 2, /* is idle */ |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 46 | WORKER_ROGUE = 1 << 4, /* not bound to any cpu */ |
| 47 | |
| 48 | /* gcwq->trustee_state */ |
| 49 | TRUSTEE_START = 0, /* start */ |
| 50 | TRUSTEE_IN_CHARGE = 1, /* trustee in charge of gcwq */ |
| 51 | TRUSTEE_BUTCHER = 2, /* butcher workers */ |
| 52 | TRUSTEE_RELEASE = 3, /* release workers */ |
| 53 | TRUSTEE_DONE = 4, /* trustee is done */ |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 54 | |
| 55 | BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */ |
| 56 | BUSY_WORKER_HASH_SIZE = 1 << BUSY_WORKER_HASH_ORDER, |
| 57 | BUSY_WORKER_HASH_MASK = BUSY_WORKER_HASH_SIZE - 1, |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 58 | |
| 59 | TRUSTEE_COOLDOWN = HZ / 10, /* for trustee draining */ |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 60 | }; |
| 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | /* |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 63 | * Structure fields follow one of the following exclusion rules. |
| 64 | * |
| 65 | * I: Set during initialization and read-only afterwards. |
| 66 | * |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 67 | * L: gcwq->lock protected. Access with gcwq->lock held. |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 68 | * |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 69 | * F: wq->flush_mutex protected. |
| 70 | * |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 71 | * W: workqueue_lock protected. |
| 72 | */ |
| 73 | |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 74 | struct global_cwq; |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 75 | struct cpu_workqueue_struct; |
| 76 | |
| 77 | struct worker { |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 78 | /* on idle list while idle, on busy hash table while busy */ |
| 79 | union { |
| 80 | struct list_head entry; /* L: while idle */ |
| 81 | struct hlist_node hentry; /* L: while busy */ |
| 82 | }; |
| 83 | |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 84 | struct work_struct *current_work; /* L: work being processed */ |
Tejun Heo | 8cca0ee | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 85 | struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */ |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 86 | struct list_head scheduled; /* L: scheduled works */ |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 87 | struct task_struct *task; /* I: worker task */ |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 88 | struct global_cwq *gcwq; /* I: the associated gcwq */ |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 89 | struct cpu_workqueue_struct *cwq; /* I: the associated cwq */ |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 90 | unsigned int flags; /* L: flags */ |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 91 | int id; /* I: worker id */ |
| 92 | }; |
| 93 | |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 94 | /* |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 95 | * Global per-cpu workqueue. |
| 96 | */ |
| 97 | struct global_cwq { |
| 98 | spinlock_t lock; /* the gcwq lock */ |
| 99 | unsigned int cpu; /* I: the associated cpu */ |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 100 | unsigned int flags; /* L: GCWQ_* flags */ |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 101 | |
| 102 | int nr_workers; /* L: total number of workers */ |
| 103 | int nr_idle; /* L: currently idle ones */ |
| 104 | |
| 105 | /* workers are chained either in the idle_list or busy_hash */ |
| 106 | struct list_head idle_list; /* L: list of idle workers */ |
| 107 | struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE]; |
| 108 | /* L: hash of busy workers */ |
| 109 | |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 110 | struct ida worker_ida; /* L: for worker IDs */ |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 111 | |
| 112 | struct task_struct *trustee; /* L: for gcwq shutdown */ |
| 113 | unsigned int trustee_state; /* L: trustee state */ |
| 114 | wait_queue_head_t trustee_wait; /* trustee wait */ |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 115 | } ____cacheline_aligned_in_smp; |
| 116 | |
| 117 | /* |
Tejun Heo | 502ca9d | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 118 | * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of |
Tejun Heo | 0f90004 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 119 | * work_struct->data are used for flags and thus cwqs need to be |
| 120 | * aligned at two's power of the number of flag bits. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | */ |
| 122 | struct cpu_workqueue_struct { |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 123 | struct global_cwq *gcwq; /* I: the associated gcwq */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | struct list_head worklist; |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 125 | struct worker *worker; |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 126 | struct workqueue_struct *wq; /* I: the owning workqueue */ |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 127 | int work_color; /* L: current color */ |
| 128 | int flush_color; /* L: flushing color */ |
| 129 | int nr_in_flight[WORK_NR_COLORS]; |
| 130 | /* L: nr of in_flight works */ |
Tejun Heo | 1e19ffc | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 131 | int nr_active; /* L: nr of active works */ |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 132 | int max_active; /* L: max active works */ |
Tejun Heo | 1e19ffc | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 133 | struct list_head delayed_works; /* L: delayed works */ |
Tejun Heo | 0f90004 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 134 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
| 136 | /* |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 137 | * Structure used to wait for workqueue flush. |
| 138 | */ |
| 139 | struct wq_flusher { |
| 140 | struct list_head list; /* F: list of flushers */ |
| 141 | int flush_color; /* F: flush color waiting for */ |
| 142 | struct completion done; /* flush completion */ |
| 143 | }; |
| 144 | |
| 145 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | * The externally visible workqueue abstraction is an array of |
| 147 | * per-CPU workqueues: |
| 148 | */ |
| 149 | struct workqueue_struct { |
Tejun Heo | 97e37d7 | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 150 | unsigned int flags; /* I: WQ_* flags */ |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 151 | struct cpu_workqueue_struct *cpu_wq; /* I: cwq's */ |
| 152 | struct list_head list; /* W: list of all workqueues */ |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 153 | |
| 154 | struct mutex flush_mutex; /* protects wq flushing */ |
| 155 | int work_color; /* F: current work color */ |
| 156 | int flush_color; /* F: current flush color */ |
| 157 | atomic_t nr_cwqs_to_flush; /* flush in progress */ |
| 158 | struct wq_flusher *first_flusher; /* F: first flusher */ |
| 159 | struct list_head flusher_queue; /* F: flush waiters */ |
| 160 | struct list_head flusher_overflow; /* F: flush overflow list */ |
| 161 | |
Tejun Heo | 502ca9d | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 162 | unsigned long single_cpu; /* cpu for single cpu wq */ |
| 163 | |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 164 | int saved_max_active; /* I: saved cwq max_active */ |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 165 | const char *name; /* I: workqueue name */ |
Johannes Berg | 4e6045f | 2007-10-18 23:39:55 -0700 | [diff] [blame] | 166 | #ifdef CONFIG_LOCKDEP |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 167 | struct lockdep_map lockdep_map; |
Johannes Berg | 4e6045f | 2007-10-18 23:39:55 -0700 | [diff] [blame] | 168 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | }; |
| 170 | |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 171 | #define for_each_busy_worker(worker, i, pos, gcwq) \ |
| 172 | for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \ |
| 173 | hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry) |
| 174 | |
Thomas Gleixner | dc186ad | 2009-11-16 01:09:48 +0900 | [diff] [blame] | 175 | #ifdef CONFIG_DEBUG_OBJECTS_WORK |
| 176 | |
| 177 | static struct debug_obj_descr work_debug_descr; |
| 178 | |
| 179 | /* |
| 180 | * fixup_init is called when: |
| 181 | * - an active object is initialized |
| 182 | */ |
| 183 | static int work_fixup_init(void *addr, enum debug_obj_state state) |
| 184 | { |
| 185 | struct work_struct *work = addr; |
| 186 | |
| 187 | switch (state) { |
| 188 | case ODEBUG_STATE_ACTIVE: |
| 189 | cancel_work_sync(work); |
| 190 | debug_object_init(work, &work_debug_descr); |
| 191 | return 1; |
| 192 | default: |
| 193 | return 0; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * fixup_activate is called when: |
| 199 | * - an active object is activated |
| 200 | * - an unknown object is activated (might be a statically initialized object) |
| 201 | */ |
| 202 | static int work_fixup_activate(void *addr, enum debug_obj_state state) |
| 203 | { |
| 204 | struct work_struct *work = addr; |
| 205 | |
| 206 | switch (state) { |
| 207 | |
| 208 | case ODEBUG_STATE_NOTAVAILABLE: |
| 209 | /* |
| 210 | * This is not really a fixup. The work struct was |
| 211 | * statically initialized. We just make sure that it |
| 212 | * is tracked in the object tracker. |
| 213 | */ |
Tejun Heo | 22df02b | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 214 | if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) { |
Thomas Gleixner | dc186ad | 2009-11-16 01:09:48 +0900 | [diff] [blame] | 215 | debug_object_init(work, &work_debug_descr); |
| 216 | debug_object_activate(work, &work_debug_descr); |
| 217 | return 0; |
| 218 | } |
| 219 | WARN_ON_ONCE(1); |
| 220 | return 0; |
| 221 | |
| 222 | case ODEBUG_STATE_ACTIVE: |
| 223 | WARN_ON(1); |
| 224 | |
| 225 | default: |
| 226 | return 0; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | * fixup_free is called when: |
| 232 | * - an active object is freed |
| 233 | */ |
| 234 | static int work_fixup_free(void *addr, enum debug_obj_state state) |
| 235 | { |
| 236 | struct work_struct *work = addr; |
| 237 | |
| 238 | switch (state) { |
| 239 | case ODEBUG_STATE_ACTIVE: |
| 240 | cancel_work_sync(work); |
| 241 | debug_object_free(work, &work_debug_descr); |
| 242 | return 1; |
| 243 | default: |
| 244 | return 0; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | static struct debug_obj_descr work_debug_descr = { |
| 249 | .name = "work_struct", |
| 250 | .fixup_init = work_fixup_init, |
| 251 | .fixup_activate = work_fixup_activate, |
| 252 | .fixup_free = work_fixup_free, |
| 253 | }; |
| 254 | |
| 255 | static inline void debug_work_activate(struct work_struct *work) |
| 256 | { |
| 257 | debug_object_activate(work, &work_debug_descr); |
| 258 | } |
| 259 | |
| 260 | static inline void debug_work_deactivate(struct work_struct *work) |
| 261 | { |
| 262 | debug_object_deactivate(work, &work_debug_descr); |
| 263 | } |
| 264 | |
| 265 | void __init_work(struct work_struct *work, int onstack) |
| 266 | { |
| 267 | if (onstack) |
| 268 | debug_object_init_on_stack(work, &work_debug_descr); |
| 269 | else |
| 270 | debug_object_init(work, &work_debug_descr); |
| 271 | } |
| 272 | EXPORT_SYMBOL_GPL(__init_work); |
| 273 | |
| 274 | void destroy_work_on_stack(struct work_struct *work) |
| 275 | { |
| 276 | debug_object_free(work, &work_debug_descr); |
| 277 | } |
| 278 | EXPORT_SYMBOL_GPL(destroy_work_on_stack); |
| 279 | |
| 280 | #else |
| 281 | static inline void debug_work_activate(struct work_struct *work) { } |
| 282 | static inline void debug_work_deactivate(struct work_struct *work) { } |
| 283 | #endif |
| 284 | |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 285 | /* Serializes the accesses to the list of workqueues. */ |
| 286 | static DEFINE_SPINLOCK(workqueue_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | static LIST_HEAD(workqueues); |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 288 | static bool workqueue_freezing; /* W: have wqs started freezing? */ |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 289 | |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 290 | static DEFINE_PER_CPU(struct global_cwq, global_cwq); |
| 291 | |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 292 | static int worker_thread(void *__worker); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 294 | static struct global_cwq *get_gcwq(unsigned int cpu) |
| 295 | { |
| 296 | return &per_cpu(global_cwq, cpu); |
| 297 | } |
| 298 | |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 299 | static struct cpu_workqueue_struct *get_cwq(unsigned int cpu, |
| 300 | struct workqueue_struct *wq) |
Oleg Nesterov | a848e3b | 2007-05-09 02:34:17 -0700 | [diff] [blame] | 301 | { |
Oleg Nesterov | a848e3b | 2007-05-09 02:34:17 -0700 | [diff] [blame] | 302 | return per_cpu_ptr(wq->cpu_wq, cpu); |
| 303 | } |
| 304 | |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 305 | static unsigned int work_color_to_flags(int color) |
| 306 | { |
| 307 | return color << WORK_STRUCT_COLOR_SHIFT; |
| 308 | } |
| 309 | |
| 310 | static int get_work_color(struct work_struct *work) |
| 311 | { |
| 312 | return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) & |
| 313 | ((1 << WORK_STRUCT_COLOR_BITS) - 1); |
| 314 | } |
| 315 | |
| 316 | static int work_next_color(int color) |
| 317 | { |
| 318 | return (color + 1) % WORK_NR_COLORS; |
| 319 | } |
| 320 | |
David Howells | 4594bf1 | 2006-12-07 11:33:26 +0000 | [diff] [blame] | 321 | /* |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame^] | 322 | * Work data points to the cwq while a work is on queue. Once |
| 323 | * execution starts, it points to the cpu the work was last on. This |
| 324 | * can be distinguished by comparing the data value against |
| 325 | * PAGE_OFFSET. |
| 326 | * |
| 327 | * set_work_{cwq|cpu}() and clear_work_data() can be used to set the |
| 328 | * cwq, cpu or clear work->data. These functions should only be |
| 329 | * called while the work is owned - ie. while the PENDING bit is set. |
| 330 | * |
| 331 | * get_work_[g]cwq() can be used to obtain the gcwq or cwq |
| 332 | * corresponding to a work. gcwq is available once the work has been |
| 333 | * queued anywhere after initialization. cwq is available only from |
| 334 | * queueing until execution starts. |
David Howells | 4594bf1 | 2006-12-07 11:33:26 +0000 | [diff] [blame] | 335 | */ |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame^] | 336 | static inline void set_work_data(struct work_struct *work, unsigned long data, |
| 337 | unsigned long flags) |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 338 | { |
David Howells | 4594bf1 | 2006-12-07 11:33:26 +0000 | [diff] [blame] | 339 | BUG_ON(!work_pending(work)); |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame^] | 340 | atomic_long_set(&work->data, data | flags | work_static(work)); |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame^] | 343 | static void set_work_cwq(struct work_struct *work, |
| 344 | struct cpu_workqueue_struct *cwq, |
| 345 | unsigned long extra_flags) |
Oleg Nesterov | 4d707b9 | 2010-04-23 17:40:40 +0200 | [diff] [blame] | 346 | { |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame^] | 347 | set_work_data(work, (unsigned long)cwq, |
| 348 | WORK_STRUCT_PENDING | extra_flags); |
Oleg Nesterov | 4d707b9 | 2010-04-23 17:40:40 +0200 | [diff] [blame] | 349 | } |
| 350 | |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame^] | 351 | static void set_work_cpu(struct work_struct *work, unsigned int cpu) |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 352 | { |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame^] | 353 | set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING); |
| 354 | } |
| 355 | |
| 356 | static void clear_work_data(struct work_struct *work) |
| 357 | { |
| 358 | set_work_data(work, WORK_STRUCT_NO_CPU, 0); |
| 359 | } |
| 360 | |
| 361 | static inline unsigned long get_work_data(struct work_struct *work) |
| 362 | { |
| 363 | return atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK; |
| 364 | } |
| 365 | |
| 366 | static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work) |
| 367 | { |
| 368 | unsigned long data = get_work_data(work); |
| 369 | |
| 370 | return data >= PAGE_OFFSET ? (void *)data : NULL; |
| 371 | } |
| 372 | |
| 373 | static struct global_cwq *get_work_gcwq(struct work_struct *work) |
| 374 | { |
| 375 | unsigned long data = get_work_data(work); |
| 376 | unsigned int cpu; |
| 377 | |
| 378 | if (data >= PAGE_OFFSET) |
| 379 | return ((struct cpu_workqueue_struct *)data)->gcwq; |
| 380 | |
| 381 | cpu = data >> WORK_STRUCT_FLAG_BITS; |
| 382 | if (cpu == NR_CPUS) |
| 383 | return NULL; |
| 384 | |
| 385 | BUG_ON(cpu >= num_possible_cpus()); |
| 386 | return get_gcwq(cpu); |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 389 | /** |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 390 | * busy_worker_head - return the busy hash head for a work |
| 391 | * @gcwq: gcwq of interest |
| 392 | * @work: work to be hashed |
| 393 | * |
| 394 | * Return hash head of @gcwq for @work. |
| 395 | * |
| 396 | * CONTEXT: |
| 397 | * spin_lock_irq(gcwq->lock). |
| 398 | * |
| 399 | * RETURNS: |
| 400 | * Pointer to the hash head. |
| 401 | */ |
| 402 | static struct hlist_head *busy_worker_head(struct global_cwq *gcwq, |
| 403 | struct work_struct *work) |
| 404 | { |
| 405 | const int base_shift = ilog2(sizeof(struct work_struct)); |
| 406 | unsigned long v = (unsigned long)work; |
| 407 | |
| 408 | /* simple shift and fold hash, do we need something better? */ |
| 409 | v >>= base_shift; |
| 410 | v += v >> BUSY_WORKER_HASH_ORDER; |
| 411 | v &= BUSY_WORKER_HASH_MASK; |
| 412 | |
| 413 | return &gcwq->busy_hash[v]; |
| 414 | } |
| 415 | |
| 416 | /** |
Tejun Heo | 8cca0ee | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 417 | * __find_worker_executing_work - find worker which is executing a work |
| 418 | * @gcwq: gcwq of interest |
| 419 | * @bwh: hash head as returned by busy_worker_head() |
| 420 | * @work: work to find worker for |
| 421 | * |
| 422 | * Find a worker which is executing @work on @gcwq. @bwh should be |
| 423 | * the hash head obtained by calling busy_worker_head() with the same |
| 424 | * work. |
| 425 | * |
| 426 | * CONTEXT: |
| 427 | * spin_lock_irq(gcwq->lock). |
| 428 | * |
| 429 | * RETURNS: |
| 430 | * Pointer to worker which is executing @work if found, NULL |
| 431 | * otherwise. |
| 432 | */ |
| 433 | static struct worker *__find_worker_executing_work(struct global_cwq *gcwq, |
| 434 | struct hlist_head *bwh, |
| 435 | struct work_struct *work) |
| 436 | { |
| 437 | struct worker *worker; |
| 438 | struct hlist_node *tmp; |
| 439 | |
| 440 | hlist_for_each_entry(worker, tmp, bwh, hentry) |
| 441 | if (worker->current_work == work) |
| 442 | return worker; |
| 443 | return NULL; |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * find_worker_executing_work - find worker which is executing a work |
| 448 | * @gcwq: gcwq of interest |
| 449 | * @work: work to find worker for |
| 450 | * |
| 451 | * Find a worker which is executing @work on @gcwq. This function is |
| 452 | * identical to __find_worker_executing_work() except that this |
| 453 | * function calculates @bwh itself. |
| 454 | * |
| 455 | * CONTEXT: |
| 456 | * spin_lock_irq(gcwq->lock). |
| 457 | * |
| 458 | * RETURNS: |
| 459 | * Pointer to worker which is executing @work if found, NULL |
| 460 | * otherwise. |
| 461 | */ |
| 462 | static struct worker *find_worker_executing_work(struct global_cwq *gcwq, |
| 463 | struct work_struct *work) |
| 464 | { |
| 465 | return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work), |
| 466 | work); |
| 467 | } |
| 468 | |
| 469 | /** |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 470 | * insert_work - insert a work into cwq |
| 471 | * @cwq: cwq @work belongs to |
| 472 | * @work: work to insert |
| 473 | * @head: insertion point |
| 474 | * @extra_flags: extra WORK_STRUCT_* flags to set |
| 475 | * |
| 476 | * Insert @work into @cwq after @head. |
| 477 | * |
| 478 | * CONTEXT: |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 479 | * spin_lock_irq(gcwq->lock). |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 480 | */ |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 481 | static void insert_work(struct cpu_workqueue_struct *cwq, |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 482 | struct work_struct *work, struct list_head *head, |
| 483 | unsigned int extra_flags) |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 484 | { |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 485 | /* we own @work, set data and link */ |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame^] | 486 | set_work_cwq(work, cwq, extra_flags); |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 487 | |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 488 | /* |
| 489 | * Ensure that we get the right work->data if we see the |
| 490 | * result of list_add() below, see try_to_grab_pending(). |
| 491 | */ |
| 492 | smp_wmb(); |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 493 | |
Oleg Nesterov | 1a4d9b0 | 2008-07-25 01:47:47 -0700 | [diff] [blame] | 494 | list_add_tail(&work->entry, head); |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 495 | wake_up_process(cwq->worker->task); |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Tejun Heo | 502ca9d | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 498 | /** |
| 499 | * cwq_unbind_single_cpu - unbind cwq from single cpu workqueue processing |
| 500 | * @cwq: cwq to unbind |
| 501 | * |
| 502 | * Try to unbind @cwq from single cpu workqueue processing. If |
| 503 | * @cwq->wq is frozen, unbind is delayed till the workqueue is thawed. |
| 504 | * |
| 505 | * CONTEXT: |
| 506 | * spin_lock_irq(gcwq->lock). |
| 507 | */ |
| 508 | static void cwq_unbind_single_cpu(struct cpu_workqueue_struct *cwq) |
| 509 | { |
| 510 | struct workqueue_struct *wq = cwq->wq; |
| 511 | struct global_cwq *gcwq = cwq->gcwq; |
| 512 | |
| 513 | BUG_ON(wq->single_cpu != gcwq->cpu); |
| 514 | /* |
| 515 | * Unbind from workqueue if @cwq is not frozen. If frozen, |
| 516 | * thaw_workqueues() will either restart processing on this |
| 517 | * cpu or unbind if empty. This keeps works queued while |
| 518 | * frozen fully ordered and flushable. |
| 519 | */ |
| 520 | if (likely(!(gcwq->flags & GCWQ_FREEZING))) { |
| 521 | smp_wmb(); /* paired with cmpxchg() in __queue_work() */ |
| 522 | wq->single_cpu = NR_CPUS; |
| 523 | } |
| 524 | } |
| 525 | |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 526 | static void __queue_work(unsigned int cpu, struct workqueue_struct *wq, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | struct work_struct *work) |
| 528 | { |
Tejun Heo | 502ca9d | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 529 | struct global_cwq *gcwq; |
| 530 | struct cpu_workqueue_struct *cwq; |
Tejun Heo | 1e19ffc | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 531 | struct list_head *worklist; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | unsigned long flags; |
Tejun Heo | 502ca9d | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 533 | bool arbitrate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | |
Thomas Gleixner | dc186ad | 2009-11-16 01:09:48 +0900 | [diff] [blame] | 535 | debug_work_activate(work); |
Tejun Heo | 1e19ffc | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 536 | |
Tejun Heo | 502ca9d | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 537 | /* determine gcwq to use */ |
| 538 | if (!(wq->flags & WQ_SINGLE_CPU)) { |
| 539 | /* just use the requested cpu for multicpu workqueues */ |
| 540 | gcwq = get_gcwq(cpu); |
| 541 | spin_lock_irqsave(&gcwq->lock, flags); |
| 542 | } else { |
| 543 | unsigned int req_cpu = cpu; |
| 544 | |
| 545 | /* |
| 546 | * It's a bit more complex for single cpu workqueues. |
| 547 | * We first need to determine which cpu is going to be |
| 548 | * used. If no cpu is currently serving this |
| 549 | * workqueue, arbitrate using atomic accesses to |
| 550 | * wq->single_cpu; otherwise, use the current one. |
| 551 | */ |
| 552 | retry: |
| 553 | cpu = wq->single_cpu; |
| 554 | arbitrate = cpu == NR_CPUS; |
| 555 | if (arbitrate) |
| 556 | cpu = req_cpu; |
| 557 | |
| 558 | gcwq = get_gcwq(cpu); |
| 559 | spin_lock_irqsave(&gcwq->lock, flags); |
| 560 | |
| 561 | /* |
| 562 | * The following cmpxchg() is a full barrier paired |
| 563 | * with smp_wmb() in cwq_unbind_single_cpu() and |
| 564 | * guarantees that all changes to wq->st_* fields are |
| 565 | * visible on the new cpu after this point. |
| 566 | */ |
| 567 | if (arbitrate) |
| 568 | cmpxchg(&wq->single_cpu, NR_CPUS, cpu); |
| 569 | |
| 570 | if (unlikely(wq->single_cpu != cpu)) { |
| 571 | spin_unlock_irqrestore(&gcwq->lock, flags); |
| 572 | goto retry; |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | /* gcwq determined, get cwq and queue */ |
| 577 | cwq = get_cwq(gcwq->cpu, wq); |
| 578 | |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 579 | BUG_ON(!list_empty(&work->entry)); |
Tejun Heo | 1e19ffc | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 580 | |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 581 | cwq->nr_in_flight[cwq->work_color]++; |
Tejun Heo | 1e19ffc | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 582 | |
| 583 | if (likely(cwq->nr_active < cwq->max_active)) { |
| 584 | cwq->nr_active++; |
| 585 | worklist = &cwq->worklist; |
| 586 | } else |
| 587 | worklist = &cwq->delayed_works; |
| 588 | |
| 589 | insert_work(cwq, work, worklist, work_color_to_flags(cwq->work_color)); |
| 590 | |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 591 | spin_unlock_irqrestore(&gcwq->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 594 | /** |
| 595 | * queue_work - queue work on a workqueue |
| 596 | * @wq: workqueue to use |
| 597 | * @work: work to queue |
| 598 | * |
Alan Stern | 057647f | 2006-10-28 10:38:58 -0700 | [diff] [blame] | 599 | * Returns 0 if @work was already on a queue, non-zero otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | * |
Oleg Nesterov | 00dfcaf | 2008-04-29 01:00:27 -0700 | [diff] [blame] | 601 | * We queue the work to the CPU on which it was submitted, but if the CPU dies |
| 602 | * it can be processed by another CPU. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 604 | int queue_work(struct workqueue_struct *wq, struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | { |
Oleg Nesterov | ef1ca23 | 2008-07-25 01:47:53 -0700 | [diff] [blame] | 606 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | |
Oleg Nesterov | ef1ca23 | 2008-07-25 01:47:53 -0700 | [diff] [blame] | 608 | ret = queue_work_on(get_cpu(), wq, work); |
| 609 | put_cpu(); |
| 610 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | return ret; |
| 612 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 613 | EXPORT_SYMBOL_GPL(queue_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | |
Zhang Rui | c1a220e | 2008-07-23 21:28:39 -0700 | [diff] [blame] | 615 | /** |
| 616 | * queue_work_on - queue work on specific cpu |
| 617 | * @cpu: CPU number to execute work on |
| 618 | * @wq: workqueue to use |
| 619 | * @work: work to queue |
| 620 | * |
| 621 | * Returns 0 if @work was already on a queue, non-zero otherwise. |
| 622 | * |
| 623 | * We queue the work to a specific CPU, the caller must ensure it |
| 624 | * can't go away. |
| 625 | */ |
| 626 | int |
| 627 | queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work) |
| 628 | { |
| 629 | int ret = 0; |
| 630 | |
Tejun Heo | 22df02b | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 631 | 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] | 632 | __queue_work(cpu, wq, work); |
Zhang Rui | c1a220e | 2008-07-23 21:28:39 -0700 | [diff] [blame] | 633 | ret = 1; |
| 634 | } |
| 635 | return ret; |
| 636 | } |
| 637 | EXPORT_SYMBOL_GPL(queue_work_on); |
| 638 | |
Li Zefan | 6d141c3 | 2008-02-08 04:21:09 -0800 | [diff] [blame] | 639 | static void delayed_work_timer_fn(unsigned long __data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | { |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 641 | struct delayed_work *dwork = (struct delayed_work *)__data; |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame^] | 642 | struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 644 | __queue_work(smp_processor_id(), cwq->wq, &dwork->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 647 | /** |
| 648 | * queue_delayed_work - queue work on a workqueue after delay |
| 649 | * @wq: workqueue to use |
Randy Dunlap | af9997e | 2006-12-22 01:06:52 -0800 | [diff] [blame] | 650 | * @dwork: delayable work to queue |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 651 | * @delay: number of jiffies to wait before queueing |
| 652 | * |
Alan Stern | 057647f | 2006-10-28 10:38:58 -0700 | [diff] [blame] | 653 | * 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] | 654 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 655 | int queue_delayed_work(struct workqueue_struct *wq, |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 656 | struct delayed_work *dwork, unsigned long delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | { |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 658 | if (delay == 0) |
Oleg Nesterov | 63bc036 | 2007-05-09 02:34:16 -0700 | [diff] [blame] | 659 | return queue_work(wq, &dwork->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | |
Oleg Nesterov | 63bc036 | 2007-05-09 02:34:16 -0700 | [diff] [blame] | 661 | return queue_delayed_work_on(-1, wq, dwork, delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 663 | EXPORT_SYMBOL_GPL(queue_delayed_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 665 | /** |
| 666 | * queue_delayed_work_on - queue work on specific CPU after delay |
| 667 | * @cpu: CPU number to execute work on |
| 668 | * @wq: workqueue to use |
Randy Dunlap | af9997e | 2006-12-22 01:06:52 -0800 | [diff] [blame] | 669 | * @dwork: work to queue |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 670 | * @delay: number of jiffies to wait before queueing |
| 671 | * |
Alan Stern | 057647f | 2006-10-28 10:38:58 -0700 | [diff] [blame] | 672 | * 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] | 673 | */ |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 674 | int queue_delayed_work_on(int cpu, struct workqueue_struct *wq, |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 675 | struct delayed_work *dwork, unsigned long delay) |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 676 | { |
| 677 | int ret = 0; |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 678 | struct timer_list *timer = &dwork->timer; |
| 679 | struct work_struct *work = &dwork->work; |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 680 | |
Tejun Heo | 22df02b | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 681 | if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame^] | 682 | struct global_cwq *gcwq = get_work_gcwq(work); |
| 683 | unsigned int lcpu = gcwq ? gcwq->cpu : raw_smp_processor_id(); |
| 684 | |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 685 | BUG_ON(timer_pending(timer)); |
| 686 | BUG_ON(!list_empty(&work->entry)); |
| 687 | |
Andrew Liu | 8a3e77c | 2008-05-01 04:35:14 -0700 | [diff] [blame] | 688 | timer_stats_timer_set_start_info(&dwork->timer); |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame^] | 689 | /* |
| 690 | * This stores cwq for the moment, for the timer_fn. |
| 691 | * Note that the work's gcwq is preserved to allow |
| 692 | * reentrance detection for delayed works. |
| 693 | */ |
| 694 | set_work_cwq(work, get_cwq(lcpu, wq), 0); |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 695 | timer->expires = jiffies + delay; |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 696 | timer->data = (unsigned long)dwork; |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 697 | timer->function = delayed_work_timer_fn; |
Oleg Nesterov | 63bc036 | 2007-05-09 02:34:16 -0700 | [diff] [blame] | 698 | |
| 699 | if (unlikely(cpu >= 0)) |
| 700 | add_timer_on(timer, cpu); |
| 701 | else |
| 702 | add_timer(timer); |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 703 | ret = 1; |
| 704 | } |
| 705 | return ret; |
| 706 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 707 | EXPORT_SYMBOL_GPL(queue_delayed_work_on); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 709 | /** |
| 710 | * worker_enter_idle - enter idle state |
| 711 | * @worker: worker which is entering idle state |
| 712 | * |
| 713 | * @worker is entering idle state. Update stats and idle timer if |
| 714 | * necessary. |
| 715 | * |
| 716 | * LOCKING: |
| 717 | * spin_lock_irq(gcwq->lock). |
| 718 | */ |
| 719 | static void worker_enter_idle(struct worker *worker) |
| 720 | { |
| 721 | struct global_cwq *gcwq = worker->gcwq; |
| 722 | |
| 723 | BUG_ON(worker->flags & WORKER_IDLE); |
| 724 | BUG_ON(!list_empty(&worker->entry) && |
| 725 | (worker->hentry.next || worker->hentry.pprev)); |
| 726 | |
| 727 | worker->flags |= WORKER_IDLE; |
| 728 | gcwq->nr_idle++; |
| 729 | |
| 730 | /* idle_list is LIFO */ |
| 731 | list_add(&worker->entry, &gcwq->idle_list); |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 732 | |
| 733 | if (unlikely(worker->flags & WORKER_ROGUE)) |
| 734 | wake_up_all(&gcwq->trustee_wait); |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | /** |
| 738 | * worker_leave_idle - leave idle state |
| 739 | * @worker: worker which is leaving idle state |
| 740 | * |
| 741 | * @worker is leaving idle state. Update stats. |
| 742 | * |
| 743 | * LOCKING: |
| 744 | * spin_lock_irq(gcwq->lock). |
| 745 | */ |
| 746 | static void worker_leave_idle(struct worker *worker) |
| 747 | { |
| 748 | struct global_cwq *gcwq = worker->gcwq; |
| 749 | |
| 750 | BUG_ON(!(worker->flags & WORKER_IDLE)); |
| 751 | worker->flags &= ~WORKER_IDLE; |
| 752 | gcwq->nr_idle--; |
| 753 | list_del_init(&worker->entry); |
| 754 | } |
| 755 | |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 756 | static struct worker *alloc_worker(void) |
| 757 | { |
| 758 | struct worker *worker; |
| 759 | |
| 760 | worker = kzalloc(sizeof(*worker), GFP_KERNEL); |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 761 | if (worker) { |
| 762 | INIT_LIST_HEAD(&worker->entry); |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 763 | INIT_LIST_HEAD(&worker->scheduled); |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 764 | } |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 765 | return worker; |
| 766 | } |
| 767 | |
| 768 | /** |
| 769 | * create_worker - create a new workqueue worker |
| 770 | * @cwq: cwq the new worker will belong to |
| 771 | * @bind: whether to set affinity to @cpu or not |
| 772 | * |
| 773 | * Create a new worker which is bound to @cwq. The returned worker |
| 774 | * can be started by calling start_worker() or destroyed using |
| 775 | * destroy_worker(). |
| 776 | * |
| 777 | * CONTEXT: |
| 778 | * Might sleep. Does GFP_KERNEL allocations. |
| 779 | * |
| 780 | * RETURNS: |
| 781 | * Pointer to the newly created worker. |
| 782 | */ |
| 783 | static struct worker *create_worker(struct cpu_workqueue_struct *cwq, bool bind) |
| 784 | { |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 785 | struct global_cwq *gcwq = cwq->gcwq; |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 786 | int id = -1; |
| 787 | struct worker *worker = NULL; |
| 788 | |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 789 | spin_lock_irq(&gcwq->lock); |
| 790 | while (ida_get_new(&gcwq->worker_ida, &id)) { |
| 791 | spin_unlock_irq(&gcwq->lock); |
| 792 | if (!ida_pre_get(&gcwq->worker_ida, GFP_KERNEL)) |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 793 | goto fail; |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 794 | spin_lock_irq(&gcwq->lock); |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 795 | } |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 796 | spin_unlock_irq(&gcwq->lock); |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 797 | |
| 798 | worker = alloc_worker(); |
| 799 | if (!worker) |
| 800 | goto fail; |
| 801 | |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 802 | worker->gcwq = gcwq; |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 803 | worker->cwq = cwq; |
| 804 | worker->id = id; |
| 805 | |
| 806 | worker->task = kthread_create(worker_thread, worker, "kworker/%u:%d", |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 807 | gcwq->cpu, id); |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 808 | if (IS_ERR(worker->task)) |
| 809 | goto fail; |
| 810 | |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 811 | /* |
| 812 | * A rogue worker will become a regular one if CPU comes |
| 813 | * online later on. Make sure every worker has |
| 814 | * PF_THREAD_BOUND set. |
| 815 | */ |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 816 | if (bind) |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 817 | kthread_bind(worker->task, gcwq->cpu); |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 818 | else |
| 819 | worker->task->flags |= PF_THREAD_BOUND; |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 820 | |
| 821 | return worker; |
| 822 | fail: |
| 823 | if (id >= 0) { |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 824 | spin_lock_irq(&gcwq->lock); |
| 825 | ida_remove(&gcwq->worker_ida, id); |
| 826 | spin_unlock_irq(&gcwq->lock); |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 827 | } |
| 828 | kfree(worker); |
| 829 | return NULL; |
| 830 | } |
| 831 | |
| 832 | /** |
| 833 | * start_worker - start a newly created worker |
| 834 | * @worker: worker to start |
| 835 | * |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 836 | * Make the gcwq aware of @worker and start it. |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 837 | * |
| 838 | * CONTEXT: |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 839 | * spin_lock_irq(gcwq->lock). |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 840 | */ |
| 841 | static void start_worker(struct worker *worker) |
| 842 | { |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 843 | worker->flags |= WORKER_STARTED; |
| 844 | worker->gcwq->nr_workers++; |
| 845 | worker_enter_idle(worker); |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 846 | wake_up_process(worker->task); |
| 847 | } |
| 848 | |
| 849 | /** |
| 850 | * destroy_worker - destroy a workqueue worker |
| 851 | * @worker: worker to be destroyed |
| 852 | * |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 853 | * Destroy @worker and adjust @gcwq stats accordingly. |
| 854 | * |
| 855 | * CONTEXT: |
| 856 | * spin_lock_irq(gcwq->lock) which is released and regrabbed. |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 857 | */ |
| 858 | static void destroy_worker(struct worker *worker) |
| 859 | { |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 860 | struct global_cwq *gcwq = worker->gcwq; |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 861 | int id = worker->id; |
| 862 | |
| 863 | /* sanity check frenzy */ |
| 864 | BUG_ON(worker->current_work); |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 865 | BUG_ON(!list_empty(&worker->scheduled)); |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 866 | |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 867 | if (worker->flags & WORKER_STARTED) |
| 868 | gcwq->nr_workers--; |
| 869 | if (worker->flags & WORKER_IDLE) |
| 870 | gcwq->nr_idle--; |
| 871 | |
| 872 | list_del_init(&worker->entry); |
| 873 | worker->flags |= WORKER_DIE; |
| 874 | |
| 875 | spin_unlock_irq(&gcwq->lock); |
| 876 | |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 877 | kthread_stop(worker->task); |
| 878 | kfree(worker); |
| 879 | |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 880 | spin_lock_irq(&gcwq->lock); |
| 881 | ida_remove(&gcwq->worker_ida, id); |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 882 | } |
| 883 | |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 884 | /** |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 885 | * move_linked_works - move linked works to a list |
| 886 | * @work: start of series of works to be scheduled |
| 887 | * @head: target list to append @work to |
| 888 | * @nextp: out paramter for nested worklist walking |
| 889 | * |
| 890 | * Schedule linked works starting from @work to @head. Work series to |
| 891 | * be scheduled starts at @work and includes any consecutive work with |
| 892 | * WORK_STRUCT_LINKED set in its predecessor. |
| 893 | * |
| 894 | * If @nextp is not NULL, it's updated to point to the next work of |
| 895 | * the last scheduled work. This allows move_linked_works() to be |
| 896 | * nested inside outer list_for_each_entry_safe(). |
| 897 | * |
| 898 | * CONTEXT: |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 899 | * spin_lock_irq(gcwq->lock). |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 900 | */ |
| 901 | static void move_linked_works(struct work_struct *work, struct list_head *head, |
| 902 | struct work_struct **nextp) |
| 903 | { |
| 904 | struct work_struct *n; |
| 905 | |
| 906 | /* |
| 907 | * Linked worklist will always end before the end of the list, |
| 908 | * use NULL for list head. |
| 909 | */ |
| 910 | list_for_each_entry_safe_from(work, n, NULL, entry) { |
| 911 | list_move_tail(&work->entry, head); |
| 912 | if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) |
| 913 | break; |
| 914 | } |
| 915 | |
| 916 | /* |
| 917 | * If we're already inside safe list traversal and have moved |
| 918 | * multiple works to the scheduled queue, the next position |
| 919 | * needs to be updated. |
| 920 | */ |
| 921 | if (nextp) |
| 922 | *nextp = n; |
| 923 | } |
| 924 | |
Tejun Heo | 1e19ffc | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 925 | static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq) |
| 926 | { |
| 927 | struct work_struct *work = list_first_entry(&cwq->delayed_works, |
| 928 | struct work_struct, entry); |
| 929 | |
| 930 | move_linked_works(work, &cwq->worklist, NULL); |
| 931 | cwq->nr_active++; |
| 932 | } |
| 933 | |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 934 | /** |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 935 | * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight |
| 936 | * @cwq: cwq of interest |
| 937 | * @color: color of work which left the queue |
| 938 | * |
| 939 | * A work either has completed or is removed from pending queue, |
| 940 | * decrement nr_in_flight of its cwq and handle workqueue flushing. |
| 941 | * |
| 942 | * CONTEXT: |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 943 | * spin_lock_irq(gcwq->lock). |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 944 | */ |
| 945 | static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color) |
| 946 | { |
| 947 | /* ignore uncolored works */ |
| 948 | if (color == WORK_NO_COLOR) |
| 949 | return; |
| 950 | |
| 951 | cwq->nr_in_flight[color]--; |
Tejun Heo | 1e19ffc | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 952 | cwq->nr_active--; |
| 953 | |
Tejun Heo | 502ca9d | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 954 | if (!list_empty(&cwq->delayed_works)) { |
| 955 | /* one down, submit a delayed one */ |
| 956 | if (cwq->nr_active < cwq->max_active) |
| 957 | cwq_activate_first_delayed(cwq); |
| 958 | } else if (!cwq->nr_active && cwq->wq->flags & WQ_SINGLE_CPU) { |
| 959 | /* this was the last work, unbind from single cpu */ |
| 960 | cwq_unbind_single_cpu(cwq); |
| 961 | } |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 962 | |
| 963 | /* is flush in progress and are we at the flushing tip? */ |
| 964 | if (likely(cwq->flush_color != color)) |
| 965 | return; |
| 966 | |
| 967 | /* are there still in-flight works? */ |
| 968 | if (cwq->nr_in_flight[color]) |
| 969 | return; |
| 970 | |
| 971 | /* this cwq is done, clear flush_color */ |
| 972 | cwq->flush_color = -1; |
| 973 | |
| 974 | /* |
| 975 | * If this was the last cwq, wake up the first flusher. It |
| 976 | * will handle the rest. |
| 977 | */ |
| 978 | if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush)) |
| 979 | complete(&cwq->wq->first_flusher->done); |
| 980 | } |
| 981 | |
| 982 | /** |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 983 | * process_one_work - process single work |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 984 | * @worker: self |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 985 | * @work: work to process |
| 986 | * |
| 987 | * Process @work. This function contains all the logics necessary to |
| 988 | * process a single work including synchronization against and |
| 989 | * interaction with other workers on the same cpu, queueing and |
| 990 | * flushing. As long as context requirement is met, any worker can |
| 991 | * call this function to process a work. |
| 992 | * |
| 993 | * CONTEXT: |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 994 | * spin_lock_irq(gcwq->lock) which is released and regrabbed. |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 995 | */ |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 996 | static void process_one_work(struct worker *worker, struct work_struct *work) |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 997 | { |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 998 | struct cpu_workqueue_struct *cwq = worker->cwq; |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 999 | struct global_cwq *gcwq = cwq->gcwq; |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1000 | struct hlist_head *bwh = busy_worker_head(gcwq, work); |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1001 | work_func_t f = work->func; |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1002 | int work_color; |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1003 | #ifdef CONFIG_LOCKDEP |
| 1004 | /* |
| 1005 | * It is permissible to free the struct work_struct from |
| 1006 | * inside the function that is called from it, this we need to |
| 1007 | * take into account for lockdep too. To avoid bogus "held |
| 1008 | * lock freed" warnings as well as problems when looking into |
| 1009 | * work->lockdep_map, make a copy and use that here. |
| 1010 | */ |
| 1011 | struct lockdep_map lockdep_map = work->lockdep_map; |
| 1012 | #endif |
| 1013 | /* claim and process */ |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1014 | debug_work_deactivate(work); |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1015 | hlist_add_head(&worker->hentry, bwh); |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1016 | worker->current_work = work; |
Tejun Heo | 8cca0ee | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 1017 | worker->current_cwq = cwq; |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1018 | work_color = get_work_color(work); |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame^] | 1019 | |
| 1020 | BUG_ON(get_work_cwq(work) != cwq); |
| 1021 | /* record the current cpu number in the work data and dequeue */ |
| 1022 | set_work_cpu(work, gcwq->cpu); |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1023 | list_del_init(&work->entry); |
| 1024 | |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1025 | spin_unlock_irq(&gcwq->lock); |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1026 | |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1027 | work_clear_pending(work); |
| 1028 | lock_map_acquire(&cwq->wq->lockdep_map); |
| 1029 | lock_map_acquire(&lockdep_map); |
| 1030 | f(work); |
| 1031 | lock_map_release(&lockdep_map); |
| 1032 | lock_map_release(&cwq->wq->lockdep_map); |
| 1033 | |
| 1034 | if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { |
| 1035 | printk(KERN_ERR "BUG: workqueue leaked lock or atomic: " |
| 1036 | "%s/0x%08x/%d\n", |
| 1037 | current->comm, preempt_count(), task_pid_nr(current)); |
| 1038 | printk(KERN_ERR " last function: "); |
| 1039 | print_symbol("%s\n", (unsigned long)f); |
| 1040 | debug_show_held_locks(current); |
| 1041 | dump_stack(); |
| 1042 | } |
| 1043 | |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1044 | spin_lock_irq(&gcwq->lock); |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1045 | |
| 1046 | /* we're done with it, release */ |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1047 | hlist_del_init(&worker->hentry); |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1048 | worker->current_work = NULL; |
Tejun Heo | 8cca0ee | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 1049 | worker->current_cwq = NULL; |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1050 | cwq_dec_nr_in_flight(cwq, work_color); |
Tejun Heo | a62428c | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1051 | } |
| 1052 | |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1053 | /** |
| 1054 | * process_scheduled_works - process scheduled works |
| 1055 | * @worker: self |
| 1056 | * |
| 1057 | * Process all scheduled works. Please note that the scheduled list |
| 1058 | * may change while processing a work, so this function repeatedly |
| 1059 | * fetches a work from the top and executes it. |
| 1060 | * |
| 1061 | * CONTEXT: |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1062 | * spin_lock_irq(gcwq->lock) which may be released and regrabbed |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1063 | * multiple times. |
| 1064 | */ |
| 1065 | static void process_scheduled_works(struct worker *worker) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | { |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1067 | while (!list_empty(&worker->scheduled)) { |
| 1068 | struct work_struct *work = list_first_entry(&worker->scheduled, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | struct work_struct, entry); |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1070 | process_one_work(worker, work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1074 | /** |
| 1075 | * worker_thread - the worker thread function |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1076 | * @__worker: self |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1077 | * |
| 1078 | * The cwq worker thread function. |
| 1079 | */ |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1080 | static int worker_thread(void *__worker) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | { |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1082 | struct worker *worker = __worker; |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1083 | struct global_cwq *gcwq = worker->gcwq; |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1084 | struct cpu_workqueue_struct *cwq = worker->cwq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1086 | woke_up: |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1087 | spin_lock_irq(&gcwq->lock); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1088 | |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1089 | /* DIE can be set only while we're idle, checking here is enough */ |
| 1090 | if (worker->flags & WORKER_DIE) { |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1091 | spin_unlock_irq(&gcwq->lock); |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1092 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | } |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1094 | |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1095 | worker_leave_idle(worker); |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1096 | recheck: |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1097 | /* |
| 1098 | * ->scheduled list can only be filled while a worker is |
| 1099 | * preparing to process a work or actually processing it. |
| 1100 | * Make sure nobody diddled with it while I was sleeping. |
| 1101 | */ |
| 1102 | BUG_ON(!list_empty(&worker->scheduled)); |
| 1103 | |
| 1104 | while (!list_empty(&cwq->worklist)) { |
| 1105 | struct work_struct *work = |
| 1106 | list_first_entry(&cwq->worklist, |
| 1107 | struct work_struct, entry); |
| 1108 | |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1109 | /* |
| 1110 | * The following is a rather inefficient way to close |
| 1111 | * race window against cpu hotplug operations. Will |
| 1112 | * be replaced soon. |
| 1113 | */ |
| 1114 | if (unlikely(!(worker->flags & WORKER_ROGUE) && |
| 1115 | !cpumask_equal(&worker->task->cpus_allowed, |
| 1116 | get_cpu_mask(gcwq->cpu)))) { |
| 1117 | spin_unlock_irq(&gcwq->lock); |
| 1118 | set_cpus_allowed_ptr(worker->task, |
| 1119 | get_cpu_mask(gcwq->cpu)); |
| 1120 | cpu_relax(); |
| 1121 | spin_lock_irq(&gcwq->lock); |
| 1122 | goto recheck; |
| 1123 | } |
| 1124 | |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1125 | if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) { |
| 1126 | /* optimization path, not strictly necessary */ |
| 1127 | process_one_work(worker, work); |
| 1128 | if (unlikely(!list_empty(&worker->scheduled))) |
| 1129 | process_scheduled_works(worker); |
| 1130 | } else { |
| 1131 | move_linked_works(work, &worker->scheduled, NULL); |
| 1132 | process_scheduled_works(worker); |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | /* |
| 1137 | * gcwq->lock is held and there's no work to process, sleep. |
| 1138 | * Workers are woken up only while holding gcwq->lock, so |
| 1139 | * setting the current state before releasing gcwq->lock is |
| 1140 | * enough to prevent losing any event. |
| 1141 | */ |
| 1142 | worker_enter_idle(worker); |
| 1143 | __set_current_state(TASK_INTERRUPTIBLE); |
| 1144 | spin_unlock_irq(&gcwq->lock); |
| 1145 | schedule(); |
| 1146 | goto woke_up; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
Oleg Nesterov | fc2e4d7 | 2007-05-09 02:33:51 -0700 | [diff] [blame] | 1149 | struct wq_barrier { |
| 1150 | struct work_struct work; |
| 1151 | struct completion done; |
| 1152 | }; |
| 1153 | |
| 1154 | static void wq_barrier_func(struct work_struct *work) |
| 1155 | { |
| 1156 | struct wq_barrier *barr = container_of(work, struct wq_barrier, work); |
| 1157 | complete(&barr->done); |
| 1158 | } |
| 1159 | |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1160 | /** |
| 1161 | * insert_wq_barrier - insert a barrier work |
| 1162 | * @cwq: cwq to insert barrier into |
| 1163 | * @barr: wq_barrier to insert |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1164 | * @target: target work to attach @barr to |
| 1165 | * @worker: worker currently executing @target, NULL if @target is not executing |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1166 | * |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1167 | * @barr is linked to @target such that @barr is completed only after |
| 1168 | * @target finishes execution. Please note that the ordering |
| 1169 | * guarantee is observed only with respect to @target and on the local |
| 1170 | * cpu. |
| 1171 | * |
| 1172 | * Currently, a queued barrier can't be canceled. This is because |
| 1173 | * try_to_grab_pending() can't determine whether the work to be |
| 1174 | * grabbed is at the head of the queue and thus can't clear LINKED |
| 1175 | * flag of the previous work while there must be a valid next work |
| 1176 | * after a work with LINKED flag set. |
| 1177 | * |
| 1178 | * Note that when @worker is non-NULL, @target may be modified |
| 1179 | * underneath us, so we can't reliably determine cwq from @target. |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1180 | * |
| 1181 | * CONTEXT: |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1182 | * spin_lock_irq(gcwq->lock). |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1183 | */ |
Oleg Nesterov | 83c2252 | 2007-05-09 02:33:54 -0700 | [diff] [blame] | 1184 | static void insert_wq_barrier(struct cpu_workqueue_struct *cwq, |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1185 | struct wq_barrier *barr, |
| 1186 | struct work_struct *target, struct worker *worker) |
Oleg Nesterov | fc2e4d7 | 2007-05-09 02:33:51 -0700 | [diff] [blame] | 1187 | { |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1188 | struct list_head *head; |
| 1189 | unsigned int linked = 0; |
| 1190 | |
Thomas Gleixner | dc186ad | 2009-11-16 01:09:48 +0900 | [diff] [blame] | 1191 | /* |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1192 | * debugobject calls are safe here even with gcwq->lock locked |
Thomas Gleixner | dc186ad | 2009-11-16 01:09:48 +0900 | [diff] [blame] | 1193 | * as we know for sure that this will not trigger any of the |
| 1194 | * checks and call back into the fixup functions where we |
| 1195 | * might deadlock. |
| 1196 | */ |
| 1197 | INIT_WORK_ON_STACK(&barr->work, wq_barrier_func); |
Tejun Heo | 22df02b | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1198 | __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); |
Oleg Nesterov | fc2e4d7 | 2007-05-09 02:33:51 -0700 | [diff] [blame] | 1199 | init_completion(&barr->done); |
Oleg Nesterov | 83c2252 | 2007-05-09 02:33:54 -0700 | [diff] [blame] | 1200 | |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1201 | /* |
| 1202 | * If @target is currently being executed, schedule the |
| 1203 | * barrier to the worker; otherwise, put it after @target. |
| 1204 | */ |
| 1205 | if (worker) |
| 1206 | head = worker->scheduled.next; |
| 1207 | else { |
| 1208 | unsigned long *bits = work_data_bits(target); |
| 1209 | |
| 1210 | head = target->entry.next; |
| 1211 | /* there can already be other linked works, inherit and set */ |
| 1212 | linked = *bits & WORK_STRUCT_LINKED; |
| 1213 | __set_bit(WORK_STRUCT_LINKED_BIT, bits); |
| 1214 | } |
| 1215 | |
Thomas Gleixner | dc186ad | 2009-11-16 01:09:48 +0900 | [diff] [blame] | 1216 | debug_work_activate(&barr->work); |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1217 | insert_work(cwq, &barr->work, head, |
| 1218 | work_color_to_flags(WORK_NO_COLOR) | linked); |
Oleg Nesterov | fc2e4d7 | 2007-05-09 02:33:51 -0700 | [diff] [blame] | 1219 | } |
| 1220 | |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1221 | /** |
| 1222 | * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing |
| 1223 | * @wq: workqueue being flushed |
| 1224 | * @flush_color: new flush color, < 0 for no-op |
| 1225 | * @work_color: new work color, < 0 for no-op |
| 1226 | * |
| 1227 | * Prepare cwqs for workqueue flushing. |
| 1228 | * |
| 1229 | * If @flush_color is non-negative, flush_color on all cwqs should be |
| 1230 | * -1. If no cwq has in-flight commands at the specified color, all |
| 1231 | * cwq->flush_color's stay at -1 and %false is returned. If any cwq |
| 1232 | * has in flight commands, its cwq->flush_color is set to |
| 1233 | * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq |
| 1234 | * wakeup logic is armed and %true is returned. |
| 1235 | * |
| 1236 | * The caller should have initialized @wq->first_flusher prior to |
| 1237 | * calling this function with non-negative @flush_color. If |
| 1238 | * @flush_color is negative, no flush color update is done and %false |
| 1239 | * is returned. |
| 1240 | * |
| 1241 | * If @work_color is non-negative, all cwqs should have the same |
| 1242 | * work_color which is previous to @work_color and all will be |
| 1243 | * advanced to @work_color. |
| 1244 | * |
| 1245 | * CONTEXT: |
| 1246 | * mutex_lock(wq->flush_mutex). |
| 1247 | * |
| 1248 | * RETURNS: |
| 1249 | * %true if @flush_color >= 0 and there's something to flush. %false |
| 1250 | * otherwise. |
| 1251 | */ |
| 1252 | static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq, |
| 1253 | int flush_color, int work_color) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | { |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1255 | bool wait = false; |
| 1256 | unsigned int cpu; |
Oleg Nesterov | 1444196 | 2007-05-23 13:57:57 -0700 | [diff] [blame] | 1257 | |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1258 | if (flush_color >= 0) { |
| 1259 | BUG_ON(atomic_read(&wq->nr_cwqs_to_flush)); |
| 1260 | atomic_set(&wq->nr_cwqs_to_flush, 1); |
Thomas Gleixner | dc186ad | 2009-11-16 01:09:48 +0900 | [diff] [blame] | 1261 | } |
Oleg Nesterov | 1444196 | 2007-05-23 13:57:57 -0700 | [diff] [blame] | 1262 | |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1263 | for_each_possible_cpu(cpu) { |
| 1264 | struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1265 | struct global_cwq *gcwq = cwq->gcwq; |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1266 | |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1267 | spin_lock_irq(&gcwq->lock); |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1268 | |
| 1269 | if (flush_color >= 0) { |
| 1270 | BUG_ON(cwq->flush_color != -1); |
| 1271 | |
| 1272 | if (cwq->nr_in_flight[flush_color]) { |
| 1273 | cwq->flush_color = flush_color; |
| 1274 | atomic_inc(&wq->nr_cwqs_to_flush); |
| 1275 | wait = true; |
| 1276 | } |
| 1277 | } |
| 1278 | |
| 1279 | if (work_color >= 0) { |
| 1280 | BUG_ON(work_color != work_next_color(cwq->work_color)); |
| 1281 | cwq->work_color = work_color; |
| 1282 | } |
| 1283 | |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1284 | spin_unlock_irq(&gcwq->lock); |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush)) |
| 1288 | complete(&wq->first_flusher->done); |
| 1289 | |
| 1290 | return wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | } |
| 1292 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 1293 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | * flush_workqueue - ensure that any scheduled work has run to completion. |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 1295 | * @wq: workqueue to flush |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | * |
| 1297 | * Forces execution of the workqueue and blocks until its completion. |
| 1298 | * This is typically used in driver shutdown handlers. |
| 1299 | * |
Oleg Nesterov | fc2e4d7 | 2007-05-09 02:33:51 -0700 | [diff] [blame] | 1300 | * We sleep until all works which were queued on entry have been handled, |
| 1301 | * but we are not livelocked by new incoming ones. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 1303 | void flush_workqueue(struct workqueue_struct *wq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | { |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1305 | struct wq_flusher this_flusher = { |
| 1306 | .list = LIST_HEAD_INIT(this_flusher.list), |
| 1307 | .flush_color = -1, |
| 1308 | .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done), |
| 1309 | }; |
| 1310 | int next_color; |
Oleg Nesterov | b1f4ec1 | 2007-05-09 02:34:12 -0700 | [diff] [blame] | 1311 | |
Ingo Molnar | 3295f0e | 2008-08-11 10:30:30 +0200 | [diff] [blame] | 1312 | lock_map_acquire(&wq->lockdep_map); |
| 1313 | lock_map_release(&wq->lockdep_map); |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1314 | |
| 1315 | mutex_lock(&wq->flush_mutex); |
| 1316 | |
| 1317 | /* |
| 1318 | * Start-to-wait phase |
| 1319 | */ |
| 1320 | next_color = work_next_color(wq->work_color); |
| 1321 | |
| 1322 | if (next_color != wq->flush_color) { |
| 1323 | /* |
| 1324 | * Color space is not full. The current work_color |
| 1325 | * becomes our flush_color and work_color is advanced |
| 1326 | * by one. |
| 1327 | */ |
| 1328 | BUG_ON(!list_empty(&wq->flusher_overflow)); |
| 1329 | this_flusher.flush_color = wq->work_color; |
| 1330 | wq->work_color = next_color; |
| 1331 | |
| 1332 | if (!wq->first_flusher) { |
| 1333 | /* no flush in progress, become the first flusher */ |
| 1334 | BUG_ON(wq->flush_color != this_flusher.flush_color); |
| 1335 | |
| 1336 | wq->first_flusher = &this_flusher; |
| 1337 | |
| 1338 | if (!flush_workqueue_prep_cwqs(wq, wq->flush_color, |
| 1339 | wq->work_color)) { |
| 1340 | /* nothing to flush, done */ |
| 1341 | wq->flush_color = next_color; |
| 1342 | wq->first_flusher = NULL; |
| 1343 | goto out_unlock; |
| 1344 | } |
| 1345 | } else { |
| 1346 | /* wait in queue */ |
| 1347 | BUG_ON(wq->flush_color == this_flusher.flush_color); |
| 1348 | list_add_tail(&this_flusher.list, &wq->flusher_queue); |
| 1349 | flush_workqueue_prep_cwqs(wq, -1, wq->work_color); |
| 1350 | } |
| 1351 | } else { |
| 1352 | /* |
| 1353 | * Oops, color space is full, wait on overflow queue. |
| 1354 | * The next flush completion will assign us |
| 1355 | * flush_color and transfer to flusher_queue. |
| 1356 | */ |
| 1357 | list_add_tail(&this_flusher.list, &wq->flusher_overflow); |
| 1358 | } |
| 1359 | |
| 1360 | mutex_unlock(&wq->flush_mutex); |
| 1361 | |
| 1362 | wait_for_completion(&this_flusher.done); |
| 1363 | |
| 1364 | /* |
| 1365 | * Wake-up-and-cascade phase |
| 1366 | * |
| 1367 | * First flushers are responsible for cascading flushes and |
| 1368 | * handling overflow. Non-first flushers can simply return. |
| 1369 | */ |
| 1370 | if (wq->first_flusher != &this_flusher) |
| 1371 | return; |
| 1372 | |
| 1373 | mutex_lock(&wq->flush_mutex); |
| 1374 | |
| 1375 | wq->first_flusher = NULL; |
| 1376 | |
| 1377 | BUG_ON(!list_empty(&this_flusher.list)); |
| 1378 | BUG_ON(wq->flush_color != this_flusher.flush_color); |
| 1379 | |
| 1380 | while (true) { |
| 1381 | struct wq_flusher *next, *tmp; |
| 1382 | |
| 1383 | /* complete all the flushers sharing the current flush color */ |
| 1384 | list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { |
| 1385 | if (next->flush_color != wq->flush_color) |
| 1386 | break; |
| 1387 | list_del_init(&next->list); |
| 1388 | complete(&next->done); |
| 1389 | } |
| 1390 | |
| 1391 | BUG_ON(!list_empty(&wq->flusher_overflow) && |
| 1392 | wq->flush_color != work_next_color(wq->work_color)); |
| 1393 | |
| 1394 | /* this flush_color is finished, advance by one */ |
| 1395 | wq->flush_color = work_next_color(wq->flush_color); |
| 1396 | |
| 1397 | /* one color has been freed, handle overflow queue */ |
| 1398 | if (!list_empty(&wq->flusher_overflow)) { |
| 1399 | /* |
| 1400 | * Assign the same color to all overflowed |
| 1401 | * flushers, advance work_color and append to |
| 1402 | * flusher_queue. This is the start-to-wait |
| 1403 | * phase for these overflowed flushers. |
| 1404 | */ |
| 1405 | list_for_each_entry(tmp, &wq->flusher_overflow, list) |
| 1406 | tmp->flush_color = wq->work_color; |
| 1407 | |
| 1408 | wq->work_color = work_next_color(wq->work_color); |
| 1409 | |
| 1410 | list_splice_tail_init(&wq->flusher_overflow, |
| 1411 | &wq->flusher_queue); |
| 1412 | flush_workqueue_prep_cwqs(wq, -1, wq->work_color); |
| 1413 | } |
| 1414 | |
| 1415 | if (list_empty(&wq->flusher_queue)) { |
| 1416 | BUG_ON(wq->flush_color != wq->work_color); |
| 1417 | break; |
| 1418 | } |
| 1419 | |
| 1420 | /* |
| 1421 | * Need to flush more colors. Make the next flusher |
| 1422 | * the new first flusher and arm cwqs. |
| 1423 | */ |
| 1424 | BUG_ON(wq->flush_color == wq->work_color); |
| 1425 | BUG_ON(wq->flush_color != next->flush_color); |
| 1426 | |
| 1427 | list_del_init(&next->list); |
| 1428 | wq->first_flusher = next; |
| 1429 | |
| 1430 | if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1)) |
| 1431 | break; |
| 1432 | |
| 1433 | /* |
| 1434 | * Meh... this color is already done, clear first |
| 1435 | * flusher and repeat cascading. |
| 1436 | */ |
| 1437 | wq->first_flusher = NULL; |
| 1438 | } |
| 1439 | |
| 1440 | out_unlock: |
| 1441 | mutex_unlock(&wq->flush_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 1443 | EXPORT_SYMBOL_GPL(flush_workqueue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | |
Oleg Nesterov | db70089 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 1445 | /** |
| 1446 | * flush_work - block until a work_struct's callback has terminated |
| 1447 | * @work: the work which is to be flushed |
| 1448 | * |
Oleg Nesterov | a67da70 | 2008-07-25 01:47:52 -0700 | [diff] [blame] | 1449 | * Returns false if @work has already terminated. |
| 1450 | * |
Oleg Nesterov | db70089 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 1451 | * It is expected that, prior to calling flush_work(), the caller has |
| 1452 | * arranged for the work to not be requeued, otherwise it doesn't make |
| 1453 | * sense to use this function. |
| 1454 | */ |
| 1455 | int flush_work(struct work_struct *work) |
| 1456 | { |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1457 | struct worker *worker = NULL; |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1458 | struct global_cwq *gcwq; |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame^] | 1459 | struct cpu_workqueue_struct *cwq; |
Oleg Nesterov | db70089 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 1460 | struct wq_barrier barr; |
| 1461 | |
| 1462 | might_sleep(); |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame^] | 1463 | gcwq = get_work_gcwq(work); |
| 1464 | if (!gcwq) |
Oleg Nesterov | db70089 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 1465 | return 0; |
Oleg Nesterov | a67da70 | 2008-07-25 01:47:52 -0700 | [diff] [blame] | 1466 | |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1467 | spin_lock_irq(&gcwq->lock); |
Oleg Nesterov | db70089 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 1468 | if (!list_empty(&work->entry)) { |
| 1469 | /* |
| 1470 | * See the comment near try_to_grab_pending()->smp_rmb(). |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame^] | 1471 | * If it was re-queued to a different gcwq under us, we |
| 1472 | * are not going to wait. |
Oleg Nesterov | db70089 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 1473 | */ |
| 1474 | smp_rmb(); |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame^] | 1475 | cwq = get_work_cwq(work); |
| 1476 | if (unlikely(!cwq || gcwq != cwq->gcwq)) |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1477 | goto already_gone; |
Oleg Nesterov | db70089 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 1478 | } else { |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame^] | 1479 | worker = find_worker_executing_work(gcwq, work); |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1480 | if (!worker) |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1481 | goto already_gone; |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame^] | 1482 | cwq = worker->current_cwq; |
Oleg Nesterov | db70089 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 1483 | } |
Oleg Nesterov | db70089 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 1484 | |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1485 | insert_wq_barrier(cwq, &barr, work, worker); |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1486 | spin_unlock_irq(&gcwq->lock); |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame^] | 1487 | |
| 1488 | lock_map_acquire(&cwq->wq->lockdep_map); |
| 1489 | lock_map_release(&cwq->wq->lockdep_map); |
| 1490 | |
Oleg Nesterov | db70089 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 1491 | wait_for_completion(&barr.done); |
Thomas Gleixner | dc186ad | 2009-11-16 01:09:48 +0900 | [diff] [blame] | 1492 | destroy_work_on_stack(&barr.work); |
Oleg Nesterov | db70089 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 1493 | return 1; |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1494 | already_gone: |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1495 | spin_unlock_irq(&gcwq->lock); |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1496 | return 0; |
Oleg Nesterov | db70089 | 2008-07-25 01:47:49 -0700 | [diff] [blame] | 1497 | } |
| 1498 | EXPORT_SYMBOL_GPL(flush_work); |
| 1499 | |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 1500 | /* |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 1501 | * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit, |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 1502 | * so this work can't be re-armed in any way. |
| 1503 | */ |
| 1504 | static int try_to_grab_pending(struct work_struct *work) |
| 1505 | { |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1506 | struct global_cwq *gcwq; |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 1507 | int ret = -1; |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 1508 | |
Tejun Heo | 22df02b | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1509 | 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] | 1510 | return 0; |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 1511 | |
| 1512 | /* |
| 1513 | * The queueing is in progress, or it is already queued. Try to |
| 1514 | * steal it from ->worklist without clearing WORK_STRUCT_PENDING. |
| 1515 | */ |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame^] | 1516 | gcwq = get_work_gcwq(work); |
| 1517 | if (!gcwq) |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 1518 | return ret; |
| 1519 | |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1520 | spin_lock_irq(&gcwq->lock); |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 1521 | if (!list_empty(&work->entry)) { |
| 1522 | /* |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame^] | 1523 | * This work is queued, but perhaps we locked the wrong gcwq. |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 1524 | * In that case we must see the new value after rmb(), see |
| 1525 | * insert_work()->wmb(). |
| 1526 | */ |
| 1527 | smp_rmb(); |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame^] | 1528 | if (gcwq == get_work_gcwq(work)) { |
Thomas Gleixner | dc186ad | 2009-11-16 01:09:48 +0900 | [diff] [blame] | 1529 | debug_work_deactivate(work); |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 1530 | list_del_init(&work->entry); |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame^] | 1531 | cwq_dec_nr_in_flight(get_work_cwq(work), |
| 1532 | get_work_color(work)); |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 1533 | ret = 1; |
| 1534 | } |
| 1535 | } |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1536 | spin_unlock_irq(&gcwq->lock); |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 1537 | |
| 1538 | return ret; |
| 1539 | } |
| 1540 | |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame^] | 1541 | static void wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work) |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 1542 | { |
| 1543 | struct wq_barrier barr; |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1544 | struct worker *worker; |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 1545 | |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1546 | spin_lock_irq(&gcwq->lock); |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1547 | |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame^] | 1548 | worker = find_worker_executing_work(gcwq, work); |
| 1549 | if (unlikely(worker)) |
| 1550 | insert_wq_barrier(worker->current_cwq, &barr, work, worker); |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1551 | |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1552 | spin_unlock_irq(&gcwq->lock); |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 1553 | |
Tejun Heo | affee4b | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1554 | if (unlikely(worker)) { |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 1555 | wait_for_completion(&barr.done); |
Thomas Gleixner | dc186ad | 2009-11-16 01:09:48 +0900 | [diff] [blame] | 1556 | destroy_work_on_stack(&barr.work); |
| 1557 | } |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 1558 | } |
| 1559 | |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 1560 | static void wait_on_work(struct work_struct *work) |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 1561 | { |
Oleg Nesterov | b1f4ec1 | 2007-05-09 02:34:12 -0700 | [diff] [blame] | 1562 | int cpu; |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 1563 | |
Oleg Nesterov | f293ea9 | 2007-05-09 02:34:10 -0700 | [diff] [blame] | 1564 | might_sleep(); |
| 1565 | |
Ingo Molnar | 3295f0e | 2008-08-11 10:30:30 +0200 | [diff] [blame] | 1566 | lock_map_acquire(&work->lockdep_map); |
| 1567 | lock_map_release(&work->lockdep_map); |
Johannes Berg | 4e6045f | 2007-10-18 23:39:55 -0700 | [diff] [blame] | 1568 | |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1569 | for_each_possible_cpu(cpu) |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame^] | 1570 | wait_on_cpu_work(get_gcwq(cpu), work); |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 1571 | } |
| 1572 | |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 1573 | static int __cancel_work_timer(struct work_struct *work, |
| 1574 | struct timer_list* timer) |
| 1575 | { |
| 1576 | int ret; |
| 1577 | |
| 1578 | do { |
| 1579 | ret = (timer && likely(del_timer(timer))); |
| 1580 | if (!ret) |
| 1581 | ret = try_to_grab_pending(work); |
| 1582 | wait_on_work(work); |
| 1583 | } while (unlikely(ret < 0)); |
| 1584 | |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame^] | 1585 | clear_work_data(work); |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 1586 | return ret; |
| 1587 | } |
| 1588 | |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 1589 | /** |
| 1590 | * cancel_work_sync - block until a work_struct's callback has terminated |
| 1591 | * @work: the work which is to be flushed |
| 1592 | * |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 1593 | * Returns true if @work was pending. |
| 1594 | * |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 1595 | * cancel_work_sync() will cancel the work if it is queued. If the work's |
| 1596 | * callback appears to be running, cancel_work_sync() will block until it |
| 1597 | * has completed. |
| 1598 | * |
| 1599 | * It is possible to use this function if the work re-queues itself. It can |
| 1600 | * cancel the work even if it migrates to another workqueue, however in that |
| 1601 | * case it only guarantees that work->func() has completed on the last queued |
| 1602 | * workqueue. |
| 1603 | * |
| 1604 | * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not |
| 1605 | * pending, otherwise it goes into a busy-wait loop until the timer expires. |
| 1606 | * |
| 1607 | * The caller must ensure that workqueue_struct on which this work was last |
| 1608 | * queued can't be destroyed before this function returns. |
| 1609 | */ |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 1610 | int cancel_work_sync(struct work_struct *work) |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 1611 | { |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 1612 | return __cancel_work_timer(work, NULL); |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 1613 | } |
Oleg Nesterov | 28e53bd | 2007-05-09 02:34:22 -0700 | [diff] [blame] | 1614 | EXPORT_SYMBOL_GPL(cancel_work_sync); |
Oleg Nesterov | b89deed | 2007-05-09 02:33:52 -0700 | [diff] [blame] | 1615 | |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 1616 | /** |
Oleg Nesterov | f5a421a | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 1617 | * cancel_delayed_work_sync - reliably kill off a delayed work. |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 1618 | * @dwork: the delayed work struct |
| 1619 | * |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 1620 | * Returns true if @dwork was pending. |
| 1621 | * |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 1622 | * It is possible to use this function if @dwork rearms itself via queue_work() |
| 1623 | * or queue_delayed_work(). See also the comment for cancel_work_sync(). |
| 1624 | */ |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 1625 | int cancel_delayed_work_sync(struct delayed_work *dwork) |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 1626 | { |
Oleg Nesterov | 1f1f642 | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 1627 | return __cancel_work_timer(&dwork->work, &dwork->timer); |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 1628 | } |
Oleg Nesterov | f5a421a | 2007-07-15 23:41:44 -0700 | [diff] [blame] | 1629 | EXPORT_SYMBOL(cancel_delayed_work_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | |
Oleg Nesterov | 6e84d64 | 2007-05-09 02:34:46 -0700 | [diff] [blame] | 1631 | static struct workqueue_struct *keventd_wq __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 1633 | /** |
| 1634 | * schedule_work - put work task in global workqueue |
| 1635 | * @work: job to be done |
| 1636 | * |
Bart Van Assche | 5b0f437d | 2009-07-30 19:00:53 +0200 | [diff] [blame] | 1637 | * Returns zero if @work was already on the kernel-global workqueue and |
| 1638 | * non-zero otherwise. |
| 1639 | * |
| 1640 | * This puts a job in the kernel-global workqueue if it was not already |
| 1641 | * queued and leaves it in the same position on the kernel-global |
| 1642 | * workqueue otherwise. |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 1643 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 1644 | int schedule_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | { |
| 1646 | return queue_work(keventd_wq, work); |
| 1647 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 1648 | EXPORT_SYMBOL(schedule_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | |
Zhang Rui | c1a220e | 2008-07-23 21:28:39 -0700 | [diff] [blame] | 1650 | /* |
| 1651 | * schedule_work_on - put work task on a specific cpu |
| 1652 | * @cpu: cpu to put the work task on |
| 1653 | * @work: job to be done |
| 1654 | * |
| 1655 | * This puts a job on a specific cpu |
| 1656 | */ |
| 1657 | int schedule_work_on(int cpu, struct work_struct *work) |
| 1658 | { |
| 1659 | return queue_work_on(cpu, keventd_wq, work); |
| 1660 | } |
| 1661 | EXPORT_SYMBOL(schedule_work_on); |
| 1662 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 1663 | /** |
| 1664 | * schedule_delayed_work - put work task in global workqueue after delay |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 1665 | * @dwork: job to be done |
| 1666 | * @delay: number of jiffies to wait or 0 for immediate execution |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 1667 | * |
| 1668 | * After waiting for a given time this puts a job in the kernel-global |
| 1669 | * workqueue. |
| 1670 | */ |
Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 1671 | int schedule_delayed_work(struct delayed_work *dwork, |
Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 1672 | unsigned long delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | { |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 1674 | return queue_delayed_work(keventd_wq, dwork, delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 1676 | EXPORT_SYMBOL(schedule_delayed_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 1678 | /** |
Linus Torvalds | 8c53e46 | 2009-10-14 09:16:42 -0700 | [diff] [blame] | 1679 | * flush_delayed_work - block until a dwork_struct's callback has terminated |
| 1680 | * @dwork: the delayed work which is to be flushed |
| 1681 | * |
| 1682 | * Any timeout is cancelled, and any pending work is run immediately. |
| 1683 | */ |
| 1684 | void flush_delayed_work(struct delayed_work *dwork) |
| 1685 | { |
| 1686 | if (del_timer_sync(&dwork->timer)) { |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame^] | 1687 | __queue_work(get_cpu(), get_work_cwq(&dwork->work)->wq, |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1688 | &dwork->work); |
Linus Torvalds | 8c53e46 | 2009-10-14 09:16:42 -0700 | [diff] [blame] | 1689 | put_cpu(); |
| 1690 | } |
| 1691 | flush_work(&dwork->work); |
| 1692 | } |
| 1693 | EXPORT_SYMBOL(flush_delayed_work); |
| 1694 | |
| 1695 | /** |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 1696 | * schedule_delayed_work_on - queue work in global workqueue on CPU after delay |
| 1697 | * @cpu: cpu to use |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 1698 | * @dwork: job to be done |
Rolf Eike Beer | 0fcb78c | 2006-07-30 03:03:42 -0700 | [diff] [blame] | 1699 | * @delay: number of jiffies to wait |
| 1700 | * |
| 1701 | * After waiting for a given time this puts a job in the kernel-global |
| 1702 | * workqueue on the specified CPU. |
| 1703 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | int schedule_delayed_work_on(int cpu, |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 1705 | struct delayed_work *dwork, unsigned long delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | { |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 1707 | return queue_delayed_work_on(cpu, keventd_wq, dwork, delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 1709 | EXPORT_SYMBOL(schedule_delayed_work_on); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 1711 | /** |
| 1712 | * schedule_on_each_cpu - call a function on each online CPU from keventd |
| 1713 | * @func: the function to call |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 1714 | * |
| 1715 | * Returns zero on success. |
| 1716 | * Returns -ve errno on failure. |
| 1717 | * |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 1718 | * schedule_on_each_cpu() is very slow. |
| 1719 | */ |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 1720 | int schedule_on_each_cpu(work_func_t func) |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 1721 | { |
| 1722 | int cpu; |
Andi Kleen | 65a6446 | 2009-10-14 06:22:47 +0200 | [diff] [blame] | 1723 | int orig = -1; |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 1724 | struct work_struct *works; |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 1725 | |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 1726 | works = alloc_percpu(struct work_struct); |
| 1727 | if (!works) |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 1728 | return -ENOMEM; |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 1729 | |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 1730 | get_online_cpus(); |
Tejun Heo | 9398180 | 2009-11-17 14:06:20 -0800 | [diff] [blame] | 1731 | |
| 1732 | /* |
| 1733 | * When running in keventd don't schedule a work item on |
| 1734 | * itself. Can just call directly because the work queue is |
| 1735 | * already bound. This also is faster. |
| 1736 | */ |
| 1737 | if (current_is_keventd()) |
| 1738 | orig = raw_smp_processor_id(); |
| 1739 | |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 1740 | for_each_online_cpu(cpu) { |
Ingo Molnar | 9bfb183 | 2006-12-18 20:05:09 +0100 | [diff] [blame] | 1741 | struct work_struct *work = per_cpu_ptr(works, cpu); |
| 1742 | |
| 1743 | INIT_WORK(work, func); |
Andi Kleen | 65a6446 | 2009-10-14 06:22:47 +0200 | [diff] [blame] | 1744 | if (cpu != orig) |
Tejun Heo | 9398180 | 2009-11-17 14:06:20 -0800 | [diff] [blame] | 1745 | schedule_work_on(cpu, work); |
Andi Kleen | 65a6446 | 2009-10-14 06:22:47 +0200 | [diff] [blame] | 1746 | } |
Tejun Heo | 9398180 | 2009-11-17 14:06:20 -0800 | [diff] [blame] | 1747 | if (orig >= 0) |
| 1748 | func(per_cpu_ptr(works, orig)); |
| 1749 | |
| 1750 | for_each_online_cpu(cpu) |
| 1751 | flush_work(per_cpu_ptr(works, cpu)); |
| 1752 | |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 1753 | put_online_cpus(); |
Andrew Morton | b613677 | 2006-06-25 05:47:49 -0700 | [diff] [blame] | 1754 | free_percpu(works); |
Christoph Lameter | 15316ba | 2006-01-08 01:00:43 -0800 | [diff] [blame] | 1755 | return 0; |
| 1756 | } |
| 1757 | |
Alan Stern | eef6a7d | 2010-02-12 17:39:21 +0900 | [diff] [blame] | 1758 | /** |
| 1759 | * flush_scheduled_work - ensure that any scheduled work has run to completion. |
| 1760 | * |
| 1761 | * Forces execution of the kernel-global workqueue and blocks until its |
| 1762 | * completion. |
| 1763 | * |
| 1764 | * Think twice before calling this function! It's very easy to get into |
| 1765 | * trouble if you don't take great care. Either of the following situations |
| 1766 | * will lead to deadlock: |
| 1767 | * |
| 1768 | * One of the work items currently on the workqueue needs to acquire |
| 1769 | * a lock held by your code or its caller. |
| 1770 | * |
| 1771 | * Your code is running in the context of a work routine. |
| 1772 | * |
| 1773 | * They will be detected by lockdep when they occur, but the first might not |
| 1774 | * occur very often. It depends on what work items are on the workqueue and |
| 1775 | * what locks they need, which you have no control over. |
| 1776 | * |
| 1777 | * In most situations flushing the entire workqueue is overkill; you merely |
| 1778 | * need to know that a particular work item isn't queued and isn't running. |
| 1779 | * In such cases you should use cancel_delayed_work_sync() or |
| 1780 | * cancel_work_sync() instead. |
| 1781 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | void flush_scheduled_work(void) |
| 1783 | { |
| 1784 | flush_workqueue(keventd_wq); |
| 1785 | } |
Dave Jones | ae90dd5 | 2006-06-30 01:40:45 -0400 | [diff] [blame] | 1786 | EXPORT_SYMBOL(flush_scheduled_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | |
| 1788 | /** |
James Bottomley | 1fa44ec | 2006-02-23 12:43:43 -0600 | [diff] [blame] | 1789 | * execute_in_process_context - reliably execute the routine with user context |
| 1790 | * @fn: the function to execute |
James Bottomley | 1fa44ec | 2006-02-23 12:43:43 -0600 | [diff] [blame] | 1791 | * @ew: guaranteed storage for the execute work structure (must |
| 1792 | * be available when the work executes) |
| 1793 | * |
| 1794 | * Executes the function immediately if process context is available, |
| 1795 | * otherwise schedules the function for delayed execution. |
| 1796 | * |
| 1797 | * Returns: 0 - function was executed |
| 1798 | * 1 - function was scheduled for execution |
| 1799 | */ |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 1800 | 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] | 1801 | { |
| 1802 | if (!in_interrupt()) { |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 1803 | fn(&ew->work); |
James Bottomley | 1fa44ec | 2006-02-23 12:43:43 -0600 | [diff] [blame] | 1804 | return 0; |
| 1805 | } |
| 1806 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 1807 | INIT_WORK(&ew->work, fn); |
James Bottomley | 1fa44ec | 2006-02-23 12:43:43 -0600 | [diff] [blame] | 1808 | schedule_work(&ew->work); |
| 1809 | |
| 1810 | return 1; |
| 1811 | } |
| 1812 | EXPORT_SYMBOL_GPL(execute_in_process_context); |
| 1813 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | int keventd_up(void) |
| 1815 | { |
| 1816 | return keventd_wq != NULL; |
| 1817 | } |
| 1818 | |
| 1819 | int current_is_keventd(void) |
| 1820 | { |
| 1821 | struct cpu_workqueue_struct *cwq; |
Hugh Dickins | d243769 | 2007-08-27 16:06:19 +0100 | [diff] [blame] | 1822 | int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | int ret = 0; |
| 1824 | |
| 1825 | BUG_ON(!keventd_wq); |
| 1826 | |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1827 | cwq = get_cwq(cpu, keventd_wq); |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1828 | if (current == cwq->worker->task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | ret = 1; |
| 1830 | |
| 1831 | return ret; |
| 1832 | |
| 1833 | } |
| 1834 | |
Tejun Heo | 0f90004 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1835 | static struct cpu_workqueue_struct *alloc_cwqs(void) |
| 1836 | { |
| 1837 | /* |
| 1838 | * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS. |
| 1839 | * Make sure that the alignment isn't lower than that of |
| 1840 | * unsigned long long. |
| 1841 | */ |
| 1842 | const size_t size = sizeof(struct cpu_workqueue_struct); |
| 1843 | const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS, |
| 1844 | __alignof__(unsigned long long)); |
| 1845 | struct cpu_workqueue_struct *cwqs; |
| 1846 | #ifndef CONFIG_SMP |
| 1847 | void *ptr; |
| 1848 | |
| 1849 | /* |
| 1850 | * On UP, percpu allocator doesn't honor alignment parameter |
| 1851 | * and simply uses arch-dependent default. Allocate enough |
| 1852 | * room to align cwq and put an extra pointer at the end |
| 1853 | * pointing back to the originally allocated pointer which |
| 1854 | * will be used for free. |
| 1855 | * |
| 1856 | * FIXME: This really belongs to UP percpu code. Update UP |
| 1857 | * percpu code to honor alignment and remove this ugliness. |
| 1858 | */ |
| 1859 | ptr = __alloc_percpu(size + align + sizeof(void *), 1); |
| 1860 | cwqs = PTR_ALIGN(ptr, align); |
| 1861 | *(void **)per_cpu_ptr(cwqs + 1, 0) = ptr; |
| 1862 | #else |
| 1863 | /* On SMP, percpu allocator can do it itself */ |
| 1864 | cwqs = __alloc_percpu(size, align); |
| 1865 | #endif |
| 1866 | /* just in case, make sure it's actually aligned */ |
| 1867 | BUG_ON(!IS_ALIGNED((unsigned long)cwqs, align)); |
| 1868 | return cwqs; |
| 1869 | } |
| 1870 | |
| 1871 | static void free_cwqs(struct cpu_workqueue_struct *cwqs) |
| 1872 | { |
| 1873 | #ifndef CONFIG_SMP |
| 1874 | /* on UP, the pointer to free is stored right after the cwq */ |
| 1875 | if (cwqs) |
| 1876 | free_percpu(*(void **)per_cpu_ptr(cwqs + 1, 0)); |
| 1877 | #else |
| 1878 | free_percpu(cwqs); |
| 1879 | #endif |
| 1880 | } |
| 1881 | |
Johannes Berg | 4e6045f | 2007-10-18 23:39:55 -0700 | [diff] [blame] | 1882 | struct workqueue_struct *__create_workqueue_key(const char *name, |
Tejun Heo | 97e37d7 | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1883 | unsigned int flags, |
Tejun Heo | 1e19ffc | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1884 | int max_active, |
Johannes Berg | eb13ba8 | 2008-01-16 09:51:58 +0100 | [diff] [blame] | 1885 | struct lock_class_key *key, |
| 1886 | const char *lock_name) |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1887 | { |
| 1888 | struct workqueue_struct *wq; |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1889 | bool failed = false; |
| 1890 | unsigned int cpu; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1891 | |
Tejun Heo | 1e19ffc | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1892 | max_active = clamp_val(max_active, 1, INT_MAX); |
| 1893 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1894 | wq = kzalloc(sizeof(*wq), GFP_KERNEL); |
| 1895 | if (!wq) |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1896 | goto err; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1897 | |
Tejun Heo | 0f90004 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1898 | wq->cpu_wq = alloc_cwqs(); |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1899 | if (!wq->cpu_wq) |
| 1900 | goto err; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1901 | |
Tejun Heo | 97e37d7 | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1902 | wq->flags = flags; |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1903 | wq->saved_max_active = max_active; |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1904 | mutex_init(&wq->flush_mutex); |
| 1905 | atomic_set(&wq->nr_cwqs_to_flush, 0); |
| 1906 | INIT_LIST_HEAD(&wq->flusher_queue); |
| 1907 | INIT_LIST_HEAD(&wq->flusher_overflow); |
Tejun Heo | 502ca9d | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 1908 | wq->single_cpu = NR_CPUS; |
| 1909 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1910 | wq->name = name; |
Johannes Berg | eb13ba8 | 2008-01-16 09:51:58 +0100 | [diff] [blame] | 1911 | lockdep_init_map(&wq->lockdep_map, lock_name, key, 0); |
Oleg Nesterov | cce1a16 | 2007-05-09 02:34:13 -0700 | [diff] [blame] | 1912 | INIT_LIST_HEAD(&wq->list); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1913 | |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1914 | cpu_maps_update_begin(); |
| 1915 | /* |
| 1916 | * We must initialize cwqs for each possible cpu even if we |
| 1917 | * are going to call destroy_workqueue() finally. Otherwise |
| 1918 | * cpu_up() can hit the uninitialized cwq once we drop the |
| 1919 | * lock. |
| 1920 | */ |
| 1921 | for_each_possible_cpu(cpu) { |
| 1922 | struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1923 | struct global_cwq *gcwq = get_gcwq(cpu); |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1924 | |
Tejun Heo | 0f90004 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1925 | BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK); |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1926 | cwq->gcwq = gcwq; |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1927 | cwq->wq = wq; |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1928 | cwq->flush_color = -1; |
Tejun Heo | 1e19ffc | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1929 | cwq->max_active = max_active; |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1930 | INIT_LIST_HEAD(&cwq->worklist); |
Tejun Heo | 1e19ffc | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1931 | INIT_LIST_HEAD(&cwq->delayed_works); |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1932 | |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1933 | if (failed) |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1934 | continue; |
Tejun Heo | 502ca9d | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 1935 | cwq->worker = create_worker(cwq, cpu_online(cpu)); |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1936 | if (cwq->worker) |
| 1937 | start_worker(cwq->worker); |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1938 | else |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1939 | failed = true; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1940 | } |
| 1941 | |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1942 | /* |
| 1943 | * workqueue_lock protects global freeze state and workqueues |
| 1944 | * list. Grab it, set max_active accordingly and add the new |
| 1945 | * workqueue to workqueues list. |
| 1946 | */ |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1947 | spin_lock(&workqueue_lock); |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1948 | |
| 1949 | if (workqueue_freezing && wq->flags & WQ_FREEZEABLE) |
| 1950 | for_each_possible_cpu(cpu) |
| 1951 | get_cwq(cpu, wq)->max_active = 0; |
| 1952 | |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1953 | list_add(&wq->list, &workqueues); |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1954 | |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1955 | spin_unlock(&workqueue_lock); |
| 1956 | |
| 1957 | cpu_maps_update_done(); |
| 1958 | |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1959 | if (failed) { |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1960 | destroy_workqueue(wq); |
| 1961 | wq = NULL; |
| 1962 | } |
| 1963 | return wq; |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1964 | err: |
| 1965 | if (wq) { |
Tejun Heo | 0f90004 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1966 | free_cwqs(wq->cpu_wq); |
Tejun Heo | 4690c4a | 2010-06-29 10:07:10 +0200 | [diff] [blame] | 1967 | kfree(wq); |
| 1968 | } |
| 1969 | return NULL; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1970 | } |
Johannes Berg | 4e6045f | 2007-10-18 23:39:55 -0700 | [diff] [blame] | 1971 | EXPORT_SYMBOL_GPL(__create_workqueue_key); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1972 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1973 | /** |
| 1974 | * destroy_workqueue - safely terminate a workqueue |
| 1975 | * @wq: target workqueue |
| 1976 | * |
| 1977 | * Safely destroy a workqueue. All work currently pending will be done first. |
| 1978 | */ |
| 1979 | void destroy_workqueue(struct workqueue_struct *wq) |
| 1980 | { |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1981 | unsigned int cpu; |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1982 | |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 1983 | flush_workqueue(wq); |
| 1984 | |
| 1985 | /* |
| 1986 | * wq list is used to freeze wq, remove from list after |
| 1987 | * flushing is complete in case freeze races us. |
| 1988 | */ |
Oleg Nesterov | 3da1c84 | 2008-07-25 01:47:50 -0700 | [diff] [blame] | 1989 | cpu_maps_update_begin(); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 1990 | spin_lock(&workqueue_lock); |
Oleg Nesterov | b1f4ec1 | 2007-05-09 02:34:12 -0700 | [diff] [blame] | 1991 | list_del(&wq->list); |
Gautham R Shenoy | 95402b3 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 1992 | spin_unlock(&workqueue_lock); |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1993 | cpu_maps_update_done(); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 1994 | |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1995 | for_each_possible_cpu(cpu) { |
| 1996 | struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); |
| 1997 | int i; |
| 1998 | |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 1999 | if (cwq->worker) { |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2000 | spin_lock_irq(&cwq->gcwq->lock); |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2001 | destroy_worker(cwq->worker); |
| 2002 | cwq->worker = NULL; |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2003 | spin_unlock_irq(&cwq->gcwq->lock); |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2004 | } |
| 2005 | |
| 2006 | for (i = 0; i < WORK_NR_COLORS; i++) |
| 2007 | BUG_ON(cwq->nr_in_flight[i]); |
Tejun Heo | 1e19ffc | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2008 | BUG_ON(cwq->nr_active); |
| 2009 | BUG_ON(!list_empty(&cwq->delayed_works)); |
Tejun Heo | 73f53c4 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2010 | } |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 2011 | |
Tejun Heo | 0f90004 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2012 | free_cwqs(wq->cpu_wq); |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 2013 | kfree(wq); |
| 2014 | } |
| 2015 | EXPORT_SYMBOL_GPL(destroy_workqueue); |
| 2016 | |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2017 | /* |
| 2018 | * CPU hotplug. |
| 2019 | * |
| 2020 | * CPU hotplug is implemented by allowing cwqs to be detached from |
| 2021 | * CPU, running with unbound workers and allowing them to be |
| 2022 | * reattached later if the cpu comes back online. A separate thread |
| 2023 | * is created to govern cwqs in such state and is called the trustee. |
| 2024 | * |
| 2025 | * Trustee states and their descriptions. |
| 2026 | * |
| 2027 | * START Command state used on startup. On CPU_DOWN_PREPARE, a |
| 2028 | * new trustee is started with this state. |
| 2029 | * |
| 2030 | * IN_CHARGE Once started, trustee will enter this state after |
| 2031 | * making all existing workers rogue. DOWN_PREPARE waits |
| 2032 | * for trustee to enter this state. After reaching |
| 2033 | * IN_CHARGE, trustee tries to execute the pending |
| 2034 | * worklist until it's empty and the state is set to |
| 2035 | * BUTCHER, or the state is set to RELEASE. |
| 2036 | * |
| 2037 | * BUTCHER Command state which is set by the cpu callback after |
| 2038 | * the cpu has went down. Once this state is set trustee |
| 2039 | * knows that there will be no new works on the worklist |
| 2040 | * and once the worklist is empty it can proceed to |
| 2041 | * killing idle workers. |
| 2042 | * |
| 2043 | * RELEASE Command state which is set by the cpu callback if the |
| 2044 | * cpu down has been canceled or it has come online |
| 2045 | * again. After recognizing this state, trustee stops |
| 2046 | * trying to drain or butcher and transits to DONE. |
| 2047 | * |
| 2048 | * DONE Trustee will enter this state after BUTCHER or RELEASE |
| 2049 | * is complete. |
| 2050 | * |
| 2051 | * trustee CPU draining |
| 2052 | * took over down complete |
| 2053 | * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE |
| 2054 | * | | ^ |
| 2055 | * | CPU is back online v return workers | |
| 2056 | * ----------------> RELEASE -------------- |
| 2057 | */ |
| 2058 | |
| 2059 | /** |
| 2060 | * trustee_wait_event_timeout - timed event wait for trustee |
| 2061 | * @cond: condition to wait for |
| 2062 | * @timeout: timeout in jiffies |
| 2063 | * |
| 2064 | * wait_event_timeout() for trustee to use. Handles locking and |
| 2065 | * checks for RELEASE request. |
| 2066 | * |
| 2067 | * CONTEXT: |
| 2068 | * spin_lock_irq(gcwq->lock) which may be released and regrabbed |
| 2069 | * multiple times. To be used by trustee. |
| 2070 | * |
| 2071 | * RETURNS: |
| 2072 | * Positive indicating left time if @cond is satisfied, 0 if timed |
| 2073 | * out, -1 if canceled. |
| 2074 | */ |
| 2075 | #define trustee_wait_event_timeout(cond, timeout) ({ \ |
| 2076 | long __ret = (timeout); \ |
| 2077 | while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \ |
| 2078 | __ret) { \ |
| 2079 | spin_unlock_irq(&gcwq->lock); \ |
| 2080 | __wait_event_timeout(gcwq->trustee_wait, (cond) || \ |
| 2081 | (gcwq->trustee_state == TRUSTEE_RELEASE), \ |
| 2082 | __ret); \ |
| 2083 | spin_lock_irq(&gcwq->lock); \ |
| 2084 | } \ |
| 2085 | gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \ |
| 2086 | }) |
| 2087 | |
| 2088 | /** |
| 2089 | * trustee_wait_event - event wait for trustee |
| 2090 | * @cond: condition to wait for |
| 2091 | * |
| 2092 | * wait_event() for trustee to use. Automatically handles locking and |
| 2093 | * checks for CANCEL request. |
| 2094 | * |
| 2095 | * CONTEXT: |
| 2096 | * spin_lock_irq(gcwq->lock) which may be released and regrabbed |
| 2097 | * multiple times. To be used by trustee. |
| 2098 | * |
| 2099 | * RETURNS: |
| 2100 | * 0 if @cond is satisfied, -1 if canceled. |
| 2101 | */ |
| 2102 | #define trustee_wait_event(cond) ({ \ |
| 2103 | long __ret1; \ |
| 2104 | __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\ |
| 2105 | __ret1 < 0 ? -1 : 0; \ |
| 2106 | }) |
| 2107 | |
| 2108 | static int __cpuinit trustee_thread(void *__gcwq) |
| 2109 | { |
| 2110 | struct global_cwq *gcwq = __gcwq; |
| 2111 | struct worker *worker; |
| 2112 | struct hlist_node *pos; |
| 2113 | int i; |
| 2114 | |
| 2115 | BUG_ON(gcwq->cpu != smp_processor_id()); |
| 2116 | |
| 2117 | spin_lock_irq(&gcwq->lock); |
| 2118 | /* |
Tejun Heo | 502ca9d | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 2119 | * Make all workers rogue. Trustee must be bound to the |
| 2120 | * target cpu and can't be cancelled. |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2121 | */ |
| 2122 | BUG_ON(gcwq->cpu != smp_processor_id()); |
| 2123 | |
| 2124 | list_for_each_entry(worker, &gcwq->idle_list, entry) |
Tejun Heo | 502ca9d | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 2125 | worker->flags |= WORKER_ROGUE; |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2126 | |
| 2127 | for_each_busy_worker(worker, i, pos, gcwq) |
Tejun Heo | 502ca9d | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 2128 | worker->flags |= WORKER_ROGUE; |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2129 | |
| 2130 | /* |
| 2131 | * We're now in charge. Notify and proceed to drain. We need |
| 2132 | * to keep the gcwq running during the whole CPU down |
| 2133 | * procedure as other cpu hotunplug callbacks may need to |
| 2134 | * flush currently running tasks. |
| 2135 | */ |
| 2136 | gcwq->trustee_state = TRUSTEE_IN_CHARGE; |
| 2137 | wake_up_all(&gcwq->trustee_wait); |
| 2138 | |
| 2139 | /* |
| 2140 | * The original cpu is in the process of dying and may go away |
| 2141 | * anytime now. When that happens, we and all workers would |
| 2142 | * be migrated to other cpus. Try draining any left work. |
| 2143 | * Note that if the gcwq is frozen, there may be frozen works |
| 2144 | * in freezeable cwqs. Don't declare completion while frozen. |
| 2145 | */ |
| 2146 | while (gcwq->nr_workers != gcwq->nr_idle || |
| 2147 | gcwq->flags & GCWQ_FREEZING || |
| 2148 | gcwq->trustee_state == TRUSTEE_IN_CHARGE) { |
| 2149 | /* give a breather */ |
| 2150 | if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0) |
| 2151 | break; |
| 2152 | } |
| 2153 | |
| 2154 | /* notify completion */ |
| 2155 | gcwq->trustee = NULL; |
| 2156 | gcwq->trustee_state = TRUSTEE_DONE; |
| 2157 | wake_up_all(&gcwq->trustee_wait); |
| 2158 | spin_unlock_irq(&gcwq->lock); |
| 2159 | return 0; |
| 2160 | } |
| 2161 | |
| 2162 | /** |
| 2163 | * wait_trustee_state - wait for trustee to enter the specified state |
| 2164 | * @gcwq: gcwq the trustee of interest belongs to |
| 2165 | * @state: target state to wait for |
| 2166 | * |
| 2167 | * Wait for the trustee to reach @state. DONE is already matched. |
| 2168 | * |
| 2169 | * CONTEXT: |
| 2170 | * spin_lock_irq(gcwq->lock) which may be released and regrabbed |
| 2171 | * multiple times. To be used by cpu_callback. |
| 2172 | */ |
| 2173 | static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state) |
| 2174 | { |
| 2175 | if (!(gcwq->trustee_state == state || |
| 2176 | gcwq->trustee_state == TRUSTEE_DONE)) { |
| 2177 | spin_unlock_irq(&gcwq->lock); |
| 2178 | __wait_event(gcwq->trustee_wait, |
| 2179 | gcwq->trustee_state == state || |
| 2180 | gcwq->trustee_state == TRUSTEE_DONE); |
| 2181 | spin_lock_irq(&gcwq->lock); |
| 2182 | } |
| 2183 | } |
| 2184 | |
Oleg Nesterov | 3af24433 | 2007-05-09 02:34:09 -0700 | [diff] [blame] | 2185 | static int __devinit workqueue_cpu_callback(struct notifier_block *nfb, |
| 2186 | unsigned long action, |
| 2187 | void *hcpu) |
| 2188 | { |
| 2189 | unsigned int cpu = (unsigned long)hcpu; |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2190 | struct global_cwq *gcwq = get_gcwq(cpu); |
| 2191 | struct task_struct *new_trustee = NULL; |
| 2192 | struct worker *worker; |
| 2193 | struct hlist_node *pos; |
| 2194 | unsigned long flags; |
| 2195 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2196 | |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 2197 | action &= ~CPU_TASKS_FROZEN; |
| 2198 | |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2199 | switch (action) { |
| 2200 | case CPU_DOWN_PREPARE: |
| 2201 | new_trustee = kthread_create(trustee_thread, gcwq, |
| 2202 | "workqueue_trustee/%d\n", cpu); |
| 2203 | if (IS_ERR(new_trustee)) |
| 2204 | return notifier_from_errno(PTR_ERR(new_trustee)); |
| 2205 | kthread_bind(new_trustee, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2206 | } |
| 2207 | |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2208 | /* some are called w/ irq disabled, don't disturb irq status */ |
| 2209 | spin_lock_irqsave(&gcwq->lock, flags); |
| 2210 | |
| 2211 | switch (action) { |
| 2212 | case CPU_DOWN_PREPARE: |
| 2213 | /* initialize trustee and tell it to acquire the gcwq */ |
| 2214 | BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE); |
| 2215 | gcwq->trustee = new_trustee; |
| 2216 | gcwq->trustee_state = TRUSTEE_START; |
| 2217 | wake_up_process(gcwq->trustee); |
| 2218 | wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE); |
| 2219 | break; |
| 2220 | |
| 2221 | case CPU_POST_DEAD: |
| 2222 | gcwq->trustee_state = TRUSTEE_BUTCHER; |
| 2223 | break; |
| 2224 | |
| 2225 | case CPU_DOWN_FAILED: |
| 2226 | case CPU_ONLINE: |
| 2227 | if (gcwq->trustee_state != TRUSTEE_DONE) { |
| 2228 | gcwq->trustee_state = TRUSTEE_RELEASE; |
| 2229 | wake_up_process(gcwq->trustee); |
| 2230 | wait_trustee_state(gcwq, TRUSTEE_DONE); |
| 2231 | } |
| 2232 | |
Tejun Heo | 502ca9d | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 2233 | /* clear ROGUE from all workers */ |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2234 | list_for_each_entry(worker, &gcwq->idle_list, entry) |
Tejun Heo | 502ca9d | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 2235 | worker->flags &= ~WORKER_ROGUE; |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2236 | |
| 2237 | for_each_busy_worker(worker, i, pos, gcwq) |
Tejun Heo | 502ca9d | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 2238 | worker->flags &= ~WORKER_ROGUE; |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2239 | break; |
| 2240 | } |
| 2241 | |
| 2242 | spin_unlock_irqrestore(&gcwq->lock, flags); |
| 2243 | |
Tejun Heo | 1537663 | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2244 | return notifier_from_errno(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2245 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 2247 | #ifdef CONFIG_SMP |
Rusty Russell | 8ccad40 | 2009-01-16 15:31:15 -0800 | [diff] [blame] | 2248 | |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 2249 | struct work_for_cpu { |
Andrew Morton | 6b44003 | 2009-04-09 09:50:37 -0600 | [diff] [blame] | 2250 | struct completion completion; |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 2251 | long (*fn)(void *); |
| 2252 | void *arg; |
| 2253 | long ret; |
| 2254 | }; |
| 2255 | |
Andrew Morton | 6b44003 | 2009-04-09 09:50:37 -0600 | [diff] [blame] | 2256 | static int do_work_for_cpu(void *_wfc) |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 2257 | { |
Andrew Morton | 6b44003 | 2009-04-09 09:50:37 -0600 | [diff] [blame] | 2258 | struct work_for_cpu *wfc = _wfc; |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 2259 | wfc->ret = wfc->fn(wfc->arg); |
Andrew Morton | 6b44003 | 2009-04-09 09:50:37 -0600 | [diff] [blame] | 2260 | complete(&wfc->completion); |
| 2261 | return 0; |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 2262 | } |
| 2263 | |
| 2264 | /** |
| 2265 | * work_on_cpu - run a function in user context on a particular cpu |
| 2266 | * @cpu: the cpu to run on |
| 2267 | * @fn: the function to run |
| 2268 | * @arg: the function arg |
| 2269 | * |
Rusty Russell | 31ad908 | 2009-01-16 15:31:15 -0800 | [diff] [blame] | 2270 | * This will return the value @fn returns. |
| 2271 | * 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] | 2272 | * 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] | 2273 | */ |
| 2274 | long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg) |
| 2275 | { |
Andrew Morton | 6b44003 | 2009-04-09 09:50:37 -0600 | [diff] [blame] | 2276 | struct task_struct *sub_thread; |
| 2277 | struct work_for_cpu wfc = { |
| 2278 | .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion), |
| 2279 | .fn = fn, |
| 2280 | .arg = arg, |
| 2281 | }; |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 2282 | |
Andrew Morton | 6b44003 | 2009-04-09 09:50:37 -0600 | [diff] [blame] | 2283 | sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu"); |
| 2284 | if (IS_ERR(sub_thread)) |
| 2285 | return PTR_ERR(sub_thread); |
| 2286 | kthread_bind(sub_thread, cpu); |
| 2287 | wake_up_process(sub_thread); |
| 2288 | wait_for_completion(&wfc.completion); |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 2289 | return wfc.ret; |
| 2290 | } |
| 2291 | EXPORT_SYMBOL_GPL(work_on_cpu); |
| 2292 | #endif /* CONFIG_SMP */ |
| 2293 | |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2294 | #ifdef CONFIG_FREEZER |
| 2295 | |
| 2296 | /** |
| 2297 | * freeze_workqueues_begin - begin freezing workqueues |
| 2298 | * |
| 2299 | * Start freezing workqueues. After this function returns, all |
| 2300 | * freezeable workqueues will queue new works to their frozen_works |
| 2301 | * list instead of the cwq ones. |
| 2302 | * |
| 2303 | * CONTEXT: |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2304 | * Grabs and releases workqueue_lock and gcwq->lock's. |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2305 | */ |
| 2306 | void freeze_workqueues_begin(void) |
| 2307 | { |
| 2308 | struct workqueue_struct *wq; |
| 2309 | unsigned int cpu; |
| 2310 | |
| 2311 | spin_lock(&workqueue_lock); |
| 2312 | |
| 2313 | BUG_ON(workqueue_freezing); |
| 2314 | workqueue_freezing = true; |
| 2315 | |
| 2316 | for_each_possible_cpu(cpu) { |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2317 | struct global_cwq *gcwq = get_gcwq(cpu); |
| 2318 | |
| 2319 | spin_lock_irq(&gcwq->lock); |
| 2320 | |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2321 | BUG_ON(gcwq->flags & GCWQ_FREEZING); |
| 2322 | gcwq->flags |= GCWQ_FREEZING; |
| 2323 | |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2324 | list_for_each_entry(wq, &workqueues, list) { |
| 2325 | struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); |
| 2326 | |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2327 | if (wq->flags & WQ_FREEZEABLE) |
| 2328 | cwq->max_active = 0; |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2329 | } |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2330 | |
| 2331 | spin_unlock_irq(&gcwq->lock); |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2332 | } |
| 2333 | |
| 2334 | spin_unlock(&workqueue_lock); |
| 2335 | } |
| 2336 | |
| 2337 | /** |
| 2338 | * freeze_workqueues_busy - are freezeable workqueues still busy? |
| 2339 | * |
| 2340 | * Check whether freezing is complete. This function must be called |
| 2341 | * between freeze_workqueues_begin() and thaw_workqueues(). |
| 2342 | * |
| 2343 | * CONTEXT: |
| 2344 | * Grabs and releases workqueue_lock. |
| 2345 | * |
| 2346 | * RETURNS: |
| 2347 | * %true if some freezeable workqueues are still busy. %false if |
| 2348 | * freezing is complete. |
| 2349 | */ |
| 2350 | bool freeze_workqueues_busy(void) |
| 2351 | { |
| 2352 | struct workqueue_struct *wq; |
| 2353 | unsigned int cpu; |
| 2354 | bool busy = false; |
| 2355 | |
| 2356 | spin_lock(&workqueue_lock); |
| 2357 | |
| 2358 | BUG_ON(!workqueue_freezing); |
| 2359 | |
| 2360 | for_each_possible_cpu(cpu) { |
| 2361 | /* |
| 2362 | * nr_active is monotonically decreasing. It's safe |
| 2363 | * to peek without lock. |
| 2364 | */ |
| 2365 | list_for_each_entry(wq, &workqueues, list) { |
| 2366 | struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); |
| 2367 | |
| 2368 | if (!(wq->flags & WQ_FREEZEABLE)) |
| 2369 | continue; |
| 2370 | |
| 2371 | BUG_ON(cwq->nr_active < 0); |
| 2372 | if (cwq->nr_active) { |
| 2373 | busy = true; |
| 2374 | goto out_unlock; |
| 2375 | } |
| 2376 | } |
| 2377 | } |
| 2378 | out_unlock: |
| 2379 | spin_unlock(&workqueue_lock); |
| 2380 | return busy; |
| 2381 | } |
| 2382 | |
| 2383 | /** |
| 2384 | * thaw_workqueues - thaw workqueues |
| 2385 | * |
| 2386 | * Thaw workqueues. Normal queueing is restored and all collected |
| 2387 | * frozen works are transferred to their respective cwq worklists. |
| 2388 | * |
| 2389 | * CONTEXT: |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2390 | * Grabs and releases workqueue_lock and gcwq->lock's. |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2391 | */ |
| 2392 | void thaw_workqueues(void) |
| 2393 | { |
| 2394 | struct workqueue_struct *wq; |
| 2395 | unsigned int cpu; |
| 2396 | |
| 2397 | spin_lock(&workqueue_lock); |
| 2398 | |
| 2399 | if (!workqueue_freezing) |
| 2400 | goto out_unlock; |
| 2401 | |
| 2402 | for_each_possible_cpu(cpu) { |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2403 | struct global_cwq *gcwq = get_gcwq(cpu); |
| 2404 | |
| 2405 | spin_lock_irq(&gcwq->lock); |
| 2406 | |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2407 | BUG_ON(!(gcwq->flags & GCWQ_FREEZING)); |
| 2408 | gcwq->flags &= ~GCWQ_FREEZING; |
| 2409 | |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2410 | list_for_each_entry(wq, &workqueues, list) { |
| 2411 | struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); |
| 2412 | |
| 2413 | if (!(wq->flags & WQ_FREEZEABLE)) |
| 2414 | continue; |
| 2415 | |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2416 | /* restore max_active and repopulate worklist */ |
| 2417 | cwq->max_active = wq->saved_max_active; |
| 2418 | |
| 2419 | while (!list_empty(&cwq->delayed_works) && |
| 2420 | cwq->nr_active < cwq->max_active) |
| 2421 | cwq_activate_first_delayed(cwq); |
| 2422 | |
Tejun Heo | 502ca9d | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 2423 | /* perform delayed unbind from single cpu if empty */ |
| 2424 | if (wq->single_cpu == gcwq->cpu && |
| 2425 | !cwq->nr_active && list_empty(&cwq->delayed_works)) |
| 2426 | cwq_unbind_single_cpu(cwq); |
| 2427 | |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2428 | wake_up_process(cwq->worker->task); |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2429 | } |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2430 | |
| 2431 | spin_unlock_irq(&gcwq->lock); |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2432 | } |
| 2433 | |
| 2434 | workqueue_freezing = false; |
| 2435 | out_unlock: |
| 2436 | spin_unlock(&workqueue_lock); |
| 2437 | } |
| 2438 | #endif /* CONFIG_FREEZER */ |
| 2439 | |
Oleg Nesterov | c12920d | 2007-05-09 02:34:14 -0700 | [diff] [blame] | 2440 | void __init init_workqueues(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | { |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2442 | unsigned int cpu; |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2443 | int i; |
Tejun Heo | c34056a | 2010-06-29 10:07:11 +0200 | [diff] [blame] | 2444 | |
Tejun Heo | 7a22ad7 | 2010-06-29 10:07:13 +0200 | [diff] [blame^] | 2445 | /* |
| 2446 | * The pointer part of work->data is either pointing to the |
| 2447 | * cwq or contains the cpu number the work ran last on. Make |
| 2448 | * sure cpu number won't overflow into kernel pointer area so |
| 2449 | * that they can be distinguished. |
| 2450 | */ |
| 2451 | BUILD_BUG_ON(NR_CPUS << WORK_STRUCT_FLAG_BITS >= PAGE_OFFSET); |
| 2452 | |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2453 | hotcpu_notifier(workqueue_cpu_callback, CPU_PRI_WORKQUEUE); |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2454 | |
| 2455 | /* initialize gcwqs */ |
| 2456 | for_each_possible_cpu(cpu) { |
| 2457 | struct global_cwq *gcwq = get_gcwq(cpu); |
| 2458 | |
| 2459 | spin_lock_init(&gcwq->lock); |
| 2460 | gcwq->cpu = cpu; |
| 2461 | |
Tejun Heo | c8e55f3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2462 | INIT_LIST_HEAD(&gcwq->idle_list); |
| 2463 | for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) |
| 2464 | INIT_HLIST_HEAD(&gcwq->busy_hash[i]); |
| 2465 | |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2466 | ida_init(&gcwq->worker_ida); |
Tejun Heo | db7bccf | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2467 | |
| 2468 | gcwq->trustee_state = TRUSTEE_DONE; |
| 2469 | init_waitqueue_head(&gcwq->trustee_wait); |
Tejun Heo | 8b03ae3 | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 2470 | } |
| 2471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2472 | keventd_wq = create_workqueue("events"); |
| 2473 | BUG_ON(!keventd_wq); |
| 2474 | } |