为什么禁用中断会禁用内核抢占以及自旋锁如何禁用抢占 [英] Why disabling interrupts disables kernel preemption and how spin lock disables preemption
问题描述
我最近正在阅读 Linux内核开发,并且我有一些有关禁用抢占的问题.
I am reading Linux Kernel Development recently, and I have a few questions related to disabling preemption.
-
在第7章的中断控制"部分中,内容为:
In the "Interrupt Control" section of chapter 7, it says:
此外,禁用中断也会禁用内核抢占.
Moreover, disabling interrupts also disables kernel preemption.
我还从书中读到,在以下情况下可能会发生内核抢占:
I also read from the book that kernel preemption can occur in the follow cases:
当中断处理程序退出时,返回到内核空间之前.
当内核代码再次被抢占时.
如果内核中的任务显式调用schedule()
如果其他内核中的任务阻塞(导致调用schedule())
When an interrupt handler exits, before returning to kernel-space.
When kernel code becomes preemptible again.
If a task in the kernel explicitly calls schedule()
If a task in ther kernel blocks (which results in a call to schedule())
但是我不能将禁用中断与这些情况联系起来.
But I can't relate disabling interrupts with these cases.
据我所知,自旋锁将使用 preempt_disable()函数禁用抢占.
As far as I know, a spinlock would disable preemption with the preempt_disable() function.
帖子"spin-locks"到底是什么? 说:
在单核计算机上,自旋锁只是一个禁用中断"或提高IRQL",可完全阻止线程调度.
On a single core machine a spinlock is simply a "disable interrupts" or "raise IRQL" which prevents thread scheduling completely.
preempt_disable()是否通过禁用中断来禁止抢占?
Does preempt_disable() disable preemption by disabling interrupts?
推荐答案
我不是调度专家,但是我想解释一下我的看法. 这是几件事.
I am not a scheduler guru, but I would like to explain how I see it. Here are several things.
- preempt_disable()不禁用IRQ .它只是增加了
thread_info->preempt_count
变量. - 禁用中断也将禁用抢占,因为调度程序在此之后不起作用-仅在单CPU计算机上起作用.在SMP上这还不够,因为当您关闭一个CPU上的中断时,另一个/其他CPU仍会/异步地执行某些操作.
- 大锁"(意味着-关闭所有CPU上的所有中断)正在极大地降低系统速度-这就是为什么不再使用它的原因.这也是preempt_disable()无法关闭IRQ的原因.
- preempt_disable() doesn't disable IRQ. It just increases a
thread_info->preempt_count
variable. - Disabling interrupts also disables preemption because scheduler isn't working after that - but only on a single-CPU machine. On the SMP it isn't enough because when you close the interrupts on one CPU the other / others still does / do something asynchronously.
- The Big Lock (means - closing all interrupts on all CPUs) is slowing the system down dramatically - so it is why it not anymore in use. This is also the reason why preempt_disable() doesn't close the IRQ.
您可以看到什么是preempt_disable().试试这个: 1.获得一个自旋锁. 2.致电schedule()
You can see what is preempt_disable(). Try this: 1. Get a spinlock. 2. Call schedule()
在dmesg中,您将看到类似"BUG:原子调度"的内容.当调度程序在原子(非抢先)上下文中检测到您的进程但自行调度时,就会发生这种情况.
In the dmesg you will see something like "BUG: scheduling while atomic". This happens when scheduler detects that your process in atomic (not preemptive) context but it schedules itself.
祝你好运.
这篇关于为什么禁用中断会禁用内核抢占以及自旋锁如何禁用抢占的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!