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