Raspberry Pi OS PREEMPT RT 6.13 Kernel Cookbook

  • gene_weber
  • gene_weber's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
15 Jan 2025 20:36 - 16 Jan 2025 13:53 #319049 by gene_weber
I'm posting the steps I executed to build and install a PREEMPT RT Linux kernel in Raspberry Pi OS for my Pi 4B for two reasons.
  1. Hopefully it helps someone trying to do this or something similar. I appreciate the pointers I got from alangibson, cornholio, and rodw
  2. This is the first Linux kernel I've ever built. So I open this up to comments and suggestions.

NOTE: Version 6.13 of the Linux kernel is under development and could have bugs / issues. So far it's been stable for me, but your mileage may vary.

I started with a fresh installation of Raspberry Pi OS on my Pi 4B.

1/16/25 Edit: Based on responses after the initial post it was clear that the Wayland display protocol is non-optimal for LinuxCNC. A comparison of the baseline pre-optimized jitter was conducted where the only difference is the display protocol, Wayland vs X11. The latency histograms for each are included in those posts, and show a dramatic difference with X11 inducing significantly less jitter. Wayland is the default in the current Raspberry Pi OS, but is easily changed to X11.

You can check the session type:
echo $XDG_SESSION_TYPE
    wayland

Launch Rasp-config command line tool in a terminal:
sudo raspi-config
Arrow down to "Advanced Options", and enter.
Arrow down to "Wayland                 Switch between X and Wayland backends", and enter.
If not on the line that says "W1 X11     Openbox window manager with X11 backend", arrow to it and enter.
The popup should say "Openbox on X11 is active" and have "Ok" highlighted. Enter.
It will return to the main screen. Use left-righ arrows to select "Finish". Enter.
Popup asks if you would like to reboot now, with "Yes" highlighted. Enter.

After reboot:
echo $XDG_SESSION_TYPE
    x11


Raspberry Pi's documentation on how to build a kernel was used as the guideline for this process:

Download the source code for the 6.13 Raspberry Pi kernel:
git clone -b rpi-6.13.y --depth=1 https://github.com/raspberrypi/linux

Install the needed build dependencies:
sudo apt install bc bison flex libssl-dev make


Download the RT patches for v6.13 and the signature file. Starting here , select the tarballs link at bottom left of page. From the list of versions, select the 6.13 link .
Download the patch file patch-6.13-rc6-rt3.patch.gz:
curl -OL https://cdn.kernel.org/pub/linux/kernel/projects/rt/6.13/patch-6.13-rc6-rt3.patch.gz
Download the signature file patch-6.13-rc6-rt3.patch.sign:
curl -OL https://cdn.kernel.org/pub/linux/kernel/projects/rt/6.13/patch-6.13-rc6-rt3.patch.sign

cd ~/Downloads

Verify the signature of the patch file. These were committed by Sebastian Andrzej Siewior.
mkdir .gnupg
vi .gnupg/gpg.conf

Add the following two lines:
auto-key-locate wkd,dane,local
auto-key-retrieve

Get the public key for "Sebastian Andrzej Siewior <This email address is being protected from spambots. You need JavaScript enabled to view it.>"
gpg --locate-keys [email]bigeasy@linutronix.de[/email]

Unzip the patch file:
gzip -d patch-6.13-rc6-rt3.patch.gz

Verify signature:
gpg2 --verify patch-6.13-rc6-rt3.patch.sign
    gpg: assuming signed data in 'patch-6.13-rc6-rt3.patch'
    gpg: Signature made Thu 09 Jan 2025 05:04:09 AM EST
    gpg:                using RSA key 57892E705233051337F6FDD105641F175712FA5B
    gpg: Good signature from "Sebastian Andrzej Siewior <This email address is being protected from spambots. You need JavaScript enabled to view it.>" [unknown]
    gpg: WARNING: This key is not certified with a trusted signature!
    gpg:          There is no indication that the signature belongs to the owner.
    Primary key fingerprint: 6425 4695 FFF0 AA44 66CC  19E6 7B96 E816 2A8C F5D1
         Subkey fingerprint: 5789 2E70 5233 0513 37F6  FDD1 0564 1F17 5712 FA5B


