There are three spin_lock functions in the kernel I am currently busy with.

  • spin_lock
  • spin_lock_irq
  • spin_lock_irqsave

I only find contributions covering only two of them (including Linux documentation).
Then answers or explanations are formulated ambigouos or contrary to each other or even contain comments saying the explanation is wrong. This makes it hard to get an overview.
Some basics are clear to me, as for example in interrupt context a simple spin_lock() can result in a deadlock. But I'd really appreciate a complete picture about this subject.

I need to understand:

  • When should or we use which version, when shouldn't we?
  • When is it not necessary to use a more safe version but doesn't hurt (except for performance)?
  • What is the reason to use a version in a particular situation?

1 Answer 1


A brief description is given in Chapter 5. Concurrency and Race Conditions of Linux Device Drivers, Third Edition

void spin_lock(spinlock_t *lock);
void spin_lock_irqsave(spinlock_t *lock, unsigned long flags);
void spin_lock_irq(spinlock_t *lock);

spin_lock_irqsave disables interrupts (on the local processor only) before taking the spinlock; the previous interrupt state is stored in flags. If you are absolutely sure nothing else might have already disabled interrupts on your processor (or, in other words, you are sure that you should enable interrupts when you release your spinlock), you can use spin_lock_irq instead and not have to keep track of the flags.

The spin_lock_irq* functions are important if you expect that the spinlock could be held in interrupt context. Reason being that if the spinlock is held by the local CPU and then the local CPU services an interrupt, which also attempts to lock the spinlock, then you have a deadlock.

  • Indeed this is the most complete description I found so far!
    – ThreadGuy
    May 16, 2019 at 4:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .