简体   繁体   English

在工作队列中使用spin_lock()vs down_interruptible()

[英]Use spin_lock() vs down_interruptible() in workqueue

I run into a situation that using spin_lock() inside a workqueue hangs the system in case the intervals between multiple interrupts are too short. 我遇到一种情况,如果多个中断之间的间隔太短,则在工作队列中使用spin_lock()会使系统挂起。 After changing spin_lock() to down_interruptible() the issue is gone for now. spin_lock()更改为down_interruptible()此问题down_interruptible()消失了。

However I saw several bottom halves implementation in kernel code that use spin_lock() instead of mutex/semaphore (for example, the irq-function in a request_threaded_irq() ). 但是,我看到了在内核代码中使用spin_lock()而不是互斥锁/信号量(例如, request_threaded_irq()的irq-function spin_lock()几个底层代码实现。 What would be the reason for that? 那是什么原因呢? My best guess is the mutex/sempahore might be overkill in this situation. 我最好的猜测是,在这种情况下,互斥体/ sempahore可能会过大。

It depends entirely on what data you are trying to protect with this lock and from where (which context) that data can be accessed (besides the workqueue). 这完全取决于您要使用此锁尝试保护的数据以及可以从何处(哪个上下文)访问数据(工作队列之外)。 If the lock and corresponding data can be also accessed from the atomic context (such as interrupt handlers), you should use appropriate locking . 如果还可以从原子上下文 (例如中断处理程序)访问锁和相应的数据,则应使用适当的lock。

Workqueue is about process context , thus it's allowed to go to sleep if necessary. 工作队列是关于流程上下文的 ,因此在必要时可以进入睡眠状态。 Also going to sleep while holding spin_lock would be a fatal error. 同样,在按住spin_lock同时进入睡眠状态也是一个致命错误。 So in workqueue you can use sleep-able functions but not while holding spin_lock . 因此,在工作队列中,可以使用可睡眠的函数,但不能在按住spin_lock

Semaphores are sleep-able locks like mutexes , so if your data and the corresponding lock will not be used from within atomic context , I see no reason to give up mutexes/semaphores. 信号量是像互斥锁一样可睡眠的 ,因此,如果您的数据和相应的锁不会在原子上下文中使用 ,我认为没有理由放弃互斥量/信号量。

More info: 更多信息:

Interrupts, Spin Locks, and Preemption 中断,自旋锁和抢占

Linux kernel interrupt handler mutex protection . Linux内核中断处理程序互斥保护

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

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