There are many Q&As about spinlocks, but it's still confusing to me. I think it's because the questions and answers assume different settings or not clearly explain the settings about if it's SMP or if it's preemptive kernel or not when they ask or answer (and some old info is mixed in too).
My first question is: (Q1) in SMP situation, is schedule()
run on every processor concurrently (I know the scheduling starts by jiffies timer interrupt)? I'll assume yes in my question below. I would appreciate it if someone could briefly explain it to me how processes move among processor cores during scheduling too.
I'm trying to understand how, why, when spin_lock/unlock_irqsave
is used. Here is my question.
Suppose there is a code which calls spin_lock_irqsave
, and the interrupt state (enable) was a 'disable' at the time of calling spin_lock_irqsave()
. Could this code be running in interrupt context? Probably not, because the ISR should not have kicked in in the first place if interrupt was disabled in the corresponding local processor. Therefore, the code calling spin_lock_irqsave
must be in process context. Ok, the interrupt had been disabled previously, but a process is trying to lock with spin_lock_irqsave
.
In what case could the interrupt have been disabled? I think there are two cases.
Case 1: a previous interrupt routine had been preempted by this process (which is calling this spin_lock_irqsave
). This is weird because ISR cannot be preempted. (Q2) By the way, in preemptive kernel, can ISR be preempted by a process? (Q3) I guess because the preempt_count()
is #define
d as (current_thread_info()->preempt_count)
, the preempt_disable
only works for process and not interrupt. Do interrupts also have the current thread info?
Case 2: a previous normal process had acquired the lock with spin_lock_irq
(or irqsave
). But this also is weird because before locking, spin_lock_irq
(or irqsave
) disables preemption and interrupt for the task telling the scheduler not to switch to other task after the scheduler timer interrupt. So this case cannot be true.
I know I have to look further about process scheduling for SMP and kernel preemption, and maybe I am misunderstanding something. Could somebody clear things up in my question? Thanks a lot for reading.