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