2 * linux/kernel/workqueue.c
4 * Generic mechanism for defining kernel helper threads for running
5 * arbitrary tasks in process context.
7 * Started by Ingo Molnar, Copyright (C) 2002
9 * Derived from the taskqueue/keventd code by:
11 * David Woodhouse <dwmw2@infradead.org>
13 * Kai Petzke <wpp@marie.physik.tu-berlin.de>
14 * Theodore Ts'o <tytso@mit.edu>
16 * Made to use alloc_percpu by Christoph Lameter.
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>
30 #include <linux/hardirq.h>
31 #include <linux/mempolicy.h>
32 #include <linux/freezer.h>
33 #include <linux/kallsyms.h>
34 #include <linux/debug_locks.h>
35 #include <linux/lockdep.h>
36 #include <linux/idr.h>
38 #include "workqueue_sched.h"
41 /* global_cwq flags */
42 GCWQ_MANAGE_WORKERS
= 1 << 0, /* need to manage workers */
43 GCWQ_MANAGING_WORKERS
= 1 << 1, /* managing workers */
44 GCWQ_DISASSOCIATED
= 1 << 2, /* cpu can't serve workers */
45 GCWQ_FREEZING
= 1 << 3, /* freeze in progress */
46 GCWQ_HIGHPRI_PENDING
= 1 << 4, /* highpri works on queue */
49 WORKER_STARTED
= 1 << 0, /* started */
50 WORKER_DIE
= 1 << 1, /* die die die */
51 WORKER_IDLE
= 1 << 2, /* is idle */
52 WORKER_PREP
= 1 << 3, /* preparing to run works */
53 WORKER_ROGUE
= 1 << 4, /* not bound to any cpu */
54 WORKER_REBIND
= 1 << 5, /* mom is home, come back */
55 WORKER_CPU_INTENSIVE
= 1 << 6, /* cpu intensive */
56 WORKER_UNBOUND
= 1 << 7, /* worker is unbound */
58 WORKER_NOT_RUNNING
= WORKER_PREP
| WORKER_ROGUE
| WORKER_REBIND
|
59 WORKER_CPU_INTENSIVE
| WORKER_UNBOUND
,
61 /* gcwq->trustee_state */
62 TRUSTEE_START
= 0, /* start */
63 TRUSTEE_IN_CHARGE
= 1, /* trustee in charge of gcwq */
64 TRUSTEE_BUTCHER
= 2, /* butcher workers */
65 TRUSTEE_RELEASE
= 3, /* release workers */
66 TRUSTEE_DONE
= 4, /* trustee is done */
68 BUSY_WORKER_HASH_ORDER
= 6, /* 64 pointers */
69 BUSY_WORKER_HASH_SIZE
= 1 << BUSY_WORKER_HASH_ORDER
,
70 BUSY_WORKER_HASH_MASK
= BUSY_WORKER_HASH_SIZE
- 1,
72 MAX_IDLE_WORKERS_RATIO
= 4, /* 1/4 of busy can be idle */
73 IDLE_WORKER_TIMEOUT
= 300 * HZ
, /* keep idle ones for 5 mins */
75 MAYDAY_INITIAL_TIMEOUT
= HZ
/ 100, /* call for help after 10ms */
76 MAYDAY_INTERVAL
= HZ
/ 10, /* and then every 100ms */
77 CREATE_COOLDOWN
= HZ
, /* time to breath after fail */
78 TRUSTEE_COOLDOWN
= HZ
/ 10, /* for trustee draining */
81 * Rescue workers are used only on emergencies and shared by
84 RESCUER_NICE_LEVEL
= -20,
88 * Structure fields follow one of the following exclusion rules.
90 * I: Set during initialization and read-only afterwards.
92 * P: Preemption protected. Disabling preemption is enough and should
93 * only be modified and accessed from the local cpu.
95 * L: gcwq->lock protected. Access with gcwq->lock held.
97 * X: During normal operation, modification requires gcwq->lock and
98 * should be done only from local cpu. Either disabling preemption
99 * on local cpu or grabbing gcwq->lock is enough for read access.
100 * If GCWQ_DISASSOCIATED is set, it's identical to L.
102 * F: wq->flush_mutex protected.
104 * W: workqueue_lock protected.
110 * The poor guys doing the actual heavy lifting. All on-duty workers
111 * are either serving the manager role, on idle list or on busy hash.
114 /* on idle list while idle, on busy hash table while busy */
116 struct list_head entry
; /* L: while idle */
117 struct hlist_node hentry
; /* L: while busy */
120 struct work_struct
*current_work
; /* L: work being processed */
121 struct cpu_workqueue_struct
*current_cwq
; /* L: current_work's cwq */
122 struct list_head scheduled
; /* L: scheduled works */
123 struct task_struct
*task
; /* I: worker task */
124 struct global_cwq
*gcwq
; /* I: the associated gcwq */
125 /* 64 bytes boundary on 64bit, 32 on 32bit */
126 unsigned long last_active
; /* L: last active timestamp */
127 unsigned int flags
; /* X: flags */
128 int id
; /* I: worker id */
129 struct work_struct rebind_work
; /* L: rebind worker to cpu */
133 * Global per-cpu workqueue. There's one and only one for each cpu
134 * and all works are queued and processed here regardless of their
138 spinlock_t lock
; /* the gcwq lock */
139 struct list_head worklist
; /* L: list of pending works */
140 unsigned int cpu
; /* I: the associated cpu */
141 unsigned int flags
; /* L: GCWQ_* flags */
143 int nr_workers
; /* L: total number of workers */
144 int nr_idle
; /* L: currently idle ones */
146 /* workers are chained either in the idle_list or busy_hash */
147 struct list_head idle_list
; /* X: list of idle workers */
148 struct hlist_head busy_hash
[BUSY_WORKER_HASH_SIZE
];
149 /* L: hash of busy workers */
151 struct timer_list idle_timer
; /* L: worker idle timeout */
152 struct timer_list mayday_timer
; /* L: SOS timer for dworkers */
154 struct ida worker_ida
; /* L: for worker IDs */
156 struct task_struct
*trustee
; /* L: for gcwq shutdown */
157 unsigned int trustee_state
; /* L: trustee state */
158 wait_queue_head_t trustee_wait
; /* trustee wait */
159 struct worker
*first_idle
; /* L: first idle worker */
160 } ____cacheline_aligned_in_smp
;
163 * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
164 * work_struct->data are used for flags and thus cwqs need to be
165 * aligned at two's power of the number of flag bits.
167 struct cpu_workqueue_struct
{
168 struct global_cwq
*gcwq
; /* I: the associated gcwq */
169 struct workqueue_struct
*wq
; /* I: the owning workqueue */
170 int work_color
; /* L: current color */
171 int flush_color
; /* L: flushing color */
172 int nr_in_flight
[WORK_NR_COLORS
];
173 /* L: nr of in_flight works */
174 int nr_active
; /* L: nr of active works */
175 int max_active
; /* L: max active works */
176 struct list_head delayed_works
; /* L: delayed works */
180 * Structure used to wait for workqueue flush.
183 struct list_head list
; /* F: list of flushers */
184 int flush_color
; /* F: flush color waiting for */
185 struct completion done
; /* flush completion */
189 * The externally visible workqueue abstraction is an array of
190 * per-CPU workqueues:
192 struct workqueue_struct
{
193 unsigned int flags
; /* I: WQ_* flags */
195 struct cpu_workqueue_struct __percpu
*pcpu
;
196 struct cpu_workqueue_struct
*single
;
198 } cpu_wq
; /* I: cwq's */
199 struct list_head list
; /* W: list of all workqueues */
201 struct mutex flush_mutex
; /* protects wq flushing */
202 int work_color
; /* F: current work color */
203 int flush_color
; /* F: current flush color */
204 atomic_t nr_cwqs_to_flush
; /* flush in progress */
205 struct wq_flusher
*first_flusher
; /* F: first flusher */
206 struct list_head flusher_queue
; /* F: flush waiters */
207 struct list_head flusher_overflow
; /* F: flush overflow list */
209 cpumask_var_t mayday_mask
; /* cpus requesting rescue */
210 struct worker
*rescuer
; /* I: rescue worker */
212 int saved_max_active
; /* W: saved cwq max_active */
213 const char *name
; /* I: workqueue name */
214 #ifdef CONFIG_LOCKDEP
215 struct lockdep_map lockdep_map
;
219 struct workqueue_struct
*system_wq __read_mostly
;
220 struct workqueue_struct
*system_long_wq __read_mostly
;
221 struct workqueue_struct
*system_nrt_wq __read_mostly
;
222 struct workqueue_struct
*system_unbound_wq __read_mostly
;
223 EXPORT_SYMBOL_GPL(system_wq
);
224 EXPORT_SYMBOL_GPL(system_long_wq
);
225 EXPORT_SYMBOL_GPL(system_nrt_wq
);
226 EXPORT_SYMBOL_GPL(system_unbound_wq
);
228 #define for_each_busy_worker(worker, i, pos, gcwq) \
229 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
230 hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
232 static inline int __next_gcwq_cpu(int cpu
, const struct cpumask
*mask
,
235 if (cpu
< nr_cpu_ids
) {
237 cpu
= cpumask_next(cpu
, mask
);
238 if (cpu
< nr_cpu_ids
)
242 return WORK_CPU_UNBOUND
;
244 return WORK_CPU_NONE
;
247 static inline int __next_wq_cpu(int cpu
, const struct cpumask
*mask
,
248 struct workqueue_struct
*wq
)
250 return __next_gcwq_cpu(cpu
, mask
, !(wq
->flags
& WQ_UNBOUND
) ? 1 : 2);
253 #define for_each_gcwq_cpu(cpu) \
254 for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
255 (cpu) < WORK_CPU_NONE; \
256 (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
258 #define for_each_online_gcwq_cpu(cpu) \
259 for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
260 (cpu) < WORK_CPU_NONE; \
261 (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
263 #define for_each_cwq_cpu(cpu, wq) \
264 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
265 (cpu) < WORK_CPU_NONE; \
266 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
268 #ifdef CONFIG_DEBUG_OBJECTS_WORK
270 static struct debug_obj_descr work_debug_descr
;
273 * fixup_init is called when:
274 * - an active object is initialized
276 static int work_fixup_init(void *addr
, enum debug_obj_state state
)
278 struct work_struct
*work
= addr
;
281 case ODEBUG_STATE_ACTIVE
:
282 cancel_work_sync(work
);
283 debug_object_init(work
, &work_debug_descr
);
291 * fixup_activate is called when:
292 * - an active object is activated
293 * - an unknown object is activated (might be a statically initialized object)
295 static int work_fixup_activate(void *addr
, enum debug_obj_state state
)
297 struct work_struct
*work
= addr
;
301 case ODEBUG_STATE_NOTAVAILABLE
:
303 * This is not really a fixup. The work struct was
304 * statically initialized. We just make sure that it
305 * is tracked in the object tracker.
307 if (test_bit(WORK_STRUCT_STATIC_BIT
, work_data_bits(work
))) {
308 debug_object_init(work
, &work_debug_descr
);
309 debug_object_activate(work
, &work_debug_descr
);
315 case ODEBUG_STATE_ACTIVE
:
324 * fixup_free is called when:
325 * - an active object is freed
327 static int work_fixup_free(void *addr
, enum debug_obj_state state
)
329 struct work_struct
*work
= addr
;
332 case ODEBUG_STATE_ACTIVE
:
333 cancel_work_sync(work
);
334 debug_object_free(work
, &work_debug_descr
);
341 static struct debug_obj_descr work_debug_descr
= {
342 .name
= "work_struct",
343 .fixup_init
= work_fixup_init
,
344 .fixup_activate
= work_fixup_activate
,
345 .fixup_free
= work_fixup_free
,
348 static inline void debug_work_activate(struct work_struct
*work
)
350 debug_object_activate(work
, &work_debug_descr
);
353 static inline void debug_work_deactivate(struct work_struct
*work
)
355 debug_object_deactivate(work
, &work_debug_descr
);
358 void __init_work(struct work_struct
*work
, int onstack
)
361 debug_object_init_on_stack(work
, &work_debug_descr
);
363 debug_object_init(work
, &work_debug_descr
);
365 EXPORT_SYMBOL_GPL(__init_work
);
367 void destroy_work_on_stack(struct work_struct
*work
)
369 debug_object_free(work
, &work_debug_descr
);
371 EXPORT_SYMBOL_GPL(destroy_work_on_stack
);
374 static inline void debug_work_activate(struct work_struct
*work
) { }
375 static inline void debug_work_deactivate(struct work_struct
*work
) { }
378 /* Serializes the accesses to the list of workqueues. */
379 static DEFINE_SPINLOCK(workqueue_lock
);
380 static LIST_HEAD(workqueues
);
381 static bool workqueue_freezing
; /* W: have wqs started freezing? */
384 * The almighty global cpu workqueues. nr_running is the only field
385 * which is expected to be used frequently by other cpus via
386 * try_to_wake_up(). Put it in a separate cacheline.
388 static DEFINE_PER_CPU(struct global_cwq
, global_cwq
);
389 static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t
, gcwq_nr_running
);
392 * Global cpu workqueue and nr_running counter for unbound gcwq. The
393 * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its
394 * workers have WORKER_UNBOUND set.
396 static struct global_cwq unbound_global_cwq
;
397 static atomic_t unbound_gcwq_nr_running
= ATOMIC_INIT(0); /* always 0 */
399 static int worker_thread(void *__worker
);
401 static struct global_cwq
*get_gcwq(unsigned int cpu
)
403 if (cpu
!= WORK_CPU_UNBOUND
)
404 return &per_cpu(global_cwq
, cpu
);
406 return &unbound_global_cwq
;
409 static atomic_t
*get_gcwq_nr_running(unsigned int cpu
)
411 if (cpu
!= WORK_CPU_UNBOUND
)
412 return &per_cpu(gcwq_nr_running
, cpu
);
414 return &unbound_gcwq_nr_running
;
417 static struct cpu_workqueue_struct
*get_cwq(unsigned int cpu
,
418 struct workqueue_struct
*wq
)
420 if (!(wq
->flags
& WQ_UNBOUND
)) {
421 if (likely(cpu
< nr_cpu_ids
)) {
423 return per_cpu_ptr(wq
->cpu_wq
.pcpu
, cpu
);
425 return wq
->cpu_wq
.single
;
428 } else if (likely(cpu
== WORK_CPU_UNBOUND
))
429 return wq
->cpu_wq
.single
;
433 static unsigned int work_color_to_flags(int color
)
435 return color
<< WORK_STRUCT_COLOR_SHIFT
;
438 static int get_work_color(struct work_struct
*work
)
440 return (*work_data_bits(work
) >> WORK_STRUCT_COLOR_SHIFT
) &
441 ((1 << WORK_STRUCT_COLOR_BITS
) - 1);
444 static int work_next_color(int color
)
446 return (color
+ 1) % WORK_NR_COLORS
;
450 * Work data points to the cwq while a work is on queue. Once
451 * execution starts, it points to the cpu the work was last on. This
452 * can be distinguished by comparing the data value against
455 * set_work_{cwq|cpu}() and clear_work_data() can be used to set the
456 * cwq, cpu or clear work->data. These functions should only be
457 * called while the work is owned - ie. while the PENDING bit is set.
459 * get_work_[g]cwq() can be used to obtain the gcwq or cwq
460 * corresponding to a work. gcwq is available once the work has been
461 * queued anywhere after initialization. cwq is available only from
462 * queueing until execution starts.
464 static inline void set_work_data(struct work_struct
*work
, unsigned long data
,
467 BUG_ON(!work_pending(work
));
468 atomic_long_set(&work
->data
, data
| flags
| work_static(work
));
471 static void set_work_cwq(struct work_struct
*work
,
472 struct cpu_workqueue_struct
*cwq
,
473 unsigned long extra_flags
)
475 set_work_data(work
, (unsigned long)cwq
,
476 WORK_STRUCT_PENDING
| extra_flags
);
479 static void set_work_cpu(struct work_struct
*work
, unsigned int cpu
)
481 set_work_data(work
, cpu
<< WORK_STRUCT_FLAG_BITS
, WORK_STRUCT_PENDING
);
484 static void clear_work_data(struct work_struct
*work
)
486 set_work_data(work
, WORK_STRUCT_NO_CPU
, 0);
489 static inline unsigned long get_work_data(struct work_struct
*work
)
491 return atomic_long_read(&work
->data
) & WORK_STRUCT_WQ_DATA_MASK
;
494 static struct cpu_workqueue_struct
*get_work_cwq(struct work_struct
*work
)
496 unsigned long data
= get_work_data(work
);
498 return data
>= PAGE_OFFSET
? (void *)data
: NULL
;
501 static struct global_cwq
*get_work_gcwq(struct work_struct
*work
)
503 unsigned long data
= get_work_data(work
);
506 if (data
>= PAGE_OFFSET
)
507 return ((struct cpu_workqueue_struct
*)data
)->gcwq
;
509 cpu
= data
>> WORK_STRUCT_FLAG_BITS
;
510 if (cpu
== WORK_CPU_NONE
)
513 BUG_ON(cpu
>= nr_cpu_ids
&& cpu
!= WORK_CPU_UNBOUND
);
514 return get_gcwq(cpu
);
518 * Policy functions. These define the policies on how the global
519 * worker pool is managed. Unless noted otherwise, these functions
520 * assume that they're being called with gcwq->lock held.
523 static bool __need_more_worker(struct global_cwq
*gcwq
)
525 return !atomic_read(get_gcwq_nr_running(gcwq
->cpu
)) ||
526 gcwq
->flags
& GCWQ_HIGHPRI_PENDING
;
530 * Need to wake up a worker? Called from anything but currently
533 static bool need_more_worker(struct global_cwq
*gcwq
)
535 return !list_empty(&gcwq
->worklist
) && __need_more_worker(gcwq
);
538 /* Can I start working? Called from busy but !running workers. */
539 static bool may_start_working(struct global_cwq
*gcwq
)
541 return gcwq
->nr_idle
;
544 /* Do I need to keep working? Called from currently running workers. */
545 static bool keep_working(struct global_cwq
*gcwq
)
547 atomic_t
*nr_running
= get_gcwq_nr_running(gcwq
->cpu
);
549 return !list_empty(&gcwq
->worklist
) && atomic_read(nr_running
) <= 1;
552 /* Do we need a new worker? Called from manager. */
553 static bool need_to_create_worker(struct global_cwq
*gcwq
)
555 return need_more_worker(gcwq
) && !may_start_working(gcwq
);
558 /* Do I need to be the manager? */
559 static bool need_to_manage_workers(struct global_cwq
*gcwq
)
561 return need_to_create_worker(gcwq
) || gcwq
->flags
& GCWQ_MANAGE_WORKERS
;
564 /* Do we have too many workers and should some go away? */
565 static bool too_many_workers(struct global_cwq
*gcwq
)
567 bool managing
= gcwq
->flags
& GCWQ_MANAGING_WORKERS
;
568 int nr_idle
= gcwq
->nr_idle
+ managing
; /* manager is considered idle */
569 int nr_busy
= gcwq
->nr_workers
- nr_idle
;
571 return nr_idle
> 2 && (nr_idle
- 2) * MAX_IDLE_WORKERS_RATIO
>= nr_busy
;
578 /* Return the first worker. Safe with preemption disabled */
579 static struct worker
*first_worker(struct global_cwq
*gcwq
)
581 if (unlikely(list_empty(&gcwq
->idle_list
)))
584 return list_first_entry(&gcwq
->idle_list
, struct worker
, entry
);
588 * wake_up_worker - wake up an idle worker
589 * @gcwq: gcwq to wake worker for
591 * Wake up the first idle worker of @gcwq.
594 * spin_lock_irq(gcwq->lock).
596 static void wake_up_worker(struct global_cwq
*gcwq
)
598 struct worker
*worker
= first_worker(gcwq
);
601 wake_up_process(worker
->task
);
605 * wq_worker_waking_up - a worker is waking up
606 * @task: task waking up
607 * @cpu: CPU @task is waking up to
609 * This function is called during try_to_wake_up() when a worker is
613 * spin_lock_irq(rq->lock)
615 void wq_worker_waking_up(struct task_struct
*task
, unsigned int cpu
)
617 struct worker
*worker
= kthread_data(task
);
619 if (likely(!(worker
->flags
& WORKER_NOT_RUNNING
)))
620 atomic_inc(get_gcwq_nr_running(cpu
));
624 * wq_worker_sleeping - a worker is going to sleep
625 * @task: task going to sleep
626 * @cpu: CPU in question, must be the current CPU number
628 * This function is called during schedule() when a busy worker is
629 * going to sleep. Worker on the same cpu can be woken up by
630 * returning pointer to its task.
633 * spin_lock_irq(rq->lock)
636 * Worker task on @cpu to wake up, %NULL if none.
638 struct task_struct
*wq_worker_sleeping(struct task_struct
*task
,
641 struct worker
*worker
= kthread_data(task
), *to_wakeup
= NULL
;
642 struct global_cwq
*gcwq
= get_gcwq(cpu
);
643 atomic_t
*nr_running
= get_gcwq_nr_running(cpu
);
645 if (unlikely(worker
->flags
& WORKER_NOT_RUNNING
))
648 /* this can only happen on the local cpu */
649 BUG_ON(cpu
!= raw_smp_processor_id());
652 * The counterpart of the following dec_and_test, implied mb,
653 * worklist not empty test sequence is in insert_work().
654 * Please read comment there.
656 * NOT_RUNNING is clear. This means that trustee is not in
657 * charge and we're running on the local cpu w/ rq lock held
658 * and preemption disabled, which in turn means that none else
659 * could be manipulating idle_list, so dereferencing idle_list
660 * without gcwq lock is safe.
662 if (atomic_dec_and_test(nr_running
) && !list_empty(&gcwq
->worklist
))
663 to_wakeup
= first_worker(gcwq
);
664 return to_wakeup
? to_wakeup
->task
: NULL
;
668 * worker_set_flags - set worker flags and adjust nr_running accordingly
670 * @flags: flags to set
671 * @wakeup: wakeup an idle worker if necessary
673 * Set @flags in @worker->flags and adjust nr_running accordingly. If
674 * nr_running becomes zero and @wakeup is %true, an idle worker is
678 * spin_lock_irq(gcwq->lock)
680 static inline void worker_set_flags(struct worker
*worker
, unsigned int flags
,
683 struct global_cwq
*gcwq
= worker
->gcwq
;
685 WARN_ON_ONCE(worker
->task
!= current
);
688 * If transitioning into NOT_RUNNING, adjust nr_running and
689 * wake up an idle worker as necessary if requested by
692 if ((flags
& WORKER_NOT_RUNNING
) &&
693 !(worker
->flags
& WORKER_NOT_RUNNING
)) {
694 atomic_t
*nr_running
= get_gcwq_nr_running(gcwq
->cpu
);
697 if (atomic_dec_and_test(nr_running
) &&
698 !list_empty(&gcwq
->worklist
))
699 wake_up_worker(gcwq
);
701 atomic_dec(nr_running
);
704 worker
->flags
|= flags
;
708 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
710 * @flags: flags to clear
712 * Clear @flags in @worker->flags and adjust nr_running accordingly.
715 * spin_lock_irq(gcwq->lock)
717 static inline void worker_clr_flags(struct worker
*worker
, unsigned int flags
)
719 struct global_cwq
*gcwq
= worker
->gcwq
;
720 unsigned int oflags
= worker
->flags
;
722 WARN_ON_ONCE(worker
->task
!= current
);
724 worker
->flags
&= ~flags
;
726 /* if transitioning out of NOT_RUNNING, increment nr_running */
727 if ((flags
& WORKER_NOT_RUNNING
) && (oflags
& WORKER_NOT_RUNNING
))
728 if (!(worker
->flags
& WORKER_NOT_RUNNING
))
729 atomic_inc(get_gcwq_nr_running(gcwq
->cpu
));
733 * busy_worker_head - return the busy hash head for a work
734 * @gcwq: gcwq of interest
735 * @work: work to be hashed
737 * Return hash head of @gcwq for @work.
740 * spin_lock_irq(gcwq->lock).
743 * Pointer to the hash head.
745 static struct hlist_head
*busy_worker_head(struct global_cwq
*gcwq
,
746 struct work_struct
*work
)
748 const int base_shift
= ilog2(sizeof(struct work_struct
));
749 unsigned long v
= (unsigned long)work
;
751 /* simple shift and fold hash, do we need something better? */
753 v
+= v
>> BUSY_WORKER_HASH_ORDER
;
754 v
&= BUSY_WORKER_HASH_MASK
;
756 return &gcwq
->busy_hash
[v
];
760 * __find_worker_executing_work - find worker which is executing a work
761 * @gcwq: gcwq of interest
762 * @bwh: hash head as returned by busy_worker_head()
763 * @work: work to find worker for
765 * Find a worker which is executing @work on @gcwq. @bwh should be
766 * the hash head obtained by calling busy_worker_head() with the same
770 * spin_lock_irq(gcwq->lock).
773 * Pointer to worker which is executing @work if found, NULL
776 static struct worker
*__find_worker_executing_work(struct global_cwq
*gcwq
,
777 struct hlist_head
*bwh
,
778 struct work_struct
*work
)
780 struct worker
*worker
;
781 struct hlist_node
*tmp
;
783 hlist_for_each_entry(worker
, tmp
, bwh
, hentry
)
784 if (worker
->current_work
== work
)
790 * find_worker_executing_work - find worker which is executing a work
791 * @gcwq: gcwq of interest
792 * @work: work to find worker for
794 * Find a worker which is executing @work on @gcwq. This function is
795 * identical to __find_worker_executing_work() except that this
796 * function calculates @bwh itself.
799 * spin_lock_irq(gcwq->lock).
802 * Pointer to worker which is executing @work if found, NULL
805 static struct worker
*find_worker_executing_work(struct global_cwq
*gcwq
,
806 struct work_struct
*work
)
808 return __find_worker_executing_work(gcwq
, busy_worker_head(gcwq
, work
),
813 * gcwq_determine_ins_pos - find insertion position
814 * @gcwq: gcwq of interest
815 * @cwq: cwq a work is being queued for
817 * A work for @cwq is about to be queued on @gcwq, determine insertion
818 * position for the work. If @cwq is for HIGHPRI wq, the work is
819 * queued at the head of the queue but in FIFO order with respect to
820 * other HIGHPRI works; otherwise, at the end of the queue. This
821 * function also sets GCWQ_HIGHPRI_PENDING flag to hint @gcwq that
822 * there are HIGHPRI works pending.
825 * spin_lock_irq(gcwq->lock).
828 * Pointer to inserstion position.
830 static inline struct list_head
*gcwq_determine_ins_pos(struct global_cwq
*gcwq
,
831 struct cpu_workqueue_struct
*cwq
)
833 struct work_struct
*twork
;
835 if (likely(!(cwq
->wq
->flags
& WQ_HIGHPRI
)))
836 return &gcwq
->worklist
;
838 list_for_each_entry(twork
, &gcwq
->worklist
, entry
) {
839 struct cpu_workqueue_struct
*tcwq
= get_work_cwq(twork
);
841 if (!(tcwq
->wq
->flags
& WQ_HIGHPRI
))
845 gcwq
->flags
|= GCWQ_HIGHPRI_PENDING
;
846 return &twork
->entry
;
850 * insert_work - insert a work into gcwq
851 * @cwq: cwq @work belongs to
852 * @work: work to insert
853 * @head: insertion point
854 * @extra_flags: extra WORK_STRUCT_* flags to set
856 * Insert @work which belongs to @cwq into @gcwq after @head.
857 * @extra_flags is or'd to work_struct flags.
860 * spin_lock_irq(gcwq->lock).
862 static void insert_work(struct cpu_workqueue_struct
*cwq
,
863 struct work_struct
*work
, struct list_head
*head
,
864 unsigned int extra_flags
)
866 struct global_cwq
*gcwq
= cwq
->gcwq
;
868 /* we own @work, set data and link */
869 set_work_cwq(work
, cwq
, extra_flags
);
872 * Ensure that we get the right work->data if we see the
873 * result of list_add() below, see try_to_grab_pending().
877 list_add_tail(&work
->entry
, head
);
880 * Ensure either worker_sched_deactivated() sees the above
881 * list_add_tail() or we see zero nr_running to avoid workers
882 * lying around lazily while there are works to be processed.
886 if (__need_more_worker(gcwq
))
887 wake_up_worker(gcwq
);
890 static void __queue_work(unsigned int cpu
, struct workqueue_struct
*wq
,
891 struct work_struct
*work
)
893 struct global_cwq
*gcwq
;
894 struct cpu_workqueue_struct
*cwq
;
895 struct list_head
*worklist
;
898 debug_work_activate(work
);
900 /* determine gcwq to use */
901 if (!(wq
->flags
& WQ_UNBOUND
)) {
902 struct global_cwq
*last_gcwq
;
904 if (unlikely(cpu
== WORK_CPU_UNBOUND
))
905 cpu
= raw_smp_processor_id();
908 * It's multi cpu. If @wq is non-reentrant and @work
909 * was previously on a different cpu, it might still
910 * be running there, in which case the work needs to
911 * be queued on that cpu to guarantee non-reentrance.
913 gcwq
= get_gcwq(cpu
);
914 if (wq
->flags
& WQ_NON_REENTRANT
&&
915 (last_gcwq
= get_work_gcwq(work
)) && last_gcwq
!= gcwq
) {
916 struct worker
*worker
;
918 spin_lock_irqsave(&last_gcwq
->lock
, flags
);
920 worker
= find_worker_executing_work(last_gcwq
, work
);
922 if (worker
&& worker
->current_cwq
->wq
== wq
)
925 /* meh... not running there, queue here */
926 spin_unlock_irqrestore(&last_gcwq
->lock
, flags
);
927 spin_lock_irqsave(&gcwq
->lock
, flags
);
930 spin_lock_irqsave(&gcwq
->lock
, flags
);
932 gcwq
= get_gcwq(WORK_CPU_UNBOUND
);
933 spin_lock_irqsave(&gcwq
->lock
, flags
);
936 /* gcwq determined, get cwq and queue */
937 cwq
= get_cwq(gcwq
->cpu
, wq
);
939 BUG_ON(!list_empty(&work
->entry
));
941 cwq
->nr_in_flight
[cwq
->work_color
]++;
943 if (likely(cwq
->nr_active
< cwq
->max_active
)) {
945 worklist
= gcwq_determine_ins_pos(gcwq
, cwq
);
947 worklist
= &cwq
->delayed_works
;
949 insert_work(cwq
, work
, worklist
, work_color_to_flags(cwq
->work_color
));
951 spin_unlock_irqrestore(&gcwq
->lock
, flags
);
955 * queue_work - queue work on a workqueue
956 * @wq: workqueue to use
957 * @work: work to queue
959 * Returns 0 if @work was already on a queue, non-zero otherwise.
961 * We queue the work to the CPU on which it was submitted, but if the CPU dies
962 * it can be processed by another CPU.
964 int queue_work(struct workqueue_struct
*wq
, struct work_struct
*work
)
968 ret
= queue_work_on(get_cpu(), wq
, work
);
973 EXPORT_SYMBOL_GPL(queue_work
);
976 * queue_work_on - queue work on specific cpu
977 * @cpu: CPU number to execute work on
978 * @wq: workqueue to use
979 * @work: work to queue
981 * Returns 0 if @work was already on a queue, non-zero otherwise.
983 * We queue the work to a specific CPU, the caller must ensure it
987 queue_work_on(int cpu
, struct workqueue_struct
*wq
, struct work_struct
*work
)
991 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT
, work_data_bits(work
))) {
992 __queue_work(cpu
, wq
, work
);
997 EXPORT_SYMBOL_GPL(queue_work_on
);
999 static void delayed_work_timer_fn(unsigned long __data
)
1001 struct delayed_work
*dwork
= (struct delayed_work
*)__data
;
1002 struct cpu_workqueue_struct
*cwq
= get_work_cwq(&dwork
->work
);
1004 __queue_work(smp_processor_id(), cwq
->wq
, &dwork
->work
);
1008 * queue_delayed_work - queue work on a workqueue after delay
1009 * @wq: workqueue to use
1010 * @dwork: delayable work to queue
1011 * @delay: number of jiffies to wait before queueing
1013 * Returns 0 if @work was already on a queue, non-zero otherwise.
1015 int queue_delayed_work(struct workqueue_struct
*wq
,
1016 struct delayed_work
*dwork
, unsigned long delay
)
1019 return queue_work(wq
, &dwork
->work
);
1021 return queue_delayed_work_on(-1, wq
, dwork
, delay
);
1023 EXPORT_SYMBOL_GPL(queue_delayed_work
);
1026 * queue_delayed_work_on - queue work on specific CPU after delay
1027 * @cpu: CPU number to execute work on
1028 * @wq: workqueue to use
1029 * @dwork: work to queue
1030 * @delay: number of jiffies to wait before queueing
1032 * Returns 0 if @work was already on a queue, non-zero otherwise.
1034 int queue_delayed_work_on(int cpu
, struct workqueue_struct
*wq
,
1035 struct delayed_work
*dwork
, unsigned long delay
)
1038 struct timer_list
*timer
= &dwork
->timer
;
1039 struct work_struct
*work
= &dwork
->work
;
1041 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT
, work_data_bits(work
))) {
1044 BUG_ON(timer_pending(timer
));
1045 BUG_ON(!list_empty(&work
->entry
));
1047 timer_stats_timer_set_start_info(&dwork
->timer
);
1050 * This stores cwq for the moment, for the timer_fn.
1051 * Note that the work's gcwq is preserved to allow
1052 * reentrance detection for delayed works.
1054 if (!(wq
->flags
& WQ_UNBOUND
)) {
1055 struct global_cwq
*gcwq
= get_work_gcwq(work
);
1057 if (gcwq
&& gcwq
->cpu
!= WORK_CPU_UNBOUND
)
1060 lcpu
= raw_smp_processor_id();
1062 lcpu
= WORK_CPU_UNBOUND
;
1064 set_work_cwq(work
, get_cwq(lcpu
, wq
), 0);
1066 timer
->expires
= jiffies
+ delay
;
1067 timer
->data
= (unsigned long)dwork
;
1068 timer
->function
= delayed_work_timer_fn
;
1070 if (unlikely(cpu
>= 0))
1071 add_timer_on(timer
, cpu
);
1078 EXPORT_SYMBOL_GPL(queue_delayed_work_on
);
1081 * worker_enter_idle - enter idle state
1082 * @worker: worker which is entering idle state
1084 * @worker is entering idle state. Update stats and idle timer if
1088 * spin_lock_irq(gcwq->lock).
1090 static void worker_enter_idle(struct worker
*worker
)
1092 struct global_cwq
*gcwq
= worker
->gcwq
;
1094 BUG_ON(worker
->flags
& WORKER_IDLE
);
1095 BUG_ON(!list_empty(&worker
->entry
) &&
1096 (worker
->hentry
.next
|| worker
->hentry
.pprev
));
1098 /* can't use worker_set_flags(), also called from start_worker() */
1099 worker
->flags
|= WORKER_IDLE
;
1101 worker
->last_active
= jiffies
;
1103 /* idle_list is LIFO */
1104 list_add(&worker
->entry
, &gcwq
->idle_list
);
1106 if (likely(!(worker
->flags
& WORKER_ROGUE
))) {
1107 if (too_many_workers(gcwq
) && !timer_pending(&gcwq
->idle_timer
))
1108 mod_timer(&gcwq
->idle_timer
,
1109 jiffies
+ IDLE_WORKER_TIMEOUT
);
1111 wake_up_all(&gcwq
->trustee_wait
);
1113 /* sanity check nr_running */
1114 WARN_ON_ONCE(gcwq
->nr_workers
== gcwq
->nr_idle
&&
1115 atomic_read(get_gcwq_nr_running(gcwq
->cpu
)));
1119 * worker_leave_idle - leave idle state
1120 * @worker: worker which is leaving idle state
1122 * @worker is leaving idle state. Update stats.
1125 * spin_lock_irq(gcwq->lock).
1127 static void worker_leave_idle(struct worker
*worker
)
1129 struct global_cwq
*gcwq
= worker
->gcwq
;
1131 BUG_ON(!(worker
->flags
& WORKER_IDLE
));
1132 worker_clr_flags(worker
, WORKER_IDLE
);
1134 list_del_init(&worker
->entry
);
1138 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1141 * Works which are scheduled while the cpu is online must at least be
1142 * scheduled to a worker which is bound to the cpu so that if they are
1143 * flushed from cpu callbacks while cpu is going down, they are
1144 * guaranteed to execute on the cpu.
1146 * This function is to be used by rogue workers and rescuers to bind
1147 * themselves to the target cpu and may race with cpu going down or
1148 * coming online. kthread_bind() can't be used because it may put the
1149 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1150 * verbatim as it's best effort and blocking and gcwq may be
1151 * [dis]associated in the meantime.
1153 * This function tries set_cpus_allowed() and locks gcwq and verifies
1154 * the binding against GCWQ_DISASSOCIATED which is set during
1155 * CPU_DYING and cleared during CPU_ONLINE, so if the worker enters
1156 * idle state or fetches works without dropping lock, it can guarantee
1157 * the scheduling requirement described in the first paragraph.
1160 * Might sleep. Called without any lock but returns with gcwq->lock
1164 * %true if the associated gcwq is online (@worker is successfully
1165 * bound), %false if offline.
1167 static bool worker_maybe_bind_and_lock(struct worker
*worker
)
1169 struct global_cwq
*gcwq
= worker
->gcwq
;
1170 struct task_struct
*task
= worker
->task
;
1174 * The following call may fail, succeed or succeed
1175 * without actually migrating the task to the cpu if
1176 * it races with cpu hotunplug operation. Verify
1177 * against GCWQ_DISASSOCIATED.
1179 if (!(gcwq
->flags
& GCWQ_DISASSOCIATED
))
1180 set_cpus_allowed_ptr(task
, get_cpu_mask(gcwq
->cpu
));
1182 spin_lock_irq(&gcwq
->lock
);
1183 if (gcwq
->flags
& GCWQ_DISASSOCIATED
)
1185 if (task_cpu(task
) == gcwq
->cpu
&&
1186 cpumask_equal(¤t
->cpus_allowed
,
1187 get_cpu_mask(gcwq
->cpu
)))
1189 spin_unlock_irq(&gcwq
->lock
);
1191 /* CPU has come up inbetween, retry migration */
1197 * Function for worker->rebind_work used to rebind rogue busy workers
1198 * to the associated cpu which is coming back online. This is
1199 * scheduled by cpu up but can race with other cpu hotplug operations
1200 * and may be executed twice without intervening cpu down.
1202 static void worker_rebind_fn(struct work_struct
*work
)
1204 struct worker
*worker
= container_of(work
, struct worker
, rebind_work
);
1205 struct global_cwq
*gcwq
= worker
->gcwq
;
1207 if (worker_maybe_bind_and_lock(worker
))
1208 worker_clr_flags(worker
, WORKER_REBIND
);
1210 spin_unlock_irq(&gcwq
->lock
);
1213 static struct worker
*alloc_worker(void)
1215 struct worker
*worker
;
1217 worker
= kzalloc(sizeof(*worker
), GFP_KERNEL
);
1219 INIT_LIST_HEAD(&worker
->entry
);
1220 INIT_LIST_HEAD(&worker
->scheduled
);
1221 INIT_WORK(&worker
->rebind_work
, worker_rebind_fn
);
1222 /* on creation a worker is in !idle && prep state */
1223 worker
->flags
= WORKER_PREP
;
1229 * create_worker - create a new workqueue worker
1230 * @gcwq: gcwq the new worker will belong to
1231 * @bind: whether to set affinity to @cpu or not
1233 * Create a new worker which is bound to @gcwq. The returned worker
1234 * can be started by calling start_worker() or destroyed using
1238 * Might sleep. Does GFP_KERNEL allocations.
1241 * Pointer to the newly created worker.
1243 static struct worker
*create_worker(struct global_cwq
*gcwq
, bool bind
)
1245 bool on_unbound_cpu
= gcwq
->cpu
== WORK_CPU_UNBOUND
;
1246 struct worker
*worker
= NULL
;
1249 spin_lock_irq(&gcwq
->lock
);
1250 while (ida_get_new(&gcwq
->worker_ida
, &id
)) {
1251 spin_unlock_irq(&gcwq
->lock
);
1252 if (!ida_pre_get(&gcwq
->worker_ida
, GFP_KERNEL
))
1254 spin_lock_irq(&gcwq
->lock
);
1256 spin_unlock_irq(&gcwq
->lock
);
1258 worker
= alloc_worker();
1262 worker
->gcwq
= gcwq
;
1265 if (!on_unbound_cpu
)
1266 worker
->task
= kthread_create(worker_thread
, worker
,
1267 "kworker/%u:%d", gcwq
->cpu
, id
);
1269 worker
->task
= kthread_create(worker_thread
, worker
,
1270 "kworker/u:%d", id
);
1271 if (IS_ERR(worker
->task
))
1275 * A rogue worker will become a regular one if CPU comes
1276 * online later on. Make sure every worker has
1277 * PF_THREAD_BOUND set.
1279 if (bind
&& !on_unbound_cpu
)
1280 kthread_bind(worker
->task
, gcwq
->cpu
);
1282 worker
->task
->flags
|= PF_THREAD_BOUND
;
1284 worker
->flags
|= WORKER_UNBOUND
;
1290 spin_lock_irq(&gcwq
->lock
);
1291 ida_remove(&gcwq
->worker_ida
, id
);
1292 spin_unlock_irq(&gcwq
->lock
);
1299 * start_worker - start a newly created worker
1300 * @worker: worker to start
1302 * Make the gcwq aware of @worker and start it.
1305 * spin_lock_irq(gcwq->lock).
1307 static void start_worker(struct worker
*worker
)
1309 worker
->flags
|= WORKER_STARTED
;
1310 worker
->gcwq
->nr_workers
++;
1311 worker_enter_idle(worker
);
1312 wake_up_process(worker
->task
);
1316 * destroy_worker - destroy a workqueue worker
1317 * @worker: worker to be destroyed
1319 * Destroy @worker and adjust @gcwq stats accordingly.
1322 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
1324 static void destroy_worker(struct worker
*worker
)
1326 struct global_cwq
*gcwq
= worker
->gcwq
;
1327 int id
= worker
->id
;
1329 /* sanity check frenzy */
1330 BUG_ON(worker
->current_work
);
1331 BUG_ON(!list_empty(&worker
->scheduled
));
1333 if (worker
->flags
& WORKER_STARTED
)
1335 if (worker
->flags
& WORKER_IDLE
)
1338 list_del_init(&worker
->entry
);
1339 worker
->flags
|= WORKER_DIE
;
1341 spin_unlock_irq(&gcwq
->lock
);
1343 kthread_stop(worker
->task
);
1346 spin_lock_irq(&gcwq
->lock
);
1347 ida_remove(&gcwq
->worker_ida
, id
);
1350 static void idle_worker_timeout(unsigned long __gcwq
)
1352 struct global_cwq
*gcwq
= (void *)__gcwq
;
1354 spin_lock_irq(&gcwq
->lock
);
1356 if (too_many_workers(gcwq
)) {
1357 struct worker
*worker
;
1358 unsigned long expires
;
1360 /* idle_list is kept in LIFO order, check the last one */
1361 worker
= list_entry(gcwq
->idle_list
.prev
, struct worker
, entry
);
1362 expires
= worker
->last_active
+ IDLE_WORKER_TIMEOUT
;
1364 if (time_before(jiffies
, expires
))
1365 mod_timer(&gcwq
->idle_timer
, expires
);
1367 /* it's been idle for too long, wake up manager */
1368 gcwq
->flags
|= GCWQ_MANAGE_WORKERS
;
1369 wake_up_worker(gcwq
);
1373 spin_unlock_irq(&gcwq
->lock
);
1376 static bool send_mayday(struct work_struct
*work
)
1378 struct cpu_workqueue_struct
*cwq
= get_work_cwq(work
);
1379 struct workqueue_struct
*wq
= cwq
->wq
;
1382 if (!(wq
->flags
& WQ_RESCUER
))
1385 /* mayday mayday mayday */
1386 cpu
= cwq
->gcwq
->cpu
;
1387 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1388 if (cpu
== WORK_CPU_UNBOUND
)
1390 if (!cpumask_test_and_set_cpu(cpu
, wq
->mayday_mask
))
1391 wake_up_process(wq
->rescuer
->task
);
1395 static void gcwq_mayday_timeout(unsigned long __gcwq
)
1397 struct global_cwq
*gcwq
= (void *)__gcwq
;
1398 struct work_struct
*work
;
1400 spin_lock_irq(&gcwq
->lock
);
1402 if (need_to_create_worker(gcwq
)) {
1404 * We've been trying to create a new worker but
1405 * haven't been successful. We might be hitting an
1406 * allocation deadlock. Send distress signals to
1409 list_for_each_entry(work
, &gcwq
->worklist
, entry
)
1413 spin_unlock_irq(&gcwq
->lock
);
1415 mod_timer(&gcwq
->mayday_timer
, jiffies
+ MAYDAY_INTERVAL
);
1419 * maybe_create_worker - create a new worker if necessary
1420 * @gcwq: gcwq to create a new worker for
1422 * Create a new worker for @gcwq if necessary. @gcwq is guaranteed to
1423 * have at least one idle worker on return from this function. If
1424 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
1425 * sent to all rescuers with works scheduled on @gcwq to resolve
1426 * possible allocation deadlock.
1428 * On return, need_to_create_worker() is guaranteed to be false and
1429 * may_start_working() true.
1432 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1433 * multiple times. Does GFP_KERNEL allocations. Called only from
1437 * false if no action was taken and gcwq->lock stayed locked, true
1440 static bool maybe_create_worker(struct global_cwq
*gcwq
)
1442 if (!need_to_create_worker(gcwq
))
1445 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
1446 mod_timer(&gcwq
->mayday_timer
, jiffies
+ MAYDAY_INITIAL_TIMEOUT
);
1449 struct worker
*worker
;
1451 spin_unlock_irq(&gcwq
->lock
);
1453 worker
= create_worker(gcwq
, true);
1455 del_timer_sync(&gcwq
->mayday_timer
);
1456 spin_lock_irq(&gcwq
->lock
);
1457 start_worker(worker
);
1458 BUG_ON(need_to_create_worker(gcwq
));
1462 if (!need_to_create_worker(gcwq
))
1465 spin_unlock_irq(&gcwq
->lock
);
1466 __set_current_state(TASK_INTERRUPTIBLE
);
1467 schedule_timeout(CREATE_COOLDOWN
);
1468 spin_lock_irq(&gcwq
->lock
);
1469 if (!need_to_create_worker(gcwq
))
1473 spin_unlock_irq(&gcwq
->lock
);
1474 del_timer_sync(&gcwq
->mayday_timer
);
1475 spin_lock_irq(&gcwq
->lock
);
1476 if (need_to_create_worker(gcwq
))
1482 * maybe_destroy_worker - destroy workers which have been idle for a while
1483 * @gcwq: gcwq to destroy workers for
1485 * Destroy @gcwq workers which have been idle for longer than
1486 * IDLE_WORKER_TIMEOUT.
1489 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1490 * multiple times. Called only from manager.
1493 * false if no action was taken and gcwq->lock stayed locked, true
1496 static bool maybe_destroy_workers(struct global_cwq
*gcwq
)
1500 while (too_many_workers(gcwq
)) {
1501 struct worker
*worker
;
1502 unsigned long expires
;
1504 worker
= list_entry(gcwq
->idle_list
.prev
, struct worker
, entry
);
1505 expires
= worker
->last_active
+ IDLE_WORKER_TIMEOUT
;
1507 if (time_before(jiffies
, expires
)) {
1508 mod_timer(&gcwq
->idle_timer
, expires
);
1512 destroy_worker(worker
);
1520 * manage_workers - manage worker pool
1523 * Assume the manager role and manage gcwq worker pool @worker belongs
1524 * to. At any given time, there can be only zero or one manager per
1525 * gcwq. The exclusion is handled automatically by this function.
1527 * The caller can safely start processing works on false return. On
1528 * true return, it's guaranteed that need_to_create_worker() is false
1529 * and may_start_working() is true.
1532 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1533 * multiple times. Does GFP_KERNEL allocations.
1536 * false if no action was taken and gcwq->lock stayed locked, true if
1537 * some action was taken.
1539 static bool manage_workers(struct worker
*worker
)
1541 struct global_cwq
*gcwq
= worker
->gcwq
;
1544 if (gcwq
->flags
& GCWQ_MANAGING_WORKERS
)
1547 gcwq
->flags
&= ~GCWQ_MANAGE_WORKERS
;
1548 gcwq
->flags
|= GCWQ_MANAGING_WORKERS
;
1551 * Destroy and then create so that may_start_working() is true
1554 ret
|= maybe_destroy_workers(gcwq
);
1555 ret
|= maybe_create_worker(gcwq
);
1557 gcwq
->flags
&= ~GCWQ_MANAGING_WORKERS
;
1560 * The trustee might be waiting to take over the manager
1561 * position, tell it we're done.
1563 if (unlikely(gcwq
->trustee
))
1564 wake_up_all(&gcwq
->trustee_wait
);
1570 * move_linked_works - move linked works to a list
1571 * @work: start of series of works to be scheduled
1572 * @head: target list to append @work to
1573 * @nextp: out paramter for nested worklist walking
1575 * Schedule linked works starting from @work to @head. Work series to
1576 * be scheduled starts at @work and includes any consecutive work with
1577 * WORK_STRUCT_LINKED set in its predecessor.
1579 * If @nextp is not NULL, it's updated to point to the next work of
1580 * the last scheduled work. This allows move_linked_works() to be
1581 * nested inside outer list_for_each_entry_safe().
1584 * spin_lock_irq(gcwq->lock).
1586 static void move_linked_works(struct work_struct
*work
, struct list_head
*head
,
1587 struct work_struct
**nextp
)
1589 struct work_struct
*n
;
1592 * Linked worklist will always end before the end of the list,
1593 * use NULL for list head.
1595 list_for_each_entry_safe_from(work
, n
, NULL
, entry
) {
1596 list_move_tail(&work
->entry
, head
);
1597 if (!(*work_data_bits(work
) & WORK_STRUCT_LINKED
))
1602 * If we're already inside safe list traversal and have moved
1603 * multiple works to the scheduled queue, the next position
1604 * needs to be updated.
1610 static void cwq_activate_first_delayed(struct cpu_workqueue_struct
*cwq
)
1612 struct work_struct
*work
= list_first_entry(&cwq
->delayed_works
,
1613 struct work_struct
, entry
);
1614 struct list_head
*pos
= gcwq_determine_ins_pos(cwq
->gcwq
, cwq
);
1616 move_linked_works(work
, pos
, NULL
);
1621 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
1622 * @cwq: cwq of interest
1623 * @color: color of work which left the queue
1625 * A work either has completed or is removed from pending queue,
1626 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1629 * spin_lock_irq(gcwq->lock).
1631 static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct
*cwq
, int color
)
1633 /* ignore uncolored works */
1634 if (color
== WORK_NO_COLOR
)
1637 cwq
->nr_in_flight
[color
]--;
1640 if (!list_empty(&cwq
->delayed_works
)) {
1641 /* one down, submit a delayed one */
1642 if (cwq
->nr_active
< cwq
->max_active
)
1643 cwq_activate_first_delayed(cwq
);
1646 /* is flush in progress and are we at the flushing tip? */
1647 if (likely(cwq
->flush_color
!= color
))
1650 /* are there still in-flight works? */
1651 if (cwq
->nr_in_flight
[color
])
1654 /* this cwq is done, clear flush_color */
1655 cwq
->flush_color
= -1;
1658 * If this was the last cwq, wake up the first flusher. It
1659 * will handle the rest.
1661 if (atomic_dec_and_test(&cwq
->wq
->nr_cwqs_to_flush
))
1662 complete(&cwq
->wq
->first_flusher
->done
);
1666 * process_one_work - process single work
1668 * @work: work to process
1670 * Process @work. This function contains all the logics necessary to
1671 * process a single work including synchronization against and
1672 * interaction with other workers on the same cpu, queueing and
1673 * flushing. As long as context requirement is met, any worker can
1674 * call this function to process a work.
1677 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
1679 static void process_one_work(struct worker
*worker
, struct work_struct
*work
)
1681 struct cpu_workqueue_struct
*cwq
= get_work_cwq(work
);
1682 struct global_cwq
*gcwq
= cwq
->gcwq
;
1683 struct hlist_head
*bwh
= busy_worker_head(gcwq
, work
);
1684 bool cpu_intensive
= cwq
->wq
->flags
& WQ_CPU_INTENSIVE
;
1685 work_func_t f
= work
->func
;
1687 struct worker
*collision
;
1688 #ifdef CONFIG_LOCKDEP
1690 * It is permissible to free the struct work_struct from
1691 * inside the function that is called from it, this we need to
1692 * take into account for lockdep too. To avoid bogus "held
1693 * lock freed" warnings as well as problems when looking into
1694 * work->lockdep_map, make a copy and use that here.
1696 struct lockdep_map lockdep_map
= work
->lockdep_map
;
1699 * A single work shouldn't be executed concurrently by
1700 * multiple workers on a single cpu. Check whether anyone is
1701 * already processing the work. If so, defer the work to the
1702 * currently executing one.
1704 collision
= __find_worker_executing_work(gcwq
, bwh
, work
);
1705 if (unlikely(collision
)) {
1706 move_linked_works(work
, &collision
->scheduled
, NULL
);
1710 /* claim and process */
1711 debug_work_deactivate(work
);
1712 hlist_add_head(&worker
->hentry
, bwh
);
1713 worker
->current_work
= work
;
1714 worker
->current_cwq
= cwq
;
1715 work_color
= get_work_color(work
);
1717 /* record the current cpu number in the work data and dequeue */
1718 set_work_cpu(work
, gcwq
->cpu
);
1719 list_del_init(&work
->entry
);
1722 * If HIGHPRI_PENDING, check the next work, and, if HIGHPRI,
1723 * wake up another worker; otherwise, clear HIGHPRI_PENDING.
1725 if (unlikely(gcwq
->flags
& GCWQ_HIGHPRI_PENDING
)) {
1726 struct work_struct
*nwork
= list_first_entry(&gcwq
->worklist
,
1727 struct work_struct
, entry
);
1729 if (!list_empty(&gcwq
->worklist
) &&
1730 get_work_cwq(nwork
)->wq
->flags
& WQ_HIGHPRI
)
1731 wake_up_worker(gcwq
);
1733 gcwq
->flags
&= ~GCWQ_HIGHPRI_PENDING
;
1737 * CPU intensive works don't participate in concurrency
1738 * management. They're the scheduler's responsibility.
1740 if (unlikely(cpu_intensive
))
1741 worker_set_flags(worker
, WORKER_CPU_INTENSIVE
, true);
1743 spin_unlock_irq(&gcwq
->lock
);
1745 work_clear_pending(work
);
1746 lock_map_acquire(&cwq
->wq
->lockdep_map
);
1747 lock_map_acquire(&lockdep_map
);
1749 lock_map_release(&lockdep_map
);
1750 lock_map_release(&cwq
->wq
->lockdep_map
);
1752 if (unlikely(in_atomic() || lockdep_depth(current
) > 0)) {
1753 printk(KERN_ERR
"BUG: workqueue leaked lock or atomic: "
1755 current
->comm
, preempt_count(), task_pid_nr(current
));
1756 printk(KERN_ERR
" last function: ");
1757 print_symbol("%s\n", (unsigned long)f
);
1758 debug_show_held_locks(current
);
1762 spin_lock_irq(&gcwq
->lock
);
1764 /* clear cpu intensive status */
1765 if (unlikely(cpu_intensive
))
1766 worker_clr_flags(worker
, WORKER_CPU_INTENSIVE
);
1768 /* we're done with it, release */
1769 hlist_del_init(&worker
->hentry
);
1770 worker
->current_work
= NULL
;
1771 worker
->current_cwq
= NULL
;
1772 cwq_dec_nr_in_flight(cwq
, work_color
);
1776 * process_scheduled_works - process scheduled works
1779 * Process all scheduled works. Please note that the scheduled list
1780 * may change while processing a work, so this function repeatedly
1781 * fetches a work from the top and executes it.
1784 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1787 static void process_scheduled_works(struct worker
*worker
)
1789 while (!list_empty(&worker
->scheduled
)) {
1790 struct work_struct
*work
= list_first_entry(&worker
->scheduled
,
1791 struct work_struct
, entry
);
1792 process_one_work(worker
, work
);
1797 * worker_thread - the worker thread function
1800 * The gcwq worker thread function. There's a single dynamic pool of
1801 * these per each cpu. These workers process all works regardless of
1802 * their specific target workqueue. The only exception is works which
1803 * belong to workqueues with a rescuer which will be explained in
1806 static int worker_thread(void *__worker
)
1808 struct worker
*worker
= __worker
;
1809 struct global_cwq
*gcwq
= worker
->gcwq
;
1811 /* tell the scheduler that this is a workqueue worker */
1812 worker
->task
->flags
|= PF_WQ_WORKER
;
1814 spin_lock_irq(&gcwq
->lock
);
1816 /* DIE can be set only while we're idle, checking here is enough */
1817 if (worker
->flags
& WORKER_DIE
) {
1818 spin_unlock_irq(&gcwq
->lock
);
1819 worker
->task
->flags
&= ~PF_WQ_WORKER
;
1823 worker_leave_idle(worker
);
1825 /* no more worker necessary? */
1826 if (!need_more_worker(gcwq
))
1829 /* do we need to manage? */
1830 if (unlikely(!may_start_working(gcwq
)) && manage_workers(worker
))
1834 * ->scheduled list can only be filled while a worker is
1835 * preparing to process a work or actually processing it.
1836 * Make sure nobody diddled with it while I was sleeping.
1838 BUG_ON(!list_empty(&worker
->scheduled
));
1841 * When control reaches this point, we're guaranteed to have
1842 * at least one idle worker or that someone else has already
1843 * assumed the manager role.
1845 worker_clr_flags(worker
, WORKER_PREP
);
1848 struct work_struct
*work
=
1849 list_first_entry(&gcwq
->worklist
,
1850 struct work_struct
, entry
);
1852 if (likely(!(*work_data_bits(work
) & WORK_STRUCT_LINKED
))) {
1853 /* optimization path, not strictly necessary */
1854 process_one_work(worker
, work
);
1855 if (unlikely(!list_empty(&worker
->scheduled
)))
1856 process_scheduled_works(worker
);
1858 move_linked_works(work
, &worker
->scheduled
, NULL
);
1859 process_scheduled_works(worker
);
1861 } while (keep_working(gcwq
));
1863 worker_set_flags(worker
, WORKER_PREP
, false);
1865 if (unlikely(need_to_manage_workers(gcwq
)) && manage_workers(worker
))
1869 * gcwq->lock is held and there's no work to process and no
1870 * need to manage, sleep. Workers are woken up only while
1871 * holding gcwq->lock or from local cpu, so setting the
1872 * current state before releasing gcwq->lock is enough to
1873 * prevent losing any event.
1875 worker_enter_idle(worker
);
1876 __set_current_state(TASK_INTERRUPTIBLE
);
1877 spin_unlock_irq(&gcwq
->lock
);
1883 * rescuer_thread - the rescuer thread function
1884 * @__wq: the associated workqueue
1886 * Workqueue rescuer thread function. There's one rescuer for each
1887 * workqueue which has WQ_RESCUER set.
1889 * Regular work processing on a gcwq may block trying to create a new
1890 * worker which uses GFP_KERNEL allocation which has slight chance of
1891 * developing into deadlock if some works currently on the same queue
1892 * need to be processed to satisfy the GFP_KERNEL allocation. This is
1893 * the problem rescuer solves.
1895 * When such condition is possible, the gcwq summons rescuers of all
1896 * workqueues which have works queued on the gcwq and let them process
1897 * those works so that forward progress can be guaranteed.
1899 * This should happen rarely.
1901 static int rescuer_thread(void *__wq
)
1903 struct workqueue_struct
*wq
= __wq
;
1904 struct worker
*rescuer
= wq
->rescuer
;
1905 struct list_head
*scheduled
= &rescuer
->scheduled
;
1906 bool is_unbound
= wq
->flags
& WQ_UNBOUND
;
1909 set_user_nice(current
, RESCUER_NICE_LEVEL
);
1911 set_current_state(TASK_INTERRUPTIBLE
);
1913 if (kthread_should_stop())
1917 * See whether any cpu is asking for help. Unbounded
1918 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
1920 for_each_cpu(cpu
, wq
->mayday_mask
) {
1921 unsigned int tcpu
= is_unbound
? WORK_CPU_UNBOUND
: cpu
;
1922 struct cpu_workqueue_struct
*cwq
= get_cwq(tcpu
, wq
);
1923 struct global_cwq
*gcwq
= cwq
->gcwq
;
1924 struct work_struct
*work
, *n
;
1926 __set_current_state(TASK_RUNNING
);
1927 cpumask_clear_cpu(cpu
, wq
->mayday_mask
);
1929 /* migrate to the target cpu if possible */
1930 rescuer
->gcwq
= gcwq
;
1931 worker_maybe_bind_and_lock(rescuer
);
1934 * Slurp in all works issued via this workqueue and
1937 BUG_ON(!list_empty(&rescuer
->scheduled
));
1938 list_for_each_entry_safe(work
, n
, &gcwq
->worklist
, entry
)
1939 if (get_work_cwq(work
) == cwq
)
1940 move_linked_works(work
, scheduled
, &n
);
1942 process_scheduled_works(rescuer
);
1943 spin_unlock_irq(&gcwq
->lock
);
1951 struct work_struct work
;
1952 struct completion done
;
1955 static void wq_barrier_func(struct work_struct
*work
)
1957 struct wq_barrier
*barr
= container_of(work
, struct wq_barrier
, work
);
1958 complete(&barr
->done
);
1962 * insert_wq_barrier - insert a barrier work
1963 * @cwq: cwq to insert barrier into
1964 * @barr: wq_barrier to insert
1965 * @target: target work to attach @barr to
1966 * @worker: worker currently executing @target, NULL if @target is not executing
1968 * @barr is linked to @target such that @barr is completed only after
1969 * @target finishes execution. Please note that the ordering
1970 * guarantee is observed only with respect to @target and on the local
1973 * Currently, a queued barrier can't be canceled. This is because
1974 * try_to_grab_pending() can't determine whether the work to be
1975 * grabbed is at the head of the queue and thus can't clear LINKED
1976 * flag of the previous work while there must be a valid next work
1977 * after a work with LINKED flag set.
1979 * Note that when @worker is non-NULL, @target may be modified
1980 * underneath us, so we can't reliably determine cwq from @target.
1983 * spin_lock_irq(gcwq->lock).
1985 static void insert_wq_barrier(struct cpu_workqueue_struct
*cwq
,
1986 struct wq_barrier
*barr
,
1987 struct work_struct
*target
, struct worker
*worker
)
1989 struct list_head
*head
;
1990 unsigned int linked
= 0;
1993 * debugobject calls are safe here even with gcwq->lock locked
1994 * as we know for sure that this will not trigger any of the
1995 * checks and call back into the fixup functions where we
1998 INIT_WORK_ON_STACK(&barr
->work
, wq_barrier_func
);
1999 __set_bit(WORK_STRUCT_PENDING_BIT
, work_data_bits(&barr
->work
));
2000 init_completion(&barr
->done
);
2003 * If @target is currently being executed, schedule the
2004 * barrier to the worker; otherwise, put it after @target.
2007 head
= worker
->scheduled
.next
;
2009 unsigned long *bits
= work_data_bits(target
);
2011 head
= target
->entry
.next
;
2012 /* there can already be other linked works, inherit and set */
2013 linked
= *bits
& WORK_STRUCT_LINKED
;
2014 __set_bit(WORK_STRUCT_LINKED_BIT
, bits
);
2017 debug_work_activate(&barr
->work
);
2018 insert_work(cwq
, &barr
->work
, head
,
2019 work_color_to_flags(WORK_NO_COLOR
) | linked
);
2023 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2024 * @wq: workqueue being flushed
2025 * @flush_color: new flush color, < 0 for no-op
2026 * @work_color: new work color, < 0 for no-op
2028 * Prepare cwqs for workqueue flushing.
2030 * If @flush_color is non-negative, flush_color on all cwqs should be
2031 * -1. If no cwq has in-flight commands at the specified color, all
2032 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2033 * has in flight commands, its cwq->flush_color is set to
2034 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2035 * wakeup logic is armed and %true is returned.
2037 * The caller should have initialized @wq->first_flusher prior to
2038 * calling this function with non-negative @flush_color. If
2039 * @flush_color is negative, no flush color update is done and %false
2042 * If @work_color is non-negative, all cwqs should have the same
2043 * work_color which is previous to @work_color and all will be
2044 * advanced to @work_color.
2047 * mutex_lock(wq->flush_mutex).
2050 * %true if @flush_color >= 0 and there's something to flush. %false
2053 static bool flush_workqueue_prep_cwqs(struct workqueue_struct
*wq
,
2054 int flush_color
, int work_color
)
2059 if (flush_color
>= 0) {
2060 BUG_ON(atomic_read(&wq
->nr_cwqs_to_flush
));
2061 atomic_set(&wq
->nr_cwqs_to_flush
, 1);
2064 for_each_cwq_cpu(cpu
, wq
) {
2065 struct cpu_workqueue_struct
*cwq
= get_cwq(cpu
, wq
);
2066 struct global_cwq
*gcwq
= cwq
->gcwq
;
2068 spin_lock_irq(&gcwq
->lock
);
2070 if (flush_color
>= 0) {
2071 BUG_ON(cwq
->flush_color
!= -1);
2073 if (cwq
->nr_in_flight
[flush_color
]) {
2074 cwq
->flush_color
= flush_color
;
2075 atomic_inc(&wq
->nr_cwqs_to_flush
);
2080 if (work_color
>= 0) {
2081 BUG_ON(work_color
!= work_next_color(cwq
->work_color
));
2082 cwq
->work_color
= work_color
;
2085 spin_unlock_irq(&gcwq
->lock
);
2088 if (flush_color
>= 0 && atomic_dec_and_test(&wq
->nr_cwqs_to_flush
))
2089 complete(&wq
->first_flusher
->done
);
2095 * flush_workqueue - ensure that any scheduled work has run to completion.
2096 * @wq: workqueue to flush
2098 * Forces execution of the workqueue and blocks until its completion.
2099 * This is typically used in driver shutdown handlers.
2101 * We sleep until all works which were queued on entry have been handled,
2102 * but we are not livelocked by new incoming ones.
2104 void flush_workqueue(struct workqueue_struct
*wq
)
2106 struct wq_flusher this_flusher
= {
2107 .list
= LIST_HEAD_INIT(this_flusher
.list
),
2109 .done
= COMPLETION_INITIALIZER_ONSTACK(this_flusher
.done
),
2113 lock_map_acquire(&wq
->lockdep_map
);
2114 lock_map_release(&wq
->lockdep_map
);
2116 mutex_lock(&wq
->flush_mutex
);
2119 * Start-to-wait phase
2121 next_color
= work_next_color(wq
->work_color
);
2123 if (next_color
!= wq
->flush_color
) {
2125 * Color space is not full. The current work_color
2126 * becomes our flush_color and work_color is advanced
2129 BUG_ON(!list_empty(&wq
->flusher_overflow
));
2130 this_flusher
.flush_color
= wq
->work_color
;
2131 wq
->work_color
= next_color
;
2133 if (!wq
->first_flusher
) {
2134 /* no flush in progress, become the first flusher */
2135 BUG_ON(wq
->flush_color
!= this_flusher
.flush_color
);
2137 wq
->first_flusher
= &this_flusher
;
2139 if (!flush_workqueue_prep_cwqs(wq
, wq
->flush_color
,
2141 /* nothing to flush, done */
2142 wq
->flush_color
= next_color
;
2143 wq
->first_flusher
= NULL
;
2148 BUG_ON(wq
->flush_color
== this_flusher
.flush_color
);
2149 list_add_tail(&this_flusher
.list
, &wq
->flusher_queue
);
2150 flush_workqueue_prep_cwqs(wq
, -1, wq
->work_color
);
2154 * Oops, color space is full, wait on overflow queue.
2155 * The next flush completion will assign us
2156 * flush_color and transfer to flusher_queue.
2158 list_add_tail(&this_flusher
.list
, &wq
->flusher_overflow
);
2161 mutex_unlock(&wq
->flush_mutex
);
2163 wait_for_completion(&this_flusher
.done
);
2166 * Wake-up-and-cascade phase
2168 * First flushers are responsible for cascading flushes and
2169 * handling overflow. Non-first flushers can simply return.
2171 if (wq
->first_flusher
!= &this_flusher
)
2174 mutex_lock(&wq
->flush_mutex
);
2176 /* we might have raced, check again with mutex held */
2177 if (wq
->first_flusher
!= &this_flusher
)
2180 wq
->first_flusher
= NULL
;
2182 BUG_ON(!list_empty(&this_flusher
.list
));
2183 BUG_ON(wq
->flush_color
!= this_flusher
.flush_color
);
2186 struct wq_flusher
*next
, *tmp
;
2188 /* complete all the flushers sharing the current flush color */
2189 list_for_each_entry_safe(next
, tmp
, &wq
->flusher_queue
, list
) {
2190 if (next
->flush_color
!= wq
->flush_color
)
2192 list_del_init(&next
->list
);
2193 complete(&next
->done
);
2196 BUG_ON(!list_empty(&wq
->flusher_overflow
) &&
2197 wq
->flush_color
!= work_next_color(wq
->work_color
));
2199 /* this flush_color is finished, advance by one */
2200 wq
->flush_color
= work_next_color(wq
->flush_color
);
2202 /* one color has been freed, handle overflow queue */
2203 if (!list_empty(&wq
->flusher_overflow
)) {
2205 * Assign the same color to all overflowed
2206 * flushers, advance work_color and append to
2207 * flusher_queue. This is the start-to-wait
2208 * phase for these overflowed flushers.
2210 list_for_each_entry(tmp
, &wq
->flusher_overflow
, list
)
2211 tmp
->flush_color
= wq
->work_color
;
2213 wq
->work_color
= work_next_color(wq
->work_color
);
2215 list_splice_tail_init(&wq
->flusher_overflow
,
2216 &wq
->flusher_queue
);
2217 flush_workqueue_prep_cwqs(wq
, -1, wq
->work_color
);
2220 if (list_empty(&wq
->flusher_queue
)) {
2221 BUG_ON(wq
->flush_color
!= wq
->work_color
);
2226 * Need to flush more colors. Make the next flusher
2227 * the new first flusher and arm cwqs.
2229 BUG_ON(wq
->flush_color
== wq
->work_color
);
2230 BUG_ON(wq
->flush_color
!= next
->flush_color
);
2232 list_del_init(&next
->list
);
2233 wq
->first_flusher
= next
;
2235 if (flush_workqueue_prep_cwqs(wq
, wq
->flush_color
, -1))
2239 * Meh... this color is already done, clear first
2240 * flusher and repeat cascading.
2242 wq
->first_flusher
= NULL
;
2246 mutex_unlock(&wq
->flush_mutex
);
2248 EXPORT_SYMBOL_GPL(flush_workqueue
);
2251 * flush_work - block until a work_struct's callback has terminated
2252 * @work: the work which is to be flushed
2254 * Returns false if @work has already terminated.
2256 * It is expected that, prior to calling flush_work(), the caller has
2257 * arranged for the work to not be requeued, otherwise it doesn't make
2258 * sense to use this function.
2260 int flush_work(struct work_struct
*work
)
2262 struct worker
*worker
= NULL
;
2263 struct global_cwq
*gcwq
;
2264 struct cpu_workqueue_struct
*cwq
;
2265 struct wq_barrier barr
;
2268 gcwq
= get_work_gcwq(work
);
2272 spin_lock_irq(&gcwq
->lock
);
2273 if (!list_empty(&work
->entry
)) {
2275 * See the comment near try_to_grab_pending()->smp_rmb().
2276 * If it was re-queued to a different gcwq under us, we
2277 * are not going to wait.
2280 cwq
= get_work_cwq(work
);
2281 if (unlikely(!cwq
|| gcwq
!= cwq
->gcwq
))
2284 worker
= find_worker_executing_work(gcwq
, work
);
2287 cwq
= worker
->current_cwq
;
2290 insert_wq_barrier(cwq
, &barr
, work
, worker
);
2291 spin_unlock_irq(&gcwq
->lock
);
2293 lock_map_acquire(&cwq
->wq
->lockdep_map
);
2294 lock_map_release(&cwq
->wq
->lockdep_map
);
2296 wait_for_completion(&barr
.done
);
2297 destroy_work_on_stack(&barr
.work
);
2300 spin_unlock_irq(&gcwq
->lock
);
2303 EXPORT_SYMBOL_GPL(flush_work
);
2306 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
2307 * so this work can't be re-armed in any way.
2309 static int try_to_grab_pending(struct work_struct
*work
)
2311 struct global_cwq
*gcwq
;
2314 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT
, work_data_bits(work
)))
2318 * The queueing is in progress, or it is already queued. Try to
2319 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
2321 gcwq
= get_work_gcwq(work
);
2325 spin_lock_irq(&gcwq
->lock
);
2326 if (!list_empty(&work
->entry
)) {
2328 * This work is queued, but perhaps we locked the wrong gcwq.
2329 * In that case we must see the new value after rmb(), see
2330 * insert_work()->wmb().
2333 if (gcwq
== get_work_gcwq(work
)) {
2334 debug_work_deactivate(work
);
2335 list_del_init(&work
->entry
);
2336 cwq_dec_nr_in_flight(get_work_cwq(work
),
2337 get_work_color(work
));
2341 spin_unlock_irq(&gcwq
->lock
);
2346 static void wait_on_cpu_work(struct global_cwq
*gcwq
, struct work_struct
*work
)
2348 struct wq_barrier barr
;
2349 struct worker
*worker
;
2351 spin_lock_irq(&gcwq
->lock
);
2353 worker
= find_worker_executing_work(gcwq
, work
);
2354 if (unlikely(worker
))
2355 insert_wq_barrier(worker
->current_cwq
, &barr
, work
, worker
);
2357 spin_unlock_irq(&gcwq
->lock
);
2359 if (unlikely(worker
)) {
2360 wait_for_completion(&barr
.done
);
2361 destroy_work_on_stack(&barr
.work
);
2365 static void wait_on_work(struct work_struct
*work
)
2371 lock_map_acquire(&work
->lockdep_map
);
2372 lock_map_release(&work
->lockdep_map
);
2374 for_each_gcwq_cpu(cpu
)
2375 wait_on_cpu_work(get_gcwq(cpu
), work
);
2378 static int __cancel_work_timer(struct work_struct
*work
,
2379 struct timer_list
* timer
)
2384 ret
= (timer
&& likely(del_timer(timer
)));
2386 ret
= try_to_grab_pending(work
);
2388 } while (unlikely(ret
< 0));
2390 clear_work_data(work
);
2395 * cancel_work_sync - block until a work_struct's callback has terminated
2396 * @work: the work which is to be flushed
2398 * Returns true if @work was pending.
2400 * cancel_work_sync() will cancel the work if it is queued. If the work's
2401 * callback appears to be running, cancel_work_sync() will block until it
2404 * It is possible to use this function if the work re-queues itself. It can
2405 * cancel the work even if it migrates to another workqueue, however in that
2406 * case it only guarantees that work->func() has completed on the last queued
2409 * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
2410 * pending, otherwise it goes into a busy-wait loop until the timer expires.
2412 * The caller must ensure that workqueue_struct on which this work was last
2413 * queued can't be destroyed before this function returns.
2415 int cancel_work_sync(struct work_struct
*work
)
2417 return __cancel_work_timer(work
, NULL
);
2419 EXPORT_SYMBOL_GPL(cancel_work_sync
);
2422 * cancel_delayed_work_sync - reliably kill off a delayed work.
2423 * @dwork: the delayed work struct
2425 * Returns true if @dwork was pending.
2427 * It is possible to use this function if @dwork rearms itself via queue_work()
2428 * or queue_delayed_work(). See also the comment for cancel_work_sync().
2430 int cancel_delayed_work_sync(struct delayed_work
*dwork
)
2432 return __cancel_work_timer(&dwork
->work
, &dwork
->timer
);
2434 EXPORT_SYMBOL(cancel_delayed_work_sync
);
2437 * schedule_work - put work task in global workqueue
2438 * @work: job to be done
2440 * Returns zero if @work was already on the kernel-global workqueue and
2441 * non-zero otherwise.
2443 * This puts a job in the kernel-global workqueue if it was not already
2444 * queued and leaves it in the same position on the kernel-global
2445 * workqueue otherwise.
2447 int schedule_work(struct work_struct
*work
)
2449 return queue_work(system_wq
, work
);
2451 EXPORT_SYMBOL(schedule_work
);
2454 * schedule_work_on - put work task on a specific cpu
2455 * @cpu: cpu to put the work task on
2456 * @work: job to be done
2458 * This puts a job on a specific cpu
2460 int schedule_work_on(int cpu
, struct work_struct
*work
)
2462 return queue_work_on(cpu
, system_wq
, work
);
2464 EXPORT_SYMBOL(schedule_work_on
);
2467 * schedule_delayed_work - put work task in global workqueue after delay
2468 * @dwork: job to be done
2469 * @delay: number of jiffies to wait or 0 for immediate execution
2471 * After waiting for a given time this puts a job in the kernel-global
2474 int schedule_delayed_work(struct delayed_work
*dwork
,
2475 unsigned long delay
)
2477 return queue_delayed_work(system_wq
, dwork
, delay
);
2479 EXPORT_SYMBOL(schedule_delayed_work
);
2482 * flush_delayed_work - block until a dwork_struct's callback has terminated
2483 * @dwork: the delayed work which is to be flushed
2485 * Any timeout is cancelled, and any pending work is run immediately.
2487 void flush_delayed_work(struct delayed_work
*dwork
)
2489 if (del_timer_sync(&dwork
->timer
)) {
2490 __queue_work(get_cpu(), get_work_cwq(&dwork
->work
)->wq
,
2494 flush_work(&dwork
->work
);
2496 EXPORT_SYMBOL(flush_delayed_work
);
2499 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2501 * @dwork: job to be done
2502 * @delay: number of jiffies to wait
2504 * After waiting for a given time this puts a job in the kernel-global
2505 * workqueue on the specified CPU.
2507 int schedule_delayed_work_on(int cpu
,
2508 struct delayed_work
*dwork
, unsigned long delay
)
2510 return queue_delayed_work_on(cpu
, system_wq
, dwork
, delay
);
2512 EXPORT_SYMBOL(schedule_delayed_work_on
);
2515 * schedule_on_each_cpu - call a function on each online CPU from keventd
2516 * @func: the function to call
2518 * Returns zero on success.
2519 * Returns -ve errno on failure.
2521 * schedule_on_each_cpu() is very slow.
2523 int schedule_on_each_cpu(work_func_t func
)
2526 struct work_struct
*works
;
2528 works
= alloc_percpu(struct work_struct
);
2534 for_each_online_cpu(cpu
) {
2535 struct work_struct
*work
= per_cpu_ptr(works
, cpu
);
2537 INIT_WORK(work
, func
);
2538 schedule_work_on(cpu
, work
);
2541 for_each_online_cpu(cpu
)
2542 flush_work(per_cpu_ptr(works
, cpu
));
2550 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2552 * Forces execution of the kernel-global workqueue and blocks until its
2555 * Think twice before calling this function! It's very easy to get into
2556 * trouble if you don't take great care. Either of the following situations
2557 * will lead to deadlock:
2559 * One of the work items currently on the workqueue needs to acquire
2560 * a lock held by your code or its caller.
2562 * Your code is running in the context of a work routine.
2564 * They will be detected by lockdep when they occur, but the first might not
2565 * occur very often. It depends on what work items are on the workqueue and
2566 * what locks they need, which you have no control over.
2568 * In most situations flushing the entire workqueue is overkill; you merely
2569 * need to know that a particular work item isn't queued and isn't running.
2570 * In such cases you should use cancel_delayed_work_sync() or
2571 * cancel_work_sync() instead.
2573 void flush_scheduled_work(void)
2575 flush_workqueue(system_wq
);
2577 EXPORT_SYMBOL(flush_scheduled_work
);
2580 * execute_in_process_context - reliably execute the routine with user context
2581 * @fn: the function to execute
2582 * @ew: guaranteed storage for the execute work structure (must
2583 * be available when the work executes)
2585 * Executes the function immediately if process context is available,
2586 * otherwise schedules the function for delayed execution.
2588 * Returns: 0 - function was executed
2589 * 1 - function was scheduled for execution
2591 int execute_in_process_context(work_func_t fn
, struct execute_work
*ew
)
2593 if (!in_interrupt()) {
2598 INIT_WORK(&ew
->work
, fn
);
2599 schedule_work(&ew
->work
);
2603 EXPORT_SYMBOL_GPL(execute_in_process_context
);
2605 int keventd_up(void)
2607 return system_wq
!= NULL
;
2610 static int alloc_cwqs(struct workqueue_struct
*wq
)
2613 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
2614 * Make sure that the alignment isn't lower than that of
2615 * unsigned long long.
2617 const size_t size
= sizeof(struct cpu_workqueue_struct
);
2618 const size_t align
= max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS
,
2619 __alignof__(unsigned long long));
2621 if (CONFIG_SMP
&& !(wq
->flags
& WQ_UNBOUND
)) {
2622 /* on SMP, percpu allocator can align itself */
2623 wq
->cpu_wq
.pcpu
= __alloc_percpu(size
, align
);
2628 * Allocate enough room to align cwq and put an extra
2629 * pointer at the end pointing back to the originally
2630 * allocated pointer which will be used for free.
2632 ptr
= kzalloc(size
+ align
+ sizeof(void *), GFP_KERNEL
);
2634 wq
->cpu_wq
.single
= PTR_ALIGN(ptr
, align
);
2635 *(void **)(wq
->cpu_wq
.single
+ 1) = ptr
;
2639 /* just in case, make sure it's actually aligned */
2640 BUG_ON(!IS_ALIGNED(wq
->cpu_wq
.v
, align
));
2641 return wq
->cpu_wq
.v
? 0 : -ENOMEM
;
2644 static void free_cwqs(struct workqueue_struct
*wq
)
2646 if (CONFIG_SMP
&& !(wq
->flags
& WQ_UNBOUND
))
2647 free_percpu(wq
->cpu_wq
.pcpu
);
2648 else if (wq
->cpu_wq
.single
) {
2649 /* the pointer to free is stored right after the cwq */
2650 kfree(*(void **)(wq
->cpu_wq
.single
+ 1));
2654 static int wq_clamp_max_active(int max_active
, unsigned int flags
,
2657 int lim
= flags
& WQ_UNBOUND
? WQ_UNBOUND_MAX_ACTIVE
: WQ_MAX_ACTIVE
;
2659 if (max_active
< 1 || max_active
> lim
)
2660 printk(KERN_WARNING
"workqueue: max_active %d requested for %s "
2661 "is out of range, clamping between %d and %d\n",
2662 max_active
, name
, 1, lim
);
2664 return clamp_val(max_active
, 1, lim
);
2667 struct workqueue_struct
*__alloc_workqueue_key(const char *name
,
2670 struct lock_class_key
*key
,
2671 const char *lock_name
)
2673 struct workqueue_struct
*wq
;
2677 * Unbound workqueues aren't concurrency managed and should be
2678 * dispatched to workers immediately.
2680 if (flags
& WQ_UNBOUND
)
2681 flags
|= WQ_HIGHPRI
;
2683 max_active
= max_active
?: WQ_DFL_ACTIVE
;
2684 max_active
= wq_clamp_max_active(max_active
, flags
, name
);
2686 wq
= kzalloc(sizeof(*wq
), GFP_KERNEL
);
2691 wq
->saved_max_active
= max_active
;
2692 mutex_init(&wq
->flush_mutex
);
2693 atomic_set(&wq
->nr_cwqs_to_flush
, 0);
2694 INIT_LIST_HEAD(&wq
->flusher_queue
);
2695 INIT_LIST_HEAD(&wq
->flusher_overflow
);
2698 lockdep_init_map(&wq
->lockdep_map
, lock_name
, key
, 0);
2699 INIT_LIST_HEAD(&wq
->list
);
2701 if (alloc_cwqs(wq
) < 0)
2704 for_each_cwq_cpu(cpu
, wq
) {
2705 struct cpu_workqueue_struct
*cwq
= get_cwq(cpu
, wq
);
2706 struct global_cwq
*gcwq
= get_gcwq(cpu
);
2708 BUG_ON((unsigned long)cwq
& WORK_STRUCT_FLAG_MASK
);
2711 cwq
->flush_color
= -1;
2712 cwq
->max_active
= max_active
;
2713 INIT_LIST_HEAD(&cwq
->delayed_works
);
2716 if (flags
& WQ_RESCUER
) {
2717 struct worker
*rescuer
;
2719 if (!alloc_cpumask_var(&wq
->mayday_mask
, GFP_KERNEL
))
2722 wq
->rescuer
= rescuer
= alloc_worker();
2726 rescuer
->task
= kthread_create(rescuer_thread
, wq
, "%s", name
);
2727 if (IS_ERR(rescuer
->task
))
2730 wq
->rescuer
= rescuer
;
2731 rescuer
->task
->flags
|= PF_THREAD_BOUND
;
2732 wake_up_process(rescuer
->task
);
2736 * workqueue_lock protects global freeze state and workqueues
2737 * list. Grab it, set max_active accordingly and add the new
2738 * workqueue to workqueues list.
2740 spin_lock(&workqueue_lock
);
2742 if (workqueue_freezing
&& wq
->flags
& WQ_FREEZEABLE
)
2743 for_each_cwq_cpu(cpu
, wq
)
2744 get_cwq(cpu
, wq
)->max_active
= 0;
2746 list_add(&wq
->list
, &workqueues
);
2748 spin_unlock(&workqueue_lock
);
2754 free_cpumask_var(wq
->mayday_mask
);
2760 EXPORT_SYMBOL_GPL(__alloc_workqueue_key
);
2763 * destroy_workqueue - safely terminate a workqueue
2764 * @wq: target workqueue
2766 * Safely destroy a workqueue. All work currently pending will be done first.
2768 void destroy_workqueue(struct workqueue_struct
*wq
)
2772 flush_workqueue(wq
);
2775 * wq list is used to freeze wq, remove from list after
2776 * flushing is complete in case freeze races us.
2778 spin_lock(&workqueue_lock
);
2779 list_del(&wq
->list
);
2780 spin_unlock(&workqueue_lock
);
2783 for_each_cwq_cpu(cpu
, wq
) {
2784 struct cpu_workqueue_struct
*cwq
= get_cwq(cpu
, wq
);
2787 for (i
= 0; i
< WORK_NR_COLORS
; i
++)
2788 BUG_ON(cwq
->nr_in_flight
[i
]);
2789 BUG_ON(cwq
->nr_active
);
2790 BUG_ON(!list_empty(&cwq
->delayed_works
));
2793 if (wq
->flags
& WQ_RESCUER
) {
2794 kthread_stop(wq
->rescuer
->task
);
2795 free_cpumask_var(wq
->mayday_mask
);
2801 EXPORT_SYMBOL_GPL(destroy_workqueue
);
2804 * workqueue_set_max_active - adjust max_active of a workqueue
2805 * @wq: target workqueue
2806 * @max_active: new max_active value.
2808 * Set max_active of @wq to @max_active.
2811 * Don't call from IRQ context.
2813 void workqueue_set_max_active(struct workqueue_struct
*wq
, int max_active
)
2817 max_active
= wq_clamp_max_active(max_active
, wq
->flags
, wq
->name
);
2819 spin_lock(&workqueue_lock
);
2821 wq
->saved_max_active
= max_active
;
2823 for_each_cwq_cpu(cpu
, wq
) {
2824 struct global_cwq
*gcwq
= get_gcwq(cpu
);
2826 spin_lock_irq(&gcwq
->lock
);
2828 if (!(wq
->flags
& WQ_FREEZEABLE
) ||
2829 !(gcwq
->flags
& GCWQ_FREEZING
))
2830 get_cwq(gcwq
->cpu
, wq
)->max_active
= max_active
;
2832 spin_unlock_irq(&gcwq
->lock
);
2835 spin_unlock(&workqueue_lock
);
2837 EXPORT_SYMBOL_GPL(workqueue_set_max_active
);
2840 * workqueue_congested - test whether a workqueue is congested
2841 * @cpu: CPU in question
2842 * @wq: target workqueue
2844 * Test whether @wq's cpu workqueue for @cpu is congested. There is
2845 * no synchronization around this function and the test result is
2846 * unreliable and only useful as advisory hints or for debugging.
2849 * %true if congested, %false otherwise.
2851 bool workqueue_congested(unsigned int cpu
, struct workqueue_struct
*wq
)
2853 struct cpu_workqueue_struct
*cwq
= get_cwq(cpu
, wq
);
2855 return !list_empty(&cwq
->delayed_works
);
2857 EXPORT_SYMBOL_GPL(workqueue_congested
);
2860 * work_cpu - return the last known associated cpu for @work
2861 * @work: the work of interest
2864 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
2866 unsigned int work_cpu(struct work_struct
*work
)
2868 struct global_cwq
*gcwq
= get_work_gcwq(work
);
2870 return gcwq
? gcwq
->cpu
: WORK_CPU_NONE
;
2872 EXPORT_SYMBOL_GPL(work_cpu
);
2875 * work_busy - test whether a work is currently pending or running
2876 * @work: the work to be tested
2878 * Test whether @work is currently pending or running. There is no
2879 * synchronization around this function and the test result is
2880 * unreliable and only useful as advisory hints or for debugging.
2881 * Especially for reentrant wqs, the pending state might hide the
2885 * OR'd bitmask of WORK_BUSY_* bits.
2887 unsigned int work_busy(struct work_struct
*work
)
2889 struct global_cwq
*gcwq
= get_work_gcwq(work
);
2890 unsigned long flags
;
2891 unsigned int ret
= 0;
2896 spin_lock_irqsave(&gcwq
->lock
, flags
);
2898 if (work_pending(work
))
2899 ret
|= WORK_BUSY_PENDING
;
2900 if (find_worker_executing_work(gcwq
, work
))
2901 ret
|= WORK_BUSY_RUNNING
;
2903 spin_unlock_irqrestore(&gcwq
->lock
, flags
);
2907 EXPORT_SYMBOL_GPL(work_busy
);
2912 * There are two challenges in supporting CPU hotplug. Firstly, there
2913 * are a lot of assumptions on strong associations among work, cwq and
2914 * gcwq which make migrating pending and scheduled works very
2915 * difficult to implement without impacting hot paths. Secondly,
2916 * gcwqs serve mix of short, long and very long running works making
2917 * blocked draining impractical.
2919 * This is solved by allowing a gcwq to be detached from CPU, running
2920 * it with unbound (rogue) workers and allowing it to be reattached
2921 * later if the cpu comes back online. A separate thread is created
2922 * to govern a gcwq in such state and is called the trustee of the
2925 * Trustee states and their descriptions.
2927 * START Command state used on startup. On CPU_DOWN_PREPARE, a
2928 * new trustee is started with this state.
2930 * IN_CHARGE Once started, trustee will enter this state after
2931 * assuming the manager role and making all existing
2932 * workers rogue. DOWN_PREPARE waits for trustee to
2933 * enter this state. After reaching IN_CHARGE, trustee
2934 * tries to execute the pending worklist until it's empty
2935 * and the state is set to BUTCHER, or the state is set
2938 * BUTCHER Command state which is set by the cpu callback after
2939 * the cpu has went down. Once this state is set trustee
2940 * knows that there will be no new works on the worklist
2941 * and once the worklist is empty it can proceed to
2942 * killing idle workers.
2944 * RELEASE Command state which is set by the cpu callback if the
2945 * cpu down has been canceled or it has come online
2946 * again. After recognizing this state, trustee stops
2947 * trying to drain or butcher and clears ROGUE, rebinds
2948 * all remaining workers back to the cpu and releases
2951 * DONE Trustee will enter this state after BUTCHER or RELEASE
2954 * trustee CPU draining
2955 * took over down complete
2956 * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
2958 * | CPU is back online v return workers |
2959 * ----------------> RELEASE --------------
2963 * trustee_wait_event_timeout - timed event wait for trustee
2964 * @cond: condition to wait for
2965 * @timeout: timeout in jiffies
2967 * wait_event_timeout() for trustee to use. Handles locking and
2968 * checks for RELEASE request.
2971 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
2972 * multiple times. To be used by trustee.
2975 * Positive indicating left time if @cond is satisfied, 0 if timed
2976 * out, -1 if canceled.
2978 #define trustee_wait_event_timeout(cond, timeout) ({ \
2979 long __ret = (timeout); \
2980 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
2982 spin_unlock_irq(&gcwq->lock); \
2983 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
2984 (gcwq->trustee_state == TRUSTEE_RELEASE), \
2986 spin_lock_irq(&gcwq->lock); \
2988 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
2992 * trustee_wait_event - event wait for trustee
2993 * @cond: condition to wait for
2995 * wait_event() for trustee to use. Automatically handles locking and
2996 * checks for CANCEL request.
2999 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3000 * multiple times. To be used by trustee.
3003 * 0 if @cond is satisfied, -1 if canceled.
3005 #define trustee_wait_event(cond) ({ \
3007 __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
3008 __ret1 < 0 ? -1 : 0; \
3011 static int __cpuinit
trustee_thread(void *__gcwq
)
3013 struct global_cwq
*gcwq
= __gcwq
;
3014 struct worker
*worker
;
3015 struct work_struct
*work
;
3016 struct hlist_node
*pos
;
3020 BUG_ON(gcwq
->cpu
!= smp_processor_id());
3022 spin_lock_irq(&gcwq
->lock
);
3024 * Claim the manager position and make all workers rogue.
3025 * Trustee must be bound to the target cpu and can't be
3028 BUG_ON(gcwq
->cpu
!= smp_processor_id());
3029 rc
= trustee_wait_event(!(gcwq
->flags
& GCWQ_MANAGING_WORKERS
));
3032 gcwq
->flags
|= GCWQ_MANAGING_WORKERS
;
3034 list_for_each_entry(worker
, &gcwq
->idle_list
, entry
)
3035 worker
->flags
|= WORKER_ROGUE
;
3037 for_each_busy_worker(worker
, i
, pos
, gcwq
)
3038 worker
->flags
|= WORKER_ROGUE
;
3041 * Call schedule() so that we cross rq->lock and thus can
3042 * guarantee sched callbacks see the rogue flag. This is
3043 * necessary as scheduler callbacks may be invoked from other
3046 spin_unlock_irq(&gcwq
->lock
);
3048 spin_lock_irq(&gcwq
->lock
);
3051 * Sched callbacks are disabled now. Zap nr_running. After
3052 * this, nr_running stays zero and need_more_worker() and
3053 * keep_working() are always true as long as the worklist is
3056 atomic_set(get_gcwq_nr_running(gcwq
->cpu
), 0);
3058 spin_unlock_irq(&gcwq
->lock
);
3059 del_timer_sync(&gcwq
->idle_timer
);
3060 spin_lock_irq(&gcwq
->lock
);
3063 * We're now in charge. Notify and proceed to drain. We need
3064 * to keep the gcwq running during the whole CPU down
3065 * procedure as other cpu hotunplug callbacks may need to
3066 * flush currently running tasks.
3068 gcwq
->trustee_state
= TRUSTEE_IN_CHARGE
;
3069 wake_up_all(&gcwq
->trustee_wait
);
3072 * The original cpu is in the process of dying and may go away
3073 * anytime now. When that happens, we and all workers would
3074 * be migrated to other cpus. Try draining any left work. We
3075 * want to get it over with ASAP - spam rescuers, wake up as
3076 * many idlers as necessary and create new ones till the
3077 * worklist is empty. Note that if the gcwq is frozen, there
3078 * may be frozen works in freezeable cwqs. Don't declare
3079 * completion while frozen.
3081 while (gcwq
->nr_workers
!= gcwq
->nr_idle
||
3082 gcwq
->flags
& GCWQ_FREEZING
||
3083 gcwq
->trustee_state
== TRUSTEE_IN_CHARGE
) {
3086 list_for_each_entry(work
, &gcwq
->worklist
, entry
) {
3091 list_for_each_entry(worker
, &gcwq
->idle_list
, entry
) {
3094 wake_up_process(worker
->task
);
3097 if (need_to_create_worker(gcwq
)) {
3098 spin_unlock_irq(&gcwq
->lock
);
3099 worker
= create_worker(gcwq
, false);
3100 spin_lock_irq(&gcwq
->lock
);
3102 worker
->flags
|= WORKER_ROGUE
;
3103 start_worker(worker
);
3107 /* give a breather */
3108 if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN
) < 0)
3113 * Either all works have been scheduled and cpu is down, or
3114 * cpu down has already been canceled. Wait for and butcher
3115 * all workers till we're canceled.
3118 rc
= trustee_wait_event(!list_empty(&gcwq
->idle_list
));
3119 while (!list_empty(&gcwq
->idle_list
))
3120 destroy_worker(list_first_entry(&gcwq
->idle_list
,
3121 struct worker
, entry
));
3122 } while (gcwq
->nr_workers
&& rc
>= 0);
3125 * At this point, either draining has completed and no worker
3126 * is left, or cpu down has been canceled or the cpu is being
3127 * brought back up. There shouldn't be any idle one left.
3128 * Tell the remaining busy ones to rebind once it finishes the
3129 * currently scheduled works by scheduling the rebind_work.
3131 WARN_ON(!list_empty(&gcwq
->idle_list
));
3133 for_each_busy_worker(worker
, i
, pos
, gcwq
) {
3134 struct work_struct
*rebind_work
= &worker
->rebind_work
;
3137 * Rebind_work may race with future cpu hotplug
3138 * operations. Use a separate flag to mark that
3139 * rebinding is scheduled.
3141 worker
->flags
|= WORKER_REBIND
;
3142 worker
->flags
&= ~WORKER_ROGUE
;
3144 /* queue rebind_work, wq doesn't matter, use the default one */
3145 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT
,
3146 work_data_bits(rebind_work
)))
3149 debug_work_activate(rebind_work
);
3150 insert_work(get_cwq(gcwq
->cpu
, system_wq
), rebind_work
,
3151 worker
->scheduled
.next
,
3152 work_color_to_flags(WORK_NO_COLOR
));
3155 /* relinquish manager role */
3156 gcwq
->flags
&= ~GCWQ_MANAGING_WORKERS
;
3158 /* notify completion */
3159 gcwq
->trustee
= NULL
;
3160 gcwq
->trustee_state
= TRUSTEE_DONE
;
3161 wake_up_all(&gcwq
->trustee_wait
);
3162 spin_unlock_irq(&gcwq
->lock
);
3167 * wait_trustee_state - wait for trustee to enter the specified state
3168 * @gcwq: gcwq the trustee of interest belongs to
3169 * @state: target state to wait for
3171 * Wait for the trustee to reach @state. DONE is already matched.
3174 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3175 * multiple times. To be used by cpu_callback.
3177 static void __cpuinit
wait_trustee_state(struct global_cwq
*gcwq
, int state
)
3179 if (!(gcwq
->trustee_state
== state
||
3180 gcwq
->trustee_state
== TRUSTEE_DONE
)) {
3181 spin_unlock_irq(&gcwq
->lock
);
3182 __wait_event(gcwq
->trustee_wait
,
3183 gcwq
->trustee_state
== state
||
3184 gcwq
->trustee_state
== TRUSTEE_DONE
);
3185 spin_lock_irq(&gcwq
->lock
);
3189 static int __devinit
workqueue_cpu_callback(struct notifier_block
*nfb
,
3190 unsigned long action
,
3193 unsigned int cpu
= (unsigned long)hcpu
;
3194 struct global_cwq
*gcwq
= get_gcwq(cpu
);
3195 struct task_struct
*new_trustee
= NULL
;
3196 struct worker
*uninitialized_var(new_worker
);
3197 unsigned long flags
;
3199 action
&= ~CPU_TASKS_FROZEN
;
3202 case CPU_DOWN_PREPARE
:
3203 new_trustee
= kthread_create(trustee_thread
, gcwq
,
3204 "workqueue_trustee/%d\n", cpu
);
3205 if (IS_ERR(new_trustee
))
3206 return notifier_from_errno(PTR_ERR(new_trustee
));
3207 kthread_bind(new_trustee
, cpu
);
3209 case CPU_UP_PREPARE
:
3210 BUG_ON(gcwq
->first_idle
);
3211 new_worker
= create_worker(gcwq
, false);
3214 kthread_stop(new_trustee
);
3219 /* some are called w/ irq disabled, don't disturb irq status */
3220 spin_lock_irqsave(&gcwq
->lock
, flags
);
3223 case CPU_DOWN_PREPARE
:
3224 /* initialize trustee and tell it to acquire the gcwq */
3225 BUG_ON(gcwq
->trustee
|| gcwq
->trustee_state
!= TRUSTEE_DONE
);
3226 gcwq
->trustee
= new_trustee
;
3227 gcwq
->trustee_state
= TRUSTEE_START
;
3228 wake_up_process(gcwq
->trustee
);
3229 wait_trustee_state(gcwq
, TRUSTEE_IN_CHARGE
);
3231 case CPU_UP_PREPARE
:
3232 BUG_ON(gcwq
->first_idle
);
3233 gcwq
->first_idle
= new_worker
;
3238 * Before this, the trustee and all workers except for
3239 * the ones which are still executing works from
3240 * before the last CPU down must be on the cpu. After
3241 * this, they'll all be diasporas.
3243 gcwq
->flags
|= GCWQ_DISASSOCIATED
;
3247 gcwq
->trustee_state
= TRUSTEE_BUTCHER
;
3249 case CPU_UP_CANCELED
:
3250 destroy_worker(gcwq
->first_idle
);
3251 gcwq
->first_idle
= NULL
;
3254 case CPU_DOWN_FAILED
:
3256 gcwq
->flags
&= ~GCWQ_DISASSOCIATED
;
3257 if (gcwq
->trustee_state
!= TRUSTEE_DONE
) {
3258 gcwq
->trustee_state
= TRUSTEE_RELEASE
;
3259 wake_up_process(gcwq
->trustee
);
3260 wait_trustee_state(gcwq
, TRUSTEE_DONE
);
3264 * Trustee is done and there might be no worker left.
3265 * Put the first_idle in and request a real manager to
3268 spin_unlock_irq(&gcwq
->lock
);
3269 kthread_bind(gcwq
->first_idle
->task
, cpu
);
3270 spin_lock_irq(&gcwq
->lock
);
3271 gcwq
->flags
|= GCWQ_MANAGE_WORKERS
;
3272 start_worker(gcwq
->first_idle
);
3273 gcwq
->first_idle
= NULL
;
3277 spin_unlock_irqrestore(&gcwq
->lock
, flags
);
3279 return notifier_from_errno(0);
3284 struct work_for_cpu
{
3285 struct completion completion
;
3291 static int do_work_for_cpu(void *_wfc
)
3293 struct work_for_cpu
*wfc
= _wfc
;
3294 wfc
->ret
= wfc
->fn(wfc
->arg
);
3295 complete(&wfc
->completion
);
3300 * work_on_cpu - run a function in user context on a particular cpu
3301 * @cpu: the cpu to run on
3302 * @fn: the function to run
3303 * @arg: the function arg
3305 * This will return the value @fn returns.
3306 * It is up to the caller to ensure that the cpu doesn't go offline.
3307 * The caller must not hold any locks which would prevent @fn from completing.
3309 long work_on_cpu(unsigned int cpu
, long (*fn
)(void *), void *arg
)
3311 struct task_struct
*sub_thread
;
3312 struct work_for_cpu wfc
= {
3313 .completion
= COMPLETION_INITIALIZER_ONSTACK(wfc
.completion
),
3318 sub_thread
= kthread_create(do_work_for_cpu
, &wfc
, "work_for_cpu");
3319 if (IS_ERR(sub_thread
))
3320 return PTR_ERR(sub_thread
);
3321 kthread_bind(sub_thread
, cpu
);
3322 wake_up_process(sub_thread
);
3323 wait_for_completion(&wfc
.completion
);
3326 EXPORT_SYMBOL_GPL(work_on_cpu
);
3327 #endif /* CONFIG_SMP */
3329 #ifdef CONFIG_FREEZER
3332 * freeze_workqueues_begin - begin freezing workqueues
3334 * Start freezing workqueues. After this function returns, all
3335 * freezeable workqueues will queue new works to their frozen_works
3336 * list instead of gcwq->worklist.
3339 * Grabs and releases workqueue_lock and gcwq->lock's.
3341 void freeze_workqueues_begin(void)
3345 spin_lock(&workqueue_lock
);
3347 BUG_ON(workqueue_freezing
);
3348 workqueue_freezing
= true;
3350 for_each_gcwq_cpu(cpu
) {
3351 struct global_cwq
*gcwq
= get_gcwq(cpu
);
3352 struct workqueue_struct
*wq
;
3354 spin_lock_irq(&gcwq
->lock
);
3356 BUG_ON(gcwq
->flags
& GCWQ_FREEZING
);
3357 gcwq
->flags
|= GCWQ_FREEZING
;
3359 list_for_each_entry(wq
, &workqueues
, list
) {
3360 struct cpu_workqueue_struct
*cwq
= get_cwq(cpu
, wq
);
3362 if (cwq
&& wq
->flags
& WQ_FREEZEABLE
)
3363 cwq
->max_active
= 0;
3366 spin_unlock_irq(&gcwq
->lock
);
3369 spin_unlock(&workqueue_lock
);
3373 * freeze_workqueues_busy - are freezeable workqueues still busy?
3375 * Check whether freezing is complete. This function must be called
3376 * between freeze_workqueues_begin() and thaw_workqueues().
3379 * Grabs and releases workqueue_lock.
3382 * %true if some freezeable workqueues are still busy. %false if
3383 * freezing is complete.
3385 bool freeze_workqueues_busy(void)
3390 spin_lock(&workqueue_lock
);
3392 BUG_ON(!workqueue_freezing
);
3394 for_each_gcwq_cpu(cpu
) {
3395 struct workqueue_struct
*wq
;
3397 * nr_active is monotonically decreasing. It's safe
3398 * to peek without lock.
3400 list_for_each_entry(wq
, &workqueues
, list
) {
3401 struct cpu_workqueue_struct
*cwq
= get_cwq(cpu
, wq
);
3403 if (!cwq
|| !(wq
->flags
& WQ_FREEZEABLE
))
3406 BUG_ON(cwq
->nr_active
< 0);
3407 if (cwq
->nr_active
) {
3414 spin_unlock(&workqueue_lock
);
3419 * thaw_workqueues - thaw workqueues
3421 * Thaw workqueues. Normal queueing is restored and all collected
3422 * frozen works are transferred to their respective gcwq worklists.
3425 * Grabs and releases workqueue_lock and gcwq->lock's.
3427 void thaw_workqueues(void)
3431 spin_lock(&workqueue_lock
);
3433 if (!workqueue_freezing
)
3436 for_each_gcwq_cpu(cpu
) {
3437 struct global_cwq
*gcwq
= get_gcwq(cpu
);
3438 struct workqueue_struct
*wq
;
3440 spin_lock_irq(&gcwq
->lock
);
3442 BUG_ON(!(gcwq
->flags
& GCWQ_FREEZING
));
3443 gcwq
->flags
&= ~GCWQ_FREEZING
;
3445 list_for_each_entry(wq
, &workqueues
, list
) {
3446 struct cpu_workqueue_struct
*cwq
= get_cwq(cpu
, wq
);
3448 if (!cwq
|| !(wq
->flags
& WQ_FREEZEABLE
))
3451 /* restore max_active and repopulate worklist */
3452 cwq
->max_active
= wq
->saved_max_active
;
3454 while (!list_empty(&cwq
->delayed_works
) &&
3455 cwq
->nr_active
< cwq
->max_active
)
3456 cwq_activate_first_delayed(cwq
);
3459 wake_up_worker(gcwq
);
3461 spin_unlock_irq(&gcwq
->lock
);
3464 workqueue_freezing
= false;
3466 spin_unlock(&workqueue_lock
);
3468 #endif /* CONFIG_FREEZER */
3470 void __init
init_workqueues(void)
3476 * The pointer part of work->data is either pointing to the
3477 * cwq or contains the cpu number the work ran last on. Make
3478 * sure cpu number won't overflow into kernel pointer area so
3479 * that they can be distinguished.
3481 BUILD_BUG_ON(WORK_CPU_LAST
<< WORK_STRUCT_FLAG_BITS
>= PAGE_OFFSET
);
3483 hotcpu_notifier(workqueue_cpu_callback
, CPU_PRI_WORKQUEUE
);
3485 /* initialize gcwqs */
3486 for_each_gcwq_cpu(cpu
) {
3487 struct global_cwq
*gcwq
= get_gcwq(cpu
);
3489 spin_lock_init(&gcwq
->lock
);
3490 INIT_LIST_HEAD(&gcwq
->worklist
);
3492 if (cpu
== WORK_CPU_UNBOUND
)
3493 gcwq
->flags
|= GCWQ_DISASSOCIATED
;
3495 INIT_LIST_HEAD(&gcwq
->idle_list
);
3496 for (i
= 0; i
< BUSY_WORKER_HASH_SIZE
; i
++)
3497 INIT_HLIST_HEAD(&gcwq
->busy_hash
[i
]);
3499 init_timer_deferrable(&gcwq
->idle_timer
);
3500 gcwq
->idle_timer
.function
= idle_worker_timeout
;
3501 gcwq
->idle_timer
.data
= (unsigned long)gcwq
;
3503 setup_timer(&gcwq
->mayday_timer
, gcwq_mayday_timeout
,
3504 (unsigned long)gcwq
);
3506 ida_init(&gcwq
->worker_ida
);
3508 gcwq
->trustee_state
= TRUSTEE_DONE
;
3509 init_waitqueue_head(&gcwq
->trustee_wait
);
3512 /* create the initial worker */
3513 for_each_online_gcwq_cpu(cpu
) {
3514 struct global_cwq
*gcwq
= get_gcwq(cpu
);
3515 struct worker
*worker
;
3517 worker
= create_worker(gcwq
, true);
3519 spin_lock_irq(&gcwq
->lock
);
3520 start_worker(worker
);
3521 spin_unlock_irq(&gcwq
->lock
);
3524 system_wq
= alloc_workqueue("events", 0, 0);
3525 system_long_wq
= alloc_workqueue("events_long", 0, 0);
3526 system_nrt_wq
= alloc_workqueue("events_nrt", WQ_NON_REENTRANT
, 0);
3527 system_unbound_wq
= alloc_workqueue("events_unbound", WQ_UNBOUND
,
3528 WQ_UNBOUND_MAX_ACTIVE
);
3529 BUG_ON(!system_wq
|| !system_long_wq
|| !system_nrt_wq
);