]>
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); | |
123 | if (tail) | |
124 | list_add_tail(&work->entry, &cwq->worklist); | |
125 | else | |
126 | list_add(&work->entry, &cwq->worklist); | |
127 | wake_up(&cwq->more_work); | |
128 | } | |
129 | ||
1da177e4 LT |
130 | /* Preempt must be disabled. */ |
131 | static void __queue_work(struct cpu_workqueue_struct *cwq, | |
132 | struct work_struct *work) | |
133 | { | |
134 | unsigned long flags; | |
135 | ||
136 | spin_lock_irqsave(&cwq->lock, flags); | |
b89deed3 | 137 | insert_work(cwq, work, 1); |
1da177e4 LT |
138 | spin_unlock_irqrestore(&cwq->lock, flags); |
139 | } | |
140 | ||
0fcb78c2 REB |
141 | /** |
142 | * queue_work - queue work on a workqueue | |
143 | * @wq: workqueue to use | |
144 | * @work: work to queue | |
145 | * | |
057647fc | 146 | * Returns 0 if @work was already on a queue, non-zero otherwise. |
1da177e4 LT |
147 | * |
148 | * We queue the work to the CPU it was submitted, but there is no | |
149 | * guarantee that it will be processed by that CPU. | |
150 | */ | |
151 | int fastcall queue_work(struct workqueue_struct *wq, struct work_struct *work) | |
152 | { | |
a848e3b6 | 153 | int ret = 0; |
1da177e4 | 154 | |
a08727ba | 155 | if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) { |
1da177e4 | 156 | BUG_ON(!list_empty(&work->entry)); |
a848e3b6 ON |
157 | __queue_work(wq_per_cpu(wq, get_cpu()), work); |
158 | put_cpu(); | |
1da177e4 LT |
159 | ret = 1; |
160 | } | |
1da177e4 LT |
161 | return ret; |
162 | } | |
ae90dd5d | 163 | EXPORT_SYMBOL_GPL(queue_work); |
1da177e4 | 164 | |
82f67cd9 | 165 | void delayed_work_timer_fn(unsigned long __data) |
1da177e4 | 166 | { |
52bad64d | 167 | struct delayed_work *dwork = (struct delayed_work *)__data; |
ed7c0fee ON |
168 | struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work); |
169 | struct workqueue_struct *wq = cwq->wq; | |
1da177e4 | 170 | |
a848e3b6 | 171 | __queue_work(wq_per_cpu(wq, smp_processor_id()), &dwork->work); |
1da177e4 LT |
172 | } |
173 | ||
0fcb78c2 REB |
174 | /** |
175 | * queue_delayed_work - queue work on a workqueue after delay | |
176 | * @wq: workqueue to use | |
af9997e4 | 177 | * @dwork: delayable work to queue |
0fcb78c2 REB |
178 | * @delay: number of jiffies to wait before queueing |
179 | * | |
057647fc | 180 | * Returns 0 if @work was already on a queue, non-zero otherwise. |
0fcb78c2 | 181 | */ |
1da177e4 | 182 | int fastcall queue_delayed_work(struct workqueue_struct *wq, |
52bad64d | 183 | struct delayed_work *dwork, unsigned long delay) |
1da177e4 | 184 | { |
63bc0362 | 185 | timer_stats_timer_set_start_info(&dwork->timer); |
52bad64d | 186 | if (delay == 0) |
63bc0362 | 187 | return queue_work(wq, &dwork->work); |
1da177e4 | 188 | |
63bc0362 | 189 | return queue_delayed_work_on(-1, wq, dwork, delay); |
1da177e4 | 190 | } |
ae90dd5d | 191 | EXPORT_SYMBOL_GPL(queue_delayed_work); |
1da177e4 | 192 | |
0fcb78c2 REB |
193 | /** |
194 | * queue_delayed_work_on - queue work on specific CPU after delay | |
195 | * @cpu: CPU number to execute work on | |
196 | * @wq: workqueue to use | |
af9997e4 | 197 | * @dwork: work to queue |
0fcb78c2 REB |
198 | * @delay: number of jiffies to wait before queueing |
199 | * | |
057647fc | 200 | * Returns 0 if @work was already on a queue, non-zero otherwise. |
0fcb78c2 | 201 | */ |
7a6bc1cd | 202 | int queue_delayed_work_on(int cpu, struct workqueue_struct *wq, |
52bad64d | 203 | struct delayed_work *dwork, unsigned long delay) |
7a6bc1cd VP |
204 | { |
205 | int ret = 0; | |
52bad64d DH |
206 | struct timer_list *timer = &dwork->timer; |
207 | struct work_struct *work = &dwork->work; | |
7a6bc1cd | 208 | |
a08727ba | 209 | if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) { |
7a6bc1cd VP |
210 | BUG_ON(timer_pending(timer)); |
211 | BUG_ON(!list_empty(&work->entry)); | |
212 | ||
ed7c0fee | 213 | /* This stores cwq for the moment, for the timer_fn */ |
a848e3b6 | 214 | set_wq_data(work, wq_per_cpu(wq, 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); |
23b2e599 | 249 | work_clear_pending(work); |
65f27f38 | 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 | 292 | |
319c2a98 | 293 | if (!cwq->wq->freezeable) |
341a5958 | 294 | current->flags |= PF_NOFREEZE; |
1da177e4 LT |
295 | |
296 | set_user_nice(current, -5); | |
1da177e4 | 297 | |
3af24433 | 298 | for (;;) { |
3af24433 | 299 | prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE); |
85f4186a ON |
300 | if (!freezing(current) && !cwq->should_stop |
301 | && list_empty(&cwq->worklist)) | |
1da177e4 | 302 | schedule(); |
3af24433 ON |
303 | finish_wait(&cwq->more_work, &wait); |
304 | ||
85f4186a ON |
305 | try_to_freeze(); |
306 | ||
3af24433 ON |
307 | if (cwq_should_stop(cwq)) |
308 | break; | |
1da177e4 | 309 | |
3af24433 | 310 | run_workqueue(cwq); |
1da177e4 | 311 | } |
3af24433 | 312 | |
1da177e4 LT |
313 | return 0; |
314 | } | |
315 | ||
fc2e4d70 ON |
316 | struct wq_barrier { |
317 | struct work_struct work; | |
318 | struct completion done; | |
319 | }; | |
320 | ||
321 | static void wq_barrier_func(struct work_struct *work) | |
322 | { | |
323 | struct wq_barrier *barr = container_of(work, struct wq_barrier, work); | |
324 | complete(&barr->done); | |
325 | } | |
326 | ||
83c22520 ON |
327 | static void insert_wq_barrier(struct cpu_workqueue_struct *cwq, |
328 | struct wq_barrier *barr, int tail) | |
fc2e4d70 ON |
329 | { |
330 | INIT_WORK(&barr->work, wq_barrier_func); | |
331 | __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work)); | |
332 | ||
333 | init_completion(&barr->done); | |
83c22520 ON |
334 | |
335 | insert_work(cwq, &barr->work, tail); | |
fc2e4d70 ON |
336 | } |
337 | ||
1da177e4 LT |
338 | static void flush_cpu_workqueue(struct cpu_workqueue_struct *cwq) |
339 | { | |
340 | if (cwq->thread == current) { | |
341 | /* | |
342 | * Probably keventd trying to flush its own queue. So simply run | |
343 | * it by hand rather than deadlocking. | |
344 | */ | |
345 | run_workqueue(cwq); | |
346 | } else { | |
fc2e4d70 | 347 | struct wq_barrier barr; |
83c22520 | 348 | int active = 0; |
1da177e4 | 349 | |
83c22520 ON |
350 | spin_lock_irq(&cwq->lock); |
351 | if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) { | |
352 | insert_wq_barrier(cwq, &barr, 1); | |
353 | active = 1; | |
354 | } | |
355 | spin_unlock_irq(&cwq->lock); | |
1da177e4 | 356 | |
d721304d | 357 | if (active) |
83c22520 | 358 | wait_for_completion(&barr.done); |
1da177e4 LT |
359 | } |
360 | } | |
361 | ||
0fcb78c2 | 362 | /** |
1da177e4 | 363 | * flush_workqueue - ensure that any scheduled work has run to completion. |
0fcb78c2 | 364 | * @wq: workqueue to flush |
1da177e4 LT |
365 | * |
366 | * Forces execution of the workqueue and blocks until its completion. | |
367 | * This is typically used in driver shutdown handlers. | |
368 | * | |
fc2e4d70 ON |
369 | * We sleep until all works which were queued on entry have been handled, |
370 | * but we are not livelocked by new incoming ones. | |
1da177e4 LT |
371 | * |
372 | * This function used to run the workqueues itself. Now we just wait for the | |
373 | * helper threads to do it. | |
374 | */ | |
375 | void fastcall flush_workqueue(struct workqueue_struct *wq) | |
376 | { | |
b1f4ec17 | 377 | const cpumask_t *cpu_map = wq_cpu_map(wq); |
cce1a165 | 378 | int cpu; |
1da177e4 | 379 | |
b1f4ec17 ON |
380 | might_sleep(); |
381 | for_each_cpu_mask(cpu, *cpu_map) | |
382 | flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu)); | |
1da177e4 | 383 | } |
ae90dd5d | 384 | EXPORT_SYMBOL_GPL(flush_workqueue); |
1da177e4 | 385 | |
b89deed3 ON |
386 | static void wait_on_work(struct cpu_workqueue_struct *cwq, |
387 | struct work_struct *work) | |
388 | { | |
389 | struct wq_barrier barr; | |
390 | int running = 0; | |
391 | ||
392 | spin_lock_irq(&cwq->lock); | |
393 | if (unlikely(cwq->current_work == work)) { | |
83c22520 | 394 | insert_wq_barrier(cwq, &barr, 0); |
b89deed3 ON |
395 | running = 1; |
396 | } | |
397 | spin_unlock_irq(&cwq->lock); | |
398 | ||
3af24433 | 399 | if (unlikely(running)) |
b89deed3 | 400 | wait_for_completion(&barr.done); |
b89deed3 ON |
401 | } |
402 | ||
403 | /** | |
28e53bdd | 404 | * cancel_work_sync - block until a work_struct's callback has terminated |
b89deed3 ON |
405 | * @work: the work which is to be flushed |
406 | * | |
28e53bdd ON |
407 | * cancel_work_sync() will attempt to cancel the work if it is queued. If the |
408 | * work's callback appears to be running, cancel_work_sync() will block until | |
409 | * it has completed. | |
b89deed3 | 410 | * |
28e53bdd ON |
411 | * cancel_work_sync() is designed to be used when the caller is tearing down |
412 | * data structures which the callback function operates upon. It is expected | |
413 | * that, prior to calling cancel_work_sync(), the caller has arranged for the | |
414 | * work to not be requeued. | |
b89deed3 | 415 | */ |
28e53bdd | 416 | void cancel_work_sync(struct work_struct *work) |
b89deed3 ON |
417 | { |
418 | struct cpu_workqueue_struct *cwq; | |
28e53bdd ON |
419 | struct workqueue_struct *wq; |
420 | const cpumask_t *cpu_map; | |
b1f4ec17 | 421 | int cpu; |
b89deed3 | 422 | |
f293ea92 ON |
423 | might_sleep(); |
424 | ||
b89deed3 ON |
425 | cwq = get_wq_data(work); |
426 | /* Was it ever queued ? */ | |
427 | if (!cwq) | |
3af24433 | 428 | return; |
b89deed3 ON |
429 | |
430 | /* | |
3af24433 ON |
431 | * This work can't be re-queued, no need to re-check that |
432 | * get_wq_data() is still the same when we take cwq->lock. | |
b89deed3 ON |
433 | */ |
434 | spin_lock_irq(&cwq->lock); | |
435 | list_del_init(&work->entry); | |
23b2e599 | 436 | work_clear_pending(work); |
b89deed3 ON |
437 | spin_unlock_irq(&cwq->lock); |
438 | ||
28e53bdd ON |
439 | wq = cwq->wq; |
440 | cpu_map = wq_cpu_map(wq); | |
441 | ||
b1f4ec17 ON |
442 | for_each_cpu_mask(cpu, *cpu_map) |
443 | wait_on_work(per_cpu_ptr(wq->cpu_wq, cpu), work); | |
b89deed3 | 444 | } |
28e53bdd | 445 | EXPORT_SYMBOL_GPL(cancel_work_sync); |
b89deed3 | 446 | |
1da177e4 LT |
447 | |
448 | static struct workqueue_struct *keventd_wq; | |
449 | ||
0fcb78c2 REB |
450 | /** |
451 | * schedule_work - put work task in global workqueue | |
452 | * @work: job to be done | |
453 | * | |
454 | * This puts a job in the kernel-global workqueue. | |
455 | */ | |
1da177e4 LT |
456 | int fastcall schedule_work(struct work_struct *work) |
457 | { | |
458 | return queue_work(keventd_wq, work); | |
459 | } | |
ae90dd5d | 460 | EXPORT_SYMBOL(schedule_work); |
1da177e4 | 461 | |
0fcb78c2 REB |
462 | /** |
463 | * schedule_delayed_work - put work task in global workqueue after delay | |
52bad64d DH |
464 | * @dwork: job to be done |
465 | * @delay: number of jiffies to wait or 0 for immediate execution | |
0fcb78c2 REB |
466 | * |
467 | * After waiting for a given time this puts a job in the kernel-global | |
468 | * workqueue. | |
469 | */ | |
82f67cd9 IM |
470 | int fastcall schedule_delayed_work(struct delayed_work *dwork, |
471 | unsigned long delay) | |
1da177e4 | 472 | { |
82f67cd9 | 473 | timer_stats_timer_set_start_info(&dwork->timer); |
52bad64d | 474 | return queue_delayed_work(keventd_wq, dwork, delay); |
1da177e4 | 475 | } |
ae90dd5d | 476 | EXPORT_SYMBOL(schedule_delayed_work); |
1da177e4 | 477 | |
0fcb78c2 REB |
478 | /** |
479 | * schedule_delayed_work_on - queue work in global workqueue on CPU after delay | |
480 | * @cpu: cpu to use | |
52bad64d | 481 | * @dwork: job to be done |
0fcb78c2 REB |
482 | * @delay: number of jiffies to wait |
483 | * | |
484 | * After waiting for a given time this puts a job in the kernel-global | |
485 | * workqueue on the specified CPU. | |
486 | */ | |
1da177e4 | 487 | int schedule_delayed_work_on(int cpu, |
52bad64d | 488 | struct delayed_work *dwork, unsigned long delay) |
1da177e4 | 489 | { |
52bad64d | 490 | return queue_delayed_work_on(cpu, keventd_wq, dwork, delay); |
1da177e4 | 491 | } |
ae90dd5d | 492 | EXPORT_SYMBOL(schedule_delayed_work_on); |
1da177e4 | 493 | |
b6136773 AM |
494 | /** |
495 | * schedule_on_each_cpu - call a function on each online CPU from keventd | |
496 | * @func: the function to call | |
b6136773 AM |
497 | * |
498 | * Returns zero on success. | |
499 | * Returns -ve errno on failure. | |
500 | * | |
501 | * Appears to be racy against CPU hotplug. | |
502 | * | |
503 | * schedule_on_each_cpu() is very slow. | |
504 | */ | |
65f27f38 | 505 | int schedule_on_each_cpu(work_func_t func) |
15316ba8 CL |
506 | { |
507 | int cpu; | |
b6136773 | 508 | struct work_struct *works; |
15316ba8 | 509 | |
b6136773 AM |
510 | works = alloc_percpu(struct work_struct); |
511 | if (!works) | |
15316ba8 | 512 | return -ENOMEM; |
b6136773 | 513 | |
e18f3ffb | 514 | preempt_disable(); /* CPU hotplug */ |
15316ba8 | 515 | for_each_online_cpu(cpu) { |
9bfb1839 IM |
516 | struct work_struct *work = per_cpu_ptr(works, cpu); |
517 | ||
518 | INIT_WORK(work, func); | |
519 | set_bit(WORK_STRUCT_PENDING, work_data_bits(work)); | |
520 | __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), work); | |
15316ba8 | 521 | } |
e18f3ffb | 522 | preempt_enable(); |
15316ba8 | 523 | flush_workqueue(keventd_wq); |
b6136773 | 524 | free_percpu(works); |
15316ba8 CL |
525 | return 0; |
526 | } | |
527 | ||
1da177e4 LT |
528 | void flush_scheduled_work(void) |
529 | { | |
530 | flush_workqueue(keventd_wq); | |
531 | } | |
ae90dd5d | 532 | EXPORT_SYMBOL(flush_scheduled_work); |
1da177e4 LT |
533 | |
534 | /** | |
1634c48f | 535 | * cancel_rearming_delayed_work - kill off a delayed work whose handler rearms the delayed work. |
52bad64d | 536 | * @dwork: the delayed work struct |
ed7c0fee ON |
537 | * |
538 | * Note that the work callback function may still be running on return from | |
28e53bdd ON |
539 | * cancel_delayed_work(). Run flush_workqueue() or cancel_work_sync() to wait |
540 | * on it. | |
1da177e4 | 541 | */ |
1634c48f | 542 | void cancel_rearming_delayed_work(struct delayed_work *dwork) |
1da177e4 | 543 | { |
1634c48f | 544 | struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work); |
dfb4b82e | 545 | |
1634c48f ON |
546 | /* Was it ever queued ? */ |
547 | if (cwq != NULL) { | |
548 | struct workqueue_struct *wq = cwq->wq; | |
1da177e4 | 549 | |
1634c48f ON |
550 | while (!cancel_delayed_work(dwork)) |
551 | flush_workqueue(wq); | |
552 | } | |
1da177e4 LT |
553 | } |
554 | EXPORT_SYMBOL(cancel_rearming_delayed_work); | |
555 | ||
1fa44eca JB |
556 | /** |
557 | * execute_in_process_context - reliably execute the routine with user context | |
558 | * @fn: the function to execute | |
1fa44eca JB |
559 | * @ew: guaranteed storage for the execute work structure (must |
560 | * be available when the work executes) | |
561 | * | |
562 | * Executes the function immediately if process context is available, | |
563 | * otherwise schedules the function for delayed execution. | |
564 | * | |
565 | * Returns: 0 - function was executed | |
566 | * 1 - function was scheduled for execution | |
567 | */ | |
65f27f38 | 568 | int execute_in_process_context(work_func_t fn, struct execute_work *ew) |
1fa44eca JB |
569 | { |
570 | if (!in_interrupt()) { | |
65f27f38 | 571 | fn(&ew->work); |
1fa44eca JB |
572 | return 0; |
573 | } | |
574 | ||
65f27f38 | 575 | INIT_WORK(&ew->work, fn); |
1fa44eca JB |
576 | schedule_work(&ew->work); |
577 | ||
578 | return 1; | |
579 | } | |
580 | EXPORT_SYMBOL_GPL(execute_in_process_context); | |
581 | ||
1da177e4 LT |
582 | int keventd_up(void) |
583 | { | |
584 | return keventd_wq != NULL; | |
585 | } | |
586 | ||
587 | int current_is_keventd(void) | |
588 | { | |
589 | struct cpu_workqueue_struct *cwq; | |
590 | int cpu = smp_processor_id(); /* preempt-safe: keventd is per-cpu */ | |
591 | int ret = 0; | |
592 | ||
593 | BUG_ON(!keventd_wq); | |
594 | ||
89ada679 | 595 | cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu); |
1da177e4 LT |
596 | if (current == cwq->thread) |
597 | ret = 1; | |
598 | ||
599 | return ret; | |
600 | ||
601 | } | |
602 | ||
3af24433 ON |
603 | static struct cpu_workqueue_struct * |
604 | init_cpu_workqueue(struct workqueue_struct *wq, int cpu) | |
1da177e4 | 605 | { |
89ada679 | 606 | struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu); |
1da177e4 | 607 | |
3af24433 ON |
608 | cwq->wq = wq; |
609 | spin_lock_init(&cwq->lock); | |
610 | INIT_LIST_HEAD(&cwq->worklist); | |
611 | init_waitqueue_head(&cwq->more_work); | |
612 | ||
613 | return cwq; | |
1da177e4 LT |
614 | } |
615 | ||
3af24433 ON |
616 | static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu) |
617 | { | |
618 | struct workqueue_struct *wq = cwq->wq; | |
619 | const char *fmt = is_single_threaded(wq) ? "%s" : "%s/%d"; | |
620 | struct task_struct *p; | |
621 | ||
622 | p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu); | |
623 | /* | |
624 | * Nobody can add the work_struct to this cwq, | |
625 | * if (caller is __create_workqueue) | |
626 | * nobody should see this wq | |
627 | * else // caller is CPU_UP_PREPARE | |
628 | * cpu is not on cpu_online_map | |
629 | * so we can abort safely. | |
630 | */ | |
631 | if (IS_ERR(p)) | |
632 | return PTR_ERR(p); | |
633 | ||
634 | cwq->thread = p; | |
635 | cwq->should_stop = 0; | |
3af24433 ON |
636 | |
637 | return 0; | |
638 | } | |
639 | ||
06ba38a9 ON |
640 | static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu) |
641 | { | |
642 | struct task_struct *p = cwq->thread; | |
643 | ||
644 | if (p != NULL) { | |
645 | if (cpu >= 0) | |
646 | kthread_bind(p, cpu); | |
647 | wake_up_process(p); | |
648 | } | |
649 | } | |
650 | ||
3af24433 ON |
651 | struct workqueue_struct *__create_workqueue(const char *name, |
652 | int singlethread, int freezeable) | |
1da177e4 | 653 | { |
1da177e4 | 654 | struct workqueue_struct *wq; |
3af24433 ON |
655 | struct cpu_workqueue_struct *cwq; |
656 | int err = 0, cpu; | |
1da177e4 | 657 | |
3af24433 ON |
658 | wq = kzalloc(sizeof(*wq), GFP_KERNEL); |
659 | if (!wq) | |
660 | return NULL; | |
661 | ||
662 | wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct); | |
663 | if (!wq->cpu_wq) { | |
664 | kfree(wq); | |
665 | return NULL; | |
666 | } | |
667 | ||
668 | wq->name = name; | |
cce1a165 | 669 | wq->singlethread = singlethread; |
3af24433 | 670 | wq->freezeable = freezeable; |
cce1a165 | 671 | INIT_LIST_HEAD(&wq->list); |
3af24433 ON |
672 | |
673 | if (singlethread) { | |
3af24433 ON |
674 | cwq = init_cpu_workqueue(wq, singlethread_cpu); |
675 | err = create_workqueue_thread(cwq, singlethread_cpu); | |
06ba38a9 | 676 | start_workqueue_thread(cwq, -1); |
3af24433 | 677 | } else { |
9b41ea72 | 678 | mutex_lock(&workqueue_mutex); |
3af24433 ON |
679 | list_add(&wq->list, &workqueues); |
680 | ||
681 | for_each_possible_cpu(cpu) { | |
682 | cwq = init_cpu_workqueue(wq, cpu); | |
683 | if (err || !cpu_online(cpu)) | |
684 | continue; | |
685 | err = create_workqueue_thread(cwq, cpu); | |
06ba38a9 | 686 | start_workqueue_thread(cwq, cpu); |
1da177e4 | 687 | } |
3af24433 ON |
688 | mutex_unlock(&workqueue_mutex); |
689 | } | |
690 | ||
691 | if (err) { | |
692 | destroy_workqueue(wq); | |
693 | wq = NULL; | |
694 | } | |
695 | return wq; | |
696 | } | |
697 | EXPORT_SYMBOL_GPL(__create_workqueue); | |
1da177e4 | 698 | |
3af24433 ON |
699 | static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu) |
700 | { | |
701 | struct wq_barrier barr; | |
702 | int alive = 0; | |
89ada679 | 703 | |
3af24433 ON |
704 | spin_lock_irq(&cwq->lock); |
705 | if (cwq->thread != NULL) { | |
706 | insert_wq_barrier(cwq, &barr, 1); | |
707 | cwq->should_stop = 1; | |
708 | alive = 1; | |
709 | } | |
710 | spin_unlock_irq(&cwq->lock); | |
711 | ||
712 | if (alive) { | |
713 | wait_for_completion(&barr.done); | |
714 | ||
715 | while (unlikely(cwq->thread != NULL)) | |
716 | cpu_relax(); | |
717 | /* | |
718 | * Wait until cwq->thread unlocks cwq->lock, | |
719 | * it won't touch *cwq after that. | |
720 | */ | |
721 | smp_rmb(); | |
722 | spin_unlock_wait(&cwq->lock); | |
723 | } | |
724 | } | |
725 | ||
726 | /** | |
727 | * destroy_workqueue - safely terminate a workqueue | |
728 | * @wq: target workqueue | |
729 | * | |
730 | * Safely destroy a workqueue. All work currently pending will be done first. | |
731 | */ | |
732 | void destroy_workqueue(struct workqueue_struct *wq) | |
733 | { | |
b1f4ec17 | 734 | const cpumask_t *cpu_map = wq_cpu_map(wq); |
3af24433 | 735 | struct cpu_workqueue_struct *cwq; |
b1f4ec17 | 736 | int cpu; |
3af24433 | 737 | |
b1f4ec17 ON |
738 | mutex_lock(&workqueue_mutex); |
739 | list_del(&wq->list); | |
740 | mutex_unlock(&workqueue_mutex); | |
3af24433 | 741 | |
b1f4ec17 ON |
742 | for_each_cpu_mask(cpu, *cpu_map) { |
743 | cwq = per_cpu_ptr(wq->cpu_wq, cpu); | |
744 | cleanup_workqueue_thread(cwq, cpu); | |
3af24433 | 745 | } |
9b41ea72 | 746 | |
3af24433 ON |
747 | free_percpu(wq->cpu_wq); |
748 | kfree(wq); | |
749 | } | |
750 | EXPORT_SYMBOL_GPL(destroy_workqueue); | |
751 | ||
752 | static int __devinit workqueue_cpu_callback(struct notifier_block *nfb, | |
753 | unsigned long action, | |
754 | void *hcpu) | |
755 | { | |
756 | unsigned int cpu = (unsigned long)hcpu; | |
757 | struct cpu_workqueue_struct *cwq; | |
758 | struct workqueue_struct *wq; | |
759 | ||
760 | switch (action) { | |
761 | case CPU_LOCK_ACQUIRE: | |
9b41ea72 | 762 | mutex_lock(&workqueue_mutex); |
3af24433 | 763 | return NOTIFY_OK; |
9b41ea72 | 764 | |
3af24433 | 765 | case CPU_LOCK_RELEASE: |
9b41ea72 | 766 | mutex_unlock(&workqueue_mutex); |
3af24433 | 767 | return NOTIFY_OK; |
1da177e4 | 768 | |
3af24433 ON |
769 | case CPU_UP_PREPARE: |
770 | cpu_set(cpu, cpu_populated_map); | |
771 | } | |
772 | ||
773 | list_for_each_entry(wq, &workqueues, list) { | |
774 | cwq = per_cpu_ptr(wq->cpu_wq, cpu); | |
775 | ||
776 | switch (action) { | |
777 | case CPU_UP_PREPARE: | |
778 | if (!create_workqueue_thread(cwq, cpu)) | |
779 | break; | |
780 | printk(KERN_ERR "workqueue for %i failed\n", cpu); | |
781 | return NOTIFY_BAD; | |
782 | ||
783 | case CPU_ONLINE: | |
06ba38a9 | 784 | start_workqueue_thread(cwq, cpu); |
3af24433 ON |
785 | break; |
786 | ||
787 | case CPU_UP_CANCELED: | |
06ba38a9 | 788 | start_workqueue_thread(cwq, -1); |
3af24433 ON |
789 | case CPU_DEAD: |
790 | cleanup_workqueue_thread(cwq, cpu); | |
791 | break; | |
792 | } | |
1da177e4 LT |
793 | } |
794 | ||
795 | return NOTIFY_OK; | |
796 | } | |
1da177e4 | 797 | |
c12920d1 | 798 | void __init init_workqueues(void) |
1da177e4 | 799 | { |
3af24433 | 800 | cpu_populated_map = cpu_online_map; |
f756d5e2 | 801 | singlethread_cpu = first_cpu(cpu_possible_map); |
b1f4ec17 | 802 | cpu_singlethread_map = cpumask_of_cpu(singlethread_cpu); |
1da177e4 LT |
803 | hotcpu_notifier(workqueue_cpu_callback, 0); |
804 | keventd_wq = create_workqueue("events"); | |
805 | BUG_ON(!keventd_wq); | |
806 | } |