diff options
Diffstat (limited to 'kernel/workqueue.c')
-rw-r--r-- | kernel/workqueue.c | 406 |
1 files changed, 263 insertions, 143 deletions
diff --git a/kernel/workqueue.c b/kernel/workqueue.c index 2994a0e3a61c..e785b0f2aea5 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c | |||
@@ -1,19 +1,26 @@ | |||
1 | /* | 1 | /* |
2 | * linux/kernel/workqueue.c | 2 | * kernel/workqueue.c - generic async execution with shared worker pool |
3 | * | 3 | * |
4 | * Generic mechanism for defining kernel helper threads for running | 4 | * Copyright (C) 2002 Ingo Molnar |
5 | * arbitrary tasks in process context. | ||
6 | * | 5 | * |
7 | * Started by Ingo Molnar, Copyright (C) 2002 | 6 | * Derived from the taskqueue/keventd code by: |
7 | * David Woodhouse <dwmw2@infradead.org> | ||
8 | * Andrew Morton | ||
9 | * Kai Petzke <wpp@marie.physik.tu-berlin.de> | ||
10 | * Theodore Ts'o <tytso@mit.edu> | ||
8 | * | 11 | * |
9 | * Derived from the taskqueue/keventd code by: | 12 | * Made to use alloc_percpu by Christoph Lameter. |
10 | * | 13 | * |
11 | * David Woodhouse <dwmw2@infradead.org> | 14 | * Copyright (C) 2010 SUSE Linux Products GmbH |
12 | * Andrew Morton | 15 | * Copyright (C) 2010 Tejun Heo <tj@kernel.org> |
13 | * Kai Petzke <wpp@marie.physik.tu-berlin.de> | ||
14 | * Theodore Ts'o <tytso@mit.edu> | ||
15 | * | 16 | * |
16 | * Made to use alloc_percpu by Christoph Lameter. | 17 | * This is the generic async execution mechanism. Work items as are |
18 | * executed in process context. The worker pool is shared and | ||
19 | * automatically managed. There is one worker pool for each CPU and | ||
20 | * one extra for works which are better served by workers which are | ||
21 | * not bound to any specific CPU. | ||
22 | * | ||
23 | * Please read Documentation/workqueue.txt for details. | ||
17 | */ | 24 | */ |
18 | 25 | ||
19 | #include <linux/module.h> | 26 | #include <linux/module.h> |
@@ -87,7 +94,8 @@ enum { | |||
87 | /* | 94 | /* |
88 | * Structure fields follow one of the following exclusion rules. | 95 | * Structure fields follow one of the following exclusion rules. |
89 | * | 96 | * |
90 | * I: Set during initialization and read-only afterwards. | 97 | * I: Modifiable by initialization/destruction paths and read-only for |
98 | * everyone else. | ||
91 | * | 99 | * |
92 | * P: Preemption protected. Disabling preemption is enough and should | 100 | * P: Preemption protected. Disabling preemption is enough and should |
93 | * only be modified and accessed from the local cpu. | 101 | * only be modified and accessed from the local cpu. |
@@ -195,7 +203,7 @@ typedef cpumask_var_t mayday_mask_t; | |||
195 | cpumask_test_and_set_cpu((cpu), (mask)) | 203 | cpumask_test_and_set_cpu((cpu), (mask)) |
196 | #define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask)) | 204 | #define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask)) |
197 | #define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask)) | 205 | #define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask)) |
198 | #define alloc_mayday_mask(maskp, gfp) alloc_cpumask_var((maskp), (gfp)) | 206 | #define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp)) |
199 | #define free_mayday_mask(mask) free_cpumask_var((mask)) | 207 | #define free_mayday_mask(mask) free_cpumask_var((mask)) |
200 | #else | 208 | #else |
201 | typedef unsigned long mayday_mask_t; | 209 | typedef unsigned long mayday_mask_t; |
@@ -246,6 +254,9 @@ EXPORT_SYMBOL_GPL(system_long_wq); | |||
246 | EXPORT_SYMBOL_GPL(system_nrt_wq); | 254 | EXPORT_SYMBOL_GPL(system_nrt_wq); |
247 | EXPORT_SYMBOL_GPL(system_unbound_wq); | 255 | EXPORT_SYMBOL_GPL(system_unbound_wq); |
248 | 256 | ||
257 | #define CREATE_TRACE_POINTS | ||
258 | #include <trace/events/workqueue.h> | ||
259 | |||
249 | #define for_each_busy_worker(worker, i, pos, gcwq) \ | 260 | #define for_each_busy_worker(worker, i, pos, gcwq) \ |
250 | for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \ | 261 | for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \ |
251 | hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry) | 262 | hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry) |
@@ -299,21 +310,6 @@ static inline int __next_wq_cpu(int cpu, const struct cpumask *mask, | |||
299 | (cpu) < WORK_CPU_NONE; \ | 310 | (cpu) < WORK_CPU_NONE; \ |
300 | (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq))) | 311 | (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq))) |
301 | 312 | ||
302 | #ifdef CONFIG_LOCKDEP | ||
303 | /** | ||
304 | * in_workqueue_context() - in context of specified workqueue? | ||
305 | * @wq: the workqueue of interest | ||
306 | * | ||
307 | * Checks lockdep state to see if the current task is executing from | ||
308 | * within a workqueue item. This function exists only if lockdep is | ||
309 | * enabled. | ||
310 | */ | ||
311 | int in_workqueue_context(struct workqueue_struct *wq) | ||
312 | { | ||
313 | return lock_is_held(&wq->lockdep_map); | ||
314 | } | ||
315 | #endif | ||
316 | |||
317 | #ifdef CONFIG_DEBUG_OBJECTS_WORK | 313 | #ifdef CONFIG_DEBUG_OBJECTS_WORK |
318 | 314 | ||
319 | static struct debug_obj_descr work_debug_descr; | 315 | static struct debug_obj_descr work_debug_descr; |
@@ -593,7 +589,9 @@ static bool keep_working(struct global_cwq *gcwq) | |||
593 | { | 589 | { |
594 | atomic_t *nr_running = get_gcwq_nr_running(gcwq->cpu); | 590 | atomic_t *nr_running = get_gcwq_nr_running(gcwq->cpu); |
595 | 591 | ||
596 | return !list_empty(&gcwq->worklist) && atomic_read(nr_running) <= 1; | 592 | return !list_empty(&gcwq->worklist) && |
593 | (atomic_read(nr_running) <= 1 || | ||
594 | gcwq->flags & GCWQ_HIGHPRI_PENDING); | ||
597 | } | 595 | } |
598 | 596 | ||
599 | /* Do we need a new worker? Called from manager. */ | 597 | /* Do we need a new worker? Called from manager. */ |
@@ -663,7 +661,7 @@ void wq_worker_waking_up(struct task_struct *task, unsigned int cpu) | |||
663 | { | 661 | { |
664 | struct worker *worker = kthread_data(task); | 662 | struct worker *worker = kthread_data(task); |
665 | 663 | ||
666 | if (likely(!(worker->flags & WORKER_NOT_RUNNING))) | 664 | if (!(worker->flags & WORKER_NOT_RUNNING)) |
667 | atomic_inc(get_gcwq_nr_running(cpu)); | 665 | atomic_inc(get_gcwq_nr_running(cpu)); |
668 | } | 666 | } |
669 | 667 | ||
@@ -689,7 +687,7 @@ struct task_struct *wq_worker_sleeping(struct task_struct *task, | |||
689 | struct global_cwq *gcwq = get_gcwq(cpu); | 687 | struct global_cwq *gcwq = get_gcwq(cpu); |
690 | atomic_t *nr_running = get_gcwq_nr_running(cpu); | 688 | atomic_t *nr_running = get_gcwq_nr_running(cpu); |
691 | 689 | ||
692 | if (unlikely(worker->flags & WORKER_NOT_RUNNING)) | 690 | if (worker->flags & WORKER_NOT_RUNNING) |
693 | return NULL; | 691 | return NULL; |
694 | 692 | ||
695 | /* this can only happen on the local cpu */ | 693 | /* this can only happen on the local cpu */ |
@@ -940,10 +938,14 @@ static void __queue_work(unsigned int cpu, struct workqueue_struct *wq, | |||
940 | struct global_cwq *gcwq; | 938 | struct global_cwq *gcwq; |
941 | struct cpu_workqueue_struct *cwq; | 939 | struct cpu_workqueue_struct *cwq; |
942 | struct list_head *worklist; | 940 | struct list_head *worklist; |
941 | unsigned int work_flags; | ||
943 | unsigned long flags; | 942 | unsigned long flags; |
944 | 943 | ||
945 | debug_work_activate(work); | 944 | debug_work_activate(work); |
946 | 945 | ||
946 | if (WARN_ON_ONCE(wq->flags & WQ_DYING)) | ||
947 | return; | ||
948 | |||
947 | /* determine gcwq to use */ | 949 | /* determine gcwq to use */ |
948 | if (!(wq->flags & WQ_UNBOUND)) { | 950 | if (!(wq->flags & WQ_UNBOUND)) { |
949 | struct global_cwq *last_gcwq; | 951 | struct global_cwq *last_gcwq; |
@@ -982,18 +984,23 @@ static void __queue_work(unsigned int cpu, struct workqueue_struct *wq, | |||
982 | 984 | ||
983 | /* gcwq determined, get cwq and queue */ | 985 | /* gcwq determined, get cwq and queue */ |
984 | cwq = get_cwq(gcwq->cpu, wq); | 986 | cwq = get_cwq(gcwq->cpu, wq); |
987 | trace_workqueue_queue_work(cpu, cwq, work); | ||
985 | 988 | ||
986 | BUG_ON(!list_empty(&work->entry)); | 989 | BUG_ON(!list_empty(&work->entry)); |
987 | 990 | ||
988 | cwq->nr_in_flight[cwq->work_color]++; | 991 | cwq->nr_in_flight[cwq->work_color]++; |
992 | work_flags = work_color_to_flags(cwq->work_color); | ||
989 | 993 | ||
990 | if (likely(cwq->nr_active < cwq->max_active)) { | 994 | if (likely(cwq->nr_active < cwq->max_active)) { |
995 | trace_workqueue_activate_work(work); | ||
991 | cwq->nr_active++; | 996 | cwq->nr_active++; |
992 | worklist = gcwq_determine_ins_pos(gcwq, cwq); | 997 | worklist = gcwq_determine_ins_pos(gcwq, cwq); |
993 | } else | 998 | } else { |
999 | work_flags |= WORK_STRUCT_DELAYED; | ||
994 | worklist = &cwq->delayed_works; | 1000 | worklist = &cwq->delayed_works; |
1001 | } | ||
995 | 1002 | ||
996 | insert_work(cwq, work, worklist, work_color_to_flags(cwq->work_color)); | 1003 | insert_work(cwq, work, worklist, work_flags); |
997 | 1004 | ||
998 | spin_unlock_irqrestore(&gcwq->lock, flags); | 1005 | spin_unlock_irqrestore(&gcwq->lock, flags); |
999 | } | 1006 | } |
@@ -1212,6 +1219,7 @@ static void worker_leave_idle(struct worker *worker) | |||
1212 | * bound), %false if offline. | 1219 | * bound), %false if offline. |
1213 | */ | 1220 | */ |
1214 | static bool worker_maybe_bind_and_lock(struct worker *worker) | 1221 | static bool worker_maybe_bind_and_lock(struct worker *worker) |
1222 | __acquires(&gcwq->lock) | ||
1215 | { | 1223 | { |
1216 | struct global_cwq *gcwq = worker->gcwq; | 1224 | struct global_cwq *gcwq = worker->gcwq; |
1217 | struct task_struct *task = worker->task; | 1225 | struct task_struct *task = worker->task; |
@@ -1485,6 +1493,8 @@ static void gcwq_mayday_timeout(unsigned long __gcwq) | |||
1485 | * otherwise. | 1493 | * otherwise. |
1486 | */ | 1494 | */ |
1487 | static bool maybe_create_worker(struct global_cwq *gcwq) | 1495 | static bool maybe_create_worker(struct global_cwq *gcwq) |
1496 | __releases(&gcwq->lock) | ||
1497 | __acquires(&gcwq->lock) | ||
1488 | { | 1498 | { |
1489 | if (!need_to_create_worker(gcwq)) | 1499 | if (!need_to_create_worker(gcwq)) |
1490 | return false; | 1500 | return false; |
@@ -1658,7 +1668,9 @@ static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq) | |||
1658 | struct work_struct, entry); | 1668 | struct work_struct, entry); |
1659 | struct list_head *pos = gcwq_determine_ins_pos(cwq->gcwq, cwq); | 1669 | struct list_head *pos = gcwq_determine_ins_pos(cwq->gcwq, cwq); |
1660 | 1670 | ||
1671 | trace_workqueue_activate_work(work); | ||
1661 | move_linked_works(work, pos, NULL); | 1672 | move_linked_works(work, pos, NULL); |
1673 | __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work)); | ||
1662 | cwq->nr_active++; | 1674 | cwq->nr_active++; |
1663 | } | 1675 | } |
1664 | 1676 | ||
@@ -1666,6 +1678,7 @@ static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq) | |||
1666 | * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight | 1678 | * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight |
1667 | * @cwq: cwq of interest | 1679 | * @cwq: cwq of interest |
1668 | * @color: color of work which left the queue | 1680 | * @color: color of work which left the queue |
1681 | * @delayed: for a delayed work | ||
1669 | * | 1682 | * |
1670 | * A work either has completed or is removed from pending queue, | 1683 | * A work either has completed or is removed from pending queue, |
1671 | * decrement nr_in_flight of its cwq and handle workqueue flushing. | 1684 | * decrement nr_in_flight of its cwq and handle workqueue flushing. |
@@ -1673,19 +1686,22 @@ static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq) | |||
1673 | * CONTEXT: | 1686 | * CONTEXT: |
1674 | * spin_lock_irq(gcwq->lock). | 1687 | * spin_lock_irq(gcwq->lock). |
1675 | */ | 1688 | */ |
1676 | static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color) | 1689 | static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color, |
1690 | bool delayed) | ||
1677 | { | 1691 | { |
1678 | /* ignore uncolored works */ | 1692 | /* ignore uncolored works */ |
1679 | if (color == WORK_NO_COLOR) | 1693 | if (color == WORK_NO_COLOR) |
1680 | return; | 1694 | return; |
1681 | 1695 | ||
1682 | cwq->nr_in_flight[color]--; | 1696 | cwq->nr_in_flight[color]--; |
1683 | cwq->nr_active--; | ||
1684 | 1697 | ||
1685 | if (!list_empty(&cwq->delayed_works)) { | 1698 | if (!delayed) { |
1686 | /* one down, submit a delayed one */ | 1699 | cwq->nr_active--; |
1687 | if (cwq->nr_active < cwq->max_active) | 1700 | if (!list_empty(&cwq->delayed_works)) { |
1688 | cwq_activate_first_delayed(cwq); | 1701 | /* one down, submit a delayed one */ |
1702 | if (cwq->nr_active < cwq->max_active) | ||
1703 | cwq_activate_first_delayed(cwq); | ||
1704 | } | ||
1689 | } | 1705 | } |
1690 | 1706 | ||
1691 | /* is flush in progress and are we at the flushing tip? */ | 1707 | /* is flush in progress and are we at the flushing tip? */ |
@@ -1722,6 +1738,8 @@ static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color) | |||
1722 | * spin_lock_irq(gcwq->lock) which is released and regrabbed. | 1738 | * spin_lock_irq(gcwq->lock) which is released and regrabbed. |
1723 | */ | 1739 | */ |
1724 | static void process_one_work(struct worker *worker, struct work_struct *work) | 1740 | static void process_one_work(struct worker *worker, struct work_struct *work) |
1741 | __releases(&gcwq->lock) | ||
1742 | __acquires(&gcwq->lock) | ||
1725 | { | 1743 | { |
1726 | struct cpu_workqueue_struct *cwq = get_work_cwq(work); | 1744 | struct cpu_workqueue_struct *cwq = get_work_cwq(work); |
1727 | struct global_cwq *gcwq = cwq->gcwq; | 1745 | struct global_cwq *gcwq = cwq->gcwq; |
@@ -1790,7 +1808,13 @@ static void process_one_work(struct worker *worker, struct work_struct *work) | |||
1790 | work_clear_pending(work); | 1808 | work_clear_pending(work); |
1791 | lock_map_acquire(&cwq->wq->lockdep_map); | 1809 | lock_map_acquire(&cwq->wq->lockdep_map); |
1792 | lock_map_acquire(&lockdep_map); | 1810 | lock_map_acquire(&lockdep_map); |
1811 | trace_workqueue_execute_start(work); | ||
1793 | f(work); | 1812 | f(work); |
1813 | /* | ||
1814 | * While we must be careful to not use "work" after this, the trace | ||
1815 | * point will only record its address. | ||
1816 | */ | ||
1817 | trace_workqueue_execute_end(work); | ||
1794 | lock_map_release(&lockdep_map); | 1818 | lock_map_release(&lockdep_map); |
1795 | lock_map_release(&cwq->wq->lockdep_map); | 1819 | lock_map_release(&cwq->wq->lockdep_map); |
1796 | 1820 | ||
@@ -1814,7 +1838,7 @@ static void process_one_work(struct worker *worker, struct work_struct *work) | |||
1814 | hlist_del_init(&worker->hentry); | 1838 | hlist_del_init(&worker->hentry); |
1815 | worker->current_work = NULL; | 1839 | worker->current_work = NULL; |
1816 | worker->current_cwq = NULL; | 1840 | worker->current_cwq = NULL; |
1817 | cwq_dec_nr_in_flight(cwq, work_color); | 1841 | cwq_dec_nr_in_flight(cwq, work_color, false); |
1818 | } | 1842 | } |
1819 | 1843 | ||
1820 | /** | 1844 | /** |
@@ -2040,7 +2064,7 @@ static void insert_wq_barrier(struct cpu_workqueue_struct *cwq, | |||
2040 | * checks and call back into the fixup functions where we | 2064 | * checks and call back into the fixup functions where we |
2041 | * might deadlock. | 2065 | * might deadlock. |
2042 | */ | 2066 | */ |
2043 | INIT_WORK_ON_STACK(&barr->work, wq_barrier_func); | 2067 | INIT_WORK_ONSTACK(&barr->work, wq_barrier_func); |
2044 | __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); | 2068 | __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); |
2045 | init_completion(&barr->done); | 2069 | init_completion(&barr->done); |
2046 | 2070 | ||
@@ -2292,27 +2316,17 @@ out_unlock: | |||
2292 | } | 2316 | } |
2293 | EXPORT_SYMBOL_GPL(flush_workqueue); | 2317 | EXPORT_SYMBOL_GPL(flush_workqueue); |
2294 | 2318 | ||
2295 | /** | 2319 | static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, |
2296 | * flush_work - block until a work_struct's callback has terminated | 2320 | bool wait_executing) |
2297 | * @work: the work which is to be flushed | ||
2298 | * | ||
2299 | * Returns false if @work has already terminated. | ||
2300 | * | ||
2301 | * It is expected that, prior to calling flush_work(), the caller has | ||
2302 | * arranged for the work to not be requeued, otherwise it doesn't make | ||
2303 | * sense to use this function. | ||
2304 | */ | ||
2305 | int flush_work(struct work_struct *work) | ||
2306 | { | 2321 | { |
2307 | struct worker *worker = NULL; | 2322 | struct worker *worker = NULL; |
2308 | struct global_cwq *gcwq; | 2323 | struct global_cwq *gcwq; |
2309 | struct cpu_workqueue_struct *cwq; | 2324 | struct cpu_workqueue_struct *cwq; |
2310 | struct wq_barrier barr; | ||
2311 | 2325 | ||
2312 | might_sleep(); | 2326 | might_sleep(); |
2313 | gcwq = get_work_gcwq(work); | 2327 | gcwq = get_work_gcwq(work); |
2314 | if (!gcwq) | 2328 | if (!gcwq) |
2315 | return 0; | 2329 | return false; |
2316 | 2330 | ||
2317 | spin_lock_irq(&gcwq->lock); | 2331 | spin_lock_irq(&gcwq->lock); |
2318 | if (!list_empty(&work->entry)) { | 2332 | if (!list_empty(&work->entry)) { |
@@ -2325,28 +2339,127 @@ int flush_work(struct work_struct *work) | |||
2325 | cwq = get_work_cwq(work); | 2339 | cwq = get_work_cwq(work); |
2326 | if (unlikely(!cwq || gcwq != cwq->gcwq)) | 2340 | if (unlikely(!cwq || gcwq != cwq->gcwq)) |
2327 | goto already_gone; | 2341 | goto already_gone; |
2328 | } else { | 2342 | } else if (wait_executing) { |
2329 | worker = find_worker_executing_work(gcwq, work); | 2343 | worker = find_worker_executing_work(gcwq, work); |
2330 | if (!worker) | 2344 | if (!worker) |
2331 | goto already_gone; | 2345 | goto already_gone; |
2332 | cwq = worker->current_cwq; | 2346 | cwq = worker->current_cwq; |
2333 | } | 2347 | } else |
2348 | goto already_gone; | ||
2334 | 2349 | ||
2335 | insert_wq_barrier(cwq, &barr, work, worker); | 2350 | insert_wq_barrier(cwq, barr, work, worker); |
2336 | spin_unlock_irq(&gcwq->lock); | 2351 | spin_unlock_irq(&gcwq->lock); |
2337 | 2352 | ||
2338 | lock_map_acquire(&cwq->wq->lockdep_map); | 2353 | lock_map_acquire(&cwq->wq->lockdep_map); |
2339 | lock_map_release(&cwq->wq->lockdep_map); | 2354 | lock_map_release(&cwq->wq->lockdep_map); |
2340 | 2355 | return true; | |
2341 | wait_for_completion(&barr.done); | ||
2342 | destroy_work_on_stack(&barr.work); | ||
2343 | return 1; | ||
2344 | already_gone: | 2356 | already_gone: |
2345 | spin_unlock_irq(&gcwq->lock); | 2357 | spin_unlock_irq(&gcwq->lock); |
2346 | return 0; | 2358 | return false; |
2359 | } | ||
2360 | |||
2361 | /** | ||
2362 | * flush_work - wait for a work to finish executing the last queueing instance | ||
2363 | * @work: the work to flush | ||
2364 | * | ||
2365 | * Wait until @work has finished execution. This function considers | ||
2366 | * only the last queueing instance of @work. If @work has been | ||
2367 | * enqueued across different CPUs on a non-reentrant workqueue or on | ||
2368 | * multiple workqueues, @work might still be executing on return on | ||
2369 | * some of the CPUs from earlier queueing. | ||
2370 | * | ||
2371 | * If @work was queued only on a non-reentrant, ordered or unbound | ||
2372 | * workqueue, @work is guaranteed to be idle on return if it hasn't | ||
2373 | * been requeued since flush started. | ||
2374 | * | ||
2375 | * RETURNS: | ||
2376 | * %true if flush_work() waited for the work to finish execution, | ||
2377 | * %false if it was already idle. | ||
2378 | */ | ||
2379 | bool flush_work(struct work_struct *work) | ||
2380 | { | ||
2381 | struct wq_barrier barr; | ||
2382 | |||
2383 | if (start_flush_work(work, &barr, true)) { | ||
2384 | wait_for_completion(&barr.done); | ||
2385 | destroy_work_on_stack(&barr.work); | ||
2386 | return true; | ||
2387 | } else | ||
2388 | return false; | ||
2347 | } | 2389 | } |
2348 | EXPORT_SYMBOL_GPL(flush_work); | 2390 | EXPORT_SYMBOL_GPL(flush_work); |
2349 | 2391 | ||
2392 | static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work) | ||
2393 | { | ||
2394 | struct wq_barrier barr; | ||
2395 | struct worker *worker; | ||
2396 | |||
2397 | spin_lock_irq(&gcwq->lock); | ||
2398 | |||
2399 | worker = find_worker_executing_work(gcwq, work); | ||
2400 | if (unlikely(worker)) | ||
2401 | insert_wq_barrier(worker->current_cwq, &barr, work, worker); | ||
2402 | |||
2403 | spin_unlock_irq(&gcwq->lock); | ||
2404 | |||
2405 | if (unlikely(worker)) { | ||
2406 | wait_for_completion(&barr.done); | ||
2407 | destroy_work_on_stack(&barr.work); | ||
2408 | return true; | ||
2409 | } else | ||
2410 | return false; | ||
2411 | } | ||
2412 | |||
2413 | static bool wait_on_work(struct work_struct *work) | ||
2414 | { | ||
2415 | bool ret = false; | ||
2416 | int cpu; | ||
2417 | |||
2418 | might_sleep(); | ||
2419 | |||
2420 | lock_map_acquire(&work->lockdep_map); | ||
2421 | lock_map_release(&work->lockdep_map); | ||
2422 | |||
2423 | for_each_gcwq_cpu(cpu) | ||
2424 | ret |= wait_on_cpu_work(get_gcwq(cpu), work); | ||
2425 | return ret; | ||
2426 | } | ||
2427 | |||
2428 | /** | ||
2429 | * flush_work_sync - wait until a work has finished execution | ||
2430 | * @work: the work to flush | ||
2431 | * | ||
2432 | * Wait until @work has finished execution. On return, it's | ||
2433 | * guaranteed that all queueing instances of @work which happened | ||
2434 | * before this function is called are finished. In other words, if | ||
2435 | * @work hasn't been requeued since this function was called, @work is | ||
2436 | * guaranteed to be idle on return. | ||
2437 | * | ||
2438 | * RETURNS: | ||
2439 | * %true if flush_work_sync() waited for the work to finish execution, | ||
2440 | * %false if it was already idle. | ||
2441 | */ | ||
2442 | bool flush_work_sync(struct work_struct *work) | ||
2443 | { | ||
2444 | struct wq_barrier barr; | ||
2445 | bool pending, waited; | ||
2446 | |||
2447 | /* we'll wait for executions separately, queue barr only if pending */ | ||
2448 | pending = start_flush_work(work, &barr, false); | ||
2449 | |||
2450 | /* wait for executions to finish */ | ||
2451 | waited = wait_on_work(work); | ||
2452 | |||
2453 | /* wait for the pending one */ | ||
2454 | if (pending) { | ||
2455 | wait_for_completion(&barr.done); | ||
2456 | destroy_work_on_stack(&barr.work); | ||
2457 | } | ||
2458 | |||
2459 | return pending || waited; | ||
2460 | } | ||
2461 | EXPORT_SYMBOL_GPL(flush_work_sync); | ||
2462 | |||
2350 | /* | 2463 | /* |
2351 | * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit, | 2464 | * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit, |
2352 | * so this work can't be re-armed in any way. | 2465 | * so this work can't be re-armed in any way. |
@@ -2379,7 +2492,8 @@ static int try_to_grab_pending(struct work_struct *work) | |||
2379 | debug_work_deactivate(work); | 2492 | debug_work_deactivate(work); |
2380 | list_del_init(&work->entry); | 2493 | list_del_init(&work->entry); |
2381 | cwq_dec_nr_in_flight(get_work_cwq(work), | 2494 | cwq_dec_nr_in_flight(get_work_cwq(work), |
2382 | get_work_color(work)); | 2495 | get_work_color(work), |
2496 | *work_data_bits(work) & WORK_STRUCT_DELAYED); | ||
2383 | ret = 1; | 2497 | ret = 1; |
2384 | } | 2498 | } |
2385 | } | 2499 | } |
@@ -2388,39 +2502,7 @@ static int try_to_grab_pending(struct work_struct *work) | |||
2388 | return ret; | 2502 | return ret; |
2389 | } | 2503 | } |
2390 | 2504 | ||
2391 | static void wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work) | 2505 | static bool __cancel_work_timer(struct work_struct *work, |
2392 | { | ||
2393 | struct wq_barrier barr; | ||
2394 | struct worker *worker; | ||
2395 | |||
2396 | spin_lock_irq(&gcwq->lock); | ||
2397 | |||
2398 | worker = find_worker_executing_work(gcwq, work); | ||
2399 | if (unlikely(worker)) | ||
2400 | insert_wq_barrier(worker->current_cwq, &barr, work, worker); | ||
2401 | |||
2402 | spin_unlock_irq(&gcwq->lock); | ||
2403 | |||
2404 | if (unlikely(worker)) { | ||
2405 | wait_for_completion(&barr.done); | ||
2406 | destroy_work_on_stack(&barr.work); | ||
2407 | } | ||
2408 | } | ||
2409 | |||
2410 | static void wait_on_work(struct work_struct *work) | ||
2411 | { | ||
2412 | int cpu; | ||
2413 | |||
2414 | might_sleep(); | ||
2415 | |||
2416 | lock_map_acquire(&work->lockdep_map); | ||
2417 | lock_map_release(&work->lockdep_map); | ||
2418 | |||
2419 | for_each_gcwq_cpu(cpu) | ||
2420 | wait_on_cpu_work(get_gcwq(cpu), work); | ||
2421 | } | ||
2422 | |||
2423 | static int __cancel_work_timer(struct work_struct *work, | ||
2424 | struct timer_list* timer) | 2506 | struct timer_list* timer) |
2425 | { | 2507 | { |
2426 | int ret; | 2508 | int ret; |
@@ -2437,42 +2519,81 @@ static int __cancel_work_timer(struct work_struct *work, | |||
2437 | } | 2519 | } |
2438 | 2520 | ||
2439 | /** | 2521 | /** |
2440 | * cancel_work_sync - block until a work_struct's callback has terminated | 2522 | * cancel_work_sync - cancel a work and wait for it to finish |
2441 | * @work: the work which is to be flushed | 2523 | * @work: the work to cancel |
2442 | * | ||
2443 | * Returns true if @work was pending. | ||
2444 | * | ||
2445 | * cancel_work_sync() will cancel the work if it is queued. If the work's | ||
2446 | * callback appears to be running, cancel_work_sync() will block until it | ||
2447 | * has completed. | ||
2448 | * | 2524 | * |
2449 | * It is possible to use this function if the work re-queues itself. It can | 2525 | * Cancel @work and wait for its execution to finish. This function |
2450 | * cancel the work even if it migrates to another workqueue, however in that | 2526 | * can be used even if the work re-queues itself or migrates to |
2451 | * case it only guarantees that work->func() has completed on the last queued | 2527 | * another workqueue. On return from this function, @work is |
2452 | * workqueue. | 2528 | * guaranteed to be not pending or executing on any CPU. |
2453 | * | 2529 | * |
2454 | * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not | 2530 | * cancel_work_sync(&delayed_work->work) must not be used for |
2455 | * pending, otherwise it goes into a busy-wait loop until the timer expires. | 2531 | * delayed_work's. Use cancel_delayed_work_sync() instead. |
2456 | * | 2532 | * |
2457 | * The caller must ensure that workqueue_struct on which this work was last | 2533 | * The caller must ensure that the workqueue on which @work was last |
2458 | * queued can't be destroyed before this function returns. | 2534 | * queued can't be destroyed before this function returns. |
2535 | * | ||
2536 | * RETURNS: | ||
2537 | * %true if @work was pending, %false otherwise. | ||
2459 | */ | 2538 | */ |
2460 | int cancel_work_sync(struct work_struct *work) | 2539 | bool cancel_work_sync(struct work_struct *work) |
2461 | { | 2540 | { |
2462 | return __cancel_work_timer(work, NULL); | 2541 | return __cancel_work_timer(work, NULL); |
2463 | } | 2542 | } |
2464 | EXPORT_SYMBOL_GPL(cancel_work_sync); | 2543 | EXPORT_SYMBOL_GPL(cancel_work_sync); |
2465 | 2544 | ||
2466 | /** | 2545 | /** |
2467 | * cancel_delayed_work_sync - reliably kill off a delayed work. | 2546 | * flush_delayed_work - wait for a dwork to finish executing the last queueing |
2468 | * @dwork: the delayed work struct | 2547 | * @dwork: the delayed work to flush |
2469 | * | 2548 | * |
2470 | * Returns true if @dwork was pending. | 2549 | * Delayed timer is cancelled and the pending work is queued for |
2550 | * immediate execution. Like flush_work(), this function only | ||
2551 | * considers the last queueing instance of @dwork. | ||
2471 | * | 2552 | * |
2472 | * It is possible to use this function if @dwork rearms itself via queue_work() | 2553 | * RETURNS: |
2473 | * or queue_delayed_work(). See also the comment for cancel_work_sync(). | 2554 | * %true if flush_work() waited for the work to finish execution, |
2555 | * %false if it was already idle. | ||
2474 | */ | 2556 | */ |
2475 | int cancel_delayed_work_sync(struct delayed_work *dwork) | 2557 | bool flush_delayed_work(struct delayed_work *dwork) |
2558 | { | ||
2559 | if (del_timer_sync(&dwork->timer)) | ||
2560 | __queue_work(raw_smp_processor_id(), | ||
2561 | get_work_cwq(&dwork->work)->wq, &dwork->work); | ||
2562 | return flush_work(&dwork->work); | ||
2563 | } | ||
2564 | EXPORT_SYMBOL(flush_delayed_work); | ||
2565 | |||
2566 | /** | ||
2567 | * flush_delayed_work_sync - wait for a dwork to finish | ||
2568 | * @dwork: the delayed work to flush | ||
2569 | * | ||
2570 | * Delayed timer is cancelled and the pending work is queued for | ||
2571 | * execution immediately. Other than timer handling, its behavior | ||
2572 | * is identical to flush_work_sync(). | ||
2573 | * | ||
2574 | * RETURNS: | ||
2575 | * %true if flush_work_sync() waited for the work to finish execution, | ||
2576 | * %false if it was already idle. | ||
2577 | */ | ||
2578 | bool flush_delayed_work_sync(struct delayed_work *dwork) | ||
2579 | { | ||
2580 | if (del_timer_sync(&dwork->timer)) | ||
2581 | __queue_work(raw_smp_processor_id(), | ||
2582 | get_work_cwq(&dwork->work)->wq, &dwork->work); | ||
2583 | return flush_work_sync(&dwork->work); | ||
2584 | } | ||
2585 | EXPORT_SYMBOL(flush_delayed_work_sync); | ||
2586 | |||
2587 | /** | ||
2588 | * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish | ||
2589 | * @dwork: the delayed work cancel | ||
2590 | * | ||
2591 | * This is cancel_work_sync() for delayed works. | ||
2592 | * | ||
2593 | * RETURNS: | ||
2594 | * %true if @dwork was pending, %false otherwise. | ||
2595 | */ | ||
2596 | bool cancel_delayed_work_sync(struct delayed_work *dwork) | ||
2476 | { | 2597 | { |
2477 | return __cancel_work_timer(&dwork->work, &dwork->timer); | 2598 | return __cancel_work_timer(&dwork->work, &dwork->timer); |
2478 | } | 2599 | } |
@@ -2524,23 +2645,6 @@ int schedule_delayed_work(struct delayed_work *dwork, | |||
2524 | EXPORT_SYMBOL(schedule_delayed_work); | 2645 | EXPORT_SYMBOL(schedule_delayed_work); |
2525 | 2646 | ||
2526 | /** | 2647 | /** |
2527 | * flush_delayed_work - block until a dwork_struct's callback has terminated | ||
2528 | * @dwork: the delayed work which is to be flushed | ||
2529 | * | ||
2530 | * Any timeout is cancelled, and any pending work is run immediately. | ||
2531 | */ | ||
2532 | void flush_delayed_work(struct delayed_work *dwork) | ||
2533 | { | ||
2534 | if (del_timer_sync(&dwork->timer)) { | ||
2535 | __queue_work(get_cpu(), get_work_cwq(&dwork->work)->wq, | ||
2536 | &dwork->work); | ||
2537 | put_cpu(); | ||
2538 | } | ||
2539 | flush_work(&dwork->work); | ||
2540 | } | ||
2541 | EXPORT_SYMBOL(flush_delayed_work); | ||
2542 | |||
2543 | /** | ||
2544 | * schedule_delayed_work_on - queue work in global workqueue on CPU after delay | 2648 | * schedule_delayed_work_on - queue work in global workqueue on CPU after delay |
2545 | * @cpu: cpu to use | 2649 | * @cpu: cpu to use |
2546 | * @dwork: job to be done | 2650 | * @dwork: job to be done |
@@ -2557,13 +2661,15 @@ int schedule_delayed_work_on(int cpu, | |||
2557 | EXPORT_SYMBOL(schedule_delayed_work_on); | 2661 | EXPORT_SYMBOL(schedule_delayed_work_on); |
2558 | 2662 | ||
2559 | /** | 2663 | /** |
2560 | * schedule_on_each_cpu - call a function on each online CPU from keventd | 2664 | * schedule_on_each_cpu - execute a function synchronously on each online CPU |
2561 | * @func: the function to call | 2665 | * @func: the function to call |
2562 | * | 2666 | * |
2563 | * Returns zero on success. | 2667 | * schedule_on_each_cpu() executes @func on each online CPU using the |
2564 | * Returns -ve errno on failure. | 2668 | * system workqueue and blocks until all CPUs have completed. |
2565 | * | ||
2566 | * schedule_on_each_cpu() is very slow. | 2669 | * schedule_on_each_cpu() is very slow. |
2670 | * | ||
2671 | * RETURNS: | ||
2672 | * 0 on success, -errno on failure. | ||
2567 | */ | 2673 | */ |
2568 | int schedule_on_each_cpu(work_func_t func) | 2674 | int schedule_on_each_cpu(work_func_t func) |
2569 | { | 2675 | { |
@@ -2685,7 +2791,9 @@ static int alloc_cwqs(struct workqueue_struct *wq) | |||
2685 | } | 2791 | } |
2686 | } | 2792 | } |
2687 | 2793 | ||
2688 | /* just in case, make sure it's actually aligned */ | 2794 | /* just in case, make sure it's actually aligned |
2795 | * - this is affected by PERCPU() alignment in vmlinux.lds.S | ||
2796 | */ | ||
2689 | BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align)); | 2797 | BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align)); |
2690 | return wq->cpu_wq.v ? 0 : -ENOMEM; | 2798 | return wq->cpu_wq.v ? 0 : -ENOMEM; |
2691 | } | 2799 | } |
@@ -2729,6 +2837,13 @@ struct workqueue_struct *__alloc_workqueue_key(const char *name, | |||
2729 | unsigned int cpu; | 2837 | unsigned int cpu; |
2730 | 2838 | ||
2731 | /* | 2839 | /* |
2840 | * Workqueues which may be used during memory reclaim should | ||
2841 | * have a rescuer to guarantee forward progress. | ||
2842 | */ | ||
2843 | if (flags & WQ_MEM_RECLAIM) | ||
2844 | flags |= WQ_RESCUER; | ||
2845 | |||
2846 | /* | ||
2732 | * Unbound workqueues aren't concurrency managed and should be | 2847 | * Unbound workqueues aren't concurrency managed and should be |
2733 | * dispatched to workers immediately. | 2848 | * dispatched to workers immediately. |
2734 | */ | 2849 | */ |
@@ -2782,7 +2897,6 @@ struct workqueue_struct *__alloc_workqueue_key(const char *name, | |||
2782 | if (IS_ERR(rescuer->task)) | 2897 | if (IS_ERR(rescuer->task)) |
2783 | goto err; | 2898 | goto err; |
2784 | 2899 | ||
2785 | wq->rescuer = rescuer; | ||
2786 | rescuer->task->flags |= PF_THREAD_BOUND; | 2900 | rescuer->task->flags |= PF_THREAD_BOUND; |
2787 | wake_up_process(rescuer->task); | 2901 | wake_up_process(rescuer->task); |
2788 | } | 2902 | } |
@@ -2824,6 +2938,7 @@ void destroy_workqueue(struct workqueue_struct *wq) | |||
2824 | { | 2938 | { |
2825 | unsigned int cpu; | 2939 | unsigned int cpu; |
2826 | 2940 | ||
2941 | wq->flags |= WQ_DYING; | ||
2827 | flush_workqueue(wq); | 2942 | flush_workqueue(wq); |
2828 | 2943 | ||
2829 | /* | 2944 | /* |
@@ -2848,6 +2963,7 @@ void destroy_workqueue(struct workqueue_struct *wq) | |||
2848 | if (wq->flags & WQ_RESCUER) { | 2963 | if (wq->flags & WQ_RESCUER) { |
2849 | kthread_stop(wq->rescuer->task); | 2964 | kthread_stop(wq->rescuer->task); |
2850 | free_mayday_mask(wq->mayday_mask); | 2965 | free_mayday_mask(wq->mayday_mask); |
2966 | kfree(wq->rescuer); | ||
2851 | } | 2967 | } |
2852 | 2968 | ||
2853 | free_cwqs(wq); | 2969 | free_cwqs(wq); |
@@ -3230,6 +3346,8 @@ static int __cpuinit trustee_thread(void *__gcwq) | |||
3230 | * multiple times. To be used by cpu_callback. | 3346 | * multiple times. To be used by cpu_callback. |
3231 | */ | 3347 | */ |
3232 | static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state) | 3348 | static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state) |
3349 | __releases(&gcwq->lock) | ||
3350 | __acquires(&gcwq->lock) | ||
3233 | { | 3351 | { |
3234 | if (!(gcwq->trustee_state == state || | 3352 | if (!(gcwq->trustee_state == state || |
3235 | gcwq->trustee_state == TRUSTEE_DONE)) { | 3353 | gcwq->trustee_state == TRUSTEE_DONE)) { |
@@ -3536,8 +3654,7 @@ static int __init init_workqueues(void) | |||
3536 | spin_lock_init(&gcwq->lock); | 3654 | spin_lock_init(&gcwq->lock); |
3537 | INIT_LIST_HEAD(&gcwq->worklist); | 3655 | INIT_LIST_HEAD(&gcwq->worklist); |
3538 | gcwq->cpu = cpu; | 3656 | gcwq->cpu = cpu; |
3539 | if (cpu == WORK_CPU_UNBOUND) | 3657 | gcwq->flags |= GCWQ_DISASSOCIATED; |
3540 | gcwq->flags |= GCWQ_DISASSOCIATED; | ||
3541 | 3658 | ||
3542 | INIT_LIST_HEAD(&gcwq->idle_list); | 3659 | INIT_LIST_HEAD(&gcwq->idle_list); |
3543 | for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) | 3660 | for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) |
@@ -3561,6 +3678,8 @@ static int __init init_workqueues(void) | |||
3561 | struct global_cwq *gcwq = get_gcwq(cpu); | 3678 | struct global_cwq *gcwq = get_gcwq(cpu); |
3562 | struct worker *worker; | 3679 | struct worker *worker; |
3563 | 3680 | ||
3681 | if (cpu != WORK_CPU_UNBOUND) | ||
3682 | gcwq->flags &= ~GCWQ_DISASSOCIATED; | ||
3564 | worker = create_worker(gcwq, true); | 3683 | worker = create_worker(gcwq, true); |
3565 | BUG_ON(!worker); | 3684 | BUG_ON(!worker); |
3566 | spin_lock_irq(&gcwq->lock); | 3685 | spin_lock_irq(&gcwq->lock); |
@@ -3573,7 +3692,8 @@ static int __init init_workqueues(void) | |||
3573 | system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0); | 3692 | system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0); |
3574 | system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, | 3693 | system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, |
3575 | WQ_UNBOUND_MAX_ACTIVE); | 3694 | WQ_UNBOUND_MAX_ACTIVE); |
3576 | BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq); | 3695 | BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq || |
3696 | !system_unbound_wq); | ||
3577 | return 0; | 3697 | return 0; |
3578 | } | 3698 | } |
3579 | early_initcall(init_workqueues); | 3699 | early_initcall(init_workqueues); |