Scheduler · Linux 6.18.37 LTS · source + diagram note

sched_init

최신 LTS 기준: kernel.org longterm 6.18.37 기준으로 파일 위치와 링크를 맞춘 원본 코드 분석 노트입니다. runqueue, idle task, scheduler class 초기화.

전체 흐름

start_kernel()은 timer interrupt를 켜기 전에 sched_init()을 호출합니다. 이 함수는 scheduler class의 링커 순서가 이미 맞는지 검사하고, possible CPU의 runqueue를 준비한 뒤 부트 CPU의 현재 task를 idle task로 연결합니다. class를 여기서 동적으로 목록에 연결하는 과정은 아닙니다.

그림 1. v6.18.37 sched_init()의 네 주요 단계와 후속 SMP 초기화
start_kernel에서 sched_init 호출 후 class 순서 검사, possible CPU별 runqueue 초기화, 부트 CPU idle task 연결, scheduler_running 설정을 수행한다. 뒤의 sched_init_smp는 별도 호출이며 SMP topology를 초기화한다.

그림은 주요 검증점만 보여 주며 사이의 조건부·보조 초기화는 생략합니다. 단계는 start_kernel():978–983, class 검사:8624–8637, per-CPU rq:8689–8776, boot idle와 완료:8801–8818에 고정했습니다. sched_init_smp()은 smp_init() 이후의 별도 단계입니다.

소스 코드 위치

파일여기서 확인할 내용
init/main.cinterrupt 이전 sched_init() 호출
kernel/sched/core.cclass 순서 검사, possible-CPU rq 초기화, boot idle binding
core.c의 init_idlerq->idle, rq->donor, rq->curr와 idle class 설정
init/main.c의 후속 호출smp_init() 다음 sched_init_smp(); 부트 시점 앞의 네 검증 단계와 구분

kernel/sched/fair.c와 include/linux/sched.h는 이후 class 동작과 task 필드를 추적할 때 함께 봅니다. 아래 발췌는 보관된 v6.18.37 core.c의 표시 범위와 줄별로 일치합니다.

대표 코드

sched_init: class 순서 검증

원본: kernel/sched/core.c:8624–8637

