]>
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> | |
12 | * Andrew Morton <andrewm@uow.edu.au> | |
13 | * Kai Petzke <wpp@marie.physik.tu-berlin.de> | |
14 | * Theodore Ts'o <tytso@mit.edu> | |
89ada679 CL |
15 | * |
16 | * Made to use alloc_percpu by Christoph Lameter <clameter@sgi.com>. | |
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> | |
1da177e4 LT |
35 | |
36 | /* | |
f756d5e2 NL |
37 | * The per-CPU workqueue (if single thread, we always use the first |
38 | * possible cpu). | |
1da177e4 LT |
39 | */ |
40 | struct cpu_workqueue_struct { | |
41 | ||
42 | spinlock_t lock; | |
43 | ||
1da177e4 LT |
44 | struct list_head worklist; |
45 | wait_queue_head_t more_work; | |
3af24433 | 46 | struct work_struct *current_work; |
1da177e4 LT |
47 | |
48 | struct workqueue_struct *wq; | |
36c8b586 | 49 | struct task_struct *thread; |
3af24433 | 50 | int should_stop; |
1da177e4 LT |
51 | |
52 | int run_depth; /* Detect run_workqueue() recursion depth */ | |
53 | } ____cacheline_aligned; | |
54 | ||
55 | /* | |
56 | * The externally visible workqueue abstraction is an array of | |
57 | * per-CPU workqueues: | |
58 | */ | |
59 | struct workqueue_struct { | |
89ada679 | 60 | struct cpu_workqueue_struct *cpu_wq; |
cce1a165 | 61 | struct list_head list; |
1da177e4 | 62 | const char *name; |
cce1a165 | 63 | int singlethread; |
319c2a98 | 64 | int freezeable; /* Freeze threads during suspend */ |
1da177e4 LT |
65 | }; |
66 | ||
67 | /* All the per-cpu workqueues on the system, for hotplug cpu to add/remove | |
68 | threads to each one as cpus come/go. */ | |
9b41ea72 | 69 | static DEFINE_MUTEX(workqueue_mutex); |
1da177e4 LT |
70 | static LIST_HEAD(workqueues); |
71 | ||
3af24433 | 72 | static int singlethread_cpu __read_mostly; |
b1f4ec17 | 73 | static cpumask_t cpu_singlethread_map __read_mostly; |
3af24433 ON |
74 | /* optimization, we could use cpu_possible_map */ |
75 | static cpumask_t cpu_populated_map __read_mostly; | |
f756d5e2 | 76 | |
1da177e4 LT |
77 | /* If it's single threaded, it isn't in the list of workqueues. */ |
78 | static inline int is_single_threaded(struct workqueue_struct *wq) | |
79 | { | |
cce1a165 | 80 | return wq->singlethread; |
1da177e4 LT |
81 | } |
82 | ||
b1f4ec17 ON |
83 | static const cpumask_t *wq_cpu_map(struct workqueue_struct *wq) |
84 | { | |
85 | return is_single_threaded(wq) | |
86 | ? &cpu_singlethread_map : &cpu_populated_map; | |
87 | } | |
88 | ||
a848e3b6 ON |
89 | static |
90 | struct cpu_workqueue_struct *wq_per_cpu(struct workqueue_struct *wq, int cpu) | |
91 | { | |
92 | if (unlikely(is_single_threaded(wq))) | |
93 | cpu = singlethread_cpu; | |
94 | return per_cpu_ptr(wq->cpu_wq, cpu); | |
95 | } | |
96 | ||
4594bf15 DH |
97 | /* |
98 | * Set the workqueue on which a work item is to be run | |
99 | * - Must *only* be called if the pending flag is set | |
100 | */ | |
ed7c0fee ON |
101 | static inline void set_wq_data(struct work_struct *work, |
102 | struct cpu_workqueue_struct *cwq) | |
365970a1 | 103 | { |
4594bf15 DH |
104 | unsigned long new; |
105 | ||
106 | BUG_ON(!work_pending(work)); | |
365970a1 | 107 | |
ed7c0fee | 108 | new = (unsigned long) cwq | (1UL << WORK_STRUCT_PENDING); |
a08727ba LT |
109 | new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work); |
110 | atomic_long_set(&work->data, new); | |
365970a1 DH |
111 | } |
112 | ||
ed7c0fee ON |
113 | static inline |
114 | struct cpu_workqueue_struct *get_wq_data(struct work_struct *work) | |
365970a1 | 115 | { |
a08727ba | 116 | return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK); |
365970a1 DH |
117 | } |
118 | ||
b89deed3 ON |
119 | static void insert_work(struct cpu_workqueue_struct *cwq, |
120 | struct work_struct *work, int tail) | |
121 | { | |
122 | set_wq_data(work, cwq); | |
6e84d644 ON |
123 | /* |
124 | * Ensure that we get the right work->data if we see the | |
125 | * result of list_add() below, see try_to_grab_pending(). | |
126 | */ | |
127 | smp_wmb(); | |
b89deed3 ON |
128 | if (tail) |
129 | list_add_tail(&work->entry, &cwq->worklist); | |
130 | else | |
131 | list_add(&work->entry, &cwq->worklist); | |
132 | wake_up(&cwq->more_work); | |
133 | } | |
134 | ||
1da177e4 LT |
135 | /* Preempt must be disabled. */ |
136 | static void __queue_work(struct cpu_workqueue_struct *cwq, | |
137 | struct work_struct *work) | |
138 | { | |
139 | unsigned long flags; | |
140 | ||
141 | spin_lock_irqsave(&cwq->lock, flags); | |
b89deed3 | 142 | insert_work(cwq, work, 1); |
1da177e4 LT |
143 | spin_unlock_irqrestore(&cwq->lock, flags); |
144 | } | |
145 | ||
0fcb78c2 REB |
146 | /** |
147 | * queue_work - queue work on a workqueue | |
148 | * @wq: workqueue to use | |
149 | * @work: work to queue | |
150 | * | |
057647fc | 151 | * Returns 0 if @work was already on a queue, non-zero otherwise. |
1da177e4 LT |
152 | * |
153 | * We queue the work to the CPU it was submitted, but there is no | |
154 | * guarantee that it will be processed by that CPU. | |
155 | */ | |
156 | int fastcall queue_work(struct workqueue_struct *wq, struct work_struct *work) | |
157 | { | |
a848e3b6 | 158 | int ret = 0; |
1da177e4 | 159 | |
a08727ba | 160 | if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) { |
1da177e4 | 161 | BUG_ON(!list_empty(&work->entry)); |
a848e3b6 ON |
162 | __queue_work(wq_per_cpu(wq, get_cpu()), work); |
163 | put_cpu(); | |
1da177e4 LT |
164 | ret = 1; |
165 | } | |
1da177e4 LT |
166 | return ret; |
167 | } | |
ae90dd5d | 168 | EXPORT_SYMBOL_GPL(queue_work); |
1da177e4 | 169 | |
82f67cd9 | 170 | void delayed_work_timer_fn(unsigned long __data) |
1da177e4 | 171 | { |
52bad64d | 172 | struct delayed_work *dwork = (struct delayed_work *)__data; |
ed7c0fee ON |
173 | struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work); |
174 | struct workqueue_struct *wq = cwq->wq; | |
1da177e4 | 175 | |
a848e3b6 | 176 | __queue_work(wq_per_cpu(wq, smp_processor_id()), &dwork->work); |
1da177e4 LT |
177 | } |
178 | ||
0fcb78c2 REB |
179 | /** |
180 | * queue_delayed_work - queue work on a workqueue after delay | |
181 | * @wq: workqueue to use | |
af9997e4 | 182 | * @dwork: delayable work to queue |
0fcb78c2 REB |
183 | * @delay: number of jiffies to wait before queueing |
184 | * | |
057647fc | 185 | * Returns 0 if @work was already on a queue, non-zero otherwise. |
0fcb78c2 | 186 | */ |
1da177e4 | 187 | int fastcall queue_delayed_work(struct workqueue_struct *wq, |
52bad64d | 188 | struct delayed_work *dwork, unsigned long delay) |
1da177e4 | 189 | { |
63bc0362 | 190 | timer_stats_timer_set_start_info(&dwork->timer); |
52bad64d | 191 | if (delay == 0) |
63bc0362 | 192 | return queue_work(wq, &dwork->work); |
1da177e4 | 193 | |
63bc0362 | 194 | return queue_delayed_work_on(-1, wq, dwork, delay); |
1da177e4 | 195 | } |
ae90dd5d | 196 | EXPORT_SYMBOL_GPL(queue_delayed_work); |
1da177e4 | 197 | |
0fcb78c2 REB |
198 | /** |
199 | * queue_delayed_work_on - queue work on specific CPU after delay | |
200 | * @cpu: CPU number to execute work on | |
201 | * @wq: workqueue to use | |
af9997e4 | 202 | * @dwork: work to queue |
0fcb78c2 REB |
203 | * @delay: number of jiffies to wait before queueing |
204 | * | |
057647fc | 205 | * Returns 0 if @work was already on a queue, non-zero otherwise. |
0fcb78c2 | 206 | */ |
7a6bc1cd | 207 | int queue_delayed_work_on(int cpu, struct workqueue_struct *wq, |
52bad64d | 208 | struct delayed_work *dwork, unsigned long delay) |
7a6bc1cd VP |
209 | { |
210 | int ret = 0; | |
52bad64d DH |
211 | struct timer_list *timer = &dwork->timer; |
212 | struct work_struct *work = &dwork->work; | |
7a6bc1cd | 213 | |
a08727ba | 214 | if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) { |
7a6bc1cd VP |
215 | BUG_ON(timer_pending(timer)); |
216 | BUG_ON(!list_empty(&work->entry)); | |
217 | ||
ed7c0fee | 218 | /* This stores cwq for the moment, for the timer_fn */ |
a848e3b6 | 219 | set_wq_data(work, wq_per_cpu(wq, raw_smp_processor_id())); |
7a6bc1cd | 220 | timer->expires = jiffies + delay; |
52bad64d | 221 | timer->data = (unsigned long)dwork; |
7a6bc1cd | 222 | timer->function = delayed_work_timer_fn; |
63bc0362 ON |
223 | |
224 | if (unlikely(cpu >= 0)) | |
225 | add_timer_on(timer, cpu); | |
226 | else | |
227 | add_timer(timer); | |
7a6bc1cd VP |
228 | ret = 1; |
229 | } | |
230 | return ret; | |
231 | } | |
ae90dd5d | 232 | EXPORT_SYMBOL_GPL(queue_delayed_work_on); |
1da177e4 | 233 | |
858119e1 | 234 | static void run_workqueue(struct cpu_workqueue_struct *cwq) |
1da177e4 | 235 | { |
f293ea92 | 236 | spin_lock_irq(&cwq->lock); |
1da177e4 LT |
237 | cwq->run_depth++; |
238 | if (cwq->run_depth > 3) { | |
239 | /* morton gets to eat his hat */ | |
240 | printk("%s: recursion depth exceeded: %d\n", | |
241 | __FUNCTION__, cwq->run_depth); | |
242 | dump_stack(); | |
243 | } | |
244 | while (!list_empty(&cwq->worklist)) { | |
245 | struct work_struct *work = list_entry(cwq->worklist.next, | |
246 | struct work_struct, entry); | |
6bb49e59 | 247 | work_func_t f = work->func; |
1da177e4 | 248 | |
b89deed3 | 249 | cwq->current_work = work; |
1da177e4 | 250 | list_del_init(cwq->worklist.next); |
f293ea92 | 251 | spin_unlock_irq(&cwq->lock); |
1da177e4 | 252 | |
365970a1 | 253 | BUG_ON(get_wq_data(work) != cwq); |
23b2e599 | 254 | work_clear_pending(work); |
65f27f38 | 255 | f(work); |
1da177e4 | 256 | |
d5abe669 PZ |
257 | if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { |
258 | printk(KERN_ERR "BUG: workqueue leaked lock or atomic: " | |
259 | "%s/0x%08x/%d\n", | |
260 | current->comm, preempt_count(), | |
261 | current->pid); | |
262 | printk(KERN_ERR " last function: "); | |
263 | print_symbol("%s\n", (unsigned long)f); | |
264 | debug_show_held_locks(current); | |
265 | dump_stack(); | |
266 | } | |
267 | ||
f293ea92 | 268 | spin_lock_irq(&cwq->lock); |
b89deed3 | 269 | cwq->current_work = NULL; |
1da177e4 LT |
270 | } |
271 | cwq->run_depth--; | |
f293ea92 | 272 | spin_unlock_irq(&cwq->lock); |
1da177e4 LT |
273 | } |
274 | ||
3af24433 ON |
275 | /* |
276 | * NOTE: the caller must not touch *cwq if this func returns true | |
277 | */ | |
278 | static int cwq_should_stop(struct cpu_workqueue_struct *cwq) | |
279 | { | |
280 | int should_stop = cwq->should_stop; | |
281 | ||
282 | if (unlikely(should_stop)) { | |
283 | spin_lock_irq(&cwq->lock); | |
284 | should_stop = cwq->should_stop && list_empty(&cwq->worklist); | |
285 | if (should_stop) | |
286 | cwq->thread = NULL; | |
287 | spin_unlock_irq(&cwq->lock); | |
288 | } | |
289 | ||
290 | return should_stop; | |
291 | } | |
292 | ||
1da177e4 LT |
293 | static int worker_thread(void *__cwq) |
294 | { | |
295 | struct cpu_workqueue_struct *cwq = __cwq; | |
3af24433 | 296 | DEFINE_WAIT(wait); |
1da177e4 | 297 | |
319c2a98 | 298 | if (!cwq->wq->freezeable) |
341a5958 | 299 | current->flags |= PF_NOFREEZE; |
1da177e4 LT |
300 | |
301 | set_user_nice(current, -5); | |
1da177e4 | 302 | |
3af24433 | 303 | for (;;) { |
3af24433 | 304 | prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE); |
85f4186a ON |
305 | if (!freezing(current) && !cwq->should_stop |
306 | && list_empty(&cwq->worklist)) | |
1da177e4 | 307 | schedule(); |
3af24433 ON |
308 | finish_wait(&cwq->more_work, &wait); |
309 | ||
85f4186a ON |
310 | try_to_freeze(); |
311 | ||
3af24433 ON |
312 | if (cwq_should_stop(cwq)) |
313 | break; | |
1da177e4 | 314 | |
3af24433 | 315 | run_workqueue(cwq); |
1da177e4 | 316 | } |
3af24433 | 317 | |
1da177e4 LT |
318 | return 0; |
319 | } | |
320 | ||
fc2e4d70 ON |
321 | struct wq_barrier { |
322 | struct work_struct work; | |
323 | struct completion done; | |
324 | }; | |
325 | ||
326 | static void wq_barrier_func(struct work_struct *work) | |
327 | { | |
328 | struct wq_barrier *barr = container_of(work, struct wq_barrier, work); | |
329 | complete(&barr->done); | |
330 | } | |
331 | ||
83c22520 ON |
332 | static void insert_wq_barrier(struct cpu_workqueue_struct *cwq, |
333 | struct wq_barrier *barr, int tail) | |
fc2e4d70 ON |
334 | { |
335 | INIT_WORK(&barr->work, wq_barrier_func); | |
336 | __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work)); | |
337 | ||
338 | init_completion(&barr->done); | |
83c22520 ON |
339 | |
340 | insert_work(cwq, &barr->work, tail); | |
fc2e4d70 ON |
341 | } |
342 | ||
1da177e4 LT |
343 | static void flush_cpu_workqueue(struct cpu_workqueue_struct *cwq) |
344 | { | |
345 | if (cwq->thread == current) { | |
346 | /* | |
347 | * Probably keventd trying to flush its own queue. So simply run | |
348 | * it by hand rather than deadlocking. | |
349 | */ | |
350 | run_workqueue(cwq); | |
351 | } else { | |
fc2e4d70 | 352 | struct wq_barrier barr; |
83c22520 | 353 | int active = 0; |
1da177e4 | 354 | |
83c22520 ON |
355 | spin_lock_irq(&cwq->lock); |
356 | if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) { | |
357 | insert_wq_barrier(cwq, &barr, 1); | |
358 | active = 1; | |
359 | } | |
360 | spin_unlock_irq(&cwq->lock); | |
1da177e4 | 361 | |
d721304d | 362 | if (active) |
83c22520 | 363 | wait_for_completion(&barr.done); |
1da177e4 LT |
364 | } |
365 | } | |
366 | ||
0fcb78c2 | 367 | /** |
1da177e4 | 368 | * flush_workqueue - ensure that any scheduled work has run to completion. |
0fcb78c2 | 369 | * @wq: workqueue to flush |
1da177e4 LT |
370 | * |
371 | * Forces execution of the workqueue and blocks until its completion. | |
372 | * This is typically used in driver shutdown handlers. | |
373 | * | |
fc2e4d70 ON |
374 | * We sleep until all works which were queued on entry have been handled, |
375 | * but we are not livelocked by new incoming ones. | |
1da177e4 LT |
376 | * |
377 | * This function used to run the workqueues itself. Now we just wait for the | |
378 | * helper threads to do it. | |
379 | */ | |
380 | void fastcall flush_workqueue(struct workqueue_struct *wq) | |
381 | { | |
b1f4ec17 | 382 | const cpumask_t *cpu_map = wq_cpu_map(wq); |
cce1a165 | 383 | int cpu; |
1da177e4 | 384 | |
b1f4ec17 ON |
385 | might_sleep(); |
386 | for_each_cpu_mask(cpu, *cpu_map) | |
387 | flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu)); | |
1da177e4 | 388 | } |
ae90dd5d | 389 | EXPORT_SYMBOL_GPL(flush_workqueue); |
1da177e4 | 390 | |
6e84d644 ON |
391 | /* |
392 | * Upon a successful return, the caller "owns" WORK_STRUCT_PENDING bit, | |
393 | * so this work can't be re-armed in any way. | |
394 | */ | |
395 | static int try_to_grab_pending(struct work_struct *work) | |
396 | { | |
397 | struct cpu_workqueue_struct *cwq; | |
398 | int ret = 0; | |
399 | ||
400 | if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) | |
401 | return 1; | |
402 | ||
403 | /* | |
404 | * The queueing is in progress, or it is already queued. Try to | |
405 | * steal it from ->worklist without clearing WORK_STRUCT_PENDING. | |
406 | */ | |
407 | ||
408 | cwq = get_wq_data(work); | |
409 | if (!cwq) | |
410 | return ret; | |
411 | ||
412 | spin_lock_irq(&cwq->lock); | |
413 | if (!list_empty(&work->entry)) { | |
414 | /* | |
415 | * This work is queued, but perhaps we locked the wrong cwq. | |
416 | * In that case we must see the new value after rmb(), see | |
417 | * insert_work()->wmb(). | |
418 | */ | |
419 | smp_rmb(); | |
420 | if (cwq == get_wq_data(work)) { | |
421 | list_del_init(&work->entry); | |
422 | ret = 1; | |
423 | } | |
424 | } | |
425 | spin_unlock_irq(&cwq->lock); | |
426 | ||
427 | return ret; | |
428 | } | |
429 | ||
430 | static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq, | |
b89deed3 ON |
431 | struct work_struct *work) |
432 | { | |
433 | struct wq_barrier barr; | |
434 | int running = 0; | |
435 | ||
436 | spin_lock_irq(&cwq->lock); | |
437 | if (unlikely(cwq->current_work == work)) { | |
83c22520 | 438 | insert_wq_barrier(cwq, &barr, 0); |
b89deed3 ON |
439 | running = 1; |
440 | } | |
441 | spin_unlock_irq(&cwq->lock); | |
442 | ||
3af24433 | 443 | if (unlikely(running)) |
b89deed3 | 444 | wait_for_completion(&barr.done); |
b89deed3 ON |
445 | } |
446 | ||
6e84d644 | 447 | static void wait_on_work(struct work_struct *work) |
b89deed3 ON |
448 | { |
449 | struct cpu_workqueue_struct *cwq; | |
28e53bdd ON |
450 | struct workqueue_struct *wq; |
451 | const cpumask_t *cpu_map; | |
b1f4ec17 | 452 | int cpu; |
b89deed3 | 453 | |
f293ea92 ON |
454 | might_sleep(); |
455 | ||
b89deed3 | 456 | cwq = get_wq_data(work); |
b89deed3 | 457 | if (!cwq) |
3af24433 | 458 | return; |
b89deed3 | 459 | |
28e53bdd ON |
460 | wq = cwq->wq; |
461 | cpu_map = wq_cpu_map(wq); | |
462 | ||
b1f4ec17 | 463 | for_each_cpu_mask(cpu, *cpu_map) |
6e84d644 ON |
464 | wait_on_cpu_work(per_cpu_ptr(wq->cpu_wq, cpu), work); |
465 | } | |
466 | ||
467 | /** | |
468 | * cancel_work_sync - block until a work_struct's callback has terminated | |
469 | * @work: the work which is to be flushed | |
470 | * | |
471 | * cancel_work_sync() will cancel the work if it is queued. If the work's | |
472 | * callback appears to be running, cancel_work_sync() will block until it | |
473 | * has completed. | |
474 | * | |
475 | * It is possible to use this function if the work re-queues itself. It can | |
476 | * cancel the work even if it migrates to another workqueue, however in that | |
477 | * case it only guarantees that work->func() has completed on the last queued | |
478 | * workqueue. | |
479 | * | |
480 | * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not | |
481 | * pending, otherwise it goes into a busy-wait loop until the timer expires. | |
482 | * | |
483 | * The caller must ensure that workqueue_struct on which this work was last | |
484 | * queued can't be destroyed before this function returns. | |
485 | */ | |
486 | void cancel_work_sync(struct work_struct *work) | |
487 | { | |
488 | while (!try_to_grab_pending(work)) | |
489 | cpu_relax(); | |
490 | wait_on_work(work); | |
491 | work_clear_pending(work); | |
b89deed3 | 492 | } |
28e53bdd | 493 | EXPORT_SYMBOL_GPL(cancel_work_sync); |
b89deed3 | 494 | |
6e84d644 ON |
495 | /** |
496 | * cancel_rearming_delayed_work - reliably kill off a delayed work. | |
497 | * @dwork: the delayed work struct | |
498 | * | |
499 | * It is possible to use this function if @dwork rearms itself via queue_work() | |
500 | * or queue_delayed_work(). See also the comment for cancel_work_sync(). | |
501 | */ | |
502 | void cancel_rearming_delayed_work(struct delayed_work *dwork) | |
503 | { | |
504 | while (!del_timer(&dwork->timer) && | |
505 | !try_to_grab_pending(&dwork->work)) | |
506 | cpu_relax(); | |
507 | wait_on_work(&dwork->work); | |
508 | work_clear_pending(&dwork->work); | |
509 | } | |
510 | EXPORT_SYMBOL(cancel_rearming_delayed_work); | |
1da177e4 | 511 | |
6e84d644 | 512 | static struct workqueue_struct *keventd_wq __read_mostly; |
1da177e4 | 513 | |
0fcb78c2 REB |
514 | /** |
515 | * schedule_work - put work task in global workqueue | |
516 | * @work: job to be done | |
517 | * | |
518 | * This puts a job in the kernel-global workqueue. | |
519 | */ | |
1da177e4 LT |
520 | int fastcall schedule_work(struct work_struct *work) |
521 | { | |
522 | return queue_work(keventd_wq, work); | |
523 | } | |
ae90dd5d | 524 | EXPORT_SYMBOL(schedule_work); |
1da177e4 | 525 | |
0fcb78c2 REB |
526 | /** |
527 | * schedule_delayed_work - put work task in global workqueue after delay | |
52bad64d DH |
528 | * @dwork: job to be done |
529 | * @delay: number of jiffies to wait or 0 for immediate execution | |
0fcb78c2 REB |
530 | * |
531 | * After waiting for a given time this puts a job in the kernel-global | |
532 | * workqueue. | |
533 | */ | |
82f67cd9 IM |
534 | int fastcall schedule_delayed_work(struct delayed_work *dwork, |
535 | unsigned long delay) | |
1da177e4 | 536 | { |
82f67cd9 | 537 | timer_stats_timer_set_start_info(&dwork->timer); |
52bad64d | 538 | return queue_delayed_work(keventd_wq, dwork, delay); |
1da177e4 | 539 | } |
ae90dd5d | 540 | EXPORT_SYMBOL(schedule_delayed_work); |
1da177e4 | 541 | |
0fcb78c2 REB |
542 | /** |
543 | * schedule_delayed_work_on - queue work in global workqueue on CPU after delay | |
544 | * @cpu: cpu to use | |
52bad64d | 545 | * @dwork: job to be done |
0fcb78c2 REB |
546 | * @delay: number of jiffies to wait |
547 | * | |
548 | * After waiting for a given time this puts a job in the kernel-global | |
549 | * workqueue on the specified CPU. | |
550 | */ | |
1da177e4 | 551 | int schedule_delayed_work_on(int cpu, |
52bad64d | 552 | struct delayed_work *dwork, unsigned long delay) |
1da177e4 | 553 | { |
52bad64d | 554 | return queue_delayed_work_on(cpu, keventd_wq, dwork, delay); |
1da177e4 | 555 | } |
ae90dd5d | 556 | EXPORT_SYMBOL(schedule_delayed_work_on); |
1da177e4 | 557 | |
b6136773 AM |
558 | /** |
559 | * schedule_on_each_cpu - call a function on each online CPU from keventd | |
560 | * @func: the function to call | |
b6136773 AM |
561 | * |
562 | * Returns zero on success. | |
563 | * Returns -ve errno on failure. | |
564 | * | |
565 | * Appears to be racy against CPU hotplug. | |
566 | * | |
567 | * schedule_on_each_cpu() is very slow. | |
568 | */ | |
65f27f38 | 569 | int schedule_on_each_cpu(work_func_t func) |
15316ba8 CL |
570 | { |
571 | int cpu; | |
b6136773 | 572 | struct work_struct *works; |
15316ba8 | 573 | |
b6136773 AM |
574 | works = alloc_percpu(struct work_struct); |
575 | if (!works) | |
15316ba8 | 576 | return -ENOMEM; |
b6136773 | 577 | |
e18f3ffb | 578 | preempt_disable(); /* CPU hotplug */ |
15316ba8 | 579 | for_each_online_cpu(cpu) { |
9bfb1839 IM |
580 | struct work_struct *work = per_cpu_ptr(works, cpu); |
581 | ||
582 | INIT_WORK(work, func); | |
583 | set_bit(WORK_STRUCT_PENDING, work_data_bits(work)); | |
584 | __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), work); | |
15316ba8 | 585 | } |
e18f3ffb | 586 | preempt_enable(); |
15316ba8 | 587 | flush_workqueue(keventd_wq); |
b6136773 | 588 | free_percpu(works); |
15316ba8 CL |
589 | return 0; |
590 | } | |
591 | ||
1da177e4 LT |
592 | void flush_scheduled_work(void) |
593 | { | |
594 | flush_workqueue(keventd_wq); | |
595 | } | |
ae90dd5d | 596 | EXPORT_SYMBOL(flush_scheduled_work); |
1da177e4 | 597 | |
1fa44eca JB |
598 | /** |
599 | * execute_in_process_context - reliably execute the routine with user context | |
600 | * @fn: the function to execute | |
1fa44eca JB |
601 | * @ew: guaranteed storage for the execute work structure (must |
602 | * be available when the work executes) | |
603 | * | |
604 | * Executes the function immediately if process context is available, | |
605 | * otherwise schedules the function for delayed execution. | |
606 | * | |
607 | * Returns: 0 - function was executed | |
608 | * 1 - function was scheduled for execution | |
609 | */ | |
65f27f38 | 610 | int execute_in_process_context(work_func_t fn, struct execute_work *ew) |
1fa44eca JB |
611 | { |
612 | if (!in_interrupt()) { | |
65f27f38 | 613 | fn(&ew->work); |
1fa44eca JB |
614 | return 0; |
615 | } | |
616 | ||
65f27f38 | 617 | INIT_WORK(&ew->work, fn); |
1fa44eca JB |
618 | schedule_work(&ew->work); |
619 | ||
620 | return 1; | |
621 | } | |
622 | EXPORT_SYMBOL_GPL(execute_in_process_context); | |
623 | ||
1da177e4 LT |
624 | int keventd_up(void) |
625 | { | |
626 | return keventd_wq != NULL; | |
627 | } | |
628 | ||
629 | int current_is_keventd(void) | |
630 | { | |
631 | struct cpu_workqueue_struct *cwq; | |
632 | int cpu = smp_processor_id(); /* preempt-safe: keventd is per-cpu */ | |
633 | int ret = 0; | |
634 | ||
635 | BUG_ON(!keventd_wq); | |
636 | ||
89ada679 | 637 | cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu); |
1da177e4 LT |
638 | if (current == cwq->thread) |
639 | ret = 1; | |
640 | ||
641 | return ret; | |
642 | ||
643 | } | |
644 | ||
3af24433 ON |
645 | static struct cpu_workqueue_struct * |
646 | init_cpu_workqueue(struct workqueue_struct *wq, int cpu) | |
1da177e4 | 647 | { |
89ada679 | 648 | struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu); |
1da177e4 | 649 | |
3af24433 ON |
650 | cwq->wq = wq; |
651 | spin_lock_init(&cwq->lock); | |
652 | INIT_LIST_HEAD(&cwq->worklist); | |
653 | init_waitqueue_head(&cwq->more_work); | |
654 | ||
655 | return cwq; | |
1da177e4 LT |
656 | } |
657 | ||
3af24433 ON |
658 | static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu) |
659 | { | |
660 | struct workqueue_struct *wq = cwq->wq; | |
661 | const char *fmt = is_single_threaded(wq) ? "%s" : "%s/%d"; | |
662 | struct task_struct *p; | |
663 | ||
664 | p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu); | |
665 | /* | |
666 | * Nobody can add the work_struct to this cwq, | |
667 | * if (caller is __create_workqueue) | |
668 | * nobody should see this wq | |
669 | * else // caller is CPU_UP_PREPARE | |
670 | * cpu is not on cpu_online_map | |
671 | * so we can abort safely. | |
672 | */ | |
673 | if (IS_ERR(p)) | |
674 | return PTR_ERR(p); | |
675 | ||
676 | cwq->thread = p; | |
677 | cwq->should_stop = 0; | |
3af24433 ON |
678 | |
679 | return 0; | |
680 | } | |
681 | ||
06ba38a9 ON |
682 | static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu) |
683 | { | |
684 | struct task_struct *p = cwq->thread; | |
685 | ||
686 | if (p != NULL) { | |
687 | if (cpu >= 0) | |
688 | kthread_bind(p, cpu); | |
689 | wake_up_process(p); | |
690 | } | |
691 | } | |
692 | ||
3af24433 ON |
693 | struct workqueue_struct *__create_workqueue(const char *name, |
694 | int singlethread, int freezeable) | |
1da177e4 | 695 | { |
1da177e4 | 696 | struct workqueue_struct *wq; |
3af24433 ON |
697 | struct cpu_workqueue_struct *cwq; |
698 | int err = 0, cpu; | |
1da177e4 | 699 | |
3af24433 ON |
700 | wq = kzalloc(sizeof(*wq), GFP_KERNEL); |
701 | if (!wq) | |
702 | return NULL; | |
703 | ||
704 | wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct); | |
705 | if (!wq->cpu_wq) { | |
706 | kfree(wq); | |
707 | return NULL; | |
708 | } | |
709 | ||
710 | wq->name = name; | |
cce1a165 | 711 | wq->singlethread = singlethread; |
3af24433 | 712 | wq->freezeable = freezeable; |
cce1a165 | 713 | INIT_LIST_HEAD(&wq->list); |
3af24433 ON |
714 | |
715 | if (singlethread) { | |
3af24433 ON |
716 | cwq = init_cpu_workqueue(wq, singlethread_cpu); |
717 | err = create_workqueue_thread(cwq, singlethread_cpu); | |
06ba38a9 | 718 | start_workqueue_thread(cwq, -1); |
3af24433 | 719 | } else { |
9b41ea72 | 720 | mutex_lock(&workqueue_mutex); |
3af24433 ON |
721 | list_add(&wq->list, &workqueues); |
722 | ||
723 | for_each_possible_cpu(cpu) { | |
724 | cwq = init_cpu_workqueue(wq, cpu); | |
725 | if (err || !cpu_online(cpu)) | |
726 | continue; | |
727 | err = create_workqueue_thread(cwq, cpu); | |
06ba38a9 | 728 | start_workqueue_thread(cwq, cpu); |
1da177e4 | 729 | } |
3af24433 ON |
730 | mutex_unlock(&workqueue_mutex); |
731 | } | |
732 | ||
733 | if (err) { | |
734 | destroy_workqueue(wq); | |
735 | wq = NULL; | |
736 | } | |
737 | return wq; | |
738 | } | |
739 | EXPORT_SYMBOL_GPL(__create_workqueue); | |
1da177e4 | 740 | |
3af24433 ON |
741 | static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu) |
742 | { | |
743 | struct wq_barrier barr; | |
744 | int alive = 0; | |
89ada679 | 745 | |
3af24433 ON |
746 | spin_lock_irq(&cwq->lock); |
747 | if (cwq->thread != NULL) { | |
748 | insert_wq_barrier(cwq, &barr, 1); | |
749 | cwq->should_stop = 1; | |
750 | alive = 1; | |
751 | } | |
752 | spin_unlock_irq(&cwq->lock); | |
753 | ||
754 | if (alive) { | |
755 | wait_for_completion(&barr.done); | |
756 | ||
757 | while (unlikely(cwq->thread != NULL)) | |
758 | cpu_relax(); | |
759 | /* | |
760 | * Wait until cwq->thread unlocks cwq->lock, | |
761 | * it won't touch *cwq after that. | |
762 | */ | |
763 | smp_rmb(); | |
764 | spin_unlock_wait(&cwq->lock); | |
765 | } | |
766 | } | |
767 | ||
768 | /** | |
769 | * destroy_workqueue - safely terminate a workqueue | |
770 | * @wq: target workqueue | |
771 | * | |
772 | * Safely destroy a workqueue. All work currently pending will be done first. | |
773 | */ | |
774 | void destroy_workqueue(struct workqueue_struct *wq) | |
775 | { | |
b1f4ec17 | 776 | const cpumask_t *cpu_map = wq_cpu_map(wq); |
3af24433 | 777 | struct cpu_workqueue_struct *cwq; |
b1f4ec17 | 778 | int cpu; |
3af24433 | 779 | |
b1f4ec17 ON |
780 | mutex_lock(&workqueue_mutex); |
781 | list_del(&wq->list); | |
782 | mutex_unlock(&workqueue_mutex); | |
3af24433 | 783 | |
b1f4ec17 ON |
784 | for_each_cpu_mask(cpu, *cpu_map) { |
785 | cwq = per_cpu_ptr(wq->cpu_wq, cpu); | |
786 | cleanup_workqueue_thread(cwq, cpu); | |
3af24433 | 787 | } |
9b41ea72 | 788 | |
3af24433 ON |
789 | free_percpu(wq->cpu_wq); |
790 | kfree(wq); | |
791 | } | |
792 | EXPORT_SYMBOL_GPL(destroy_workqueue); | |
793 | ||
794 | static int __devinit workqueue_cpu_callback(struct notifier_block *nfb, | |
795 | unsigned long action, | |
796 | void *hcpu) | |
797 | { | |
798 | unsigned int cpu = (unsigned long)hcpu; | |
799 | struct cpu_workqueue_struct *cwq; | |
800 | struct workqueue_struct *wq; | |
801 | ||
8bb78442 RW |
802 | action &= ~CPU_TASKS_FROZEN; |
803 | ||
3af24433 ON |
804 | switch (action) { |
805 | case CPU_LOCK_ACQUIRE: | |
9b41ea72 | 806 | mutex_lock(&workqueue_mutex); |
3af24433 | 807 | return NOTIFY_OK; |
9b41ea72 | 808 | |
3af24433 | 809 | case CPU_LOCK_RELEASE: |
9b41ea72 | 810 | mutex_unlock(&workqueue_mutex); |
3af24433 | 811 | return NOTIFY_OK; |
1da177e4 | 812 | |
3af24433 ON |
813 | case CPU_UP_PREPARE: |
814 | cpu_set(cpu, cpu_populated_map); | |
815 | } | |
816 | ||
817 | list_for_each_entry(wq, &workqueues, list) { | |
818 | cwq = per_cpu_ptr(wq->cpu_wq, cpu); | |
819 | ||
820 | switch (action) { | |
821 | case CPU_UP_PREPARE: | |
822 | if (!create_workqueue_thread(cwq, cpu)) | |
823 | break; | |
824 | printk(KERN_ERR "workqueue for %i failed\n", cpu); | |
825 | return NOTIFY_BAD; | |
826 | ||
827 | case CPU_ONLINE: | |
06ba38a9 | 828 | start_workqueue_thread(cwq, cpu); |
3af24433 ON |
829 | break; |
830 | ||
831 | case CPU_UP_CANCELED: | |
06ba38a9 | 832 | start_workqueue_thread(cwq, -1); |
3af24433 ON |
833 | case CPU_DEAD: |
834 | cleanup_workqueue_thread(cwq, cpu); | |
835 | break; | |
836 | } | |
1da177e4 LT |
837 | } |
838 | ||
839 | return NOTIFY_OK; | |
840 | } | |
1da177e4 | 841 | |
c12920d1 | 842 | void __init init_workqueues(void) |
1da177e4 | 843 | { |
3af24433 | 844 | cpu_populated_map = cpu_online_map; |
f756d5e2 | 845 | singlethread_cpu = first_cpu(cpu_possible_map); |
b1f4ec17 | 846 | cpu_singlethread_map = cpumask_of_cpu(singlethread_cpu); |
1da177e4 LT |
847 | hotcpu_notifier(workqueue_cpu_callback, 0); |
848 | keventd_wq = create_workqueue("events"); | |
849 | BUG_ON(!keventd_wq); | |
850 | } |