From: Lai Jiangshan <laijs@cn.fujitsu.com>
To: Tejun Heo <tj@kernel.org>, <linux-kernel@vger.kernel.org>
Cc: Lai Jiangshan <laijs@cn.fujitsu.com>
Subject: [PATCH 1/2 V4] workqueue: rescuer_thread() processes all pwqs before exit
Date: Fri, 18 Apr 2014 21:25:54 +0800 [thread overview]
Message-ID: <1397827555-19641-1-git-send-email-laijs@cn.fujitsu.com> (raw)
In-Reply-To: <20140417162720.GQ15326@htj.dyndns.org>
Before the rescuer is picked to running, the works of the @pwq
may be processed by some other workers, and destroy_workqueue()
may called at the same time. This may result a nasty situation
that rescuer may exit with non-empty mayday list.
It is no harm currently, destroy_workqueue() can safely to free
them all(workqueue&pwqs) togerther, since the rescuer is stopped.
No rescuer nor mayday-timer can access the mayday list.
But it is nasty and error-prone in future development. Fix it.
Signed-off-by: Lai Jiangshan <laijs@cn.fujitsu.com>
---
kernel/workqueue.c | 21 +++++++++++++++------
1 files changed, 15 insertions(+), 6 deletions(-)
diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 0ee63af..7539244 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -2398,6 +2398,7 @@ static int rescuer_thread(void *__rescuer)
struct worker *rescuer = __rescuer;
struct workqueue_struct *wq = rescuer->rescue_wq;
struct list_head *scheduled = &rescuer->scheduled;
+ bool should_stop;
set_user_nice(current, RESCUER_NICE_LEVEL);
@@ -2408,12 +2409,14 @@ static int rescuer_thread(void *__rescuer)
rescuer->task->flags |= PF_WQ_WORKER;
repeat:
set_current_state(TASK_INTERRUPTIBLE);
-
- if (kthread_should_stop()) {
- __set_current_state(TASK_RUNNING);
- rescuer->task->flags &= ~PF_WQ_WORKER;
- return 0;
- }
+ /*
+ * When the rescuer is requested to stop, the workqueue has no
+ * work pending, but wq->maydays may still have pwq(s) queued.
+ * This can happend when some other workers process all works
+ * before this rescuer is scheduled. The rescuer must process
+ * all pwq(s) before exit.
+ */
+ should_stop = kthread_should_stop();
/* see whether any pwq is asking for help */
spin_lock_irq(&wq_mayday_lock);
@@ -2459,6 +2462,12 @@ repeat:
spin_unlock_irq(&wq_mayday_lock);
+ if (should_stop) {
+ __set_current_state(TASK_RUNNING);
+ rescuer->task->flags &= ~PF_WQ_WORKER;
+ return 0;
+ }
+
/* rescuers should never participate in concurrency management */
WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
schedule();
--
1.7.4.4
next prev parent reply other threads:[~2014-04-18 13:23 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-03-27 16:20 [PATCH] workqueue: fix possible race condition when rescuer VS pwq-release Lai Jiangshan
2014-03-28 12:07 ` [PATCH V2] " Lai Jiangshan
2014-03-31 14:40 ` Lai Jiangshan
2014-03-31 20:06 ` Tejun Heo
2014-04-14 7:02 ` Lai Jiangshan
2014-04-15 16:47 ` Tejun Heo
2014-04-16 1:25 ` Lai Jiangshan
2014-04-16 15:23 ` Tejun Heo
2014-04-16 16:21 ` Lai Jiangshan
2014-04-16 16:50 ` Tejun Heo
2014-04-16 22:35 ` Lai Jiangshan
2014-04-16 23:34 ` [PATCH 1/2] workqueue: rescuer_thread() processes all pwqs before exit Lai Jiangshan
2014-04-16 23:34 ` [PATCH 2/2] workqueue: fix possible race condition when rescuer VS pwq-release Lai Jiangshan
2014-04-17 15:27 ` [PATCH 1/2] workqueue: rescuer_thread() processes all pwqs before exit Tejun Heo
2014-04-17 16:04 ` Lai Jiangshan
2014-04-17 16:08 ` Tejun Heo
2014-04-17 16:21 ` Lai Jiangshan
2014-04-17 16:27 ` Tejun Heo
2014-04-18 13:25 ` Lai Jiangshan [this message]
2014-04-18 13:25 ` [PATCH 2/2 V4] workqueue: fix possible race condition when rescuer VS pwq-release Lai Jiangshan
2014-04-18 15:06 ` [PATCH 2/2] workqueue: fix a possible race condition between rescuer and pwq-release Tejun Heo
2014-04-18 16:24 ` Lai Jiangshan
2014-04-18 16:35 ` Tejun Heo
2014-04-18 15:06 ` [PATCH 1/2] workqueue: make rescuer_thread() empty wq->maydays list before exiting Tejun Heo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1397827555-19641-1-git-send-email-laijs@cn.fujitsu.com \
--to=laijs@cn.fujitsu.com \
--cc=linux-kernel@vger.kernel.org \
--cc=tj@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®