如何启用linux内核异常自动重启机制

内核出现异常,有这么几种情况:
1.在内核代码执行路径上会主动的去检查一些执行环境,如果不满足触发panic;
2.某段代码一直占着cpu,其他进程都得不到执行,这又分为两种情况:
a)某段代码禁止了内核抢占,并且进入了死循环,这种情况下时钟中断可以响应,但是因为禁止了内核抢占,中断处理函数结束后仍然占着cpu,这就是所谓的soft lockup
b)某段代码禁止了中断,并且进入了死循环,这种情况下时钟中断都不可以响应,所以这段代码也会一直占着cpu,这就是所谓的hard lockup
3.内核内存中的代码被破坏,可能是内核中的bug,比如内存溢出写,可能把中断向量或者中断处理函数也破坏了。

下面分别说说每种情况,如何实现自动重启:
1.只需打开panic后自动重启就可以了
echo 1 > /proc/sys/kernel/panic
2.分情况处理
a)对于soft lockup的检测,内核在每个cpu上启动一个watchdog/x的内核线程,把它的调度策略设置为SCHED_FIFO,即实时线程,因为soft lockup是可以响应中断的,所有利用了hrtimer中断,在hrtimer中断处理函数中做了以下事情:
-唤醒[watchdog/x]内核线程,该线程的任务是更新一个时间戳;
-soft lock detector检查时间戳,如果超过soft lockup threshold一直未更新,说明[watchdog/x]未得到运行机会,意味着CPU被霸占,也就是发生了soft lockup。
该机制的实现代码位于:kernel/watchdog.c
启用该机制的方法:
echo 1 > /proc/sys/kernel/watchdog
echo 1 > /proc/sys/kernel/soft_watchdog
echo 1 > /proc/sys/kernel/softlockup_panic
可以同时设置 watchdog_thresh参数来定义发现softlockup以后系统panic的时间,默认是10s, 也就是说20s后系统panic。最大能设到60s,也就是说,120s后启动系统panic。

softlockup_all_cpu_backtrace:

This value controls the soft lockup detector thread’s behavior
when a soft lockup condition is detected as to whether or not
to gather further debug information. If enabled, each cpu will
be issued an NMI and instructed to capture stack trace.

This feature is only applicable for architectures which support
NMI.

0: do nothing. This is the default behavior.

1: on detection capture more debug information.

一般来说,在production system上,不建议使用watchdog_cpumask:

This value can be used to control on which cpus the watchdog may run.
The default cpumask is all possible cores, but if NO_HZ_FULL is
enabled in the kernel config, and cores are specified with the
nohz_full= boot argument, those cores are excluded by default.
Offline cores can be included in this mask, and if the core is later
brought online, the watchdog will be started based on the mask value.

Typically this value would only be touched in the nohz_full case
to re-enable cores that by default were not running the watchdog,
if a kernel lockup was suspected on those cores.

The argument value is the standard cpulist format for cpumasks,
so for example to enable the watchdog on cores 0, 2, 3, and 4 you
might say:

echo 0,2-4 > /proc/sys/kernel/watchdog_cpumask

==============================================================

watchdog_thresh:

This value can be used to control the frequency of hrtimer and NMI
events and the soft and hard lockup thresholds. The default threshold
is 10 seconds.

The softlockup threshold is (2 * watchdog_thresh). Setting this
tunable to zero will disable lockup detection altogether.softlockup_panic选项(有可能误伤)。可以在调试系统上使用。

b)对于hard lockup的检测,hard lockup比soft lockup更加严重,CPU不仅无法执行其它进程,而且不再响应中断。检测hard lockup的原理利用了PMU的NMI perf event,因为NMI中断是不可屏蔽的,在CPU不再响应中断的情况下仍然可以得到执行,它再去检查时钟中断的计数器hrtimer_interrupts是否在保持递增,如果停滞就意味着时钟中断未得到响应,也就是发生了hard lockup
NMI watchdog会利用到之前讲到的hrtimer。它的触发条件是基于PMU的NMI perf event,当PMU的计数器溢出时会触发NMI中断,对应的中断处理例程是 kernel/watchdog.c: watchdog_overflow_callback(),hard lockup detector就在其中,它会检查上述hrtimer的中断次数(hrtimer_interrupts)是否在保持递增,如果停滞则表明hrtimer中断未得到响应,也就是发生了hard lockup。
启用该机制的方法:
echo 1 > /proc/sys/kernel/watchdog
echo 1 > /proc/sys/kernel/nmi_watchdog
echo 1 > /proc/sys/kernel/hardlockup_panic

3.中断向量或者NMI 中断处理函数都被破坏了,这种情况神仙也救不回来了,只能物理重启了。

参考资料:
https://blog.csdn.net/ericstarmars/article/details/81750919
Documentation/sysctl/kernel.txt

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值