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

前端 未结 4 1890
梦谈多话
梦谈多话 2020-12-13 10:55

I am reading Linux Kernel Development recently, and I have a few questions related to disabling preemption.

  1. In the \"Interrupt Control\" section of

4条回答
  •  春和景丽
    2020-12-13 11:35

    1. preempt_disable() doesn't disable the interrupts. It however increments the count of preempt counter. Let's say you call preempt_disable() n times in your code path, preemption will only enable at the nth preempt_enable().
    2. disabling interrupts to prevent preemption : not a safe way. This will undoubtedly disable normal kernel preemption because scheduler_tick() won't be called on system tick (no interrupt handler invoked). However, if the program triggers the schedule function, preemption will occur if preempt_disable() was not invoked.
    3. In linux, raw_spin_lock() doesn't disable local interrupts which may lead to deadlock. For instance, if an interrupt handler is invoked which tries to lock already held spin lock, it won't be able to unless the process itself releases it which is not possible as interrupt return wouldn't occur. So, it's better to use raw_spin_lock_irq(), which disables interrupts.

提交回复
热议问题