Skip to contentSkip to conversation

sched/core: Fix a TOCTOU bug in sched_non_preferred_cpu_push_stop()

PATCH Superseded Fushuai Wang 3 participants 4 messages Nov 18 – Nov 20 Scheduler

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 sched/core: Fix a TOCTOU bug in sched_non_preferred_cpu_push_stop()

From: Fushuai Wang <[email protected]>

sched_push_current_non_preferred_cpu() is called in irq context. It checks is_migration_disabled() before queuing the stopper.

migrate_disable() sets p->migration_disabled in preempt_disable() section. An irq can hit after preempt_disable() but before p->migration_disabled=1. After the irq returns, the task cannot be preempted by stopper becuase preemption is disabled, so it finishes setting p->migration_disabled=1 before the stopper runs.

It may be like:
task context: irq context:
migrate_disable()
preempt_disable()
is_migration_disabled()
migration_disabled = 1
preempt_enable()

So recheck is_migration_disabled() in sched_non_preferred_cpu_push_stop() before calling __migrate_task().

The stack may be like:

   WARNING: kernel/sched/core.c:3432 at set_task_cpu+0x1b8/0xdf0, CPU#21: migration/21/152
   CPU: 21 UID: 0 PID: 152 Comm: migration/21 Tainted: G        W  O        7.4.0-rc1 #3 PREEMPT(lazy)
   Stopper: sched_non_preferred_cpu_push_stop+0x0/0xb10 <- sched_push_current_non_preferred_cpu+0x6a6/0x9a0
   RIP: 0010:set_task_cpu+0x1b8/0xdf0
   Call Trace:
    <TASK>
    ? set_task_cpu+0x5/0xdf0
    move_queued_task+0xfc/0x650
    ? is_cpu_allowed+0x341/0x820
    ? __this_cpu_preempt_check+0x13/0x20
    sched_non_preferred_cpu_push_stop+0x7b7/0xb10
    ? __this_cpu_preempt_check+0x13/0x20
    ? __pfx_sched_non_preferred_cpu_push_stop+0x10/0x10
    ? __pfx_sched_non_preferred_cpu_push_stop+0x10/0x10
    ? sched_non_preferred_cpu_push_stop+0x5/0xb10
    ? __pfx_sched_non_preferred_cpu_push_stop+0x10/0x10
    cpu_stopper_thread+0x205/0x410
    ? smpboot_thread_fn+0x307/0xa60
    ? __pfx_cpu_stopper_thread+0x10/0x10
    smpboot_thread_fn+0x352/0xa60
    ? __pfx_smpboot_thread_fn+0x10/0x10
    kthread+0x3a6/0x4d0
    ? __pfx_kthread+0x10/0x10
    ret_from_fork+0x68e/0xb40
    ? __pfx_ret_from_fork+0x10/0x10
    ? __kasan_check_read+0x11/0x20
    ? __switch_to+0x592/0x1210
    ? __pfx_kthread+0x10/0x10
    ret_from_fork_asm+0x1a/0x30
    </TASK>
Fixes 74699f56ebcf ("sched/core: Push current task from non preferred CPU")

---
v1->v2: No code changes, just improve the commit message.

 kernel/sched/core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/sched/core.c +2 -1
index 5ed660f099a5..6f9374aa3f19 100644
@@ -11314,7 +11314,8 @@ static int sched_non_preferred_cpu_push_stop(void *arg)
 		rq->npc_push_work_pending = false;
 		update_rq_clock(rq);
 
