If a user-space application (like WhatsApp) crashes due to a Null Pointer Exception, the kernel simply kills the process, cleans up its memory, and Android continues running smoothly.
However, if the kernel itself encounters a fatal software bug, or if critical hardware fails catastrophically, the kernel cannot trust its own state. If it were to continue running, it might permanently corrupt the user's filesystem or permanently damage the hardware.
In this scenario, the kernel triggers a Kernel Panic.
What Causes a Kernel Panic?
A kernel panic is a deliberate, defensive action. Common triggers in Android include:
- Hardware Watchdog Timeout: A critical hardware chip (like the modem) stops responding.
- Bad Memory Access in a Driver: A buggy display driver tries to write to a memory address that does not exist.
- Software Watchdog: The core Android
system_servergets deadlocked. The kernel detects this and triggers a panic, because a phone withoutsystem_serveris functionally dead anyway.
// Example from a buggy Android kernel driver triggering a panic
if (critical_hardware_state == NULL) {
panic("Fatal Error: Display hardware controller is missing!\n");
}
The panic() Call Chain
When a fatal error is detected, the kernel calls the panic() function (located in kernel/panic.c).
This function executes a drastic sequence of events:
- Disable Interrupts: It immediately disables all CPU interrupts (
local_irq_disable()), preventing any other code from running. - Stop Other Cores: It sends a Non-Maskable Interrupt (NMI) to all other CPU cores, forcing them to halt instantly via
smp_send_stop(). - Print to Console: It prints a massive stack trace and error log directly to the raw serial console memory.
- Halt or Reboot: Depending on the configuration, the kernel will either freeze forever or automatically reboot the phone.
Panic Reboot Timeout
By default, standard Linux will halt and freeze on a panic, displaying the infamous flashing keyboard LEDs.
On Android, freezing is a terrible user experience because there is no physical keyboard to restart the device. Therefore, Android configures the kernel panic_timeout parameter (usually to 5 seconds).
When a panic occurs, the phone freezes for 5 seconds (dumping the error logs to memory) and then automatically reboots. From the user's perspective, the phone simply restarted randomly.
You can check the panic timeout value on your device:
adb shell cat /proc/sys/kernel/panic
If this returns 5, the device will reboot 5 seconds after a kernel panic.