Re: why use spin_lock_{irq, irqsave}()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2010/3/30 अनुज <anuj01@xxxxxxxxx>:
> Hi list
>
> Please anybody elaborate on this point : When we take any lock in
> kernel e.g. spin_lock() then preemption is automatically disabled by
> kernel. Then why to use spin_lock_{irq, irqsave}()? I am assuming even
> interrupt handler cant preempt the kernel when preemption is disabled
> by kernel.
>

My understanding goes like this.....

when in a multi-CPU environment (if one CPU....spinlock dropped to
NOP), preemption/interrupt all can occur independently per-CPU.   when
u used spin_lock() (vs mutex_lock()), u already are telling the
kernel....go ahead and lock me in forever...till the resources is
available (if it is not available....then necess....never never swap
me out and replaced with a new process ....on this particular CPU.
which goes the reason for preemption disabling (effectively disabling
the timer interrupt from swapping out the thread really).   as for
spin_lock_irq(), it is to disable the interrupt...because if the
non-interrupt-context thread is trying to lock a resource, and then
got into an interrupt handler (before the resource is locked), which
also attempted to lock the same resource.....POTENTIALLY u can be
deadlocked.   but if u are sure your lock resource is not use in any
interrupt contenxt handler, then u don't have to use the irq version.
A good read:
http://lwn.net/Articles/267968/
http://lwn.net/Articles/97537/
http://lwn.net/Articles/14473/
and check out wiki for Operating System, which have good coverage for
preemption.
this is just touching the surface....there are other variation like
spin_lock*bh which is meant for interrupt handler.....and the
following article is describing a spin lock, which normally is not
preemptible....and how to make it preemptible.

http://lwn.net/Articles/271817/

(in my view....when u make spin lock preemptible.....and thus
eliminating the deadlock that goes with it ...u are effective
converting it to mutex locks...because the CPU is no longer spinning
and waiting for the resource to become available)

>
> Thanks,
>
> Regards
>
> --
> Anuj Aggarwal
>
> .''`.
> : :Ⓐ :   # apt-get install hakuna-matata
> `. `'`
>   `-
>



-- 
Regards,
Peter Teoh

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux