From: Kuyo Chang <kuyo.chang@mediatek.com>
To: Ingo Molnar <mingo@redhat.com>,
Peter Zijlstra <peterz@infradead.org>,
Juri Lelli <juri.lelli@redhat.com>,
Vincent Guittot <vincent.guittot@linaro.org>,
Dietmar Eggemann <dietmar.eggemann@arm.com>,
Steven Rostedt <rostedt@goodmis.org>,
Ben Segall <bsegall@google.com>, Mel Gorman <mgorman@suse.de>,
Valentin Schneider <vschneid@redhat.com>,
Matthias Brugger <matthias.bgg@gmail.com>,
AngeloGioacchino Del Regno
<angelogioacchino.delregno@collabora.com>
Cc: kuyo chang <kuyo.chang@mediatek.com>,
<linux-kernel@vger.kernel.org>,
<linux-arm-kernel@lists.infradead.org>,
<linux-mediatek@lists.infradead.org>
Subject: [PATCH 1/1] sched/core: Fix migrate_swap() vs. hotplug
Date: Mon, 2 Jun 2025 15:22:13 +0800 [thread overview]
Message-ID: <20250602072242.1839605-1-kuyo.chang@mediatek.com> (raw)
From: kuyo chang <kuyo.chang@mediatek.com>
It encounters sporadic failures during CPU hotplug stress test.
[Syndrome]
The kernel log shows list add fail as below.
kmemleak: list_add corruption.
prev->next should be next (ffffff82812c7a00),
but was 0000000000000000. (prev=ffffff82812c3208).
kmemleak: kernel BUG at lib/list_debug.c:34!
kmemleak: Call trace:
kmemleak: __list_add_valid_or_report+0x11c/0x144
kmemleak: cpu_stop_queue_work+0x440/0x474
kmemleak: stop_one_cpu_nowait+0xe4/0x138
kmemleak: balance_push+0x1f4/0x3e4
kmemleak: __schedule+0x1adc/0x23bc
kmemleak: preempt_schedule_common+0x68/0xd0
kmemleak: preempt_schedule+0x60/0x80
kmemleak: _raw_spin_unlock_irqrestore+0x9c/0xa0
kmemleak: scan_gray_list+0x220/0x3e4
kmemleak: kmemleak_scan+0x410/0x740
kmemleak: kmemleak_scan_thread+0xb0/0xdc
kmemleak: kthread+0x2bc/0x494
kmemleak: ret_from_fork+0x10/0x20
[Analysis]
In the failure case, by memory dump, we find
cpu_stopper.enabled = TRUE
but the wakeq is empty(the migrate/1 is at another wakeq)
static bool cpu_stop_queue_work(...)
{
...
..
enabled = stopper->enabled;
if (enabled)
__cpu_stop_queue_work(stopper, work, &wakeq);
...
...
wake_up_q(&wakeq); -> wakeq is empty !!
preempt_enable();
return enabled;
}
Through analysis of the CPU0 call trace and memory dump
CPU0: migration/0, pid: 43, priority: 99
Native callstack:
vmlinux __kern_my_cpu_offset() <arch/arm64/include/asm/percpu.h:40>
vmlinux ct_state_inc(incby=8) <include/linux/context_tracking.h:137>
vmlinux rcu_momentary_eqs() + 72 <kernel/rcu/tree.c:375>
vmlinux multi_cpu_stop() + 316 <kernel/stop_machine.c:278>
vmlinux cpu_stopper_thread() + 676 <kernel/stop_machine.c:563>
vmlinux smpboot_thread_fn(data=0) + 1188 <kernel/smpboot.c:164>
vmlinux kthread() + 696 <kernel/kthread.c:389>
vmlinux 0xFFFFFFC08005941C() <arch/arm64/kernel/entry.S:845>
(struct migration_swap_arg *)0xFFFFFFC08FF87A40 (
src_task = 0xFFFFFF80FF519740 ,
dst_task = 0xFFFFFF802A579740 ,
src_cpu = 0x0,
dst_cpu = 0x1)
(struct multi_stop_data)* 0xFFFFFFC08FF87930 = (
fn = 0xFFFFFFC0802657F4 = migrate_swap_stop,
data = 0xFFFFFFC08FF87A40
num_threads = 0x2,
active_cpus = cpu_bit_bitmap[1] -> (
bits = (0x2)),
state = MULTI_STOP_PREPARE = 0x1,
thread_ack = (
counter = 0x1))
By cpu mask memory dump:
((const struct cpumask *)&__cpu_online_mask) (
bits = (0xFF))
((const struct cpumask *)&__cpu_dying_mask) (
bits = (0x2))
((const struct cpumask *)&__cpu_active_mask)(
bits = (0xFD))
((const struct cpumask *)&__cpu_possible_mask) (
bits = (0xFF))
->Imply cpu1 is dying & non-active
So, the potential race scenario is:
CPU0 CPU1
// doing migrate_swap(cpu0/cpu1)
stop_two_cpus()
...
// doing _cpu_down()
sched_cpu_deactivate()
set_cpu_active(cpu, false);
balance_push_set(cpu, true);
cpu_stop_queue_two_works
__cpu_stop_queue_work(stopper1,...);
__cpu_stop_queue_work(stopper2,..);
stop_cpus_in_progress -> true
preempt_enable();
...
1st balance_push
stop_one_cpu_nowait
cpu_stop_queue_work
__cpu_stop_queue_work
list_add_tail -> 1st add push_work
wake_up_q(&wakeq); -> "wakeq is empty.
This implies that the stopper is at wakeq@migrate_swap."
preempt_disable
wake_up_q(&wakeq);
wake_up_process // wakeup migrate/0
try_to_wake_up
ttwu_queue
ttwu_queue_cond ->meet below case
if (cpu == smp_processor_id())
return false;
ttwu_do_activate
//migrate/0 wakeup done
wake_up_process // wakeup migrate/1
try_to_wake_up
ttwu_queue
ttwu_queue_cond
ttwu_queue_wakelist
__ttwu_queue_wakelist
__smp_call_single_queue
preempt_enable();
2nd balance_push
stop_one_cpu_nowait
cpu_stop_queue_work
__cpu_stop_queue_work
list_add_tail -> 2nd add push_work, so the double list add is detected
...
...
cpu1 get ipi, do sched_ttwu_pending, wakeup migrate/1
[Solution]
Fix this race condition by adding cpus_read_lock/cpus_read_unlock around stop_two_cpus().
This ensures that no CPUs can come up or go down during this operation.
Signed-off-by: kuyo chang <kuyo.chang@mediatek.com>
---
kernel/sched/core.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 62b3416f5e43..1b371575206f 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -3441,6 +3441,8 @@ int migrate_swap(struct task_struct *cur, struct task_struct *p,
.dst_cpu = target_cpu,
};
+ /* Make sure no CPUs can come up or down */
+ cpus_read_lock();
if (arg.src_cpu == arg.dst_cpu)
goto out;
@@ -3461,6 +3463,7 @@ int migrate_swap(struct task_struct *cur, struct task_struct *p,
ret = stop_two_cpus(arg.dst_cpu, arg.src_cpu, migrate_swap_stop, &arg);
out:
+ cpus_read_unlock();
return ret;
}
#endif /* CONFIG_NUMA_BALANCING */
--
2.45.2
next reply other threads:[~2025-06-02 7:23 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-02 7:22 Kuyo Chang [this message]
2025-06-05 10:00 ` [PATCH 1/1] sched/core: Fix migrate_swap() vs. hotplug Peter Zijlstra
2025-06-06 3:46 ` Kuyo Chang
2025-06-06 8:28 ` Peter Zijlstra
2025-06-13 7:47 ` Kuyo Chang
2025-06-26 12:53 ` Peter Zijlstra
2025-06-26 2:23 ` [SPAM]Re: " Kuyo Chang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250602072242.1839605-1-kuyo.chang@mediatek.com \
--to=kuyo.chang@mediatek.com \
--cc=angelogioacchino.delregno@collabora.com \
--cc=bsegall@google.com \
--cc=dietmar.eggemann@arm.com \
--cc=juri.lelli@redhat.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mediatek@lists.infradead.org \
--cc=matthias.bgg@gmail.com \
--cc=mgorman@suse.de \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=vincent.guittot@linaro.org \
--cc=vschneid@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox