blob: 9953d3c7bd1077ee28f484a89f12c2c6305ccc59 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/workqueue.c
3 *
4 * Generic mechanism for defining kernel helper threads for running
5 * arbitrary tasks in process context.
6 *
7 * Started by Ingo Molnar, Copyright (C) 2002
8 *
9 * Derived from the taskqueue/keventd code by:
10 *
11 * David Woodhouse <dwmw2@infradead.org>
Francois Camie1f8e872008-10-15 22:01:59 -070012 * Andrew Morton
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Kai Petzke <wpp@marie.physik.tu-berlin.de>
14 * Theodore Ts'o <tytso@mit.edu>
Christoph Lameter89ada672005-10-30 15:01:59 -080015 *
Christoph Lametercde53532008-07-04 09:59:22 -070016 * Made to use alloc_percpu by Christoph Lameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18
19#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/sched.h>
22#include <linux/init.h>
23#include <linux/signal.h>
24#include <linux/completion.h>
25#include <linux/workqueue.h>
26#include <linux/slab.h>
27#include <linux/cpu.h>
28#include <linux/notifier.h>
29#include <linux/kthread.h>
James Bottomley1fa44ec2006-02-23 12:43:43 -060030#include <linux/hardirq.h>
Christoph Lameter46934022006-10-11 01:21:26 -070031#include <linux/mempolicy.h>
Rafael J. Wysocki341a5952006-12-06 20:34:49 -080032#include <linux/freezer.h>
Peter Zijlstrad5abe662006-12-06 20:37:26 -080033#include <linux/kallsyms.h>
34#include <linux/debug_locks.h>
Johannes Berg4e6045f2007-10-18 23:39:55 -070035#include <linux/lockdep.h>
Tejun Heoc34056a2010-06-29 10:07:11 +020036#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/*
Tejun Heo4690c4a2010-06-29 10:07:10 +020039 * Structure fields follow one of the following exclusion rules.
40 *
41 * I: Set during initialization and read-only afterwards.
42 *
43 * L: cwq->lock protected. Access with cwq->lock held.
44 *
Tejun Heo73f53c42010-06-29 10:07:11 +020045 * F: wq->flush_mutex protected.
46 *
Tejun Heo4690c4a2010-06-29 10:07:10 +020047 * W: workqueue_lock protected.
48 */
49
Tejun Heoc34056a2010-06-29 10:07:11 +020050struct cpu_workqueue_struct;
51
52struct worker {
53 struct work_struct *current_work; /* L: work being processed */
Tejun Heoaffee4b2010-06-29 10:07:12 +020054 struct list_head scheduled; /* L: scheduled works */
Tejun Heoc34056a2010-06-29 10:07:11 +020055 struct task_struct *task; /* I: worker task */
56 struct cpu_workqueue_struct *cwq; /* I: the associated cwq */
57 int id; /* I: worker id */
58};
59
Tejun Heo4690c4a2010-06-29 10:07:10 +020060/*
Nathan Lynchf756d5e2006-01-08 01:05:12 -080061 * The per-CPU workqueue (if single thread, we always use the first
Tejun Heo0f900042010-06-29 10:07:11 +020062 * possible cpu). The lower WORK_STRUCT_FLAG_BITS of
63 * work_struct->data are used for flags and thus cwqs need to be
64 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 */
66struct cpu_workqueue_struct {
67
68 spinlock_t lock;
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 struct list_head worklist;
71 wait_queue_head_t more_work;
Tejun Heo15376632010-06-29 10:07:11 +020072 unsigned int cpu;
Tejun Heoc34056a2010-06-29 10:07:11 +020073 struct worker *worker;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Tejun Heo4690c4a2010-06-29 10:07:10 +020075 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +020076 int work_color; /* L: current color */
77 int flush_color; /* L: flushing color */
78 int nr_in_flight[WORK_NR_COLORS];
79 /* L: nr of in_flight works */
Tejun Heo0f900042010-06-29 10:07:11 +020080};
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/*
Tejun Heo73f53c42010-06-29 10:07:11 +020083 * Structure used to wait for workqueue flush.
84 */
85struct wq_flusher {
86 struct list_head list; /* F: list of flushers */
87 int flush_color; /* F: flush color waiting for */
88 struct completion done; /* flush completion */
89};
90
91/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * The externally visible workqueue abstraction is an array of
93 * per-CPU workqueues:
94 */
95struct workqueue_struct {
Tejun Heo97e37d72010-06-29 10:07:10 +020096 unsigned int flags; /* I: WQ_* flags */
Tejun Heo4690c4a2010-06-29 10:07:10 +020097 struct cpu_workqueue_struct *cpu_wq; /* I: cwq's */
98 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +020099
100 struct mutex flush_mutex; /* protects wq flushing */
101 int work_color; /* F: current work color */
102 int flush_color; /* F: current flush color */
103 atomic_t nr_cwqs_to_flush; /* flush in progress */
104 struct wq_flusher *first_flusher; /* F: first flusher */
105 struct list_head flusher_queue; /* F: flush waiters */
106 struct list_head flusher_overflow; /* F: flush overflow list */
107
Tejun Heo4690c4a2010-06-29 10:07:10 +0200108 const char *name; /* I: workqueue name */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700109#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200110 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700111#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112};
113
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900114#ifdef CONFIG_DEBUG_OBJECTS_WORK
115
116static struct debug_obj_descr work_debug_descr;
117
118/*
119 * fixup_init is called when:
120 * - an active object is initialized
121 */
122static int work_fixup_init(void *addr, enum debug_obj_state state)
123{
124 struct work_struct *work = addr;
125
126 switch (state) {
127 case ODEBUG_STATE_ACTIVE:
128 cancel_work_sync(work);
129 debug_object_init(work, &work_debug_descr);
130 return 1;
131 default:
132 return 0;
133 }
134}
135
136/*
137 * fixup_activate is called when:
138 * - an active object is activated
139 * - an unknown object is activated (might be a statically initialized object)
140 */
141static int work_fixup_activate(void *addr, enum debug_obj_state state)
142{
143 struct work_struct *work = addr;
144
145 switch (state) {
146
147 case ODEBUG_STATE_NOTAVAILABLE:
148 /*
149 * This is not really a fixup. The work struct was
150 * statically initialized. We just make sure that it
151 * is tracked in the object tracker.
152 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200153 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900154 debug_object_init(work, &work_debug_descr);
155 debug_object_activate(work, &work_debug_descr);
156 return 0;
157 }
158 WARN_ON_ONCE(1);
159 return 0;
160
161 case ODEBUG_STATE_ACTIVE:
162 WARN_ON(1);
163
164 default:
165 return 0;
166 }
167}
168
169/*
170 * fixup_free is called when:
171 * - an active object is freed
172 */
173static int work_fixup_free(void *addr, enum debug_obj_state state)
174{
175 struct work_struct *work = addr;
176
177 switch (state) {
178 case ODEBUG_STATE_ACTIVE:
179 cancel_work_sync(work);
180 debug_object_free(work, &work_debug_descr);
181 return 1;
182 default:
183 return 0;
184 }
185}
186
187static struct debug_obj_descr work_debug_descr = {
188 .name = "work_struct",
189 .fixup_init = work_fixup_init,
190 .fixup_activate = work_fixup_activate,
191 .fixup_free = work_fixup_free,
192};
193
194static inline void debug_work_activate(struct work_struct *work)
195{
196 debug_object_activate(work, &work_debug_descr);
197}
198
199static inline void debug_work_deactivate(struct work_struct *work)
200{
201 debug_object_deactivate(work, &work_debug_descr);
202}
203
204void __init_work(struct work_struct *work, int onstack)
205{
206 if (onstack)
207 debug_object_init_on_stack(work, &work_debug_descr);
208 else
209 debug_object_init(work, &work_debug_descr);
210}
211EXPORT_SYMBOL_GPL(__init_work);
212
213void destroy_work_on_stack(struct work_struct *work)
214{
215 debug_object_free(work, &work_debug_descr);
216}
217EXPORT_SYMBOL_GPL(destroy_work_on_stack);
218
219#else
220static inline void debug_work_activate(struct work_struct *work) { }
221static inline void debug_work_deactivate(struct work_struct *work) { }
222#endif
223
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100224/* Serializes the accesses to the list of workqueues. */
225static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226static LIST_HEAD(workqueues);
Tejun Heoc34056a2010-06-29 10:07:11 +0200227static DEFINE_PER_CPU(struct ida, worker_ida);
228
229static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Oleg Nesterov3af244332007-05-09 02:34:09 -0700231static int singlethread_cpu __read_mostly;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700232
Tejun Heo4690c4a2010-06-29 10:07:10 +0200233static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
234 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700235{
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700236 return per_cpu_ptr(wq->cpu_wq, cpu);
237}
238
Tejun Heo15376632010-06-29 10:07:11 +0200239static struct cpu_workqueue_struct *target_cwq(unsigned int cpu,
240 struct workqueue_struct *wq)
241{
242 if (unlikely(wq->flags & WQ_SINGLE_THREAD))
243 cpu = singlethread_cpu;
244 return get_cwq(cpu, wq);
245}
246
Tejun Heo73f53c42010-06-29 10:07:11 +0200247static unsigned int work_color_to_flags(int color)
248{
249 return color << WORK_STRUCT_COLOR_SHIFT;
250}
251
252static int get_work_color(struct work_struct *work)
253{
254 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
255 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
256}
257
258static int work_next_color(int color)
259{
260 return (color + 1) % WORK_NR_COLORS;
261}
262
David Howells4594bf12006-12-07 11:33:26 +0000263/*
264 * Set the workqueue on which a work item is to be run
265 * - Must *only* be called if the pending flag is set
266 */
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700267static inline void set_wq_data(struct work_struct *work,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200268 struct cpu_workqueue_struct *cwq,
269 unsigned long extra_flags)
David Howells365970a2006-11-22 14:54:49 +0000270{
David Howells4594bf12006-12-07 11:33:26 +0000271 BUG_ON(!work_pending(work));
272
Tejun Heo4690c4a2010-06-29 10:07:10 +0200273 atomic_long_set(&work->data, (unsigned long)cwq | work_static(work) |
Tejun Heo22df02b2010-06-29 10:07:10 +0200274 WORK_STRUCT_PENDING | extra_flags);
David Howells365970a2006-11-22 14:54:49 +0000275}
276
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200277/*
278 * Clear WORK_STRUCT_PENDING and the workqueue on which it was queued.
279 */
280static inline void clear_wq_data(struct work_struct *work)
281{
Tejun Heo4690c4a2010-06-29 10:07:10 +0200282 atomic_long_set(&work->data, work_static(work));
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200283}
284
Tejun Heo64166692010-06-29 10:07:11 +0200285static inline struct cpu_workqueue_struct *get_wq_data(struct work_struct *work)
David Howells365970a2006-11-22 14:54:49 +0000286{
Tejun Heo64166692010-06-29 10:07:11 +0200287 return (void *)(atomic_long_read(&work->data) &
288 WORK_STRUCT_WQ_DATA_MASK);
David Howells365970a2006-11-22 14:54:49 +0000289}
290
Tejun Heo4690c4a2010-06-29 10:07:10 +0200291/**
292 * insert_work - insert a work into cwq
293 * @cwq: cwq @work belongs to
294 * @work: work to insert
295 * @head: insertion point
296 * @extra_flags: extra WORK_STRUCT_* flags to set
297 *
298 * Insert @work into @cwq after @head.
299 *
300 * CONTEXT:
301 * spin_lock_irq(cwq->lock).
302 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700303static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200304 struct work_struct *work, struct list_head *head,
305 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700306{
Tejun Heo4690c4a2010-06-29 10:07:10 +0200307 /* we own @work, set data and link */
308 set_wq_data(work, cwq, extra_flags);
309
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700310 /*
311 * Ensure that we get the right work->data if we see the
312 * result of list_add() below, see try_to_grab_pending().
313 */
314 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +0200315
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700316 list_add_tail(&work->entry, head);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700317 wake_up(&cwq->more_work);
318}
319
Tejun Heo4690c4a2010-06-29 10:07:10 +0200320static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 struct work_struct *work)
322{
Tejun Heo15376632010-06-29 10:07:11 +0200323 struct cpu_workqueue_struct *cwq = target_cwq(cpu, wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 unsigned long flags;
325
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900326 debug_work_activate(work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 spin_lock_irqsave(&cwq->lock, flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +0200328 BUG_ON(!list_empty(&work->entry));
Tejun Heo73f53c42010-06-29 10:07:11 +0200329 cwq->nr_in_flight[cwq->work_color]++;
330 insert_work(cwq, work, &cwq->worklist,
331 work_color_to_flags(cwq->work_color));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 spin_unlock_irqrestore(&cwq->lock, flags);
333}
334
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700335/**
336 * queue_work - queue work on a workqueue
337 * @wq: workqueue to use
338 * @work: work to queue
339 *
Alan Stern057647f2006-10-28 10:38:58 -0700340 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -0700342 * We queue the work to the CPU on which it was submitted, but if the CPU dies
343 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800345int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Oleg Nesterovef1ca232008-07-25 01:47:53 -0700347 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Oleg Nesterovef1ca232008-07-25 01:47:53 -0700349 ret = queue_work_on(get_cpu(), wq, work);
350 put_cpu();
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return ret;
353}
Dave Jonesae90dd52006-06-30 01:40:45 -0400354EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Zhang Ruic1a220e2008-07-23 21:28:39 -0700356/**
357 * queue_work_on - queue work on specific cpu
358 * @cpu: CPU number to execute work on
359 * @wq: workqueue to use
360 * @work: work to queue
361 *
362 * Returns 0 if @work was already on a queue, non-zero otherwise.
363 *
364 * We queue the work to a specific CPU, the caller must ensure it
365 * can't go away.
366 */
367int
368queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
369{
370 int ret = 0;
371
Tejun Heo22df02b2010-06-29 10:07:10 +0200372 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +0200373 __queue_work(cpu, wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -0700374 ret = 1;
375 }
376 return ret;
377}
378EXPORT_SYMBOL_GPL(queue_work_on);
379
Li Zefan6d141c32008-02-08 04:21:09 -0800380static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
David Howells52bad642006-11-22 14:54:01 +0000382 struct delayed_work *dwork = (struct delayed_work *)__data;
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700383 struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Tejun Heo4690c4a2010-06-29 10:07:10 +0200385 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700388/**
389 * queue_delayed_work - queue work on a workqueue after delay
390 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800391 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700392 * @delay: number of jiffies to wait before queueing
393 *
Alan Stern057647f2006-10-28 10:38:58 -0700394 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700395 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800396int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000397 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
David Howells52bad642006-11-22 14:54:01 +0000399 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -0700400 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Oleg Nesterov63bc0362007-05-09 02:34:16 -0700402 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
Dave Jonesae90dd52006-06-30 01:40:45 -0400404EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700406/**
407 * queue_delayed_work_on - queue work on specific CPU after delay
408 * @cpu: CPU number to execute work on
409 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800410 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700411 * @delay: number of jiffies to wait before queueing
412 *
Alan Stern057647f2006-10-28 10:38:58 -0700413 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700414 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700415int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000416 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700417{
418 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +0000419 struct timer_list *timer = &dwork->timer;
420 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700421
Tejun Heo22df02b2010-06-29 10:07:10 +0200422 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700423 BUG_ON(timer_pending(timer));
424 BUG_ON(!list_empty(&work->entry));
425
Andrew Liu8a3e77c2008-05-01 04:35:14 -0700426 timer_stats_timer_set_start_info(&dwork->timer);
427
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700428 /* This stores cwq for the moment, for the timer_fn */
Tejun Heo15376632010-06-29 10:07:11 +0200429 set_wq_data(work, target_cwq(raw_smp_processor_id(), wq), 0);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700430 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +0000431 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700432 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -0700433
434 if (unlikely(cpu >= 0))
435 add_timer_on(timer, cpu);
436 else
437 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700438 ret = 1;
439 }
440 return ret;
441}
Dave Jonesae90dd52006-06-30 01:40:45 -0400442EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Tejun Heoc34056a2010-06-29 10:07:11 +0200444static struct worker *alloc_worker(void)
445{
446 struct worker *worker;
447
448 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoaffee4b2010-06-29 10:07:12 +0200449 if (worker)
450 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoc34056a2010-06-29 10:07:11 +0200451 return worker;
452}
453
454/**
455 * create_worker - create a new workqueue worker
456 * @cwq: cwq the new worker will belong to
457 * @bind: whether to set affinity to @cpu or not
458 *
459 * Create a new worker which is bound to @cwq. The returned worker
460 * can be started by calling start_worker() or destroyed using
461 * destroy_worker().
462 *
463 * CONTEXT:
464 * Might sleep. Does GFP_KERNEL allocations.
465 *
466 * RETURNS:
467 * Pointer to the newly created worker.
468 */
469static struct worker *create_worker(struct cpu_workqueue_struct *cwq, bool bind)
470{
471 int id = -1;
472 struct worker *worker = NULL;
473
474 spin_lock(&workqueue_lock);
475 while (ida_get_new(&per_cpu(worker_ida, cwq->cpu), &id)) {
476 spin_unlock(&workqueue_lock);
477 if (!ida_pre_get(&per_cpu(worker_ida, cwq->cpu), GFP_KERNEL))
478 goto fail;
479 spin_lock(&workqueue_lock);
480 }
481 spin_unlock(&workqueue_lock);
482
483 worker = alloc_worker();
484 if (!worker)
485 goto fail;
486
487 worker->cwq = cwq;
488 worker->id = id;
489
490 worker->task = kthread_create(worker_thread, worker, "kworker/%u:%d",
491 cwq->cpu, id);
492 if (IS_ERR(worker->task))
493 goto fail;
494
495 if (bind)
496 kthread_bind(worker->task, cwq->cpu);
497
498 return worker;
499fail:
500 if (id >= 0) {
501 spin_lock(&workqueue_lock);
502 ida_remove(&per_cpu(worker_ida, cwq->cpu), id);
503 spin_unlock(&workqueue_lock);
504 }
505 kfree(worker);
506 return NULL;
507}
508
509/**
510 * start_worker - start a newly created worker
511 * @worker: worker to start
512 *
513 * Start @worker.
514 *
515 * CONTEXT:
516 * spin_lock_irq(cwq->lock).
517 */
518static void start_worker(struct worker *worker)
519{
520 wake_up_process(worker->task);
521}
522
523/**
524 * destroy_worker - destroy a workqueue worker
525 * @worker: worker to be destroyed
526 *
527 * Destroy @worker.
528 */
529static void destroy_worker(struct worker *worker)
530{
531 int cpu = worker->cwq->cpu;
532 int id = worker->id;
533
534 /* sanity check frenzy */
535 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +0200536 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +0200537
538 kthread_stop(worker->task);
539 kfree(worker);
540
541 spin_lock(&workqueue_lock);
542 ida_remove(&per_cpu(worker_ida, cpu), id);
543 spin_unlock(&workqueue_lock);
544}
545
Tejun Heoa62428c2010-06-29 10:07:10 +0200546/**
Tejun Heoaffee4b2010-06-29 10:07:12 +0200547 * move_linked_works - move linked works to a list
548 * @work: start of series of works to be scheduled
549 * @head: target list to append @work to
550 * @nextp: out paramter for nested worklist walking
551 *
552 * Schedule linked works starting from @work to @head. Work series to
553 * be scheduled starts at @work and includes any consecutive work with
554 * WORK_STRUCT_LINKED set in its predecessor.
555 *
556 * If @nextp is not NULL, it's updated to point to the next work of
557 * the last scheduled work. This allows move_linked_works() to be
558 * nested inside outer list_for_each_entry_safe().
559 *
560 * CONTEXT:
561 * spin_lock_irq(cwq->lock).
562 */
563static void move_linked_works(struct work_struct *work, struct list_head *head,
564 struct work_struct **nextp)
565{
566 struct work_struct *n;
567
568 /*
569 * Linked worklist will always end before the end of the list,
570 * use NULL for list head.
571 */
572 list_for_each_entry_safe_from(work, n, NULL, entry) {
573 list_move_tail(&work->entry, head);
574 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
575 break;
576 }
577
578 /*
579 * If we're already inside safe list traversal and have moved
580 * multiple works to the scheduled queue, the next position
581 * needs to be updated.
582 */
583 if (nextp)
584 *nextp = n;
585}
586
587/**
Tejun Heo73f53c42010-06-29 10:07:11 +0200588 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
589 * @cwq: cwq of interest
590 * @color: color of work which left the queue
591 *
592 * A work either has completed or is removed from pending queue,
593 * decrement nr_in_flight of its cwq and handle workqueue flushing.
594 *
595 * CONTEXT:
596 * spin_lock_irq(cwq->lock).
597 */
598static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color)
599{
600 /* ignore uncolored works */
601 if (color == WORK_NO_COLOR)
602 return;
603
604 cwq->nr_in_flight[color]--;
605
606 /* is flush in progress and are we at the flushing tip? */
607 if (likely(cwq->flush_color != color))
608 return;
609
610 /* are there still in-flight works? */
611 if (cwq->nr_in_flight[color])
612 return;
613
614 /* this cwq is done, clear flush_color */
615 cwq->flush_color = -1;
616
617 /*
618 * If this was the last cwq, wake up the first flusher. It
619 * will handle the rest.
620 */
621 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
622 complete(&cwq->wq->first_flusher->done);
623}
624
625/**
Tejun Heoa62428c2010-06-29 10:07:10 +0200626 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +0200627 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +0200628 * @work: work to process
629 *
630 * Process @work. This function contains all the logics necessary to
631 * process a single work including synchronization against and
632 * interaction with other workers on the same cpu, queueing and
633 * flushing. As long as context requirement is met, any worker can
634 * call this function to process a work.
635 *
636 * CONTEXT:
637 * spin_lock_irq(cwq->lock) which is released and regrabbed.
638 */
Tejun Heoc34056a2010-06-29 10:07:11 +0200639static void process_one_work(struct worker *worker, struct work_struct *work)
Tejun Heoa62428c2010-06-29 10:07:10 +0200640{
Tejun Heoc34056a2010-06-29 10:07:11 +0200641 struct cpu_workqueue_struct *cwq = worker->cwq;
Tejun Heoa62428c2010-06-29 10:07:10 +0200642 work_func_t f = work->func;
Tejun Heo73f53c42010-06-29 10:07:11 +0200643 int work_color;
Tejun Heoa62428c2010-06-29 10:07:10 +0200644#ifdef CONFIG_LOCKDEP
645 /*
646 * It is permissible to free the struct work_struct from
647 * inside the function that is called from it, this we need to
648 * take into account for lockdep too. To avoid bogus "held
649 * lock freed" warnings as well as problems when looking into
650 * work->lockdep_map, make a copy and use that here.
651 */
652 struct lockdep_map lockdep_map = work->lockdep_map;
653#endif
654 /* claim and process */
Tejun Heoa62428c2010-06-29 10:07:10 +0200655 debug_work_deactivate(work);
Tejun Heoc34056a2010-06-29 10:07:11 +0200656 worker->current_work = work;
Tejun Heo73f53c42010-06-29 10:07:11 +0200657 work_color = get_work_color(work);
Tejun Heoa62428c2010-06-29 10:07:10 +0200658 list_del_init(&work->entry);
659
660 spin_unlock_irq(&cwq->lock);
661
662 BUG_ON(get_wq_data(work) != cwq);
663 work_clear_pending(work);
664 lock_map_acquire(&cwq->wq->lockdep_map);
665 lock_map_acquire(&lockdep_map);
666 f(work);
667 lock_map_release(&lockdep_map);
668 lock_map_release(&cwq->wq->lockdep_map);
669
670 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
671 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
672 "%s/0x%08x/%d\n",
673 current->comm, preempt_count(), task_pid_nr(current));
674 printk(KERN_ERR " last function: ");
675 print_symbol("%s\n", (unsigned long)f);
676 debug_show_held_locks(current);
677 dump_stack();
678 }
679
680 spin_lock_irq(&cwq->lock);
681
682 /* we're done with it, release */
Tejun Heoc34056a2010-06-29 10:07:11 +0200683 worker->current_work = NULL;
Tejun Heo73f53c42010-06-29 10:07:11 +0200684 cwq_dec_nr_in_flight(cwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +0200685}
686
Tejun Heoaffee4b2010-06-29 10:07:12 +0200687/**
688 * process_scheduled_works - process scheduled works
689 * @worker: self
690 *
691 * Process all scheduled works. Please note that the scheduled list
692 * may change while processing a work, so this function repeatedly
693 * fetches a work from the top and executes it.
694 *
695 * CONTEXT:
696 * spin_lock_irq(cwq->lock) which may be released and regrabbed
697 * multiple times.
698 */
699static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Tejun Heoaffee4b2010-06-29 10:07:12 +0200701 while (!list_empty(&worker->scheduled)) {
702 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +0200704 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
Tejun Heo4690c4a2010-06-29 10:07:10 +0200708/**
709 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +0200710 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +0200711 *
712 * The cwq worker thread function.
713 */
Tejun Heoc34056a2010-06-29 10:07:11 +0200714static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Tejun Heoc34056a2010-06-29 10:07:11 +0200716 struct worker *worker = __worker;
717 struct cpu_workqueue_struct *cwq = worker->cwq;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700718 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Tejun Heo97e37d72010-06-29 10:07:10 +0200720 if (cwq->wq->flags & WQ_FREEZEABLE)
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700721 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Oleg Nesterov3af244332007-05-09 02:34:09 -0700723 for (;;) {
Oleg Nesterov3af244332007-05-09 02:34:09 -0700724 prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
Oleg Nesterov14441962007-05-23 13:57:57 -0700725 if (!freezing(current) &&
726 !kthread_should_stop() &&
727 list_empty(&cwq->worklist))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 schedule();
Oleg Nesterov3af244332007-05-09 02:34:09 -0700729 finish_wait(&cwq->more_work, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Oleg Nesterov85f41862007-05-09 02:34:20 -0700731 try_to_freeze();
732
Oleg Nesterov14441962007-05-23 13:57:57 -0700733 if (kthread_should_stop())
Oleg Nesterov3af244332007-05-09 02:34:09 -0700734 break;
735
Tejun Heoc34056a2010-06-29 10:07:11 +0200736 if (unlikely(!cpumask_equal(&worker->task->cpus_allowed,
Tejun Heo15376632010-06-29 10:07:11 +0200737 get_cpu_mask(cwq->cpu))))
Tejun Heoc34056a2010-06-29 10:07:11 +0200738 set_cpus_allowed_ptr(worker->task,
Tejun Heo15376632010-06-29 10:07:11 +0200739 get_cpu_mask(cwq->cpu));
Tejun Heoaffee4b2010-06-29 10:07:12 +0200740
741 spin_lock_irq(&cwq->lock);
742
743 while (!list_empty(&cwq->worklist)) {
744 struct work_struct *work =
745 list_first_entry(&cwq->worklist,
746 struct work_struct, entry);
747
748 if (likely(!(*work_data_bits(work) &
749 WORK_STRUCT_LINKED))) {
750 /* optimization path, not strictly necessary */
751 process_one_work(worker, work);
752 if (unlikely(!list_empty(&worker->scheduled)))
753 process_scheduled_works(worker);
754 } else {
755 move_linked_works(work, &worker->scheduled,
756 NULL);
757 process_scheduled_works(worker);
758 }
759 }
760
761 spin_unlock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
Oleg Nesterov3af244332007-05-09 02:34:09 -0700763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return 0;
765}
766
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700767struct wq_barrier {
768 struct work_struct work;
769 struct completion done;
770};
771
772static void wq_barrier_func(struct work_struct *work)
773{
774 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
775 complete(&barr->done);
776}
777
Tejun Heo4690c4a2010-06-29 10:07:10 +0200778/**
779 * insert_wq_barrier - insert a barrier work
780 * @cwq: cwq to insert barrier into
781 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +0200782 * @target: target work to attach @barr to
783 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +0200784 *
Tejun Heoaffee4b2010-06-29 10:07:12 +0200785 * @barr is linked to @target such that @barr is completed only after
786 * @target finishes execution. Please note that the ordering
787 * guarantee is observed only with respect to @target and on the local
788 * cpu.
789 *
790 * Currently, a queued barrier can't be canceled. This is because
791 * try_to_grab_pending() can't determine whether the work to be
792 * grabbed is at the head of the queue and thus can't clear LINKED
793 * flag of the previous work while there must be a valid next work
794 * after a work with LINKED flag set.
795 *
796 * Note that when @worker is non-NULL, @target may be modified
797 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200798 *
799 * CONTEXT:
800 * spin_lock_irq(cwq->lock).
801 */
Oleg Nesterov83c22522007-05-09 02:33:54 -0700802static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +0200803 struct wq_barrier *barr,
804 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700805{
Tejun Heoaffee4b2010-06-29 10:07:12 +0200806 struct list_head *head;
807 unsigned int linked = 0;
808
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900809 /*
810 * debugobject calls are safe here even with cwq->lock locked
811 * as we know for sure that this will not trigger any of the
812 * checks and call back into the fixup functions where we
813 * might deadlock.
814 */
815 INIT_WORK_ON_STACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +0200816 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700817 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -0700818
Tejun Heoaffee4b2010-06-29 10:07:12 +0200819 /*
820 * If @target is currently being executed, schedule the
821 * barrier to the worker; otherwise, put it after @target.
822 */
823 if (worker)
824 head = worker->scheduled.next;
825 else {
826 unsigned long *bits = work_data_bits(target);
827
828 head = target->entry.next;
829 /* there can already be other linked works, inherit and set */
830 linked = *bits & WORK_STRUCT_LINKED;
831 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
832 }
833
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900834 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +0200835 insert_work(cwq, &barr->work, head,
836 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700837}
838
Tejun Heo73f53c42010-06-29 10:07:11 +0200839/**
840 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
841 * @wq: workqueue being flushed
842 * @flush_color: new flush color, < 0 for no-op
843 * @work_color: new work color, < 0 for no-op
844 *
845 * Prepare cwqs for workqueue flushing.
846 *
847 * If @flush_color is non-negative, flush_color on all cwqs should be
848 * -1. If no cwq has in-flight commands at the specified color, all
849 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
850 * has in flight commands, its cwq->flush_color is set to
851 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
852 * wakeup logic is armed and %true is returned.
853 *
854 * The caller should have initialized @wq->first_flusher prior to
855 * calling this function with non-negative @flush_color. If
856 * @flush_color is negative, no flush color update is done and %false
857 * is returned.
858 *
859 * If @work_color is non-negative, all cwqs should have the same
860 * work_color which is previous to @work_color and all will be
861 * advanced to @work_color.
862 *
863 * CONTEXT:
864 * mutex_lock(wq->flush_mutex).
865 *
866 * RETURNS:
867 * %true if @flush_color >= 0 and there's something to flush. %false
868 * otherwise.
869 */
870static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
871 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Tejun Heo73f53c42010-06-29 10:07:11 +0200873 bool wait = false;
874 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -0700875
Tejun Heo73f53c42010-06-29 10:07:11 +0200876 if (flush_color >= 0) {
877 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
878 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900879 }
Oleg Nesterov14441962007-05-23 13:57:57 -0700880
Tejun Heo73f53c42010-06-29 10:07:11 +0200881 for_each_possible_cpu(cpu) {
882 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
883
884 spin_lock_irq(&cwq->lock);
885
886 if (flush_color >= 0) {
887 BUG_ON(cwq->flush_color != -1);
888
889 if (cwq->nr_in_flight[flush_color]) {
890 cwq->flush_color = flush_color;
891 atomic_inc(&wq->nr_cwqs_to_flush);
892 wait = true;
893 }
894 }
895
896 if (work_color >= 0) {
897 BUG_ON(work_color != work_next_color(cwq->work_color));
898 cwq->work_color = work_color;
899 }
900
901 spin_unlock_irq(&cwq->lock);
902 }
903
904 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
905 complete(&wq->first_flusher->done);
906
907 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700910/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700912 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 *
914 * Forces execution of the workqueue and blocks until its completion.
915 * This is typically used in driver shutdown handlers.
916 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700917 * We sleep until all works which were queued on entry have been handled,
918 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800920void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Tejun Heo73f53c42010-06-29 10:07:11 +0200922 struct wq_flusher this_flusher = {
923 .list = LIST_HEAD_INIT(this_flusher.list),
924 .flush_color = -1,
925 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
926 };
927 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700928
Ingo Molnar3295f0e2008-08-11 10:30:30 +0200929 lock_map_acquire(&wq->lockdep_map);
930 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +0200931
932 mutex_lock(&wq->flush_mutex);
933
934 /*
935 * Start-to-wait phase
936 */
937 next_color = work_next_color(wq->work_color);
938
939 if (next_color != wq->flush_color) {
940 /*
941 * Color space is not full. The current work_color
942 * becomes our flush_color and work_color is advanced
943 * by one.
944 */
945 BUG_ON(!list_empty(&wq->flusher_overflow));
946 this_flusher.flush_color = wq->work_color;
947 wq->work_color = next_color;
948
949 if (!wq->first_flusher) {
950 /* no flush in progress, become the first flusher */
951 BUG_ON(wq->flush_color != this_flusher.flush_color);
952
953 wq->first_flusher = &this_flusher;
954
955 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
956 wq->work_color)) {
957 /* nothing to flush, done */
958 wq->flush_color = next_color;
959 wq->first_flusher = NULL;
960 goto out_unlock;
961 }
962 } else {
963 /* wait in queue */
964 BUG_ON(wq->flush_color == this_flusher.flush_color);
965 list_add_tail(&this_flusher.list, &wq->flusher_queue);
966 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
967 }
968 } else {
969 /*
970 * Oops, color space is full, wait on overflow queue.
971 * The next flush completion will assign us
972 * flush_color and transfer to flusher_queue.
973 */
974 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
975 }
976
977 mutex_unlock(&wq->flush_mutex);
978
979 wait_for_completion(&this_flusher.done);
980
981 /*
982 * Wake-up-and-cascade phase
983 *
984 * First flushers are responsible for cascading flushes and
985 * handling overflow. Non-first flushers can simply return.
986 */
987 if (wq->first_flusher != &this_flusher)
988 return;
989
990 mutex_lock(&wq->flush_mutex);
991
992 wq->first_flusher = NULL;
993
994 BUG_ON(!list_empty(&this_flusher.list));
995 BUG_ON(wq->flush_color != this_flusher.flush_color);
996
997 while (true) {
998 struct wq_flusher *next, *tmp;
999
1000 /* complete all the flushers sharing the current flush color */
1001 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
1002 if (next->flush_color != wq->flush_color)
1003 break;
1004 list_del_init(&next->list);
1005 complete(&next->done);
1006 }
1007
1008 BUG_ON(!list_empty(&wq->flusher_overflow) &&
1009 wq->flush_color != work_next_color(wq->work_color));
1010
1011 /* this flush_color is finished, advance by one */
1012 wq->flush_color = work_next_color(wq->flush_color);
1013
1014 /* one color has been freed, handle overflow queue */
1015 if (!list_empty(&wq->flusher_overflow)) {
1016 /*
1017 * Assign the same color to all overflowed
1018 * flushers, advance work_color and append to
1019 * flusher_queue. This is the start-to-wait
1020 * phase for these overflowed flushers.
1021 */
1022 list_for_each_entry(tmp, &wq->flusher_overflow, list)
1023 tmp->flush_color = wq->work_color;
1024
1025 wq->work_color = work_next_color(wq->work_color);
1026
1027 list_splice_tail_init(&wq->flusher_overflow,
1028 &wq->flusher_queue);
1029 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
1030 }
1031
1032 if (list_empty(&wq->flusher_queue)) {
1033 BUG_ON(wq->flush_color != wq->work_color);
1034 break;
1035 }
1036
1037 /*
1038 * Need to flush more colors. Make the next flusher
1039 * the new first flusher and arm cwqs.
1040 */
1041 BUG_ON(wq->flush_color == wq->work_color);
1042 BUG_ON(wq->flush_color != next->flush_color);
1043
1044 list_del_init(&next->list);
1045 wq->first_flusher = next;
1046
1047 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
1048 break;
1049
1050 /*
1051 * Meh... this color is already done, clear first
1052 * flusher and repeat cascading.
1053 */
1054 wq->first_flusher = NULL;
1055 }
1056
1057out_unlock:
1058 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
Dave Jonesae90dd52006-06-30 01:40:45 -04001060EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Oleg Nesterovdb700892008-07-25 01:47:49 -07001062/**
1063 * flush_work - block until a work_struct's callback has terminated
1064 * @work: the work which is to be flushed
1065 *
Oleg Nesterova67da702008-07-25 01:47:52 -07001066 * Returns false if @work has already terminated.
1067 *
Oleg Nesterovdb700892008-07-25 01:47:49 -07001068 * It is expected that, prior to calling flush_work(), the caller has
1069 * arranged for the work to not be requeued, otherwise it doesn't make
1070 * sense to use this function.
1071 */
1072int flush_work(struct work_struct *work)
1073{
Tejun Heoaffee4b2010-06-29 10:07:12 +02001074 struct worker *worker = NULL;
Oleg Nesterovdb700892008-07-25 01:47:49 -07001075 struct cpu_workqueue_struct *cwq;
Oleg Nesterovdb700892008-07-25 01:47:49 -07001076 struct wq_barrier barr;
1077
1078 might_sleep();
1079 cwq = get_wq_data(work);
1080 if (!cwq)
1081 return 0;
1082
Ingo Molnar3295f0e2008-08-11 10:30:30 +02001083 lock_map_acquire(&cwq->wq->lockdep_map);
1084 lock_map_release(&cwq->wq->lockdep_map);
Oleg Nesterova67da702008-07-25 01:47:52 -07001085
Oleg Nesterovdb700892008-07-25 01:47:49 -07001086 spin_lock_irq(&cwq->lock);
1087 if (!list_empty(&work->entry)) {
1088 /*
1089 * See the comment near try_to_grab_pending()->smp_rmb().
1090 * If it was re-queued under us we are not going to wait.
1091 */
1092 smp_rmb();
1093 if (unlikely(cwq != get_wq_data(work)))
Tejun Heo4690c4a2010-06-29 10:07:10 +02001094 goto already_gone;
Oleg Nesterovdb700892008-07-25 01:47:49 -07001095 } else {
Tejun Heoaffee4b2010-06-29 10:07:12 +02001096 if (cwq->worker && cwq->worker->current_work == work)
1097 worker = cwq->worker;
1098 if (!worker)
Tejun Heo4690c4a2010-06-29 10:07:10 +02001099 goto already_gone;
Oleg Nesterovdb700892008-07-25 01:47:49 -07001100 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07001101
Tejun Heoaffee4b2010-06-29 10:07:12 +02001102 insert_wq_barrier(cwq, &barr, work, worker);
Tejun Heo4690c4a2010-06-29 10:07:10 +02001103 spin_unlock_irq(&cwq->lock);
Oleg Nesterovdb700892008-07-25 01:47:49 -07001104 wait_for_completion(&barr.done);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001105 destroy_work_on_stack(&barr.work);
Oleg Nesterovdb700892008-07-25 01:47:49 -07001106 return 1;
Tejun Heo4690c4a2010-06-29 10:07:10 +02001107already_gone:
1108 spin_unlock_irq(&cwq->lock);
1109 return 0;
Oleg Nesterovdb700892008-07-25 01:47:49 -07001110}
1111EXPORT_SYMBOL_GPL(flush_work);
1112
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001113/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001114 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001115 * so this work can't be re-armed in any way.
1116 */
1117static int try_to_grab_pending(struct work_struct *work)
1118{
1119 struct cpu_workqueue_struct *cwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001120 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001121
Tejun Heo22df02b2010-06-29 10:07:10 +02001122 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001123 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001124
1125 /*
1126 * The queueing is in progress, or it is already queued. Try to
1127 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1128 */
1129
1130 cwq = get_wq_data(work);
1131 if (!cwq)
1132 return ret;
1133
1134 spin_lock_irq(&cwq->lock);
1135 if (!list_empty(&work->entry)) {
1136 /*
1137 * This work is queued, but perhaps we locked the wrong cwq.
1138 * In that case we must see the new value after rmb(), see
1139 * insert_work()->wmb().
1140 */
1141 smp_rmb();
1142 if (cwq == get_wq_data(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001143 debug_work_deactivate(work);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001144 list_del_init(&work->entry);
Tejun Heo73f53c42010-06-29 10:07:11 +02001145 cwq_dec_nr_in_flight(cwq, get_work_color(work));
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001146 ret = 1;
1147 }
1148 }
1149 spin_unlock_irq(&cwq->lock);
1150
1151 return ret;
1152}
1153
1154static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq,
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001155 struct work_struct *work)
1156{
1157 struct wq_barrier barr;
Tejun Heoaffee4b2010-06-29 10:07:12 +02001158 struct worker *worker;
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001159
1160 spin_lock_irq(&cwq->lock);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001161
1162 worker = NULL;
Tejun Heoc34056a2010-06-29 10:07:11 +02001163 if (unlikely(cwq->worker && cwq->worker->current_work == work)) {
Tejun Heoaffee4b2010-06-29 10:07:12 +02001164 worker = cwq->worker;
1165 insert_wq_barrier(cwq, &barr, work, worker);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001166 }
Tejun Heoaffee4b2010-06-29 10:07:12 +02001167
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001168 spin_unlock_irq(&cwq->lock);
1169
Tejun Heoaffee4b2010-06-29 10:07:12 +02001170 if (unlikely(worker)) {
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001171 wait_for_completion(&barr.done);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001172 destroy_work_on_stack(&barr.work);
1173 }
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001174}
1175
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001176static void wait_on_work(struct work_struct *work)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001177{
1178 struct cpu_workqueue_struct *cwq;
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07001179 struct workqueue_struct *wq;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07001180 int cpu;
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001181
Oleg Nesterovf293ea92007-05-09 02:34:10 -07001182 might_sleep();
1183
Ingo Molnar3295f0e2008-08-11 10:30:30 +02001184 lock_map_acquire(&work->lockdep_map);
1185 lock_map_release(&work->lockdep_map);
Johannes Berg4e6045f2007-10-18 23:39:55 -07001186
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001187 cwq = get_wq_data(work);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001188 if (!cwq)
Oleg Nesterov3af244332007-05-09 02:34:09 -07001189 return;
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001190
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07001191 wq = cwq->wq;
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07001192
Tejun Heo15376632010-06-29 10:07:11 +02001193 for_each_possible_cpu(cpu)
Tejun Heo4690c4a2010-06-29 10:07:10 +02001194 wait_on_cpu_work(get_cwq(cpu, wq), work);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001195}
1196
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001197static int __cancel_work_timer(struct work_struct *work,
1198 struct timer_list* timer)
1199{
1200 int ret;
1201
1202 do {
1203 ret = (timer && likely(del_timer(timer)));
1204 if (!ret)
1205 ret = try_to_grab_pending(work);
1206 wait_on_work(work);
1207 } while (unlikely(ret < 0));
1208
Oleg Nesterov4d707b92010-04-23 17:40:40 +02001209 clear_wq_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001210 return ret;
1211}
1212
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001213/**
1214 * cancel_work_sync - block until a work_struct's callback has terminated
1215 * @work: the work which is to be flushed
1216 *
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001217 * Returns true if @work was pending.
1218 *
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001219 * cancel_work_sync() will cancel the work if it is queued. If the work's
1220 * callback appears to be running, cancel_work_sync() will block until it
1221 * has completed.
1222 *
1223 * It is possible to use this function if the work re-queues itself. It can
1224 * cancel the work even if it migrates to another workqueue, however in that
1225 * case it only guarantees that work->func() has completed on the last queued
1226 * workqueue.
1227 *
1228 * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
1229 * pending, otherwise it goes into a busy-wait loop until the timer expires.
1230 *
1231 * The caller must ensure that workqueue_struct on which this work was last
1232 * queued can't be destroyed before this function returns.
1233 */
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001234int cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001235{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001236 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001237}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07001238EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001239
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001240/**
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07001241 * cancel_delayed_work_sync - reliably kill off a delayed work.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001242 * @dwork: the delayed work struct
1243 *
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001244 * Returns true if @dwork was pending.
1245 *
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001246 * It is possible to use this function if @dwork rearms itself via queue_work()
1247 * or queue_delayed_work(). See also the comment for cancel_work_sync().
1248 */
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001249int cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001250{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001251 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001252}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07001253EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001255static struct workqueue_struct *keventd_wq __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001257/**
1258 * schedule_work - put work task in global workqueue
1259 * @work: job to be done
1260 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02001261 * Returns zero if @work was already on the kernel-global workqueue and
1262 * non-zero otherwise.
1263 *
1264 * This puts a job in the kernel-global workqueue if it was not already
1265 * queued and leaves it in the same position on the kernel-global
1266 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001267 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001268int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
1270 return queue_work(keventd_wq, work);
1271}
Dave Jonesae90dd52006-06-30 01:40:45 -04001272EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Zhang Ruic1a220e2008-07-23 21:28:39 -07001274/*
1275 * schedule_work_on - put work task on a specific cpu
1276 * @cpu: cpu to put the work task on
1277 * @work: job to be done
1278 *
1279 * This puts a job on a specific cpu
1280 */
1281int schedule_work_on(int cpu, struct work_struct *work)
1282{
1283 return queue_work_on(cpu, keventd_wq, work);
1284}
1285EXPORT_SYMBOL(schedule_work_on);
1286
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001287/**
1288 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +00001289 * @dwork: job to be done
1290 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001291 *
1292 * After waiting for a given time this puts a job in the kernel-global
1293 * workqueue.
1294 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001295int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001296 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
David Howells52bad642006-11-22 14:54:01 +00001298 return queue_delayed_work(keventd_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
Dave Jonesae90dd52006-06-30 01:40:45 -04001300EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001302/**
Linus Torvalds8c53e462009-10-14 09:16:42 -07001303 * flush_delayed_work - block until a dwork_struct's callback has terminated
1304 * @dwork: the delayed work which is to be flushed
1305 *
1306 * Any timeout is cancelled, and any pending work is run immediately.
1307 */
1308void flush_delayed_work(struct delayed_work *dwork)
1309{
1310 if (del_timer_sync(&dwork->timer)) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001311 __queue_work(get_cpu(), get_wq_data(&dwork->work)->wq,
1312 &dwork->work);
Linus Torvalds8c53e462009-10-14 09:16:42 -07001313 put_cpu();
1314 }
1315 flush_work(&dwork->work);
1316}
1317EXPORT_SYMBOL(flush_delayed_work);
1318
1319/**
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001320 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
1321 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00001322 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001323 * @delay: number of jiffies to wait
1324 *
1325 * After waiting for a given time this puts a job in the kernel-global
1326 * workqueue on the specified CPU.
1327 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +00001329 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
David Howells52bad642006-11-22 14:54:01 +00001331 return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
Dave Jonesae90dd52006-06-30 01:40:45 -04001333EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Andrew Mortonb6136772006-06-25 05:47:49 -07001335/**
1336 * schedule_on_each_cpu - call a function on each online CPU from keventd
1337 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07001338 *
1339 * Returns zero on success.
1340 * Returns -ve errno on failure.
1341 *
Andrew Mortonb6136772006-06-25 05:47:49 -07001342 * schedule_on_each_cpu() is very slow.
1343 */
David Howells65f27f32006-11-22 14:55:48 +00001344int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08001345{
1346 int cpu;
Andi Kleen65a64462009-10-14 06:22:47 +02001347 int orig = -1;
Andrew Mortonb6136772006-06-25 05:47:49 -07001348 struct work_struct *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08001349
Andrew Mortonb6136772006-06-25 05:47:49 -07001350 works = alloc_percpu(struct work_struct);
1351 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08001352 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07001353
Gautham R Shenoy95402b32008-01-25 21:08:02 +01001354 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08001355
1356 /*
1357 * When running in keventd don't schedule a work item on
1358 * itself. Can just call directly because the work queue is
1359 * already bound. This also is faster.
1360 */
1361 if (current_is_keventd())
1362 orig = raw_smp_processor_id();
1363
Christoph Lameter15316ba2006-01-08 01:00:43 -08001364 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01001365 struct work_struct *work = per_cpu_ptr(works, cpu);
1366
1367 INIT_WORK(work, func);
Andi Kleen65a64462009-10-14 06:22:47 +02001368 if (cpu != orig)
Tejun Heo93981802009-11-17 14:06:20 -08001369 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02001370 }
Tejun Heo93981802009-11-17 14:06:20 -08001371 if (orig >= 0)
1372 func(per_cpu_ptr(works, orig));
1373
1374 for_each_online_cpu(cpu)
1375 flush_work(per_cpu_ptr(works, cpu));
1376
Gautham R Shenoy95402b32008-01-25 21:08:02 +01001377 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07001378 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08001379 return 0;
1380}
1381
Alan Sterneef6a7d2010-02-12 17:39:21 +09001382/**
1383 * flush_scheduled_work - ensure that any scheduled work has run to completion.
1384 *
1385 * Forces execution of the kernel-global workqueue and blocks until its
1386 * completion.
1387 *
1388 * Think twice before calling this function! It's very easy to get into
1389 * trouble if you don't take great care. Either of the following situations
1390 * will lead to deadlock:
1391 *
1392 * One of the work items currently on the workqueue needs to acquire
1393 * a lock held by your code or its caller.
1394 *
1395 * Your code is running in the context of a work routine.
1396 *
1397 * They will be detected by lockdep when they occur, but the first might not
1398 * occur very often. It depends on what work items are on the workqueue and
1399 * what locks they need, which you have no control over.
1400 *
1401 * In most situations flushing the entire workqueue is overkill; you merely
1402 * need to know that a particular work item isn't queued and isn't running.
1403 * In such cases you should use cancel_delayed_work_sync() or
1404 * cancel_work_sync() instead.
1405 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406void flush_scheduled_work(void)
1407{
1408 flush_workqueue(keventd_wq);
1409}
Dave Jonesae90dd52006-06-30 01:40:45 -04001410EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06001413 * execute_in_process_context - reliably execute the routine with user context
1414 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06001415 * @ew: guaranteed storage for the execute work structure (must
1416 * be available when the work executes)
1417 *
1418 * Executes the function immediately if process context is available,
1419 * otherwise schedules the function for delayed execution.
1420 *
1421 * Returns: 0 - function was executed
1422 * 1 - function was scheduled for execution
1423 */
David Howells65f27f32006-11-22 14:55:48 +00001424int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06001425{
1426 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00001427 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06001428 return 0;
1429 }
1430
David Howells65f27f32006-11-22 14:55:48 +00001431 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06001432 schedule_work(&ew->work);
1433
1434 return 1;
1435}
1436EXPORT_SYMBOL_GPL(execute_in_process_context);
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438int keventd_up(void)
1439{
1440 return keventd_wq != NULL;
1441}
1442
1443int current_is_keventd(void)
1444{
1445 struct cpu_workqueue_struct *cwq;
Hugh Dickinsd2437692007-08-27 16:06:19 +01001446 int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 int ret = 0;
1448
1449 BUG_ON(!keventd_wq);
1450
Tejun Heo15376632010-06-29 10:07:11 +02001451 cwq = get_cwq(cpu, keventd_wq);
Tejun Heoc34056a2010-06-29 10:07:11 +02001452 if (current == cwq->worker->task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 ret = 1;
1454
1455 return ret;
1456
1457}
1458
Tejun Heo0f900042010-06-29 10:07:11 +02001459static struct cpu_workqueue_struct *alloc_cwqs(void)
1460{
1461 /*
1462 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
1463 * Make sure that the alignment isn't lower than that of
1464 * unsigned long long.
1465 */
1466 const size_t size = sizeof(struct cpu_workqueue_struct);
1467 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
1468 __alignof__(unsigned long long));
1469 struct cpu_workqueue_struct *cwqs;
1470#ifndef CONFIG_SMP
1471 void *ptr;
1472
1473 /*
1474 * On UP, percpu allocator doesn't honor alignment parameter
1475 * and simply uses arch-dependent default. Allocate enough
1476 * room to align cwq and put an extra pointer at the end
1477 * pointing back to the originally allocated pointer which
1478 * will be used for free.
1479 *
1480 * FIXME: This really belongs to UP percpu code. Update UP
1481 * percpu code to honor alignment and remove this ugliness.
1482 */
1483 ptr = __alloc_percpu(size + align + sizeof(void *), 1);
1484 cwqs = PTR_ALIGN(ptr, align);
1485 *(void **)per_cpu_ptr(cwqs + 1, 0) = ptr;
1486#else
1487 /* On SMP, percpu allocator can do it itself */
1488 cwqs = __alloc_percpu(size, align);
1489#endif
1490 /* just in case, make sure it's actually aligned */
1491 BUG_ON(!IS_ALIGNED((unsigned long)cwqs, align));
1492 return cwqs;
1493}
1494
1495static void free_cwqs(struct cpu_workqueue_struct *cwqs)
1496{
1497#ifndef CONFIG_SMP
1498 /* on UP, the pointer to free is stored right after the cwq */
1499 if (cwqs)
1500 free_percpu(*(void **)per_cpu_ptr(cwqs + 1, 0));
1501#else
1502 free_percpu(cwqs);
1503#endif
1504}
1505
Johannes Berg4e6045f2007-10-18 23:39:55 -07001506struct workqueue_struct *__create_workqueue_key(const char *name,
Tejun Heo97e37d72010-06-29 10:07:10 +02001507 unsigned int flags,
Johannes Bergeb13ba82008-01-16 09:51:58 +01001508 struct lock_class_key *key,
1509 const char *lock_name)
Oleg Nesterov3af244332007-05-09 02:34:09 -07001510{
Tejun Heo15376632010-06-29 10:07:11 +02001511 bool singlethread = flags & WQ_SINGLE_THREAD;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001512 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001513 bool failed = false;
1514 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001515
1516 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
1517 if (!wq)
Tejun Heo4690c4a2010-06-29 10:07:10 +02001518 goto err;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001519
Tejun Heo0f900042010-06-29 10:07:11 +02001520 wq->cpu_wq = alloc_cwqs();
Tejun Heo4690c4a2010-06-29 10:07:10 +02001521 if (!wq->cpu_wq)
1522 goto err;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001523
Tejun Heo97e37d72010-06-29 10:07:10 +02001524 wq->flags = flags;
Tejun Heo73f53c42010-06-29 10:07:11 +02001525 mutex_init(&wq->flush_mutex);
1526 atomic_set(&wq->nr_cwqs_to_flush, 0);
1527 INIT_LIST_HEAD(&wq->flusher_queue);
1528 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001529 wq->name = name;
Johannes Bergeb13ba82008-01-16 09:51:58 +01001530 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07001531 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001532
Tejun Heo15376632010-06-29 10:07:11 +02001533 cpu_maps_update_begin();
1534 /*
1535 * We must initialize cwqs for each possible cpu even if we
1536 * are going to call destroy_workqueue() finally. Otherwise
1537 * cpu_up() can hit the uninitialized cwq once we drop the
1538 * lock.
1539 */
1540 for_each_possible_cpu(cpu) {
1541 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
1542
Tejun Heo0f900042010-06-29 10:07:11 +02001543 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo15376632010-06-29 10:07:11 +02001544 cwq->cpu = cpu;
Tejun Heoc34056a2010-06-29 10:07:11 +02001545 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001546 cwq->flush_color = -1;
Tejun Heo15376632010-06-29 10:07:11 +02001547 spin_lock_init(&cwq->lock);
1548 INIT_LIST_HEAD(&cwq->worklist);
1549 init_waitqueue_head(&cwq->more_work);
1550
Tejun Heoc34056a2010-06-29 10:07:11 +02001551 if (failed)
Tejun Heo15376632010-06-29 10:07:11 +02001552 continue;
Tejun Heoc34056a2010-06-29 10:07:11 +02001553 cwq->worker = create_worker(cwq,
1554 cpu_online(cpu) && !singlethread);
1555 if (cwq->worker)
1556 start_worker(cwq->worker);
Tejun Heo15376632010-06-29 10:07:11 +02001557 else
Tejun Heoc34056a2010-06-29 10:07:11 +02001558 failed = true;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001559 }
1560
Tejun Heo15376632010-06-29 10:07:11 +02001561 spin_lock(&workqueue_lock);
1562 list_add(&wq->list, &workqueues);
1563 spin_unlock(&workqueue_lock);
1564
1565 cpu_maps_update_done();
1566
Tejun Heoc34056a2010-06-29 10:07:11 +02001567 if (failed) {
Oleg Nesterov3af244332007-05-09 02:34:09 -07001568 destroy_workqueue(wq);
1569 wq = NULL;
1570 }
1571 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02001572err:
1573 if (wq) {
Tejun Heo0f900042010-06-29 10:07:11 +02001574 free_cwqs(wq->cpu_wq);
Tejun Heo4690c4a2010-06-29 10:07:10 +02001575 kfree(wq);
1576 }
1577 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001578}
Johannes Berg4e6045f2007-10-18 23:39:55 -07001579EXPORT_SYMBOL_GPL(__create_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001580
Oleg Nesterov3af244332007-05-09 02:34:09 -07001581/**
1582 * destroy_workqueue - safely terminate a workqueue
1583 * @wq: target workqueue
1584 *
1585 * Safely destroy a workqueue. All work currently pending will be done first.
1586 */
1587void destroy_workqueue(struct workqueue_struct *wq)
1588{
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07001589 int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001590
Oleg Nesterov3da1c842008-07-25 01:47:50 -07001591 cpu_maps_update_begin();
Gautham R Shenoy95402b32008-01-25 21:08:02 +01001592 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07001593 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01001594 spin_unlock(&workqueue_lock);
Tejun Heo15376632010-06-29 10:07:11 +02001595 cpu_maps_update_done();
Oleg Nesterov3af244332007-05-09 02:34:09 -07001596
Tejun Heo73f53c42010-06-29 10:07:11 +02001597 flush_workqueue(wq);
1598
1599 for_each_possible_cpu(cpu) {
1600 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
1601 int i;
1602
Tejun Heoc34056a2010-06-29 10:07:11 +02001603 if (cwq->worker) {
1604 destroy_worker(cwq->worker);
1605 cwq->worker = NULL;
Tejun Heo73f53c42010-06-29 10:07:11 +02001606 }
1607
1608 for (i = 0; i < WORK_NR_COLORS; i++)
1609 BUG_ON(cwq->nr_in_flight[i]);
1610 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001611
Tejun Heo0f900042010-06-29 10:07:11 +02001612 free_cwqs(wq->cpu_wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001613 kfree(wq);
1614}
1615EXPORT_SYMBOL_GPL(destroy_workqueue);
1616
1617static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
1618 unsigned long action,
1619 void *hcpu)
1620{
1621 unsigned int cpu = (unsigned long)hcpu;
1622 struct cpu_workqueue_struct *cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 struct workqueue_struct *wq;
1624
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001625 action &= ~CPU_TASKS_FROZEN;
1626
Oleg Nesterov3af244332007-05-09 02:34:09 -07001627 list_for_each_entry(wq, &workqueues, list) {
Tejun Heo15376632010-06-29 10:07:11 +02001628 if (wq->flags & WQ_SINGLE_THREAD)
1629 continue;
1630
1631 cwq = get_cwq(cpu, wq);
Christoph Lameter89ada672005-10-30 15:01:59 -08001632
Oleg Nesterov3af244332007-05-09 02:34:09 -07001633 switch (action) {
Oleg Nesterov3da1c842008-07-25 01:47:50 -07001634 case CPU_POST_DEAD:
Tejun Heo73f53c42010-06-29 10:07:11 +02001635 flush_workqueue(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001636 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
1639
Tejun Heo15376632010-06-29 10:07:11 +02001640 return notifier_from_errno(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Rusty Russell2d3854a2008-11-05 13:39:10 +11001643#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08001644
Rusty Russell2d3854a2008-11-05 13:39:10 +11001645struct work_for_cpu {
Andrew Morton6b440032009-04-09 09:50:37 -06001646 struct completion completion;
Rusty Russell2d3854a2008-11-05 13:39:10 +11001647 long (*fn)(void *);
1648 void *arg;
1649 long ret;
1650};
1651
Andrew Morton6b440032009-04-09 09:50:37 -06001652static int do_work_for_cpu(void *_wfc)
Rusty Russell2d3854a2008-11-05 13:39:10 +11001653{
Andrew Morton6b440032009-04-09 09:50:37 -06001654 struct work_for_cpu *wfc = _wfc;
Rusty Russell2d3854a2008-11-05 13:39:10 +11001655 wfc->ret = wfc->fn(wfc->arg);
Andrew Morton6b440032009-04-09 09:50:37 -06001656 complete(&wfc->completion);
1657 return 0;
Rusty Russell2d3854a2008-11-05 13:39:10 +11001658}
1659
1660/**
1661 * work_on_cpu - run a function in user context on a particular cpu
1662 * @cpu: the cpu to run on
1663 * @fn: the function to run
1664 * @arg: the function arg
1665 *
Rusty Russell31ad9082009-01-16 15:31:15 -08001666 * This will return the value @fn returns.
1667 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06001668 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11001669 */
1670long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
1671{
Andrew Morton6b440032009-04-09 09:50:37 -06001672 struct task_struct *sub_thread;
1673 struct work_for_cpu wfc = {
1674 .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
1675 .fn = fn,
1676 .arg = arg,
1677 };
Rusty Russell2d3854a2008-11-05 13:39:10 +11001678
Andrew Morton6b440032009-04-09 09:50:37 -06001679 sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
1680 if (IS_ERR(sub_thread))
1681 return PTR_ERR(sub_thread);
1682 kthread_bind(sub_thread, cpu);
1683 wake_up_process(sub_thread);
1684 wait_for_completion(&wfc.completion);
Rusty Russell2d3854a2008-11-05 13:39:10 +11001685 return wfc.ret;
1686}
1687EXPORT_SYMBOL_GPL(work_on_cpu);
1688#endif /* CONFIG_SMP */
1689
Oleg Nesterovc12920d2007-05-09 02:34:14 -07001690void __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
Tejun Heoc34056a2010-06-29 10:07:11 +02001692 unsigned int cpu;
1693
1694 for_each_possible_cpu(cpu)
1695 ida_init(&per_cpu(worker_ida, cpu));
1696
Rusty Russelle7577c52009-01-01 10:12:25 +10301697 singlethread_cpu = cpumask_first(cpu_possible_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 hotcpu_notifier(workqueue_cpu_callback, 0);
1699 keventd_wq = create_workqueue("events");
1700 BUG_ON(!keventd_wq);
1701}