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.

Signed-off-by: Tejun Heo <tj@kernel.org>
Reported-by: Thilo-Alexander Ginkel <thilo@ginkel.com>
Tested-by: Thilo-Alexander Ginkel <thilo@ginkel.com>
Cc: stable@kernel.org

Tejun Heo 5035b20f e2de9e08

+7 -1
+7 -1
kernel/workqueue.c
··· 1291 1291 return true; 1292 1292 spin_unlock_irq(&gcwq->lock); 1293 1293 1294 - /* CPU has come up inbetween, retry migration */ 1294 + /* 1295 + * We've raced with CPU hot[un]plug. Give it a breather 1296 + * and retry migration. cond_resched() is required here; 1297 + * otherwise, we might deadlock against cpu_stop trying to 1298 + * bring down the CPU on non-preemptive kernel. 1299 + */ 1295 1300 cpu_relax(); 1301 + cond_resched(); 1296 1302 } 1297 1303 } 1298 1304