Skip to contentSkip to conversation

sched/core: Fix premature p->migration_pending completion

RFC PATCH In review Valentin Schneider 2 participants 6 messages Jan 27, 2021 – Feb 5, 2021 1 message not in the archive Scheduler

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 sched/core: Fix premature p->migration_pending completion

Fiddling some more with a TLA+ model of set_cpus_allowed_ptr() & friends unearthed one more outstanding issue. This doesn't even involve migrate_disable(), but rather affinity changes and execution of the stopper racing with each other.

My own interpretation of the (lengthy) TLA+ splat (note the potential for errors at each level) is:

Initial conditions:

    victim.cpus_mask = {CPU0, CPU1}

CPU0 CPU1 CPU<don't care>

switch_to(victim)
set_cpus_allowed(victim, {CPU1})
kick CPU0 migration_cpu_stop({.dest_cpu = CPU1})
switch_to(stopper/0)

								    // e.g. CFS load balance
								    move_queued_task(CPU0, victim, CPU1);
				   switch_to(victim)
								    set_cpus_allowed(victim, {CPU0});
								      task_rq_unlock();
  migration_cpu_stop(dest_cpu=CPU1)
    task_rq(p) != rq && pending
      kick CPU1 migration_cpu_stop({.dest_cpu = CPU1})

				   switch_to(stopper/1)
				   migration_cpu_stop(dest_cpu=CPU1)
				     task_rq(p) == rq && pending
				       __migrate_task(dest_cpu) // no-op
				     complete_all() <-- !!! affinity is {CPU0} !!!

I believe there are two issues there:
- retriggering of migration_cpu_stop() from within migration_cpu_stop() itself doesn't change arg.dest_cpu
- we'll issue a complete_all() in the task_rq(p) == rq path of migration_cpu_stop() even if the dest_cpu has been superseded by a further affinity change.

Something similar could happen with NUMA's migrate_task_to(), and arguably any other user of migration_cpu_stop() with a .dest_cpu >= 0. Consider:

CPU0 CPUX

switch_to(victim)

					migrate_task_to(victim, CPU1)
					  kick CPU0 migration_cpu_stop({.dest_cpu = CPU1})

					set_cpus_allowed(victim, {CPU42})
					  task_rq_unlock();
  switch_to(stopper/0)
  migration_cpu_stop(dest_cpu=CPU1)
    task_rq(p) == rq && pending
      __migrate_task(dest_cpu)
    complete_all() <-- !!! affinity is {CPU42} !!!

Prevent such premature completions by ensuring the dest_cpu in migration_cpu_stop() is in the task's allowed cpumask.

---
 kernel/sched/core.c | 32 ++++++++++++++++++++------------
 1 file changed, 20 insertions(+), 12 deletions(-)
kernel/sched/core.c +20 -12
index 06b449942adf..b57326b0a742 100644
@@ -1923,20 +1923,28 @@ static int migration_cpu_stop(void *data)
 			complete = true;
 		}
 
-		/* migrate_enable() --  we must not race against SCA */
-		if (dest_cpu < 0) {
-			/*
-			 * When this was migrate_enable() but we no longer
-			 * have a @pending, a concurrent SCA 'fixed' things
-			 * and we should be valid again. Nothing to do.
-			 */
-			if (!pending) {
-				WARN_ON_ONCE(!cpumask_test_cpu(task_cpu(p), &p->cpus_mask));
-				goto out;
-			}
+	       /*
+		* When this was migrate_enable() but we no longer
+		* have a @pending, a concurrent SCA 'fixed' things
+		* and we should be valid again.
+		*
+		* This can also be a stopper invocation that was 'fixed' by an
+		* earlier one.
+		*
+		* Nothing to do.
+		*/
+		if ((dest_cpu < 0 || dest_cpu == cpu_of(rq)) && !pending) {
+			WARN_ON_ONCE(!cpumask_test_cpu(task_cpu(p), &p->cpus_mask));
+			goto out;
+		}
 
+		/*
+		 * Catch any affinity change between the stop_cpu() call and us
+		 * getting here.
+		 * For migrate_enable(), we just want to pick an allowed one.
+		 */
+		if (dest_cpu < 0 || !cpumask_test_cpu(dest_cpu, &p->cpus_mask))
 			dest_cpu = cpumask_any_distribute(&p->cpus_mask);
-		}
 
 		if (task_on_rq_queued(p))
 			rq = __migrate_task(rq, &rf, p, dest_cpu);
