Kernel Panic in Linux - What, Reason, Configure, Generate

What is Kernel Panic

Kernel Panic is an error in the kernel code. On Kernel Panic, kernel stop running immediately to avoid data loss or other damage. The reason to stop running is to protect your computer, for example, if the module which is controlling the fan fails to load, kernel generates a panic and freezes the system.

What are the reasons for Kernel Panic


  • Hardware or Software Issue (e.g. unable to start init process)
  • Bug in the kernel driver
  • Defective or Incompatible RAM
What happens during Kernel Panic

When kernel decides to Panic, it calls the panic() function which dumps some debug information and depending on the configuration reboots the system

Configure Kernel to reboot on Kernel Panic

By default, the kernel will not reboot on Kernel Panic. There are two ways by which you can instruct the kernel to reboot

1. Kernel Command line: Add "panic=N" to the kernel command line, for the kernel to reboot after N seconds
2. Proc File system: echo N > /proc/sys/kernel/debug , for kernel to reboot after N seconds on reboot. Note this setting is not persistent on reboot.

Sample Code to generate Kernel Panic:

Make sure you configure the kernel to reboot else you need to hard reset the system



#include <linux/kernel.h>
#include <linux/module.h>

MODULE_LICENSE("GPL");
static int test_panic_init(void)
{
    printk(KERN_INFO"%s: In init\n", __func__);
    panic("Hello Kernel I am causing the panic\n");
    return 0;
}

static void test_panic_exit(void)
{
    printk(KERN_INFO"%s: In exit\n", __func__);
}

module_init(test_panic_init);
module_exit(test_panic_exit);

Loading the module will either freeze or reboot the kernel depending on your kernel configuration

Comments

Popular posts from this blog

bb.utils.contains yocto

Difference between RDEPENDS and DEPENDS in Yocto

PR, PN and PV Variable in Yocto