Skip to contentSkip to conversation

sched/core: Fix an SMP ordering race in try_to_wake_up() vs. schedule()

tip:locking/core Mike Galbraith 3 participants 4 messages Oct 7, 2016 1 message not in the archive Stable branch

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

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.

Quoted 96 lines · tip-bot for Peter Zijlstra
Commit-ID:  ecf7d01c229d11a44609c0067889372c91fb4f36
Gitweb:     http://git.kernel.org/tip/ecf7d01c229d11a44609c0067889372c91fb4f36
Author:     Peter Zijlstra <[email protected]>
AuthorDate: Wed, 7 Oct 2015 14:14:13 +0200
Committer:  Ingo Molnar <[email protected]>
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.

Cc Linus Torvalds <[email protected]>
Cc Mike Galbraith <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleixner <[email protected]>
---
 kernel/sched/core.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
kernel/sched/core.c +19 -0
index b64f163..7063c6a 100644
@@ -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.
 > 	>  */
#2 lore raw
Quoted 4 lines · Mike Galbraith

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.

Added to 3.12, thanks!

Signature · 2 lines
js
suse labs
#3 lore raw
In reply to Mike Galbraith
Quoted 2 lines · Mike Galbraith

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.

Cute :-) Why was you running a year old kernel on that box anyway? ;-)

Quoted 1 line

Stable material methinks.

Yep..

Quoted 13 lines · tip-bot for Peter Zijlstra
Quoted 12 lines
Commit-ID:  ecf7d01c229d11a44609c0067889372c91fb4f36
Gitweb:     http://git.kernel.org/tip/ecf7d01c229d11a44609c0067889372c91fb4f36
Author:     Peter Zijlstra <[email protected]>
AuthorDate: Wed, 7 Oct 2015 14:14:13 +0200
Committer:  Ingo Molnar <[email protected]>
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.

#4 lore raw
Quoted 5 lines · Peter Zijlstra
Quoted 2 lines · Mike Galbraith

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.

Cute :-) Why was you running a year old kernel on that box anyway? ;-)

It was a 3.0 kernel.. and nearly virgin, a tad less than 22k patches :)

-Mike