Signature · 1 line
2.27.0

A message that is not in the archive (sent off-list or not yet received): BN8PR12MB2978A9A4435A01EDC97D27E89ABA9@BN8PR12MB2978.namprd12.prod.outlook.com

#2 lore raw
Quoted 23 lines · Tao Zhou

Hi,

Quoted 11 lines · Valentin Schneider

Fiddling some more with a TLA+ model of set_cpus_allowed_ptr() & friends unearthed one more outstanding issue. This doesn't even involve migrate_disable(), but rather affinity changes and execution of the stopper racing with each other.

My own interpretation of the (lengthy) TLA+ splat (note the potential for errors at each level) is:

Initial conditions:

    victim.cpus_mask = {CPU0, CPU1}
Quoted 8 lines

CPU0 CPU1 CPU<don't care>

  switch_to(victim)
                                                                 set_cpus_allowed(victim, {CPU1})
                                                                   kick CPU0 migration_cpu_stop({.dest_cpu = CPU1})
  switch_to(stopper/0)
                                                                 // e.g. CFS load balance
                                                                 move_queued_task(CPU0, victim, CPU1);
Quoted 4 lines
                                    ^^^^^^^^^^^^^^^^

Why is move_queued_task() not attach_task()/detach_task() for CFS load..

Heh I expected that one; it is indeed detach_task()/attach_task() for CFS LB. I didn't want to make this any longer than it needed to, and I figured that move_queued_task() being a composition of detach_task(), attach_task() and rq_locks, this would get the point across.

This does raise an "interesting" point that ATM I think this issue cannot actually involve move_queued_task(), since all current move_queued_task() callsites are issued either from a stopper or from set_cpus_allowed_ptr().

CFS' detach_task() + attach_task() could do it, though.

Quoted 34 lines
Quoted 20 lines
kernel/sched/core.c +1 -11
index 06b449942adf..b57326b0a742 100644
@@ -1923,20 +1923,28 @@ static int migration_cpu_stop(void *data)
                     complete = true;
             }
 
