All of lore.kernel.org
 help / color / mirror / Atom feed
* confusion about locking [ Pls help ]
@ 2015-04-11 14:12 Er Krishna
  2015-04-12  9:00 ` michi1 at michaelblizek.twilightparadox.com
  0 siblings, 1 reply; 2+ messages in thread
From: Er Krishna @ 2015-04-11 14:12 UTC (permalink / raw)
  To: kernelnewbies

Hi All,

Can somebody help me here to understand  following basic scenario about
locking. Sorry if I am wrong in putting my points in correct manner.

1. Can we take semaphore (binary semaphore or mutex) if the critical
section data is shared across two  different threads/processes which are
running on two different processors simultaneously/parallely ? I know
spin_lock() can handle this scenario, but I was trying to understand can
semaphore also handle this scenario, assuming there is no interrupt and
only process context is there ? I think yes.  Pls confirm me on this and if
not then why not ? If any pross and cons are involved pls tell that also.


2. If the critical section data is shared across process and interrupt (
consider this scenario on uniprocessor machine  and I have not taken
local_irq_save() or any spin_lock_irqsave )  and in the currently executing
process  critical section is protected by  preempt_disable()  and suddenly
an interrupt occurs which also wants to access same critical section data,
so what will happen ? Will in  this case Interrupt handler  acquire the
critical section data and corrupt the kernel or it will sleep and corrupt
the kernel or it will keep doing busy waiting ? I think interrupt handler
will keep doing busy waiting and machine will stuck, kind of deadlock. Pls
confirm me.


3. Does Preemption gets disable ( on local processor ) in case of binary
semaphore or mutex ? I think no. Pls confirm. Second,  Lets say critical
section is shared across process and interrupt and it is locked by
semaphore (binary semaphore or mutex) in process context on  uniprocessor
machine.  If Interrupt comes then what will happen ? Will it preempt the
process and acquire the critical section of data and corrupt it or
interrupt will go into sleep mode and machine can  crash/stuck. Pls explain.

Best Regards,
Krishna
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20150411/3bebcb92/attachment-0001.html 

^ permalink raw reply	[flat|nested] 2+ messages in thread

* confusion about locking [ Pls help ]
  2015-04-11 14:12 confusion about locking [ Pls help ] Er Krishna
@ 2015-04-12  9:00 ` michi1 at michaelblizek.twilightparadox.com
  0 siblings, 0 replies; 2+ messages in thread
From: michi1 at michaelblizek.twilightparadox.com @ 2015-04-12  9:00 UTC (permalink / raw)
  To: kernelnewbies

Hi!

On 19:42 Sat 11 Apr     , Er Krishna wrote:
> Hi All,
> 
> Can somebody help me here to understand  following basic scenario about
> locking. Sorry if I am wrong in putting my points in correct manner.
> 
> 1. Can we take semaphore (binary semaphore or mutex) if the critical
> section data is shared across two  different threads/processes which are
> running on two different processors simultaneously/parallely ? I know
> spin_lock() can handle this scenario, but I was trying to understand can
> semaphore also handle this scenario, assuming there is no interrupt and
> only process context is there ? I think yes.  Pls confirm me on this and if
> not then why not ? If any pross and cons are involved pls tell that also.

Please do not use semaphores for mutual exclusion. Mutexes ensures that only
one "thread" can enter a critical section at the same time.

> 2. If the critical section data is shared across process and interrupt (
> consider this scenario on uniprocessor machine  and I have not taken
> local_irq_save() or any spin_lock_irqsave )  and in the currently executing
> process  critical section is protected by  preempt_disable()  and suddenly
> an interrupt occurs which also wants to access same critical section data,
> so what will happen ? Will in  this case Interrupt handler  acquire the
> critical section data and corrupt the kernel or it will sleep and corrupt
> the kernel or it will keep doing busy waiting ? I think interrupt handler
> will keep doing busy waiting and machine will stuck, kind of deadlock. Pls
> confirm me.

The idea behind spinlocks is that the process obtaining the lock cannot be
interrupted. An interrupt on the same CPU will be delayed until the critical
section is finished. An interrupt on another CPU trying to obtain the same
spinlock will busywait. This should not take too long, because the task
executing it cannot be preempted ond should not be loo long either.

> 3. Does Preemption gets disable ( on local processor ) in case of binary
> semaphore or mutex ? I think no. Pls confirm.

Semophore: I am not sure, but I think preemption does get disabled.
Mutex: Preemption will not get disabled.

> Second,  Lets say critical
> section is shared across process and interrupt and it is locked by
> semaphore (binary semaphore or mutex) in process context on  uniprocessor
> machine.  If Interrupt comes then what will happen ? Will it preempt the
> process

Yes, it will.

> and acquire the critical section of data and corrupt it or
> interrupt will go into sleep mode and machine can  crash/stuck. Pls explain.

You cannot acquire a mutex in any context which cannot sleep (this includes
interrupts, softirq, tasklets, ...). Use spinlocks instead.

	-Michi
-- 
programing a layer 3+4 network protocol for mesh networks
see http://michaelblizek.twilightparadox.com

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-04-12  9:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-04-11 14:12 confusion about locking [ Pls help ] Er Krishna
2015-04-12  9:00 ` michi1 at michaelblizek.twilightparadox.com

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.