void __init sched_init(void)
{
	unsigned long ptr = 0;
	int i;

	/* Make sure the linker didn't screw up */
	BUG_ON(!sched_class_above(&stop_sched_class, &dl_sched_class));
	BUG_ON(!sched_class_above(&dl_sched_class, &rt_sched_class));
	BUG_ON(!sched_class_above(&rt_sched_class, &fair_sched_class));
	BUG_ON(!sched_class_above(&fair_sched_class, &idle_sched_class));
#ifdef CONFIG_SCHED_CLASS_EXT
	BUG_ON(!sched_class_above(&fair_sched_class, &ext_sched_class));
	BUG_ON(!sched_class_above(&ext_sched_class, &idle_sched_class));
#endif

읽는 지점: sched_class_above()는 링커가 배치한 stop→DL→RT→fair→idle 순서를 검사합니다. CONFIG_SCHED_CLASS_EXT의 추가 검사는 조건부입니다.

sched_init: possible CPU의 rq

원본: kernel/sched/core.c:8689–8699

	for_each_possible_cpu(i) {
		struct rq *rq;

		rq = cpu_rq(i);
		raw_spin_lock_init(&rq->__lock);
		rq->nr_running = 0;
		rq->calc_load_active = 0;
		rq->calc_load_update = jiffies + LOAD_FREQ;
		init_cfs_rq(&rq->cfs);
		init_rt_rq(&rq->rt);
		init_dl_rq(&rq->dl);

읽는 지점: for_each_possible_cpu는 아직 online인 CPU만 도는 루프가 아닙니다. 각 rq의 기본 계수와 CFS/RT/DL 하위 큐를 준비합니다.

sched_init: 부트 idle task와 완료

원본: kernel/sched/core.c:8801–8818

	__sched_fork(0, current);
	init_idle(current, smp_processor_id());

	calc_load_update = jiffies + LOAD_FREQ;

	idle_thread_set_boot_cpu();

	balance_push_set(smp_processor_id(), false);
	init_sched_fair_class();
	init_sched_ext_class();

	psi_init();

	init_uclamp();

	preempt_dynamic_init();

	scheduler_running = 1;

읽는 지점: __sched_fork() 다음 init_idle(current, boot CPU)을 호출합니다. 이후 fair/ext class와 PSI·uclamp·동적 선점 준비가 끝나야 scheduler_running이 1입니다.

init_idle: rq에 boot idle 연결

원본: kernel/sched/core.c:8000–8019

	rcu_read_lock();
	__set_task_cpu(idle, cpu);
	rcu_read_unlock();

	rq->idle = idle;
	rq_set_donor(rq, idle);
	rcu_assign_pointer(rq->curr, idle);
	idle->on_rq = TASK_ON_RQ_QUEUED;
	idle->on_cpu = 1;
	raw_spin_rq_unlock(rq);
	raw_spin_unlock_irqrestore(&idle->pi_lock, flags);

	/* Set the preempt count _outside_ the spinlocks! */
	init_idle_preempt_count(idle, cpu);

	/*
	 * The idle tasks have their own, simple scheduling class:
	 */
	idle->sched_class = &idle_sched_class;
	ftrace_graph_init_idle_task(idle, cpu);

읽는 지점: rq->idle과 donor/curr를 boot idle task에 연결하고, lock을 푼 뒤 idle class를 설정합니다. 가능한 모든 CPU의 idle task가 여기서 한꺼번에 만들어진다는 뜻은 아닙니다.

함수별 분석

범위행위혼동하기 쉬운 경계
sched_init() 시작linker class 순서가 stop·DL·RT·fair·idle 기준과 맞는지 BUG_ON으로 검증class를 지금 연결하는 작업이 아니라 이미 배치된 순서의 검사
possible-CPU looprq 잠금·계수·CFS/RT/DL 하위 큐·root domain 등을 CPU별 초기화rq->online=0도 설정됩니다. CPU online/hotplug 절차가 끝났다는 뜻이 아님
boot task__sched_fork(0,current), init_idle(current,smp_processor_id())PID 0 부트 idle task와 이후의 PID 1 사용자 공간 init을 구분
뒤의 sched_init_smp()smp_init() 후 domain·RT/DL class 초기화sched_init() 내부 호출이나 같은 부트 순간으로 합치지 않음

구조체와 필드

객체·필드코드에서 확인되는 역할
struct rqCPU별 runqueue. cpu_rq(i)로 잡고 lock·계수·CFS/RT/DL 하위 큐와 balance 관련 필드를 초기화합니다.
rq->online초기 루프에서 0으로 설정합니다. possible CPU 자료 준비와 online 전환은 별개입니다.
rq->idle / donor / currboot CPU에서는 init_idle()이 현재 task를 이 관계에 연결합니다.
root_task_groupFAIR/RT group과 CGROUP 관련 필드는 각각의 CONFIG_* 분기에서 준비합니다. 모든 항목을 무조건 실행 경로로 읽지 않습니다.
그림 2. v6.18.37 부트 CPU의 idle task 참조 관계
boot CPU의 currentinit_idle(current, smp_processor_id())에 전달되는 idle task
rq->idleCPU의 idle task 포인터
rq->donorrq_set_donor(rq, idle)의 대상
rq->currrcu_assign_pointer(..., idle)의 대상

core.c:8000–8018. 이 그림은 부트 CPU 하나의 관계다. possible CPU마다 idle task를 이 호출에서 할당한다는 뜻은 아니다.

실행 단계와 상태 변화

확인 순서는 class 순서 검증 → possible-CPU rq 준비 → boot idle binding → scheduler_running=1입니다. smp_init()과 sched_init_smp()은 뒤에 옵니다. 그림의 화살표는 함수 안에서 확인된 주요 단계만 나타내며 모든 CPU가 online이 됐다는 뜻이 아닙니다.

init_idle()은 boot CPU의 rq->curr에 현재 task를 연결합니다. 아직 init 사용자 프로세스(PID 1)의 일반 스케줄링으로 넘어간 상태로 해석하지 않습니다.

관찰 지점v6.6v6.18.37
class 순서 검사인접한 class 객체 주소 비교. stop class 검사는 CONFIG_SMP 조건부.sched_class_above() 검사. EXT class의 두 관계는 CONFIG_SCHED_CLASS_EXT 조건부.
부트 idle의 rq 포인터rq->idle, rq->curr 설정.rq->idle과 rq_set_donor(rq, idle), rq->curr 설정.
후기 sched_init_smp()domain·NUMA와 RT/DL class 설정. CONFIG_SMP가 아니면 granularity만 설정.domain·NUMA와 RT/DL class에 더해 sched_init_dl_servers() 호출.

v6.6 init_idle · v6.6 sched_init · v6.18.37 init_idle · v6.18.37 sched_init_smp

불변 조건과 실패 사례

  • class 순서 검사가 실패하면 초기화는 BUG_ON에서 멈춥니다. 이 경로를 자원 획득 후 일반적인 rollback으로 그리지 않습니다.
  • possible CPU의 rq를 준비하는 일과 실제 CPU online/hotplug는 다른 수명 단계입니다.
  • boot idle task의 rq->idle, donor와 curr 연결을 한 객체 관계로 추적하되 idle class 지정 위치는 init_idle() 후반입니다.
  • idle task(PID 0 계열)와 userspace init(PID 1)의 역할을 섞지 않습니다.

계측과 검증

부트 중 멈춘 지점을 먼저 구분합니다. class 순서 검사는 BUG_ON 위치, CPU별 rq 준비는 loop와 rq->online, boot idle 연결은 rq->idle/donor/curr 값이 각각 검증점입니다. 일반 런타임의 perf sched 결과로 이 초기화 순서를 직접 증명할 수는 없습니다.

빌드 구성의 CONFIG_SCHED_CLASS_EXT, CONFIG_CGROUP_SCHED와 boot CPU 번호를 함께 기록해야 조건부 행을 정확히 읽을 수 있습니다.

추가 확인 사항

  • 대상 빌드의 class linker 순서와 CONFIG_SCHED_CLASS_EXT 설정은 무엇인가?
  • for_each_possible_cpu의 CPU mask와 실제 online mask는 부트 시점에 어떻게 다른가?
  • 부트 CPU rq->curr와 이후 CPU의 idle task가 어느 호출에서 각각 연결되는가?