WARNING in __set_cpus_allowed_ptr_locked
syzbot
syzbotHello,
syzbot found the following issue on:
HEAD commit: 08dbfad3f504 Merge tag 'for-linus' of git://git.kernel.org.. git tree: upstream console+strace: https://syzkaller.appspot.com/x/log.txt?x=14970349580000 kernel config: https://syzkaller.appspot.com/x/.config?x=b454dc6b1b7acd30 dashboard link: https://syzkaller.appspot.com/bug?extid=6835d5c11145e4f77057 compiler: Debian clang version 22.1.8 (++20260613092233+e80beda6e255-1~exp1~20260613092250.77), Debian LLD 22.1.8 syz repro: https://syzkaller.appspot.com/x/repro.syz?x=13221e25580000 C reproducer: https://syzkaller.appspot.com/x/repro.c?x=17221e25580000
Downloadable assets:
disk image: https://storage.googleapis.com/syzbot-assets/8aa9167f616f/disk-08dbfad3.raw.xz
vmlinux: https://storage.googleapis.com/syzbot-assets/9ec932d59de3/vmlinux-08dbfad3.xz
kernel image: https://storage.googleapis.com/syzbot-assets/86e2243e886e/bzImage-08dbfad3.xz
IMPORTANT: if you fix the issue, please add the following tag to the commit:
------------[ cut here ]------------
!pending
WARNING: kernel/sched/core.c:3057 at affine_move_task kernel/sched/core.c:3057 [inline], CPU#0: irq/33-virtio1-/1195
WARNING: kernel/sched/core.c:3057 at __set_cpus_allowed_ptr_locked+0x10ff/0x19c0 kernel/sched/core.c:3190, CPU#0: irq/33-virtio1-/1195
Modules linked in:
CPU: 0 UID: 0 PID: 1195 Comm: irq/33-virtio1- Not tainted syzkaller #0 PREEMPT_{RT,(full)}
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 07/24/2026
RIP: 0010:affine_move_task kernel/sched/core.c:3057 [inline]
RIP: 0010:__set_cpus_allowed_ptr_locked+0x10ff/0x19c0 kernel/sched/core.c:3190
Code: cf ff ff 48 8b 5c 24 18 48 8d 7b 08 e8 9a 93 09 00 e9 2e fe ff ff 90 0f 0b 90 e9 74 f3 ff ff e8 b7 ee 9d ff e9 dd f8 ff ff 90 <0f> 0b 90 48 8b 3c 24 48 8b 74 24 08 48 8b 54 24 10 e8 2b cf ff ff
RSP: 0018:ffffc9000681f900 EFLAGS: 00010046
RAX: 0000000000000000 RBX: ffff8880267a84b8 RCX: ffffffff81950a2b
RDX: dffffc0000000000 RSI: 0000000000000008 RDI: ffff8880267a84b0
RBP: ffffc9000681fb10 R08: ffff8880267a84b7 R09: 1ffff11004cf5096
R10: dffffc0000000000 R11: ffffed1004cf5097 R12: 1ffff11004cf5097
R13: 1ffff92000d03f28 R14: ffff8880267a84b0 R15: 0000000000000004
FS: 0000000000000000(0000) GS:ffff888125bd6000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 0000555581c3f6d0 CR3: 0000000043f1e000 CR4: 00000000003526f0
Call Trace:
<TASK>
__set_cpus_allowed_ptr kernel/sched/core.c:3222 [inline]
___migrate_enable+0x19c/0x1f0 kernel/sched/core.c:2486
__migrate_enable include/linux/sched.h:2471 [inline]
migrate_enable include/linux/sched.h:2520 [inline]
__local_bh_enable+0x21c/0x2f0 kernel/softirq.c:273
__local_bh_enable_ip+0x19f/0x2b0 kernel/softirq.c:328
local_bh_enable include/linux/bottom_half.h:33 [inline]
irq_forced_thread_fn+0xe9/0x120 kernel/irq/manage.c:1169
irq_thread+0x455/0x690 kernel/irq/manage.c:1272
kthread+0x38b/0x470 kernel/kthread.c:436
ret_from_fork+0x514/0xb70 arch/x86/kernel/process.c:158
ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245
</TASK>---
This report is generated by a bot. It may contain errors. See https://goo.gl/tpsmEJ for more information about syzbot. syzbot engineers can be reached at [email protected].
syzbot will keep track of this issue. See:
https://goo.gl/tpsmEJ#status for how to communicate with syzbot.
If the report is already addressed, let syzbot know by replying with:
#syz fix: exact-commit-title
If you want syzbot to run the reproducer, reply with:
#syz test: git://repo/address.git branch-or-commit-hash
If you attach or paste a git patch, syzbot will apply it before testing.
If you want to overwrite report's subsystems, reply with:
#syz set subsystems: new-subsystem
(See the list of subsystem names on the web dashboard)
If the report is a duplicate of another one, reply with:
#syz dup: exact-subject-of-another-report
If you want to undo deduplication, reply with:
#syz undup
RFC PATCH 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 noone 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 operaton completes. Maybe we should also check if cpus_read_lock is held during the invocation of set_cpus_allowed_ptr() so we don't get this problem from other callers.
- 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.
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.
--- kernel/irq/manage.c | 6 ++++-- kernel/sched/core.c | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-)
kernel/irq/manage.c +4 -2
@@ -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
@@ -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
Re: [RFC PATCH] sched: irq: cpu-hotplug race vs set_cpus_allowed_ptr()
Hello :)
Quoted 26 lines · To syzbot
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 setsIRQTF_AFFINITYand wakes the relevant interrupt threads which need to adjust their affinity mask. The thread will then invokeset_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 longercpu_online_mask) then__migrate_task()(due tois_cpu_allowed()reject) will return the rq of the CPU on which the task is currently running (which does not match it'stask_struct::cpus_mask).The aftermath:
The IRQ-thread's CPU and task'scpus_maskdo not match. A migrate_disable()->schedule()will changetask_struct::cpus_ptrensuring that the followingmigrate_enable()will update the task to the requested affinity mask. At this point,affine_move_task()expectstask_struct::migration_pendingset but it is NULL because noone requested an affinity change while the task was in migrate-disable section.
Quoted 8 lines
I see two ways of fixing this:
- Holding thecpus_read_lockwhileset_cpus_allowed_ptr()is invoked. This ensure that the CPU remains in thecpu_online_maskwhile the migration task moving the task over. Should the mask be already invalid, then it is rejected otherwise the operaton completes. Maybe we should also check ifcpus_read_lockis held during the invocation ofset_cpus_allowed_ptr()so we don't get this problem from other callers.
So sashiko complains about deadlock of cpus_write_lock() in the CPU down path and calling irq_migrate_all_off_this_cpu() vs and 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.
But there is nothing that waits for the interrupt thread to complete, there is no synchronize_irq() invocation which would be required for the deadlock.
Quoted 6 lines
- Should
__migrate_task()fail to return the requested rq make sure its CPU is parttask_struct::cpus_mask. This ensures that the current CPU is still part of mask avoiding a possible push bymigrate_enable().sched_class::set_cpus_alloweddid not see this mask. It might be a bit inconsistent and feels a bit likeselect_fallback_rq()without the printk.
Here 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 hold the CPU-hotplug lock and not every caller holds it. This seems to be limited to migrate-disable. Should this happen without it, then the during 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 41 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.Reported-by [email protected]Signed-off-by Sebastian Andrzej Siewior <[email protected]>--- kernel/irq/manage.c | 6 ++++-- kernel/sched/core.c | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-)kernel/irq/manage.c +4
-2@@ -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 +0
-0
kernel/sched/core.c static int migration_cpu_stop(void *data) · line 2681
Quoted 8 lines
kernel/sched/core.c +2
-0@@ -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; }
Sebastian
syzbot
syzbotForwarded: #syz test
In reply to syzbotFor archival purposes, forwarding an incoming command email to [email protected], [email protected].
For archival purposes, forwarding an incoming command email to [email protected], [email protected].
***
Subject: #syz test
Author: [email protected]
#syz test
init/main.c +0 -0
@@ -10,7 +10,8 @@
*/
#define DEBUG /* Enable initcall_debug */
+#error ASDF
#include <linux/types.h>
#include <linux/export.h>
#include <linux/extable.h>
syzbot
syzbotForwarded: #syz test
In reply to syzbotFor archival purposes, forwarding an incoming command email to [email protected], [email protected].
For archival purposes, forwarding an incoming command email to [email protected], [email protected].
***
Subject: #syz test
Author: [email protected]
#syz test