1. Throughput tuning breaks real-time bounds
General-purpose server tuning optimises mean throughput: batching interrupts, coalescing network rings, migrating tasks across sockets, and parking idle cores in deep C-states. Deterministic control loops care only about the right tail — the single worst wakeup across a hundred million timer periods. A system with a 3 µs average wakeup and one 420 µs spike misses a 250 µs motor-control deadline just as surely as a broken driver.
With CONFIG_PREEMPT_RT merged across mainline architectures in Linux 6.12 LTS, most in-kernel critical sections become preemptible without out-of-tree patchsets. Enabling the Kconfig symbol is only step one; bounding worst-case latency requires aligning four layers simultaneously: kernel locking semantics, interrupt threading, scheduler housekeeping isolation, and platform firmware power states.
2. What CONFIG_PREEMPT_RT actually changes in the kernel
On a standard voluntary or preemptible kernel, spinlock_t disables preemption and — in spin_lock_irqsave() — masks local CPU interrupts. Any high-priority SCHED_FIFO task waking on that CPU stalls until the lock holder finishes.
Under PREEMPT_RT, kernel primitives split into two explicit contracts:
spinlock_t&rwlock_tbecome sleeping locks backed byrt_mutexwith priority inheritance (PI). Preemption and hardware interrupts remain enabled; if a priority-90 control thread blocks on a lock held by a priority-20 worker, the holder inherits priority 90 until release.raw_spinlock_tstays a true spinning lock that disables preemption and interrupts. It is reserved strictly for the scheduler core, RCU internals, GIC/APIC irqchip controllers, and low-level timer hardware. Any driver holding araw_spinlock_tacross MMIO polling or variable-length loops directly inflates worst-case IRQ latency.- Hardware interrupt handlers run as scheduled kernel threads (
irq/<N>-<name>atSCHED_FIFOpriority 50 by default) unless registered withIRQF_NO_THREAD. Softirqs move into per-CPUktimersandksoftirqdcontext rather than hijacking arbitrary task returns.
3. Kernel boot parameters and housekeeping CPU split
Never run deterministic real-time threads on CPU 0. Boot housekeeping, timekeeping duties, and non-affinitised platform work gravitate there. Partition your cores into housekeeping CPUs (for SSH, logging, storage, and non-RT network traffic) and isolated RT CPUs:
# Example 4-core industrial SoC: CPUs 0-1 housekeeping, CPUs 2-3 isolated RT
isolcpus=managed_irq,domain,2-3
irqaffinity=0-1
nohz_full=2-3
rcu_nocbs=2-3
rcutree.kthread_prio=2
processor.max_cstate=1 idle=poll
Verify the active isolation masks immediately after boot rather than assuming the bootloader passed the command line intact:
cat /sys/devices/system/cpu/isolated
# expected: 2-3
cat /sys/devices/system/cpu/nohz_full
# expected: 2-3
ps -eo pid,psr,cls,rtprio,comm | awk '$3=="FF" {print}'
Note the trade-off on idle=poll versus /dev/cpu_dma_latency: holding a file descriptor open on /dev/cpu_dma_latency with a 32-bit integer value of 0 written to it prevents deep package C-state transitions dynamically while your real-time application runs, without burning thermal headroom when the machine is disarmed.
4. Ranking IRQ priorities above ksoftirqd, below the control loop
Leaving every threaded interrupt at the default SCHED_FIFO priority 50 means an Ethernet burst or MMC block completion preempts or contends with your fieldbus or sensor interrupt. Establish an explicit priority hierarchy:
| Layer | Policy & priority | CPU mask |
|---|---|---|
| Hard safety / servo loop | SCHED_FIFO 90 (mlockall(MCL_CURRENT|MCL_FUTURE)) | Isolated (2–3) |
| Deterministic bus IRQ (EtherCAT / CAN / SPI ADC) | SCHED_FIFO 80 | Isolated core matching consumer |
ktimers/* on RT cores | SCHED_FIFO 75 | Per-CPU |
| Non-RT peripheral IRQs (NVMe, USB, management NIC) | SCHED_FIFO 50 (default) | Housekeeping (0–1) |
rcuc/*, rcuog/*, rcuop/* | SCHED_FIFO 2–5 | Housekeeping (0–1) |
5. Hunting tail spikes with rtla timerlat and osnoise
cyclictest tells you that a 190 µs spike occurred; it does not tell you who disabled interrupts or held preemption before your thread ran. Mainline Linux ships rtla on top of the kernel timerlat and osnoise tracers specifically to close that gap without flooding ring buffers during normal cycles.
# Stress housekeeping & memory buses while watching isolated cores 2-3
stress-ng --cpu 2 --taskset 0-1 --io 2 --vm 1 --vm-bytes 256M --timeout 300s &
# Trigger an ftrace snapshot automatically if thread latency exceeds 40 us
rtla timerlat top -q -c 2-3 -p 1000 -P f:90 -i 25 -T 40 --aa-only 5
Read the two latency columns separately:
- IRQ Timer Latency measures hardware timer interrupt delay. A spike here points below the scheduler: a driver holding a
raw_spinlock_t,local_irq_disable(), cache/interconnect stall, UEFI runtime service, or x86/ARM firmware SMM/EL3 trap. - Thread Timer Latency measures the gap from timer IRQ exit to the real-time thread executing. When IRQ latency stays at 2 µs while Thread latency jumps to 85 µs, inspect the auto-analysis output from
rtla: it names the exact blocking kernel thread, preemption-disabled section, or page fault.
6. Three userspace mistakes that ruin a tuned RT kernel
- Lazy page faults and stack growth: Always call
mlockall(MCL_CURRENT | MCL_FUTURE)and pre-touch the worst-case thread stack plus heap arenas before entering the cyclic loop. One minor fault under memory pressure costs tens of microseconds. - Standard
pthread_mutex_twithout PI: Initialise shared locks withpthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT)so the kernel backs them withFUTEX_LOCK_PIinstead of plain wait queues subject to unbounded priority inversion. - Synchronous logging on the RT thread: Calling
fprintf(),syslog(), or allocating sockets inside the control loop hands your deterministic thread directly to VFS writeback or journal locks. Push fixed-size records into a lock-free SPSC ring buffer drained by a housekeeping thread on CPU 0.

