public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] USB testing driver: convert dev->sem to mutex
@ 2007-12-04 16:55 Matthias Kaehlcke
  2007-12-04 19:09 ` Daniel Walker
  0 siblings, 1 reply; 3+ messages in thread
From: Matthias Kaehlcke @ 2007-12-04 16:55 UTC (permalink / raw)
  To: gregkh, linux-usb-users; +Cc: linux-kernel, Andrew Morton

USB testing driver: convert semaphore dev->sem to the mutex API

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

--

diff --git a/drivers/usb/misc/usbtest.c b/drivers/usb/misc/usbtest.c
index ea31621..57260ab 100644
--- a/drivers/usb/misc/usbtest.c
+++ b/drivers/usb/misc/usbtest.c
@@ -6,6 +6,7 @@
 #include <linux/module.h>
 #include <linux/moduleparam.h>
 #include <linux/scatterlist.h>
+#include <linux/mutex.h>
 
 #include <linux/usb.h>
 
@@ -64,7 +65,7 @@ struct usbtest_dev {
 	int			in_iso_pipe;
 	int			out_iso_pipe;
 	struct usb_endpoint_descriptor	*iso_in, *iso_out;
-	struct semaphore	sem;
+	struct mutex		lock;
 
 #define TBUF_SIZE	256
 	u8			*buf;
@@ -1558,11 +1559,11 @@ usbtest_ioctl (struct usb_interface *intf, unsigned int code, void *buf)
 			|| param->sglen < 0 || param->vary < 0)
 		return -EINVAL;
 
-	if (down_interruptible (&dev->sem))
+	if (mutex_lock_interruptible(&dev->lock))
 		return -ERESTARTSYS;
 
 	if (intf->dev.power.power_state.event != PM_EVENT_ON) {
-		up (&dev->sem);
+		mutex_unlock(&dev->lock);
 		return -EHOSTUNREACH;
 	}
 
@@ -1574,7 +1575,7 @@ usbtest_ioctl (struct usb_interface *intf, unsigned int code, void *buf)
 	    	int	res;
 
 		if (intf->altsetting->desc.bInterfaceNumber) {
-			up (&dev->sem);
+			mutex_unlock(&dev->lock);
 			return -ENODEV;
 		}
 		res = set_altsetting (dev, dev->info->alt);
@@ -1582,7 +1583,7 @@ usbtest_ioctl (struct usb_interface *intf, unsigned int code, void *buf)
 			dev_err (&intf->dev,
 					"set altsetting to %d failed, %d\n",
 					dev->info->alt, res);
-			up (&dev->sem);
+			mutex_unlock(&dev->lock);
 			return res;
 		}
 	}
@@ -1855,7 +1856,7 @@ usbtest_ioctl (struct usb_interface *intf, unsigned int code, void *buf)
 		param->duration.tv_usec += 1000 * 1000;
 		param->duration.tv_sec -= 1;
 	}
-	up (&dev->sem);
+	mutex_unlock(&dev->lock);
 	return retval;
 }
 
@@ -1905,7 +1906,7 @@ usbtest_probe (struct usb_interface *intf, const struct usb_device_id *id)
 		return -ENOMEM;
 	info = (struct usbtest_info *) id->driver_info;
 	dev->info = info;
-	init_MUTEX (&dev->sem);
+	mutex_init(&dev->lock);
 
 	dev->intf = intf;
 
@@ -1990,7 +1991,7 @@ static void usbtest_disconnect (struct usb_interface *intf)
 {
 	struct usbtest_dev	*dev = usb_get_intfdata (intf);
 
-	down (&dev->sem);
+	mutex_lock(&dev->lock);
 
 	usb_set_intfdata (intf, NULL);
 	dev_dbg (&intf->dev, "disconnect\n");

-- 
Matthias Kaehlcke
Linux Application Developer
Barcelona

    Tant qu'il y aura sur terre des hommes pour qui existe un concept
    d' 'honneur national', la menace d'une nouvelle guerre subsistera
                              (B. Traven)
                                                                 .''`.
    using free software / Debian GNU/Linux | http://debian.org  : :'  :
                                                                `. `'`
gpg --keyserver pgp.mit.edu --recv-keys 47D8E5D4                  `-

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

* Re: [PATCH] USB testing driver: convert dev->sem to mutex
  2007-12-04 16:55 [PATCH] USB testing driver: convert dev->sem to mutex Matthias Kaehlcke
@ 2007-12-04 19:09 ` Daniel Walker
  2007-12-04 22:31   ` Matthias Kaehlcke
  0 siblings, 1 reply; 3+ messages in thread
From: Daniel Walker @ 2007-12-04 19:09 UTC (permalink / raw)
  To: Matthias Kaehlcke
  Cc: gregkh, linux-usb-users, linux-kernel, Andrew Morton, mingo

On Tue, 2007-12-04 at 17:55 +0100, Matthias Kaehlcke wrote:
> @@ -1990,7 +1991,7 @@ static void usbtest_disconnect (struct
> usb_interface *intf)
>  {
>         struct usbtest_dev      *dev = usb_get_intfdata (intf);
>  
> -       down (&dev->sem);
> +       mutex_lock(&dev->lock);
>  
>         usb_set_intfdata (intf, NULL);
>         dev_dbg (&intf->dev, "disconnect\n"); 

This looks like a bit of an anomaly.. The code that isn't shown above is
the "kfree(dev)" .. So this isn't legal since it's freeing a locked
mutex. Here's a excerpt from the comment above mutex_lock(),

 * The mutex must later on be released by the same task that
 * acquired it. Recursive locking is not allowed. The task
 * may not exit without first unlocking the mutex. Also, kernel
 * memory where the mutex resides mutex must not be freed with
 * the mutex still locked. The mutex must first be initialized
 * (or statically defined) before it can be locked. memset()-ing
 * the mutex to 0 is not allowed.

Daniel


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

* Re: [PATCH] USB testing driver: convert dev->sem to mutex
  2007-12-04 19:09 ` Daniel Walker
@ 2007-12-04 22:31   ` Matthias Kaehlcke
  0 siblings, 0 replies; 3+ messages in thread
From: Matthias Kaehlcke @ 2007-12-04 22:31 UTC (permalink / raw)
  To: Daniel Walker; +Cc: gregkh, linux-usb-users, linux-kernel, Andrew Morton, mingo

El Tue, Dec 04, 2007 at 11:09:02AM -0800 Daniel Walker ha dit:

> On Tue, 2007-12-04 at 17:55 +0100, Matthias Kaehlcke wrote:
> > @@ -1990,7 +1991,7 @@ static void usbtest_disconnect (struct
> > usb_interface *intf)
> >  {
> >         struct usbtest_dev      *dev = usb_get_intfdata (intf);
> >  
> > -       down (&dev->sem);
> > +       mutex_lock(&dev->lock);
> >  
> >         usb_set_intfdata (intf, NULL);
> >         dev_dbg (&intf->dev, "disconnect\n"); 
> 
> This looks like a bit of an anomaly.. The code that isn't shown above is
> the "kfree(dev)" .. So this isn't legal since it's freeing a locked
> mutex. Here's a excerpt from the comment above mutex_lock(),
> 
>  * The mutex must later on be released by the same task that
>  * acquired it. Recursive locking is not allowed. The task
>  * may not exit without first unlocking the mutex. Also, kernel
>  * memory where the mutex resides mutex must not be freed with
>  * the mutex still locked. The mutex must first be initialized
>  * (or statically defined) before it can be locked. memset()-ing
>  * the mutex to 0 is not allowed.

you're absolutely right that freeing a locked mutex isn't a good idea

thanks for pointing it out!

-- 
Matthias Kaehlcke
Linux System Developer
Barcelona

                Don't walk behind me, I may not lead
             Don't walk in front of me, I may not follow
                Just walk beside me and be my friend
                          (Albert Camus)
                                                                 .''`.
    using free software / Debian GNU/Linux | http://debian.org  : :'  :
                                                                `. `'`
gpg --keyserver pgp.mit.edu --recv-keys 47D8E5D4                  `-

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

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

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-12-04 16:55 [PATCH] USB testing driver: convert dev->sem to mutex Matthias Kaehlcke
2007-12-04 19:09 ` Daniel Walker
2007-12-04 22:31   ` Matthias Kaehlcke

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