]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - releases/3.10.42/workqueue-make-rescuer_thread-empty-wq-maydays-list-before-exiting.patch
drop queue-4.14/mips-make-sure-dt-memory-regions-are-valid.patch
[thirdparty/kernel/stable-queue.git] / releases / 3.10.42 / workqueue-make-rescuer_thread-empty-wq-maydays-list-before-exiting.patch
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
5
6 From: Lai Jiangshan <laijs@cn.fujitsu.com>
7
8 commit 4d595b866d2c653dc90a492b9973a834eabfa354 upstream.
9
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.
16
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.
20
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.
24
25 tj: Updated comment and patch description.
26
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>
30
31 ---
32 kernel/workqueue.c | 21 ++++++++++++++++-----
33 1 file changed, 16 insertions(+), 5 deletions(-)
34
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;
41 + bool should_stop;
42
43 set_user_nice(current, RESCUER_NICE_LEVEL);
44
45 @@ -2373,11 +2374,15 @@ static int rescuer_thread(void *__rescue
46 repeat:
47 set_current_state(TASK_INTERRUPTIBLE);
48
49 - if (kthread_should_stop()) {
50 - __set_current_state(TASK_RUNNING);
51 - rescuer->task->flags &= ~PF_WQ_WORKER;
52 - return 0;
53 - }
54 + /*
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.
61 + */
62 + should_stop = kthread_should_stop();
63
64 /* see whether any pwq is asking for help */
65 spin_lock_irq(&wq_mayday_lock);
66 @@ -2429,6 +2434,12 @@ repeat:
67
68 spin_unlock_irq(&wq_mayday_lock);
69
70 + if (should_stop) {
71 + __set_current_state(TASK_RUNNING);
72 + rescuer->task->flags &= ~PF_WQ_WORKER;
73 + return 0;
74 + }
75 +
76 /* rescuers should never participate in concurrency management */
77 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
78 schedule();