1. 前言
由“Linux CPU core的電源管理(1)_概述”的描述可知,kernel cpu control位於“.\kernel\cpu.c”中,是一個承上啟下的模組,負責屏蔽arch-dependent的實現細節,向上層軟體提供控制CPU core的統一API(主要包括cpu_up/cpu_down等介面的實現)。本文將基於這些API,從上到下,分析CPU core從啟動到關閉的整個過程(主要是CPU hotplug),進一步理解系統運行過程中CPU core電源管理相關的行為。
注1:其實這一部分已經不屬於電源管理的範疇了,而是系統級的軟體行為(boot、調度、電源管理等等),之所以放到這裡講述,主要原因是,這些複雜行為的背後,目的只有一個----節電。因此,本文只會focus在CPU core power狀態切換的過程上,涉及到得其它知識,如進程調度,只會一筆帶過。 2. possible/present/active/online cpus
前面文章提到過,kernel使用4個bitmap,來儲存分別處於4種狀態的CPU core:possible、present、active和online。這四個狀態的意義到底是什麼。下面我們根據相關的代碼邏輯,來解答這個問題。
開始之前,先看一下kernel中對他們的注釋:
1: /* include/linux/cpumask.h */
2:
3:
4: /*
5: * The following particular system cpumasks and operations manage
6: * possible, present, active and online cpus.
7: *
8: * cpu_possible_mask- has bit 'cpu' set iff cpu is populatable
9: * cpu_present_mask - has bit 'cpu' set iff cpu is populated
10: * cpu_online_mask - has bit 'cpu' set iff cpu available to scheduler
11: * cpu_active_mask - has bit 'cpu' set iff cpu available to migration
12: *
13: * If !CONFIG_HOTPLUG_CPU, present == possible, and active == online.
14: *
15: * The cpu_possible_mask is fixed at boot time, as the set of CPU id's
16: * that it is possible might ever be plugged in at anytime during the
17: * life of that system boot. The cpu_present_mask is dynamic(*),
18: * representing which CPUs are currently plugged in. And
19: * cpu_online_mask is the dynamic subset of cpu_present_mask,
20: * indicating those CPUs available for scheduling.
21: *
22: * If HOTPLUG is enabled, then cpu_possible_mask is forced to have
23: * all NR_CPUS bits set, otherwise it is just the set of CPUs that
24: * ACPI reports present at boot.
25: *
26: * If HOTPLUG is enabled, then cpu_present_mask varies dynamically,
27: * depending on what ACPI reports as currently plugged in, otherwise
28: * cpu_present_mask is just a copy of cpu_possible_mask.
29: */
30:
大意是這樣的:
possible狀態的CPU意味著是“populatable(覺得這個單詞還沒有possible易懂)”的,可理解為存在這個CPU資源,但還沒有納入Kernel的管理範圍;
present狀態的CPU,是已經“populated”的CPU,可理解為已經被kernel接管;
online狀態的CPU,表示可以被調度器使用;
active狀態的CPU,表示可以被migrate(什麼意思。);
如果系統沒有使能CPU Hotplug功能,則present等於possible,active等於online。
還真不是很容易理解,不急,我們一個一個分析。 2.1 possible CPU
possible的CPUs,代表了系統中可被使用的所有的CPU,在boot階段確定之後,就不會再修改。以ARM64為例,其初始化的過程如下。
1)系統上電後,boot CPU啟動,執行start_kernel(init/main.c),並分別調用boot_cpu_init和setup_arch兩個介面,進行possible CPU相關的初始化。
2)boot_cpu_init負責將當前的boot CPU放到possible CPU的bitmap中,同理,boot CPU也是present、oneline、active CPU(因此,後續的描述,都是針對非boot CPU的)。如下:
1: /* init/main.c */
2:
3: static void __init boot_cpu_init(void)
4: {
5: int cpu = smp_processor_id();
6: /* Mark the boot cpu "present", "online" etc for SMP and UP case */
7: set_cpu_online(cpu, true);
8: set_cpu_active(cpu, true);
9: set_cpu_present(cpu, true);
10: set_cpu_possible(cpu, true);
11: }
smp_processor_id用於擷取當前的CPU id;
set_cpu_xxx介面,可以將指定的CPU設定為(或者清除)指定的狀態。
3)setup_arch負責根據MPIDR寄存器,以及DTS配置,解析並設定其它的possible CPU,如下:
1: /* arch/arm64/kernel/setup.c */
2:
3: void __init setup_arch(char **cmdline_p)
4: {
5: ...
6: cpu_logical_map(0) = read_cpuid_mpidr() & MPIDR_HWID_BITMASK;
7: cpu_read_bootcpu_ops();
8: #ifdef CONFIG_SMP
9: smp_init_cpus();
10: smp_build_mpidr_hash();
11: #endif
12: ...
13: }
14:
3a)cpu_logical_map數組
kernel使用一個整形數組(cpu_logical_map,定義如下),儲存物理CPU(由ID標示)和邏輯CPU(數組的index)之間的映射,該數組的長度由NR_CPUS決定。
1: /* arch/arm64/include/asm/smp_plat.h */
2:
3: /*
4: * Logical CPU mapping.
5: */
6: extern u64 __cpu_logical_map[NR_CPUS];
7: #define cpu_logical_map(cpu) __cpu_logical_map[cpu]
上面setup_arch代碼的第六行,通過read_cpuid_mpidr介面,讀取當前CPU(boot CPU)的ID(物理ID),並儲存在map表的第一個位置。
3b)smp_init_cpus
如果使能了SMP,則調用smp_init_cpus介面,完成如下事情:
從DTS中解析其它CPU的HW ID(通過‘reg’關鍵字,如下),並儲存在cpu_logical_map數組中;
對所有cpu_logical_map數組中的CPU,執行set_cpu_possible操作,將它們設定為possible狀態。
1: {
2: ...
3: cpus {
4: #address-cells = <2>;
5: #size-cells = <0>;
6:
7: cpu@0 {
8: device_type = "cpu";
9: compatible = "arm,cortex-a53", "arm,armv8";
10: reg = <0x0 0x0>;
11: enable-method = "psci";
12: cpu-idle-states = <&CPU_SLEEP_0 &CPU_SLEEP_1>;
13: };
14:
15: cpu@1 {
16: device_type = "cpu";
17: compatible = "arm,cortex-a53", "arm,armv8";
18: reg = <0x0 0x1>;
19: enable-method = "psci";
20: cpu-idle-states = <&CPU_SLEEP_0 &CPU_SLEEP_1>;
21: };
22: ...
23: };
24: ...
25: }
26:
CPU DTS檔案樣本。
4)總結
對ARM64來說,possible的CPU,就是在DTS中指定了的,物理存在的CPU core。 2.2 present CPU
還是以ARM64為例,“start_kernel—>setup_arch”成功執行之後,繼續執行“start_kernel—>rest_init—>kernel_init(pid 1,init task)—>kernel_init_freeable”,在kernel_init_freeable中會調用arch-dependent的介面:smp_prepare_cpus,該介面主要的主要功能有兩個:
1)構建系統中CPU的拓撲結構,具體可參考“Linux CPU core的電源管理(2)_cpu topology”。
2)拓撲結構構建完成後,根據CPU的拓撲,初始化系統的present CPU mask,代碼如下:
1: void __init smp_prepare_cpus(unsigned int max_cpus)
2: {
3: ...
4: /* Don't bother if we're effectively UP */
5: if (max_cpus <= 1)
6: return;
7:
8: /*
9: * Initialise the present map (which describes the set of CPUs
10: * actually populated at the present time) and release the
11: * secondaries from the bootloader.
12: *
13: * Make sure we online at most (max_cpus - 1) additional CPUs.
14: */
15: max_cpus--;
16: for_each_possible_cpu(cpu) {
17: if (max_cpus == 0)
18: break;
19:
20: if (cpu == smp_processor_id())
21: continue;
22:
23: if (!cpu_ops[cpu])
24: continue;
25:
26: err = cpu_ops[cpu]->cpu_prepare(cpu);
27: if (err)
28: continue;
29:
30: set_cpu_present(cpu, true);
31: max_cpus--;
32: }
33: }
4~6行:當然,如果CPU個數不大於1,則不是SMP系統,就沒有後續的概念,直接返回。
16~32行,輪詢所有的possib