CPU 热插拔, 或者是 online/offline, 是 linux 支持的一个功能.
要使用这个功能,先要在内核配置选项里选中相关的选项:
CONFIG_HOTPLUG=y
CONFIG_HOTPLUG_CPU=y
CONFIG_SMP=y
系统启动后,执行:
root@taotao:/sys/devices/system/cpu/cpu1#echo 0 > online CPU1: shutdown
root@taotao:/sys/devices/system/cpu/cpu1#cat /proc/cpuinfo processor : 0 model name : ARMv7 Processor rev 0 (v7l) BogoMIPS : 587.36 Features : swp half thumb fastmult vfp edsp neon vfpv3 tls CPU implementer : 0x41 CPU architecture: 7 CPU variant : 0x0 CPU part : 0xc09 CPU revision : 0 processor : 2 model name : ARMv7 Processor rev 0 (v7l) BogoMIPS : 589.00 Features : swp half thumb fastmult vfp edsp neon vfpv3 tls CPU implementer : 0x41 CPU architecture: 7 CPU variant : 0x0 CPU part : 0xc09 CPU revision : 0 processor : 3 model name : ARMv7 Processor rev 0 (v7l) BogoMIPS : 611.94 Features : swp half thumb fastmult vfp edsp neon vfpv3 tls CPU implementer : 0x41 CPU architecture: 7 CPU variant : 0x0 CPU part : 0xc09 CPU revision : 0 Hardware : ARM-Versatile Express Revision : 0000 Serial : 0000000000000000
下面重新挂上 CPU1:
root@taotao:/sys/devices/system/cpu/cpu1#echo 1 > online CPU1: 13 spurious wakeup calls CPU1: Booted secondary processor
root@taotao:/sys/devices/system/cpu/cpu1#cat /proc/cpuinfo processor : 0 model name : ARMv7 Processor rev 0 (v7l) BogoMIPS : 587.36 Features : swp half thumb fastmult vfp edsp neon vfpv3 tls CPU implementer : 0x41 CPU architecture: 7 CPU variant : 0x0 CPU part : 0xc09 CPU revision : 0 processor : 1 model name : ARMv7 Processor rev 0 (v7l) BogoMIPS : 1219.78 Features : swp half thumb fastmult vfp edsp neon vfpv3 tls CPU implementer : 0x41 CPU architecture: 7 CPU variant : 0x0 CPU part : 0xc09 CPU revision : 0 processor : 2 model name : ARMv7 Processor rev 0 (v7l) BogoMIPS : 589.00 Features : swp half thumb fastmult vfp edsp neon vfpv3 tls CPU implementer : 0x41 CPU architecture: 7 CPU variant : 0x0 CPU part : 0xc09 CPU revision : 0 processor : 3 model name : ARMv7 Processor rev 0 (v7l) BogoMIPS : 611.94 Features : swp half thumb fastmult vfp edsp neon vfpv3 tls CPU implementer : 0x41 CPU architecture: 7 CPU variant : 0x0 CPU part : 0xc09 CPU revision : 0 Hardware : ARM-Versatile Express Revision : 0000 Serial : 0000000000000000
但是遇到一个问题,去卸载 CPU0的时候,没有成功,提示不允许:
root@taotao:/sys/devices/system/cpu/cpu0#echo 0 > online sh: write error: Operation not permitted
内核关于 hotplug的文档(Documentation/cpu-hotplug.txt)里说,在某些情况下 CPU0确实是不可以卸载的.
比如,CONFIG_BOOTPARAM_HOTPLUG_CPU0没有选中,那 CPU0就不能卸载.
在有些架构上, 如果 CPU0不能卸载,那么 cpu0目录下 online文件就不存在.
但是,有些架构的 CPU, 需要动态判断.也就是说,需要根据 echo 0 > online的返回值判断.
如果为 1, 那么表示这个 CPU是不能卸载的.
看来我所实验的属于需要动态判断的类型.
-------------------------------------------------------分割线-------------------------------------------------------------------------------------------
今天查看了下代码,确实是这样的.
相关的函数如下:
static int platform_cpu_disable(unsigned int cpu) { if (smp_ops.cpu_disable) return smp_ops.cpu_disable(cpu); /* * By default, allow disabling all CPUs except the first one, * since this is special on a lot of platforms, e.g. because * of clock tick interrupts. */ return cpu == 0 ? -EPERM : 0; }其中
smp_ops.cpu_disable为NULL, 所以 cpu0 是不能 offline的.
echo > online 对应的这个函数的调用栈为
#0 platform_cpu_disable (cpu=0) at arch/arm/kernel/smp.c:155 #1 0xc037b794 in __cpu_disable () at arch/arm/kernel/smp.c:165 #2 0xc0379594 in take_cpu_down (_param=0xee43def0) at kernel/cpu.c:275 #3 0xc0076d10 in stop_machine_cpu_stop (data=data@entry=0xee43deb0) at kernel/stop_machine.c:436 #4 0xc0076b84 in cpu_stopper_thread (cpu=<optimized out>) at kernel/stop_machine.c:285 #5 0xc0044dc8 in smpboot_thread_fn (data=0xef084d80) at kernel/smpboot.c:160 #6 0xc003e03c in kthread (_create=0xef09fe70) at kernel/kthread.c:200 #7 0xc000e158 in ret_from_fork () at arch/arm/kernel/entry-common.S:91