Go back to the linux directory created by the git clone:
cd ~/linux
Patch the 6.13 RT kernel:
patch -p1 < ~/Downloads/patch-6.13-rc6-rt3.patch
    patching file arch/arm/Kconfig
    patching file arch/arm/kernel/module-plts.c
    patching file arch/arm/kernel/traps.c
    patching file arch/arm/mm/fault.c
    patching file arch/arm64/kernel/ftrace.c
    patching file arch/arm64/kernel/traps.c
    patching file arch/loongarch/kernel/ftrace_dyn.c
    patching file arch/loongarch/kernel/unwind_orc.c
    patching file arch/powerpc/Kconfig
    patching file arch/powerpc/include/asm/stackprotector.h
    patching file arch/powerpc/include/asm/thread_info.h
    patching file arch/powerpc/kernel/interrupt.c
    patching file arch/powerpc/kernel/trace/ftrace.c
    patching file arch/powerpc/kernel/trace/ftrace_64_pg.c
    patching file arch/powerpc/kernel/traps.c
    patching file arch/powerpc/kvm/Kconfig
    patching file arch/powerpc/lib/vmx-helper.c
    patching file arch/powerpc/platforms/pseries/Kconfig
    patching file arch/powerpc/platforms/pseries/iommu.c
    patching file arch/s390/kernel/dumpstack.c
    patching file arch/x86/kernel/callthunks.c
    patching file arch/x86/kernel/dumpstack.c
    patching file arch/x86/kernel/unwind_orc.c
    patching file arch/xtensa/kernel/traps.c
    patching file drivers/gpu/drm/i915/Kconfig
    patching file drivers/gpu/drm/i915/display/intel_crtc.c
    patching file drivers/gpu/drm/i915/display/intel_cursor.c
    patching file drivers/gpu/drm/i915/display/intel_display_trace.h
    patching file drivers/gpu/drm/i915/display/intel_vblank.c
    patching file drivers/gpu/drm/i915/gt/intel_execlists_submission.c
    patching file drivers/gpu/drm/i915/gt/uc/intel_guc.h
    patching file drivers/gpu/drm/i915/i915_request.c
    patching file drivers/gpu/drm/i915/i915_trace.h
    patching file drivers/gpu/drm/i915/i915_utils.h
    patching file drivers/tty/serial/8250/8250_core.c
    Hunk #1 succeeded at 403 (offset 15 lines).
    Hunk #2 succeeded at 411 (offset 15 lines).
    Hunk #3 succeeded at 541 (offset 15 lines).
    patching file drivers/tty/serial/8250/8250_port.c
    Hunk #4 succeeded at 3317 (offset 9 lines).
    Hunk #5 succeeded at 3350 (offset 9 lines).
    Hunk #6 succeeded at 3409 (offset 9 lines).
    Hunk #7 succeeded at 3475 (offset 9 lines).
    Hunk #8 succeeded at 3618 (offset 9 lines).
    Hunk #9 succeeded at 3628 (offset 9 lines).
    patching file include/linux/kallsyms.h
    patching file include/linux/module.h
    patching file include/linux/preempt.h
    patching file include/linux/serial_8250.h
    patching file kernel/cfi.c
    patching file kernel/jump_label.c
    patching file kernel/kprobes.c
    patching file kernel/ksysfs.c
    patching file kernel/livepatch/core.c
    patching file kernel/module/internal.h
    patching file kernel/module/kallsyms.c
    patching file kernel/module/main.c
    patching file kernel/module/tracking.c
    patching file kernel/module/tree_lookup.c
    patching file kernel/module/version.c
    patching file kernel/sched/core.c
    patching file kernel/static_call_inline.c
    patching file kernel/trace/bpf_trace.c
    patching file kernel/trace/trace.c
    patching file kernel/trace/trace_kprobe.c
    patching file lib/bug.c
    patching file lib/dump_stack.c
    patching file localversion-rt
