linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4] stop_machine: Disable preemption after queueing stopper threads
@ 2018-07-17 16:30 Isaac J. Manjarres
  2018-07-17 18:34 ` Peter Zijlstra
  0 siblings, 1 reply; 3+ messages in thread
From: Isaac J. Manjarres @ 2018-07-17 16:30 UTC (permalink / raw)
  To: peterz, matt, mingo, tglx, bigeasy
  Cc: Isaac J. Manjarres, linux-kernel, psodagud, gregkh, pkondeti, stable

After cpu_stop_queue_two_works() queues the cpu_stop works
for the stopper threads, it releases the locks held for
both threads, which enables preemption, which allows the
following race condition to occur:

On one CPU, call it CPU 3, thread 1 invokes
cpu_stop_queue_two_works(2, 3,...), and the execution is such
that thread 1 queues the works for migration/2 and migration/3,
and is preempted after releasing the locks for migration/2 and
migration/3, but before waking the threads.

Then, On CPU 2, a kworker, call it thread 2, is running,
and it invokes cpu_stop_queue_two_works(1, 2,...), such that
thread 2 queues the works for migration/1 and migration/2.
Meanwhile, on CPU 3, thread 1 resumes execution, and wakes
migration/2 and migration/3. This means that when CPU 2
releases the locks for migration/1 and migration/2, but before
it wakes those threads, it can be preempted by migration/2.

If thread 2 is preempted by migration/2, then migration/2 will
execute the first work item successfully, since migration/3
was woken up by CPU 3, but when it goes to execute the second
work item, it disables preemption, calls multi_cpu_stop(),
and thus, CPU 2 will wait forever for migration/1, which should
have been woken up by thread 2. However migration/1 cannot be
woken up by thread 2, since it is a kworker, so it is affine to
CPU 2, but CPU 2 is running migration/2 with preemption
disabled, so thread 2 will never run.

Disable preemption after queueing works for stopper threads
to ensure that the operation of queueing the works and waking
the stopper threads is atomic.

Fixes: 0b26351b910f ("stop_machine, sched: Fix migrate_swap() vs. active_balance() deadlock")
Co-Developed-by: Prasad Sodagudi <psodagud@codeaurora.org>
Co-Developed-by: Pavankumar Kondeti <pkondeti@codeaurora.org>
Signed-off-by: Isaac J. Manjarres <isaacm@codeaurora.org>
Signed-off-by: Prasad Sodagudi <psodagud@codeaurora.org>
Signed-off-by: Pavankumar Kondeti <pkondeti@codeaurora.org>
Cc: stable@vger.kernel.org
---
 kernel/stop_machine.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/kernel/stop_machine.c b/kernel/stop_machine.c
index f89014a..e190d1e 100644
--- a/kernel/stop_machine.c
+++ b/kernel/stop_machine.c
@@ -260,6 +260,15 @@ static int cpu_stop_queue_two_works(int cpu1, struct cpu_stop_work *work1,
 	err = 0;
 	__cpu_stop_queue_work(stopper1, work1, &wakeq);
 	__cpu_stop_queue_work(stopper2, work2, &wakeq);
+	/*
+	 * The waking up of stopper threads has to happen
+	 * in the same scheduling context as the queueing.
+	 * Otherwise, there is a possibility of one of the
+	 * above stoppers being woken up by another CPU,
+	 * and preempting us. This will cause us to n ot
+	 * wake up the other stopper forever.
+	 */
+	preempt_disable();
 unlock:
 	raw_spin_unlock(&stopper2->lock);
 	raw_spin_unlock_irq(&stopper1->lock);
@@ -270,7 +279,10 @@ static int cpu_stop_queue_two_works(int cpu1, struct cpu_stop_work *work1,
 		goto retry;
 	}
 
-	wake_up_q(&wakeq);
+	if (!err) {
+		wake_up_q(&wakeq);
+		preempt_enable();
+	}
 
 	return err;
 }
-- 
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v4] stop_machine: Disable preemption after queueing stopper threads
  2018-07-17 16:30 [PATCH v4] stop_machine: Disable preemption after queueing stopper threads Isaac J. Manjarres
@ 2018-07-17 18:34 ` Peter Zijlstra
  2018-07-17 19:34   ` isaacm
  0 siblings, 1 reply; 3+ messages in thread
From: Peter Zijlstra @ 2018-07-17 18:34 UTC (permalink / raw)
  To: Isaac J. Manjarres
  Cc: matt, mingo, tglx, bigeasy, linux-kernel, psodagud, gregkh,
	pkondeti, stable

On Tue, Jul 17, 2018 at 09:30:17AM -0700, Isaac J. Manjarres wrote:
> After cpu_stop_queue_two_works() queues the cpu_stop works
> for the stopper threads, it releases the locks held for
> both threads, which enables preemption, which allows the
> following race condition to occur:


Does not apply because an earlier version is already applied.

https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git/commit/kernel/stop_machine.c?id=9fb8d5dc4b649dd190e1af4ead670753e71bf907

if there still is a problem, please send a new patch that describes the
remaining problem.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v4] stop_machine: Disable preemption after queueing stopper threads
  2018-07-17 18:34 ` Peter Zijlstra
@ 2018-07-17 19:34   ` isaacm
  0 siblings, 0 replies; 3+ messages in thread
From: isaacm @ 2018-07-17 19:34 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: matt, mingo, tglx, bigeasy, linux-kernel, psodagud, gregkh,
	pkondeti, stable

Alright, we will make a new patch for this. Thanks for the feedback.

Thanks,
Isaac Manjarres
On 2018-07-17 11:34, Peter Zijlstra wrote:
> On Tue, Jul 17, 2018 at 09:30:17AM -0700, Isaac J. Manjarres wrote:
>> After cpu_stop_queue_two_works() queues the cpu_stop works
>> for the stopper threads, it releases the locks held for
>> both threads, which enables preemption, which allows the
>> following race condition to occur:
> 
> 
> Does not apply because an earlier version is already applied.
> 
> https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git/commit/kernel/stop_machine.c?id=9fb8d5dc4b649dd190e1af4ead670753e71bf907
> 
> if there still is a problem, please send a new patch that describes the
> remaining problem.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-07-17 19:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-17 16:30 [PATCH v4] stop_machine: Disable preemption after queueing stopper threads Isaac J. Manjarres
2018-07-17 18:34 ` Peter Zijlstra
2018-07-17 19:34   ` isaacm

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).