rt kernel原理简介

RT 的基本原理是分解主线内核中任意长的抢占/中断禁用部分。
大多数抢占/中断禁用部分都是通过获取锁(spinlock、rwlock)隐式实现的。只有少数部分是通过发出 preempt/local_irq_disable() 显式实现的
RT 用 RT 感知对应部分替换 spinlock/rwlock,这些对应部分
- 不禁用抢占/中断
- 防止迁移以保持抢占禁用的隐式迁移禁用语义
- 将底层锁定原语转换为优先级继承感知机制,又名 rtmutex。
为了使上述操作有效,RT 强制将中断和软中断处理放入线程上下文中,但明确标记为中断安全的中断(IRQF_NOTHREAD)除外。
因此,大多数内核代码都完全可抢占。当然,仍有一些代码部分需要硬禁用抢占/中断。这几乎就是初始低级入口代码、硬中断处理代码(仅唤醒线程)、上下文切换代码和其他一些相当低级的函数(vmenter/exit …)。
这还要求我们仍然有禁用抢占/中断的锁。这就是我们有 raw_spinlock 和 spinlock 的原因。spinlock 被 RT 原语取代,而 raw_spinlock 的行为类似于非 RT 内核上的传统自旋锁(禁用抢占/中断)。
但这也意味着任何明确禁用抢占或中断而不采取自旋/读写锁的代码都可能触发以下问题:
- 调用需要在 RT 上可抢占/可休眠的代码可能会导致休眠失败。
- 在 RT 术语中可能具有不受约束或不受欢迎的运行时间长度,而调度程序没有任何机会对其进行控制。
除此之外,RT 对锁所有权有更严格的看法,因为除了实数计数信号量之外,几乎所有的锁原语都被优先级继承感知的对应物所取代。PI 感知锁不仅要求它们只能在可抢占的上下文中使用,而且出于显而易见的原因,它们还具有严格的 locker == unlocker 要求。

原文:
The basic principle of RT is to break up the arbitrary long preemption/interrupt disabled sections of the mainline kernel.
Most preempt/interrupt disabled sections are implicit by taking locks

(spinlock, rwlock). Just a few are explicit by issuing preempt/local_irq_disable()
RT substitutes spinlock/rwlock with RT aware counterparts which
- Do not disable preemption/interrupts
- Prevent migration to keep the implicit migrate disable semantics of preempt disable
- Convert the underlying lock primitive to a priority inheritance aware mechanism, aka. rtmutex.
In order to make the above work, RT forces interrupt and soft interrupt processing into thread context except for interrupts which are explicitely marked as interrupt safe (IRQF_NOTHREAD).
As a consequence most of the kernel code becomes fully preemptible. Of course there are still code parts which require that preemption/interrupts are hard disabled. That’s pretty much initial low level entry code, hard interrupt handling code (which just wakes up the threads), context switch code and some other rather low level functions (vmenter/exit …).
That also requires that we have still locks which disable preemption/interrupts. That’s why we have raw_spinlock and spinlock. spinlock is substituted with a RT primitive while raw_spinlock behaves like the traditional spinlock on a non RT kernel (disables preemption/interrupts).
But that also means any code which explcitely disables preemption or interrupts without taking a spin/rw lock can trigger the following issues:
- Calling into code which requires to be preemtible/sleepable on RT results in a might sleep splat.
- Has in RT terms potentially unbound or undesired runtime length without any chance for the scheduler to control it.
Aside of that RT has a more strict view vs. lock ownership because almost all lock primitives except real counting semaphores are substituted by priority inheritance aware counterparts. PI aware locks have not only the requirement that they can only be taken in preemptible context , they also have a strict locker == unlocker requirement for obvious reasons.

ref:https://lore.kernel.org/bpf/20191018075800.238f4f9c@tagon/T/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值