Skip to contentSkip to conversation

sched: irq: cpu-hotplug race vs set_cpus_allowed_ptr()

RFC PATCH In review Sebastian Andrzej Siewior 3 participants 5 messages Oct 12 – Oct 13

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH v2 1/1 sched: irq: cpu-hotplug race vs set_cpus_allowed_ptr()

syzbot reported a race of assigning a CPU affinity during CPU hotplug operation.
After the affinity of an IRQ has been changed, the IRQ-core sets IRQTF_AFFINITY and wakes the relevant interrupt threads which need to adjust their affinity mask. The thread will then invoke set_cpus_allowed_ptr() to update the mask. Based on the new mask a CPU is chosen on which the thread should run.

It is verified that this CPU is online however there is no guarantee that this CPU remains online while affine_move_task() is moving _this_ task. Since the current task requests the migration the stopper/ migration_cpu_stop() is involved at which point the task pauses for a while. If the CPU goes offline (or is no longer cpu_online_mask) then __migrate_task() (due to is_cpu_allowed() reject) will return the rq of the CPU on which the task is currently running (which does not match it's task_struct::cpus_mask).

The aftermath:
The IRQ-thread's CPU and task's cpus_mask do not match. A migrate_disable()-> schedule() will change task_struct::cpus_ptr ensuring that the following migrate_enable() will update the task to the requested affinity mask. At this point, affine_move_task() expects task_struct::migration_pending set but it is NULL because no one requested an affinity change while the task was in migrate-disable section.

I see two ways of fixing this:
- Holding the cpus_read_lock while set_cpus_allowed_ptr() is invoked. This ensure that the CPU remains in the cpu_online_mask while the migration task moving the task over. Should the mask be already invalid, then it is rejected otherwise the operation completes.

sashiko complains about deadlock of cpus_write_lock() in the CPU down path and calling irq_migrate_all_off_this_cpu() vs irq_thread_check_affinity() doing cpus_read_lock().
As far as I see, there is no deadlock because the mask is updated, the tread is woken, the tread blocks with cpus_read_lock() until the CPU is down.
There is nothing that waits for the interrupt thread to complete, there is no synchronize_irq() invocation which would be required for the deadlock to happen.

- Should __migrate_task() fail to return the requested rq make sure its CPU is part task_struct::cpus_mask. This ensures that the current CPU is still part of mask avoiding a possible push by migrate_enable(). sched_class::set_cpus_allowed did not see this mask. It might be a bit inconsistent and feels a bit like select_fallback_rq() without the printk.

Sashiko complains:

| If __migrate_task() fails (e.g., due to a concurrent CPU offline event), this
| fallback code forcibly modifies p->cpus_mask with cpumask_set_cpu(). This
| skips the required p->sched_class->set_cpus_allowed() callback and fails to
| update p->nr_cpus_allowed.
| Can this bypass global EDF real-time invariants for SCHED_DEADLINE (as
| set_cpus_allowed_dl() is skipped), and allow unprivileged tasks to escape
| affinity boundaries, giving them access to CPUs outside their cpusets?

We did do_set_cpus_allowed() for CPU X and can't migrate there now. We also don't know if the invocation was successful.

Looking at this, it is not possible acquire the CPU-hotplug lock here and not every caller holds it. This seems to be limited to migrate-disable. Should this happen without the lock, then during the wake-up the scheduler will just print "process … no longer affine to cpu" pick a CPU and be done with it.
Maybe set the bit followed by do_set_cpus_allowed()?

Both changes are implemented to illustrate, one is enough.
I can reproduce this back on v6.8, therefore I assume we have this since day #1 of migrate-disable.

---

v1…v2: https://lore.kernel.org/all/[email protected]/
- Repost with collected notes.

 kernel/irq/manage.c | 6 ++++--
 kernel/sched/core.c | 2 ++
 2 files changed, 6 insertions(+), 2 deletions(-)
kernel/irq/manage.c +4 -2
index 57eff26fa646a..75b6798c1d97c 100644
@@ -8,6 +8,7 @@
 
 #define pr_fmt(fmt) "genirq: " fmt
 
+#include <linux/cpuhplock.h>
 #include <linux/irq.h>
 #include <linux/kthread.h>
 #include <linux/module.h>
@@ -1044,8 +1045,9 @@ static void irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *a
 		m = irq_data_get_effective_affinity_mask(&desc->irq_data);
 		cpumask_copy(mask, m);
 	}
