Why can't you sleep while holding spinlock?

前端 未结 8 1995
醉梦人生
醉梦人生 2020-12-08 12:17

In the linux kernel, why can\'t you sleep while holding a spinlock?

相关标签:
8条回答
  • 2020-12-08 12:20

    Another likely explanation is that, in a spinlock context pre-emption is disabled.

    0 讨论(0)
  • 2020-12-08 12:27

    total agree with Nan Wang. I guess most important concept is "preemption" & "scheduling" and how happen when spinlock is acquired. when spinlock is acquired, preemption is disabled(true or not, I don't know, but assume it is correct), it means timer interrupt can't preempt current spinlock holder, but current spinlock hold still call sleepable kernel functions & actively invoke scheduler & run "another task". if "another task" happened to want to acquire the same spinlock as the first spinlock holder, here is problem come: since preemption is already disabled by first spinlock holder, "another task" which is invoked by actively call of scheduler by first spinlock holder, can't be preempted out, so its spinning always take the cpu, this is why deadlock happen.

    0 讨论(0)
  • 2020-12-08 12:28

    I disagree with William's response (his example). He's mixing two different concepts: preemption and synchronization.

    An Interrupt Context could preempt a Process Context and thus if there a RESOURCE shared by the both, we need to use

    spin_lock_irqsave()
    

    to (1) disable the IRQ (2) acquire the lock. By step 1, we could disable interrupt preemption.

    I think this thread is much convincing. Sleep() means a thread/process yields the control of the CPU and CONTEXT SWITCH to another, without releasing the spinlock, that's why it's wrong.

    0 讨论(0)
  • 2020-12-08 12:29

    I think this mail has a clarity answer:

    A process cannot be preempted nor sleep while holding a spinlock due spinlocks behavior. If a process grabs a spinlock and goes to sleep before releasing it. A second process (or an interrupt handler) that to grab the spinlock will busy wait. On an uniprocessor machine the second process will lock the CPU not allowing the first process to wake up and release the spinlock so the second process can continue, it is basically a deadlock.

    0 讨论(0)
  • 2020-12-08 12:31

    Example: your driver is executing and has just taken out a lock that controls access to its device. While the lock is held, the device issues an interrupt, which causes your interrupt handler to run. The interrupt handler, before accessing the device, must also obtain the lock. Taking out a spinlock in an interrupt handler is a legitimate thing to do; that is one of the reasons that spinlock operations do not sleep. But what happens if the interrupt routine executes in the same processor as the code that took out the lock originally? While the interrupt handler is spinning, the noninterrupt code will not be able to run to release the lock. That processor will spin forever.

    Source: http://www.makelinux.net/ldd3/chp-5-sect-5.shtml

    0 讨论(0)
  • 2020-12-08 12:31

    Apart from what willtate has mentioned, assume that a process sleeps while holding a spilock. If the new process that is scheduled tries to acquire the same spinlock, it starts spinning for the lock to be available. Since the new process keeps spinning, it is not possible to schedule the first process and thus the lock is never released making the second process to spin for ever and we have a deadlock.

    0 讨论(0)
提交回复
热议问题