Commit ae93e478 authored by Peter Zijlstra's avatar Peter Zijlstra Committed by Khalid Elmously

stop_machine, sched: Fix migrate_swap() vs. active_balance() deadlock

BugLink: https://bugs.launchpad.net/bugs/1821259

Matt reported the following deadlock:

CPU0					CPU1

schedule(.prev=migrate/0)		<fault>
  pick_next_task()			  ...
    idle_balance()			    migrate_swap()
      active_balance()			      stop_two_cpus()
						spin_lock(stopper0->lock)
						spin_lock(stopper1->lock)
						ttwu(migrate/0)
						  smp_cond_load_acquire() -- waits for schedule()
        stop_one_cpu(1)
	  spin_lock(stopper1->lock) -- waits for stopper lock

Fix this deadlock by taking the wakeups out from under stopper->lock.
This allows the active_balance() to queue the stop work and finish the
context switch, which in turn allows the wakeup from migrate_swap() to
observe the context and complete the wakeup.
Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
Reported-by: default avatarMatt Fleming <matt@codeblueprint.co.uk>
Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: default avatarMatt Fleming <matt@codeblueprint.co.uk>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Mike Galbraith <umgwanakikbuti@gmail.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: http://lkml.kernel.org/r/20180420095005.GH4064@hirez.programming.kicks-ass.netSigned-off-by: default avatarIngo Molnar <mingo@kernel.org>
(backported from commit 0b26351b)
[mfo: backport:
 - hunk 1:
   - refresh context lines
   - include 'linux/sched.h' instead of 'linux/sched/wake_q.h' which is
     code moved by upstream commit eb61baf6 ("sched/headers: Move the
     wake-queue types and interfaces from sched.h into <linux/sched/wake_q.h>")
 - hunk 2:
   - refresh context lines
   - s/bool/void/ return type of cpu_stop_queue_work() and other 2 changes
     which are not relevant to this fix, due to lack of upstream commits:
     - 'enabled' variable:
       commit 1b034bd9 ("stop_machine: Make cpu_stop_queue_work() and
       stop_one_cpu_nowait() return bool")
     - else 'if (work->done)' condition
       commit dd2e3121 ("stop_machine: Shift the 'done != NULL' check
       from cpu_stop_signal_done() to callers")
   - s/DEFINE_WAKE_Q/WAKE_Q/ due to lack of upstream commit 194a6b5b
     ("sched/wake_q: Rename WAKE_Q to DEFINE_WAKE_Q")
 - hunk 3:
   - refresh context lines
   - s/DEFINE_WAKE_Q/WAKE_Q/ due to lack of upstream commit 194a6b5b
     ("sched/wake_q: Rename WAKE_Q to DEFINE_WAKE_Q")
 - hunk 4:
   - refresh context lines
 - hunk 5:
   - merge with hunk 4
   - refresh context lines]
Signed-off-by: default avatarMauricio Faria de Oliveira <mfo@canonical.com>
Acked-by: default avatarKhalid Elmously <khalid.elmously@canonical.com>
Acked-by: default avatarMarcelo Henrique Cerri <marcelo.cerri@canonical.com>
Signed-off-by: default avatarKhalid Elmously <khalid.elmously@canonical.com>
parent 6fc830eb
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include <linux/smpboot.h> #include <linux/smpboot.h>
#include <linux/atomic.h> #include <linux/atomic.h>
#include <linux/lglock.h> #include <linux/lglock.h>
#include <linux/sched.h>
/* /*
* Structure to determine completion condition and record errors. May * Structure to determine completion condition and record errors. May
...@@ -74,24 +75,28 @@ static void cpu_stop_signal_done(struct cpu_stop_done *done, bool executed) ...@@ -74,24 +75,28 @@ static void cpu_stop_signal_done(struct cpu_stop_done *done, bool executed)
} }
static void __cpu_stop_queue_work(struct cpu_stopper *stopper, static void __cpu_stop_queue_work(struct cpu_stopper *stopper,
struct cpu_stop_work *work) struct cpu_stop_work *work,
struct wake_q_head *wakeq)
{ {
list_add_tail(&work->list, &stopper->works); list_add_tail(&work->list, &stopper->works);
wake_up_process(stopper->thread); wake_q_add(wakeq, stopper->thread);
} }
/* queue @work to @stopper. if offline, @work is completed immediately */ /* queue @work to @stopper. if offline, @work is completed immediately */
static void cpu_stop_queue_work(unsigned int cpu, struct cpu_stop_work *work) static void cpu_stop_queue_work(unsigned int cpu, struct cpu_stop_work *work)
{ {
struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu); struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
WAKE_Q(wakeq);
unsigned long flags; unsigned long flags;
spin_lock_irqsave(&stopper->lock, flags); spin_lock_irqsave(&stopper->lock, flags);
if (stopper->enabled) if (stopper->enabled)
__cpu_stop_queue_work(stopper, work); __cpu_stop_queue_work(stopper, work, &wakeq);
else else
cpu_stop_signal_done(work->done, false); cpu_stop_signal_done(work->done, false);
spin_unlock_irqrestore(&stopper->lock, flags); spin_unlock_irqrestore(&stopper->lock, flags);
wake_up_q(&wakeq);
} }
/** /**
...@@ -221,6 +226,7 @@ static int cpu_stop_queue_two_works(int cpu1, struct cpu_stop_work *work1, ...@@ -221,6 +226,7 @@ static int cpu_stop_queue_two_works(int cpu1, struct cpu_stop_work *work1,
{ {
struct cpu_stopper *stopper1 = per_cpu_ptr(&cpu_stopper, cpu1); struct cpu_stopper *stopper1 = per_cpu_ptr(&cpu_stopper, cpu1);
struct cpu_stopper *stopper2 = per_cpu_ptr(&cpu_stopper, cpu2); struct cpu_stopper *stopper2 = per_cpu_ptr(&cpu_stopper, cpu2);
WAKE_Q(wakeq);
int err; int err;
lg_double_lock(&stop_cpus_lock, cpu1, cpu2); lg_double_lock(&stop_cpus_lock, cpu1, cpu2);
...@@ -232,13 +238,15 @@ static int cpu_stop_queue_two_works(int cpu1, struct cpu_stop_work *work1, ...@@ -232,13 +238,15 @@ static int cpu_stop_queue_two_works(int cpu1, struct cpu_stop_work *work1,
goto unlock; goto unlock;
err = 0; err = 0;
__cpu_stop_queue_work(stopper1, work1); __cpu_stop_queue_work(stopper1, work1, &wakeq);
__cpu_stop_queue_work(stopper2, work2); __cpu_stop_queue_work(stopper2, work2, &wakeq);
unlock: unlock:
spin_unlock(&stopper2->lock); spin_unlock(&stopper2->lock);
spin_unlock_irq(&stopper1->lock); spin_unlock_irq(&stopper1->lock);
lg_double_unlock(&stop_cpus_lock, cpu1, cpu2); lg_double_unlock(&stop_cpus_lock, cpu1, cpu2);
wake_up_q(&wakeq);
return err; return err;
} }
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment