kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
* down_interruptible() return non-zero value once signal is received and trying down_interruptible again in loop return value is always non-zero
@ 2013-10-18 12:26 Mushtaq Khan
  2013-10-20  7:51 ` anish singh
  2013-10-20 15:32 ` Josh Cartwright
  0 siblings, 2 replies; 3+ messages in thread
From: Mushtaq Khan @ 2013-10-18 12:26 UTC (permalink / raw)
  To: kernelnewbies

Hi,

In kernel driver, using Semaphore to block on resources (Descriptors and
memory).
Semaphore is initialized in locking state using call "init_MTEX_LOCKED()",
when resources are not available process calls down_interruptible() and
blocks. Once resources are freed up() is called, and blocked process is
unblocked. up() is called from interrupt context which frees resources on
receiving ACK-Interrupt.
Following is the code snippet for blocking:
do {
    ret = down_interruptible(sem)
    if (ret == 0) {
        break;
    } else {
        printk("Semaphore is not acquired try again\n");
        continue;
   }
} while (1);

While loop is used to make sure down_interrptible() is called until process
acquires the semaphore. If process receives signal, process unblocks from
down_interrptible() without acquiring the semaphore.
Issue am seeing is once signal is received under the blocking state, again
trying to acquire semaphore using down_interruptible() return value is
always non-zero and process is looping in while loop.

Can someone explain this behavior, I wrote the code with expectation that
after signal is received again trying to acquire lock will either block the
process or process will get semaphore.
Please help me on this.

Thanks,
Mushtaq Khan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20131018/6c275846/attachment.html 

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

* down_interruptible() return non-zero value once signal is received and trying down_interruptible again in loop return value is always non-zero
  2013-10-18 12:26 down_interruptible() return non-zero value once signal is received and trying down_interruptible again in loop return value is always non-zero Mushtaq Khan
@ 2013-10-20  7:51 ` anish singh
  2013-10-20 15:32 ` Josh Cartwright
  1 sibling, 0 replies; 3+ messages in thread
From: anish singh @ 2013-10-20  7:51 UTC (permalink / raw)
  To: kernelnewbies

On Fri, Oct 18, 2013 at 5:26 AM, Mushtaq Khan <mushtaqkhan921@gmail.com> wrote:
> Hi,
>
> In kernel driver, using Semaphore to block on resources (Descriptors and
> memory).
> Semaphore is initialized in locking state using call "init_MTEX_LOCKED()",
> when resources are not available process calls down_interruptible() and
> blocks. Once resources are freed up() is called, and blocked process is
> unblocked. up() is called from interrupt context which frees resources on
> receiving ACK-Interrupt.
> Following is the code snippet for blocking:
> do {
>     ret = down_interruptible(sem)
>     if (ret == 0) {
>         break;
>     } else {
>         printk("Semaphore is not acquired try again\n");
>         continue;
>    }
> } while (1);
>
> While loop is used to make sure down_interrptible() is called until process
> acquires the semaphore. If process receives signal, process unblocks from
> down_interrptible() without acquiring the semaphore.
> Issue am seeing is once signal is received under the blocking state, again
> trying to acquire semaphore using down_interruptible() return value is
> always non-zero and process is looping in while loop.
Please post your full code here.
>
> Can someone explain this behavior, I wrote the code with expectation that
> after signal is received again trying to acquire lock will either block the
> process or process will get semaphore.
> Please help me on this.
>
> Thanks,
> Mushtaq Khan
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>

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

* down_interruptible() return non-zero value once signal is received and trying down_interruptible again in loop return value is always non-zero
  2013-10-18 12:26 down_interruptible() return non-zero value once signal is received and trying down_interruptible again in loop return value is always non-zero Mushtaq Khan
  2013-10-20  7:51 ` anish singh
@ 2013-10-20 15:32 ` Josh Cartwright
  1 sibling, 0 replies; 3+ messages in thread
From: Josh Cartwright @ 2013-10-20 15:32 UTC (permalink / raw)
  To: kernelnewbies

On Fri, Oct 18, 2013 at 05:56:41PM +0530, Mushtaq Khan wrote:
> Hi,
> 
> In kernel driver, using Semaphore to block on resources (Descriptors and
> memory).
> Semaphore is initialized in locking state using call "init_MTEX_LOCKED()",
> when resources are not available process calls down_interruptible() and
> blocks. Once resources are freed up() is called, and blocked process is
> unblocked. up() is called from interrupt context which frees resources on
> receiving ACK-Interrupt.
> Following is the code snippet for blocking:
> do {
>     ret = down_interruptible(sem)
>     if (ret == 0) {
>         break;
>     } else {
>         printk("Semaphore is not acquired try again\n");
>         continue;
>    }
> } while (1);
> 
> While loop is used to make sure down_interrptible() is called until process
> acquires the semaphore. If process receives signal, process unblocks from
> down_interrptible() without acquiring the semaphore.
> Issue am seeing is once signal is received under the blocking state, again
> trying to acquire semaphore using down_interruptible() return value is
> always non-zero and process is looping in while loop.

Yes.  This happens because there is a signal pending for the calling
thread.  If you follow the down_interruptible() code path, you see that
the first thing in the loop in __down_common (kernel/semaphore.c) is
that signal_pending_state() is called to see if there is a pending
signal, and if so, it immediately returns -EINTR.

Using down_interruptible() in the method above is ill-advised.  It's
expected that if you are interrupted, you will propagate that
'interrupted' condition to your caller (and eventually back out to
usermode).

   Josh

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

end of thread, other threads:[~2013-10-20 15:32 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-10-18 12:26 down_interruptible() return non-zero value once signal is received and trying down_interruptible again in loop return value is always non-zero Mushtaq Khan
2013-10-20  7:51 ` anish singh
2013-10-20 15:32 ` Josh Cartwright

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).