Last edit: 16 Jan 2025 13:53 by gene_weber.

Please Log in or Create an account to join the conversation.

  • gene_weber
  • gene_weber's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
15 Jan 2025 20:38 - 16 Jan 2025 14:32 #319050 by gene_weber
Replied by gene_weber on topic Raspberry Pi OS PREEMPT RT 6.13 Kernel Cookbook
Create the base .config file for the Raspberry Pi 4 64-bit:
KERNEL=kernel8
make bcm2711_defconfig

NOTE: For a Raspberry Pi 5 64-bit the lines should be:
KERNEL=kernel_2712
make bcm2712_defconfig


Make / launch the menuconfig utility:
Install the required package:
sudo apt install libncurses5-dev
make menuconfig

Under General Setup, select: Fully Preemptible Kernel (Real-Time)Exit and Save.


Change Local version name. vi .config and change the following line:
CONFIG_LOCALVERSION="-v8"
To:
CONFIG_LOCALVERSION="-v8_full_preempt"


Build the kernel and log the output (Note this took over 2 hr 10 min):
make -j6 Image.gz modules dtbs 2>&1 | tee make.log

NOTE: During the compilation process the temperature of the ARM processor increased from ~30°C to as high as 43°C. I have a heat-sink and cooling fan on my Pi. If you don't, you may want to keep an eye on the temp (/usr/bin/vcgencmd measure_temp). The maximum rating on the Pi is 85°C.


Install the kernel modules onto the boot media and log the output:
sudo make -j6 modules_install 2>&1 | tee install.log

