简体   繁体   English

在Linux内核中,spin_lock_irqsave()是否可以保护我免受信号处理程序,页面错误,对schedule()的调用的影响?

[英]In the Linux kernel, does spin_lock_irqsave() protect me from signal handlers, page faults, calls to schedule()?

I'm a bit confused as to what constitutes an interrupt in the Linux kernel. 我对Linux内核中的中断构成什么感到困惑。 From what I understand, spin_lock_irqsave() / spin_lock_irqrestore() ensure that my critical section won't be preempted by an interrupt handler. 据我了解,spin_lock_irqsave()/ spin_lock_irqrestore()确保我的关键部分不会被中断处理程序抢占。 Fair enough. 很公平。

But I'm a bit confused by what constitutes an interrupt handler. 但是我对中断处理程序的构成有些困惑。 First, I'm not 100% sure that I'm also protected from preemption from softirqs. 首先,我不是100%肯定我也可以免受softirqs的抢占。 Is this the case? 是这样吗

What about exceptions? 那例外呢? From what I understand, they're the same thing as interrupts, but not really. 据我了解,它们与中断是一回事,但实际上并非如此。 Am I protected from preemption by a page fault handler when I use spin_lock_irqsave() for instance? 例如,当我使用spin_lock_irqsave()时,是否可以防止页面错误处理程序抢占? What precisely happens when a page fault occurs, is the mechanism used to handle them the exact same thing as interrupts? 当发生页面错误时,究竟发生了什么,用于处理它们的机制与中断完全一样吗?

What about signal handlers? 信号处理程序呢? I don't get how those are implemented. 我不知道这些是如何实现的。 What if I send a signal a SIGTERM, SIGINT, or SIGKILL signal? 如果我发送信号SIGTERM,SIGINT或SIGKILL怎么办? Is this implemented with a soft IRQ? 这是通过软IRQ来实现的吗? something else? 还有什么吗 Can such a signal preempt my critical section when I use spin_lock_irqsave()? 当我使用spin_lock_irqsave()时,这样的信号可以抢占我的关键部分吗? Or not at all because those signals are handled exclusively in user space? 还是根本不因为这些信号是在用户空间中专门处理的?

What about calls to schedule()? 调用schedule()怎么样? Can those preempt me at any time, even in my critical sections when I use spin_lock_irqsave()? 即使在我使用spin_lock_irqsave()的关键部分中,这些指令也可以在任何时候抢占我吗?

I guess what I'd really like to know is what exactly can preempt me when I use spin_lock_irqsave(). 我想我真正想知道的是,当我使用spin_lock_irqsave()时,究竟有什么可以抢占我。 Nobody who uses the same lock as me, and no interrupt handlers, for sure. 当然,没有人使用与我相同的锁,也没有中断处理程序。 What about all these other things? 这些其他东西呢? What about softirqs/tasklets/work queues? 那么softirqs / tasklet /工作队列呢?

Finally, is spin_lock_irqsave() stronger than spin_lock_bh()? 最后,spin_lock_irqsave()是否比spin_lock_bh()更强? Ie, does spin_lock_irqsave() prevent preemption from bottom halves (ie, softirqs, tasklets, and work queues)? 即,spin_lock_irqsave()是否可以防止下半部分(例如softirqs,tasklet和工作队列)的抢占?

I've been Googling all of this quite a bit, but I find it hard to find clear answers. 我已经对所有这些进行了谷歌搜索,但是我发现很难找到明确的答案。

