Why disabling interrupts disables kernel preemption and how spin lock disables preemption

删除回忆录丶 提交于 2019-11-29 23:19:00
Sebastian Mountaniol

I am not a scheduler guru, but I would like to explain how I see it. Here are several things.

  1. preempt_disable() doesn't disable IRQ. It just increases a thread_info->preempt_count variable.
  2. 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.
  3. 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.

You can see what is preempt_disable(). Try this: 1. Get a spinlock. 2. Call schedule()

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.

Good luck.

In a test kernel module I wrote to monitor/profile a task, I've tried disabling interrupts by:

1 - Using local_irq_save()

2 - Using spin_lock_irqsave()

3 - Manually disable_irq() to all IRQs in /proc/interrupts

In all 3 cases I could still use the hrtimer to measure time even though IRQs were disabled (and a task I was monitoring got preempted as well).

I find this veeeeerrrryyyy strange... I personally was anticipating what Sebastian Mountaniol pointed out -> No interrupts - no clock. No clock - no timers...

Linux kernel 2.6.32 on a single core, single CPU... Can anyone have a better explanation ?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!