3

I want to understand difference between spin_lock and spin_lock_bh.

As per https://www.kernel.org/doc/html/v4.15/kernel-hacking/locking.html#cheat-sheet-for-locking

if critical section is between softirq and userspace process
    we should use spin_lock_bh.
if between two softirq
    we should use spin_lock.

Anand
  • 119
  • 1
  • 11
  • 4
    `spin_lock_bh` prevents a softirq running on the same CPU as the caller while the lock is held. Since the userspace process can be preempted by a softirq, this prevents a deadlock from occurring if a softirq waits for a spin-lock that is already locked by the same CPU. – Ian Abbott May 14 '19 at 16:35
  • Can you please explain using code reference as how softirq are disabled on that core using spin_lock_bh. – Anand May 16 '19 at 08:34
  • 2
    It increases the "softirq" count which is part of the "preempt" count for the current CPU so that the task cannot be preempted by other tasks and so that `in_interrupt()` (defined in "include/linux/preempt.h") is true. After any hardware interrupt servicing by the CPU, the usual pending softirq processing invoked by `irq_exit()` (in "kernel/softirq.c") is suppressed due to `in_interrupt()` being true. – Ian Abbott May 16 '19 at 10:25
  • Ok. Thanks got it – Anand May 16 '19 at 14:37

0 Answers0