-		/* migrate_enable() --  we must not race against SCA */
-		if (dest_cpu < 0) {
-			/*
-			 * When this was migrate_enable() but we no longer
-			 * have a @pending, a concurrent SCA 'fixed' things
-			 * and we should be valid again. Nothing to do.
-			 */
-			if (!pending) {
-				WARN_ON_ONCE(!cpumask_test_cpu(task_cpu(p), &p->cpus_mask));
-				goto out;
-			}
+	       /*

kernel/sched/core.c static int migration_cpu_stop(void *data) · line 1936

Quoted 10 lines
kernel/sched/core.c +10 -0
+		* When this was migrate_enable() but we no longer
+		* have a @pending, a concurrent SCA 'fixed' things
+		* and we should be valid again.
+		*
+		* This can also be a stopper invocation that was 'fixed' by an
+		* earlier one.
+		*
+		* Nothing to do.
+		*/
+		if ((dest_cpu < 0 || dest_cpu == cpu_of(rq)) && !pending) {

When the condition 'dest_cpu == cpu_of(rq)' is true, pending is not NULL. The condition may be like this:

Quoted 5 lines
    if ((dest_cpu < 0 && !pending) || dest_cpu == cpu_of(rq))

We want to choose one cpu in the new(currently modified) cpu_mask and complete all.

Consider the execution of migration_cpu_stop() in above trace with migrate_task_to(). We do have:
- dest_cpu == cpu_of(rq)
- p->migration_pending

but we do *not* want to bail out at this condition, because we need to fix up dest_cpu.

#3 lore raw
In reply to Valentin Schneider
Quoted 17 lines · Valentin Schneider

Fiddling some more with a TLA+ model of set_cpus_allowed_ptr() & friends unearthed one more outstanding issue. This doesn't even involve migrate_disable(), but rather affinity changes and execution of the stopper racing with each other.

My own interpretation of the (lengthy) TLA+ splat (note the potential for errors at each level) is:

Initial conditions:

    victim.cpus_mask = {CPU0, CPU1}

CPU0 CPU1 CPU<don't care>

switch_to(victim)
set_cpus_allowed(victim, {CPU1})
kick CPU0 migration_cpu_stop({.dest_cpu = CPU1})
switch_to(stopper/0)

Quoted 6 lines
								    // e.g. CFS load balance
								    move_queued_task(CPU0, victim, CPU1);
				   switch_to(victim)
								    set_cpus_allowed(victim, {CPU0});
								      task_rq_unlock();

migration_cpu_stop(dest_cpu=CPU1)

This migration stop is due to set_cpus_allowed(victim, {CPU1}), right?

Quoted 5 lines

task_rq(p) != rq && pending
kick CPU1 migration_cpu_stop({.dest_cpu = CPU1})

switch_to(stopper/1) migration_cpu_stop(dest_cpu=CPU1)

And this migration stop is due to set_cpus_allowed(victim, {CPU0}), right?

If I didn't miss something, then dest_cpu should be CPU0 too, not CPU1 and the task should be moved back to CPU0 as expected?

Thanks

--
Qais Yousef

Quoted 85 lines
				     task_rq(p) == rq && pending
				       __migrate_task(dest_cpu) // no-op
				     complete_all() <-- !!! affinity is {CPU0} !!!

I believe there are two issues there:
- retriggering of migration_cpu_stop() from within migration_cpu_stop() itself doesn't change arg.dest_cpu
- we'll issue a complete_all() in the task_rq(p) == rq path of migration_cpu_stop() even if the dest_cpu has been superseded by a further affinity change.

Something similar could happen with NUMA's migrate_task_to(), and arguably any other user of migration_cpu_stop() with a .dest_cpu >= 0. Consider:

CPU0 CPUX

switch_to(victim)

					migrate_task_to(victim, CPU1)
					  kick CPU0 migration_cpu_stop({.dest_cpu = CPU1})

					set_cpus_allowed(victim, {CPU42})
					  task_rq_unlock();
  switch_to(stopper/0)
  migration_cpu_stop(dest_cpu=CPU1)
    task_rq(p) == rq && pending
      __migrate_task(dest_cpu)
    complete_all() <-- !!! affinity is {CPU42} !!!

Prevent such premature completions by ensuring the dest_cpu in migration_cpu_stop() is in the task's allowed cpumask.

---
 kernel/sched/core.c | 32 ++++++++++++++++++++------------
 1 file changed, 20 insertions(+), 12 deletions(-)
kernel/sched/core.c +20 -12
index 06b449942adf..b57326b0a742 100644
@@ -1923,20 +1923,28 @@ static int migration_cpu_stop(void *data)
 			complete = true;
 		}
 
-		/* migrate_enable() --  we must not race against SCA */
-		if (dest_cpu < 0) {
-			/*
-			 * When this was migrate_enable() but we no longer
-			 * have a @pending, a concurrent SCA 'fixed' things
-			 * and we should be valid again. Nothing to do.
-			 */
-			if (!pending) {
-				WARN_ON_ONCE(!cpumask_test_cpu(task_cpu(p), &p->cpus_mask));
-				goto out;
-			}
+	       /*
+		* When this was migrate_enable() but we no longer
+		* have a @pending, a concurrent SCA 'fixed' things
+		* and we should be valid again.
+		*
+		* This can also be a stopper invocation that was 'fixed' by an
+		* earlier one.
+		*
+		* Nothing to do.
+		*/
+		if ((dest_cpu < 0 || dest_cpu == cpu_of(rq)) && !pending) {
+			WARN_ON_ONCE(!cpumask_test_cpu(task_cpu(p), &p->cpus_mask));
+			goto out;
+		}
 
+		/*
+		 * Catch any affinity change between the stop_cpu() call and us
+		 * getting here.
+		 * For migrate_enable(), we just want to pick an allowed one.
+		 */
+		if (dest_cpu < 0 || !cpumask_test_cpu(dest_cpu, &p->cpus_mask))
 			dest_cpu = cpumask_any_distribute(&p->cpus_mask);
-		}
 
 		if (task_on_rq_queued(p))
 			rq = __migrate_task(rq, &rf, p, dest_cpu);
Signature · 1 line
2.27.0
#4 lore raw
Quoted 25 lines · Qais Yousef
Quoted 17 lines · Valentin Schneider

Fiddling some more with a TLA+ model of set_cpus_allowed_ptr() & friends unearthed one more outstanding issue. This doesn't even involve migrate_disable(), but rather affinity changes and execution of the stopper racing with each other.

My own interpretation of the (lengthy) TLA+ splat (note the potential for errors at each level) is:

Initial conditions:

    victim.cpus_mask = {CPU0, CPU1}

CPU0 CPU1 CPU<don't care>

switch_to(victim)
set_cpus_allowed(victim, {CPU1})
kick CPU0 migration_cpu_stop({.dest_cpu = CPU1})
switch_to(stopper/0)

Quoted 6 lines
								    // e.g. CFS load balance
								    move_queued_task(CPU0, victim, CPU1);
				   switch_to(victim)
								    set_cpus_allowed(victim, {CPU0});
								      task_rq_unlock();

migration_cpu_stop(dest_cpu=CPU1)

Quoted 2 lines

This migration stop is due to set_cpus_allowed(victim, {CPU1}), right?

Right

Quoted 8 lines
Quoted 5 lines

task_rq(p) != rq && pending
kick CPU1 migration_cpu_stop({.dest_cpu = CPU1})

switch_to(stopper/1) migration_cpu_stop(dest_cpu=CPU1)

And this migration stop is due to set_cpus_allowed(victim, {CPU0}), right?

Nein! This is a retriggering of the "current" stopper (triggered by set_cpus_allowed(victim, {CPU1})), see the tail of that

  else if (dest_cpu < 0 || pending)

branch in migration_cpu_stop(), is what I'm trying to hint at with that

task_rq(p) != rq && pending

Quoted 7 lines

If I didn't miss something, then dest_cpu should be CPU0 too, not CPU1 and the task should be moved back to CPU0 as expected?

Thanks

--
Qais Yousef

#5 lore raw
Quoted 40 lines · Valentin Schneider
Quoted 25 lines · Qais Yousef
Quoted 17 lines · Valentin Schneider

Fiddling some more with a TLA+ model of set_cpus_allowed_ptr() & friends unearthed one more outstanding issue. This doesn't even involve migrate_disable(), but rather affinity changes and execution of the stopper racing with each other.

My own interpretation of the (lengthy) TLA+ splat (note the potential for errors at each level) is:

Initial conditions:

    victim.cpus_mask = {CPU0, CPU1}

CPU0 CPU1 CPU<don't care>

switch_to(victim)
set_cpus_allowed(victim, {CPU1})
kick CPU0 migration_cpu_stop({.dest_cpu = CPU1})
switch_to(stopper/0)

Quoted 6 lines
								    // e.g. CFS load balance
								    move_queued_task(CPU0, victim, CPU1);
				   switch_to(victim)
								    set_cpus_allowed(victim, {CPU0});
								      task_rq_unlock();

migration_cpu_stop(dest_cpu=CPU1)

Quoted 2 lines

This migration stop is due to set_cpus_allowed(victim, {CPU1}), right?

Right

Quoted 8 lines
Quoted 5 lines

task_rq(p) != rq && pending
kick CPU1 migration_cpu_stop({.dest_cpu = CPU1})

switch_to(stopper/1) migration_cpu_stop(dest_cpu=CPU1)

And this migration stop is due to set_cpus_allowed(victim, {CPU0}), right?

Quoted 8 lines

Nein! This is a retriggering of the "current" stopper (triggered by set_cpus_allowed(victim, {CPU1})), see the tail of that

  else if (dest_cpu < 0 || pending)

branch in migration_cpu_stop(), is what I'm trying to hint at with that

task_rq(p) != rq && pending

Okay I see. But AFAIU, the work will be queued in order. So we should first handle the set_cpus_allowed_ptr(victim, {CPU0}) before the retrigger, no?

So I see migration_cpu_stop() running 3 times

1. because of set_cpus_allowed(victim, {CPU1}) on CPU0
2. because of set_cpus_allowed(victim, {CPU0}) on CPU1
3. because of retrigger of '1' on CPU0

Thanks

--
Qais Yousef

#6 lore raw
Quoted 42 lines · Qais Yousef
Quoted 32 lines · Valentin Schneider
Quoted 17 lines · Qais Yousef
Quoted 5 lines · Valentin Schneider

Initial conditions:

    victim.cpus_mask = {CPU0, CPU1}

CPU0 CPU1 CPU<don't care>

Quoted 10 lines
  switch_to(victim)
                                                              set_cpus_allowed(victim, {CPU1})
                                                                kick CPU0 migration_cpu_stop({.dest_cpu = CPU1})
  switch_to(stopper/0)
                                                              // e.g. CFS load balance
                                                              move_queued_task(CPU0, victim, CPU1);
                             switch_to(victim)
                                                              set_cpus_allowed(victim, {CPU0});
                                                                task_rq_unlock();
  migration_cpu_stop(dest_cpu=CPU1)
Quoted 2 lines

This migration stop is due to set_cpus_allowed(victim, {CPU1}), right?

Right

Quoted 8 lines
Quoted 5 lines

task_rq(p) != rq && pending
kick CPU1 migration_cpu_stop({.dest_cpu = CPU1})

switch_to(stopper/1) migration_cpu_stop(dest_cpu=CPU1)

And this migration stop is due to set_cpus_allowed(victim, {CPU0}), right?

Quoted 8 lines

Nein! This is a retriggering of the "current" stopper (triggered by set_cpus_allowed(victim, {CPU1})), see the tail of that

  else if (dest_cpu < 0 || pending)

branch in migration_cpu_stop(), is what I'm trying to hint at with that

task_rq(p) != rq && pending

Quoted 9 lines

Okay I see. But AFAIU, the work will be queued in order. So we should first handle the set_cpus_allowed_ptr(victim, {CPU0}) before the retrigger, no?

So I see migration_cpu_stop() running 3 times

1. because of set_cpus_allowed(victim, {CPU1}) on CPU0
2. because of set_cpus_allowed(victim, {CPU0}) on CPU1
3. because of retrigger of '1' on CPU0

On that 'CPU<don't care>' lane, I intentionally included task_rq_unlock() but not 'kick CPU1 migration_cpu_stop({.dest_cpu = CPU0})'. IOW, there is nothing in that trace that queues a stopper work for 2. - it *will* happen at some point, but harm will already have been done.

The migrate_task_to() example is potentially worse, because it doesn't rely on which stopper work gets enqueued first - only that an extra affinity change happens before the first stopper work grabs the pi_lock and completes.