按住自旋锁返回是否自动不安全? [英] Is returning while holding a spinlock automatically unsafe?

查看:107
本文介绍了按住自旋锁返回是否自动不安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的书

The venerated book Linux Driver Development says that

传递给spin_unlock_irqrestoreflags参数必须与传递给spin_lock_irqsave的变量相同. 您还必须在同一函数中调用spin_lock_irqsavespin_unlock_irqrestore;否则您的代码可能会在某些架构上中断.

The flags argument passed to spin_unlock_irqrestore must be the same variable passed to spin_lock_irqsave. You must also call spin_lock_irqsave and spin_unlock_irqrestore in the same function; otherwise your code may break on some architectures.

但是,我找不到与捆绑在一起的官方文档所要求的任何此类限制内核代码本身.而且我发现驱动程序代码违反了该指南.

Yet I can't find any such restriction required by the official documentation bundled with the kernel code itself. And I find driver code that violates this guidance.

显然,从单独的函数中调用spin_lock_irqsavespin_unlock_irqrestore并不是一个好主意,因为您应该最大程度地减少持有锁时的工作量(禁用中断,不少!).但是对内核进行的更改是否使我们得以谨慎地执行呢?是它从未真正违反API合同,还是仍然可以这样做?

Obviously it isn't a good idea to call spin_lock_irqsave and spin_unlock_irqrestore from separate functions, because you're supposed to minimize the work done while holding a lock (with interrupts disabled, no less!). But have changes to the kernel made it possible if done with care, was it never actually against the API contract, or is it still verboten to do so?

如果在某个时候取消了限制,那么它是否适用于版本3.10.17?

If the restriction has been removed at some point, did it apply to version 3.10.17?

推荐答案

除了这本书,我再也没有看到过这种限制.本书中所提供的信息可能已经过时,或者..完全是错误的.

I have never see that constraint aside from the book. Probably, given information in the book is just outdated, or .. simply wrong.

在当前内核中(至少从2.6.32开始,我开始使用它),实际的锁定是通过spin_lock_irqsave的许多嵌套调用来完成的(例如,参见

In the current kernel(and at least since 2.6.32, which I start to work with) actual locking is done through many level of nested calls from spin_lock_irqsave(see, e.g. __raw_spin_lock_irqsave, which is called in the middle). So different function's context for lock and unlock may hardly be a reason for misfunction.

这篇关于按住自旋锁返回是否自动不安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