当前位置:网站首页>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停下来,然后调用重启接口重启系统。
-
边栏推荐
- Acwing3593. 统计单词
- 应用联合、体系化推进。集团型化工企业数字化转型路径
- Charles & TCPDump & Fiddler 抓包三兄弟七夕联手,还抓不到你的心?
- 测试薪资这么高?刚毕业20K,仅需3.5个月
- panic: reflect: reflect.Value.SetString using value obtained using unexported field
- 得不到你的心,就用“分布式锁”锁住你的人
- 地面高度检测/平面提取与检测(Fast Plane Extraction in Organized Point Clouds Using Agglomerative Hierarchical Clu)
- MQTT[一]基础知识介绍
- 【TCP/IP 四 IP 网际协议】
- typeScript-promise
猜你喜欢

备战9月,美团50道软件测试经典面试题及答案汇总

重新配置chrome中ffmpeg插件

养殖虚拟仿真软件提供高沉浸式的虚拟场景互动操作体验学习

CS8416国产替代DP8416 数字音频接收器
![[Mock Interview - 10 Years of Work] Are more projects an advantage?](/img/fa/2652629d1ff4653aca0d626ac89bf8.jpg)
[Mock Interview - 10 Years of Work] Are more projects an advantage?

使用cpolar优化树莓派上的网页(1)

go语言的日志实现(打印日志、日志写入文件、日志切割)

How to make a video gif?Try this video making gif artifact

FinClip崁入式搭建生态平台,降低合作门槛

各行各业都受到重创,游戏行业却如火如荼,如何加入游戏模型师职业
随机推荐
剑指Offer | 数值的整数次方
基于事实的讨论
typeScript-promise
Deep Learning RNN Architecture Analysis
SQL Server 调用 WebService
Controller层代码这么写,简洁又优雅!
typeScript-部分应用函数
JVM内存配置参数GC日志
【字符串函数内功修炼】strcpy + strcat + strcmp(一)
Community Sharing|Tencent Overseas Games builds game security operation capabilities based on JumpServer
QT[一] 信号与槽
正则表达式绕过
【字符串函数内功修炼】strlen + strstr + strtok + strerror(三)
文章占位 文章占位
2022/8/3
重新配置chrome中ffmpeg插件
Service Mesh落地路径
【云原生 · Kubernetes】Kubernetes运维
【论文笔记KDD2021】MixGCF: An Improved Training Method for Graph Neural Network-based Recommender Systems
postman接口测试
