mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 4/7 V6] workqueue: fix idle worker depletion
Date: Sun, 9 Sep 2012 01:12:53 +0800	[thread overview]
Message-ID: <1347124383-18723-5-git-send-email-laijs@cn.fujitsu.com> (raw)
In-Reply-To: <1347124383-18723-1-git-send-email-laijs@cn.fujitsu.com>

If hotplug code grabbed the manager_mutex and worker_thread try to create
a worker, the manage_worker() will return false and worker_thread go to
process work items. Now, on the CPU, all workers are processing work items,
no idle_worker left/ready for managing. It breaks the concept of workqueue
and it is bug.

So when manage_worker() failed to grab the manager_mutex, it should
release gcwq->lock and try again.

After gcwq->lock is released, hotplug can happen. gcwq_unbind_fn() will
do the right thing for manager via ->manager. But rebind_workers()
can't rebind workers directly, worker rebind itself when it is noticed.

Manager worker will be noticed by the bit of GCWQ_DISASSOCIATED and
WORKER_UNBIND. Because the %UNBOUND bit of manager can't be cleared
while it is managing workers. maybe_rebind_manager() will be noticed
when rebind_workers() happens.

Signed-off-by: Lai Jiangshan <laijs@cn.fujitsu.com>
---
 kernel/workqueue.c |   33 ++++++++++++++++++++++++++++++++-
 1 files changed, 32 insertions(+), 1 deletions(-)

diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index b203806..207b6a1 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -2039,6 +2039,20 @@ static bool maybe_destroy_workers(struct worker_pool *pool)
 	return ret;
 }
 
+/* does the manager need to be rebind after we just release gcwq->lock */
+static void maybe_rebind_manager(struct worker *manager)
+{
+	struct global_cwq *gcwq = manager->pool->gcwq;
+	bool assoc = !(gcwq->flags & GCWQ_DISASSOCIATED);
+
+	if (assoc && (manager->flags & WORKER_UNBOUND)) {
+		spin_unlock_irq(&gcwq->lock);
+
+		if (worker_maybe_bind_and_lock(manager))
+			worker_clr_flags(manager, WORKER_UNBOUND);
+	}
+}
+
 /**
  * manage_workers - manage worker pool
  * @worker: self
@@ -2062,12 +2076,29 @@ static bool maybe_destroy_workers(struct worker_pool *pool)
 static bool manage_workers(struct worker *worker)
 {
 	struct worker_pool *pool = worker->pool;
+	struct global_cwq *gcwq = pool->gcwq;
 	bool ret = false;
 
-	if (!mutex_trylock(&pool->manager_mutex))
+	if (pool->manager)
 		return ret;
 
 	pool->manager = worker;
+	if (unlikely(!mutex_trylock(&pool->manager_mutex))) {
+		/*
+		 * Ouch! rebind_workers() or gcwq_unbind_fn() beats we.
+		 * it can't return false here, otherwise it will lead to
+		 * worker depletion. So we release gcwq->lock and then
+		 * grab manager_mutex again.
+		 */
+		spin_unlock_irq(&gcwq->lock);
+		mutex_lock(&pool->manager_mutex);
+		spin_lock_irq(&gcwq->lock);
+
+		/* rebind_workers() can happen when we release gcwq->lock */
+		maybe_rebind_manager(worker);
+		ret = true;
+	}
+
 	pool->flags &= ~POOL_MANAGE_WORKERS;
 
 	/*
-- 
1.7.4.4


  parent reply	other threads:[~2012-09-08 17:11 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-09-08 17:12 [PATCH 0/7 V6] workqueue: fix hoplug things Lai Jiangshan
2012-09-08 17:12 ` [PATCH 1/7 V6] workqueue: ensure the wq_worker_sleeping() see the right flags Lai Jiangshan
2012-09-08 17:12 ` [PATCH 2/7 V6] workqueue: async idle rebinding Lai Jiangshan
2012-09-08 17:12 ` [PATCH 3/7 V6] workqueue: add manager pointer for worker_pool Lai Jiangshan
2012-09-08 17:12 ` Lai Jiangshan [this message]
2012-09-08 17:40   ` [PATCH 4/7 V6] workqueue: fix idle worker depletion Tejun Heo
2012-09-08 17:50     ` Lai Jiangshan
2012-09-08 17:53       ` Tejun Heo
2012-09-08 18:07         ` Lai Jiangshan
2012-09-08 18:11           ` Tejun Heo
2012-09-08 18:34             ` Lai Jiangshan
2012-09-08 19:02               ` Tejun Heo
2012-09-09  4:09                 ` Lai Jiangshan
2012-09-09  6:30                   ` Tejun Heo
2012-09-08 17:12 ` [PATCH 5/7 V6] workqueue: rename manager_mutex to assoc_mutex Lai Jiangshan
2012-09-08 17:12 ` [PATCH 6/7 V6] workqueue: new day don't need WORKER_REBIND for busy rebinding Lai Jiangshan
2012-09-08 17:12 ` [PATCH 7/7 V6] workqueue: remove WORKER_REBIND Lai Jiangshan
2012-09-08 17:27 ` [PATCH 0/7 V6] workqueue: fix hoplug things Lai Jiangshan
2012-09-08 17:34   ` Lai Jiangshan
2012-09-08 17:37   ` Tejun Heo
2012-09-08 17:46     ` Lai Jiangshan
2012-09-08 17:50       ` Tejun Heo
2012-09-08 17:51         ` Tejun Heo
2012-09-08 17:56         ` Lai Jiangshan
2012-09-08 17:59           ` Tejun Heo
2012-09-08 18:21             ` Lai Jiangshan

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=1347124383-18723-5-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®