create a backup image of the current kernel, install the fresh kernel image, overlays, README, and unmount the partitions:
sudo cp /boot/firmware/$KERNEL.img /boot/firmware/$KERNEL-backup.img
sudo cp arch/arm64/boot/Image.gz /boot/firmware/$KERNEL.img
sudo cp arch/arm64/boot/dts/broadcom/*.dtb /boot/firmware/
sudo cp arch/arm64/boot/dts/overlays/*.dtb* /boot/firmware/overlays/
sudo cp arch/arm64/boot/dts/overlays/README /boot/firmware/overlays/


Reboot the Raspberry Pi to run the new kernel:
sudo reboot

Check kernel after reboot:
uname -a
    Linux raspberrypi 6.13.0-rc7-rt3-v8_full_preempt+ #1 SMP PREEMPT_RT Tue Jan 14 12:49:46 EST 2025 aarch64 GNU/Linux


From the LinuxCNC downloads page , download LinuxCNC Uspace 2.9.3 arm64 .

Install the Gdebi installer:
sudo apt install gdebi
Run the Gdebi installer and install linuxcnc-uspace_2.9.3_arm64.deb

EDITED 1/16/25: Changed image to X11 pre-optimization latency.
Ran a baseline latency test before making any jitter optimizations.
latency-histogram --nobase --sbins 1000
    Note: Using POSIX realtime


I checked the real-time attributes of the latency-histogram process (the process ID for this run was 10527):
chrt -p 10527
    pid 10527's current scheduling policy: SCHED_OTHER
    pid 10527's current scheduling priority: 0

I had thought the policy would be SCHED_FIFO, or SCHED_RR. But even after I added CPU isolation it remains SCHED_OTHER.

I will probably create a separate thread about jitter optimization.
Attachments:
Last edit: 16 Jan 2025 14:32 by gene_weber.
The following user(s) said Thank You: tommylight, rodw

Please Log in or Create an account to join the conversation.

More
15 Jan 2025 22:12 #319057 by rodw

I will probably create a separate thread about jitter optimization.

This is badly needed based on the reported latency which is unusable

Please Log in or Create an account to join the conversation.

  • cornholio
  • cornholio's Avatar
  • Away
  • Platinum Member
  • Platinum Member
More
15 Jan 2025 23:36 #319061 by cornholio
Replied by cornholio on topic Raspberry Pi OS PREEMPT RT 6.13 Kernel Cookbook


Kernel built whilst using Rod's imager, running over a ssh session.
Attachments:

Please Log in or Create an account to join the conversation.

  • cornholio
  • cornholio's Avatar
  • Away
  • Platinum Member
  • Platinum Member
More
16 Jan 2025 00:03 #319063 by cornholio
Replied by cornholio on topic Raspberry Pi OS PREEMPT RT 6.13 Kernel Cookbook
If you're using a Rasp OS image you'll want to swap from wayland to X if you are looking to run axis, the "compatibility layer" is "not quite optimum".

Please Log in or Create an account to join the conversation.

  • gene_weber
  • gene_weber's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
16 Jan 2025 00:08 #319064 by gene_weber
Replied by gene_weber on topic Raspberry Pi OS PREEMPT RT 6.13 Kernel Cookbook
Thanks for the heads up regarding wayland and axis.

Please Log in or Create an account to join the conversation.

  • cornholio
  • cornholio's Avatar
  • Away
  • Platinum Member
  • Platinum Member
More
16 Jan 2025 00:29 #319067 by cornholio
Replied by cornholio on topic Raspberry Pi OS PREEMPT RT 6.13 Kernel Cookbook
It would be interesting to see the latency values after switching to X. This can be done via the config utility for the Rpi, the actual binary name escapes me ATM.

Please Log in or Create an account to join the conversation.

More
16 Jan 2025 01:19 #319074 by rodw
In a Debian environment tasksel allows you to install a number of desktops. There is a (X) reason why we use XFCE in the linuxcnc images. It uses X windows not wayland

Please Log in or Create an account to join the conversation.

  • cornholio
  • cornholio's Avatar
  • Away
  • Platinum Member
  • Platinum Member
More
16 Jan 2025 01:36 #319076 by cornholio
Replied by cornholio on topic Raspberry Pi OS PREEMPT RT 6.13 Kernel Cookbook
First of all your opening post was great. I'm actaully giving it a go now, just wish I had a nvme drive hooked up to the PCIe bus.
There's a couple of points regarding Linux kernels.

Sometimes the latest kernel may not be the optimum for your hardware.
The kernel you are using is a "release candidate" whilst usable for all intents is still going through some work.
The 6.1.y branch works fine on the RPi4, I think the kernel in the Linuxcnc image maybe 6.1.54
The RPi platforms with regards to Linuxcnc are a little different to the x86 platforms. Your Rpi4 & Rpi5 hardware is what it is. Whereas you may need a specific kernel dpending which x86 motherboard you buy.
The Rpi branch that appears to be active is the 6.6.y branch, so one could assume this is the branch that is receiving the most "bug fixies".
Simple things like isolating cores 2,3 will see latency drop. There doesn't seem to be as many options/tricks for latency on the RPi platforms as the x86 platforms.
I would chose to use a kernel that is a couple of version behind the the latest and greatest if it more tried in the real world and has better latency.
In my discussions with royka we talked about the fact that a kernel that is a little older is sometimes the better option.
Due to the issues with wayland I would recommend the use of the image form the Download page, Xorg is used. Plus it's easier to deploy ;)

Mind tho I'm not saying this is the only way to do it.
What I would suggest is trying some of the earlier kernels to see how performance differs.

One thing regarding the 6.6.y branch.
The latest version is 6.6.70 which I couldn't find any real time patches for.
The previous commit is 6.6.65 for which there are rt patches, but for some reason the overlay's aren't in the source. Which is a pain when building an image using Rod's image builder.

It seems, ATM, the deeper you dive it can seem a little confusing.

Sorry for the multiple posts but this editor is PITA.

Please Log in or Create an account to join the conversation.

  • cornholio
  • cornholio's Avatar
  • Away
  • Platinum Member
  • Platinum Member
More
16 Jan 2025 02:41 #319081 by cornholio
Replied by cornholio on topic Raspberry Pi OS PREEMPT RT 6.13 Kernel Cookbook
Kernel as built following instructions

Attachments:
The following user(s) said Thank You: rodw

Please Log in or Create an account to join the conversation.

Time to create page: 0.132 seconds
Powered by Kunena Forum