spin_lock and scheduler confusion

Alexandre Courbot gnurou at gmail.com
Thu Jan 6 22:51:07 EST 2011


Hi,

> 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
>     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 ?
> Ever if timeslice is over for the current task ?

As far as I understand (from quickly reading
include/linux/spinlock*.h), kernel preemption is disabled as long as a
spin lock is used (and in your case, as soon as spin_lock_irqsave is
entered). This means that a core that started acquiring a spinlock
will not execute another task not only until the lock is acquired, but
until the lock is released.

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

AFAIK (that is, not very far) the scheduler code only cares for the
current core and does not assign tasks to others. But even if it did,
it would not have any way to stop the second core until it releases
the spin lock. So in any case, both core 1 and core 2 will be blocked
until they release the lock.

Alex.



More information about the Kernelnewbies mailing list