2022-02-09 15:26:42

by stsp

[permalink] [raw]
Subject: Q: is spin_lock() good outside of irq handler?

Hi kernel developers!

Just a quick question.
Kernel doc says this:
https://www.kernel.org/doc/Documentation/locking/spinlocks.txt
---

IFF you know that the spinlocks are
never used in interrupt handlers, you can use the non-irq versions:

    spin_lock(&lock);
    ...
    spin_unlock(&lock);

(and the equivalent read-write versions too, of course). The spinlock will
guarantee the same kind of exclusive access, and it will be much faster.

---

But in this case the interrupt is possible
inside the locked section, which can make
that section much longer than expected,
and will make other CPU cores to spin longer
while waiting for that lock.

So my question is: is spin_lock() actually
preferred to spin_lock_irq() when it is known
that the inthandler doesn't get the same lock?
Or maybe I should still use spin_lock_irq()
just to make sure my critical section doesn't
get an interrupt, causing other cores to spin
longer?