简体   繁体   English

linux 设备驱动文件操作:可能存在竞争条件?

[英]linux device driver file operations: it is possible to have race conditions?

Consider a linux device driver that implements the open(), read(), write(), close(), unlocked_ioctl() and, maybe, mmap().考虑一个实现 open()、read()、write()、close()、unlocked_ioctl() 和 mmap() 的 linux 设备驱动程序。

Now, imagine that multiple (or the same) processes open the same device (/dev/device) concurrently.现在,想象多个(或相同)进程同时打开同一个设备(/dev/device)。

Are those file operations guaranteed in any way to be atomic wrt to each other or should each of the open(), read(), write(), close() take a mutex so that one of their pairs doesn't preempt them while in the middle of changing, for example, a buffer data (through the same inode)?这些文件操作是否以任何方式保证相互之间是原子性的,还是应该每个 open()、read()、write()、close() 都采用互斥锁,以便它们的对中的一个不会抢占它们例如,在更改缓冲区数据的过程中(通过相同的 inode)?

It should not be necessary if the kernel guarantee their atomicity with respect to each other and if each operation finds and leave the buffer/hardware in a consistent state.如果内核保证它们彼此之间的原子性,并且每个操作都找到并保持缓冲区/硬件处于一致状态,则应该没有必要。

Please, redirect me towards some reference (if you know).请把我重定向到一些参考(如果你知道)。

Thank you.谢谢你。

edit: it is in one of the comments, but the best reference that I found is here:编辑:它在其中一条评论中,但我找到的最佳参考资料在这里:

http://www.makelinux.net/ldd3/chp-6-sect-6 http://www.makelinux.net/ldd3/chp-6-sect-6

It also show strategies to alleviate the problem, either by restricting to a single user, either by creating copies, either by forcing the user to wait etc.它还展示了缓解问题的策略,通过限制单个用户,或者通过创建副本,或者通过强制用户等待等。

Device driver code runs in the process which invoked the system calls.设备驱动程序代码运行在调用系统调用的进程中。 The kernel does not have an implicit "module lock" it locks before invoking module code.内核没有在调用模块代码之前锁定的隐式“模块锁”。 It is definitely possible for concurrent driver calls to happen when separate processes invoke system calls that end up in your driver code.当单独的进程调用最终出现在驱动程序代码中的系统调用时,并发驱动程序调用绝对有可能发生。

As you might expect, the kernel favors simplicity and performance over ease of implementation.正如您所料,内核更喜欢简单性和性能,而不是易于实现。 It is up to you to grab the necessary spinlocks and semaphores when accessing shared state.在访问共享状态时,由您来获取必要的自旋锁和信号量。

See Chapter 5 of Linux Device Drivers , which talks about concurrency and race conditions in great detail.请参阅Linux 设备驱动程序的第 5 章,其中详细讨论了并发和竞争条件。

Concurrency and Its Management并发及其管理

In a modern Linux system, there are numerous sources of concurrency and, therefore, possible race conditions.在现代 Linux 系统中,有许多并发源,因此可能存在竞争条件。 Multiple user-space processes are running, and they can access your code in surprising combinations of ways.多个用户空间进程正在运行,它们可以以惊人的方式组合访问您的代码。 SMP systems can be executing your code simultaneously on different processors. SMP 系统可以在不同的处理器上同时执行您的代码。 Kernel code is preemptible;内核代码是可抢占的; your driver's code can lose the processor at any time, and the process that replaces it could also be running in your driver.您的驱动程序代码随时可能丢失处理器,并且替换它的进程也可能在您的驱动程序中运行。 Device interrupts are asynchronous events that can cause concurrent execution of your code.设备中断是异步事件,可导致代码并发执行。 The kernel also provides various mechanisms for delayed code execution, such as workqueues, tasklets, and timers, which can cause your code to run at any time in ways unrelated to what the current process is doing.内核还提供了各种延迟代码执行机制,例如工作队列、小任务和计时器,它们可以使您的代码随时以与当前进程正在执行的操作无关的方式运行。 In the modern, hot-pluggable world, your device could simply disappear while you are in the middle of working with it.在现代的热插拔世界中,您的设备可能会在您使用它时消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM