Spin_lock_bh is usually used in the process to prohibit the hijacking and soft interruption.
In spin_lock_bh (), local_bh_disable () is called to Disable software interruption of the current CPU. While the spin_unlock_bh () function calls local_bh_enable () to interrupt the software of the local CPU. When the software interruption is disabled, all soft interruptions of the local CPU will not be executed.
If a softirq shares data with the user context, there are two problems: first, the current user context may be interrupted by softirq; second, the critical section may enter another CPU. In this case, the spin_lock_bh () (include/Linux/spinlock. h) can be used. It will disable softirqs on that CPU and then get
Lock. Spin_unlock_bh () does the opposite. (For historical reasons, the suffix 'bh 'is the general name for the lower half, which is the old name of software interrupts. In fact, it is only appropriate to call spin_lock_softirq). You can also use spin_lock_irq () or spin_lock_irqsave () Here. This will not only disable softirqs, but also disable hardware interruption.
Tasklets and timer are actually softirq. From the locking point of view, tasklets and timers have the same status.
A hard interrupt usually communicates with a tasklet or softirq. This usually involves placing a task in a queue and then getting it from softirq. If a hardware interruption service routine shares data with a softirq, two considerations are required. First, the execution process of softirq may be interrupted by hardware; second, the critical section may be interrupted by hardware on another CPU. This is exactly where spin_lock_irq () comes in handy. It disables interruption on that CPU and obtains the lock. Spin_unlock_irq () does the opposite.
In a hardware interrupt service routine, you do not need to use spin_lock_irq () Because softirq cannot be executed during execution: it can use spin_lock (), which is faster. The only exception is that another hardware interruption service routine uses the same lock: spin_lock_irq () will disable that hardware interruption.