1*f51fe3b7SSebastian Andrzej Siewior.. SPDX-License-Identifier: GPL-2.0 2*f51fe3b7SSebastian Andrzej Siewior 3*f51fe3b7SSebastian Andrzej Siewior============================================= 4*f51fe3b7SSebastian Andrzej SiewiorPorting an architecture to support PREEMPT_RT 5*f51fe3b7SSebastian Andrzej Siewior============================================= 6*f51fe3b7SSebastian Andrzej Siewior 7*f51fe3b7SSebastian Andrzej Siewior:Author: Sebastian Andrzej Siewior <bigeasy@linutronix.de> 8*f51fe3b7SSebastian Andrzej Siewior 9*f51fe3b7SSebastian Andrzej SiewiorThis list outlines the architecture specific requirements that must be 10*f51fe3b7SSebastian Andrzej Siewiorimplemented in order to enable PREEMPT_RT. Once all required features are 11*f51fe3b7SSebastian Andrzej Siewiorimplemented, ARCH_SUPPORTS_RT can be selected in architecture’s Kconfig to make 12*f51fe3b7SSebastian Andrzej SiewiorPREEMPT_RT selectable. 13*f51fe3b7SSebastian Andrzej SiewiorMany prerequisites (genirq support for example) are enforced by the common code 14*f51fe3b7SSebastian Andrzej Siewiorand are omitted here. 15*f51fe3b7SSebastian Andrzej Siewior 16*f51fe3b7SSebastian Andrzej SiewiorThe optional features are not strictly required but it is worth to consider 17*f51fe3b7SSebastian Andrzej Siewiorthem. 18*f51fe3b7SSebastian Andrzej Siewior 19*f51fe3b7SSebastian Andrzej SiewiorRequirements 20*f51fe3b7SSebastian Andrzej Siewior------------ 21*f51fe3b7SSebastian Andrzej Siewior 22*f51fe3b7SSebastian Andrzej SiewiorForced threaded interrupts 23*f51fe3b7SSebastian Andrzej Siewior CONFIG_IRQ_FORCED_THREADING must be selected. Any interrupts that must 24*f51fe3b7SSebastian Andrzej Siewior remain in hard-IRQ context must be marked with IRQF_NO_THREAD. This 25*f51fe3b7SSebastian Andrzej Siewior requirement applies for instance to clocksource event interrupts, 26*f51fe3b7SSebastian Andrzej Siewior perf interrupts and cascading interrupt-controller handlers. 27*f51fe3b7SSebastian Andrzej Siewior 28*f51fe3b7SSebastian Andrzej SiewiorPREEMPTION support 29*f51fe3b7SSebastian Andrzej Siewior Kernel preemption must be supported and requires that 30*f51fe3b7SSebastian Andrzej Siewior CONFIG_ARCH_NO_PREEMPT remain unselected. Scheduling requests, such as those 31*f51fe3b7SSebastian Andrzej Siewior issued from an interrupt or other exception handler, must be processed 32*f51fe3b7SSebastian Andrzej Siewior immediately. 33*f51fe3b7SSebastian Andrzej Siewior 34*f51fe3b7SSebastian Andrzej SiewiorPOSIX CPU timers and KVM 35*f51fe3b7SSebastian Andrzej Siewior POSIX CPU timers must expire from thread context rather than directly within 36*f51fe3b7SSebastian Andrzej Siewior the timer interrupt. This behavior is enabled by setting the configuration 37*f51fe3b7SSebastian Andrzej Siewior option CONFIG_HAVE_POSIX_CPU_TIMERS_TASK_WORK. 38*f51fe3b7SSebastian Andrzej Siewior When KVM is enabled, CONFIG_KVM_XFER_TO_GUEST_WORK must also be set to ensure 39*f51fe3b7SSebastian Andrzej Siewior that any pending work, such as POSIX timer expiration, is handled before 40*f51fe3b7SSebastian Andrzej Siewior transitioning into guest mode. 41*f51fe3b7SSebastian Andrzej Siewior 42*f51fe3b7SSebastian Andrzej SiewiorHard-IRQ and Soft-IRQ stacks 43*f51fe3b7SSebastian Andrzej Siewior Soft interrupts are handled in the thread context in which they are raised. If 44*f51fe3b7SSebastian Andrzej Siewior a soft interrupt is triggered from hard-IRQ context, its execution is deferred 45*f51fe3b7SSebastian Andrzej Siewior to the ksoftirqd thread. Preemption is never disabled during soft interrupt 46*f51fe3b7SSebastian Andrzej Siewior handling, which makes soft interrupts preemptible. 47*f51fe3b7SSebastian Andrzej Siewior If an architecture provides a custom __do_softirq() implementation that uses a 48*f51fe3b7SSebastian Andrzej Siewior separate stack, it must select CONFIG_HAVE_SOFTIRQ_ON_OWN_STACK. The 49*f51fe3b7SSebastian Andrzej Siewior functionality should only be enabled when CONFIG_SOFTIRQ_ON_OWN_STACK is set. 50*f51fe3b7SSebastian Andrzej Siewior 51*f51fe3b7SSebastian Andrzej SiewiorFPU and SIMD access in kernel mode 52*f51fe3b7SSebastian Andrzej Siewior FPU and SIMD registers are typically not used in kernel mode and are therefore 53*f51fe3b7SSebastian Andrzej Siewior not saved during kernel preemption. As a result, any kernel code that uses 54*f51fe3b7SSebastian Andrzej Siewior these registers must be enclosed within a kernel_fpu_begin() and 55*f51fe3b7SSebastian Andrzej Siewior kernel_fpu_end() section. 56*f51fe3b7SSebastian Andrzej Siewior The kernel_fpu_begin() function usually invokes local_bh_disable() to prevent 57*f51fe3b7SSebastian Andrzej Siewior interruptions from softirqs and to disable regular preemption. This allows the 58*f51fe3b7SSebastian Andrzej Siewior protected code to run safely in both thread and softirq contexts. 59*f51fe3b7SSebastian Andrzej Siewior On PREEMPT_RT kernels, however, kernel_fpu_begin() must not call 60*f51fe3b7SSebastian Andrzej Siewior local_bh_disable(). Instead, it should use preempt_disable(), since softirqs 61*f51fe3b7SSebastian Andrzej Siewior are always handled in thread context under PREEMPT_RT. In this case, disabling 62*f51fe3b7SSebastian Andrzej Siewior preemption alone is sufficient. 63*f51fe3b7SSebastian Andrzej Siewior The crypto subsystem operates on memory pages and requires users to "walk and 64*f51fe3b7SSebastian Andrzej Siewior map" these pages while processing a request. This operation must occur outside 65*f51fe3b7SSebastian Andrzej Siewior the kernel_fpu_begin()/ kernel_fpu_end() section because it requires preemption 66*f51fe3b7SSebastian Andrzej Siewior to be enabled. These preemption points are generally sufficient to avoid 67*f51fe3b7SSebastian Andrzej Siewior excessive scheduling latency. 68*f51fe3b7SSebastian Andrzej Siewior 69*f51fe3b7SSebastian Andrzej SiewiorException handlers 70*f51fe3b7SSebastian Andrzej Siewior Exception handlers, such as the page fault handler, typically enable interrupts 71*f51fe3b7SSebastian Andrzej Siewior early, before invoking any generic code to process the exception. This is 72*f51fe3b7SSebastian Andrzej Siewior necessary because handling a page fault may involve operations that can sleep. 73*f51fe3b7SSebastian Andrzej Siewior Enabling interrupts is especially important on PREEMPT_RT, where certain 74*f51fe3b7SSebastian Andrzej Siewior locks, such as spinlock_t, become sleepable. For example, handling an 75*f51fe3b7SSebastian Andrzej Siewior invalid opcode may result in sending a SIGILL signal to the user task. A 76*f51fe3b7SSebastian Andrzej Siewior debug excpetion will send a SIGTRAP signal. 77*f51fe3b7SSebastian Andrzej Siewior In both cases, if the exception occurred in user space, it is safe to enable 78*f51fe3b7SSebastian Andrzej Siewior interrupts early. Sending a signal requires both interrupts and kernel 79*f51fe3b7SSebastian Andrzej Siewior preemption to be enabled. 80*f51fe3b7SSebastian Andrzej Siewior 81*f51fe3b7SSebastian Andrzej SiewiorOptional features 82*f51fe3b7SSebastian Andrzej Siewior----------------- 83*f51fe3b7SSebastian Andrzej Siewior 84*f51fe3b7SSebastian Andrzej SiewiorTimer and clocksource 85*f51fe3b7SSebastian Andrzej Siewior A high-resolution clocksource and clockevents device are recommended. The 86*f51fe3b7SSebastian Andrzej Siewior clockevents device should support the CLOCK_EVT_FEAT_ONESHOT feature for 87*f51fe3b7SSebastian Andrzej Siewior optimal timer behavior. In most cases, microsecond-level accuracy is 88*f51fe3b7SSebastian Andrzej Siewior sufficient 89*f51fe3b7SSebastian Andrzej Siewior 90*f51fe3b7SSebastian Andrzej SiewiorLazy preemption 91*f51fe3b7SSebastian Andrzej Siewior This mechanism allows an in-kernel scheduling request for non-real-time tasks 92*f51fe3b7SSebastian Andrzej Siewior to be delayed until the task is about to return to user space. It helps avoid 93*f51fe3b7SSebastian Andrzej Siewior preempting a task that holds a sleeping lock at the time of the scheduling 94*f51fe3b7SSebastian Andrzej Siewior request. 95*f51fe3b7SSebastian Andrzej Siewior With CONFIG_GENERIC_IRQ_ENTRY enabled, supporting this feature requires 96*f51fe3b7SSebastian Andrzej Siewior defining a bit for TIF_NEED_RESCHED_LAZY, preferably near TIF_NEED_RESCHED. 97*f51fe3b7SSebastian Andrzej Siewior 98*f51fe3b7SSebastian Andrzej SiewiorSerial console with NBCON 99*f51fe3b7SSebastian Andrzej Siewior With PREEMPT_RT enabled, all console output is handled by a dedicated thread 100*f51fe3b7SSebastian Andrzej Siewior rather than directly from the context in which printk() is invoked. This design 101*f51fe3b7SSebastian Andrzej Siewior allows printk() to be safely used in atomic contexts. 102*f51fe3b7SSebastian Andrzej Siewior However, this also means that if the kernel crashes and cannot switch to the 103*f51fe3b7SSebastian Andrzej Siewior printing thread, no output will be visible preventing the system from printing 104*f51fe3b7SSebastian Andrzej Siewior its final messages. 105*f51fe3b7SSebastian Andrzej Siewior There are exceptions for immediate output, such as during panic() handling. To 106*f51fe3b7SSebastian Andrzej Siewior support this, the console driver must implement new-style lock handling. This 107*f51fe3b7SSebastian Andrzej Siewior involves setting the CON_NBCON flag in console::flags and providing 108*f51fe3b7SSebastian Andrzej Siewior implementations for the write_atomic, write_thread, device_lock, and 109*f51fe3b7SSebastian Andrzej Siewior device_unlock callbacks. 110