sched: Reset cpumask if __migrate_task() failed for migration_cpu_stop()
PATCH v3 1/1 sched: Reset cpumask if __migrate_task() failed for migration_cpu_stop()
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 noone requested an affinity change while the task was in migrate-disable section.
There are a few spots where set_cpus_allowed_ptr() does not use cpus_read_lock(). In the IRQ thread case, there will be an update following if the CPU goes offline so it is not really needed to block CPU hotplug during the update.
If __migrate_task() fails, set the CPU mask of the task to runqueue it is currently on. Use do_set_cpus_allowed() for this so it also invokes class' set_cpus_allowed callback.
Remove the comment about __migrate_task() failing.
---
This survives my testcase without any warnings.
v2…v3: https://lore.kernel.org/all/[email protected]/
- Drop RFC and with it the irq_thread_check_affinity() since the problem is not limited to it.
- Instead of manually setting the CPU mask bit, use do_set_cpus_allowed() which should handle the DL bits correctly.
v1…v2: https://lore.kernel.org/all/[email protected]/
- Repost with collected notes.
kernel/sched/core.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)
kernel/sched/core.c +7 -8
@@ -2710,17 +2710,16 @@ 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)) { +struct affinity_context ac = { + .new_mask =cpumask_of (rq->cpu), + }; + +do_set_cpus_allowed (p, &ac); + } }else { p->wake_cpu = arg->dest_cpu; } - -/* -* XXX __migrate_task() can fail, at which point we might end -* up running on a dodgy CPU, AFAICT this can only happen -* during CPU hotplug, at which point we'll get pushed out -* anyway, so it's probably not a big deal. -*/ - }else if (pending) {/* * This happens when we get migrated between migrate_enable()'s
Signature · 1 line
2.55.0
kernel/sched/core.c static int migration_cpu_stop(void *data) · line 2719
Quoted 10 lines · Sebastian Andrzej Siewior
kernel/sched/core.c +7
-0if (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)) { +struct affinity_context ac = { + .new_mask =cpumask_of (rq->cpu), + }; + +do_set_cpus_allowed (p, &ac); + }
do_set_cpus_allowed() updates the rq clock a second time here, through sched_change_begin() without DEQUEUE_NOCLOCK. With WARN_DOUBLE_CLOCK enabled:
WARNING: kernel/sched/core.c:881 at update_rq_clock+0x4c7/0x560, CPU#0: migration/0/22 Stopper: migration_cpu_stop+0x0/0xbe0 <- __set_cpus_allowed_ptr_locked+0xd1e/0x19c0 sched_change_begin+0x14d/0x660 migration_cpu_stop+0x5f6/0xbe0 cpu_stopper_thread+0x25e/0x3f0
Seen on v7.4-rc3 with this patch (RT and non-RT) with sched_setaffinity() loops while another loop writes off and on to /sys/devices/system/cpu/smt/control.
Thanks,
Karl
Quoted 2 lines · Sebastian Andrzej Siewior
syzbot reported a race of assigning a CPU affinity during CPU hotplug operation.
I tested the patch on PREEMPT_RT with syzbot's config, changing IRQ affinities while SMT was switched off and on. The warning is gone with the patch. Without it, every boot warned, and one also hit a BUG:
kernel BUG at kernel/sched/core.c:3642!
try_to_wake_up+0x85f/0x1320 irq_do_set_affinity+0x42c/0x5d0 RIP: 0010:select_fallback_rq+0x1f6/0x200 Kernel panic - not syncing: Fatal exception in interrupt
Quoted 4 lines
There are a few spots where
set_cpus_allowed_ptr()does not usecpus_read_lock(). In the IRQ thread case, there will be an update following if the CPU goes offline so it is not really needed to block CPU hotplug during the update.
sched_setaffinity() is one of those spots, but a user task gets no later update.
With the patch, a test that changed the affinity of user threads in the same setup read back a CPU outside the requested mask in about 1 of 5000 calls, on RT and non-RT. Without the patch it never did.
Thanks,
Karl