Skip to contentSkip to conversation

workqueue: Don't spin forever in worker_maybe_bind_and_lock

PATCH In review Paul Mackerras 2 participants 2 messages May 4, 2011 Workqueue

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 workqueue: Don't spin forever in worker_maybe_bind_and_lock

On a 48-thread POWER7 box, I often see the system hang when offlining processors. What happens is that we get a rescuer thread trying to move to some processor at the same time that a cpu offline operation is happening for that processor, and we end up with one cpu spinning in worker_maybe_bind_and_lock() and all of the rest of the online cpus spinning inside the stop_machine code. The rescuer thread is continually calling set_cpus_allowed_ptr() which is continually failing because the cpu it is trying to move to is no longer in the cpu_active_mask. The result is a deadlock.

This fixes worker_maybe_bind_and_lock so that it stops trying to move to a cpu if that cpu is no longer in the cpu_active_mask, and instead returns to its caller. With this I no longer see the deadlocks when offlining cpus.

---
 kernel/workqueue.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
kernel/workqueue.c +2 -0
index 8859a41..12faf78 100644
@@ -1289,6 +1289,8 @@ __acquires(&gcwq->lock)
 		    cpumask_equal(&current->cpus_allowed,
 				  get_cpu_mask(gcwq->cpu)))
 			return true;
+		if (!cpumask_test_cpu(gcwq->cpu, cpu_active_mask))
+			return false;
 		spin_unlock_irq(&gcwq->lock);
 
 		/* CPU has come up in between, retry migration */
Signature · 1 line
1.7.4.1
maintainer Tested-by Thilo-Alexander Ginkel on v1 1/1
#1 lore raw
Quoted 10 lines · Paul Mackerras

On a 48-thread POWER7 box, I often see the system hang when offlining processors. What happens is that we get a rescuer thread trying to move to some processor at the same time that a cpu offline operation is happening for that processor, and we end up with one cpu spinning in worker_maybe_bind_and_lock() and all of the rest of the online cpus spinning inside the stop_machine code. The rescuer thread is continually calling set_cpus_allowed_ptr() which is continually failing because the cpu it is trying to move to is no longer in the cpu_active_mask. The result is a deadlock.

Quoted 6 lines

This fixes worker_maybe_bind_and_lock so that it stops trying to move to a cpu if that cpu is no longer in the cpu_active_mask, and instead returns to its caller. With this I no longer see the deadlocks when offlining cpus.

Hmm.. fix for the problem has already been merged into mainline and scheduled for -stable. Can you please verify the following fixes the problem?

Thank you.

Quoted 1 line

From 5035b20fa5cd146b66f5f89619c20a4177fb736d Mon Sep 17 00:00:00 2001

From: Tejun Heo <[email protected]>
Date: Fri, 29 Apr 2011 18:08:37 +0200
Subject: [PATCH] workqueue: fix deadlock in worker_maybe_bind_and_lock()

If a rescuer and stop_machine() bringing down a CPU race with each other, they may deadlock on non-preemptive kernel. The CPU won't accept a new task, so the rescuer can't migrate to the target CPU, while stop_machine() can't proceed because the rescuer is holding one of the CPU retrying migration. GCWQ_DISASSOCIATED is never cleared and worker_maybe_bind_and_lock() retries indefinitely.

This problem can be reproduced semi reliably while the system is entering suspend.

http://thread.gmane.org/gmane.linux.kernel/1122051

A lot of kudos to Thilo-Alexander for reporting this tricky issue and painstaking testing.

stable: This affects all kernels with cmwq, so all kernels since and
including v2.6.36 need this fix.

---
 kernel/workqueue.c |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)
kernel/workqueue.c +7 -1
index 04ef830..e3378e8 100644
@@ -1291,8 +1291,14 @@ __acquires(&gcwq->lock)
 			return true;
 		spin_unlock_irq(&gcwq->lock);
 
-		/* CPU has come up inbetween, retry migration */
+		/*
+		 * We've raced with CPU hot[un]plug.  Give it a breather
+		 * and retry migration.  cond_resched() is required here;
+		 * otherwise, we might deadlock against cpu_stop trying to
+		 * bring down the CPU on non-preemptive kernel.
+		 */
 		cpu_relax();
+		cond_resched();
 	}
 }
 
Signature · 1 line
1.7.1