blob: 600db10a4dbf3857e18038461eda41b02fc0ddf3 [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 */
54 struct task_struct *task; /* I: worker task */
55 struct cpu_workqueue_struct *cwq; /* I: the associated cwq */
56 int id; /* I: worker id */
57};
58
Tejun Heo4690c4a2010-06-29 10:07:10 +020059/*
Nathan Lynchf756d5e2006-01-08 01:05:12 -080060 * The per-CPU workqueue (if single thread, we always use the first
Tejun Heo0f900042010-06-29 10:07:11 +020061 * possible cpu). The lower WORK_STRUCT_FLAG_BITS of
62 * work_struct->data are used for flags and thus cwqs need to be
63 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 */
65struct cpu_workqueue_struct {
66
67 spinlock_t lock;
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 struct list_head worklist;
70 wait_queue_head_t more_work;
Tejun Heo15376632010-06-29 10:07:11 +020071 unsigned int cpu;
Tejun Heoc34056a2010-06-29 10:07:11 +020072 struct worker *worker;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Tejun Heo4690c4a2010-06-29 10:07:10 +020074 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +020075 int work_color; /* L: current color */
76 int flush_color; /* L: flushing color */
77 int nr_in_flight[WORK_NR_COLORS];
78 /* L: nr of in_flight works */
Tejun Heo0f900042010-06-29 10:07:11 +020079};
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/*
Tejun Heo73f53c42010-06-29 10:07:11 +020082 * Structure used to wait for workqueue flush.
83 */
84struct wq_flusher {
85 struct list_head list; /* F: list of flushers */
86 int flush_color; /* F: flush color waiting for */
87 struct completion done; /* flush completion */
88};
89
90/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * The externally visible workqueue abstraction is an array of
92 * per-CPU workqueues:
93 */
94struct workqueue_struct {
Tejun Heo97e37d72010-06-29 10:07:10 +020095 unsigned int flags; /* I: WQ_* flags */
Tejun Heo4690c4a2010-06-29 10:07:10 +020096 struct cpu_workqueue_struct *cpu_wq; /* I: cwq's */
97 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +020098
99 struct mutex flush_mutex; /* protects wq flushing */
100 int work_color; /* F: current work color */
101 int flush_color; /* F: current flush color */
102 atomic_t nr_cwqs_to_flush; /* flush in progress */
103 struct wq_flusher *first_flusher; /* F: first flusher */
104 struct list_head flusher_queue; /* F: flush waiters */
105 struct list_head flusher_overflow; /* F: flush overflow list */
106
Tejun Heo4690c4a2010-06-29 10:07:10 +0200107 const char *name; /* I: workqueue name */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700108#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200109 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700110#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900113#ifdef CONFIG_DEBUG_OBJECTS_WORK
114
115static struct debug_obj_descr work_debug_descr;
116
117/*
118 * fixup_init is called when:
119 * - an active object is initialized
120 */
121static int work_fixup_init(void *addr, enum debug_obj_state state)
122{
123 struct work_struct *work = addr;
124
125 switch (state) {
126 case ODEBUG_STATE_ACTIVE:
127 cancel_work_sync(work);
128 debug_object_init(work, &work_debug_descr);
129 return 1;
130 default:
131 return 0;
132 }
133}
134
135/*
136 * fixup_activate is called when:
137 * - an active object is activated
138 * - an unknown object is activated (might be a statically initialized object)
139 */
140static int work_fixup_activate(void *addr, enum debug_obj_state state)
141{
142 struct work_struct *work = addr;
143
144 switch (state) {
145
146 case ODEBUG_STATE_NOTAVAILABLE:
147 /*
148 * This is not really a fixup. The work struct was
149 * statically initialized. We just make sure that it
150 * is tracked in the object tracker.
151 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200152 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900153 debug_object_init(work, &work_debug_descr);
154 debug_object_activate(work, &work_debug_descr);
155 return 0;
156 }
157 WARN_ON_ONCE(1);
158 return 0;
159
160 case ODEBUG_STATE_ACTIVE:
161 WARN_ON(1);
162
163 default:
164 return 0;
165 }
166}
167
168/*
169 * fixup_free is called when:
170 * - an active object is freed
171 */
172static int work_fixup_free(void *addr, enum debug_obj_state state)
173{
174 struct work_struct *work = addr;
175
176 switch (state) {
177 case ODEBUG_STATE_ACTIVE:
178 cancel_work_sync(work);
179 debug_object_free(work, &work_debug_descr);
180 return 1;
181 default:
182 return 0;
183 }
184}
185
186static struct debug_obj_descr work_debug_descr = {
187 .name = "work_struct",
188 .fixup_init = work_fixup_init,
189 .fixup_activate = work_fixup_activate,
190 .fixup_free = work_fixup_free,
191};
192
193static inline void debug_work_activate(struct work_struct *work)
194{
195 debug_object_activate(work, &work_debug_descr);
196}
197
198static inline void debug_work_deactivate(struct work_struct *work)
199{
200 debug_object_deactivate(work, &work_debug_descr);
201}
202
203void __init_work(struct work_struct *work, int onstack)
204{
205 if (onstack)
206 debug_object_init_on_stack(work, &work_debug_descr);
207 else
208 debug_object_init(work, &work_debug_descr);
209}
210EXPORT_SYMBOL_GPL(__init_work);
211
212void destroy_work_on_stack(struct work_struct *work)
213{
214 debug_object_free(work, &work_debug_descr);
215}
216EXPORT_SYMBOL_GPL(destroy_work_on_stack);
217
218#else
219static inline void debug_work_activate(struct work_struct *work) { }
220static inline void debug_work_deactivate(struct work_struct *work) { }
221#endif
222
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100223/* Serializes the accesses to the list of workqueues. */
224static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static LIST_HEAD(workqueues);
Tejun Heoc34056a2010-06-29 10:07:11 +0200226static DEFINE_PER_CPU(struct ida, worker_ida);
227
228static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Oleg Nesterov3af244332007-05-09 02:34:09 -0700230static int singlethread_cpu __read_mostly;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700231
Tejun Heo4690c4a2010-06-29 10:07:10 +0200232static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
233 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700234{
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700235 return per_cpu_ptr(wq->cpu_wq, cpu);
236}
237
Tejun Heo15376632010-06-29 10:07:11 +0200238static struct cpu_workqueue_struct *target_cwq(unsigned int cpu,
239 struct workqueue_struct *wq)
240{
241 if (unlikely(wq->flags & WQ_SINGLE_THREAD))
242 cpu = singlethread_cpu;
243 return get_cwq(cpu, wq);
244}
245
Tejun Heo73f53c42010-06-29 10:07:11 +0200246static unsigned int work_color_to_flags(int color)
247{
248 return color << WORK_STRUCT_COLOR_SHIFT;
249}
250
251static int get_work_color(struct work_struct *work)
252{
253 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
254 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
255}
256
257static int work_next_color(int color)
258{
259 return (color + 1) % WORK_NR_COLORS;
260}
261
David Howells4594bf12006-12-07 11:33:26 +0000262/*
263 * Set the workqueue on which a work item is to be run
264 * - Must *only* be called if the pending flag is set
265 */
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700266static inline void set_wq_data(struct work_struct *work,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200267 struct cpu_workqueue_struct *cwq,
268 unsigned long extra_flags)
David Howells365970a2006-11-22 14:54:49 +0000269{
David Howells4594bf12006-12-07 11:33:26 +0000270 BUG_ON(!work_pending(work));
271
Tejun Heo4690c4a2010-06-29 10:07:10 +0200272 atomic_long_set(&work->data, (unsigned long)cwq | work_static(work) |
Tejun Heo22df02b2010-06-29 10:07:10 +0200273 WORK_STRUCT_PENDING | extra_flags);
David Howells365970a2006-11-22 14:54:49 +0000274}
275
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200276/*
277 * Clear WORK_STRUCT_PENDING and the workqueue on which it was queued.
278 */
279static inline void clear_wq_data(struct work_struct *work)
280{
Tejun Heo4690c4a2010-06-29 10:07:10 +0200281 atomic_long_set(&work->data, work_static(work));
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200282}
283
Tejun Heo64166692010-06-29 10:07:11 +0200284static inline struct cpu_workqueue_struct *get_wq_data(struct work_struct *work)
David Howells365970a2006-11-22 14:54:49 +0000285{
Tejun Heo64166692010-06-29 10:07:11 +0200286 return (void *)(atomic_long_read(&work->data) &
287 WORK_STRUCT_WQ_DATA_MASK);
David Howells365970a2006-11-22 14:54:49 +0000288}
289
Tejun Heo4690c4a2010-06-29 10:07:10 +0200290/**
291 * insert_work - insert a work into cwq
292 * @cwq: cwq @work belongs to
293 * @work: work to insert
294 * @head: insertion point
295 * @extra_flags: extra WORK_STRUCT_* flags to set
296 *
297 * Insert @work into @cwq after @head.
298 *
299 * CONTEXT:
300 * spin_lock_irq(cwq->lock).
301 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700302static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200303 struct work_struct *work, struct list_head *head,
304 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700305{
Tejun Heo4690c4a2010-06-29 10:07:10 +0200306 /* we own @work, set data and link */
307 set_wq_data(work, cwq, extra_flags);
308
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700309 /*
310 * Ensure that we get the right work->data if we see the
311 * result of list_add() below, see try_to_grab_pending().
312 */
313 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +0200314
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700315 list_add_tail(&work->entry, head);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700316 wake_up(&cwq->more_work);
317}
318
Tejun Heo4690c4a2010-06-29 10:07:10 +0200319static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 struct work_struct *work)
321{
Tejun Heo15376632010-06-29 10:07:11 +0200322 struct cpu_workqueue_struct *cwq = target_cwq(cpu, wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 unsigned long flags;
324
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900325 debug_work_activate(work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 spin_lock_irqsave(&cwq->lock, flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +0200327 BUG_ON(!list_empty(&work->entry));
Tejun Heo73f53c42010-06-29 10:07:11 +0200328 cwq->nr_in_flight[cwq->work_color]++;
329 insert_work(cwq, work, &cwq->worklist,
330 work_color_to_flags(cwq->work_color));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 spin_unlock_irqrestore(&cwq->lock, flags);
332}
333
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700334/**
335 * queue_work - queue work on a workqueue
336 * @wq: workqueue to use
337 * @work: work to queue
338 *
Alan Stern057647f2006-10-28 10:38:58 -0700339 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -0700341 * We queue the work to the CPU on which it was submitted, but if the CPU dies
342 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800344int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Oleg Nesterovef1ca232008-07-25 01:47:53 -0700346 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Oleg Nesterovef1ca232008-07-25 01:47:53 -0700348 ret = queue_work_on(get_cpu(), wq, work);
349 put_cpu();
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return ret;
352}
Dave Jonesae90dd52006-06-30 01:40:45 -0400353EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Zhang Ruic1a220e2008-07-23 21:28:39 -0700355/**
356 * queue_work_on - queue work on specific cpu
357 * @cpu: CPU number to execute work on
358 * @wq: workqueue to use
359 * @work: work to queue
360 *
361 * Returns 0 if @work was already on a queue, non-zero otherwise.
362 *
363 * We queue the work to a specific CPU, the caller must ensure it
364 * can't go away.
365 */
366int
367queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
368{
369 int ret = 0;
370
Tejun Heo22df02b2010-06-29 10:07:10 +0200371 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +0200372 __queue_work(cpu, wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -0700373 ret = 1;
374 }
375 return ret;
376}
377EXPORT_SYMBOL_GPL(queue_work_on);
378
Li Zefan6d141c32008-02-08 04:21:09 -0800379static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
David Howells52bad642006-11-22 14:54:01 +0000381 struct delayed_work *dwork = (struct delayed_work *)__data;
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700382 struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Tejun Heo4690c4a2010-06-29 10:07:10 +0200384 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700387/**
388 * queue_delayed_work - queue work on a workqueue after delay
389 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800390 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700391 * @delay: number of jiffies to wait before queueing
392 *
Alan Stern057647f2006-10-28 10:38:58 -0700393 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700394 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800395int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000396 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
David Howells52bad642006-11-22 14:54:01 +0000398 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -0700399 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Oleg Nesterov63bc0362007-05-09 02:34:16 -0700401 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
Dave Jonesae90dd52006-06-30 01:40:45 -0400403EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700405/**
406 * queue_delayed_work_on - queue work on specific CPU after delay
407 * @cpu: CPU number to execute work on
408 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800409 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700410 * @delay: number of jiffies to wait before queueing
411 *
Alan Stern057647f2006-10-28 10:38:58 -0700412 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700413 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700414int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000415 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700416{
417 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +0000418 struct timer_list *timer = &dwork->timer;
419 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700420
Tejun Heo22df02b2010-06-29 10:07:10 +0200421 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700422 BUG_ON(timer_pending(timer));
423 BUG_ON(!list_empty(&work->entry));
424
Andrew Liu8a3e77c2008-05-01 04:35:14 -0700425 timer_stats_timer_set_start_info(&dwork->timer);
426
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700427 /* This stores cwq for the moment, for the timer_fn */
Tejun Heo15376632010-06-29 10:07:11 +0200428 set_wq_data(work, target_cwq(raw_smp_processor_id(), wq), 0);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700429 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +0000430 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700431 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -0700432
433 if (unlikely(cpu >= 0))
434 add_timer_on(timer, cpu);
435 else
436 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700437 ret = 1;
438 }
439 return ret;
440}
Dave Jonesae90dd52006-06-30 01:40:45 -0400441EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Tejun Heoc34056a2010-06-29 10:07:11 +0200443static struct worker *alloc_worker(void)
444{
445 struct worker *worker;
446
447 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
448 return worker;
449}
450
451/**
452 * create_worker - create a new workqueue worker
453 * @cwq: cwq the new worker will belong to
454 * @bind: whether to set affinity to @cpu or not
455 *
456 * Create a new worker which is bound to @cwq. The returned worker
457 * can be started by calling start_worker() or destroyed using
458 * destroy_worker().
459 *
460 * CONTEXT:
461 * Might sleep. Does GFP_KERNEL allocations.
462 *
463 * RETURNS:
464 * Pointer to the newly created worker.
465 */
466static struct worker *create_worker(struct cpu_workqueue_struct *cwq, bool bind)
467{
468 int id = -1;
469 struct worker *worker = NULL;
470
471 spin_lock(&workqueue_lock);
472 while (ida_get_new(&per_cpu(worker_ida, cwq->cpu), &id)) {
473 spin_unlock(&workqueue_lock);
474 if (!ida_pre_get(&per_cpu(worker_ida, cwq->cpu), GFP_KERNEL))
475 goto fail;
476 spin_lock(&workqueue_lock);
477 }
478 spin_unlock(&workqueue_lock);
479
480 worker = alloc_worker();
481 if (!worker)
482 goto fail;
483
484 worker->cwq = cwq;
485 worker->id = id;
486
487 worker->task = kthread_create(worker_thread, worker, "kworker/%u:%d",
488 cwq->cpu, id);
489 if (IS_ERR(worker->task))
490 goto fail;
491
492 if (bind)
493 kthread_bind(worker->task, cwq->cpu);
494
495 return worker;
496fail:
497 if (id >= 0) {
498 spin_lock(&workqueue_lock);
499 ida_remove(&per_cpu(worker_ida, cwq->cpu), id);
500 spin_unlock(&workqueue_lock);
501 }
502 kfree(worker);
503 return NULL;
504}
505
506/**
507 * start_worker - start a newly created worker
508 * @worker: worker to start
509 *
510 * Start @worker.
511 *
512 * CONTEXT:
513 * spin_lock_irq(cwq->lock).
514 */
515static void start_worker(struct worker *worker)
516{
517 wake_up_process(worker->task);
518}
519
520/**
521 * destroy_worker - destroy a workqueue worker
522 * @worker: worker to be destroyed
523 *
524 * Destroy @worker.
525 */
526static void destroy_worker(struct worker *worker)
527{
528 int cpu = worker->cwq->cpu;
529 int id = worker->id;
530
531 /* sanity check frenzy */
532 BUG_ON(worker->current_work);
533
534 kthread_stop(worker->task);
535 kfree(worker);
536
537 spin_lock(&workqueue_lock);
538 ida_remove(&per_cpu(worker_ida, cpu), id);
539 spin_unlock(&workqueue_lock);
540}
541
Tejun Heoa62428c2010-06-29 10:07:10 +0200542/**
Tejun Heo73f53c42010-06-29 10:07:11 +0200543 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
544 * @cwq: cwq of interest
545 * @color: color of work which left the queue
546 *
547 * A work either has completed or is removed from pending queue,
548 * decrement nr_in_flight of its cwq and handle workqueue flushing.
549 *
550 * CONTEXT:
551 * spin_lock_irq(cwq->lock).
552 */
553static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color)
554{
555 /* ignore uncolored works */
556 if (color == WORK_NO_COLOR)
557 return;
558
559 cwq->nr_in_flight[color]--;
560
561 /* is flush in progress and are we at the flushing tip? */
562 if (likely(cwq->flush_color != color))
563 return;
564
565 /* are there still in-flight works? */
566 if (cwq->nr_in_flight[color])
567 return;
568
569 /* this cwq is done, clear flush_color */
570 cwq->flush_color = -1;
571
572 /*
573 * If this was the last cwq, wake up the first flusher. It
574 * will handle the rest.
575 */
576 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
577 complete(&cwq->wq->first_flusher->done);
578}
579
580/**
Tejun Heoa62428c2010-06-29 10:07:10 +0200581 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +0200582 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +0200583 * @work: work to process
584 *
585 * Process @work. This function contains all the logics necessary to
586 * process a single work including synchronization against and
587 * interaction with other workers on the same cpu, queueing and
588 * flushing. As long as context requirement is met, any worker can
589 * call this function to process a work.
590 *
591 * CONTEXT:
592 * spin_lock_irq(cwq->lock) which is released and regrabbed.
593 */
Tejun Heoc34056a2010-06-29 10:07:11 +0200594static void process_one_work(struct worker *worker, struct work_struct *work)
Tejun Heoa62428c2010-06-29 10:07:10 +0200595{
Tejun Heoc34056a2010-06-29 10:07:11 +0200596 struct cpu_workqueue_struct *cwq = worker->cwq;
Tejun Heoa62428c2010-06-29 10:07:10 +0200597 work_func_t f = work->func;
Tejun Heo73f53c42010-06-29 10:07:11 +0200598 int work_color;
Tejun Heoa62428c2010-06-29 10:07:10 +0200599#ifdef CONFIG_LOCKDEP
600 /*
601 * It is permissible to free the struct work_struct from
602 * inside the function that is called from it, this we need to
603 * take into account for lockdep too. To avoid bogus "held
604 * lock freed" warnings as well as problems when looking into
605 * work->lockdep_map, make a copy and use that here.
606 */
607 struct lockdep_map lockdep_map = work->lockdep_map;
608#endif
609 /* claim and process */
Tejun Heoa62428c2010-06-29 10:07:10 +0200610 debug_work_deactivate(work);
Tejun Heoc34056a2010-06-29 10:07:11 +0200611 worker->current_work = work;
Tejun Heo73f53c42010-06-29 10:07:11 +0200612 work_color = get_work_color(work);
Tejun Heoa62428c2010-06-29 10:07:10 +0200613 list_del_init(&work->entry);
614
615 spin_unlock_irq(&cwq->lock);
616
617 BUG_ON(get_wq_data(work) != cwq);
618 work_clear_pending(work);
619 lock_map_acquire(&cwq->wq->lockdep_map);
620 lock_map_acquire(&lockdep_map);
621 f(work);
622 lock_map_release(&lockdep_map);
623 lock_map_release(&cwq->wq->lockdep_map);
624
625 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
626 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
627 "%s/0x%08x/%d\n",
628 current->comm, preempt_count(), task_pid_nr(current));
629 printk(KERN_ERR " last function: ");
630 print_symbol("%s\n", (unsigned long)f);
631 debug_show_held_locks(current);
632 dump_stack();
633 }
634
635 spin_lock_irq(&cwq->lock);
636
637 /* we're done with it, release */
Tejun Heoc34056a2010-06-29 10:07:11 +0200638 worker->current_work = NULL;
Tejun Heo73f53c42010-06-29 10:07:11 +0200639 cwq_dec_nr_in_flight(cwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +0200640}
641
Tejun Heoc34056a2010-06-29 10:07:11 +0200642static void run_workqueue(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Tejun Heoc34056a2010-06-29 10:07:11 +0200644 struct cpu_workqueue_struct *cwq = worker->cwq;
645
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700646 spin_lock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 while (!list_empty(&cwq->worklist)) {
648 struct work_struct *work = list_entry(cwq->worklist.next,
649 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +0200650 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700652 spin_unlock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
Tejun Heo4690c4a2010-06-29 10:07:10 +0200655/**
656 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +0200657 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +0200658 *
659 * The cwq worker thread function.
660 */
Tejun Heoc34056a2010-06-29 10:07:11 +0200661static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Tejun Heoc34056a2010-06-29 10:07:11 +0200663 struct worker *worker = __worker;
664 struct cpu_workqueue_struct *cwq = worker->cwq;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700665 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Tejun Heo97e37d72010-06-29 10:07:10 +0200667 if (cwq->wq->flags & WQ_FREEZEABLE)
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700668 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Oleg Nesterov3af244332007-05-09 02:34:09 -0700670 for (;;) {
Oleg Nesterov3af244332007-05-09 02:34:09 -0700671 prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
Oleg Nesterov14441962007-05-23 13:57:57 -0700672 if (!freezing(current) &&
673 !kthread_should_stop() &&
674 list_empty(&cwq->worklist))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 schedule();
Oleg Nesterov3af244332007-05-09 02:34:09 -0700676 finish_wait(&cwq->more_work, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Oleg Nesterov85f41862007-05-09 02:34:20 -0700678 try_to_freeze();
679
Oleg Nesterov14441962007-05-23 13:57:57 -0700680 if (kthread_should_stop())
Oleg Nesterov3af244332007-05-09 02:34:09 -0700681 break;
682
Tejun Heoc34056a2010-06-29 10:07:11 +0200683 if (unlikely(!cpumask_equal(&worker->task->cpus_allowed,
Tejun Heo15376632010-06-29 10:07:11 +0200684 get_cpu_mask(cwq->cpu))))
Tejun Heoc34056a2010-06-29 10:07:11 +0200685 set_cpus_allowed_ptr(worker->task,
Tejun Heo15376632010-06-29 10:07:11 +0200686 get_cpu_mask(cwq->cpu));
Tejun Heoc34056a2010-06-29 10:07:11 +0200687 run_workqueue(worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
Oleg Nesterov3af244332007-05-09 02:34:09 -0700689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return 0;
691}
692
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700693struct wq_barrier {
694 struct work_struct work;
695 struct completion done;
696};
697
698static void wq_barrier_func(struct work_struct *work)
699{
700 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
701 complete(&barr->done);
702}
703
Tejun Heo4690c4a2010-06-29 10:07:10 +0200704/**
705 * insert_wq_barrier - insert a barrier work
706 * @cwq: cwq to insert barrier into
707 * @barr: wq_barrier to insert
708 * @head: insertion point
709 *
710 * Insert barrier @barr into @cwq before @head.
711 *
712 * CONTEXT:
713 * spin_lock_irq(cwq->lock).
714 */
Oleg Nesterov83c22522007-05-09 02:33:54 -0700715static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700716 struct wq_barrier *barr, struct list_head *head)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700717{
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900718 /*
719 * debugobject calls are safe here even with cwq->lock locked
720 * as we know for sure that this will not trigger any of the
721 * checks and call back into the fixup functions where we
722 * might deadlock.
723 */
724 INIT_WORK_ON_STACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +0200725 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700726 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -0700727
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900728 debug_work_activate(&barr->work);
Tejun Heo73f53c42010-06-29 10:07:11 +0200729 insert_work(cwq, &barr->work, head, work_color_to_flags(WORK_NO_COLOR));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700730}
731
Tejun Heo73f53c42010-06-29 10:07:11 +0200732/**
733 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
734 * @wq: workqueue being flushed
735 * @flush_color: new flush color, < 0 for no-op
736 * @work_color: new work color, < 0 for no-op
737 *
738 * Prepare cwqs for workqueue flushing.
739 *
740 * If @flush_color is non-negative, flush_color on all cwqs should be
741 * -1. If no cwq has in-flight commands at the specified color, all
742 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
743 * has in flight commands, its cwq->flush_color is set to
744 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
745 * wakeup logic is armed and %true is returned.
746 *
747 * The caller should have initialized @wq->first_flusher prior to
748 * calling this function with non-negative @flush_color. If
749 * @flush_color is negative, no flush color update is done and %false
750 * is returned.
751 *
752 * If @work_color is non-negative, all cwqs should have the same
753 * work_color which is previous to @work_color and all will be
754 * advanced to @work_color.
755 *
756 * CONTEXT:
757 * mutex_lock(wq->flush_mutex).
758 *
759 * RETURNS:
760 * %true if @flush_color >= 0 and there's something to flush. %false
761 * otherwise.
762 */
763static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
764 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Tejun Heo73f53c42010-06-29 10:07:11 +0200766 bool wait = false;
767 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -0700768
Tejun Heo73f53c42010-06-29 10:07:11 +0200769 if (flush_color >= 0) {
770 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
771 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900772 }
Oleg Nesterov14441962007-05-23 13:57:57 -0700773
Tejun Heo73f53c42010-06-29 10:07:11 +0200774 for_each_possible_cpu(cpu) {
775 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
776
777 spin_lock_irq(&cwq->lock);
778
779 if (flush_color >= 0) {
780 BUG_ON(cwq->flush_color != -1);
781
782 if (cwq->nr_in_flight[flush_color]) {
783 cwq->flush_color = flush_color;
784 atomic_inc(&wq->nr_cwqs_to_flush);
785 wait = true;
786 }
787 }
788
789 if (work_color >= 0) {
790 BUG_ON(work_color != work_next_color(cwq->work_color));
791 cwq->work_color = work_color;
792 }
793
794 spin_unlock_irq(&cwq->lock);
795 }
796
797 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
798 complete(&wq->first_flusher->done);
799
800 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700803/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700805 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 *
807 * Forces execution of the workqueue and blocks until its completion.
808 * This is typically used in driver shutdown handlers.
809 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700810 * We sleep until all works which were queued on entry have been handled,
811 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800813void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Tejun Heo73f53c42010-06-29 10:07:11 +0200815 struct wq_flusher this_flusher = {
816 .list = LIST_HEAD_INIT(this_flusher.list),
817 .flush_color = -1,
818 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
819 };
820 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700821
Ingo Molnar3295f0e2008-08-11 10:30:30 +0200822 lock_map_acquire(&wq->lockdep_map);
823 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +0200824
825 mutex_lock(&wq->flush_mutex);
826
827 /*
828 * Start-to-wait phase
829 */
830 next_color = work_next_color(wq->work_color);
831
832 if (next_color != wq->flush_color) {
833 /*
834 * Color space is not full. The current work_color
835 * becomes our flush_color and work_color is advanced
836 * by one.
837 */
838 BUG_ON(!list_empty(&wq->flusher_overflow));
839 this_flusher.flush_color = wq->work_color;
840 wq->work_color = next_color;
841
842 if (!wq->first_flusher) {
843 /* no flush in progress, become the first flusher */
844 BUG_ON(wq->flush_color != this_flusher.flush_color);
845
846 wq->first_flusher = &this_flusher;
847
848 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
849 wq->work_color)) {
850 /* nothing to flush, done */
851 wq->flush_color = next_color;
852 wq->first_flusher = NULL;
853 goto out_unlock;
854 }
855 } else {
856 /* wait in queue */
857 BUG_ON(wq->flush_color == this_flusher.flush_color);
858 list_add_tail(&this_flusher.list, &wq->flusher_queue);
859 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
860 }
861 } else {
862 /*
863 * Oops, color space is full, wait on overflow queue.
864 * The next flush completion will assign us
865 * flush_color and transfer to flusher_queue.
866 */
867 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
868 }
869
870 mutex_unlock(&wq->flush_mutex);
871
872 wait_for_completion(&this_flusher.done);
873
874 /*
875 * Wake-up-and-cascade phase
876 *
877 * First flushers are responsible for cascading flushes and
878 * handling overflow. Non-first flushers can simply return.
879 */
880 if (wq->first_flusher != &this_flusher)
881 return;
882
883 mutex_lock(&wq->flush_mutex);
884
885 wq->first_flusher = NULL;
886
887 BUG_ON(!list_empty(&this_flusher.list));
888 BUG_ON(wq->flush_color != this_flusher.flush_color);
889
890 while (true) {
891 struct wq_flusher *next, *tmp;
892
893 /* complete all the flushers sharing the current flush color */
894 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
895 if (next->flush_color != wq->flush_color)
896 break;
897 list_del_init(&next->list);
898 complete(&next->done);
899 }
900
901 BUG_ON(!list_empty(&wq->flusher_overflow) &&
902 wq->flush_color != work_next_color(wq->work_color));
903
904 /* this flush_color is finished, advance by one */
905 wq->flush_color = work_next_color(wq->flush_color);
906
907 /* one color has been freed, handle overflow queue */
908 if (!list_empty(&wq->flusher_overflow)) {
909 /*
910 * Assign the same color to all overflowed
911 * flushers, advance work_color and append to
912 * flusher_queue. This is the start-to-wait
913 * phase for these overflowed flushers.
914 */
915 list_for_each_entry(tmp, &wq->flusher_overflow, list)
916 tmp->flush_color = wq->work_color;
917
918 wq->work_color = work_next_color(wq->work_color);
919
920 list_splice_tail_init(&wq->flusher_overflow,
921 &wq->flusher_queue);
922 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
923 }
924
925 if (list_empty(&wq->flusher_queue)) {
926 BUG_ON(wq->flush_color != wq->work_color);
927 break;
928 }
929
930 /*
931 * Need to flush more colors. Make the next flusher
932 * the new first flusher and arm cwqs.
933 */
934 BUG_ON(wq->flush_color == wq->work_color);
935 BUG_ON(wq->flush_color != next->flush_color);
936
937 list_del_init(&next->list);
938 wq->first_flusher = next;
939
940 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
941 break;
942
943 /*
944 * Meh... this color is already done, clear first
945 * flusher and repeat cascading.
946 */
947 wq->first_flusher = NULL;
948 }
949
950out_unlock:
951 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
Dave Jonesae90dd52006-06-30 01:40:45 -0400953EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Oleg Nesterovdb700892008-07-25 01:47:49 -0700955/**
956 * flush_work - block until a work_struct's callback has terminated
957 * @work: the work which is to be flushed
958 *
Oleg Nesterova67da702008-07-25 01:47:52 -0700959 * Returns false if @work has already terminated.
960 *
Oleg Nesterovdb700892008-07-25 01:47:49 -0700961 * It is expected that, prior to calling flush_work(), the caller has
962 * arranged for the work to not be requeued, otherwise it doesn't make
963 * sense to use this function.
964 */
965int flush_work(struct work_struct *work)
966{
967 struct cpu_workqueue_struct *cwq;
968 struct list_head *prev;
969 struct wq_barrier barr;
970
971 might_sleep();
972 cwq = get_wq_data(work);
973 if (!cwq)
974 return 0;
975
Ingo Molnar3295f0e2008-08-11 10:30:30 +0200976 lock_map_acquire(&cwq->wq->lockdep_map);
977 lock_map_release(&cwq->wq->lockdep_map);
Oleg Nesterova67da702008-07-25 01:47:52 -0700978
Oleg Nesterovdb700892008-07-25 01:47:49 -0700979 spin_lock_irq(&cwq->lock);
980 if (!list_empty(&work->entry)) {
981 /*
982 * See the comment near try_to_grab_pending()->smp_rmb().
983 * If it was re-queued under us we are not going to wait.
984 */
985 smp_rmb();
986 if (unlikely(cwq != get_wq_data(work)))
Tejun Heo4690c4a2010-06-29 10:07:10 +0200987 goto already_gone;
Oleg Nesterovdb700892008-07-25 01:47:49 -0700988 prev = &work->entry;
989 } else {
Tejun Heoc34056a2010-06-29 10:07:11 +0200990 if (!cwq->worker || cwq->worker->current_work != work)
Tejun Heo4690c4a2010-06-29 10:07:10 +0200991 goto already_gone;
Oleg Nesterovdb700892008-07-25 01:47:49 -0700992 prev = &cwq->worklist;
993 }
994 insert_wq_barrier(cwq, &barr, prev->next);
Oleg Nesterovdb700892008-07-25 01:47:49 -0700995
Tejun Heo4690c4a2010-06-29 10:07:10 +0200996 spin_unlock_irq(&cwq->lock);
Oleg Nesterovdb700892008-07-25 01:47:49 -0700997 wait_for_completion(&barr.done);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900998 destroy_work_on_stack(&barr.work);
Oleg Nesterovdb700892008-07-25 01:47:49 -0700999 return 1;
Tejun Heo4690c4a2010-06-29 10:07:10 +02001000already_gone:
1001 spin_unlock_irq(&cwq->lock);
1002 return 0;
Oleg Nesterovdb700892008-07-25 01:47:49 -07001003}
1004EXPORT_SYMBOL_GPL(flush_work);
1005
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001006/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001007 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001008 * so this work can't be re-armed in any way.
1009 */
1010static int try_to_grab_pending(struct work_struct *work)
1011{
1012 struct cpu_workqueue_struct *cwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001013 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001014
Tejun Heo22df02b2010-06-29 10:07:10 +02001015 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001016 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001017
1018 /*
1019 * The queueing is in progress, or it is already queued. Try to
1020 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1021 */
1022
1023 cwq = get_wq_data(work);
1024 if (!cwq)
1025 return ret;
1026
1027 spin_lock_irq(&cwq->lock);
1028 if (!list_empty(&work->entry)) {
1029 /*
1030 * This work is queued, but perhaps we locked the wrong cwq.
1031 * In that case we must see the new value after rmb(), see
1032 * insert_work()->wmb().
1033 */
1034 smp_rmb();
1035 if (cwq == get_wq_data(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001036 debug_work_deactivate(work);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001037 list_del_init(&work->entry);
Tejun Heo73f53c42010-06-29 10:07:11 +02001038 cwq_dec_nr_in_flight(cwq, get_work_color(work));
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001039 ret = 1;
1040 }
1041 }
1042 spin_unlock_irq(&cwq->lock);
1043
1044 return ret;
1045}
1046
1047static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq,
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001048 struct work_struct *work)
1049{
1050 struct wq_barrier barr;
1051 int running = 0;
1052
1053 spin_lock_irq(&cwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001054 if (unlikely(cwq->worker && cwq->worker->current_work == work)) {
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001055 insert_wq_barrier(cwq, &barr, cwq->worklist.next);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001056 running = 1;
1057 }
1058 spin_unlock_irq(&cwq->lock);
1059
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001060 if (unlikely(running)) {
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001061 wait_for_completion(&barr.done);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001062 destroy_work_on_stack(&barr.work);
1063 }
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001064}
1065
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001066static void wait_on_work(struct work_struct *work)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001067{
1068 struct cpu_workqueue_struct *cwq;
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07001069 struct workqueue_struct *wq;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07001070 int cpu;
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001071
Oleg Nesterovf293ea92007-05-09 02:34:10 -07001072 might_sleep();
1073
Ingo Molnar3295f0e2008-08-11 10:30:30 +02001074 lock_map_acquire(&work->lockdep_map);
1075 lock_map_release(&work->lockdep_map);
Johannes Berg4e6045f2007-10-18 23:39:55 -07001076
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001077 cwq = get_wq_data(work);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001078 if (!cwq)
Oleg Nesterov3af244332007-05-09 02:34:09 -07001079 return;
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001080
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07001081 wq = cwq->wq;
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07001082
Tejun Heo15376632010-06-29 10:07:11 +02001083 for_each_possible_cpu(cpu)
Tejun Heo4690c4a2010-06-29 10:07:10 +02001084 wait_on_cpu_work(get_cwq(cpu, wq), work);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001085}
1086
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001087static int __cancel_work_timer(struct work_struct *work,
1088 struct timer_list* timer)
1089{
1090 int ret;
1091
1092 do {
1093 ret = (timer && likely(del_timer(timer)));
1094 if (!ret)
1095 ret = try_to_grab_pending(work);
1096 wait_on_work(work);
1097 } while (unlikely(ret < 0));
1098
Oleg Nesterov4d707b92010-04-23 17:40:40 +02001099 clear_wq_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001100 return ret;
1101}
1102
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001103/**
1104 * cancel_work_sync - block until a work_struct's callback has terminated
1105 * @work: the work which is to be flushed
1106 *
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001107 * Returns true if @work was pending.
1108 *
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001109 * cancel_work_sync() will cancel the work if it is queued. If the work's
1110 * callback appears to be running, cancel_work_sync() will block until it
1111 * has completed.
1112 *
1113 * It is possible to use this function if the work re-queues itself. It can
1114 * cancel the work even if it migrates to another workqueue, however in that
1115 * case it only guarantees that work->func() has completed on the last queued
1116 * workqueue.
1117 *
1118 * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
1119 * pending, otherwise it goes into a busy-wait loop until the timer expires.
1120 *
1121 * The caller must ensure that workqueue_struct on which this work was last
1122 * queued can't be destroyed before this function returns.
1123 */
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001124int cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001125{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001126 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001127}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07001128EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001129
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001130/**
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07001131 * cancel_delayed_work_sync - reliably kill off a delayed work.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001132 * @dwork: the delayed work struct
1133 *
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001134 * Returns true if @dwork was pending.
1135 *
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001136 * It is possible to use this function if @dwork rearms itself via queue_work()
1137 * or queue_delayed_work(). See also the comment for cancel_work_sync().
1138 */
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001139int cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001140{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001141 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001142}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07001143EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001145static struct workqueue_struct *keventd_wq __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001147/**
1148 * schedule_work - put work task in global workqueue
1149 * @work: job to be done
1150 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02001151 * Returns zero if @work was already on the kernel-global workqueue and
1152 * non-zero otherwise.
1153 *
1154 * This puts a job in the kernel-global workqueue if it was not already
1155 * queued and leaves it in the same position on the kernel-global
1156 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001157 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001158int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
1160 return queue_work(keventd_wq, work);
1161}
Dave Jonesae90dd52006-06-30 01:40:45 -04001162EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Zhang Ruic1a220e2008-07-23 21:28:39 -07001164/*
1165 * schedule_work_on - put work task on a specific cpu
1166 * @cpu: cpu to put the work task on
1167 * @work: job to be done
1168 *
1169 * This puts a job on a specific cpu
1170 */
1171int schedule_work_on(int cpu, struct work_struct *work)
1172{
1173 return queue_work_on(cpu, keventd_wq, work);
1174}
1175EXPORT_SYMBOL(schedule_work_on);
1176
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001177/**
1178 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +00001179 * @dwork: job to be done
1180 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001181 *
1182 * After waiting for a given time this puts a job in the kernel-global
1183 * workqueue.
1184 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001185int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001186 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
David Howells52bad642006-11-22 14:54:01 +00001188 return queue_delayed_work(keventd_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
Dave Jonesae90dd52006-06-30 01:40:45 -04001190EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001192/**
Linus Torvalds8c53e462009-10-14 09:16:42 -07001193 * flush_delayed_work - block until a dwork_struct's callback has terminated
1194 * @dwork: the delayed work which is to be flushed
1195 *
1196 * Any timeout is cancelled, and any pending work is run immediately.
1197 */
1198void flush_delayed_work(struct delayed_work *dwork)
1199{
1200 if (del_timer_sync(&dwork->timer)) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001201 __queue_work(get_cpu(), get_wq_data(&dwork->work)->wq,
1202 &dwork->work);
Linus Torvalds8c53e462009-10-14 09:16:42 -07001203 put_cpu();
1204 }
1205 flush_work(&dwork->work);
1206}
1207EXPORT_SYMBOL(flush_delayed_work);
1208
1209/**
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001210 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
1211 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00001212 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001213 * @delay: number of jiffies to wait
1214 *
1215 * After waiting for a given time this puts a job in the kernel-global
1216 * workqueue on the specified CPU.
1217 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +00001219 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
David Howells52bad642006-11-22 14:54:01 +00001221 return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
Dave Jonesae90dd52006-06-30 01:40:45 -04001223EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Andrew Mortonb6136772006-06-25 05:47:49 -07001225/**
1226 * schedule_on_each_cpu - call a function on each online CPU from keventd
1227 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07001228 *
1229 * Returns zero on success.
1230 * Returns -ve errno on failure.
1231 *
Andrew Mortonb6136772006-06-25 05:47:49 -07001232 * schedule_on_each_cpu() is very slow.
1233 */
David Howells65f27f32006-11-22 14:55:48 +00001234int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08001235{
1236 int cpu;
Andi Kleen65a64462009-10-14 06:22:47 +02001237 int orig = -1;
Andrew Mortonb6136772006-06-25 05:47:49 -07001238 struct work_struct *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08001239
Andrew Mortonb6136772006-06-25 05:47:49 -07001240 works = alloc_percpu(struct work_struct);
1241 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08001242 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07001243
Gautham R Shenoy95402b32008-01-25 21:08:02 +01001244 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08001245
1246 /*
1247 * When running in keventd don't schedule a work item on
1248 * itself. Can just call directly because the work queue is
1249 * already bound. This also is faster.
1250 */
1251 if (current_is_keventd())
1252 orig = raw_smp_processor_id();
1253
Christoph Lameter15316ba2006-01-08 01:00:43 -08001254 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01001255 struct work_struct *work = per_cpu_ptr(works, cpu);
1256
1257 INIT_WORK(work, func);
Andi Kleen65a64462009-10-14 06:22:47 +02001258 if (cpu != orig)
Tejun Heo93981802009-11-17 14:06:20 -08001259 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02001260 }
Tejun Heo93981802009-11-17 14:06:20 -08001261 if (orig >= 0)
1262 func(per_cpu_ptr(works, orig));
1263
1264 for_each_online_cpu(cpu)
1265 flush_work(per_cpu_ptr(works, cpu));
1266
Gautham R Shenoy95402b32008-01-25 21:08:02 +01001267 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07001268 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08001269 return 0;
1270}
1271
Alan Sterneef6a7d2010-02-12 17:39:21 +09001272/**
1273 * flush_scheduled_work - ensure that any scheduled work has run to completion.
1274 *
1275 * Forces execution of the kernel-global workqueue and blocks until its
1276 * completion.
1277 *
1278 * Think twice before calling this function! It's very easy to get into
1279 * trouble if you don't take great care. Either of the following situations
1280 * will lead to deadlock:
1281 *
1282 * One of the work items currently on the workqueue needs to acquire
1283 * a lock held by your code or its caller.
1284 *
1285 * Your code is running in the context of a work routine.
1286 *
1287 * They will be detected by lockdep when they occur, but the first might not
1288 * occur very often. It depends on what work items are on the workqueue and
1289 * what locks they need, which you have no control over.
1290 *
1291 * In most situations flushing the entire workqueue is overkill; you merely
1292 * need to know that a particular work item isn't queued and isn't running.
1293 * In such cases you should use cancel_delayed_work_sync() or
1294 * cancel_work_sync() instead.
1295 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296void flush_scheduled_work(void)
1297{
1298 flush_workqueue(keventd_wq);
1299}
Dave Jonesae90dd52006-06-30 01:40:45 -04001300EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06001303 * execute_in_process_context - reliably execute the routine with user context
1304 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06001305 * @ew: guaranteed storage for the execute work structure (must
1306 * be available when the work executes)
1307 *
1308 * Executes the function immediately if process context is available,
1309 * otherwise schedules the function for delayed execution.
1310 *
1311 * Returns: 0 - function was executed
1312 * 1 - function was scheduled for execution
1313 */
David Howells65f27f32006-11-22 14:55:48 +00001314int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06001315{
1316 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00001317 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06001318 return 0;
1319 }
1320
David Howells65f27f32006-11-22 14:55:48 +00001321 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06001322 schedule_work(&ew->work);
1323
1324 return 1;
1325}
1326EXPORT_SYMBOL_GPL(execute_in_process_context);
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328int keventd_up(void)
1329{
1330 return keventd_wq != NULL;
1331}
1332
1333int current_is_keventd(void)
1334{
1335 struct cpu_workqueue_struct *cwq;
Hugh Dickinsd2437692007-08-27 16:06:19 +01001336 int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 int ret = 0;
1338
1339 BUG_ON(!keventd_wq);
1340
Tejun Heo15376632010-06-29 10:07:11 +02001341 cwq = get_cwq(cpu, keventd_wq);
Tejun Heoc34056a2010-06-29 10:07:11 +02001342 if (current == cwq->worker->task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 ret = 1;
1344
1345 return ret;
1346
1347}
1348
Tejun Heo0f900042010-06-29 10:07:11 +02001349static struct cpu_workqueue_struct *alloc_cwqs(void)
1350{
1351 /*
1352 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
1353 * Make sure that the alignment isn't lower than that of
1354 * unsigned long long.
1355 */
1356 const size_t size = sizeof(struct cpu_workqueue_struct);
1357 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
1358 __alignof__(unsigned long long));
1359 struct cpu_workqueue_struct *cwqs;
1360#ifndef CONFIG_SMP
1361 void *ptr;
1362
1363 /*
1364 * On UP, percpu allocator doesn't honor alignment parameter
1365 * and simply uses arch-dependent default. Allocate enough
1366 * room to align cwq and put an extra pointer at the end
1367 * pointing back to the originally allocated pointer which
1368 * will be used for free.
1369 *
1370 * FIXME: This really belongs to UP percpu code. Update UP
1371 * percpu code to honor alignment and remove this ugliness.
1372 */
1373 ptr = __alloc_percpu(size + align + sizeof(void *), 1);
1374 cwqs = PTR_ALIGN(ptr, align);
1375 *(void **)per_cpu_ptr(cwqs + 1, 0) = ptr;
1376#else
1377 /* On SMP, percpu allocator can do it itself */
1378 cwqs = __alloc_percpu(size, align);
1379#endif
1380 /* just in case, make sure it's actually aligned */
1381 BUG_ON(!IS_ALIGNED((unsigned long)cwqs, align));
1382 return cwqs;
1383}
1384
1385static void free_cwqs(struct cpu_workqueue_struct *cwqs)
1386{
1387#ifndef CONFIG_SMP
1388 /* on UP, the pointer to free is stored right after the cwq */
1389 if (cwqs)
1390 free_percpu(*(void **)per_cpu_ptr(cwqs + 1, 0));
1391#else
1392 free_percpu(cwqs);
1393#endif
1394}
1395
Johannes Berg4e6045f2007-10-18 23:39:55 -07001396struct workqueue_struct *__create_workqueue_key(const char *name,
Tejun Heo97e37d72010-06-29 10:07:10 +02001397 unsigned int flags,
Johannes Bergeb13ba82008-01-16 09:51:58 +01001398 struct lock_class_key *key,
1399 const char *lock_name)
Oleg Nesterov3af244332007-05-09 02:34:09 -07001400{
Tejun Heo15376632010-06-29 10:07:11 +02001401 bool singlethread = flags & WQ_SINGLE_THREAD;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001402 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001403 bool failed = false;
1404 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001405
1406 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
1407 if (!wq)
Tejun Heo4690c4a2010-06-29 10:07:10 +02001408 goto err;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001409
Tejun Heo0f900042010-06-29 10:07:11 +02001410 wq->cpu_wq = alloc_cwqs();
Tejun Heo4690c4a2010-06-29 10:07:10 +02001411 if (!wq->cpu_wq)
1412 goto err;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001413
Tejun Heo97e37d72010-06-29 10:07:10 +02001414 wq->flags = flags;
Tejun Heo73f53c42010-06-29 10:07:11 +02001415 mutex_init(&wq->flush_mutex);
1416 atomic_set(&wq->nr_cwqs_to_flush, 0);
1417 INIT_LIST_HEAD(&wq->flusher_queue);
1418 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001419 wq->name = name;
Johannes Bergeb13ba82008-01-16 09:51:58 +01001420 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07001421 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001422
Tejun Heo15376632010-06-29 10:07:11 +02001423 cpu_maps_update_begin();
1424 /*
1425 * We must initialize cwqs for each possible cpu even if we
1426 * are going to call destroy_workqueue() finally. Otherwise
1427 * cpu_up() can hit the uninitialized cwq once we drop the
1428 * lock.
1429 */
1430 for_each_possible_cpu(cpu) {
1431 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
1432
Tejun Heo0f900042010-06-29 10:07:11 +02001433 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo15376632010-06-29 10:07:11 +02001434 cwq->cpu = cpu;
Tejun Heoc34056a2010-06-29 10:07:11 +02001435 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001436 cwq->flush_color = -1;
Tejun Heo15376632010-06-29 10:07:11 +02001437 spin_lock_init(&cwq->lock);
1438 INIT_LIST_HEAD(&cwq->worklist);
1439 init_waitqueue_head(&cwq->more_work);
1440
Tejun Heoc34056a2010-06-29 10:07:11 +02001441 if (failed)
Tejun Heo15376632010-06-29 10:07:11 +02001442 continue;
Tejun Heoc34056a2010-06-29 10:07:11 +02001443 cwq->worker = create_worker(cwq,
1444 cpu_online(cpu) && !singlethread);
1445 if (cwq->worker)
1446 start_worker(cwq->worker);
Tejun Heo15376632010-06-29 10:07:11 +02001447 else
Tejun Heoc34056a2010-06-29 10:07:11 +02001448 failed = true;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001449 }
1450
Tejun Heo15376632010-06-29 10:07:11 +02001451 spin_lock(&workqueue_lock);
1452 list_add(&wq->list, &workqueues);
1453 spin_unlock(&workqueue_lock);
1454
1455 cpu_maps_update_done();
1456
Tejun Heoc34056a2010-06-29 10:07:11 +02001457 if (failed) {
Oleg Nesterov3af244332007-05-09 02:34:09 -07001458 destroy_workqueue(wq);
1459 wq = NULL;
1460 }
1461 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02001462err:
1463 if (wq) {
Tejun Heo0f900042010-06-29 10:07:11 +02001464 free_cwqs(wq->cpu_wq);
Tejun Heo4690c4a2010-06-29 10:07:10 +02001465 kfree(wq);
1466 }
1467 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001468}
Johannes Berg4e6045f2007-10-18 23:39:55 -07001469EXPORT_SYMBOL_GPL(__create_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001470
Oleg Nesterov3af244332007-05-09 02:34:09 -07001471/**
1472 * destroy_workqueue - safely terminate a workqueue
1473 * @wq: target workqueue
1474 *
1475 * Safely destroy a workqueue. All work currently pending will be done first.
1476 */
1477void destroy_workqueue(struct workqueue_struct *wq)
1478{
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07001479 int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001480
Oleg Nesterov3da1c842008-07-25 01:47:50 -07001481 cpu_maps_update_begin();
Gautham R Shenoy95402b32008-01-25 21:08:02 +01001482 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07001483 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01001484 spin_unlock(&workqueue_lock);
Tejun Heo15376632010-06-29 10:07:11 +02001485 cpu_maps_update_done();
Oleg Nesterov3af244332007-05-09 02:34:09 -07001486
Tejun Heo73f53c42010-06-29 10:07:11 +02001487 flush_workqueue(wq);
1488
1489 for_each_possible_cpu(cpu) {
1490 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
1491 int i;
1492
Tejun Heoc34056a2010-06-29 10:07:11 +02001493 if (cwq->worker) {
1494 destroy_worker(cwq->worker);
1495 cwq->worker = NULL;
Tejun Heo73f53c42010-06-29 10:07:11 +02001496 }
1497
1498 for (i = 0; i < WORK_NR_COLORS; i++)
1499 BUG_ON(cwq->nr_in_flight[i]);
1500 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001501
Tejun Heo0f900042010-06-29 10:07:11 +02001502 free_cwqs(wq->cpu_wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001503 kfree(wq);
1504}
1505EXPORT_SYMBOL_GPL(destroy_workqueue);
1506
1507static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
1508 unsigned long action,
1509 void *hcpu)
1510{
1511 unsigned int cpu = (unsigned long)hcpu;
1512 struct cpu_workqueue_struct *cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 struct workqueue_struct *wq;
1514
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001515 action &= ~CPU_TASKS_FROZEN;
1516
Oleg Nesterov3af244332007-05-09 02:34:09 -07001517 list_for_each_entry(wq, &workqueues, list) {
Tejun Heo15376632010-06-29 10:07:11 +02001518 if (wq->flags & WQ_SINGLE_THREAD)
1519 continue;
1520
1521 cwq = get_cwq(cpu, wq);
Christoph Lameter89ada672005-10-30 15:01:59 -08001522
Oleg Nesterov3af244332007-05-09 02:34:09 -07001523 switch (action) {
Oleg Nesterov3da1c842008-07-25 01:47:50 -07001524 case CPU_POST_DEAD:
Tejun Heo73f53c42010-06-29 10:07:11 +02001525 flush_workqueue(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001526 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 }
1529
Tejun Heo15376632010-06-29 10:07:11 +02001530 return notifier_from_errno(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Rusty Russell2d3854a2008-11-05 13:39:10 +11001533#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08001534
Rusty Russell2d3854a2008-11-05 13:39:10 +11001535struct work_for_cpu {
Andrew Morton6b440032009-04-09 09:50:37 -06001536 struct completion completion;
Rusty Russell2d3854a2008-11-05 13:39:10 +11001537 long (*fn)(void *);
1538 void *arg;
1539 long ret;
1540};
1541
Andrew Morton6b440032009-04-09 09:50:37 -06001542static int do_work_for_cpu(void *_wfc)
Rusty Russell2d3854a2008-11-05 13:39:10 +11001543{
Andrew Morton6b440032009-04-09 09:50:37 -06001544 struct work_for_cpu *wfc = _wfc;
Rusty Russell2d3854a2008-11-05 13:39:10 +11001545 wfc->ret = wfc->fn(wfc->arg);
Andrew Morton6b440032009-04-09 09:50:37 -06001546 complete(&wfc->completion);
1547 return 0;
Rusty Russell2d3854a2008-11-05 13:39:10 +11001548}
1549
1550/**
1551 * work_on_cpu - run a function in user context on a particular cpu
1552 * @cpu: the cpu to run on
1553 * @fn: the function to run
1554 * @arg: the function arg
1555 *
Rusty Russell31ad9082009-01-16 15:31:15 -08001556 * This will return the value @fn returns.
1557 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06001558 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11001559 */
1560long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
1561{
Andrew Morton6b440032009-04-09 09:50:37 -06001562 struct task_struct *sub_thread;
1563 struct work_for_cpu wfc = {
1564 .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
1565 .fn = fn,
1566 .arg = arg,
1567 };
Rusty Russell2d3854a2008-11-05 13:39:10 +11001568
Andrew Morton6b440032009-04-09 09:50:37 -06001569 sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
1570 if (IS_ERR(sub_thread))
1571 return PTR_ERR(sub_thread);
1572 kthread_bind(sub_thread, cpu);
1573 wake_up_process(sub_thread);
1574 wait_for_completion(&wfc.completion);
Rusty Russell2d3854a2008-11-05 13:39:10 +11001575 return wfc.ret;
1576}
1577EXPORT_SYMBOL_GPL(work_on_cpu);
1578#endif /* CONFIG_SMP */
1579
Oleg Nesterovc12920d2007-05-09 02:34:14 -07001580void __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
Tejun Heoc34056a2010-06-29 10:07:11 +02001582 unsigned int cpu;
1583
1584 for_each_possible_cpu(cpu)
1585 ida_init(&per_cpu(worker_ida, cpu));
1586
Rusty Russelle7577c52009-01-01 10:12:25 +10301587 singlethread_cpu = cpumask_first(cpu_possible_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 hotcpu_notifier(workqueue_cpu_callback, 0);
1589 keventd_wq = create_workqueue("events");
1590 BUG_ON(!keventd_wq);
1591}