Linux spin_lock 与 NT KeAcquireSpinLock

发布于 2024-12-09 17:12:51 字数 581 浏览 2 评论 0 原文

据我所知:

  • NT 的 KeAcquireSpinLock 相当于 spin_lock_bh:一个将 IRQL 提升到 DISPATCH_LEVEL,另一个屏蔽下半部分中断——功能相同。虽然 NT 变体保留了 OldIrql,但 Linux 变体似乎没有在任何地方存储“wereInterruptsAlreadyMasked”。这是否意味着 spin_unlock_bh 总是会揭开它们的面纱?
  • NT 的 KeAcquireInterruptSpinLock 类似于 spin_lock_irqsave

spin_lock 在 NT 中的等价物是什么?

如果 spin_unlock_bh 始终取消屏蔽中断(在 NT 语言中,始终将 IRQL 降至 spin_lock 类似于 KeAcquireSpinLockAtDpcLevel

From what I can gather:

  • NT's KeAcquireSpinLock is equivalent to spin_lock_bh: the one raises IRQL to DISPATCH_LEVEL, the other masks the bottom half interrupts -- functionally the same. While the NT variant keeps the OldIrql, the Linux variant doesn't seem to store "wereInterruptsAlreadyMasked" anywhere. Does this mean spin_unlock_bh always unmasks them?
  • NT's KeAcquireInterruptSpinLock is like spin_lock_irqsave.

What is the NT equivalent of spin_lock?

If spin_unlock_bh always unmasks interrupts (in NT-speak, always drops IRQL to <DISPATCH_LEVEL ), does it mean spin_lock is akin to KeAcquireSpinLockAtDpcLevel?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我家小可爱 2024-12-16 17:12:51

当您知道没有中断或下半部分会争夺锁时,可以使用原始的spin_lock。通过避免中断屏蔽,您可以降低中断延迟,同时仍然避免关键部分的互斥体开销足够短以进行旋转。

实际上,它们似乎主要由文件系统驱动程序等事物使用,用于锁定内部缓存结构,以及在持有锁时永远不需要阻塞 IO 的其他事物。由于后半部分和驱动程序中断永远不会直接接触 FS 驱动程序,因此无需屏蔽中断。

我怀疑 Windows 的等效项是 CRITICAL_SECTION,或者 NT 内核 API 的等效项;然而,与 NT 临界区不同,Linux 自旋锁在发生争用时不会回退到互斥体;他们只是不停地旋转。

是的,spin_unlock_bh 无条件恢复下半部分。您可以跟踪何时手动启用/禁用(因为您通常应该以与获取相反的顺序释放锁,这通常不是问题),或者只是诉诸 spin_lock_irqsave

The raw spin_lock can be used when you know no interrupts or bottom-halves will ever contend for the lock. By avoiding interrupt masking, you keep interrupt latency down, while still avoiding the overhead of a mutex for critical sections short enough to spin on.

In practice, they seem to be primarily used by things like filesystem drivers, for locking internal cache structures, and other things where there is never a need to block on IO when holding the lock. Since back-halves and driver interrupts never touch the FS driver directly, there's no need to mask interrupts.

I suspect the Windows equivalent would be a CRITICAL_SECTION, or whatever the NT kernel API equivalent is; however, unlike a NT critical section, Linux spinlocks do not fall back to a mutex when contended; they just keep spinning.

And, yes, spin_unlock_bh unconditionally restores bottom-halves. You can either keep track of when to enable/disable manually (since you should generally release locks in opposite order of acquisition this usually isn't a problem), or just resort to spin_lock_irqsave.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文