-
-	set_cpus_allowed_ptr(current, mask);
+	scoped_guard(cpus_read_lock) {
+		set_cpus_allowed_ptr(current, mask);
+	}
 	free_cpumask_var(mask);
 }
 #else
kernel/sched/core.c +2 -0
index 1fe40de6ebe3c..5896be82e6ddc 100644
@@ -2674,6 +2674,8 @@ static int migration_cpu_stop(void *data)
 		if (task_on_rq_queued(p)) {
 			update_rq_clock(rq);
 			rq = __migrate_task(rq, &rf, p, arg->dest_cpu);
+			if (rq != cpu_rq(arg->dest_cpu))
+				cpumask_set_cpu(rq->cpu, &p->cpus_mask);
 		} else {
 			p->wake_cpu = arg->dest_cpu;
 		}
Signature · 1 line
2.55.0
maintainer
#1 lore raw

Hello,

Quoted 32 lines · Sebastian Andrzej Siewior

syzbot reported a race of assigning a CPU affinity during CPU hotplug operation.
After the affinity of an IRQ has been changed, the IRQ-core sets IRQTF_AFFINITY and wakes the relevant interrupt threads which need to adjust their affinity mask. The thread will then invoke set_cpus_allowed_ptr() to update the mask. Based on the new mask a CPU is chosen on which the thread should run.

