All of lore.kernel.org
 help / color / mirror / Atom feed
From: Mike Galbraith <efault@gmx.de>
To: stable <stable@vger.kernel.org>
Cc: peterz@infradead.org, Ingo Molnar <mingo@kernel.org>,
	Oleg Nesterov <oleg@redhat.com>,
	linux-tip-commits <linux-tip-commits@vger.kernel.org>
Subject: Re: [tip:locking/core] sched/core: Fix an SMP ordering race in try_to_wake_up() vs. schedule()
Date: Fri, 07 Oct 2016 03:36:55 +0200	[thread overview]
Message-ID: <1475804215.4075.13.camel@gmx.de> (raw)
In-Reply-To: <tip-ecf7d01c229d11a44609c0067889372c91fb4f36@git.kernel.org>

Seems this may be one of those not so theoretical races.  A humongous
ppc64 box actually managed to run a task on two cores.. briefly.

Stable material methinks.

On Fri, 2015-12-04 at 03:53 -0800, tip-bot for Peter Zijlstra wrote:
> Commit-ID:  ecf7d01c229d11a44609c0067889372c91fb4f36
> Gitweb:     http://git.kernel.org/tip/ecf7d01c229d11a44609c0067889372c91fb4f36
> Author:     Peter Zijlstra <peterz@infradead.org>
> AuthorDate: Wed, 7 Oct 2015 14:14:13 +0200
> Committer:  Ingo Molnar <mingo@kernel.org>
> CommitDate: Fri, 4 Dec 2015 10:26:43 +0100
> 
> sched/core: Fix an SMP ordering race in try_to_wake_up() vs. schedule()
> 
> Oleg noticed that its possible to falsely observe p->on_cpu == 0 such
> that we'll prematurely continue with the wakeup and effectively run p on
> two CPUs at the same time.
> 
> Even though the overlap is very limited; the task is in the middle of
> being scheduled out; it could still result in corruption of the
> scheduler data structures.
> 
>         CPU0                            CPU1
> 
>         set_current_state(...)
> 
>         
>           context_switch(X, Y)
>             prepare_lock_switch(Y)
>               Y->on_cpu = 1;
>             finish_lock_switch(X)
>               store_release(X->on_cpu, 0);
> 
>                                         try_to_wake_up(X)
>                                           LOCK(p->pi_lock);
> 
>                                           t = X->on_cpu; // 0
> 
>           context_switch(Y, X)
>             prepare_lock_switch(X)
>               X->on_cpu = 1;
>             finish_lock_switch(Y)
>               store_release(Y->on_cpu, 0);
>         
> 
>         schedule();
>           deactivate_task(X);
>           X->on_rq = 0;
> 
>                                           if (X->on_rq) // false
> 
>                                           if (t) while (X->on_cpu)
>                                             cpu_relax();
> 
>           context_switch(X, ..)
>             finish_lock_switch(X)
>               store_release(X->on_cpu, 0);
> 
> Avoid the load of X->on_cpu being hoisted over the X->on_rq load.
> 
> Reported-by: Oleg Nesterov <oleg@redhat.com>
> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
> Cc: Linus Torvalds <torvalds@linux-foundation.org>
> Cc: Mike Galbraith <efault@gmx.de>
> Cc: Peter Zijlstra <peterz@infradead.org>
> Cc: Thomas Gleixner <tglx@linutronix.de>
> Signed-off-by: Ingo Molnar <mingo@kernel.org>
> ---
>  kernel/sched/core.c | 19 +++++++++++++++++++
>  1 file changed, 19 insertions(+)
> 
> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> index b64f163..7063c6a 100644
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -1947,6 +1947,25 @@ try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
>  
>  #ifdef CONFIG_SMP
>  > 	> /*
> +> 	>  * Ensure we load p->on_cpu _after_ p->on_rq, otherwise it would be
> +> 	>  * possible to, falsely, observe p->on_cpu == 0.
> +> 	>  *
> +> 	>  * One must be running (->on_cpu == 1) in order to remove oneself
> +> 	>  * from the runqueue.
> +> 	>  *
> +> 	>  *  [S] ->on_cpu = 1;> 	> [L] ->on_rq
> +> 	>  *      UNLOCK rq->lock
> +> 	>  *> 	> 	> 	> RMB
> +> 	>  *      LOCK   rq->lock
> +> 	>  *  [S] ->on_rq = 0;    [L] ->on_cpu
> +> 	>  *
> +> 	>  * Pairs with the full barrier implied in the UNLOCK+LOCK on rq->lock
> +> 	>  * from the consecutive calls to schedule(); the first switching to our
> +> 	>  * task, the second putting it to sleep.
> +> 	>  */
> +> 	> smp_rmb();
> +
> +> 	> /*
>  > 	>  * If the owning (remote) cpu is still in the middle of schedule() with
>  > 	>  * this task as prev, wait until its done referencing the task.
>  > 	>  */

       reply	other threads:[~2016-10-07  1:37 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <tip-ecf7d01c229d11a44609c0067889372c91fb4f36@git.kernel.org>
2016-10-07  1:36 ` Mike Galbraith [this message]
2016-10-07  6:38   ` [tip:locking/core] sched/core: Fix an SMP ordering race in try_to_wake_up() vs. schedule() Jiri Slaby
2016-10-07  8:29   ` Peter Zijlstra
2016-10-07  9:33     ` Mike Galbraith

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=1475804215.4075.13.camel@gmx.de \
    --to=efault@gmx.de \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=oleg@redhat.com \
    --cc=peterz@infradead.org \
    --cc=stable@vger.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.