Linux 用户空间的中断处理
在 Linux 中,有哪些选项可以在用户空间代码而不是内核空间中处理设备中断?
In Linux, what are the options for handling device interrupts in user space code rather than in kernel space?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
经验告诉我们,可以为几乎所有 PCI 适配器编写良好且稳定的用户空间驱动程序。它只需要一些复杂性和内核中的一个小型代理层。 UIO 是朝这个方向迈出的一步,但如果您想正确处理用户空间中的中断,那么 UIO 可能还不够,例如,如果设备不支持 UIO 所依赖的 PCI 规范的中断禁用位。
请注意,进程唤醒延迟为几微秒,因此如果您的实现需要非常低的延迟,那么用户空间可能会拖累它。
如果我要实现一个用户空间驱动程序,我会将内核 ISR 简化为“禁用、确认和唤醒用户空间”操作,在唤醒进程内处理中断,然后重新启用中断(当然,通过从用户空间进程写入映射的 PCI 内存)。
Experience tells it is possible to write good and stable user-space drivers for almost any PCI adapter. It just requires some sophistication and a small proxying layer in the kernel. UIO is a step in that direction, but If you want to correctly handle interrupts in user-space then UIO might not be enough, for example if the device doesn't support the PCI-spec's interrupt disable bit which UIO relies on.
Notice that process wakeup latencies are a few microsecs so if your implementation requires very low latency then user-space might be a drag on it.
If I were to implement a user-space driver, I would reduce the kernel ISR to just a "disable & ack & wakeup-userpace" operation, handle the interrupt inside the waked-up process, and then re-enable the interrupt (of course, by writing to mapped PCI memory from the userspace process).
有 用户空间 I/O 系统 ( UIO),但是处理仍然应该在内核空间中完成。 OTOH,如果您只需要注意中断,则不需要内核部分。
There is Userspace I/O system (UIO), but handling should still be done in kernelspace. OTOH, if you just need to notice the interrupt, you don't need the kernel part.
您可能想查看 第 10 章:中断处理,来自 Linux 设备驱动程序,第三版一书。
You may like to take a look at CHAPTER 10: Interrupt Handling from Linux Device Drivers, Third Edition book.
必须间接触发用户层代码。
内核ISR通过写文件/设置寄存器/信令来指示中断。用户空间应用程序对此进行轮询并继续执行适当的代码。
边缘情况:中断比预期多或少(超时/每个时间间隔中断太多)
Linux 文件抽象用于连接内核和用户空间。这是通过字符设备和 ioctl() 调用来执行的。有些人可能更喜欢 sysfs 条目来实现此目的。
这可能看起来很奇怪,因为事件触发的设备通知(中断)与“时间触发”轮询挂钩,但它实际上是异步阻塞(读取/选择)。无论如何,根据性能会出现一些问题。
所以中断不能在内核之外直接处理。
例如,共享内存可以位于用户空间中,并且可以映射一些 I/O 权限设置地址,因此 UI/O 可以工作,但不能用于直接中断处理。
我在 vfio 主题中只找到了一份“少数派报告”(http://lxr .free-electrons.com/source/Documentation/vfio.txt):
https://stackoverflow.com/a/21197797/5349798
类似问题:
在上下文中运行用户线程Linux中的中断
在linux中是否可以从任何用户空间程序注册中断处理程序?
Linux 内核:在用户空间调用回调函数来自内核空间
Linux 中断与轮询
Linux 用户空间 PCI 驱动程序
如何通知用户空间应用程序驱动程序已收到中断Linux?
Have to trigger userland code indirectly.
Kernel ISR indicates interrupt by writing file / setting register / signalling. User space application polls this and goes on with the appropriate code.
Edge cases: more or less interrupts than expected (time out / too many interrupts per time interval)
Linux file abstraction is used to connect kernel and user space. This is performed by character devices and
ioctl()
calls. Some may prefer sysfs entries for this purpose.This can look odd because event triggered device notifications (interrupts) are hooked with 'time triggered' polling, but it is actually asyncronous blocking (read/select). Anyway some questions are arising according to performance.
So interrupts cannot be directly handled outside the kernel.
E.g. shared memory can be in user space and with some I/O permission settings addresses can be mapped, so U-I/O works, but not for direct interrupt handling.
I have found only one 'minority report' in topic vfio (http://lxr.free-electrons.com/source/Documentation/vfio.txt):
https://stackoverflow.com/a/21197797/5349798
Similar questions:
Running user thread in context of an interrupt in linux
Is it possible in linux to register a interrupt handler from any user-space program?
Linux Kernel: invoke call back function in user space from kernel space
Linux Interrupt vs. Polling
Linux user space PCI driver
How do I inform a user space application that the driver has received an interrupt in linux?