# PREEMPT_RT

**PREEMPT_RT**（实时抢占）是一组Linux内核改动，如今已成为主线配置选项，它通过让内核几乎完全可抢占，将内核转变为实时内核：用可抢占、具备优先级继承感知能力的睡眠锁取代自旋锁，并强制中断处理程序作为由调度器控制的线程运行。<sup>[1](https://kernel.org/doc/html/next/core-api/real-time/theory.html)</sup>经过约二十年的树外补丁集阶段，它于2024年随Linux 6.12在内核主线中启用。<sup>[2](https://kernel-internals.org/locking/preempt-rt/)</sup>

| 关键事实 | 详情 |
|---|---|
| 是什么 | 一种内核抢占模型，`CONFIG_PREEMPT_RT`，使Linux内核除少数关键路径（如入口代码、调度器和底层中断处理）外完全可抢占<sup>[1](https://kernel.org/doc/html/next/core-api/real-time/theory.html)</sup> |
| 锁的变化 | `spinlock_t`和`rwlock_t`在rtmutex之上实现，变为具有优先级继承的睡眠锁；`raw_spinlock_t`保持严格自旋行为<sup>[1](https://kernel.org/doc/html/next/core-api/real-time/theory.html)</sup><sup> • </sup><sup>[3](https://kernel.org/doc/html/latest/locking/locktypes.html)</sup> |
| 中断 | 中断处理程序强制线程化：中断上下文中的主处理程序只负责唤醒线程化处理程序，后者在进程上下文中运行，默认以SCHED_FIFO策略、优先级50（MAX_RT_PRIO / 2）执行<sup>[1](https://kernel.org/doc/html/next/core-api/real-time/theory.html)</sup> |
| 历史 | 工作始于约2004年，由英戈·莫尔纳（Ingo Molnar）和托马斯·格莱克斯纳（Thomas Gleixner）发起；启用补丁系列由塞巴斯蒂安·安德烈·谢维奥尔（Sebastian Andrzej Siewior）于2024年9月6日发布，并为Linux 6.12合并<sup>[2](https://kernel-internals.org/locking/preempt-rt/)</sup><sup> • </sup><sup>[4](https://fosdem.org/2025/events/attachments/fosdem-2025-5411-linux-kernel-mainline-real-time-history-support-and-experience-based-on-robotic-and-automotive-projects/slides/236080/fosdem25_SS91561.pdf)</sup> |
| 实测效果 | 在树莓派5（Raspberry Pi 5）上施加压力测试，Cyclictest最大调度延迟从标准内核的36,802微秒降至PREEMPT_RT的125微秒，降至原来的约1/294<sup>[5](https://archives.ecrts.org/fileadmin/WebsitesArchiv/ecrts2025/wp-content/uploads/2024/07/ospert24-proceedings.pdf)</sup> |
| 配置 | 必须启用`CONFIG_PREEMPT_RT`才能获得完全可抢占的内核（在内核实时配置指南中严重性为“致命”）；它默认启用`CONFIG_EFI_DISABLE_RUNTIME`<sup>[6](https://www.kernel.org/doc/html/next/core-api/real-time/kernel-configuration.html)</sup> |

## 睡眠自旋锁

在标准内核中，`spinlock_t`为同时从中断上下文和进程上下文访问的数据结构提供同步，带`_irq()`或`_irqsave()`后缀的锁定函数在获取锁之前禁用中断。<sup>[7](https://cdn.kernel.org/doc/html/latest/core-api/real-time/differences.html)</sup>以这种方式禁用抢占会阻止调度器切换到更高优先级的任务，从而增加延迟。<sup>[1](https://kernel.org/doc/html/next/core-api/real-time/theory.html)</sup>

PREEMPT_RT用不禁用抢占的睡眠自旋锁取代这些自旋锁。在PREEMPT_RT内核上，`spinlock_t`使用rtmutex实现：尝试获取争用锁的任务会禁用CPU迁移，通过优先级继承将自己的优先级捐赠给锁持有者，并在等待时主动调度出去。<sup>[1](https://kernel.org/doc/html/next/core-api/real-time/theory.html)</sup>为尽量减少对内核源代码的改动，现有的`spinlock_t`类型及其函数保留旧名称，但在启用PREEMPT_RT时指向rtmutex锁。<sup>[8](https://wiki.linuxfoundation.org/realtime/documentation/technical_details/sleeping_spinlocks)</sup>等待睡眠自旋锁的任务会进入睡眠，并在锁释放时被唤醒；原始自旋锁保持原有行为。<sup>[9](https://wiki.linuxfoundation.org/realtime/documentation/technical_details/start)</sup>

**优先级继承。** PREEMPT_RT内核中的`spinlock_t`和互斥体等锁类型在rtmutex之上实现，后者支持优先级继承。当任务阻塞在这样的锁上时，该机制临时将阻塞任务的调度参数传播给锁持有者。例如，如果一个SCHED_FIFO任务A阻塞在一个由SCHED_OTHER任务B持有的锁上，任务A的策略和优先级会被B临时继承，B随后实际上成为系统中最高优先级的任务，能够继续推进并释放锁；B在释放后恢复其原始参数。<sup>[1](https://kernel.org/doc/html/next/core-api/real-time/theory.html)</sup>

**哪些保持为原始自旋锁。** `raw_spinlock_t`在所有内核中保持严格自旋锁，包括PREEMPT_RT内核。其使用应仅限于真正关键的核心代码、底层中断处理，以及要求禁用抢占或中断的场合，例如安全访问硬件状态；当临界区非常小时也可以使用它，以节省RT互斥体的开销。<sup>[3](https://kernel.org/doc/html/latest/locking/locktypes.html)</sup>由于PREEMPT_RT将`spinlock_t`和`rwlock_t`从自旋改为睡眠，并用每CPU的`spinlock_t`替代`local_lock`，因此不能在持有原始自旋锁时获取这些锁。由此产生的嵌套顺序是：睡眠锁在最外层，然后是`spinlock_t`、`rwlock_t`和`local_lock`，然后是`raw_spinlock_t`和位自旋锁在最内层。如果违反这些约束，Lockdep会发出警告，无论是否启用PREEMPT_RT。<sup>[3](https://kernel.org/doc/html/latest/locking/locktypes.html)</sup>

在PREEMPT_RT上，`local_lock_t`使用每CPU的`spinlock_t`实现，在保护每CPU数据的同时保持系统可抢占。由于`spinlock_t`不再阻止抢占，开发者不应仅依赖`preempt_disable()`来保护每CPU数据，特别是在临界区可能运行无界时间或调用可能睡眠的API时。<sup>[7](https://cdn.kernel.org/doc/html/latest/core-api/real-time/differences.html)</sup>

## 线程化中断

中断处理程序是在禁用抢占且不受调度器控制的情况下运行的第二种代码来源。PREEMPT_RT强制实行线程化中断处理程序，将处理分为两个阶段：在中断上下文中执行的主处理程序，中断处于禁用状态，其唯一职责是唤醒关联的线程化处理程序；以及线程化处理程序，即传递给`request_irq()`的函数，在进程上下文中运行，受调度器控制。从唤醒中断线程到线程化处理完成，中断源在中断控制器中被屏蔽，因此设备中断保持挂起状态，不会重新触发CPU。<sup>[1](https://kernel.org/doc/html/next/core-api/real-time/theory.html)</sup>

默认情况下，线程化处理程序以SCHED_FIFO策略、优先级50运行，位于最低和最高实时优先级的中间。如果线程化处理程序引发软中断，这些软中断例程在线程化处理程序完成后、在同一线程内运行，抢占处于启用状态。<sup>[1](https://kernel.org/doc/html/next/core-api/real-time/theory.html)</sup>线程化中断处理本身在Linux 2.6.30进入主线，从树外RT补丁集引入，在那里已被依赖多年。<sup>[10](https://kernel-internals.org/interrupts/threaded-irq/)</sup>该机制也可以在没有PREEMPT_RT的主线内核中使用`threadirqs`内核命令行选项强制启用。<sup>[9](https://wiki.linuxfoundation.org/realtime/documentation/technical_details/start)</sup>

睡眠锁和强制线程化中断共同显著减少了禁用中断或抢占的代码段，使调度器能够抢占当前上下文并切换到更高优先级的任务。<sup>[1](https://kernel.org/doc/html/next/core-api/real-time/theory.html)</sup>

## 对内核代码的影响

锁类别的变化会波及其他内核子系统。PREEMPT_RT中的内存分配器内部使用睡眠锁，在禁用抢占时无法获取，因此来自中断上下文的`GFP_ATOMIC`分配约定无法工作。这通常不是问题，因为PREEMPT_RT将传统上在禁用抢占或中断下运行的大多数上下文移入线程化上下文，那里允许睡眠；明确禁用抢占或中断的代码必须在临界区外执行分配，同样的限制适用于`kfree()`和`free_pages()`。<sup>[7](https://cdn.kernel.org/doc/html/latest/core-api/real-time/differences.html)</sup>

其他执行上下文也会发生变化。在PREEMPT_RT上，高分辨率定时器默认在软中断上下文中执行，通常位于最低实时优先级的ktimerssd线程内，定时器必须标记`HRTIMER_MODE_HARD`标志才能在硬中断上下文中运行。irq_work回调（可能获取睡眠锁或执行时间无界）由每CPU的irq_work内核线程在最低实时优先级处理，而不是立即在中断上下文中运行。<sup>[7](https://cdn.kernel.org/doc/html/latest/core-api/real-time/differences.html)</sup>

将架构移植为支持PREEMPT_RT需要强制线程化中断（`CONFIG_IRQ_FORCED_THREADING`，真正的硬IRQ处理程序标记为`IRQF_NO_THREAD`）、内核抢占支持，以及在满足要求后在架构的Kconfig中选择`ARCH_SUPPORTS_RT`。<sup>[11](https://cdn.kernel.org/doc/html/latest/core-api/real-time/architecture-porting.html)</sup>

## 历史与主线合并

实时Linux工作早于PREEMPT_RT项目本身。基于Linux 2.2.5构建的KURT 2.0为后来成为PREEMPT_RT的项目提供了蓝图，其关键设计概念之一——将中断处理程序作为线程运行——如今被称为线程化中断。<sup>[12](https://realtime-linux.org/how-preempt_rt-works/)</sup>2004年，英戈·莫尔纳和托马斯·格莱克斯纳加入了实时抢占补丁工作。<sup>[4](https://fosdem.org/2025/events/attachments/fosdem-2025-5411-linux-kernel-mainline-real-time-history-support-and-experience-based-on-robotic-and-automotive-projects/slides/236080/fosdem25_SS91561.pdf)</sup>PREEMPT_RT补丁集一直由托马斯·格莱克斯纳领导，他是Linux基金会（Linux Foundation）院士，担任Linutronix GmbH的CTO；格莱克斯纳和谢维奥尔都在Linutronix担任工程师，这是一家专注于实时Linux的咨询公司，在项目存在的几乎整个过程中，一直是PREEMPT_RT核心团队大多数成员的雇主。<sup>[13](https://www.linux.com/news/in-the-trenches-with-thomas-gleixner-real-time-linux-kernel-patch-set/)</sup><sup> • </sup><sup>[2](https://kernel-internals.org/locking/preempt-rt/)</sup>

补丁集的许多部分在二十年里零散进入主线，而非一次合并事件：其中包括线程化中断处理程序、通用rt_mutex基础设施和futex的优先级继承。具有优先级继承的RT-mutex于2006年在v2.6.16中合并。<sup>[2](https://kernel-internals.org/locking/preempt-rt/)</sup><sup> • </sup><sup>[4](https://fosdem.org/2025/events/attachments/fosdem-2025-5411-linux-kernel-mainline-real-time-history-support-and-experience-based-on-robotic-and-automotive-projects/slides/236080/fosdem25_SS91561.pdf)</sup>2015年，项目被移交给Linux基金会，成为官方Linux实时解决方案。<sup>[14](https://pmc.ncbi.nlm.nih.gov/articles/PMC6423167/)</sup>

最后一步发生在2024年9月，谢维奥尔于9月6日发布了在主线上启用`PREEMPT_RT`的补丁系列；支持在6.12合并窗口期间被合并，并在Linux 6.12-rc1中作为配置选项（位于“EXPERT”菜单下）启用。<sup>[2](https://kernel-internals.org/locking/preempt-rt/)</sup><sup> • </sup><sup>[15](https://lwn.net/Articles/992185/)</sup>到那时，树外补丁集已经足够可用，供应商在其上发布了产品和发行版，业界使用PREEMPT_RT补丁已有10到15年。<sup>[2](https://kernel-internals.org/locking/preempt-rt/)</sup><sup> • </sup><sup>[16](https://bootlin.com/pub/conferences/2025/cdl/kernel-review.pdf)</sup>

## 实测延迟效果

实时Linux的标准基准测试工具是Cyclictest，由托马斯·格莱克斯纳开发，它测量调度延迟，即线程预期唤醒时间与实际唤醒时间之间的差值，报告最大值、最小值和平均值。<sup>[5](https://archives.ecrts.org/fileadmin/WebsitesArchiv/ecrts2025/wp-content/uploads/2024/07/ospert24-proceedings.pdf)</sup>在一项树莓派5上的研究中，Cyclictest任务每100微秒唤醒一次，持续一小时，同时在stress-ng和iperf3负载下运行，标准内核显示最大观测延迟为36,802微秒，而PREEMPT_RT内核为125微秒，最大延迟降至原来的约1/294，作者将改进归因于确定性提高而非原始速度。<sup>[5](https://archives.ecrts.org/fileadmin/WebsitesArchiv/ecrts2025/wp-content/uploads/2024/07/ospert24-proceedings.pdf)</sup>一项Linux 6.12的基准研究发现，PREEMPT_RT内核在争用情况下优于标准PREEMPT内核。<sup>[17](https://www.mdpi.com/2079-9292/14/22/4515)</sup>

与Xenomai等双内核方法的比较（Xenomai与Linux并行运行一个单独的实时核心）通常在最坏情况和平均调度延迟方面有利于Xenomai。在一项嵌入式音乐应用研究中，Xenomai保证最坏情况延迟为12微秒，而PREEMPT_RT为20微秒，平均调度延迟分别为2.5微秒和6.5微秒；同一研究发现核心隔离在PREEMPT_RT上有效。综述文献得出结论，RTOS和协同内核方法在实时性能上仍优于Linux PREEMPT_RT，而Linux在平均性能上更好。<sup>[18](https://www.researchgate.net/publication/358135895_A_Comparison_of_Real-Time_Linux-Based_Architectures_for_Embedded_Musical_Applications)</sup><sup> • </sup><sup>[19](https://re.public.polimi.it/retrieve/e0c31c12-9844-4599-e053-1705fe0aef77/11311-1076057_Reghenzani.pdf)</sup>然而，该项目的既定目标从来不是以牺牲通用功能为代价使Linux成为完全硬实时内核，而是服务于混合关键性工作负载的广泛用例。<sup>[5](https://archives.ecrts.org/fileadmin/WebsitesArchiv/ecrts2025/wp-content/uploads/2024/07/ospert24-proceedings.pdf)</sup><sup> • </sup><sup>[13](https://www.linux.com/news/in-the-trenches-with-thomas-gleixner-real-time-linux-kernel-patch-set/)</sup>

## 配置

内核的实时配置指南将`CONFIG_PREEMPT_RT`视为完全可抢占、具备实时能力内核的强制选项，如果省略则严重性为“致命”。系统集成商的相关指导包括：使用performance调控器保持CPU频率固定，避免ONDEMAND调控器，因为其频率变化损害确定性；为周期性工作负载限制C状态；禁用软件锁死检测器，改用硬件看门狗，因为其周期性硬IRQ定时器回调会导致延迟尖峰。指南强调没有一刀切的配置，错误的设置可能引入新的最大延迟。<sup>[6](https://www.kernel.org/doc/html/next/core-api/real-time/kernel-configuration.html)</sup>

## References

1. [Theory of operation, The Linux Kernel documentation](https://kernel.org/doc/html/next/core-api/real-time/theory.html)
2. [PREEMPT_RT: Twenty Years to Mainline, Linux Kernel Internals](https://kernel-internals.org/locking/preempt-rt/)
3. [Lock types and their rules, The Linux Kernel documentation](https://kernel.org/doc/html/latest/locking/locktypes.html)
4. [Linux Kernel Mainline Real-Time History, Support and Experience, FOSDEM 2025 slides](https://fosdem.org/2025/events/attachments/fosdem-2025-5411-linux-kernel-mainline-real-time-history-support-and-experience-based-on-robotic-and-automotive-projects/slides/236080/fosdem25_SS91561.pdf)
5. [Evaluation of Real-Time Linux with the PREEMPT_RT patch on the Raspberry Pi 5, OSPERT 2024 proceedings](https://archives.ecrts.org/fileadmin/WebsitesArchiv/ecrts2025/wp-content/uploads/2024/07/ospert24-proceedings.pdf)
6. [Real-Time Kernel configuration, The Linux Kernel documentation](https://www.kernel.org/doc/html/next/core-api/real-time/kernel-configuration.html)
7. [How realtime kernels differ, The Linux Kernel documentation](https://cdn.kernel.org/doc/html/latest/core-api/real-time/differences.html)
8. [Sleeping spinlocks, Real-Time Linux Wiki, Linux Foundation](https://wiki.linuxfoundation.org/realtime/documentation/technical_details/sleeping_spinlocks)
9. [Technical details start page, Real-Time Linux Wiki, Linux Foundation](https://wiki.linuxfoundation.org/realtime/documentation/technical_details/start)
10. [Threaded IRQs, Linux Kernel Internals](https://kernel-internals.org/interrupts/threaded-irq/)
11. [Porting an architecture to support PREEMPT_RT, The Linux Kernel documentation](https://cdn.kernel.org/doc/html/latest/core-api/real-time/architecture-porting.html)
12. [How PREEMPT_RT works, Realtime Linux](https://realtime-linux.org/how-preempt_rt-works/)
13. [In the trenches with Thomas Gleixner, real-time Linux kernel patch set, Linux.com](https://www.linux.com/news/in-the-trenches-with-thomas-gleixner-real-time-linux-kernel-patch-set/)
14. [RTHybrid: A Standardized and Open-Source Real-Time Software Model Library for Experimental Neuroscience, PMC](https://pmc.ncbi.nlm.nih.gov/articles/PMC6423167/)
15. [Kernel prepatch 6.12-rc1, LWN.net](https://lwn.net/Articles/992185/)
16. [What's new in the Linux kernel? A year in review, Bootlin](https://bootlin.com/pub/conferences/2025/cdl/kernel-review.pdf)
17. [A Multi-Core Benchmark Framework for Linux-Based Embedded Systems Using Synthetic Task-Set Generation, Electronics](https://www.mdpi.com/2079-9292/14/22/4515)
18. [A Comparison of Real-Time Linux-Based Architectures for Embedded Musical Applications, ResearchGate](https://www.researchgate.net/publication/358135895_A_Comparison_of_Real-Time_Linux-Based_Architectures_for_Embedded_Musical_Applications)
19. [The Real-Time Linux Kernel: A Survey on PREEMPT_RT, Politecnico di Milano](https://re.public.polimi.it/retrieve/e0c31c12-9844-4599-e053-1705fe0aef77/11311-1076057_Reghenzani.pdf)

---
*Initially written Oct 15, 2026 · Edited: Oct 25, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Operating systems › Unix-like and Linux › Unix, BSD, and kernel internals

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/preempt-rt

English original: https://www.edgepedia.org/preempt-rt
