public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] use mutex instead of semaphore in RocketPort driver
@ 2007-04-24 17:49 Matthias Kaehlcke
  2007-04-24 17:53 ` Oliver Neukum
  0 siblings, 1 reply; 6+ messages in thread
From: Matthias Kaehlcke @ 2007-04-24 17:49 UTC (permalink / raw)
  To: linux-kernel

the RocketPort driver uses a semaphore as mutex. use the mutex API
instead of the (binary) semaphore

Signed-off-by: Matthias Kaehlcke <matthias.kaehlcke@gmail.com>

--

diff --git a/drivers/char/rocket.c b/drivers/char/rocket.c
index 76357c8..faa5dd5 100644
--- a/drivers/char/rocket.c
+++ b/drivers/char/rocket.c
@@ -702,7 +702,7 @@ static void init_r_port(int board, int aiop, int chan, struct pci_dev *pci_dev)
 		}
 	}
 	spin_lock_init(&info->slock);
-	sema_init(&info->write_sem, 1);
+	mutex_init(&info->write_mtx);
 	rp_table[line] = info;
 	if (pci_dev)
 		tty_register_device(rocket_driver, line, &pci_dev->dev);
@@ -1662,7 +1662,7 @@ static void rp_put_char(struct tty_struct *tty, unsigned char ch)
 		return;
 
 	/*  Grab the port write semaphore, locking out other processes that try to write to this port */
-	down(&info->write_sem);
+	mutex_lock(&info->write_mtx);
 
 #ifdef ROCKET_DEBUG_WRITE
 	printk(KERN_INFO "rp_put_char %c...", ch);
@@ -1684,7 +1684,7 @@ static void rp_put_char(struct tty_struct *tty, unsigned char ch)
 		info->xmit_fifo_room--;
 	}
 	spin_unlock_irqrestore(&info->slock, flags);
-	up(&info->write_sem);
+	mutex_unlock(&info->write_mtx);
 }
 
 /*
@@ -1706,7 +1706,7 @@ static int rp_write(struct tty_struct *tty,
 	if (count <= 0 || rocket_paranoia_check(info, "rp_write"))
 		return 0;
 
-	down_interruptible(&info->write_sem);
+	mutex_lock_interruptible(&info->write_mtx);
 
 #ifdef ROCKET_DEBUG_WRITE
 	printk(KERN_INFO "rp_write %d chars...", count);
@@ -1777,7 +1777,7 @@ end:
 		wake_up_interruptible(&tty->poll_wait);
 #endif
 	}
-	up(&info->write_sem);
+	mutex_unlock(&info->write_mtx);
 	return retval;
 }
 
diff --git a/drivers/char/rocket_int.h b/drivers/char/rocket_int.h
index 3a8bcc8..04bcf61 100644
--- a/drivers/char/rocket_int.h
+++ b/drivers/char/rocket_int.h
@@ -1171,7 +1171,7 @@ struct r_port {
 	struct wait_queue *close_wait;
 #endif
 	spinlock_t slock;
-	struct semaphore write_sem;
+	struct mutex write_mtx;
 };
 
 #define RPORT_MAGIC 0x525001


-- 
Matthias Kaehlcke
Linux Application Developer
Barcelona


   Usually when people are sad, they don't do anything. They just cry over
     their condition. But when they get angry, they bring about a change
                              (Malcolm X)
                                                                 .''`.
    using free software / Debian GNU/Linux | http://debian.org  : :'  :
                                                                `. `'`
gpg --keyserver pgp.mit.edu --recv-keys 47D8E5D4                  `-

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

* Re: [PATCH] use mutex instead of semaphore in RocketPort driver
  2007-04-24 17:49 Matthias Kaehlcke
@ 2007-04-24 17:53 ` Oliver Neukum
  2007-04-24 18:19   ` Matthias Kaehlcke
  0 siblings, 1 reply; 6+ messages in thread
From: Oliver Neukum @ 2007-04-24 17:53 UTC (permalink / raw)
  To: Matthias Kaehlcke; +Cc: linux-kernel

Am Dienstag, 24. April 2007 19:49 schrieb Matthias Kaehlcke:
> @@ -1706,7 +1706,7 @@ static int rp_write(struct tty_struct *tty,
>         if (count <= 0 || rocket_paranoia_check(info, "rp_write"))
>                 return 0;
>  
> -       down_interruptible(&info->write_sem);
> +       mutex_lock_interruptible(&info->write_mtx);

This is a bug. It is also present in the current code, but nevertheless
it is a bug. If you use an interruptible lock, you must be ready to deal
with interrupts, which are ignored by this code.

	Regards
		Oliver

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

* Re: [PATCH] use mutex instead of semaphore in RocketPort driver
  2007-04-24 17:53 ` Oliver Neukum
@ 2007-04-24 18:19   ` Matthias Kaehlcke
  0 siblings, 0 replies; 6+ messages in thread
From: Matthias Kaehlcke @ 2007-04-24 18:19 UTC (permalink / raw)
  To: Oliver Neukum; +Cc: linux-kernel

El Tue, Apr 24, 2007 at 07:53:04PM +0200 Oliver Neukum ha dit:

> Am Dienstag, 24. April 2007 19:49 schrieb Matthias Kaehlcke:
> > @@ -1706,7 +1706,7 @@ static int rp_write(struct tty_struct *tty,
> >         if (count <= 0 || rocket_paranoia_check(info, "rp_write"))
> >                 return 0;
> >  
> > -       down_interruptible(&info->write_sem);
> > +       mutex_lock_interruptible(&info->write_mtx);
> 
> This is a bug. It is also present in the current code, but nevertheless
> it is a bug. If you use an interruptible lock, you must be ready to deal
> with interrupts, which are ignored by this code.

i fear i don't have the experience/knowledge to fix this bug, thanks
for your remark. 

i'm a bit confused now about the interruptible locks, i thought using
them means that the process will be waked up when receiving a
signal. what role are playing interrupts when using interruptible locks?

-- 
Matthias Kaehlcke
Linux Application Developer
Barcelona

     La libertad es como la mañana. Hay quienes esperan dormidos a que
   llegue, pero hay quienes desvelan y caminan la noche para alcanzarla
                        (Subcomandante Marcos)
                                                                 .''`.
    using free software / Debian GNU/Linux | http://debian.org  : :'  :
                                                                `. `'`
gpg --keyserver pgp.mit.edu --recv-keys 47D8E5D4                  `-

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

* Re: [PATCH] use mutex instead of semaphore in RocketPort driver
       [not found]   ` <fa.ZWnvY4ypIcIOxuu79yJn6cBDr8M@ifi.uio.no>
@ 2007-04-24 23:53     ` Robert Hancock
  2007-04-25  5:06       ` Satyam Sharma
  0 siblings, 1 reply; 6+ messages in thread
From: Robert Hancock @ 2007-04-24 23:53 UTC (permalink / raw)
  To: Matthias Kaehlcke, Oliver Neukum, linux-kernel

