Commit | Line | Data |
---|---|---|
1da177e4 LT |
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> | |
e1f8e874 | 12 | * Andrew Morton |
1da177e4 LT |
13 | * Kai Petzke <wpp@marie.physik.tu-berlin.de> |
14 | * Theodore Ts'o <tytso@mit.edu> | |
89ada679 | 15 | * |
cde53535 | 16 | * Made to use alloc_percpu by Christoph Lameter. |
1da177e4 LT |
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> | |
1fa44eca | 30 | #include <linux/hardirq.h> |
46934023 | 31 | #include <linux/mempolicy.h> |
341a5958 | 32 | #include <linux/freezer.h> |
d5abe669 PZ |
33 | #include <linux/kallsyms.h> |
34 | #include <linux/debug_locks.h> | |
4e6045f1 | 35 | #include <linux/lockdep.h> |
fb39125f Z |
36 | #define CREATE_TRACE_POINTS |
37 | #include <trace/events/workqueue.h> | |
1da177e4 | 38 | |
4690c4ab TH |
39 | /* |
40 | * Structure fields follow one of the following exclusion rules. | |
41 | * | |
42 | * I: Set during initialization and read-only afterwards. | |
43 | * | |
44 | * L: cwq->lock protected. Access with cwq->lock held. | |
45 | * | |
46 | * W: workqueue_lock protected. | |
47 | */ | |
48 | ||
1da177e4 | 49 | /* |
f756d5e2 NL |
50 | * The per-CPU workqueue (if single thread, we always use the first |
51 | * possible cpu). | |
1da177e4 LT |
52 | */ |
53 | struct cpu_workqueue_struct { | |
54 | ||
55 | spinlock_t lock; | |
56 | ||
1da177e4 LT |
57 | struct list_head worklist; |
58 | wait_queue_head_t more_work; | |
3af24433 | 59 | struct work_struct *current_work; |
1da177e4 | 60 | |
4690c4ab TH |
61 | struct workqueue_struct *wq; /* I: the owning workqueue */ |
62 | struct task_struct *thread; | |
1da177e4 LT |
63 | } ____cacheline_aligned; |
64 | ||
65 | /* | |
66 | * The externally visible workqueue abstraction is an array of | |
67 | * per-CPU workqueues: | |
68 | */ | |
69 | struct workqueue_struct { | |
4690c4ab TH |
70 | struct cpu_workqueue_struct *cpu_wq; /* I: cwq's */ |
71 | struct list_head list; /* W: list of all workqueues */ | |
72 | const char *name; /* I: workqueue name */ | |
cce1a165 | 73 | int singlethread; |
319c2a98 | 74 | int freezeable; /* Freeze threads during suspend */ |
4e6045f1 | 75 | #ifdef CONFIG_LOCKDEP |
4690c4ab | 76 | struct lockdep_map lockdep_map; |
4e6045f1 | 77 | #endif |
1da177e4 LT |
78 | }; |
79 | ||
dc186ad7 TG |
80 | #ifdef CONFIG_DEBUG_OBJECTS_WORK |
81 | ||
82 | static struct debug_obj_descr work_debug_descr; | |
83 | ||
84 | /* | |
85 | * fixup_init is called when: | |
86 | * - an active object is initialized | |
87 | */ | |
88 | static int work_fixup_init(void *addr, enum debug_obj_state state) | |
89 | { | |
90 | struct work_struct *work = addr; | |
91 | ||
92 | switch (state) { | |
93 | case ODEBUG_STATE_ACTIVE: | |
94 | cancel_work_sync(work); | |
95 | debug_object_init(work, &work_debug_descr); | |
96 | return 1; | |
97 | default: | |
98 | return 0; | |
99 | } | |
100 | } | |
101 | ||
102 | /* | |
103 | * fixup_activate is called when: | |
104 | * - an active object is activated | |
105 | * - an unknown object is activated (might be a statically initialized object) | |
106 | */ | |
107 | static int work_fixup_activate(void *addr, enum debug_obj_state state) | |
108 | { | |
109 | struct work_struct *work = addr; | |
110 | ||
111 | switch (state) { | |
112 | ||
113 | case ODEBUG_STATE_NOTAVAILABLE: | |
114 | /* | |
115 | * This is not really a fixup. The work struct was | |
116 | * statically initialized. We just make sure that it | |
117 | * is tracked in the object tracker. | |
118 | */ | |
119 | if (test_bit(WORK_STRUCT_STATIC, work_data_bits(work))) { | |
120 | debug_object_init(work, &work_debug_descr); | |
121 | debug_object_activate(work, &work_debug_descr); | |
122 | return 0; | |
123 | } | |
124 | WARN_ON_ONCE(1); | |
125 | return 0; | |
126 | ||
127 | case ODEBUG_STATE_ACTIVE: | |
128 | WARN_ON(1); | |
129 | ||
130 | default: | |
131 | return 0; | |
132 | } | |
133 | } | |
134 | ||
135 | /* | |
136 | * fixup_free is called when: | |
137 | * - an active object is freed | |
138 | */ | |
139 | static int work_fixup_free(void *addr, enum debug_obj_state state) | |
140 | { | |
141 | struct work_struct *work = addr; | |
142 | ||
143 | switch (state) { | |
144 | case ODEBUG_STATE_ACTIVE: | |
145 | cancel_work_sync(work); | |
146 | debug_object_free(work, &work_debug_descr); | |
147 | return 1; | |
148 | default: | |
149 | return 0; | |
150 | } | |
151 | } | |
152 | ||
153 | static struct debug_obj_descr work_debug_descr = { | |
154 | .name = "work_struct", | |
155 | .fixup_init = work_fixup_init, | |
156 | .fixup_activate = work_fixup_activate, | |
157 | .fixup_free = work_fixup_free, | |
158 | }; | |
159 | ||
160 | static inline void debug_work_activate(struct work_struct *work) | |
161 | { | |
162 | debug_object_activate(work, &work_debug_descr); | |
163 | } | |
164 | ||
165 | static inline void debug_work_deactivate(struct work_struct *work) | |
166 | { | |
167 | debug_object_deactivate(work, &work_debug_descr); | |
168 | } | |
169 | ||
170 | void __init_work(struct work_struct *work, int onstack) | |
171 | { | |
172 | if (onstack) | |
173 | debug_object_init_on_stack(work, &work_debug_descr); | |
174 | else | |
175 | debug_object_init(work, &work_debug_descr); | |
176 | } | |
177 | EXPORT_SYMBOL_GPL(__init_work); | |
178 | ||
179 | void destroy_work_on_stack(struct work_struct *work) | |
180 | { | |
181 | debug_object_free(work, &work_debug_descr); | |
182 | } | |
183 | EXPORT_SYMBOL_GPL(destroy_work_on_stack); | |
184 | ||
185 | #else | |
186 | static inline void debug_work_activate(struct work_struct *work) { } | |
187 | static inline void debug_work_deactivate(struct work_struct *work) { } | |
188 | #endif | |
189 | ||
95402b38 GS |
190 | /* Serializes the accesses to the list of workqueues. */ |
191 | static DEFINE_SPINLOCK(workqueue_lock); | |
1da177e4 LT |
192 | static LIST_HEAD(workqueues); |
193 | ||
3af24433 | 194 | static int singlethread_cpu __read_mostly; |
e7577c50 | 195 | static const struct cpumask *cpu_singlethread_map __read_mostly; |
14441960 ON |
196 | /* |
197 | * _cpu_down() first removes CPU from cpu_online_map, then CPU_DEAD | |
198 | * flushes cwq->worklist. This means that flush_workqueue/wait_on_work | |
199 | * which comes in between can't use for_each_online_cpu(). We could | |
200 | * use cpu_possible_map, the cpumask below is more a documentation | |
201 | * than optimization. | |
202 | */ | |
e7577c50 | 203 | static cpumask_var_t cpu_populated_map __read_mostly; |
f756d5e2 | 204 | |
1da177e4 | 205 | /* If it's single threaded, it isn't in the list of workqueues. */ |
6cc88bc4 | 206 | static inline int is_wq_single_threaded(struct workqueue_struct *wq) |
1da177e4 | 207 | { |
cce1a165 | 208 | return wq->singlethread; |
1da177e4 LT |
209 | } |
210 | ||
e7577c50 | 211 | static const struct cpumask *wq_cpu_map(struct workqueue_struct *wq) |
b1f4ec17 | 212 | { |
6cc88bc4 | 213 | return is_wq_single_threaded(wq) |
e7577c50 | 214 | ? cpu_singlethread_map : cpu_populated_map; |
b1f4ec17 ON |
215 | } |
216 | ||
4690c4ab TH |
217 | static struct cpu_workqueue_struct *get_cwq(unsigned int cpu, |
218 | struct workqueue_struct *wq) | |
a848e3b6 | 219 | { |
6cc88bc4 | 220 | if (unlikely(is_wq_single_threaded(wq))) |
a848e3b6 ON |
221 | cpu = singlethread_cpu; |
222 | return per_cpu_ptr(wq->cpu_wq, cpu); | |
223 | } | |
224 | ||
4594bf15 DH |
225 | /* |
226 | * Set the workqueue on which a work item is to be run | |
227 | * - Must *only* be called if the pending flag is set | |
228 | */ | |
ed7c0fee | 229 | static inline void set_wq_data(struct work_struct *work, |
4690c4ab TH |
230 | struct cpu_workqueue_struct *cwq, |
231 | unsigned long extra_flags) | |
365970a1 | 232 | { |
4594bf15 | 233 | BUG_ON(!work_pending(work)); |
365970a1 | 234 | |
4690c4ab TH |
235 | atomic_long_set(&work->data, (unsigned long)cwq | work_static(work) | |
236 | (1UL << WORK_STRUCT_PENDING) | extra_flags); | |
365970a1 DH |
237 | } |
238 | ||
4d707b9f ON |
239 | /* |
240 | * Clear WORK_STRUCT_PENDING and the workqueue on which it was queued. | |
241 | */ | |
242 | static inline void clear_wq_data(struct work_struct *work) | |
243 | { | |
4690c4ab | 244 | atomic_long_set(&work->data, work_static(work)); |
4d707b9f ON |
245 | } |
246 | ||
ed7c0fee ON |
247 | static inline |
248 | struct cpu_workqueue_struct *get_wq_data(struct work_struct *work) | |
365970a1 | 249 | { |
a08727ba | 250 | return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK); |
365970a1 DH |
251 | } |
252 | ||
4690c4ab TH |
253 | /** |
254 | * insert_work - insert a work into cwq | |
255 | * @cwq: cwq @work belongs to | |
256 | * @work: work to insert | |
257 | * @head: insertion point | |
258 | * @extra_flags: extra WORK_STRUCT_* flags to set | |
259 | * | |
260 | * Insert @work into @cwq after @head. | |
261 | * | |
262 | * CONTEXT: | |
263 | * spin_lock_irq(cwq->lock). | |
264 | */ | |
b89deed3 | 265 | static void insert_work(struct cpu_workqueue_struct *cwq, |
4690c4ab TH |
266 | struct work_struct *work, struct list_head *head, |
267 | unsigned int extra_flags) | |
b89deed3 | 268 | { |
e1d8aa9f FW |
269 | trace_workqueue_insertion(cwq->thread, work); |
270 | ||
4690c4ab TH |
271 | /* we own @work, set data and link */ |
272 | set_wq_data(work, cwq, extra_flags); | |
273 | ||
6e84d644 ON |
274 | /* |
275 | * Ensure that we get the right work->data if we see the | |
276 | * result of list_add() below, see try_to_grab_pending(). | |
277 | */ | |
278 | smp_wmb(); | |
4690c4ab | 279 | |
1a4d9b0a | 280 | list_add_tail(&work->entry, head); |
b89deed3 ON |
281 | wake_up(&cwq->more_work); |
282 | } | |
283 | ||
4690c4ab | 284 | static void __queue_work(unsigned int cpu, struct workqueue_struct *wq, |
1da177e4 LT |
285 | struct work_struct *work) |
286 | { | |
4690c4ab | 287 | struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); |
1da177e4 LT |
288 | unsigned long flags; |
289 | ||
dc186ad7 | 290 | debug_work_activate(work); |
1da177e4 | 291 | spin_lock_irqsave(&cwq->lock, flags); |
4690c4ab TH |
292 | BUG_ON(!list_empty(&work->entry)); |
293 | insert_work(cwq, work, &cwq->worklist, 0); | |
1da177e4 LT |
294 | spin_unlock_irqrestore(&cwq->lock, flags); |
295 | } | |
296 | ||
0fcb78c2 REB |
297 | /** |
298 | * queue_work - queue work on a workqueue | |
299 | * @wq: workqueue to use | |
300 | * @work: work to queue | |
301 | * | |
057647fc | 302 | * Returns 0 if @work was already on a queue, non-zero otherwise. |
1da177e4 | 303 | * |
00dfcaf7 ON |
304 | * We queue the work to the CPU on which it was submitted, but if the CPU dies |
305 | * it can be processed by another CPU. | |
1da177e4 | 306 | */ |
7ad5b3a5 | 307 | int queue_work(struct workqueue_struct *wq, struct work_struct *work) |
1da177e4 | 308 | { |
ef1ca236 ON |
309 | int ret; |
310 | ||
311 | ret = queue_work_on(get_cpu(), wq, work); | |
312 | put_cpu(); | |
313 | ||
1da177e4 LT |
314 | return ret; |
315 | } | |
ae90dd5d | 316 | EXPORT_SYMBOL_GPL(queue_work); |
1da177e4 | 317 | |
c1a220e7 ZR |
318 | /** |
319 | * queue_work_on - queue work on specific cpu | |
320 | * @cpu: CPU number to execute work on | |
321 | * @wq: workqueue to use | |
322 | * @work: work to queue | |
323 | * | |
324 | * Returns 0 if @work was already on a queue, non-zero otherwise. | |
325 | * | |
326 | * We queue the work to a specific CPU, the caller must ensure it | |
327 | * can't go away. | |
328 | */ | |
329 | int | |
330 | queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work) | |
331 | { | |
332 | int ret = 0; | |
333 | ||
334 | if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) { | |
4690c4ab | 335 | __queue_work(cpu, wq, work); |
c1a220e7 ZR |
336 | ret = 1; |
337 | } | |
338 | return ret; | |
339 | } | |
340 | EXPORT_SYMBOL_GPL(queue_work_on); | |
341 | ||
6d141c3f | 342 | static void delayed_work_timer_fn(unsigned long __data) |
1da177e4 | 343 | { |
52bad64d | 344 | struct delayed_work *dwork = (struct delayed_work *)__data; |
ed7c0fee | 345 | struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work); |
1da177e4 | 346 | |
4690c4ab | 347 | __queue_work(smp_processor_id(), cwq->wq, &dwork->work); |
1da177e4 LT |
348 | } |
349 | ||
0fcb78c2 REB |
350 | /** |
351 | * queue_delayed_work - queue work on a workqueue after delay | |
352 | * @wq: workqueue to use | |
af9997e4 | 353 | * @dwork: delayable work to queue |
0fcb78c2 REB |
354 | * @delay: number of jiffies to wait before queueing |
355 | * | |
057647fc | 356 | * Returns 0 if @work was already on a queue, non-zero otherwise. |
0fcb78c2 | 357 | */ |
7ad5b3a5 | 358 | int queue_delayed_work(struct workqueue_struct *wq, |
52bad64d | 359 | struct delayed_work *dwork, unsigned long delay) |
1da177e4 | 360 | { |
52bad64d | 361 | if (delay == 0) |
63bc0362 | 362 | return queue_work(wq, &dwork->work); |
1da177e4 | 363 | |
63bc0362 | 364 | return queue_delayed_work_on(-1, wq, dwork, delay); |
1da177e4 | 365 | } |
ae90dd5d | 366 | EXPORT_SYMBOL_GPL(queue_delayed_work); |
1da177e4 | 367 | |
0fcb78c2 REB |
368 | /** |
369 | * queue_delayed_work_on - queue work on specific CPU after delay | |
370 | * @cpu: CPU number to execute work on | |
371 | * @wq: workqueue to use | |
af9997e4 | 372 | * @dwork: work to queue |
0fcb78c2 REB |
373 | * @delay: number of jiffies to wait before queueing |
374 | * | |
057647fc | 375 | * Returns 0 if @work was already on a queue, non-zero otherwise. |
0fcb78c2 | 376 | */ |
7a6bc1cd | 377 | int queue_delayed_work_on(int cpu, struct workqueue_struct *wq, |
52bad64d | 378 | struct delayed_work *dwork, unsigned long delay) |
7a6bc1cd VP |
379 | { |
380 | int ret = 0; | |
52bad64d DH |
381 | struct timer_list *timer = &dwork->timer; |
382 | struct work_struct *work = &dwork->work; | |
7a6bc1cd | 383 | |
a08727ba | 384 | if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) { |
7a6bc1cd VP |
385 | BUG_ON(timer_pending(timer)); |
386 | BUG_ON(!list_empty(&work->entry)); | |
387 | ||
8a3e77cc AL |
388 | timer_stats_timer_set_start_info(&dwork->timer); |
389 | ||
ed7c0fee | 390 | /* This stores cwq for the moment, for the timer_fn */ |
4690c4ab | 391 | set_wq_data(work, get_cwq(raw_smp_processor_id(), wq), 0); |
7a6bc1cd | 392 | timer->expires = jiffies + delay; |
52bad64d | 393 | timer->data = (unsigned long)dwork; |
7a6bc1cd | 394 | timer->function = delayed_work_timer_fn; |
63bc0362 ON |
395 | |
396 | if (unlikely(cpu >= 0)) | |
397 | add_timer_on(timer, cpu); | |
398 | else | |
399 | add_timer(timer); | |
7a6bc1cd VP |
400 | ret = 1; |
401 | } | |
402 | return ret; | |
403 | } | |
ae90dd5d | 404 | EXPORT_SYMBOL_GPL(queue_delayed_work_on); |
1da177e4 | 405 | |
858119e1 | 406 | static void run_workqueue(struct cpu_workqueue_struct *cwq) |
1da177e4 | 407 | { |
f293ea92 | 408 | spin_lock_irq(&cwq->lock); |
1da177e4 LT |
409 | while (!list_empty(&cwq->worklist)) { |
410 | struct work_struct *work = list_entry(cwq->worklist.next, | |
411 | struct work_struct, entry); | |
6bb49e59 | 412 | work_func_t f = work->func; |
4e6045f1 JB |
413 | #ifdef CONFIG_LOCKDEP |
414 | /* | |
415 | * It is permissible to free the struct work_struct | |
416 | * from inside the function that is called from it, | |
417 | * this we need to take into account for lockdep too. | |
418 | * To avoid bogus "held lock freed" warnings as well | |
419 | * as problems when looking into work->lockdep_map, | |
420 | * make a copy and use that here. | |
421 | */ | |
422 | struct lockdep_map lockdep_map = work->lockdep_map; | |
423 | #endif | |
e1d8aa9f | 424 | trace_workqueue_execution(cwq->thread, work); |
dc186ad7 | 425 | debug_work_deactivate(work); |
b89deed3 | 426 | cwq->current_work = work; |
1da177e4 | 427 | list_del_init(cwq->worklist.next); |
f293ea92 | 428 | spin_unlock_irq(&cwq->lock); |
1da177e4 | 429 | |
365970a1 | 430 | BUG_ON(get_wq_data(work) != cwq); |
23b2e599 | 431 | work_clear_pending(work); |
3295f0ef IM |
432 | lock_map_acquire(&cwq->wq->lockdep_map); |
433 | lock_map_acquire(&lockdep_map); | |
65f27f38 | 434 | f(work); |
3295f0ef IM |
435 | lock_map_release(&lockdep_map); |
436 | lock_map_release(&cwq->wq->lockdep_map); | |
1da177e4 | 437 | |
d5abe669 PZ |
438 | if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { |
439 | printk(KERN_ERR "BUG: workqueue leaked lock or atomic: " | |
440 | "%s/0x%08x/%d\n", | |
441 | current->comm, preempt_count(), | |
ba25f9dc | 442 | task_pid_nr(current)); |
d5abe669 PZ |
443 | printk(KERN_ERR " last function: "); |
444 | print_symbol("%s\n", (unsigned long)f); | |
445 | debug_show_held_locks(current); | |
446 | dump_stack(); | |
447 | } | |
448 | ||
f293ea92 | 449 | spin_lock_irq(&cwq->lock); |
b89deed3 | 450 | cwq->current_work = NULL; |
1da177e4 | 451 | } |
f293ea92 | 452 | spin_unlock_irq(&cwq->lock); |
1da177e4 LT |
453 | } |
454 | ||
4690c4ab TH |
455 | /** |
456 | * worker_thread - the worker thread function | |
457 | * @__cwq: cwq to serve | |
458 | * | |
459 | * The cwq worker thread function. | |
460 | */ | |
1da177e4 LT |
461 | static int worker_thread(void *__cwq) |
462 | { | |
463 | struct cpu_workqueue_struct *cwq = __cwq; | |
3af24433 | 464 | DEFINE_WAIT(wait); |
1da177e4 | 465 | |
83144186 RW |
466 | if (cwq->wq->freezeable) |
467 | set_freezable(); | |
1da177e4 | 468 | |
3af24433 | 469 | for (;;) { |
3af24433 | 470 | prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE); |
14441960 ON |
471 | if (!freezing(current) && |
472 | !kthread_should_stop() && | |
473 | list_empty(&cwq->worklist)) | |
1da177e4 | 474 | schedule(); |
3af24433 ON |
475 | finish_wait(&cwq->more_work, &wait); |
476 | ||
85f4186a ON |
477 | try_to_freeze(); |
478 | ||
14441960 | 479 | if (kthread_should_stop()) |
3af24433 | 480 | break; |
1da177e4 | 481 | |
3af24433 | 482 | run_workqueue(cwq); |
1da177e4 | 483 | } |
3af24433 | 484 | |
1da177e4 LT |
485 | return 0; |
486 | } | |
487 | ||
fc2e4d70 ON |
488 | struct wq_barrier { |
489 | struct work_struct work; | |
490 | struct completion done; | |
491 | }; | |
492 | ||
493 | static void wq_barrier_func(struct work_struct *work) | |
494 | { | |
495 | struct wq_barrier *barr = container_of(work, struct wq_barrier, work); | |
496 | complete(&barr->done); | |
497 | } | |
498 | ||
4690c4ab TH |
499 | /** |
500 | * insert_wq_barrier - insert a barrier work | |
501 | * @cwq: cwq to insert barrier into | |
502 | * @barr: wq_barrier to insert | |
503 | * @head: insertion point | |
504 | * | |
505 | * Insert barrier @barr into @cwq before @head. | |
506 | * | |
507 | * CONTEXT: | |
508 | * spin_lock_irq(cwq->lock). | |
509 | */ | |
83c22520 | 510 | static void insert_wq_barrier(struct cpu_workqueue_struct *cwq, |
1a4d9b0a | 511 | struct wq_barrier *barr, struct list_head *head) |
fc2e4d70 | 512 | { |
dc186ad7 TG |
513 | /* |
514 | * debugobject calls are safe here even with cwq->lock locked | |
515 | * as we know for sure that this will not trigger any of the | |
516 | * checks and call back into the fixup functions where we | |
517 | * might deadlock. | |
518 | */ | |
519 | INIT_WORK_ON_STACK(&barr->work, wq_barrier_func); | |
fc2e4d70 | 520 | __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work)); |
fc2e4d70 | 521 | init_completion(&barr->done); |
83c22520 | 522 | |
dc186ad7 | 523 | debug_work_activate(&barr->work); |
4690c4ab | 524 | insert_work(cwq, &barr->work, head, 0); |
fc2e4d70 ON |
525 | } |
526 | ||
14441960 | 527 | static int flush_cpu_workqueue(struct cpu_workqueue_struct *cwq) |
1da177e4 | 528 | { |
2355b70f LJ |
529 | int active = 0; |
530 | struct wq_barrier barr; | |
1da177e4 | 531 | |
2355b70f | 532 | WARN_ON(cwq->thread == current); |
1da177e4 | 533 | |
2355b70f LJ |
534 | spin_lock_irq(&cwq->lock); |
535 | if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) { | |
536 | insert_wq_barrier(cwq, &barr, &cwq->worklist); | |
537 | active = 1; | |
1da177e4 | 538 | } |
2355b70f LJ |
539 | spin_unlock_irq(&cwq->lock); |
540 | ||
dc186ad7 | 541 | if (active) { |
2355b70f | 542 | wait_for_completion(&barr.done); |
dc186ad7 TG |
543 | destroy_work_on_stack(&barr.work); |
544 | } | |
14441960 ON |
545 | |
546 | return active; | |
1da177e4 LT |
547 | } |
548 | ||
0fcb78c2 | 549 | /** |
1da177e4 | 550 | * flush_workqueue - ensure that any scheduled work has run to completion. |
0fcb78c2 | 551 | * @wq: workqueue to flush |
1da177e4 LT |
552 | * |
553 | * Forces execution of the workqueue and blocks until its completion. | |
554 | * This is typically used in driver shutdown handlers. | |
555 | * | |
fc2e4d70 ON |
556 | * We sleep until all works which were queued on entry have been handled, |
557 | * but we are not livelocked by new incoming ones. | |
1da177e4 | 558 | */ |
7ad5b3a5 | 559 | void flush_workqueue(struct workqueue_struct *wq) |
1da177e4 | 560 | { |
e7577c50 | 561 | const struct cpumask *cpu_map = wq_cpu_map(wq); |
cce1a165 | 562 | int cpu; |
1da177e4 | 563 | |
b1f4ec17 | 564 | might_sleep(); |
3295f0ef IM |
565 | lock_map_acquire(&wq->lockdep_map); |
566 | lock_map_release(&wq->lockdep_map); | |
aa85ea5b | 567 | for_each_cpu(cpu, cpu_map) |
b1f4ec17 | 568 | flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu)); |
1da177e4 | 569 | } |
ae90dd5d | 570 | EXPORT_SYMBOL_GPL(flush_workqueue); |
1da177e4 | 571 | |
db700897 ON |
572 | /** |
573 | * flush_work - block until a work_struct's callback has terminated | |
574 | * @work: the work which is to be flushed | |
575 | * | |
a67da70d ON |
576 | * Returns false if @work has already terminated. |
577 | * | |
db700897 ON |
578 | * It is expected that, prior to calling flush_work(), the caller has |
579 | * arranged for the work to not be requeued, otherwise it doesn't make | |
580 | * sense to use this function. | |
581 | */ | |
582 | int flush_work(struct work_struct *work) | |
583 | { | |
584 | struct cpu_workqueue_struct *cwq; | |
585 | struct list_head *prev; | |
586 | struct wq_barrier barr; | |
587 | ||
588 | might_sleep(); | |
589 | cwq = get_wq_data(work); | |
590 | if (!cwq) | |
591 | return 0; | |
592 | ||
3295f0ef IM |
593 | lock_map_acquire(&cwq->wq->lockdep_map); |
594 | lock_map_release(&cwq->wq->lockdep_map); | |
a67da70d | 595 | |
db700897 ON |
596 | spin_lock_irq(&cwq->lock); |
597 | if (!list_empty(&work->entry)) { | |
598 | /* | |
599 | * See the comment near try_to_grab_pending()->smp_rmb(). | |
600 | * If it was re-queued under us we are not going to wait. | |
601 | */ | |
602 | smp_rmb(); | |
603 | if (unlikely(cwq != get_wq_data(work))) | |
4690c4ab | 604 | goto already_gone; |
db700897 ON |
605 | prev = &work->entry; |
606 | } else { | |
607 | if (cwq->current_work != work) | |
4690c4ab | 608 | goto already_gone; |
db700897 ON |
609 | prev = &cwq->worklist; |
610 | } | |
611 | insert_wq_barrier(cwq, &barr, prev->next); | |
db700897 | 612 | |
4690c4ab | 613 | spin_unlock_irq(&cwq->lock); |
db700897 | 614 | wait_for_completion(&barr.done); |
dc186ad7 | 615 | destroy_work_on_stack(&barr.work); |
db700897 | 616 | return 1; |
4690c4ab TH |
617 | already_gone: |
618 | spin_unlock_irq(&cwq->lock); | |
619 | return 0; | |
db700897 ON |
620 | } |
621 | EXPORT_SYMBOL_GPL(flush_work); | |
622 | ||
6e84d644 | 623 | /* |
1f1f642e | 624 | * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit, |
6e84d644 ON |
625 | * so this work can't be re-armed in any way. |
626 | */ | |
627 | static int try_to_grab_pending(struct work_struct *work) | |
628 | { | |
629 | struct cpu_workqueue_struct *cwq; | |
1f1f642e | 630 | int ret = -1; |
6e84d644 ON |
631 | |
632 | if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) | |
1f1f642e | 633 | return 0; |
6e84d644 ON |
634 | |
635 | /* | |
636 | * The queueing is in progress, or it is already queued. Try to | |
637 | * steal it from ->worklist without clearing WORK_STRUCT_PENDING. | |
638 | */ | |
639 | ||
640 | cwq = get_wq_data(work); | |
641 | if (!cwq) | |
642 | return ret; | |
643 | ||
644 | spin_lock_irq(&cwq->lock); | |
645 | if (!list_empty(&work->entry)) { | |
646 | /* | |
647 | * This work is queued, but perhaps we locked the wrong cwq. | |
648 | * In that case we must see the new value after rmb(), see | |
649 | * insert_work()->wmb(). | |
650 | */ | |
651 | smp_rmb(); | |
652 | if (cwq == get_wq_data(work)) { | |
dc186ad7 | 653 | debug_work_deactivate(work); |
6e84d644 ON |
654 | list_del_init(&work->entry); |
655 | ret = 1; | |
656 | } | |
657 | } | |
658 | spin_unlock_irq(&cwq->lock); | |
659 | ||
660 | return ret; | |
661 | } | |
662 | ||
663 | static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq, | |
b89deed3 ON |
664 | struct work_struct *work) |
665 | { | |
666 | struct wq_barrier barr; | |
667 | int running = 0; | |
668 | ||
669 | spin_lock_irq(&cwq->lock); | |
670 | if (unlikely(cwq->current_work == work)) { | |
1a4d9b0a | 671 | insert_wq_barrier(cwq, &barr, cwq->worklist.next); |
b89deed3 ON |
672 | running = 1; |
673 | } | |
674 | spin_unlock_irq(&cwq->lock); | |
675 | ||
dc186ad7 | 676 | if (unlikely(running)) { |
b89deed3 | 677 | wait_for_completion(&barr.done); |
dc186ad7 TG |
678 | destroy_work_on_stack(&barr.work); |
679 | } | |
b89deed3 ON |
680 | } |
681 | ||
6e84d644 | 682 | static void wait_on_work(struct work_struct *work) |
b89deed3 ON |
683 | { |
684 | struct cpu_workqueue_struct *cwq; | |
28e53bdd | 685 | struct workqueue_struct *wq; |
e7577c50 | 686 | const struct cpumask *cpu_map; |
b1f4ec17 | 687 | int cpu; |
b89deed3 | 688 | |
f293ea92 ON |
689 | might_sleep(); |
690 | ||
3295f0ef IM |
691 | lock_map_acquire(&work->lockdep_map); |
692 | lock_map_release(&work->lockdep_map); | |
4e6045f1 | 693 | |
b89deed3 | 694 | cwq = get_wq_data(work); |
b89deed3 | 695 | if (!cwq) |
3af24433 | 696 | return; |
b89deed3 | 697 | |
28e53bdd ON |
698 | wq = cwq->wq; |
699 | cpu_map = wq_cpu_map(wq); | |
700 | ||
aa85ea5b | 701 | for_each_cpu(cpu, cpu_map) |
4690c4ab | 702 | wait_on_cpu_work(get_cwq(cpu, wq), work); |
6e84d644 ON |
703 | } |
704 | ||
1f1f642e ON |
705 | static int __cancel_work_timer(struct work_struct *work, |
706 | struct timer_list* timer) | |
707 | { | |
708 | int ret; | |
709 | ||
710 | do { | |
711 | ret = (timer && likely(del_timer(timer))); | |
712 | if (!ret) | |
713 | ret = try_to_grab_pending(work); | |
714 | wait_on_work(work); | |
715 | } while (unlikely(ret < 0)); | |
716 | ||
4d707b9f | 717 | clear_wq_data(work); |
1f1f642e ON |
718 | return ret; |
719 | } | |
720 | ||
6e84d644 ON |
721 | /** |
722 | * cancel_work_sync - block until a work_struct's callback has terminated | |
723 | * @work: the work which is to be flushed | |
724 | * | |
1f1f642e ON |
725 | * Returns true if @work was pending. |
726 | * | |
6e84d644 ON |
727 | * cancel_work_sync() will cancel the work if it is queued. If the work's |
728 | * callback appears to be running, cancel_work_sync() will block until it | |
729 | * has completed. | |
730 | * | |
731 | * It is possible to use this function if the work re-queues itself. It can | |
732 | * cancel the work even if it migrates to another workqueue, however in that | |
733 | * case it only guarantees that work->func() has completed on the last queued | |
734 | * workqueue. | |
735 | * | |
736 | * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not | |
737 | * pending, otherwise it goes into a busy-wait loop until the timer expires. | |
738 | * | |
739 | * The caller must ensure that workqueue_struct on which this work was last | |
740 | * queued can't be destroyed before this function returns. | |
741 | */ | |
1f1f642e | 742 | int cancel_work_sync(struct work_struct *work) |
6e84d644 | 743 | { |
1f1f642e | 744 | return __cancel_work_timer(work, NULL); |
b89deed3 | 745 | } |
28e53bdd | 746 | EXPORT_SYMBOL_GPL(cancel_work_sync); |
b89deed3 | 747 | |
6e84d644 | 748 | /** |
f5a421a4 | 749 | * cancel_delayed_work_sync - reliably kill off a delayed work. |
6e84d644 ON |
750 | * @dwork: the delayed work struct |
751 | * | |
1f1f642e ON |
752 | * Returns true if @dwork was pending. |
753 | * | |
6e84d644 ON |
754 | * It is possible to use this function if @dwork rearms itself via queue_work() |
755 | * or queue_delayed_work(). See also the comment for cancel_work_sync(). | |
756 | */ | |
1f1f642e | 757 | int cancel_delayed_work_sync(struct delayed_work *dwork) |
6e84d644 | 758 | { |
1f1f642e | 759 | return __cancel_work_timer(&dwork->work, &dwork->timer); |
6e84d644 | 760 | } |
f5a421a4 | 761 | EXPORT_SYMBOL(cancel_delayed_work_sync); |
1da177e4 | 762 | |
6e84d644 | 763 | static struct workqueue_struct *keventd_wq __read_mostly; |
1da177e4 | 764 | |
0fcb78c2 REB |
765 | /** |
766 | * schedule_work - put work task in global workqueue | |
767 | * @work: job to be done | |
768 | * | |
5b0f437d BVA |
769 | * Returns zero if @work was already on the kernel-global workqueue and |
770 | * non-zero otherwise. | |
771 | * | |
772 | * This puts a job in the kernel-global workqueue if it was not already | |
773 | * queued and leaves it in the same position on the kernel-global | |
774 | * workqueue otherwise. | |
0fcb78c2 | 775 | */ |
7ad5b3a5 | 776 | int schedule_work(struct work_struct *work) |
1da177e4 LT |
777 | { |
778 | return queue_work(keventd_wq, work); | |
779 | } | |
ae90dd5d | 780 | EXPORT_SYMBOL(schedule_work); |
1da177e4 | 781 | |
c1a220e7 ZR |
782 | /* |
783 | * schedule_work_on - put work task on a specific cpu | |
784 | * @cpu: cpu to put the work task on | |
785 | * @work: job to be done | |
786 | * | |
787 | * This puts a job on a specific cpu | |
788 | */ | |
789 | int schedule_work_on(int cpu, struct work_struct *work) | |
790 | { | |
791 | return queue_work_on(cpu, keventd_wq, work); | |
792 | } | |
793 | EXPORT_SYMBOL(schedule_work_on); | |
794 | ||
0fcb78c2 REB |
795 | /** |
796 | * schedule_delayed_work - put work task in global workqueue after delay | |
52bad64d DH |
797 | * @dwork: job to be done |
798 | * @delay: number of jiffies to wait or 0 for immediate execution | |
0fcb78c2 REB |
799 | * |
800 | * After waiting for a given time this puts a job in the kernel-global | |
801 | * workqueue. | |
802 | */ | |
7ad5b3a5 | 803 | int schedule_delayed_work(struct delayed_work *dwork, |
82f67cd9 | 804 | unsigned long delay) |
1da177e4 | 805 | { |
52bad64d | 806 | return queue_delayed_work(keventd_wq, dwork, delay); |
1da177e4 | 807 | } |
ae90dd5d | 808 | EXPORT_SYMBOL(schedule_delayed_work); |
1da177e4 | 809 | |
8c53e463 LT |
810 | /** |
811 | * flush_delayed_work - block until a dwork_struct's callback has terminated | |
812 | * @dwork: the delayed work which is to be flushed | |
813 | * | |
814 | * Any timeout is cancelled, and any pending work is run immediately. | |
815 | */ | |
816 | void flush_delayed_work(struct delayed_work *dwork) | |
817 | { | |
818 | if (del_timer_sync(&dwork->timer)) { | |
4690c4ab TH |
819 | __queue_work(get_cpu(), get_wq_data(&dwork->work)->wq, |
820 | &dwork->work); | |
8c53e463 LT |
821 | put_cpu(); |
822 | } | |
823 | flush_work(&dwork->work); | |
824 | } | |
825 | EXPORT_SYMBOL(flush_delayed_work); | |
826 | ||
0fcb78c2 REB |
827 | /** |
828 | * schedule_delayed_work_on - queue work in global workqueue on CPU after delay | |
829 | * @cpu: cpu to use | |
52bad64d | 830 | * @dwork: job to be done |
0fcb78c2 REB |
831 | * @delay: number of jiffies to wait |
832 | * | |
833 | * After waiting for a given time this puts a job in the kernel-global | |
834 | * workqueue on the specified CPU. | |
835 | */ | |
1da177e4 | 836 | int schedule_delayed_work_on(int cpu, |
52bad64d | 837 | struct delayed_work *dwork, unsigned long delay) |
1da177e4 | 838 | { |
52bad64d | 839 | return queue_delayed_work_on(cpu, keventd_wq, dwork, delay); |
1da177e4 | 840 | } |
ae90dd5d | 841 | EXPORT_SYMBOL(schedule_delayed_work_on); |
1da177e4 | 842 | |
b6136773 AM |
843 | /** |
844 | * schedule_on_each_cpu - call a function on each online CPU from keventd | |
845 | * @func: the function to call | |
b6136773 AM |
846 | * |
847 | * Returns zero on success. | |
848 | * Returns -ve errno on failure. | |
849 | * | |
b6136773 AM |
850 | * schedule_on_each_cpu() is very slow. |
851 | */ | |
65f27f38 | 852 | int schedule_on_each_cpu(work_func_t func) |
15316ba8 CL |
853 | { |
854 | int cpu; | |
65a64464 | 855 | int orig = -1; |
b6136773 | 856 | struct work_struct *works; |
15316ba8 | 857 | |
b6136773 AM |
858 | works = alloc_percpu(struct work_struct); |
859 | if (!works) | |
15316ba8 | 860 | return -ENOMEM; |
b6136773 | 861 | |
93981800 TH |
862 | get_online_cpus(); |
863 | ||
65a64464 | 864 | /* |
93981800 TH |
865 | * When running in keventd don't schedule a work item on |
866 | * itself. Can just call directly because the work queue is | |
867 | * already bound. This also is faster. | |
65a64464 | 868 | */ |
93981800 | 869 | if (current_is_keventd()) |
65a64464 | 870 | orig = raw_smp_processor_id(); |
65a64464 | 871 | |
15316ba8 | 872 | for_each_online_cpu(cpu) { |
9bfb1839 IM |
873 | struct work_struct *work = per_cpu_ptr(works, cpu); |
874 | ||
875 | INIT_WORK(work, func); | |
65a64464 | 876 | if (cpu != orig) |
93981800 | 877 | schedule_work_on(cpu, work); |
65a64464 | 878 | } |
93981800 TH |
879 | if (orig >= 0) |
880 | func(per_cpu_ptr(works, orig)); | |
881 | ||
882 | for_each_online_cpu(cpu) | |
883 | flush_work(per_cpu_ptr(works, cpu)); | |
884 | ||
95402b38 | 885 | put_online_cpus(); |
b6136773 | 886 | free_percpu(works); |
15316ba8 CL |
887 | return 0; |
888 | } | |
889 | ||
eef6a7d5 AS |
890 | /** |
891 | * flush_scheduled_work - ensure that any scheduled work has run to completion. | |
892 | * | |
893 | * Forces execution of the kernel-global workqueue and blocks until its | |
894 | * completion. | |
895 | * | |
896 | * Think twice before calling this function! It's very easy to get into | |
897 | * trouble if you don't take great care. Either of the following situations | |
898 | * will lead to deadlock: | |
899 | * | |
900 | * One of the work items currently on the workqueue needs to acquire | |
901 | * a lock held by your code or its caller. | |
902 | * | |
903 | * Your code is running in the context of a work routine. | |
904 | * | |
905 | * They will be detected by lockdep when they occur, but the first might not | |
906 | * occur very often. It depends on what work items are on the workqueue and | |
907 | * what locks they need, which you have no control over. | |
908 | * | |
909 | * In most situations flushing the entire workqueue is overkill; you merely | |
910 | * need to know that a particular work item isn't queued and isn't running. | |
911 | * In such cases you should use cancel_delayed_work_sync() or | |
912 | * cancel_work_sync() instead. | |
913 | */ | |
1da177e4 LT |
914 | void flush_scheduled_work(void) |
915 | { | |
916 | flush_workqueue(keventd_wq); | |
917 | } | |
ae90dd5d | 918 | EXPORT_SYMBOL(flush_scheduled_work); |
1da177e4 | 919 | |
1fa44eca JB |
920 | /** |
921 | * execute_in_process_context - reliably execute the routine with user context | |
922 | * @fn: the function to execute | |
1fa44eca JB |
923 | * @ew: guaranteed storage for the execute work structure (must |
924 | * be available when the work executes) | |
925 | * | |
926 | * Executes the function immediately if process context is available, | |
927 | * otherwise schedules the function for delayed execution. | |
928 | * | |
929 | * Returns: 0 - function was executed | |
930 | * 1 - function was scheduled for execution | |
931 | */ | |
65f27f38 | 932 | int execute_in_process_context(work_func_t fn, struct execute_work *ew) |
1fa44eca JB |
933 | { |
934 | if (!in_interrupt()) { | |
65f27f38 | 935 | fn(&ew->work); |
1fa44eca JB |
936 | return 0; |
937 | } | |
938 | ||
65f27f38 | 939 | INIT_WORK(&ew->work, fn); |
1fa44eca JB |
940 | schedule_work(&ew->work); |
941 | ||
942 | return 1; | |
943 | } | |
944 | EXPORT_SYMBOL_GPL(execute_in_process_context); | |
945 | ||
1da177e4 LT |
946 | int keventd_up(void) |
947 | { | |
948 | return keventd_wq != NULL; | |
949 | } | |
950 | ||
951 | int current_is_keventd(void) | |
952 | { | |
953 | struct cpu_workqueue_struct *cwq; | |
d243769d | 954 | int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */ |
1da177e4 LT |
955 | int ret = 0; |
956 | ||
957 | BUG_ON(!keventd_wq); | |
958 | ||
89ada679 | 959 | cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu); |
1da177e4 LT |
960 | if (current == cwq->thread) |
961 | ret = 1; | |
962 | ||
963 | return ret; | |
964 | ||
965 | } | |
966 | ||
3af24433 ON |
967 | static struct cpu_workqueue_struct * |
968 | init_cpu_workqueue(struct workqueue_struct *wq, int cpu) | |
1da177e4 | 969 | { |
89ada679 | 970 | struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu); |
1da177e4 | 971 | |
3af24433 ON |
972 | cwq->wq = wq; |
973 | spin_lock_init(&cwq->lock); | |
974 | INIT_LIST_HEAD(&cwq->worklist); | |
975 | init_waitqueue_head(&cwq->more_work); | |
976 | ||
977 | return cwq; | |
1da177e4 LT |
978 | } |
979 | ||
3af24433 ON |
980 | static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu) |
981 | { | |
982 | struct workqueue_struct *wq = cwq->wq; | |
6cc88bc4 | 983 | const char *fmt = is_wq_single_threaded(wq) ? "%s" : "%s/%d"; |
3af24433 ON |
984 | struct task_struct *p; |
985 | ||
986 | p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu); | |
987 | /* | |
988 | * Nobody can add the work_struct to this cwq, | |
989 | * if (caller is __create_workqueue) | |
990 | * nobody should see this wq | |
991 | * else // caller is CPU_UP_PREPARE | |
992 | * cpu is not on cpu_online_map | |
993 | * so we can abort safely. | |
994 | */ | |
995 | if (IS_ERR(p)) | |
996 | return PTR_ERR(p); | |
3af24433 | 997 | cwq->thread = p; |
3af24433 | 998 | |
e1d8aa9f FW |
999 | trace_workqueue_creation(cwq->thread, cpu); |
1000 | ||
3af24433 ON |
1001 | return 0; |
1002 | } | |
1003 | ||
06ba38a9 ON |
1004 | static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu) |
1005 | { | |
1006 | struct task_struct *p = cwq->thread; | |
1007 | ||
1008 | if (p != NULL) { | |
1009 | if (cpu >= 0) | |
1010 | kthread_bind(p, cpu); | |
1011 | wake_up_process(p); | |
1012 | } | |
1013 | } | |
1014 | ||
4e6045f1 JB |
1015 | struct workqueue_struct *__create_workqueue_key(const char *name, |
1016 | int singlethread, | |
1017 | int freezeable, | |
eb13ba87 JB |
1018 | struct lock_class_key *key, |
1019 | const char *lock_name) | |
1da177e4 | 1020 | { |
1da177e4 | 1021 | struct workqueue_struct *wq; |
3af24433 ON |
1022 | struct cpu_workqueue_struct *cwq; |
1023 | int err = 0, cpu; | |
1da177e4 | 1024 | |
3af24433 ON |
1025 | wq = kzalloc(sizeof(*wq), GFP_KERNEL); |
1026 | if (!wq) | |
4690c4ab | 1027 | goto err; |
3af24433 ON |
1028 | |
1029 | wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct); | |
4690c4ab TH |
1030 | if (!wq->cpu_wq) |
1031 | goto err; | |
3af24433 ON |
1032 | |
1033 | wq->name = name; | |
eb13ba87 | 1034 | lockdep_init_map(&wq->lockdep_map, lock_name, key, 0); |
cce1a165 | 1035 | wq->singlethread = singlethread; |
3af24433 | 1036 | wq->freezeable = freezeable; |
cce1a165 | 1037 | INIT_LIST_HEAD(&wq->list); |
3af24433 ON |
1038 | |
1039 | if (singlethread) { | |
3af24433 ON |
1040 | cwq = init_cpu_workqueue(wq, singlethread_cpu); |
1041 | err = create_workqueue_thread(cwq, singlethread_cpu); | |
06ba38a9 | 1042 | start_workqueue_thread(cwq, -1); |
3af24433 | 1043 | } else { |
3da1c84c | 1044 | cpu_maps_update_begin(); |
6af8bf3d ON |
1045 | /* |
1046 | * We must place this wq on list even if the code below fails. | |
1047 | * cpu_down(cpu) can remove cpu from cpu_populated_map before | |
1048 | * destroy_workqueue() takes the lock, in that case we leak | |
1049 | * cwq[cpu]->thread. | |
1050 | */ | |
95402b38 | 1051 | spin_lock(&workqueue_lock); |
3af24433 | 1052 | list_add(&wq->list, &workqueues); |
95402b38 | 1053 | spin_unlock(&workqueue_lock); |
6af8bf3d ON |
1054 | /* |
1055 | * We must initialize cwqs for each possible cpu even if we | |
1056 | * are going to call destroy_workqueue() finally. Otherwise | |
1057 | * cpu_up() can hit the uninitialized cwq once we drop the | |
1058 | * lock. | |
1059 | */ | |
3af24433 ON |
1060 | for_each_possible_cpu(cpu) { |
1061 | cwq = init_cpu_workqueue(wq, cpu); | |
1062 | if (err || !cpu_online(cpu)) | |
1063 | continue; | |
1064 | err = create_workqueue_thread(cwq, cpu); | |
06ba38a9 | 1065 | start_workqueue_thread(cwq, cpu); |
1da177e4 | 1066 | } |
3da1c84c | 1067 | cpu_maps_update_done(); |
3af24433 ON |
1068 | } |
1069 | ||
1070 | if (err) { | |
1071 | destroy_workqueue(wq); | |
1072 | wq = NULL; | |
1073 | } | |
1074 | return wq; | |
4690c4ab TH |
1075 | err: |
1076 | if (wq) { | |
1077 | free_percpu(wq->cpu_wq); | |
1078 | kfree(wq); | |
1079 | } | |
1080 | return NULL; | |
3af24433 | 1081 | } |
4e6045f1 | 1082 | EXPORT_SYMBOL_GPL(__create_workqueue_key); |
1da177e4 | 1083 | |
1e35eaa2 | 1084 | static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq) |
3af24433 | 1085 | { |
14441960 | 1086 | /* |
3da1c84c ON |
1087 | * Our caller is either destroy_workqueue() or CPU_POST_DEAD, |
1088 | * cpu_add_remove_lock protects cwq->thread. | |
14441960 ON |
1089 | */ |
1090 | if (cwq->thread == NULL) | |
1091 | return; | |
3af24433 | 1092 | |
3295f0ef IM |
1093 | lock_map_acquire(&cwq->wq->lockdep_map); |
1094 | lock_map_release(&cwq->wq->lockdep_map); | |
4e6045f1 | 1095 | |
13c22168 | 1096 | flush_cpu_workqueue(cwq); |
14441960 | 1097 | /* |
3da1c84c | 1098 | * If the caller is CPU_POST_DEAD and cwq->worklist was not empty, |
13c22168 ON |
1099 | * a concurrent flush_workqueue() can insert a barrier after us. |
1100 | * However, in that case run_workqueue() won't return and check | |
1101 | * kthread_should_stop() until it flushes all work_struct's. | |
14441960 ON |
1102 | * When ->worklist becomes empty it is safe to exit because no |
1103 | * more work_structs can be queued on this cwq: flush_workqueue | |
1104 | * checks list_empty(), and a "normal" queue_work() can't use | |
1105 | * a dead CPU. | |
1106 | */ | |
e1d8aa9f | 1107 | trace_workqueue_destruction(cwq->thread); |
14441960 ON |
1108 | kthread_stop(cwq->thread); |
1109 | cwq->thread = NULL; | |
3af24433 ON |
1110 | } |
1111 | ||
1112 | /** | |
1113 | * destroy_workqueue - safely terminate a workqueue | |
1114 | * @wq: target workqueue | |
1115 | * | |
1116 | * Safely destroy a workqueue. All work currently pending will be done first. | |
1117 | */ | |
1118 | void destroy_workqueue(struct workqueue_struct *wq) | |
1119 | { | |
e7577c50 | 1120 | const struct cpumask *cpu_map = wq_cpu_map(wq); |
b1f4ec17 | 1121 | int cpu; |
3af24433 | 1122 | |
3da1c84c | 1123 | cpu_maps_update_begin(); |
95402b38 | 1124 | spin_lock(&workqueue_lock); |
b1f4ec17 | 1125 | list_del(&wq->list); |
95402b38 | 1126 | spin_unlock(&workqueue_lock); |
3af24433 | 1127 | |
aa85ea5b | 1128 | for_each_cpu(cpu, cpu_map) |
1e35eaa2 | 1129 | cleanup_workqueue_thread(per_cpu_ptr(wq->cpu_wq, cpu)); |
3da1c84c | 1130 | cpu_maps_update_done(); |
9b41ea72 | 1131 | |
3af24433 ON |
1132 | free_percpu(wq->cpu_wq); |
1133 | kfree(wq); | |
1134 | } | |
1135 | EXPORT_SYMBOL_GPL(destroy_workqueue); | |
1136 | ||
1137 | static int __devinit workqueue_cpu_callback(struct notifier_block *nfb, | |
1138 | unsigned long action, | |
1139 | void *hcpu) | |
1140 | { | |
1141 | unsigned int cpu = (unsigned long)hcpu; | |
1142 | struct cpu_workqueue_struct *cwq; | |
1143 | struct workqueue_struct *wq; | |
80b5184c | 1144 | int err = 0; |
3af24433 | 1145 | |
8bb78442 RW |
1146 | action &= ~CPU_TASKS_FROZEN; |
1147 | ||
3af24433 | 1148 | switch (action) { |
3af24433 | 1149 | case CPU_UP_PREPARE: |
e7577c50 | 1150 | cpumask_set_cpu(cpu, cpu_populated_map); |
3af24433 | 1151 | } |
8448502c | 1152 | undo: |
3af24433 ON |
1153 | list_for_each_entry(wq, &workqueues, list) { |
1154 | cwq = per_cpu_ptr(wq->cpu_wq, cpu); | |
1155 | ||
1156 | switch (action) { | |
1157 | case CPU_UP_PREPARE: | |
80b5184c AM |
1158 | err = create_workqueue_thread(cwq, cpu); |
1159 | if (!err) | |
3af24433 | 1160 | break; |
95402b38 GS |
1161 | printk(KERN_ERR "workqueue [%s] for %i failed\n", |
1162 | wq->name, cpu); | |
8448502c | 1163 | action = CPU_UP_CANCELED; |
80b5184c | 1164 | err = -ENOMEM; |
8448502c | 1165 | goto undo; |
3af24433 ON |
1166 | |
1167 | case CPU_ONLINE: | |
06ba38a9 | 1168 | start_workqueue_thread(cwq, cpu); |
3af24433 ON |
1169 | break; |
1170 | ||
1171 | case CPU_UP_CANCELED: | |
06ba38a9 | 1172 | start_workqueue_thread(cwq, -1); |
3da1c84c | 1173 | case CPU_POST_DEAD: |
1e35eaa2 | 1174 | cleanup_workqueue_thread(cwq); |
3af24433 ON |
1175 | break; |
1176 | } | |
1da177e4 LT |
1177 | } |
1178 | ||
00dfcaf7 ON |
1179 | switch (action) { |
1180 | case CPU_UP_CANCELED: | |
3da1c84c | 1181 | case CPU_POST_DEAD: |
e7577c50 | 1182 | cpumask_clear_cpu(cpu, cpu_populated_map); |
00dfcaf7 ON |
1183 | } |
1184 | ||
80b5184c | 1185 | return notifier_from_errno(err); |
1da177e4 | 1186 | } |
1da177e4 | 1187 | |
2d3854a3 | 1188 | #ifdef CONFIG_SMP |
8ccad40d | 1189 | |
2d3854a3 | 1190 | struct work_for_cpu { |
6b44003e | 1191 | struct completion completion; |
2d3854a3 RR |
1192 | long (*fn)(void *); |
1193 | void *arg; | |
1194 | long ret; | |
1195 | }; | |
1196 | ||
6b44003e | 1197 | static int do_work_for_cpu(void *_wfc) |
2d3854a3 | 1198 | { |
6b44003e | 1199 | struct work_for_cpu *wfc = _wfc; |
2d3854a3 | 1200 | wfc->ret = wfc->fn(wfc->arg); |
6b44003e AM |
1201 | complete(&wfc->completion); |
1202 | return 0; | |
2d3854a3 RR |
1203 | } |
1204 | ||
1205 | /** | |
1206 | * work_on_cpu - run a function in user context on a particular cpu | |
1207 | * @cpu: the cpu to run on | |
1208 | * @fn: the function to run | |
1209 | * @arg: the function arg | |
1210 | * | |
31ad9081 RR |
1211 | * This will return the value @fn returns. |
1212 | * It is up to the caller to ensure that the cpu doesn't go offline. | |
6b44003e | 1213 | * The caller must not hold any locks which would prevent @fn from completing. |
2d3854a3 RR |
1214 | */ |
1215 | long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg) | |
1216 | { | |
6b44003e AM |
1217 | struct task_struct *sub_thread; |
1218 | struct work_for_cpu wfc = { | |
1219 | .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion), | |
1220 | .fn = fn, | |
1221 | .arg = arg, | |
1222 | }; | |
1223 | ||
1224 | sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu"); | |
1225 | if (IS_ERR(sub_thread)) | |
1226 | return PTR_ERR(sub_thread); | |
1227 | kthread_bind(sub_thread, cpu); | |
1228 | wake_up_process(sub_thread); | |
1229 | wait_for_completion(&wfc.completion); | |
2d3854a3 RR |
1230 | return wfc.ret; |
1231 | } | |
1232 | EXPORT_SYMBOL_GPL(work_on_cpu); | |
1233 | #endif /* CONFIG_SMP */ | |
1234 | ||
c12920d1 | 1235 | void __init init_workqueues(void) |
1da177e4 | 1236 | { |
e7577c50 RR |
1237 | alloc_cpumask_var(&cpu_populated_map, GFP_KERNEL); |
1238 | ||
1239 | cpumask_copy(cpu_populated_map, cpu_online_mask); | |
1240 | singlethread_cpu = cpumask_first(cpu_possible_mask); | |
1241 | cpu_singlethread_map = cpumask_of(singlethread_cpu); | |
1da177e4 LT |
1242 | hotcpu_notifier(workqueue_cpu_callback, 0); |
1243 | keventd_wq = create_workqueue("events"); | |
1244 | BUG_ON(!keventd_wq); | |
1245 | } |