System Hangs
Although most bugs in kernel code end up as oops messages, sometimes they can completely hang the system. If the system hangs, no message is printed. For example, if the code enters an endless loop, the kernel stops scheduling,[3] and the system doesn't respond to any action, including the magic Ctrl-Alt-Del combination. You have two choices for dealing with system hangs—either prevent them beforehand or be able to debug them after the fact. 尽管大多数内核代码中的bug最后都变成了 "哎呀 "信息,但有时它们会使系统完全挂起。如果系统挂起,就不会有任何信息被打印出来。例如,如果代码进入了一个无休止的循环,内核就会停止调度,[3] 而且系统对任何操作都没有反应,包括神奇的Ctrl-Alt-Del组合。在处理系统挂起时,你有两个选择--要么事先防止它们,要么事后能够调试它们。
You can prevent an endless loop by inserting schedule invocations at strategic points. The schedule call (as you might guess) invokes the scheduler and, therefore, allows other processes to steal CPU time from the current process. If a process is looping in kernel space due to a bug in your driver, the schedule calls enable you to kill the process after tracing what is happening. 你可以通过在战略点插入schedule调用来防止无休止的循环。schedule调用(正如你可能猜到的那样)会调用调度程序,因此,允许其他进程从当前进程中窃取CPU时间。如果一个进程由于你的驱动程序中的一个错误而在内核空间中循环,schedule调用可以使你在追踪正在发生的事情后杀死该进程。
You should be aware, of course, that any call to schedule may create an additional source of reentrant calls to your driver, since it allows other processes to run. This reentrancy should not normally be a problem, assuming that you have used suitable locking in your driver. Be sure, however, not to call schedule any time that your driver is holding a spinlock. 当然,你应该意识到,任何对schedule的调用都可能给你的驱动程序创造一个额外的可重入调用源,因为它允许其他进程运行。假设你在你的驱动程序中使用了合适的锁,这种可重入性通常不应该是一个问题。然而,请确保不要在你的驱动程序持有自旋锁的时候调用schedule。
If your driver really hangs the system, and you don't know where to insert schedule calls, the best way to go may be to add some print messages and write them to the console (by changing the console_loglevel value if need be). 如果你的驱动程序真的挂起了系统,而你又不知道在哪里插入schedule调用,最好的办法可能是增加一些打印信息,并把它们写到控制台(如果需要的话,通过改变console_loglevel值)。
Sometimes the system may appear to be hung, but it isn't. This can happen, for example, if the keyboard remains locked in some strange way. These false hangs can be detected by looking at the output of a program you keep running for just this purpose. A clock or system load meter on your display is a good status monitor; as long as it continues to update, the scheduler is working. 有时,系统可能看起来是挂起的,但其实不是。例如,如果键盘以某种奇怪的方式被锁定,就会发生这种情况。这些错误的挂起可以通过查看你为这个目的而运行的程序的输出来检测。你显示屏上的时钟或系统负载表是一个很好的状态监测器;只要它持续更新,调度器就在工作。
An indispensable tool for many lockups is the "magic SysRq key," which is available on most architectures. Magic SysRq is invoked with the combination of the Alt and SysRq keys on the PC keyboard, or with other special keys on other platforms (see Documentation/sysrq.txt for details), and is available on the serial console as well. A third key, pressed along with these two, performs one of a number of useful actions: 对于许多锁定来说,一个不可缺少的工具是 "Magic SysRq键",它在大多数架构上都可用。魔术SysRq是通过PC键盘上的Alt和SysRq键的组合来调用的,或者在其他平台上通过其他特殊键来调用(详见文档/sysrq.txt),并且在串行控制台中也可以使用。与这两个键同时按下的第三个键,可以执行一系列有用的操作之一。
r
Turns off keyboard raw mode; useful in situations where a crashed application (such as the X server) may have left your keyboard in a strange state. 关闭键盘原始模式;在一个崩溃的应用程序(如X服务器)可能使你的键盘处于一个奇怪的状态的情况下,非常有用。
k
Invokes the " secure attention key" (SAK) function. SAK kills all processes running on the current console, leaving you with a clean terminal. 调用 "安全注意键"(SAK)功能。SAK杀死所有在当前控制台运行的进程,给你留下一个干净的终端。
s
Performs an emergency synchronization of all disks. 对所有磁盘进行紧急同步。
u
Umount. Attempts to remount all disks in a read-only mode. This operation, usually invoked immediately after s, can save a lot of filesystem checking time in cases where the system is in serious trouble. Umount。试图以只读模式重新挂载所有磁盘。这个操作通常在s之后立即调用,在系统出现严重问题的情况下可以节省大量的文件系统检查时间。
b
Boot. Immediately reboots the system. Be sure to synchronize and remount the disks first. 启动。立即重新启动系统。请确保首先同步和重新安装磁盘。
p
Prints processor registers information. 打印处理器的寄存器信息。
t
Prints the current task list. 打印当前的任务列表。
m
Prints memory information. 打印内存信息
Other magic SysRq functions exist; see sysrq.txt in the Documentation directory of the kernel source for the full list. Note that magic SysRq must be explicitly enabled in the kernel configuration and that most distributions do not enable it, for obvious security reasons. For a system used to develop drivers, however, enabling magic SysRq is worth the trouble of building a new kernel in itself. Magic SysRq may be disabled at runtime with a command such as the following: 还有其他神奇的SysRq函数,完整的列表见内核源文件目录下的sysrq.txt。请注意,magic SysRq必须在内核配置中明确启用,大多数发行版都不启用它,这显然是出于安全考虑。然而,对于一个用于开发驱动程序的系统来说,启用magic SysRq本身就值得为构建一个新的内核而烦恼。Magic SysRq可以在运行时用如下命令来禁用。
echo 0 > /proc/sys/kernel/sysrq
You should consider disabling it if unprivileged users can reach your system keyboard, to prevent accidental or willing damages. Some previous kernel versions had sysrq disabled by default, so you needed to enable it at runtime by writing 1 to that same /proc/sys file. 如果没有特权的用户可以接触到你的系统键盘,你应该考虑禁用它,以防止意外或自愿的损害。一些以前的内核版本默认是禁用sysrq的,所以你需要在运行时通过在同一个/proc/sys文件中写1来启用它。
The sysrq operations are exceedingly useful, so they have been made available to system administrators who can't reach the console. The file /proc/sysrq-trigger is a write-only entry point, where you can trigger a specific sysrq action by writing the associated command character; you can then collect any output data from the kernel logs. This entry point to sysrq is always working, even if sysrq is disabled on the console. sysrq操作是非常有用的,所以它们被提供给那些无法到达控制台的系统管理员使用。文件/proc/sysrq-trigger是一个只写的入口点,在这里你可以通过写相关的命令字符来触发一个特定的sysrq操作;然后你可以从内核日志中收集任何输出数据。这个sysrq的入口点总是在工作,即使控制台的sysrq被禁用。
If you are experiencing a "live hang," in which your driver is stuck in a loop but the system as a whole is still functioning, there are a couple of techniques worth knowing. Often, the SysRq p function points the finger directly at the guilty routine. Failing that, you can also use the kernel profiling function. Build a kernel with profiling enabled, and boot it with profile=2 on the command line. Reset the profile counters with the readprofile utility, then send your driver into its loop. After a little while, use readprofile again to see where the kernel is spending its time. Another more advanced alternative is oprofile, that you may consider as well. The file Documentation/basic_profiling.txt tells you everything you need to know to get started with the profilers. 如果你遇到了 "live hang",即你的驱动程序被卡在一个循环中,但整个系统仍在运行,有几个技巧值得了解。通常,SysRq p函数可以直接指出有问题的程序。如果不这样做,你也可以使用内核profiling功能。建立一个启用了profiling功能的内核,并在命令行上用profile=2启动它。用readprofile工具重置profile计数器,然后把你的驱动程序送入它的循环。一段时间后,再次使用readprofile来查看内核在哪里花费时间。另一个更高级的选择是OPROFILE,你也可以考虑。Documentation/basic_profiling.txt文件告诉你开始使用profile所需要知道的一切。
One precaution worth using when chasing system hangs is to mount all your disks as read-only (or unmount them). If the disks are read-only or unmounted, there's no risk of damaging the filesystem or leaving it in an inconsistent state. Another possibility is using a computer that mounts all of its filesystems via NFS, the network file system. The "NFS-Root" capability must be enabled in the kernel, and special parameters must be passed at boot time. In this case, you'll avoid filesystem corruption without even resorting to SysRq, because filesystem coherence is managed by the NFS server, which is not brought down by your device driver. 当调查系统挂起时,一个值得使用的预防措施是将你所有的磁盘挂起为只读(或取消挂起)。如果磁盘是只读或未挂载的,就不会有损坏文件系统或使其处于不一致状态的风险。另一种可能性是使用一台通过NFS(网络文件系统)挂载其所有文件系统的计算机。必须在内核中启用 "NFS-Root "功能,并且在启动时必须传递特殊参数。在这种情况下,你会避免文件系统的损坏,甚至不需要借助SysRq,因为文件系统的一致性是由NFS服务器管理的,它不会被你的设备驱动程序所破坏。

2071

被折叠的 条评论
为什么被折叠?



