diff options
Diffstat (limited to 'kernel/workqueue.c')
-rw-r--r-- | kernel/workqueue.c | 458 |
1 files changed, 319 insertions, 139 deletions
diff --git a/kernel/workqueue.c b/kernel/workqueue.c index f77afd939229..0400553f0d04 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c | |||
@@ -42,9 +42,6 @@ | |||
42 | #include <linux/lockdep.h> | 42 | #include <linux/lockdep.h> |
43 | #include <linux/idr.h> | 43 | #include <linux/idr.h> |
44 | 44 | ||
45 | #define CREATE_TRACE_POINTS | ||
46 | #include <trace/events/workqueue.h> | ||
47 | |||
48 | #include "workqueue_sched.h" | 45 | #include "workqueue_sched.h" |
49 | 46 | ||
50 | enum { | 47 | enum { |
@@ -82,7 +79,9 @@ enum { | |||
82 | MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */ | 79 | MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */ |
83 | IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */ | 80 | IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */ |
84 | 81 | ||
85 | MAYDAY_INITIAL_TIMEOUT = HZ / 100, /* call for help after 10ms */ | 82 | MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2, |
83 | /* call for help after 10ms | ||
84 | (min two ticks) */ | ||
86 | MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */ | 85 | MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */ |
87 | CREATE_COOLDOWN = HZ, /* time to breath after fail */ | 86 | CREATE_COOLDOWN = HZ, /* time to breath after fail */ |
88 | TRUSTEE_COOLDOWN = HZ / 10, /* for trustee draining */ | 87 | TRUSTEE_COOLDOWN = HZ / 10, /* for trustee draining */ |
@@ -252,10 +251,15 @@ struct workqueue_struct *system_wq __read_mostly; | |||
252 | struct workqueue_struct *system_long_wq __read_mostly; | 251 | struct workqueue_struct *system_long_wq __read_mostly; |
253 | struct workqueue_struct *system_nrt_wq __read_mostly; | 252 | struct workqueue_struct *system_nrt_wq __read_mostly; |
254 | struct workqueue_struct *system_unbound_wq __read_mostly; | 253 | struct workqueue_struct *system_unbound_wq __read_mostly; |
254 | struct workqueue_struct *system_freezable_wq __read_mostly; | ||
255 | EXPORT_SYMBOL_GPL(system_wq); | 255 | EXPORT_SYMBOL_GPL(system_wq); |
256 | EXPORT_SYMBOL_GPL(system_long_wq); | 256 | EXPORT_SYMBOL_GPL(system_long_wq); |
257 | EXPORT_SYMBOL_GPL(system_nrt_wq); | 257 | EXPORT_SYMBOL_GPL(system_nrt_wq); |
258 | EXPORT_SYMBOL_GPL(system_unbound_wq); | 258 | EXPORT_SYMBOL_GPL(system_unbound_wq); |
259 | EXPORT_SYMBOL_GPL(system_freezable_wq); | ||
260 | |||
261 | #define CREATE_TRACE_POINTS | ||
262 | #include <trace/events/workqueue.h> | ||
259 | 263 | ||
260 | #define for_each_busy_worker(worker, i, pos, gcwq) \ | 264 | #define for_each_busy_worker(worker, i, pos, gcwq) \ |
261 | for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \ | 265 | for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \ |
@@ -310,25 +314,15 @@ static inline int __next_wq_cpu(int cpu, const struct cpumask *mask, | |||
310 | (cpu) < WORK_CPU_NONE; \ | 314 | (cpu) < WORK_CPU_NONE; \ |
311 | (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq))) | 315 | (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq))) |
312 | 316 | ||
313 | #ifdef CONFIG_LOCKDEP | ||
314 | /** | ||
315 | * in_workqueue_context() - in context of specified workqueue? | ||
316 | * @wq: the workqueue of interest | ||
317 | * | ||
318 | * Checks lockdep state to see if the current task is executing from | ||
319 | * within a workqueue item. This function exists only if lockdep is | ||
320 | * enabled. | ||
321 | */ | ||
322 | int in_workqueue_context(struct workqueue_struct *wq) | ||
323 | { | ||
324 | return lock_is_held(&wq->lockdep_map); | ||
325 | } | ||
326 | #endif | ||
327 | |||
328 | #ifdef CONFIG_DEBUG_OBJECTS_WORK | 317 | #ifdef CONFIG_DEBUG_OBJECTS_WORK |
329 | 318 | ||
330 | static struct debug_obj_descr work_debug_descr; | 319 | static struct debug_obj_descr work_debug_descr; |
331 | 320 | ||
321 | static void *work_debug_hint(void *addr) | ||
322 | { | ||
323 | return ((struct work_struct *) addr)->func; | ||
324 | } | ||
325 | |||
332 | /* | 326 | /* |
333 | * fixup_init is called when: | 327 | * fixup_init is called when: |
334 | * - an active object is initialized | 328 | * - an active object is initialized |
@@ -400,6 +394,7 @@ static int work_fixup_free(void *addr, enum debug_obj_state state) | |||
400 | 394 | ||
401 | static struct debug_obj_descr work_debug_descr = { | 395 | static struct debug_obj_descr work_debug_descr = { |
402 | .name = "work_struct", | 396 | .name = "work_struct", |
397 | .debug_hint = work_debug_hint, | ||
403 | .fixup_init = work_fixup_init, | 398 | .fixup_init = work_fixup_init, |
404 | .fixup_activate = work_fixup_activate, | 399 | .fixup_activate = work_fixup_activate, |
405 | .fixup_free = work_fixup_free, | 400 | .fixup_free = work_fixup_free, |
@@ -604,7 +599,9 @@ static bool keep_working(struct global_cwq *gcwq) | |||
604 | { | 599 | { |
605 | atomic_t *nr_running = get_gcwq_nr_running(gcwq->cpu); | 600 | atomic_t *nr_running = get_gcwq_nr_running(gcwq->cpu); |
606 | 601 | ||
607 | return !list_empty(&gcwq->worklist) && atomic_read(nr_running) <= 1; | 602 | return !list_empty(&gcwq->worklist) && |
603 | (atomic_read(nr_running) <= 1 || | ||
604 | gcwq->flags & GCWQ_HIGHPRI_PENDING); | ||
608 | } | 605 | } |
609 | 606 | ||
610 | /* Do we need a new worker? Called from manager. */ | 607 | /* Do we need a new worker? Called from manager. */ |
@@ -674,7 +671,7 @@ void wq_worker_waking_up(struct task_struct *task, unsigned int cpu) | |||
674 | { | 671 | { |
675 | struct worker *worker = kthread_data(task); | 672 | struct worker *worker = kthread_data(task); |
676 | 673 | ||
677 | if (likely(!(worker->flags & WORKER_NOT_RUNNING))) | 674 | if (!(worker->flags & WORKER_NOT_RUNNING)) |
678 | atomic_inc(get_gcwq_nr_running(cpu)); | 675 | atomic_inc(get_gcwq_nr_running(cpu)); |
679 | } | 676 | } |
680 | 677 | ||
@@ -700,7 +697,7 @@ struct task_struct *wq_worker_sleeping(struct task_struct *task, | |||
700 | struct global_cwq *gcwq = get_gcwq(cpu); | 697 | struct global_cwq *gcwq = get_gcwq(cpu); |
701 | atomic_t *nr_running = get_gcwq_nr_running(cpu); | 698 | atomic_t *nr_running = get_gcwq_nr_running(cpu); |
702 | 699 | ||
703 | if (unlikely(worker->flags & WORKER_NOT_RUNNING)) | 700 | if (worker->flags & WORKER_NOT_RUNNING) |
704 | return NULL; | 701 | return NULL; |
705 | 702 | ||
706 | /* this can only happen on the local cpu */ | 703 | /* this can only happen on the local cpu */ |
@@ -781,7 +778,11 @@ static inline void worker_clr_flags(struct worker *worker, unsigned int flags) | |||
781 | 778 | ||
782 | worker->flags &= ~flags; | 779 | worker->flags &= ~flags; |
783 | 780 | ||
784 | /* if transitioning out of NOT_RUNNING, increment nr_running */ | 781 | /* |
782 | * If transitioning out of NOT_RUNNING, increment nr_running. Note | ||
783 | * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask | ||
784 | * of multiple flags, not a single flag. | ||
785 | */ | ||
785 | if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) | 786 | if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) |
786 | if (!(worker->flags & WORKER_NOT_RUNNING)) | 787 | if (!(worker->flags & WORKER_NOT_RUNNING)) |
787 | atomic_inc(get_gcwq_nr_running(gcwq->cpu)); | 788 | atomic_inc(get_gcwq_nr_running(gcwq->cpu)); |
@@ -945,6 +946,38 @@ static void insert_work(struct cpu_workqueue_struct *cwq, | |||
945 | wake_up_worker(gcwq); | 946 | wake_up_worker(gcwq); |
946 | } | 947 | } |
947 | 948 | ||
949 | /* | ||
950 | * Test whether @work is being queued from another work executing on the | ||
951 | * same workqueue. This is rather expensive and should only be used from | ||
952 | * cold paths. | ||
953 | */ | ||
954 | static bool is_chained_work(struct workqueue_struct *wq) | ||
955 | { | ||
956 | unsigned long flags; | ||
957 | unsigned int cpu; | ||
958 | |||
959 | for_each_gcwq_cpu(cpu) { | ||
960 | struct global_cwq *gcwq = get_gcwq(cpu); | ||
961 | struct worker *worker; | ||
962 | struct hlist_node *pos; | ||
963 | int i; | ||
964 | |||
965 | spin_lock_irqsave(&gcwq->lock, flags); | ||
966 | for_each_busy_worker(worker, i, pos, gcwq) { | ||
967 | if (worker->task != current) | ||
968 | continue; | ||
969 | spin_unlock_irqrestore(&gcwq->lock, flags); | ||
970 | /* | ||
971 | * I'm @worker, no locking necessary. See if @work | ||
972 | * is headed to the same workqueue. | ||
973 | */ | ||
974 | return worker->current_cwq->wq == wq; | ||
975 | } | ||
976 | spin_unlock_irqrestore(&gcwq->lock, flags); | ||
977 | } | ||
978 | return false; | ||
979 | } | ||
980 | |||
948 | static void __queue_work(unsigned int cpu, struct workqueue_struct *wq, | 981 | static void __queue_work(unsigned int cpu, struct workqueue_struct *wq, |
949 | struct work_struct *work) | 982 | struct work_struct *work) |
950 | { | 983 | { |
@@ -956,7 +989,9 @@ static void __queue_work(unsigned int cpu, struct workqueue_struct *wq, | |||
956 | 989 | ||
957 | debug_work_activate(work); | 990 | debug_work_activate(work); |
958 | 991 | ||
959 | if (WARN_ON_ONCE(wq->flags & WQ_DYING)) | 992 | /* if dying, only works from the same workqueue are allowed */ |
993 | if (unlikely(wq->flags & WQ_DYING) && | ||
994 | WARN_ON_ONCE(!is_chained_work(wq))) | ||
960 | return; | 995 | return; |
961 | 996 | ||
962 | /* determine gcwq to use */ | 997 | /* determine gcwq to use */ |
@@ -997,6 +1032,7 @@ static void __queue_work(unsigned int cpu, struct workqueue_struct *wq, | |||
997 | 1032 | ||
998 | /* gcwq determined, get cwq and queue */ | 1033 | /* gcwq determined, get cwq and queue */ |
999 | cwq = get_cwq(gcwq->cpu, wq); | 1034 | cwq = get_cwq(gcwq->cpu, wq); |
1035 | trace_workqueue_queue_work(cpu, cwq, work); | ||
1000 | 1036 | ||
1001 | BUG_ON(!list_empty(&work->entry)); | 1037 | BUG_ON(!list_empty(&work->entry)); |
1002 | 1038 | ||
@@ -1004,6 +1040,7 @@ static void __queue_work(unsigned int cpu, struct workqueue_struct *wq, | |||
1004 | work_flags = work_color_to_flags(cwq->work_color); | 1040 | work_flags = work_color_to_flags(cwq->work_color); |
1005 | 1041 | ||
1006 | if (likely(cwq->nr_active < cwq->max_active)) { | 1042 | if (likely(cwq->nr_active < cwq->max_active)) { |
1043 | trace_workqueue_activate_work(work); | ||
1007 | cwq->nr_active++; | 1044 | cwq->nr_active++; |
1008 | worklist = gcwq_determine_ins_pos(gcwq, cwq); | 1045 | worklist = gcwq_determine_ins_pos(gcwq, cwq); |
1009 | } else { | 1046 | } else { |
@@ -1254,8 +1291,14 @@ __acquires(&gcwq->lock) | |||
1254 | return true; | 1291 | return true; |
1255 | spin_unlock_irq(&gcwq->lock); | 1292 | spin_unlock_irq(&gcwq->lock); |
1256 | 1293 | ||
1257 | /* CPU has come up inbetween, retry migration */ | 1294 | /* |
1295 | * We've raced with CPU hot[un]plug. Give it a breather | ||
1296 | * and retry migration. cond_resched() is required here; | ||
1297 | * otherwise, we might deadlock against cpu_stop trying to | ||
1298 | * bring down the CPU on non-preemptive kernel. | ||
1299 | */ | ||
1258 | cpu_relax(); | 1300 | cpu_relax(); |
1301 | cond_resched(); | ||
1259 | } | 1302 | } |
1260 | } | 1303 | } |
1261 | 1304 | ||
@@ -1329,8 +1372,10 @@ static struct worker *create_worker(struct global_cwq *gcwq, bool bind) | |||
1329 | worker->id = id; | 1372 | worker->id = id; |
1330 | 1373 | ||
1331 | if (!on_unbound_cpu) | 1374 | if (!on_unbound_cpu) |
1332 | worker->task = kthread_create(worker_thread, worker, | 1375 | worker->task = kthread_create_on_node(worker_thread, |
1333 | "kworker/%u:%d", gcwq->cpu, id); | 1376 | worker, |
1377 | cpu_to_node(gcwq->cpu), | ||
1378 | "kworker/%u:%d", gcwq->cpu, id); | ||
1334 | else | 1379 | else |
1335 | worker->task = kthread_create(worker_thread, worker, | 1380 | worker->task = kthread_create(worker_thread, worker, |
1336 | "kworker/u:%d", id); | 1381 | "kworker/u:%d", id); |
@@ -1679,6 +1724,7 @@ static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq) | |||
1679 | struct work_struct, entry); | 1724 | struct work_struct, entry); |
1680 | struct list_head *pos = gcwq_determine_ins_pos(cwq->gcwq, cwq); | 1725 | struct list_head *pos = gcwq_determine_ins_pos(cwq->gcwq, cwq); |
1681 | 1726 | ||
1727 | trace_workqueue_activate_work(work); | ||
1682 | move_linked_works(work, pos, NULL); | 1728 | move_linked_works(work, pos, NULL); |
1683 | __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work)); | 1729 | __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work)); |
1684 | cwq->nr_active++; | 1730 | cwq->nr_active++; |
@@ -1816,7 +1862,7 @@ __acquires(&gcwq->lock) | |||
1816 | spin_unlock_irq(&gcwq->lock); | 1862 | spin_unlock_irq(&gcwq->lock); |
1817 | 1863 | ||
1818 | work_clear_pending(work); | 1864 | work_clear_pending(work); |
1819 | lock_map_acquire(&cwq->wq->lockdep_map); | 1865 | lock_map_acquire_read(&cwq->wq->lockdep_map); |
1820 | lock_map_acquire(&lockdep_map); | 1866 | lock_map_acquire(&lockdep_map); |
1821 | trace_workqueue_execute_start(work); | 1867 | trace_workqueue_execute_start(work); |
1822 | f(work); | 1868 | f(work); |
@@ -2019,6 +2065,15 @@ repeat: | |||
2019 | move_linked_works(work, scheduled, &n); | 2065 | move_linked_works(work, scheduled, &n); |
2020 | 2066 | ||
2021 | process_scheduled_works(rescuer); | 2067 | process_scheduled_works(rescuer); |
2068 | |||
2069 | /* | ||
2070 | * Leave this gcwq. If keep_working() is %true, notify a | ||
2071 | * regular worker; otherwise, we end up with 0 concurrency | ||
2072 | * and stalling the execution. | ||
2073 | */ | ||
2074 | if (keep_working(gcwq)) | ||
2075 | wake_up_worker(gcwq); | ||
2076 | |||
2022 | spin_unlock_irq(&gcwq->lock); | 2077 | spin_unlock_irq(&gcwq->lock); |
2023 | } | 2078 | } |
2024 | 2079 | ||
@@ -2074,7 +2129,7 @@ static void insert_wq_barrier(struct cpu_workqueue_struct *cwq, | |||
2074 | * checks and call back into the fixup functions where we | 2129 | * checks and call back into the fixup functions where we |
2075 | * might deadlock. | 2130 | * might deadlock. |
2076 | */ | 2131 | */ |
2077 | INIT_WORK_ON_STACK(&barr->work, wq_barrier_func); | 2132 | INIT_WORK_ONSTACK(&barr->work, wq_barrier_func); |
2078 | __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); | 2133 | __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); |
2079 | init_completion(&barr->done); | 2134 | init_completion(&barr->done); |
2080 | 2135 | ||
@@ -2326,27 +2381,17 @@ out_unlock: | |||
2326 | } | 2381 | } |
2327 | EXPORT_SYMBOL_GPL(flush_workqueue); | 2382 | EXPORT_SYMBOL_GPL(flush_workqueue); |
2328 | 2383 | ||
2329 | /** | 2384 | static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, |
2330 | * flush_work - block until a work_struct's callback has terminated | 2385 | bool wait_executing) |
2331 | * @work: the work which is to be flushed | ||
2332 | * | ||
2333 | * Returns false if @work has already terminated. | ||
2334 | * | ||
2335 | * It is expected that, prior to calling flush_work(), the caller has | ||
2336 | * arranged for the work to not be requeued, otherwise it doesn't make | ||
2337 | * sense to use this function. | ||
2338 | */ | ||
2339 | int flush_work(struct work_struct *work) | ||
2340 | { | 2386 | { |
2341 | struct worker *worker = NULL; | 2387 | struct worker *worker = NULL; |
2342 | struct global_cwq *gcwq; | 2388 | struct global_cwq *gcwq; |
2343 | struct cpu_workqueue_struct *cwq; | 2389 | struct cpu_workqueue_struct *cwq; |
2344 | struct wq_barrier barr; | ||
2345 | 2390 | ||
2346 | might_sleep(); | 2391 | might_sleep(); |
2347 | gcwq = get_work_gcwq(work); | 2392 | gcwq = get_work_gcwq(work); |
2348 | if (!gcwq) | 2393 | if (!gcwq) |
2349 | return 0; | 2394 | return false; |
2350 | 2395 | ||
2351 | spin_lock_irq(&gcwq->lock); | 2396 | spin_lock_irq(&gcwq->lock); |
2352 | if (!list_empty(&work->entry)) { | 2397 | if (!list_empty(&work->entry)) { |
@@ -2359,28 +2404,137 @@ int flush_work(struct work_struct *work) | |||
2359 | cwq = get_work_cwq(work); | 2404 | cwq = get_work_cwq(work); |
2360 | if (unlikely(!cwq || gcwq != cwq->gcwq)) | 2405 | if (unlikely(!cwq || gcwq != cwq->gcwq)) |
2361 | goto already_gone; | 2406 | goto already_gone; |
2362 | } else { | 2407 | } else if (wait_executing) { |
2363 | worker = find_worker_executing_work(gcwq, work); | 2408 | worker = find_worker_executing_work(gcwq, work); |
2364 | if (!worker) | 2409 | if (!worker) |
2365 | goto already_gone; | 2410 | goto already_gone; |
2366 | cwq = worker->current_cwq; | 2411 | cwq = worker->current_cwq; |
2367 | } | 2412 | } else |
2413 | goto already_gone; | ||
2368 | 2414 | ||
2369 | insert_wq_barrier(cwq, &barr, work, worker); | 2415 | insert_wq_barrier(cwq, barr, work, worker); |
2370 | spin_unlock_irq(&gcwq->lock); | 2416 | spin_unlock_irq(&gcwq->lock); |
2371 | 2417 | ||
2372 | lock_map_acquire(&cwq->wq->lockdep_map); | 2418 | /* |
2419 | * If @max_active is 1 or rescuer is in use, flushing another work | ||
2420 | * item on the same workqueue may lead to deadlock. Make sure the | ||
2421 | * flusher is not running on the same workqueue by verifying write | ||
2422 | * access. | ||
2423 | */ | ||
2424 | if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER) | ||
2425 | lock_map_acquire(&cwq->wq->lockdep_map); | ||
2426 | else | ||
2427 | lock_map_acquire_read(&cwq->wq->lockdep_map); | ||
2373 | lock_map_release(&cwq->wq->lockdep_map); | 2428 | lock_map_release(&cwq->wq->lockdep_map); |
2374 | 2429 | ||
2375 | wait_for_completion(&barr.done); | 2430 | return true; |
2376 | destroy_work_on_stack(&barr.work); | ||
2377 | return 1; | ||
2378 | already_gone: | 2431 | already_gone: |
2379 | spin_unlock_irq(&gcwq->lock); | 2432 | spin_unlock_irq(&gcwq->lock); |
2380 | return 0; | 2433 | return false; |
2434 | } | ||
2435 | |||
2436 | /** | ||
2437 | * flush_work - wait for a work to finish executing the last queueing instance | ||
2438 | * @work: the work to flush | ||
2439 | * | ||
2440 | * Wait until @work has finished execution. This function considers | ||
2441 | * only the last queueing instance of @work. If @work has been | ||
2442 | * enqueued across different CPUs on a non-reentrant workqueue or on | ||
2443 | * multiple workqueues, @work might still be executing on return on | ||
2444 | * some of the CPUs from earlier queueing. | ||
2445 | * | ||
2446 | * If @work was queued only on a non-reentrant, ordered or unbound | ||
2447 | * workqueue, @work is guaranteed to be idle on return if it hasn't | ||
2448 | * been requeued since flush started. | ||
2449 | * | ||
2450 | * RETURNS: | ||
2451 | * %true if flush_work() waited for the work to finish execution, | ||
2452 | * %false if it was already idle. | ||
2453 | */ | ||
2454 | bool flush_work(struct work_struct *work) | ||
2455 | { | ||
2456 | struct wq_barrier barr; | ||
2457 | |||
2458 | if (start_flush_work(work, &barr, true)) { | ||
2459 | wait_for_completion(&barr.done); | ||
2460 | destroy_work_on_stack(&barr.work); | ||
2461 | return true; | ||
2462 | } else | ||
2463 | return false; | ||
2381 | } | 2464 | } |
2382 | EXPORT_SYMBOL_GPL(flush_work); | 2465 | EXPORT_SYMBOL_GPL(flush_work); |
2383 | 2466 | ||
2467 | static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work) | ||
2468 | { | ||
2469 | struct wq_barrier barr; | ||
2470 | struct worker *worker; | ||
2471 | |||
2472 | spin_lock_irq(&gcwq->lock); | ||
2473 | |||
2474 | worker = find_worker_executing_work(gcwq, work); | ||
2475 | if (unlikely(worker)) | ||
2476 | insert_wq_barrier(worker->current_cwq, &barr, work, worker); | ||
2477 | |||
2478 | spin_unlock_irq(&gcwq->lock); | ||
2479 | |||
2480 | if (unlikely(worker)) { | ||
2481 | wait_for_completion(&barr.done); | ||
2482 | destroy_work_on_stack(&barr.work); | ||
2483 | return true; | ||
2484 | } else | ||
2485 | return false; | ||
2486 | } | ||
2487 | |||
2488 | static bool wait_on_work(struct work_struct *work) | ||
2489 | { | ||
2490 | bool ret = false; | ||
2491 | int cpu; | ||
2492 | |||
2493 | might_sleep(); | ||
2494 | |||
2495 | lock_map_acquire(&work->lockdep_map); | ||
2496 | lock_map_release(&work->lockdep_map); | ||
2497 | |||
2498 | for_each_gcwq_cpu(cpu) | ||
2499 | ret |= wait_on_cpu_work(get_gcwq(cpu), work); | ||
2500 | return ret; | ||
2501 | } | ||
2502 | |||
2503 | /** | ||
2504 | * flush_work_sync - wait until a work has finished execution | ||
2505 | * @work: the work to flush | ||
2506 | * | ||
2507 | * Wait until @work has finished execution. On return, it's | ||
2508 | * guaranteed that all queueing instances of @work which happened | ||
2509 | * before this function is called are finished. In other words, if | ||
2510 | * @work hasn't been requeued since this function was called, @work is | ||
2511 | * guaranteed to be idle on return. | ||
2512 | * | ||
2513 | * RETURNS: | ||
2514 | * %true if flush_work_sync() waited for the work to finish execution, | ||
2515 | * %false if it was already idle. | ||
2516 | */ | ||
2517 | bool flush_work_sync(struct work_struct *work) | ||
2518 | { | ||
2519 | struct wq_barrier barr; | ||
2520 | bool pending, waited; | ||
2521 | |||
2522 | /* we'll wait for executions separately, queue barr only if pending */ | ||
2523 | pending = start_flush_work(work, &barr, false); | ||
2524 | |||
2525 | /* wait for executions to finish */ | ||
2526 | waited = wait_on_work(work); | ||
2527 | |||
2528 | /* wait for the pending one */ | ||
2529 | if (pending) { | ||
2530 | wait_for_completion(&barr.done); | ||
2531 | destroy_work_on_stack(&barr.work); | ||
2532 | } | ||
2533 | |||
2534 | return pending || waited; | ||
2535 | } | ||
2536 | EXPORT_SYMBOL_GPL(flush_work_sync); | ||
2537 | |||
2384 | /* | 2538 | /* |
2385 | * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit, | 2539 | * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit, |
2386 | * so this work can't be re-armed in any way. | 2540 | * so this work can't be re-armed in any way. |
@@ -2423,39 +2577,7 @@ static int try_to_grab_pending(struct work_struct *work) | |||
2423 | return ret; | 2577 | return ret; |
2424 | } | 2578 | } |
2425 | 2579 | ||
2426 | static void wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work) | 2580 | static bool __cancel_work_timer(struct work_struct *work, |
2427 | { | ||
2428 | struct wq_barrier barr; | ||
2429 | struct worker *worker; | ||
2430 | |||
2431 | spin_lock_irq(&gcwq->lock); | ||
2432 | |||
2433 | worker = find_worker_executing_work(gcwq, work); | ||
2434 | if (unlikely(worker)) | ||
2435 | insert_wq_barrier(worker->current_cwq, &barr, work, worker); | ||
2436 | |||
2437 | spin_unlock_irq(&gcwq->lock); | ||
2438 | |||
2439 | if (unlikely(worker)) { | ||
2440 | wait_for_completion(&barr.done); | ||
2441 | destroy_work_on_stack(&barr.work); | ||
2442 | } | ||
2443 | } | ||
2444 | |||
2445 | static void wait_on_work(struct work_struct *work) | ||
2446 | { | ||
2447 | int cpu; | ||
2448 | |||
2449 | might_sleep(); | ||
2450 | |||
2451 | lock_map_acquire(&work->lockdep_map); | ||
2452 | lock_map_release(&work->lockdep_map); | ||
2453 | |||
2454 | for_each_gcwq_cpu(cpu) | ||
2455 | wait_on_cpu_work(get_gcwq(cpu), work); | ||
2456 | } | ||
2457 | |||
2458 | static int __cancel_work_timer(struct work_struct *work, | ||
2459 | struct timer_list* timer) | 2581 | struct timer_list* timer) |
2460 | { | 2582 | { |
2461 | int ret; | 2583 | int ret; |
@@ -2472,42 +2594,81 @@ static int __cancel_work_timer(struct work_struct *work, | |||
2472 | } | 2594 | } |
2473 | 2595 | ||
2474 | /** | 2596 | /** |
2475 | * cancel_work_sync - block until a work_struct's callback has terminated | 2597 | * cancel_work_sync - cancel a work and wait for it to finish |
2476 | * @work: the work which is to be flushed | 2598 | * @work: the work to cancel |
2477 | * | 2599 | * |
2478 | * Returns true if @work was pending. | 2600 | * Cancel @work and wait for its execution to finish. This function |
2479 | * | 2601 | * can be used even if the work re-queues itself or migrates to |
2480 | * cancel_work_sync() will cancel the work if it is queued. If the work's | 2602 | * another workqueue. On return from this function, @work is |
2481 | * callback appears to be running, cancel_work_sync() will block until it | 2603 | * guaranteed to be not pending or executing on any CPU. |
2482 | * has completed. | ||
2483 | * | ||
2484 | * It is possible to use this function if the work re-queues itself. It can | ||
2485 | * cancel the work even if it migrates to another workqueue, however in that | ||
2486 | * case it only guarantees that work->func() has completed on the last queued | ||
2487 | * workqueue. | ||
2488 | * | 2604 | * |
2489 | * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not | 2605 | * cancel_work_sync(&delayed_work->work) must not be used for |
2490 | * pending, otherwise it goes into a busy-wait loop until the timer expires. | 2606 | * delayed_work's. Use cancel_delayed_work_sync() instead. |
2491 | * | 2607 | * |
2492 | * The caller must ensure that workqueue_struct on which this work was last | 2608 | * The caller must ensure that the workqueue on which @work was last |
2493 | * queued can't be destroyed before this function returns. | 2609 | * queued can't be destroyed before this function returns. |
2610 | * | ||
2611 | * RETURNS: | ||
2612 | * %true if @work was pending, %false otherwise. | ||
2494 | */ | 2613 | */ |
2495 | int cancel_work_sync(struct work_struct *work) | 2614 | bool cancel_work_sync(struct work_struct *work) |
2496 | { | 2615 | { |
2497 | return __cancel_work_timer(work, NULL); | 2616 | return __cancel_work_timer(work, NULL); |
2498 | } | 2617 | } |
2499 | EXPORT_SYMBOL_GPL(cancel_work_sync); | 2618 | EXPORT_SYMBOL_GPL(cancel_work_sync); |
2500 | 2619 | ||
2501 | /** | 2620 | /** |
2502 | * cancel_delayed_work_sync - reliably kill off a delayed work. | 2621 | * flush_delayed_work - wait for a dwork to finish executing the last queueing |
2503 | * @dwork: the delayed work struct | 2622 | * @dwork: the delayed work to flush |
2623 | * | ||
2624 | * Delayed timer is cancelled and the pending work is queued for | ||
2625 | * immediate execution. Like flush_work(), this function only | ||
2626 | * considers the last queueing instance of @dwork. | ||
2627 | * | ||
2628 | * RETURNS: | ||
2629 | * %true if flush_work() waited for the work to finish execution, | ||
2630 | * %false if it was already idle. | ||
2631 | */ | ||
2632 | bool flush_delayed_work(struct delayed_work *dwork) | ||
2633 | { | ||
2634 | if (del_timer_sync(&dwork->timer)) | ||
2635 | __queue_work(raw_smp_processor_id(), | ||
2636 | get_work_cwq(&dwork->work)->wq, &dwork->work); | ||
2637 | return flush_work(&dwork->work); | ||
2638 | } | ||
2639 | EXPORT_SYMBOL(flush_delayed_work); | ||
2640 | |||
2641 | /** | ||
2642 | * flush_delayed_work_sync - wait for a dwork to finish | ||
2643 | * @dwork: the delayed work to flush | ||
2644 | * | ||
2645 | * Delayed timer is cancelled and the pending work is queued for | ||
2646 | * execution immediately. Other than timer handling, its behavior | ||
2647 | * is identical to flush_work_sync(). | ||
2648 | * | ||
2649 | * RETURNS: | ||
2650 | * %true if flush_work_sync() waited for the work to finish execution, | ||
2651 | * %false if it was already idle. | ||
2652 | */ | ||
2653 | bool flush_delayed_work_sync(struct delayed_work *dwork) | ||
2654 | { | ||
2655 | if (del_timer_sync(&dwork->timer)) | ||
2656 | __queue_work(raw_smp_processor_id(), | ||
2657 | get_work_cwq(&dwork->work)->wq, &dwork->work); | ||
2658 | return flush_work_sync(&dwork->work); | ||
2659 | } | ||
2660 | EXPORT_SYMBOL(flush_delayed_work_sync); | ||
2661 | |||
2662 | /** | ||
2663 | * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish | ||
2664 | * @dwork: the delayed work cancel | ||
2504 | * | 2665 | * |
2505 | * Returns true if @dwork was pending. | 2666 | * This is cancel_work_sync() for delayed works. |
2506 | * | 2667 | * |
2507 | * It is possible to use this function if @dwork rearms itself via queue_work() | 2668 | * RETURNS: |
2508 | * or queue_delayed_work(). See also the comment for cancel_work_sync(). | 2669 | * %true if @dwork was pending, %false otherwise. |
2509 | */ | 2670 | */ |
2510 | int cancel_delayed_work_sync(struct delayed_work *dwork) | 2671 | bool cancel_delayed_work_sync(struct delayed_work *dwork) |
2511 | { | 2672 | { |
2512 | return __cancel_work_timer(&dwork->work, &dwork->timer); | 2673 | return __cancel_work_timer(&dwork->work, &dwork->timer); |
2513 | } | 2674 | } |
@@ -2559,23 +2720,6 @@ int schedule_delayed_work(struct delayed_work *dwork, | |||
2559 | EXPORT_SYMBOL(schedule_delayed_work); | 2720 | EXPORT_SYMBOL(schedule_delayed_work); |
2560 | 2721 | ||
2561 | /** | 2722 | /** |
2562 | * flush_delayed_work - block until a dwork_struct's callback has terminated | ||
2563 | * @dwork: the delayed work which is to be flushed | ||
2564 | * | ||
2565 | * Any timeout is cancelled, and any pending work is run immediately. | ||
2566 | */ | ||
2567 | void flush_delayed_work(struct delayed_work *dwork) | ||
2568 | { | ||
2569 | if (del_timer_sync(&dwork->timer)) { | ||
2570 | __queue_work(get_cpu(), get_work_cwq(&dwork->work)->wq, | ||
2571 | &dwork->work); | ||
2572 | put_cpu(); | ||
2573 | } | ||
2574 | flush_work(&dwork->work); | ||
2575 | } | ||
2576 | EXPORT_SYMBOL(flush_delayed_work); | ||
2577 | |||
2578 | /** | ||
2579 | * schedule_delayed_work_on - queue work in global workqueue on CPU after delay | 2723 | * schedule_delayed_work_on - queue work in global workqueue on CPU after delay |
2580 | * @cpu: cpu to use | 2724 | * @cpu: cpu to use |
2581 | * @dwork: job to be done | 2725 | * @dwork: job to be done |
@@ -2592,13 +2736,15 @@ int schedule_delayed_work_on(int cpu, | |||
2592 | EXPORT_SYMBOL(schedule_delayed_work_on); | 2736 | EXPORT_SYMBOL(schedule_delayed_work_on); |
2593 | 2737 | ||
2594 | /** | 2738 | /** |
2595 | * schedule_on_each_cpu - call a function on each online CPU from keventd | 2739 | * schedule_on_each_cpu - execute a function synchronously on each online CPU |
2596 | * @func: the function to call | 2740 | * @func: the function to call |
2597 | * | 2741 | * |
2598 | * Returns zero on success. | 2742 | * schedule_on_each_cpu() executes @func on each online CPU using the |
2599 | * Returns -ve errno on failure. | 2743 | * system workqueue and blocks until all CPUs have completed. |
2600 | * | ||
2601 | * schedule_on_each_cpu() is very slow. | 2744 | * schedule_on_each_cpu() is very slow. |
2745 | * | ||
2746 | * RETURNS: | ||
2747 | * 0 on success, -errno on failure. | ||
2602 | */ | 2748 | */ |
2603 | int schedule_on_each_cpu(work_func_t func) | 2749 | int schedule_on_each_cpu(work_func_t func) |
2604 | { | 2750 | { |
@@ -2764,6 +2910,13 @@ struct workqueue_struct *__alloc_workqueue_key(const char *name, | |||
2764 | unsigned int cpu; | 2910 | unsigned int cpu; |
2765 | 2911 | ||
2766 | /* | 2912 | /* |
2913 | * Workqueues which may be used during memory reclaim should | ||
2914 | * have a rescuer to guarantee forward progress. | ||
2915 | */ | ||
2916 | if (flags & WQ_MEM_RECLAIM) | ||
2917 | flags |= WQ_RESCUER; | ||
2918 | |||
2919 | /* | ||
2767 | * Unbound workqueues aren't concurrency managed and should be | 2920 | * Unbound workqueues aren't concurrency managed and should be |
2768 | * dispatched to workers immediately. | 2921 | * dispatched to workers immediately. |
2769 | */ | 2922 | */ |
@@ -2828,7 +2981,7 @@ struct workqueue_struct *__alloc_workqueue_key(const char *name, | |||
2828 | */ | 2981 | */ |
2829 | spin_lock(&workqueue_lock); | 2982 | spin_lock(&workqueue_lock); |
2830 | 2983 | ||
2831 | if (workqueue_freezing && wq->flags & WQ_FREEZEABLE) | 2984 | if (workqueue_freezing && wq->flags & WQ_FREEZABLE) |
2832 | for_each_cwq_cpu(cpu, wq) | 2985 | for_each_cwq_cpu(cpu, wq) |
2833 | get_cwq(cpu, wq)->max_active = 0; | 2986 | get_cwq(cpu, wq)->max_active = 0; |
2834 | 2987 | ||
@@ -2856,11 +3009,35 @@ EXPORT_SYMBOL_GPL(__alloc_workqueue_key); | |||
2856 | */ | 3009 | */ |
2857 | void destroy_workqueue(struct workqueue_struct *wq) | 3010 | void destroy_workqueue(struct workqueue_struct *wq) |
2858 | { | 3011 | { |
3012 | unsigned int flush_cnt = 0; | ||
2859 | unsigned int cpu; | 3013 | unsigned int cpu; |
2860 | 3014 | ||
3015 | /* | ||
3016 | * Mark @wq dying and drain all pending works. Once WQ_DYING is | ||
3017 | * set, only chain queueing is allowed. IOW, only currently | ||
3018 | * pending or running work items on @wq can queue further work | ||
3019 | * items on it. @wq is flushed repeatedly until it becomes empty. | ||
3020 | * The number of flushing is detemined by the depth of chaining and | ||
3021 | * should be relatively short. Whine if it takes too long. | ||
3022 | */ | ||
2861 | wq->flags |= WQ_DYING; | 3023 | wq->flags |= WQ_DYING; |
3024 | reflush: | ||
2862 | flush_workqueue(wq); | 3025 | flush_workqueue(wq); |
2863 | 3026 | ||
3027 | for_each_cwq_cpu(cpu, wq) { | ||
3028 | struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); | ||
3029 | |||
3030 | if (!cwq->nr_active && list_empty(&cwq->delayed_works)) | ||
3031 | continue; | ||
3032 | |||
3033 | if (++flush_cnt == 10 || | ||
3034 | (flush_cnt % 100 == 0 && flush_cnt <= 1000)) | ||
3035 | printk(KERN_WARNING "workqueue %s: flush on " | ||
3036 | "destruction isn't complete after %u tries\n", | ||
3037 | wq->name, flush_cnt); | ||
3038 | goto reflush; | ||
3039 | } | ||
3040 | |||
2864 | /* | 3041 | /* |
2865 | * wq list is used to freeze wq, remove from list after | 3042 | * wq list is used to freeze wq, remove from list after |
2866 | * flushing is complete in case freeze races us. | 3043 | * flushing is complete in case freeze races us. |
@@ -2916,7 +3093,7 @@ void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) | |||
2916 | 3093 | ||
2917 | spin_lock_irq(&gcwq->lock); | 3094 | spin_lock_irq(&gcwq->lock); |
2918 | 3095 | ||
2919 | if (!(wq->flags & WQ_FREEZEABLE) || | 3096 | if (!(wq->flags & WQ_FREEZABLE) || |
2920 | !(gcwq->flags & GCWQ_FREEZING)) | 3097 | !(gcwq->flags & GCWQ_FREEZING)) |
2921 | get_cwq(gcwq->cpu, wq)->max_active = max_active; | 3098 | get_cwq(gcwq->cpu, wq)->max_active = max_active; |
2922 | 3099 | ||
@@ -3166,7 +3343,7 @@ static int __cpuinit trustee_thread(void *__gcwq) | |||
3166 | * want to get it over with ASAP - spam rescuers, wake up as | 3343 | * want to get it over with ASAP - spam rescuers, wake up as |
3167 | * many idlers as necessary and create new ones till the | 3344 | * many idlers as necessary and create new ones till the |
3168 | * worklist is empty. Note that if the gcwq is frozen, there | 3345 | * worklist is empty. Note that if the gcwq is frozen, there |
3169 | * may be frozen works in freezeable cwqs. Don't declare | 3346 | * may be frozen works in freezable cwqs. Don't declare |
3170 | * completion while frozen. | 3347 | * completion while frozen. |
3171 | */ | 3348 | */ |
3172 | while (gcwq->nr_workers != gcwq->nr_idle || | 3349 | while (gcwq->nr_workers != gcwq->nr_idle || |
@@ -3424,9 +3601,9 @@ EXPORT_SYMBOL_GPL(work_on_cpu); | |||
3424 | /** | 3601 | /** |
3425 | * freeze_workqueues_begin - begin freezing workqueues | 3602 | * freeze_workqueues_begin - begin freezing workqueues |
3426 | * | 3603 | * |
3427 | * Start freezing workqueues. After this function returns, all | 3604 | * Start freezing workqueues. After this function returns, all freezable |
3428 | * freezeable workqueues will queue new works to their frozen_works | 3605 | * workqueues will queue new works to their frozen_works list instead of |
3429 | * list instead of gcwq->worklist. | 3606 | * gcwq->worklist. |
3430 | * | 3607 | * |
3431 | * CONTEXT: | 3608 | * CONTEXT: |
3432 | * Grabs and releases workqueue_lock and gcwq->lock's. | 3609 | * Grabs and releases workqueue_lock and gcwq->lock's. |
@@ -3452,7 +3629,7 @@ void freeze_workqueues_begin(void) | |||
3452 | list_for_each_entry(wq, &workqueues, list) { | 3629 | list_for_each_entry(wq, &workqueues, list) { |
3453 | struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); | 3630 | struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); |
3454 | 3631 | ||
3455 | if (cwq && wq->flags & WQ_FREEZEABLE) | 3632 | if (cwq && wq->flags & WQ_FREEZABLE) |
3456 | cwq->max_active = 0; | 3633 | cwq->max_active = 0; |
3457 | } | 3634 | } |
3458 | 3635 | ||
@@ -3463,7 +3640,7 @@ void freeze_workqueues_begin(void) | |||
3463 | } | 3640 | } |
3464 | 3641 | ||
3465 | /** | 3642 | /** |
3466 | * freeze_workqueues_busy - are freezeable workqueues still busy? | 3643 | * freeze_workqueues_busy - are freezable workqueues still busy? |
3467 | * | 3644 | * |
3468 | * Check whether freezing is complete. This function must be called | 3645 | * Check whether freezing is complete. This function must be called |
3469 | * between freeze_workqueues_begin() and thaw_workqueues(). | 3646 | * between freeze_workqueues_begin() and thaw_workqueues(). |
@@ -3472,8 +3649,8 @@ void freeze_workqueues_begin(void) | |||
3472 | * Grabs and releases workqueue_lock. | 3649 | * Grabs and releases workqueue_lock. |
3473 | * | 3650 | * |
3474 | * RETURNS: | 3651 | * RETURNS: |
3475 | * %true if some freezeable workqueues are still busy. %false if | 3652 | * %true if some freezable workqueues are still busy. %false if freezing |
3476 | * freezing is complete. | 3653 | * is complete. |
3477 | */ | 3654 | */ |
3478 | bool freeze_workqueues_busy(void) | 3655 | bool freeze_workqueues_busy(void) |
3479 | { | 3656 | { |
@@ -3493,7 +3670,7 @@ bool freeze_workqueues_busy(void) | |||
3493 | list_for_each_entry(wq, &workqueues, list) { | 3670 | list_for_each_entry(wq, &workqueues, list) { |
3494 | struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); | 3671 | struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); |
3495 | 3672 | ||
3496 | if (!cwq || !(wq->flags & WQ_FREEZEABLE)) | 3673 | if (!cwq || !(wq->flags & WQ_FREEZABLE)) |
3497 | continue; | 3674 | continue; |
3498 | 3675 | ||
3499 | BUG_ON(cwq->nr_active < 0); | 3676 | BUG_ON(cwq->nr_active < 0); |
@@ -3538,7 +3715,7 @@ void thaw_workqueues(void) | |||
3538 | list_for_each_entry(wq, &workqueues, list) { | 3715 | list_for_each_entry(wq, &workqueues, list) { |
3539 | struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); | 3716 | struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); |
3540 | 3717 | ||
3541 | if (!cwq || !(wq->flags & WQ_FREEZEABLE)) | 3718 | if (!cwq || !(wq->flags & WQ_FREEZABLE)) |
3542 | continue; | 3719 | continue; |
3543 | 3720 | ||
3544 | /* restore max_active and repopulate worklist */ | 3721 | /* restore max_active and repopulate worklist */ |
@@ -3612,7 +3789,10 @@ static int __init init_workqueues(void) | |||
3612 | system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0); | 3789 | system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0); |
3613 | system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, | 3790 | system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, |
3614 | WQ_UNBOUND_MAX_ACTIVE); | 3791 | WQ_UNBOUND_MAX_ACTIVE); |
3615 | BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq); | 3792 | system_freezable_wq = alloc_workqueue("events_freezable", |
3793 | WQ_FREEZABLE, 0); | ||
3794 | BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq || | ||
3795 | !system_unbound_wq || !system_freezable_wq); | ||
3616 | return 0; | 3796 | return 0; |
3617 | } | 3797 | } |
3618 | early_initcall(init_workqueues); | 3798 | early_initcall(init_workqueues); |