spin_lock and scheduler confusion

Dave Hylands dhylands at gmail.com
Fri Jan 7 00:28:40 EST 2011

Hi Viral,

On Wed, Jan 5, 2011 at 2:23 PM, Viral Mehta <Viral.Mehta at lntinfotech.com> wrote:
> Hi ,
> I need your help to solve below confusion.
> 1. I know that spin_lock_irqsave, disables premption and local_irq.
> 2. There is some below piece of kernel code running on Quad core Machine.
>     spin_lock_irqsave(&lock, flags);
>     some_large_critical_section //sleep of 5 minutes
>     //I know there should be as small as possible task in critical section
>     // treat this as an example only

Note that you can't sleep while you hold a spinlock. You're not
allowed to perform any type of blocking operations. If you're holding
the spinlock for any significant length of time, then you're using the
wrong design.

>     spin_lock_irqrestore();
> 3. One of the CPU core tries to execute this code and so acquires the lock.
> 4. Now, second core is also goes to execute same piece of code and so will
> keep spinning for lock
> Now, the question is
> Will EVER second CPU core get chance to execute another task ?

Not while it's holding the spinlock or waiting for the spinlock.

> Ever if timeslice is over for the current task ?

The time tick interrupt is what determines when the timeslice is over.
Since you have interrupts disabled, the timer interrupt can't happen.

> What if scheduler code is running on CPU core-3 and sees that
> timeslice for task running on CPU core-2 has expired ?

Each core only considers the timeslices for its own core.

> I guess timeslice expire case is not as same as preemption. Or may be I am
> terribly wrong.

You shouldn't be holding  a spinlock for periods of time approaching
the length of a timeslice. The timer interrupt is what determines the
end of a timeslice. No timer interrupt, no end of a timeslice.
Preemption is also triggered by the timer interrupt, or by releasing a
resource that a higher priority task is waiting for.

Dave Hylands

More information about the Kernelnewbies mailing list