Skip to content

Commit de9b8f5

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
sched: Fix crash trying to dequeue/enqueue the idle thread
Sasha reports that his virtual machine tries to schedule the idle thread since commit 6c37067 ("sched: Change the sched_class::set_cpus_allowed() calling context"). Hit trace shows this happening from idle_thread_get()->init_idle(), which is the _second_ init_idle() invocation on that task_struct, the first being done through idle_init()->fork_idle(). (this code is insane...) Because we call init_idle() twice in a row, its ->sched_class == &idle_sched_class and ->on_rq = TASK_ON_RQ_QUEUED. This means do_set_cpus_allowed() think we're queued and will call dequeue_task(), which is implemented with BUG() for the idle class, seeing how dequeueing the idle task is a daft thing. Aside of the whole insanity of calling init_idle() _twice_, change the code to call set_cpus_allowed_common() instead as this is 'obviously' before the idle task gets ran etc.. Reported-by: Sasha Levin <[email protected]> Tested-by: Sasha Levin <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Thomas Gleixner <[email protected]> Fixes: 6c37067 ("sched: Change the sched_class::set_cpus_allowed() calling context") Signed-off-by: Ingo Molnar <[email protected]>
1 parent a7d5c18 commit de9b8f5

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

kernel/sched/core.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4927,7 +4927,15 @@ void init_idle(struct task_struct *idle, int cpu)
49274927
idle->state = TASK_RUNNING;
49284928
idle->se.exec_start = sched_clock();
49294929

4930-
do_set_cpus_allowed(idle, cpumask_of(cpu));
4930+
#ifdef CONFIG_SMP
4931+
/*
4932+
* Its possible that init_idle() gets called multiple times on a task,
4933+
* in that case do_set_cpus_allowed() will not do the right thing.
4934+
*
4935+
* And since this is boot we can forgo the serialization.
4936+
*/
4937+
set_cpus_allowed_common(idle, cpumask_of(cpu));
4938+
#endif
49314939
/*
49324940
* We're having a chicken and egg problem, even though we are
49334941
* holding rq->lock, the cpu isn't yet set to this cpu so the
@@ -4944,7 +4952,7 @@ void init_idle(struct task_struct *idle, int cpu)
49444952

49454953
rq->curr = rq->idle = idle;
49464954
idle->on_rq = TASK_ON_RQ_QUEUED;
4947-
#if defined(CONFIG_SMP)
4955+
#ifdef CONFIG_SMP
49484956
idle->on_cpu = 1;
49494957
#endif
49504958
raw_spin_unlock(&rq->lock);
@@ -4959,7 +4967,7 @@ void init_idle(struct task_struct *idle, int cpu)
49594967
idle->sched_class = &idle_sched_class;
49604968
ftrace_graph_init_idle_task(idle, cpu);
49614969
vtime_init_idle(idle, cpu);
4962-
#if defined(CONFIG_SMP)
4970+
#ifdef CONFIG_SMP
49634971
sprintf(idle->comm, "%s/%d", INIT_TASK_COMM, cpu);
49644972
#endif
49654973
}

0 commit comments

Comments
 (0)