-		if (task_rq(p) == rq && task_on_rq_queued(p)) {
+		if (task_rq(p) == rq && task_on_rq_queued(p) &&
+		    !is_migration_disabled(p)) {
 			struct rq *dest_rq = __migrate_task(rq, &rf, p, cpu);
 
 			if (rq != dest_rq)
Signature · 1 line
2.36.1
Reviewed-by on v2 1/1
#1 lore raw

Hi Fushuai. Thanks for the fix.

Quoted 54 lines · Fushuai Wang

From: Fushuai Wang <[email protected]>

sched_push_current_non_preferred_cpu() is called in irq context. It checks is_migration_disabled() before queuing the stopper.

migrate_disable() sets p->migration_disabled in preempt_disable() section. An irq can hit after preempt_disable() but before p->migration_disabled=1. After the irq returns, the task cannot be preempted by stopper becuase preemption is disabled, so it finishes setting p->migration_disabled=1 before the stopper runs.

It may be like:
task context: irq context:
migrate_disable()
preempt_disable()
is_migration_disabled()
migration_disabled = 1
preempt_enable()

So recheck is_migration_disabled() in sched_non_preferred_cpu_push_stop() before calling __migrate_task().

The stack may be like:

    WARNING: kernel/sched/core.c:3432 at set_task_cpu+0x1b8/0xdf0, CPU#21: migration/21/152
    CPU: 21 UID: 0 PID: 152 Comm: migration/21 Tainted: G        W  O        7.4.0-rc1 #3 PREEMPT(lazy)
    Stopper: sched_non_preferred_cpu_push_stop+0x0/0xb10 <- sched_push_current_non_preferred_cpu+0x6a6/0x9a0
    RIP: 0010:set_task_cpu+0x1b8/0xdf0
    Call Trace:
     <TASK>
     ? set_task_cpu+0x5/0xdf0
     move_queued_task+0xfc/0x650
     ? is_cpu_allowed+0x341/0x820
     ? __this_cpu_preempt_check+0x13/0x20
     sched_non_preferred_cpu_push_stop+0x7b7/0xb10
     ? __this_cpu_preempt_check+0x13/0x20
     ? __pfx_sched_non_preferred_cpu_push_stop+0x10/0x10
     ? __pfx_sched_non_preferred_cpu_push_stop+0x10/0x10
     ? sched_non_preferred_cpu_push_stop+0x5/0xb10
     ? __pfx_sched_non_preferred_cpu_push_stop+0x10/0x10
     cpu_stopper_thread+0x205/0x410
     ? smpboot_thread_fn+0x307/0xa60
     ? __pfx_cpu_stopper_thread+0x10/0x10
     smpboot_thread_fn+0x352/0xa60
     ? __pfx_smpboot_thread_fn+0x10/0x10
     kthread+0x3a6/0x4d0
     ? __pfx_kthread+0x10/0x10
     ret_from_fork+0x68e/0xb40
     ? __pfx_ret_from_fork+0x10/0x10
     ? __kasan_check_read+0x11/0x20
     ? __switch_to+0x592/0x1210
     ? __pfx_kthread+0x10/0x10
     ret_from_fork_asm+0x1a/0x30
     </TASK>
Quoted 2 lines
Fixes 74699f56ebcf ("sched/core: Push current task from non preferred CPU")
Quoted 20 lines

---
v1->v2: No code changes, just improve the commit message.

  kernel/sched/core.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
kernel/sched/core.c +2 -1
index 5ed660f099a5..6f9374aa3f19 100644
@@ -11314,7 +11314,8 @@ static int sched_non_preferred_cpu_push_stop(void *arg)
  		rq->npc_push_work_pending = false;
  		update_rq_clock(rq);
 
-		if (task_rq(p) == rq && task_on_rq_queued(p)) {
+		if (task_rq(p) == rq && task_on_rq_queued(p) &&
+		    !is_migration_disabled(p)) {
  			struct rq *dest_rq = __migrate_task(rq, &rf, p, cpu);
 
  			if (rq != dest_rq)
maintainer
#2 lore raw
In reply to Fushuai Wang
Quoted 19 lines · Fushuai Wang

From: Fushuai Wang <[email protected]>

sched_push_current_non_preferred_cpu() is called in irq context. It checks is_migration_disabled() before queuing the stopper.

migrate_disable() sets p->migration_disabled in preempt_disable() section. An irq can hit after preempt_disable() but before p->migration_disabled=1. After the irq returns, the task cannot be preempted by stopper becuase preemption is disabled, so it finishes setting p->migration_disabled=1 before the stopper runs.

It may be like:
task context: irq context:
migrate_disable()
preempt_disable()
is_migration_disabled()
migration_disabled = 1
preempt_enable()

Quoted 3 lines

So recheck is_migration_disabled() in sched_non_preferred_cpu_push_stop() before calling __migrate_task().

*sigh*:

https://lore.kernel.org/r/[email protected]

Quoted 13 lines
Fixes 74699f56ebcf ("sched/core: Push current task from non preferred CPU")

---
v1->v2: No code changes, just improve the commit message.

 kernel/sched/core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/sched/core.c +0 -0
index 5ed660f099a5..6f9374aa3f19 100644

kernel/sched/core.c static int sched_non_preferred_cpu_push_stop(void *arg) · line 11321

Quoted 9 lines
kernel/sched/core.c +2 -1
@@ -11314,7 +11314,8 @@ static int sched_non_preferred_cpu_push_stop(void *arg)
 		rq->npc_push_work_pending = false;
 		update_rq_clock(rq);
 
-		if (task_rq(p) == rq && task_on_rq_queued(p)) {
+		if (task_rq(p) == rq && task_on_rq_queued(p) &&
+		    !is_migration_disabled(p)) {
 			struct rq *dest_rq = __migrate_task(rq, &rf, p, cpu);
 
 			if (rq != dest_rq)

I think I like a comment with this. Perhaps add something like:

	/*
	 * Racy: is_migration_disabled(current) isn't stable.
	 */
	if (is_migration_disabled(push_task))
		return;

in sched_push_current_non_preferred_cpu().

Also, I wonder if you'd find more of this if you stick something like:

	lockdep_assert(!task_on_cpu(p) || in_task());

inside is_migration_disabled().

#3 lore raw

2026年11月19日 17:07, "Peter Zijlstra" <[email protected] mailto:[email protected]?to=%22Peter%20Zijlstra%22%20%3Cpeterz%40infradead.org%3E > 写到:

Quoted 21 lines
Quoted 12 lines
 
 kernel/sched/core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 5ed660f099a5..6f9374aa3f19 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -11314,7 +11314,8 @@ static int sched_non_preferred_cpu_push_stop(void *arg) rq->npc_push_work_pending = false;
update_rq_clock(rq);

Quoted 7 lines

- if (task_rq(p) == rq && task_on_rq_queued(p)) {
+ if (task_rq(p) == rq && task_on_rq_queued(p) &&
+ !is_migration_disabled(p)) {
struct rq *dest_rq = __migrate_task(rq, &rf, p, cpu);

if (rq != dest_rq)

I think I like a comment with this. Perhaps add something like:

Quoted 7 lines

/*
* Racy: is_migration_disabled(current) isn't stable. */
if (is_migration_disabled(push_task)) return;

in sched_push_current_non_preferred_cpu().

Hi, Peter

Will add it in v3.

Quoted 7 lines

Also, I wonder if you'd find more of this if you stick something like:

lockdep_assert(!task_on_cpu(p) || in_task());

inside is_migration_disabled().

I'll add it and run more tests.

Regards,
Fushuai.