It is verified that this CPU is online however there is no guarantee that this CPU remains online while affine_move_task() is moving _this_ task. Since the current task requests the migration the stopper/ migration_cpu_stop() is involved at which point the task pauses for a while. If the CPU goes offline (or is no longer cpu_online_mask) then __migrate_task() (due to is_cpu_allowed() reject) will return the rq of the CPU on which the task is currently running (which does not match it's task_struct::cpus_mask).

The aftermath:
The IRQ-thread's CPU and task's cpus_mask do not match. A migrate_disable()-> schedule() will change task_struct::cpus_ptr ensuring that the following migrate_enable() will update the task to the requested affinity mask. At this point, affine_move_task() expects task_struct::migration_pending set but it is NULL because no one requested an affinity change while the task was in migrate-disable section.

I see two ways of fixing this:
- Holding the cpus_read_lock while set_cpus_allowed_ptr() is invoked. This ensure that the CPU remains in the cpu_online_mask while the migration task moving the task over. Should the mask be already invalid, then it is rejected otherwise the operation completes.

Quoted 9 lines

sashiko complains about deadlock of cpus_write_lock() in the CPU down path and calling irq_migrate_all_off_this_cpu() vs irq_thread_check_affinity() doing cpus_read_lock().
As far as I see, there is no deadlock because the mask is updated, the tread is woken, the tread blocks with cpus_read_lock() until the CPU is down.
There is nothing that waits for the interrupt thread to complete, there is no synchronize_irq() invocation which would be required for the deadlock to happen.

I would agree with your assessment here. Can't see how a deadlock is possible.

Quoted 7 lines

- Should __migrate_task() fail to return the requested rq make sure its CPU is part task_struct::cpus_mask. This ensures that the current CPU is still part of mask avoiding a possible push by migrate_enable(). sched_class::set_cpus_allowed did not see this mask. It might be a bit inconsistent and feels a bit like select_fallback_rq() without the printk.

Quoted 8 lines

Sashiko complains:

| If __migrate_task() fails (e.g., due to a concurrent CPU offline event), this
| fallback code forcibly modifies p->cpus_mask with cpumask_set_cpu(). This
| skips the required p->sched_class->set_cpus_allowed() callback and fails to
| update p->nr_cpus_allowed.
| Can this bypass global EDF real-time invariants for SCHED_DEADLINE (as
| set_cpus_allowed_dl() is skipped), and allow unprivileged tasks to escape
| affinity boundaries, giving them access to CPUs outside their cpusets?

And not going via set_cpus_allowed_dl() might indeed create problems.

So, I would vote for option 1.

Thanks,
Juri

maintainer
#2 lore raw
In reply to Sebastian Andrzej Siewior
Quoted 34 lines · Sebastian Andrzej Siewior

syzbot reported a race of assigning a CPU affinity during CPU hotplug operation.
After the affinity of an IRQ has been changed, the IRQ-core sets IRQTF_AFFINITY and wakes the relevant interrupt threads which need to adjust their affinity mask. The thread will then invoke set_cpus_allowed_ptr() to update the mask. Based on the new mask a CPU is chosen on which the thread should run.

It is verified that this CPU is online however there is no guarantee that this CPU remains online while affine_move_task() is moving _this_ task. Since the current task requests the migration the stopper/ migration_cpu_stop() is involved at which point the task pauses for a while. If the CPU goes offline (or is no longer cpu_online_mask) then __migrate_task() (due to is_cpu_allowed() reject) will return the rq of the CPU on which the task is currently running (which does not match it's task_struct::cpus_mask).

The aftermath:
The IRQ-thread's CPU and task's cpus_mask do not match. A migrate_disable()-> schedule() will change task_struct::cpus_ptr ensuring that the following migrate_enable() will update the task to the requested affinity mask. At this point, affine_move_task() expects task_struct::migration_pending set but it is NULL because no one requested an affinity change while the task was in migrate-disable section.

I see two ways of fixing this:
- Holding the cpus_read_lock while set_cpus_allowed_ptr() is invoked. This ensure that the CPU remains in the cpu_online_mask while the migration task moving the task over. Should the mask be already invalid, then it is rejected otherwise the operation completes.

Quoted 9 lines

sashiko complains about deadlock of cpus_write_lock() in the CPU down path and calling irq_migrate_all_off_this_cpu() vs irq_thread_check_affinity() doing cpus_read_lock().
As far as I see, there is no deadlock because the mask is updated, the tread is woken, the tread blocks with cpus_read_lock() until the CPU is down.
There is nothing that waits for the interrupt thread to complete, there is no synchronize_irq() invocation which would be required for the deadlock to happen.

But any other set_cpus_allowed_ptr() call in the kernel can suffer the same problem, no? Why is the irq thing 'special'?

Quoted 26 lines

- Should __migrate_task() fail to return the requested rq make sure its CPU is part task_struct::cpus_mask. This ensures that the current CPU is still part of mask avoiding a possible push by migrate_enable(). sched_class::set_cpus_allowed did not see this mask. It might be a bit inconsistent and feels a bit like select_fallback_rq() without the printk.

Sashiko complains:

| If __migrate_task() fails (e.g., due to a concurrent CPU offline event), this
| fallback code forcibly modifies p->cpus_mask with cpumask_set_cpu(). This
| skips the required p->sched_class->set_cpus_allowed() callback and fails to
| update p->nr_cpus_allowed.
| Can this bypass global EDF real-time invariants for SCHED_DEADLINE (as
| set_cpus_allowed_dl() is skipped), and allow unprivileged tasks to escape
| affinity boundaries, giving them access to CPUs outside their cpusets?

We did do_set_cpus_allowed() for CPU X and can't migrate there now. We also don't know if the invocation was successful.

Looking at this, it is not possible acquire the CPU-hotplug lock here and not every caller holds it. This seems to be limited to migrate-disable. Should this happen without the lock, then during the wake-up the scheduler will just print "process … no longer affine to cpu" pick a CPU and be done with it.
Maybe set the bit followed by do_set_cpus_allowed()?

Quoted 3 lines

Both changes are implemented to illustrate, one is enough.
I can reproduce this back on v6.8, therefore I assume we have this since day #1 of migrate-disable.

I am struggling to follow along. The actual fail scenario is a combination of hotplug *and* migrate_disable()? Can we get a picture?

A CPU must not be able to go offline while there are migrate_disable() tasks on it. Are you saying the migrate_disable() happens after hotplug already marks the CPU offline?

The thinking was that either:

- task is asleep, and ttwu()/select_task_rq() picks a fallback
- task is running, and __balance_push_cpu_stop() picks a fallback

There is a TLA+ model of all this somewhere; Valentin, do you remember? That would have to be extended at the very least.

#3 lore raw
Quoted 6 lines · Peter Zijlstra
Quoted 3 lines

There is nothing that waits for the interrupt thread to complete, there is no synchronize_irq() invocation which would be required for the deadlock to happen.

But any other set_cpus_allowed_ptr() call in the kernel can suffer the same problem, no? Why is the irq thing 'special'?

Not saying this is special.

Quoted 2 lines

I am struggling to follow along. The actual fail scenario is a combination of hotplug *and* migrate_disable()? Can we get a picture?

let me grab the reproducer and a few tracepoints to make an accurate drawing.

Quoted 3 lines

A CPU must not be able to go offline while there are migrate_disable() tasks on it. Are you saying the migrate_disable() happens after hotplug already marks the CPU offline?

I *think* there were no tasks on it. So based on fading out memory:
T on CPU0 in migrate_disable(), assigned to CPU1, schedule(). CPU1 about to go down while the T on CPU0 returns does migrate_enable() and should migrate to CPU1.
But this old memory. Let me get the reproducer and get back to you.

Sebastian

#4 lore raw
Quoted 5 lines · To Peter Zijlstra
Quoted 2 lines

I am struggling to follow along. The actual fail scenario is a combination of hotplug *and* migrate_disable()? Can we get a picture?

let me grab the reproducer and a few tracepoints to make an accurate drawing.

CPU1 is actually doing nothing. And migrate_disable() is involved way later:

CPU0
_cpu_down(1)

    set_cpu_dying(1)

/* CPU1 is now present in ONLINE and DYING mask */

<idle or whatever>
try_to_wake_up(p)
-> select_fallback_rq() -> falls back to CPU0 because CPU1 is not available

<irq-thread>
irq_thread_check_affinity()

    set_cpus_allowed_ptr(, 1)
    /* cpu_valid_mask = cpu_online_mask */
     -> affine_move_task(dest_cpu = 1);
       -> stop_one_cpu_nowait()

/* switch to migration_cpu_stop-0 */ migration_cpu_stop()
-> __migrate_task(arg->dest_cpu = 1) /* returns rq of 0 */

< back to irq-thread >
set_cpus_allowed_ptr(, 1) return with cpus_ptr set to CPU1 while running on CPU0.

A schedule out (and sleep) at this point is fine, try_to_wake_up() will again go to select_fallback_rq() and schedule the task on 0 with proper ::cpus_ptr.

If instead the task does

	migrate_disable()
	schedule() /* remain running on RQ */
	-> migrate_disable_switch()
	   -> set_cpus_allowed_common()
	      -> p->cpus_ptr = ctx->new_mask; = CPU0

Once back on CPU0, migrate_enable() does:

	___migrate_enable()
	-> __set_cpus_allowed_ptr(SCA_MIGRATE_ENABLE | &p->cpus_mask)
	  /* ::cpus_mask has still the 1 set. There was no wakeup, task
	     remained on RQ so the mask is not fixed */

Now affine_move_task() will expect task_struct::migration_pending to be set but it was never set. The task would have remained on the current CPU (0) if the previous set_cpus_allowed_ptr() would not have set an invalid CPU mask.

Sebastian