spin_lock_irqsave takes a lock and disables (temporarily masks) interrupts. spin_lock_irqsave进行锁定并禁用(临时屏蔽)中断。 You will not experience an interrupt until you release the lock (or otherwise re-enable interrupts -- which you shouldn't do). 在释放锁之前,您将不会遇到中断(或者重新启用中断,这是不应该的)。 Likewise, you will not and cannot be preempted since preemption requires an interrupt in order to cause a transfer of control. 同样,您不会也不会被抢占,因为抢占需要中断才能引起控制权的转移。

An interrupt is a signal from an external device indicating that it needs attention. 中断是来自外部设备的信号,表明需要引起注意。 The CPU hardware supports a mechanism allowing automatic transfer of control to an "interrupt handler" when the interrupt signal is asserted (but this can be temporarily masked which is what spin_lock_irqsave does). CPU硬件支持一种机制,当断言中断信号时,该机制允许将控制权自动转移到“中断处理程序”(但这可以被临时屏蔽,这是spin_lock_irqsave作用)。

Exceptions are similar to interrupts except they are caused by the program execution not by external hardware/devices. 异常与中断类似,除了中断是由程序执行而不是外部硬件/设备引起的。 So this includes divide-by-zero, illegal instructions, page faults. 因此,这包括被零除,非法指令,页面错误。 In all but the most unusual cases, your code should ensure there are no exceptions. 除了最不常见的情况外,您的代码应确保没有例外。 For example, you should not be causing a page fault when you hold a spin lock. 例如,当您按住自旋锁时,不应引起页面错误。 This will result in a kernel OOPS. 这将导致内核OOPS。

Signal handlers are primarily a user-mode concept. 信号处理程序主要是用户模式的概念。 Signals sent to a process are checked for explicitly by the kernel at times when it's appropriate to do so, and either cause an action directly (like process termination) or cause the next return to user mode in that process to vector to the user code signal handler. 内核会在适当时候适当地检查发送给进程的信号,然后直接引起动作(例如进程终止),或者使该进程中的下一次返回用户模式,以引导到用户代码信号处理程序。 You should not access any code that would do that while you hold the spin lock. 按住自旋锁时,不应访问任何会执行此操作的代码。

Likewise, you may not call schedule while you hold a spin lock, because... 同样,您在持有自旋锁的同时也可能不会拨打schedule ,因为...

Spin locks are intended to be held for very short periods of time to protect against simultaneous access to data structures that are shared across multiple processors. 自旋锁旨在保留很短的时间,以防止同时访问跨多个处理器共享的数据结构。 The reason interrupts are disabled is so that you can assure that the task holding the spin lock doesn't get preempted -- because if it did, other CPUs could be left spinning for a long time or even forever. 禁用中断的原因是为了确保拥有自旋锁的任务不会被抢占-因为如果这样做,其他CPU可能会长时间旋转甚至永久旋转。

... and schedule is specifically intended to allow you to give up control to another task. ...和schedule专门用于使您放弃对另一任务的控制。

See also this article. 另请参阅本文。 Specific details are probably outdated but all the concepts are still valid. 具体细节可能已过时,但所有概念仍然有效。 https://www.kernel.org/pub/linux/kernel/people/rusty/kernel-locking/ https://www.kernel.org/pub/linux/kernel/people/rusty/kernel-locking/

暂无
暂无

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

相关问题 spin_lock_irqsave中的flag参数保存哪些信息? - What information does flag argument in spin_lock_irqsave save? 中断上下文中的 spin_lock_irqsave() - spin_lock_irqsave() in interrupt context 调用 spin_lock_irqsave,而不是 local_irq_disable 后跟 spin_lock,对于每个处理器的结构是否相同? - Is calling spin_lock_irqsave, instead of local_irq_disable followed by spin_lock, the same for a per-prorcessor struct? 如何在Linux内核中找到信号处理程序定义? - How to find signal handlers definitions in Linux kernel? linux是否允许从信号处理程序进行任何系统调用? - Does linux allow any system call to be made from signal handlers? Linux如何优先考虑自定义信号处理程序? - How does Linux prioritize custom signal handlers? 当内核模块在不禁用中断的情况下持有 spin_lock 时,从用户空间发出 IOCTL 时是否会发生上下文切换? - Does context switching occurs when IOCTL is issued from user space while kernel module is holding a spin_lock without disabling interrupts? 为什么Linux内核中没有wait_event_ ... _irqsave()函数或宏? - Why is there no wait_event_…_irqsave() function or macro in the Linux kernel? 在 Linux 上调试信号处理程序 - Debugging signal handlers on Linux Linux 内核:自旋锁 SMP:为什么在 spin_lock_irq SMP 版本中有一个 preempt_disable()? - Linux Kernel: Spinlock SMP: Why there is a preempt_disable() in spin_lock_irq SMP version?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM