[CPUFREQ] Misc cleanups in ondemand.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / workqueue.c
CommitLineData
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>
1da177e4
LT
31
32/*
f756d5e2
NL
33 * The per-CPU workqueue (if single thread, we always use the first
34 * possible cpu).
1da177e4
LT
35 *
36 * The sequence counters are for flush_scheduled_work(). It wants to wait
37 * until until all currently-scheduled works are completed, but it doesn't
38 * want to be livelocked by new, incoming ones. So it waits until
39 * remove_sequence is >= the insert_sequence which pertained when
40 * flush_scheduled_work() was called.
41 */
42struct cpu_workqueue_struct {
43
44 spinlock_t lock;
45
46 long remove_sequence; /* Least-recently added (next to run) */
47 long insert_sequence; /* Next to add */
48
49 struct list_head worklist;
50 wait_queue_head_t more_work;
51 wait_queue_head_t work_done;
52
53 struct workqueue_struct *wq;
54 task_t *thread;
55
56 int run_depth; /* Detect run_workqueue() recursion depth */
57} ____cacheline_aligned;
58
59/*
60 * The externally visible workqueue abstraction is an array of
61 * per-CPU workqueues:
62 */
63struct workqueue_struct {
89ada679 64 struct cpu_workqueue_struct *cpu_wq;
1da177e4
LT
65 const char *name;
66 struct list_head list; /* Empty if single thread */
67};
68
69/* All the per-cpu workqueues on the system, for hotplug cpu to add/remove
70 threads to each one as cpus come/go. */
71static DEFINE_SPINLOCK(workqueue_lock);
72static LIST_HEAD(workqueues);
73
f756d5e2
NL
74static int singlethread_cpu;
75
1da177e4
LT
76/* If it's single threaded, it isn't in the list of workqueues. */
77static inline int is_single_threaded(struct workqueue_struct *wq)
78{
79 return list_empty(&wq->list);
80}
81
82/* Preempt must be disabled. */
83static void __queue_work(struct cpu_workqueue_struct *cwq,
84 struct work_struct *work)
85{
86 unsigned long flags;
87
88 spin_lock_irqsave(&cwq->lock, flags);
89 work->wq_data = cwq;
90 list_add_tail(&work->entry, &cwq->worklist);
91 cwq->insert_sequence++;
92 wake_up(&cwq->more_work);
93 spin_unlock_irqrestore(&cwq->lock, flags);
94}
95
96/*
97 * Queue work on a workqueue. Return non-zero if it was successfully
98 * added.
99 *
100 * We queue the work to the CPU it was submitted, but there is no
101 * guarantee that it will be processed by that CPU.
102 */
103int fastcall queue_work(struct workqueue_struct *wq, struct work_struct *work)
104{
105 int ret = 0, cpu = get_cpu();
106
107 if (!test_and_set_bit(0, &work->pending)) {
108 if (unlikely(is_single_threaded(wq)))
f756d5e2 109 cpu = singlethread_cpu;
1da177e4 110 BUG_ON(!list_empty(&work->entry));
89ada679 111 __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
1da177e4
LT
112 ret = 1;
113 }
114 put_cpu();
115 return ret;
116}
117
118static void delayed_work_timer_fn(unsigned long __data)
119{
120 struct work_struct *work = (struct work_struct *)__data;
121 struct workqueue_struct *wq = work->wq_data;
122 int cpu = smp_processor_id();
123
124 if (unlikely(is_single_threaded(wq)))
f756d5e2 125 cpu = singlethread_cpu;
1da177e4 126
89ada679 127 __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
1da177e4
LT
128}
129
130int fastcall queue_delayed_work(struct workqueue_struct *wq,
131 struct work_struct *work, unsigned long delay)
132{
133 int ret = 0;
134 struct timer_list *timer = &work->timer;
135
136 if (!test_and_set_bit(0, &work->pending)) {
137 BUG_ON(timer_pending(timer));
138 BUG_ON(!list_empty(&work->entry));
139
140 /* This stores wq for the moment, for the timer_fn */
141 work->wq_data = wq;
142 timer->expires = jiffies + delay;
143 timer->data = (unsigned long)work;
144 timer->function = delayed_work_timer_fn;
145 add_timer(timer);
146 ret = 1;
147 }
148 return ret;
149}
150
7a6bc1cd
VP
151int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
152 struct work_struct *work, unsigned long delay)
153{
154 int ret = 0;
155 struct timer_list *timer = &work->timer;
156
157 if (!test_and_set_bit(0, &work->pending)) {
158 BUG_ON(timer_pending(timer));
159 BUG_ON(!list_empty(&work->entry));
160
161 /* This stores wq for the moment, for the timer_fn */
162 work->wq_data = wq;
163 timer->expires = jiffies + delay;
164 timer->data = (unsigned long)work;
165 timer->function = delayed_work_timer_fn;
166 add_timer_on(timer, cpu);
167 ret = 1;
168 }
169 return ret;
170}
171
858119e1 172static void run_workqueue(struct cpu_workqueue_struct *cwq)
1da177e4
LT
173{
174 unsigned long flags;
175
176 /*
177 * Keep taking off work from the queue until
178 * done.
179 */
180 spin_lock_irqsave(&cwq->lock, flags);
181 cwq->run_depth++;
182 if (cwq->run_depth > 3) {
183 /* morton gets to eat his hat */
184 printk("%s: recursion depth exceeded: %d\n",
185 __FUNCTION__, cwq->run_depth);
186 dump_stack();
187 }
188 while (!list_empty(&cwq->worklist)) {
189 struct work_struct *work = list_entry(cwq->worklist.next,
190 struct work_struct, entry);
191 void (*f) (void *) = work->func;
192 void *data = work->data;
193
194 list_del_init(cwq->worklist.next);
195 spin_unlock_irqrestore(&cwq->lock, flags);
196
197 BUG_ON(work->wq_data != cwq);
198 clear_bit(0, &work->pending);
199 f(data);
200
201 spin_lock_irqsave(&cwq->lock, flags);
202 cwq->remove_sequence++;
203 wake_up(&cwq->work_done);
204 }
205 cwq->run_depth--;
206 spin_unlock_irqrestore(&cwq->lock, flags);
207}
208
209static int worker_thread(void *__cwq)
210{
211 struct cpu_workqueue_struct *cwq = __cwq;
212 DECLARE_WAITQUEUE(wait, current);
213 struct k_sigaction sa;
214 sigset_t blocked;
215
216 current->flags |= PF_NOFREEZE;
217
218 set_user_nice(current, -5);
219
220 /* Block and flush all signals */
221 sigfillset(&blocked);
222 sigprocmask(SIG_BLOCK, &blocked, NULL);
223 flush_signals(current);
224
225 /* SIG_IGN makes children autoreap: see do_notify_parent(). */
226 sa.sa.sa_handler = SIG_IGN;
227 sa.sa.sa_flags = 0;
228 siginitset(&sa.sa.sa_mask, sigmask(SIGCHLD));
229 do_sigaction(SIGCHLD, &sa, (struct k_sigaction *)0);
230
231 set_current_state(TASK_INTERRUPTIBLE);
232 while (!kthread_should_stop()) {
233 add_wait_queue(&cwq->more_work, &wait);
234 if (list_empty(&cwq->worklist))
235 schedule();
236 else
237 __set_current_state(TASK_RUNNING);
238 remove_wait_queue(&cwq->more_work, &wait);
239
240 if (!list_empty(&cwq->worklist))
241 run_workqueue(cwq);
242 set_current_state(TASK_INTERRUPTIBLE);
243 }
244 __set_current_state(TASK_RUNNING);
245 return 0;
246}
247
248static void flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
249{
250 if (cwq->thread == current) {
251 /*
252 * Probably keventd trying to flush its own queue. So simply run
253 * it by hand rather than deadlocking.
254 */
255 run_workqueue(cwq);
256 } else {
257 DEFINE_WAIT(wait);
258 long sequence_needed;
259
260 spin_lock_irq(&cwq->lock);
261 sequence_needed = cwq->insert_sequence;
262
263 while (sequence_needed - cwq->remove_sequence > 0) {
264 prepare_to_wait(&cwq->work_done, &wait,
265 TASK_UNINTERRUPTIBLE);
266 spin_unlock_irq(&cwq->lock);
267 schedule();
268 spin_lock_irq(&cwq->lock);
269 }
270 finish_wait(&cwq->work_done, &wait);
271 spin_unlock_irq(&cwq->lock);
272 }
273}
274
275/*
276 * flush_workqueue - ensure that any scheduled work has run to completion.
277 *
278 * Forces execution of the workqueue and blocks until its completion.
279 * This is typically used in driver shutdown handlers.
280 *
281 * This function will sample each workqueue's current insert_sequence number and
282 * will sleep until the head sequence is greater than or equal to that. This
283 * means that we sleep until all works which were queued on entry have been
284 * handled, but we are not livelocked by new incoming ones.
285 *
286 * This function used to run the workqueues itself. Now we just wait for the
287 * helper threads to do it.
288 */
289void fastcall flush_workqueue(struct workqueue_struct *wq)
290{
291 might_sleep();
292
293 if (is_single_threaded(wq)) {
bce61dd4 294 /* Always use first cpu's area. */
f756d5e2 295 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, singlethread_cpu));
1da177e4
LT
296 } else {
297 int cpu;
298
299 lock_cpu_hotplug();
300 for_each_online_cpu(cpu)
89ada679 301 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
1da177e4
LT
302 unlock_cpu_hotplug();
303 }
304}
305
306static struct task_struct *create_workqueue_thread(struct workqueue_struct *wq,
307 int cpu)
308{
89ada679 309 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
1da177e4
LT
310 struct task_struct *p;
311
312 spin_lock_init(&cwq->lock);
313 cwq->wq = wq;
314 cwq->thread = NULL;
315 cwq->insert_sequence = 0;
316 cwq->remove_sequence = 0;
317 INIT_LIST_HEAD(&cwq->worklist);
318 init_waitqueue_head(&cwq->more_work);
319 init_waitqueue_head(&cwq->work_done);
320
321 if (is_single_threaded(wq))
322 p = kthread_create(worker_thread, cwq, "%s", wq->name);
323 else
324 p = kthread_create(worker_thread, cwq, "%s/%d", wq->name, cpu);
325 if (IS_ERR(p))
326 return NULL;
327 cwq->thread = p;
328 return p;
329}
330
331struct workqueue_struct *__create_workqueue(const char *name,
332 int singlethread)
333{
334 int cpu, destroy = 0;
335 struct workqueue_struct *wq;
336 struct task_struct *p;
337
dd392710 338 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
1da177e4
LT
339 if (!wq)
340 return NULL;
1da177e4 341
89ada679 342 wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
676121fc
BC
343 if (!wq->cpu_wq) {
344 kfree(wq);
345 return NULL;
346 }
347
1da177e4
LT
348 wq->name = name;
349 /* We don't need the distraction of CPUs appearing and vanishing. */
350 lock_cpu_hotplug();
351 if (singlethread) {
352 INIT_LIST_HEAD(&wq->list);
f756d5e2 353 p = create_workqueue_thread(wq, singlethread_cpu);
1da177e4
LT
354 if (!p)
355 destroy = 1;
356 else
357 wake_up_process(p);
358 } else {
359 spin_lock(&workqueue_lock);
360 list_add(&wq->list, &workqueues);
361 spin_unlock(&workqueue_lock);
362 for_each_online_cpu(cpu) {
363 p = create_workqueue_thread(wq, cpu);
364 if (p) {
365 kthread_bind(p, cpu);
366 wake_up_process(p);
367 } else
368 destroy = 1;
369 }
370 }
371 unlock_cpu_hotplug();
372
373 /*
374 * Was there any error during startup? If yes then clean up:
375 */
376 if (destroy) {
377 destroy_workqueue(wq);
378 wq = NULL;
379 }
380 return wq;
381}
382
383static void cleanup_workqueue_thread(struct workqueue_struct *wq, int cpu)
384{
385 struct cpu_workqueue_struct *cwq;
386 unsigned long flags;
387 struct task_struct *p;
388
89ada679 389 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
1da177e4
LT
390 spin_lock_irqsave(&cwq->lock, flags);
391 p = cwq->thread;
392 cwq->thread = NULL;
393 spin_unlock_irqrestore(&cwq->lock, flags);
394 if (p)
395 kthread_stop(p);
396}
397
398void destroy_workqueue(struct workqueue_struct *wq)
399{
400 int cpu;
401
402 flush_workqueue(wq);
403
404 /* We don't need the distraction of CPUs appearing and vanishing. */
405 lock_cpu_hotplug();
406 if (is_single_threaded(wq))
f756d5e2 407 cleanup_workqueue_thread(wq, singlethread_cpu);
1da177e4
LT
408 else {
409 for_each_online_cpu(cpu)
410 cleanup_workqueue_thread(wq, cpu);
411 spin_lock(&workqueue_lock);
412 list_del(&wq->list);
413 spin_unlock(&workqueue_lock);
414 }
415 unlock_cpu_hotplug();
89ada679 416 free_percpu(wq->cpu_wq);
1da177e4
LT
417 kfree(wq);
418}
419
420static struct workqueue_struct *keventd_wq;
421
422int fastcall schedule_work(struct work_struct *work)
423{
424 return queue_work(keventd_wq, work);
425}
426
427int fastcall schedule_delayed_work(struct work_struct *work, unsigned long delay)
428{
429 return queue_delayed_work(keventd_wq, work, delay);
430}
431
432int schedule_delayed_work_on(int cpu,
433 struct work_struct *work, unsigned long delay)
434{
7a6bc1cd 435 return queue_delayed_work_on(cpu, keventd_wq, work, delay);
1da177e4
LT
436}
437
b6136773
AM
438/**
439 * schedule_on_each_cpu - call a function on each online CPU from keventd
440 * @func: the function to call
441 * @info: a pointer to pass to func()
442 *
443 * Returns zero on success.
444 * Returns -ve errno on failure.
445 *
446 * Appears to be racy against CPU hotplug.
447 *
448 * schedule_on_each_cpu() is very slow.
449 */
450int schedule_on_each_cpu(void (*func)(void *info), void *info)
15316ba8
CL
451{
452 int cpu;
b6136773 453 struct work_struct *works;
15316ba8 454
b6136773
AM
455 works = alloc_percpu(struct work_struct);
456 if (!works)
15316ba8 457 return -ENOMEM;
b6136773 458
15316ba8 459 for_each_online_cpu(cpu) {
b6136773 460 INIT_WORK(per_cpu_ptr(works, cpu), func, info);
15316ba8 461 __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu),
b6136773 462 per_cpu_ptr(works, cpu));
15316ba8
CL
463 }
464 flush_workqueue(keventd_wq);
b6136773 465 free_percpu(works);
15316ba8
CL
466 return 0;
467}
468
1da177e4
LT
469void flush_scheduled_work(void)
470{
471 flush_workqueue(keventd_wq);
472}
473
474/**
475 * cancel_rearming_delayed_workqueue - reliably kill off a delayed
476 * work whose handler rearms the delayed work.
477 * @wq: the controlling workqueue structure
478 * @work: the delayed work struct
479 */
81ddef77
JB
480void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq,
481 struct work_struct *work)
1da177e4
LT
482{
483 while (!cancel_delayed_work(work))
484 flush_workqueue(wq);
485}
81ddef77 486EXPORT_SYMBOL(cancel_rearming_delayed_workqueue);
1da177e4
LT
487
488/**
489 * cancel_rearming_delayed_work - reliably kill off a delayed keventd
490 * work whose handler rearms the delayed work.
491 * @work: the delayed work struct
492 */
493void cancel_rearming_delayed_work(struct work_struct *work)
494{
495 cancel_rearming_delayed_workqueue(keventd_wq, work);
496}
497EXPORT_SYMBOL(cancel_rearming_delayed_work);
498
1fa44eca
JB
499/**
500 * execute_in_process_context - reliably execute the routine with user context
501 * @fn: the function to execute
502 * @data: data to pass to the function
503 * @ew: guaranteed storage for the execute work structure (must
504 * be available when the work executes)
505 *
506 * Executes the function immediately if process context is available,
507 * otherwise schedules the function for delayed execution.
508 *
509 * Returns: 0 - function was executed
510 * 1 - function was scheduled for execution
511 */
512int execute_in_process_context(void (*fn)(void *data), void *data,
513 struct execute_work *ew)
514{
515 if (!in_interrupt()) {
516 fn(data);
517 return 0;
518 }
519
520 INIT_WORK(&ew->work, fn, data);
521 schedule_work(&ew->work);
522
523 return 1;
524}
525EXPORT_SYMBOL_GPL(execute_in_process_context);
526
1da177e4
LT
527int keventd_up(void)
528{
529 return keventd_wq != NULL;
530}
531
532int current_is_keventd(void)
533{
534 struct cpu_workqueue_struct *cwq;
535 int cpu = smp_processor_id(); /* preempt-safe: keventd is per-cpu */
536 int ret = 0;
537
538 BUG_ON(!keventd_wq);
539
89ada679 540 cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
1da177e4
LT
541 if (current == cwq->thread)
542 ret = 1;
543
544 return ret;
545
546}
547
548#ifdef CONFIG_HOTPLUG_CPU
549/* Take the work from this (downed) CPU. */
550static void take_over_work(struct workqueue_struct *wq, unsigned int cpu)
551{
89ada679 552 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
626ab0e6 553 struct list_head list;
1da177e4
LT
554 struct work_struct *work;
555
556 spin_lock_irq(&cwq->lock);
626ab0e6 557 list_replace_init(&cwq->worklist, &list);
1da177e4
LT
558
559 while (!list_empty(&list)) {
560 printk("Taking work for %s\n", wq->name);
561 work = list_entry(list.next,struct work_struct,entry);
562 list_del(&work->entry);
89ada679 563 __queue_work(per_cpu_ptr(wq->cpu_wq, smp_processor_id()), work);
1da177e4
LT
564 }
565 spin_unlock_irq(&cwq->lock);
566}
567
568/* We're holding the cpucontrol mutex here */
9c7b216d 569static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
1da177e4
LT
570 unsigned long action,
571 void *hcpu)
572{
573 unsigned int hotcpu = (unsigned long)hcpu;
574 struct workqueue_struct *wq;
575
576 switch (action) {
577 case CPU_UP_PREPARE:
578 /* Create a new workqueue thread for it. */
579 list_for_each_entry(wq, &workqueues, list) {
230649da 580 if (!create_workqueue_thread(wq, hotcpu)) {
1da177e4
LT
581 printk("workqueue for %i failed\n", hotcpu);
582 return NOTIFY_BAD;
583 }
584 }
585 break;
586
587 case CPU_ONLINE:
588 /* Kick off worker threads. */
589 list_for_each_entry(wq, &workqueues, list) {
89ada679
CL
590 struct cpu_workqueue_struct *cwq;
591
592 cwq = per_cpu_ptr(wq->cpu_wq, hotcpu);
593 kthread_bind(cwq->thread, hotcpu);
594 wake_up_process(cwq->thread);
1da177e4
LT
595 }
596 break;
597
598 case CPU_UP_CANCELED:
599 list_for_each_entry(wq, &workqueues, list) {
fc75cdfa
HC
600 if (!per_cpu_ptr(wq->cpu_wq, hotcpu)->thread)
601 continue;
1da177e4 602 /* Unbind so it can run. */
89ada679 603 kthread_bind(per_cpu_ptr(wq->cpu_wq, hotcpu)->thread,
a4c4af7c 604 any_online_cpu(cpu_online_map));
1da177e4
LT
605 cleanup_workqueue_thread(wq, hotcpu);
606 }
607 break;
608
609 case CPU_DEAD:
610 list_for_each_entry(wq, &workqueues, list)
611 cleanup_workqueue_thread(wq, hotcpu);
612 list_for_each_entry(wq, &workqueues, list)
613 take_over_work(wq, hotcpu);
614 break;
615 }
616
617 return NOTIFY_OK;
618}
619#endif
620
621void init_workqueues(void)
622{
f756d5e2 623 singlethread_cpu = first_cpu(cpu_possible_map);
1da177e4
LT
624 hotcpu_notifier(workqueue_cpu_callback, 0);
625 keventd_wq = create_workqueue("events");
626 BUG_ON(!keventd_wq);
627}
628
629EXPORT_SYMBOL_GPL(__create_workqueue);
630EXPORT_SYMBOL_GPL(queue_work);
631EXPORT_SYMBOL_GPL(queue_delayed_work);
7a6bc1cd 632EXPORT_SYMBOL_GPL(queue_delayed_work_on);
1da177e4
LT
633EXPORT_SYMBOL_GPL(flush_workqueue);
634EXPORT_SYMBOL_GPL(destroy_workqueue);
635
636EXPORT_SYMBOL(schedule_work);
637EXPORT_SYMBOL(schedule_delayed_work);
638EXPORT_SYMBOL(schedule_delayed_work_on);
639EXPORT_SYMBOL(flush_scheduled_work);