PREEMPT_RT Real-Time Kernel on Raspberry Pi

The default Raspberry Pi OS kernel uses the general-purpose preemption model. Under an interrupt storm or a heavy system load, the latency of a task can jump from a few milliseconds to tens of milliseconds. For robot control, motor drivers, and audio processing, where each cycle must finish before a deadline, this jitter is fatal. PREEMPT_RT reduces the problem by making almost all locks in the kernel preemptible. It is now in mainline, and Raspberry Pi provides an RT kernel as an APT package. You no longer need to build the whole kernel yourself. This post installs the PREEMPT_RT kernel on a Raspberry Pi 4 or 5, configures the boot, and measures the latency with cyclictest.

What is PREEMPT_RT?

PREEMPT_RT converts most spinlocks in the kernel to sleeping locks based on rt_mutex, and it runs interrupt handlers in kernel threads (threaded IRQs). As a result, a high-priority task does not wait without bound for interrupt handling or lock contention. The patch set was maintained out of tree for a long time. Since kernel 6.12 it is in mainline, and you enable it by selecting the “Fully Preemptible Kernel (Real-Time)” preemption model. Raspberry Pi now also ships an RT kernel variant as a separate APT package.

Install the RT kernel and configure the boot

The package name is linux-image-rpi-v8-rt. v8 means the ARMv8 (64-bit) architecture, so the package is available only on 64-bit Raspberry Pi OS.

sudo apt update
sudo apt full-upgrade -y
sudo apt install linux-image-rpi-v8-rt

The package does not make the system boot the RT kernel. Raspberry Pi OS keeps several kernel images installed, and the kernel= entry in config.txt selects the one to boot. Edit /boot/firmware/config.txt (/boot/config.txt before Bookworm).

kernel=kernel8_rt.img
arm_64bit=1
auto_initramfs=0

arm_64bit=1 tells the firmware to boot in 64-bit mode when the kernel image does not have a standard name such as kernel8.img. On a Pi 4 or CM4, the boot can fail without it. Save the file and reboot.

sudo reboot

Check the boot and measure latency with cyclictest

After the reboot, run uname -a to make sure that the RT kernel is running. A PREEMPT_RT kernel shows PREEMPT_RT in its version string.

uname -a

The exact version depends on the package version at installation time. If the output contains PREEMPT_RT, the RT kernel booted correctly. To know whether the latency really improved, measure it with cyclictest from the rt-tests package.

sudo apt install rt-tests
sudo cyclictest -m -Sp99 -i200 -h400 -q
OptionMeaning
-mLock memory with mlockall to exclude page fault latency
-Sp99Run one measurement thread on each CPU with SCHED_FIFO priority 99
-i200Set the timer interval to 200 µs
-h400Print a latency histogram up to 400 µs

Look at the maximum latency and the jitter, not the average. Under load, the maximum of a standard kernel jumps high, while an RT kernel stays in a narrow range. Run a load with stress-ng, let cyclictest run for several minutes, and stop it with Ctrl+C. It prints the minimum, average, and maximum latency for each CPU.

Supported models and limitations

The RT kernel variant is closer to a community build than to a standard feature of the official images. The support level depends on the board.

  • Pi 5, Pi 4/CM4: The RT kernel boots. On a Pi 4 or CM4, arm_64bit=1 is required.
  • Pi Zero 2 W: It boots, but with an F2FS root file system you must build and add an initramfs yourself.
  • Pi 3B+: Boot failures are reported, so it is not reliable yet.

Go back to the standard kernel

If the RT kernel conflicts with a peripheral driver or is not stable, change the kernel= value in config.txt back.

kernel=kernel8.img

After a reboot, the system runs the standard kernel again. You do not need to remove the linux-image-rpi-v8-rt package. Change config.txt to switch between the two kernels.

Cautions

  • Test with your real workload before you use the RT kernel in production. It is not in the official Raspberry Pi documentation and comes from forum and community builds.
  • Check the support status of your model first. Boot failures are reported on older boards such as the Pi 3B+.
  • Measure under a load similar to your application. cyclictest results change a lot with the system load.

Conclusion

With PREEMPT_RT in mainline, you can try a real-time kernel on a Raspberry Pi without rebuilding the kernel. One APT command and a few lines in config.txt apply it, and one line rolls it back. This is a quick test before you start a latency-sensitive project such as motor control or audio on a Raspberry Pi. The builds are still at the community stage, so measure with cyclictest before you adopt them.

This is a referral link. I may receive credit if you sign up, at no extra cost to you.

Try it on a cloud server — Deploy Ubuntu on Vultr

References

Leave a Reply