探索 Linux 内核启动过程

目录

    • [1. Linux 内核启动过程概述](#1. Linux 内核启动过程概述)
    • [2. start_kernel (内核初始化)](#2. start_kernel (内核初始化))
    • [3. rest_init](#3. rest_init)
    • [4. kernel_init](#4. kernel_init)
    • [5. kthreadd](#5. kthreadd)
    • 总结

1. Linux 内核启动过程概述

在 Linux (2.6.39.4)系统的启动过程中,内核扮演了至关重要的角色。它负责从系统启动开始,初始化硬件、管理内存、加载文件系统,直到最终将控制权交给用户空间的第一个进程。本文将详细介绍 Linux 内核的启动过程,重点解析 rest_init, kernel_init 和 kthreadd 函数的作用,并概述内核初始化的调用关系。

在 Linux 系统启动时,内核的初始化过程分为多个阶段。以下是启动过程的主要步骤:

  • 引导加载(BootLoader):引导加载程序(如 GRUB)将内核镜像加载到内存中,并将控制权转交给内核。
  • 内核初始化(kernel_start):内核开始执行其初始化代码,包括硬件和内存的设置。
  • 启动内核线程(kthreadd):内核启动必要的内核线程和服务。
  • 启动用户空间进程(kernel_init):最终,内核启动第一个用户空间进程 init(/sbin/init 或者其他)。

2. start_kernel (内核初始化)

start_kernel 函数

  • start_kernel 函数是 Linux 内核初始化的关键部分。它负责设置和初始化内核在完全运行之前的必要子系统和基础设施。主要步骤包括:

    • 早期初始化:初始化锁机制(lockdep_init)、栈保护(boot_init_stack_canary),并禁用中断(local_irq_disable)。

    • 系统设置:执行 CPU 相关的设置(smp_setup_processor_id、boot_cpu_init)、内存管理初始化(page_address_init、page_alloc_init),并解析命令行(setup_command_line、parse_args)。

    • 内核基础设施:设置调度器(sched_init)、中断处理(init_IRQ)和调试功能(console_init、lockdep_info)。

    • 内存和资源管理:初始化内存管理和资源管理子系统,包括 kmem_cache_init_late、page_cgroup_init 和 vfs_caches_init。

    • 最终步骤:使用 rest_init 函数完成初始化,启动内核的主要进程。

start_kernel 和 init_task 的联系

  • start_kernel 函数:
    • start_kernel 是内核初始化过程的核心函数之一。它负责启动内核的各种子系统,创建和初始化系统线程(包括 kernel_init, kthreadd),设置 CPU 和内存管理等。
  • init_task 结构体:
    • init_task 是一个定义在内核中的特殊 task_struct 结构体实例。它代表了系统启动时的初始进程(PID 0),通常被称为 init 进程或 swapper 进程。
      init_task 被定义为 INIT_TASK(init_task),它是内核初始化过程中第一个创建的进程(线程),并且作为系统中的根进程存在。init_task 结构体在内核启动时设置好,然后由 start_kernel 进行初始化。

init_task 和 kernel_init的联系

  • init_task:
    • 这是内核中的第一个进程结构体,具有 PID 0。
    • init_task 不是一个真正运行的进程,而是内核为系统初始化和调度而创建的特殊结构体。它作为所有进程的基类,包含了最初的进程上下文。
  • kernel_init:
    • kernel_init 是内核中实际运行的进程,其 PID 通常是 1。
    • kernel_init 是内核启动过程中的关键进程,它负责进一步初始化系统、启动其他内核线程和最终启动用户空间的 init 进程。
  • 简而言之,init_task 是内核初始化时的基础结构体,而 kernel_init 是内核启动后的第一个实际运行的进程。

start_kernel 部分code

cpp 复制代码
asmlinkage void __init start_kernel(void)
{
	//......
	mm_init_owner(&init_mm, &init_task);
	mm_init();
	/*
	 * Set up the scheduler prior starting any interrupts (such as the
	 * timer interrupt). Full topology setup happens at smp_init()
	 * time - but meanwhile we still have a functioning scheduler.
	 */
	sched_init();
	time_init();
	/*
	 * HACK ALERT! This is early. We're enabling the console before
	 * we've done PCI setups etc, and console_init() must be aware of
	 * this. But we do want output early, in case something goes wrong.
	 */
	console_init();
	/* Do the rest non-__init'ed, we're now alive */
	rest_init();
}

3. rest_init

rest_init 函数是 Linux 内核启动过程中重要的一部分,用于完成内核初始化的最后步骤。它主要负责启动第一个用户空间进程 init(PID 1) 和内核线程 kthreadd,并设置初始的空闲任务。

rest_init code

cpp 复制代码
static noinline void __init_refok rest_init(void)
{
	int pid;

	rcu_scheduler_starting();
	/*
	 * We need to spawn init first so that it obtains pid 1, however
	 * the init task will end up wanting to create kthreads, which, if
	 * we schedule it before we create kthreadd, will OOPS.
	 */
	kernel_thread(kernel_init, NULL, CLONE_FS | CLONE_SIGHAND);
	numa_default_policy();
	pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
	rcu_read_lock();
	kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
	rcu_read_unlock();
	complete(&kthreadd_done);

	/*
	 * The boot idle thread must execute schedule()
	 * at least once to get things moving:
	 */
	init_idle_bootup_task(current);
	preempt_enable_no_resched();
	schedule(); //启动调度
	preempt_disable();

	/* Call into cpu_idle with preempt disabled */
	cpu_idle(); //进入空闲状态
}

4. kernel_init

kernel_init 函数通过这些步骤确保系统的基本初始化和配置,接下来系统将进入用户空间并继续启动其他进程
rest_init code

cpp 复制代码
static int __init kernel_init(void * unused)
{
	/*
	 * Wait until kthreadd is all set-up.
	 */
	wait_for_completion(&kthreadd_done); //等待 kthreadd 线程初始化完成
	/*
	 * init can allocate pages on any node
	 */
	set_mems_allowed(node_states[N_HIGH_MEMORY]); //设置内存允许的节点
	/*
	 * init can run on any cpu.
	 */
	set_cpus_allowed_ptr(current, cpu_all_mask); //设置 CPU 允许的掩码

	cad_pid = task_pid(current); //获取进程 ID

	smp_prepare_cpus(setup_max_cpus);

	do_pre_smp_initcalls();
	lockup_detector_init();

	smp_init(); //进行 SMP 初始化
	sched_init_smp(); //初始化调度器

	do_basic_setup(); //执行基础系统设置,如初始化文件系统等

	/* Open the /dev/console on the rootfs, this should never fail */
	if (sys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
		printk(KERN_WARNING "Warning: unable to open an initial console.\n");

	(void) sys_dup(0);
	(void) sys_dup(0);
	/*
	 * check if there is an early userspace init.  If yes, let it do all
	 * the work
	 */

	if (!ramdisk_execute_command)
		ramdisk_execute_command = "/init";

	if (sys_access((const char __user *) ramdisk_execute_command, 0) != 0) {
		ramdisk_execute_command = NULL;
		prepare_namespace();
	}

	/*
	 * Ok, we have completed the initial bootup, and
	 * we're essentially up and running. Get rid of the
	 * initmem segments and start the user-mode stuff..
	 */

	init_post(); //完成初始化并开始用户空间进程
	return 0;
}

init_post code

init_post 确保在系统启动过程中能够找到并执行一个有效的初始用户空间进程,确保系统能够正常启动和运行。

cpp 复制代码
static noinline int init_post(void)
{
	/* need to finish all async __init code before freeing the memory */
	async_synchronize_full();
	free_initmem();
	mark_rodata_ro();
	system_state = SYSTEM_RUNNING;
	numa_default_policy();


	current->signal->flags |= SIGNAL_UNKILLABLE;

	if (ramdisk_execute_command) {
		run_init_process(ramdisk_execute_command);
		printk(KERN_WARNING "Failed to execute %s\n",
				ramdisk_execute_command);
	}

	/*
	 * We try each of these until one succeeds.
	 *
	 * The Bourne shell can be used instead of init if we are
	 * trying to recover a really broken machine.
	 */
	if (execute_command) {
		run_init_process(execute_command);
		printk(KERN_WARNING "Failed to execute %s.  Attempting "
					"defaults...\n", execute_command);
	}
	run_init_process("/sbin/init");
	run_init_process("/etc/init");
	run_init_process("/bin/init");
	run_init_process("/bin/sh");

	panic("No init found.  Try passing init= option to kernel. "
	      "See Linux Documentation/init.txt for guidance.");
}

5. kthreadd

kthreadd 函数负责管理内核线程。以下是它的关键部分:

  • 初始化上下文:
    • set_task_comm(tsk, "kthreadd"); 设置线程名称为 "kthreadd"。
    • ignore_signals(tsk); 使线程忽略信号。
    • set_cpus_allowed_ptr(tsk, cpu_all_mask); 允许线程在所有 CPU 上运行。
    • set_mems_allowed(node_states[N_HIGH_MEMORY]); 设置内存分配约束。
  • 设置标志:
    • current->flags |= PF_NOFREEZE | PF_FREEZER_NOSIG; 防止线程被冻结或接收冻结信号。
  • 主循环:
    • 线程不断检查 kthread_create_list 列表。当列表为空时,线程会休眠(set_current_state(TASK_INTERRUPTIBLE); schedule();),当有新的线程需要创建时会唤醒。
    • 使用 spin_lock 来保护列表,在遍历和创建新线程时确保线程安全。
  • 线程创建:
    • create_kthread(create); 从 kthread_create_info 结构中创建新的内核线程。
      kthreadd 函数确保内核线程的创建和管理正确进行,同时维护线程安全和调度。

kthreadd code

cpp 复制代码
int kthreadd(void *unused)
{
	struct task_struct *tsk = current;

	/* Setup a clean context for our children to inherit. */
	set_task_comm(tsk, "kthreadd");
	ignore_signals(tsk);
	set_cpus_allowed_ptr(tsk, cpu_all_mask);
	set_mems_allowed(node_states[N_HIGH_MEMORY]);

	current->flags |= PF_NOFREEZE | PF_FREEZER_NOSIG;

	for (;;) {
		set_current_state(TASK_INTERRUPTIBLE);
		if (list_empty(&kthread_create_list))
			schedule();
		__set_current_state(TASK_RUNNING);

		spin_lock(&kthread_create_lock);
		while (!list_empty(&kthread_create_list)) {
			struct kthread_create_info *create;

			create = list_entry(kthread_create_list.next,
					    struct kthread_create_info, list);
			list_del_init(&create->list);
			spin_unlock(&kthread_create_lock);

			create_kthread(create);

			spin_lock(&kthread_create_lock);
		}
		spin_unlock(&kthread_create_lock);
	}

	return 0;
}

总结

Linux 内核的启动过程是一个系统化的流程,各个函数和机制通过精确的调用关系确保系统的正常启动。start_kernel 函数为启动过程奠定基础,init_task(pid = 0) 代表了系统的第一个进程。在rest_init 函数创建 kernel_init (启动用户空间进程 init, pid = 1)和 kthreadd 进程,kthreadd 是内核线程管理的起点。通过理解这些关键函数及其相互关系,可以更深入地掌握 Linux 内核的启动机制,并为系统的稳定运行提供支持。

相关推荐
清风 0012 小时前
在 Linux 上以 All-in-One 模式安装 KubeSphere
linux·运维·服务器
小白学习记录555552 小时前
香橙派转换模型以及在开发板上部署
linux·运维·服务器
tRNA做科研3 小时前
Bio-Linux-shell详解-1-从0开始
linux·运维·服务器
小黑爱编程3 小时前
【Linux网络】Socket套接字
linux·运维·网络
xyt0_03 小时前
【Linux】常见指令
linux
gs801404 小时前
安装node 报错需要:glibc >= 2.28
linux·服务器·前端·node.js
何老生6 小时前
Linux之MySQL主从复制
linux·运维·mysql
ggdpzhk6 小时前
图片详解,最简单易懂!!!Ubuntu增强功能
linux·ubuntu
神奇椰子7 小时前
Ubuntu 常用指令和作用解析
linux·运维·ubuntu·centos·云计算·代码规范·浪浪云
望获linux7 小时前
Linux网络协议栈的实现
linux·服务器·arm开发·网络协议·操作系统·嵌入式操作系统