Skip to content

Commit eeeec71

Browse files
KAGA-KOKOSasha Levin
authored andcommitted
sched: Initialize idle tasks only once
[ Upstream commit b23decf ] Idle tasks are initialized via __sched_fork() twice: fork_idle() copy_process() sched_fork() __sched_fork() init_idle() __sched_fork() Instead of cleaning this up, sched_ext hacked around it. Even when analyis and solution were provided in a discussion, nobody cared to clean this up. init_idle() is also invoked from sched_init() to initialize the boot CPU's idle task, which requires the __sched_fork() invocation. But this can be trivially solved by invoking __sched_fork() before init_idle() in sched_init() and removing the __sched_fork() invocation from init_idle(). Do so and clean up the comments explaining this historical leftover. Signed-off-by: Thomas Gleixner <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sasha Levin <[email protected]>
1 parent 565c4e4 commit eeeec71

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

kernel/sched/core.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4488,7 +4488,8 @@ int wake_up_state(struct task_struct *p, unsigned int state)
44884488
* Perform scheduler related setup for a newly forked process p.
44894489
* p is forked by current.
44904490
*
4491-
* __sched_fork() is basic setup used by init_idle() too:
4491+
* __sched_fork() is basic setup which is also used by sched_init() to
4492+
* initialize the boot CPU's idle task.
44924493
*/
44934494
static void __sched_fork(unsigned long clone_flags, struct task_struct *p)
44944495
{
@@ -9257,8 +9258,6 @@ void __init init_idle(struct task_struct *idle, int cpu)
92579258
struct rq *rq = cpu_rq(cpu);
92589259
unsigned long flags;
92599260

9260-
__sched_fork(0, idle);
9261-
92629261
raw_spin_lock_irqsave(&idle->pi_lock, flags);
92639262
raw_spin_rq_lock(rq);
92649263

@@ -9273,10 +9272,8 @@ void __init init_idle(struct task_struct *idle, int cpu)
92739272

92749273
#ifdef CONFIG_SMP
92759274
/*
9276-
* It's possible that init_idle() gets called multiple times on a task,
9277-
* in that case do_set_cpus_allowed() will not do the right thing.
9278-
*
9279-
* And since this is boot we can forgo the serialization.
9275+
* No validation and serialization required at boot time and for
9276+
* setting up the idle tasks of not yet online CPUs.
92809277
*/
92819278
set_cpus_allowed_common(idle, &ac);
92829279
#endif
@@ -10105,6 +10102,7 @@ void __init sched_init(void)
1010510102
* but because we are the idle thread, we just pick up running again
1010610103
* when this runqueue becomes "idle".
1010710104
*/
10105+
__sched_fork(0, current);
1010810106
init_idle(current, smp_processor_id());
1010910107

1011010108
calc_load_update = jiffies + LOAD_FREQ;

0 commit comments

Comments
 (0)