当前位置:网站首页>Kernel函数解析之kernel_restart
Kernel函数解析之kernel_restart
2022-08-04 22:50:00 【szembed】
该函数实现在kernel/reboot.c中,主要功能为重新启动kernel,比如发现kernel进入到了一个异常场景,此时我想重启kernel,那么该函数就可以调用。
-
void
kernel_restart(
char *cmd)
-
{
-
kernel_restart_prepare(cmd);
-
migrate_to_reboot_cpu();
-
syscore_shutdown();
-
if (!cmd)
-
pr_emerg(
"Restarting system\n");
-
else
-
pr_emerg(
"Restarting system with command '%s'\n", cmd);
-
kmsg_dump(
KMSG_DUMP_SHUTDOWN);
-
machine_restart(cmd);
-
}
那么接下来 我们解析下该函数的实现过程,该函数中,总共调用了5个函数:
- kernel_restart_prepare(cmd)
-
void kernel_restart_prepare( char *cmd)
-
{
-
blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
-
system_state = SYSTEM_RESTART;
-
usermodehelper_disable();
-
device_shutdown();
-
}
主要功能为回调注册到reboot_notifier_list链表中的回调函数,因为有部分模块,需要在重启系统前做一些模块相关的工作,举个简单的例子,比如系统要重启时edma正在搬移数据,那么回调后可以把EDMA停下来,以防止系统挂死。
-
- migrate_to_reboot_cpu();
-
void migrate_to_reboot_cpu( void)
-
{
-
/* The boot cpu is always logical cpu 0 */
-
int cpu = reboot_cpu;
-
-
cpu_hotplug_disable();
-
-
/* Make certain the cpu I'm about to reboot on is online */
-
if (! cpu_online(cpu))
-
cpu = cpumask_first(cpu_online_mask);
-
-
/* Prevent races with other tasks migrating this task */
-
current->flags |= PF_NO_SETAFFINITY;
-
-
/* Make certain I only run on the appropriate processor */
-
set_cpus_allowed_ptr(current, cpumask_of(cpu));
-
}
该函数主要是把当前调用重启接口的任务,绑定到固定的一个CPU上,通常为当前online的序号排第一的CPU。此处不用纠结为什么绑定到第一个online的而不是第二个第三个,主要原因一是为了简单,二是防止在重启时再有什么任务迁移。
-
- syscore_shutdown()
-
/**
-
* syscore_shutdown - Execute all the registered system core shutdown callbacks.
-
*/
-
void syscore_shutdown( void)
-
{
-
struct syscore_ops *ops;
-
-
mutex_lock(&syscore_ops_lock);
-
-
list_for_each_entry_reverse(ops, &syscore_ops_list, node)
-
if (ops->shutdown) {
-
if (initcall_debug)
-
pr_info( "PM: Calling %pS\n", ops->shutdown);
-
ops->shutdown();
-
}
-
-
mutex_unlock(&syscore_ops_lock);
-
}
回调所有注册syscore shutdown回调的回调函数,通常注册syscore的回调有3个:suspend\resume\shutdown,其中suspend和resume在低功耗流程中调用,shutdown则在此处调用。
-
- kmsg_dump(KMSG_DUMP_SHUTDOWN);
回调注册到dump_list中的dump回调,注册的模块可能会保存一些自己关心的数据。
在这里也简单说下传参:
enum kmsg_dump_reason {
KMSG_DUMP_UNDEF,
KMSG_DUMP_PANIC,
KMSG_DUMP_OOPS,
KMSG_DUMP_EMERG,
KMSG_DUMP_SHUTDOWN,
KMSG_DUMP_MAX
};
可以根据重启的不同场景,然后传递对应的重启原因。 - machine_restart(cmd);
-
void machine_restart( char *cmd)
-
{
-
local_irq_disable();
-
smp_send_stop();
-
-
if (arm_pm_restart)
-
arm_pm_restart(reboot_mode, cmd);
-
else
-
do_kernel_restart(cmd);
-
-
/* Give a grace period for failure to restart of 1s */
-
mdelay( 1000);
-
-
/* Whoops - the platform was unable to reboot. Tell the user! */
-
printk( "Reboot failed -- System halted\n");
-
while ( 1);
-
}
函数实现在arch/arm/kernel/reboot.c中,我们首先可以看到,复位cpu会通过smp_send_stop接口把其他cpu停下来,然后调用重启接口重启系统。
-
边栏推荐
猜你喜欢

逆序对的数量

各行各业都受到重创,游戏行业却如火如荼,如何加入游戏模型师职业

老叶的三束玫瑰

深度学习 RNN架构解析

【游戏建模模型制作全流程】使用ZBrush制作骷髅王

To Offer | 03. Repeat Numbers in the array

地面高度检测/平面提取与检测(Fast Plane Extraction in Organized Point Clouds Using Agglomerative Hierarchical Clu)

重新配置chrome中ffmpeg插件

Charles & TCPDump & Fiddler 抓包三兄弟七夕联手,还抓不到你的心?

2022精选最新金融银行面试真题——附带答案
随机推荐
360市值四年蒸发3900亿,政企安全能救命吗?
DREAMWEAVER8 部分问题解决方案
视频gif如何制作?试试这个视频制作gif神器
Based on the results of the facts
测试薪资这么高?刚毕业20K,仅需3.5个月
panic: reflect: reflect.Value.SetString using value obtained using unexported field
Linear DP (bottom)
Since a new byte of 20K came out, I have seen what the ceiling is
Pytest learning - fixtures
软件测试技术之如何编写测试用例(4)
[Paper Notes KDD2021] MixGCF: An Improved Training Method for Graph Neural Network-based Recommender Systems
【TCP/IP 四 IP 网际协议】
中国的顶级黑客在国际上是一个什么样的水平?
【3D建模制作技巧分享】Maya模型如何导入zbrush
Shell编程之循环语句与函数的使用
2022精选最新金融银行面试真题——附带答案
ANT1.7下载以及配置方法
【字符串函数内功修炼】strlen + strstr + strtok + strerror(三)
最温馨的家园
【3D建模制作技巧分享】ZBrush如何使用Z球
