1 From 4d595b866d2c653dc90a492b9973a834eabfa354 Mon Sep 17 00:00:00 2001
2 From: Lai Jiangshan <laijs@cn.fujitsu.com>
3 Date: Fri, 18 Apr 2014 11:04:16 -0400
4 Subject: workqueue: make rescuer_thread() empty wq->maydays list before exiting
6 From: Lai Jiangshan <laijs@cn.fujitsu.com>
8 commit 4d595b866d2c653dc90a492b9973a834eabfa354 upstream.
10 After a @pwq is scheduled for emergency execution, other workers may
11 consume the affectd work items before the rescuer gets to them. This
12 means that a workqueue many have pwqs queued on @wq->maydays list
13 while not having any work item pending or in-flight. If
14 destroy_workqueue() executes in such condition, the rescuer may exit
15 without emptying @wq->maydays.
17 This currently doesn't cause any actual harm. destroy_workqueue() can
18 safely destroy all the involved data structures whether @wq->maydays
19 is populated or not as nobody access the list once the rescuer exits.
21 However, this is nasty and makes future development difficult. Let's
22 update rescuer_thread() so that it empties @wq->maydays after seeing
23 should_stop to guarantee that the list is empty on rescuer exit.
25 tj: Updated comment and patch description.
27 Signed-off-by: Lai Jiangshan <laijs@cn.fujitsu.com>
28 Signed-off-by: Tejun Heo <tj@kernel.org>
29 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
32 kernel/workqueue.c | 21 ++++++++++++++++-----
33 1 file changed, 16 insertions(+), 5 deletions(-)
35 --- a/kernel/workqueue.c
36 +++ b/kernel/workqueue.c
37 @@ -2362,6 +2362,7 @@ static int rescuer_thread(void *__rescue
38 struct worker *rescuer = __rescuer;
39 struct workqueue_struct *wq = rescuer->rescue_wq;
40 struct list_head *scheduled = &rescuer->scheduled;
43 set_user_nice(current, RESCUER_NICE_LEVEL);
45 @@ -2373,11 +2374,15 @@ static int rescuer_thread(void *__rescue
47 set_current_state(TASK_INTERRUPTIBLE);
49 - if (kthread_should_stop()) {
50 - __set_current_state(TASK_RUNNING);
51 - rescuer->task->flags &= ~PF_WQ_WORKER;
55 + * By the time the rescuer is requested to stop, the workqueue
56 + * shouldn't have any work pending, but @wq->maydays may still have
57 + * pwq(s) queued. This can happen by non-rescuer workers consuming
58 + * all the work items before the rescuer got to them. Go through
59 + * @wq->maydays processing before acting on should_stop so that the
60 + * list is always empty on exit.
62 + should_stop = kthread_should_stop();
64 /* see whether any pwq is asking for help */
65 spin_lock_irq(&wq_mayday_lock);
66 @@ -2429,6 +2434,12 @@ repeat:
68 spin_unlock_irq(&wq_mayday_lock);
71 + __set_current_state(TASK_RUNNING);
72 + rescuer->task->flags &= ~PF_WQ_WORKER;
76 /* rescuers should never participate in concurrency management */
77 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));