Matthias Kaehlcke wrote:
> El Tue, Apr 24, 2007 at 07:53:04PM +0200 Oliver Neukum ha dit:
> 
>> Am Dienstag, 24. April 2007 19:49 schrieb Matthias Kaehlcke:
>>> @@ -1706,7 +1706,7 @@ static int rp_write(struct tty_struct *tty,
>>>         if (count <= 0 || rocket_paranoia_check(info, "rp_write"))
>>>                 return 0;
>>>  
>>> -       down_interruptible(&info->write_sem);
>>> +       mutex_lock_interruptible(&info->write_mtx);
>> This is a bug. It is also present in the current code, but nevertheless
>> it is a bug. If you use an interruptible lock, you must be ready to deal
>> with interrupts, which are ignored by this code.
> 
> i fear i don't have the experience/knowledge to fix this bug, thanks
> for your remark. 
> 
> i'm a bit confused now about the interruptible locks, i thought using
> them means that the process will be waked up when receiving a
> signal. what role are playing interrupts when using interruptible locks?

You are correct, interrupts aren't involved. However if the wait is 
interrupted by a signal, mutex_lock_interruptible will return a nonzero 
return code which needs to be checked for (and likely -ERESTARTSYS or 
-EINTR returned), otherwise the code will blindly continue as though it 
has locked the mutex even though it has not.

-- 
Robert Hancock      Saskatoon, SK, Canada
To email, remove "nospam" from hancockr@nospamshaw.ca
Home Page: http://www.roberthancock.com/


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

* Re: [PATCH] use mutex instead of semaphore in RocketPort driver
  2007-04-24 23:53     ` [PATCH] use mutex instead of semaphore in RocketPort driver Robert Hancock
@ 2007-04-25  5:06       ` Satyam Sharma
  2007-04-25 12:56         ` Matthias Kaehlcke
  0 siblings, 1 reply; 6+ messages in thread
From: Satyam Sharma @ 2007-04-25  5:06 UTC (permalink / raw)
  To: Robert Hancock; +Cc: Matthias Kaehlcke, Oliver Neukum, linux-kernel

Hi Matthias,

On 4/25/07, Robert Hancock <hancockr@shaw.ca> wrote:
> Matthias Kaehlcke wrote:
> > El Tue, Apr 24, 2007 at 07:53:04PM +0200 Oliver Neukum ha dit:
> >
> >> Am Dienstag, 24. April 2007 19:49 schrieb Matthias Kaehlcke:
> >>> @@ -1706,7 +1706,7 @@ static int rp_write(struct tty_struct *tty,
> >>>         if (count <= 0 || rocket_paranoia_check(info, "rp_write"))
> >>>                 return 0;
> >>>
> >>> -       down_interruptible(&info->write_sem);
> >>> +       mutex_lock_interruptible(&info->write_mtx);
> >> This is a bug. It is also present in the current code, but nevertheless
> >> it is a bug. If you use an interruptible lock, you must be ready to deal
> >> with interrupts, which are ignored by this code.
> > [...]
> > i'm a bit confused now about the interruptible locks, i thought using
> > them means that the process will be waked up when receiving a
> > signal. what role are playing interrupts when using interruptible locks?
>
> You are correct, interrupts aren't involved. However if the wait is
> interrupted by a signal, mutex_lock_interruptible will return a nonzero
> return code which needs to be checked for (and likely -ERESTARTSYS or
> -EINTR returned), otherwise the code will blindly continue as though it
> has locked the mutex even though it has not.

Think I'll elaborate Robert's explanation for your benefit :-) Unlike
mutex_lock() and down() that put the task to TASK_UNINTERRUPTIBLE
sleep if the lock can't be acquired immediately,
mutex_lock_interruptible() and down_interruptible() sleep in
TASK_INTERRUPTIBLE state. So the task _can_ be woken up (without even
acquiring the lock) by incoming signals. When that happens, we can't
just blindly go on ... so the return values of the _interruptible()
versions of the locking functions *must* be checked for success and if
not, the task should return with error.

Use -ERESTARTSYS if a previous intermediate caller checks this return
value and tries and restarts the whole operation. If no such previous
caller exists (and/or introducing it would involve a change in kernel
behaviour as seen from userspace), you can safely use -EINTR. The goal
is that userspace must not get to see -ERESTARTSYS.

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

* Re: [PATCH] use mutex instead of semaphore in RocketPort driver
  2007-04-25  5:06       ` Satyam Sharma
@ 2007-04-25 12:56         ` Matthias Kaehlcke
  0 siblings, 0 replies; 6+ messages in thread
From: Matthias Kaehlcke @ 2007-04-25 12:56 UTC (permalink / raw)
  To: Satyam Sharma; +Cc: Robert Hancock, Oliver Neukum, linux-kernel

El Wed, Apr 25, 2007 at 10:36:38AM +0530 Satyam Sharma ha dit:

> Hi Matthias,
> 
> On 4/25/07, Robert Hancock <hancockr@shaw.ca> wrote:
> >Matthias Kaehlcke wrote:
> >> El Tue, Apr 24, 2007 at 07:53:04PM +0200 Oliver Neukum ha dit:
> >>
> >>> Am Dienstag, 24. April 2007 19:49 schrieb Matthias Kaehlcke:
> >>>> @@ -1706,7 +1706,7 @@ static int rp_write(struct tty_struct *tty,
> >>>>         if (count <= 0 || rocket_paranoia_check(info, "rp_write"))
> >>>>                 return 0;
> >>>>
> >>>> -       down_interruptible(&info->write_sem);
> >>>> +       mutex_lock_interruptible(&info->write_mtx);
> >>> This is a bug. It is also present in the current code, but nevertheless
> >>> it is a bug. If you use an interruptible lock, you must be ready to deal
> >>> with interrupts, which are ignored by this code.
> >> [...]
> >> i'm a bit confused now about the interruptible locks, i thought using
> >> them means that the process will be waked up when receiving a
> >> signal. what role are playing interrupts when using interruptible locks?
> >
> >You are correct, interrupts aren't involved. However if the wait is
> >interrupted by a signal, mutex_lock_interruptible will return a nonzero
> >return code which needs to be checked for (and likely -ERESTARTSYS or
> >-EINTR returned), otherwise the code will blindly continue as though it
> >has locked the mutex even though it has not.
> 
> Think I'll elaborate Robert's explanation for your benefit :-) Unlike
> mutex_lock() and down() that put the task to TASK_UNINTERRUPTIBLE
> sleep if the lock can't be acquired immediately,
> mutex_lock_interruptible() and down_interruptible() sleep in
> TASK_INTERRUPTIBLE state. So the task _can_ be woken up (without even
> acquiring the lock) by incoming signals. When that happens, we can't
> just blindly go on ... so the return values of the _interruptible()
> versions of the locking functions *must* be checked for success and if
> not, the task should return with error.
> 
> Use -ERESTARTSYS if a previous intermediate caller checks this return
> value and tries and restarts the whole operation. If no such previous
> caller exists (and/or introducing it would involve a change in kernel
> behaviour as seen from userspace), you can safely use -EINTR. The goal
> is that userspace must not get to see -ERESTARTSYS.

thanks to both of you for your explications, i think i understand the
problem much better now

-- 
Matthias Kaehlcke
Linux Application Developer
Barcelona

      The assumption that what currently exists must necessarily
        exist is the acid that corrodes all visionary thinking
                                                                 .''`.
    using free software / Debian GNU/Linux | http://debian.org  : :'  :
                                                                `. `'`
gpg --keyserver pgp.mit.edu --recv-keys 47D8E5D4                  `-

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

end of thread, other threads:[~2007-04-25 12:53 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <fa.CH+7gvaumSAVtl/1QOSoYPsXewg@ifi.uio.no>
     [not found] ` <fa.NPDKv3GAx73J43g63faobz7IZVo@ifi.uio.no>
     [not found]   ` <fa.ZWnvY4ypIcIOxuu79yJn6cBDr8M@ifi.uio.no>
2007-04-24 23:53     ` [PATCH] use mutex instead of semaphore in RocketPort driver Robert Hancock
2007-04-25  5:06       ` Satyam Sharma
2007-04-25 12:56         ` Matthias Kaehlcke
2007-04-24 17:49 Matthias Kaehlcke
2007-04-24 17:53 ` Oliver Neukum
2007-04-24 18:19   ` Matthias Kaehlcke

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox