* [PATCH 10/11] Introduce single user mode
@ 2012-06-06 16:31 stefani
2012-06-06 16:31 ` [PATCH 11/11] Bump version number and add aditional author stefani
2012-06-07 17:48 ` [PATCH 10/11] Introduce single user mode Andi Kleen
0 siblings, 2 replies; 4+ messages in thread
From: stefani @ 2012-06-06 16:31 UTC (permalink / raw)
To: linux-kernel, gregkh, oneukum; +Cc: alan, linux-usb, Stefani Seibold
From: Stefani Seibold <stefani@seibold.net>
Most USB devices can only used in a single usage mode. This patch
prevents a reopening on an already opened device.
Signed-off-by: Stefani Seibold <stefani@seibold.net>
---
drivers/usb/usb-skeleton.c | 9 +++++++++
1 files changed, 9 insertions(+), 0 deletions(-)
diff --git a/drivers/usb/usb-skeleton.c b/drivers/usb/usb-skeleton.c
index 11cc97b..81d256f 100644
--- a/drivers/usb/usb-skeleton.c
+++ b/drivers/usb/usb-skeleton.c
@@ -61,6 +61,7 @@ struct usb_skel {
int errors; /* the last request tanked */
bool ongoing_read; /* a read is going on */
bool connected; /* connected flag */
+ bool in_use; /* in use flag */
spinlock_t err_lock; /* lock for errors */
struct kref kref;
struct mutex io_mutex; /* synchronize with disconnect */
@@ -104,6 +105,11 @@ static int skel_open(struct inode *inode, struct file *file)
goto exit;
}
+ if (dev->in_use) {
+ retval = -EBUSY;
+ goto exit;
+ }
+
retval = usb_autopm_get_interface(interface);
if (retval)
goto exit;
@@ -111,6 +117,7 @@ static int skel_open(struct inode *inode, struct file *file)
/* increment our usage count for the device */
kref_get(&dev->kref);
+ dev->in_use = true;
mutex_unlock(&sync_mutex);
/* save our object in the file's private structure */
@@ -131,6 +138,7 @@ static int skel_release(struct inode *inode, struct file *file)
if (dev->connected)
usb_autopm_put_interface(
usb_find_interface(&skel_driver, iminor(inode)));
+ dev->in_use = false;
/* decrement the count on our device */
kref_put(&dev->kref, skel_delete);
@@ -529,6 +537,7 @@ static int skel_probe(struct usb_interface *interface,
dev->udev = usb_get_dev(interface_to_usbdev(interface));
dev->connected = true;
+ dev->in_use = false;
/* set up the endpoint information */
/* use only the first bulk-in and bulk-out endpoints */
--
1.7.8.6
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH 11/11] Bump version number and add aditional author
2012-06-06 16:31 [PATCH 10/11] Introduce single user mode stefani
@ 2012-06-06 16:31 ` stefani
2012-06-07 17:48 ` [PATCH 10/11] Introduce single user mode Andi Kleen
1 sibling, 0 replies; 4+ messages in thread
From: stefani @ 2012-06-06 16:31 UTC (permalink / raw)
To: linux-kernel, gregkh, oneukum; +Cc: alan, linux-usb, Stefani Seibold
From: Stefani Seibold <stefani@seibold.net>
Signed-off-by: Stefani Seibold <stefani@seibold.net>
---
drivers/usb/usb-skeleton.c | 3 ++-
1 files changed, 2 insertions(+), 1 deletions(-)
diff --git a/drivers/usb/usb-skeleton.c b/drivers/usb/usb-skeleton.c
index 81d256f..c93841c 100644
--- a/drivers/usb/usb-skeleton.c
+++ b/drivers/usb/usb-skeleton.c
@@ -1,7 +1,8 @@
/*
- * USB Skeleton driver - 2.2
+ * USB Skeleton driver - 2.3
*
* Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
+ * fixes by Stefani Seibold (stefani@seibold.net)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
--
1.7.8.6
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH 10/11] Introduce single user mode
2012-06-06 16:31 [PATCH 10/11] Introduce single user mode stefani
2012-06-06 16:31 ` [PATCH 11/11] Bump version number and add aditional author stefani
@ 2012-06-07 17:48 ` Andi Kleen
1 sibling, 0 replies; 4+ messages in thread
From: Andi Kleen @ 2012-06-07 17:48 UTC (permalink / raw)
To: stefani; +Cc: linux-kernel, gregkh, oneukum, alan, linux-usb
stefani@seibold.net writes:
> }
>
> + if (dev->in_use) {
> + retval = -EBUSY;
> + goto exit;
> + }
This plus setting the flag has to be inside the mutex, otherwise you
have an obvious race.
-Andi
--
ak@linux.intel.com -- Speaking for myself only
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH] fix usb skeleton driver
@ 2012-06-06 16:27 stefani
2012-06-06 16:27 ` [PATCH 10/11] Introduce single user mode stefani
0 siblings, 1 reply; 4+ messages in thread
From: stefani @ 2012-06-06 16:27 UTC (permalink / raw)
To: linux-kernel, gregkh, oneukum; +Cc: alan, linux-usb, Stefani Seibold
From: Stefani Seibold <stefani@seibold.net>
This is a fix for the USB skeleton driver to bring it in shape.
- The usb_interface structure pointer will be no longer stored
- Every access to the USB will be handled trought the usb_interface pointer
- Add a new bool 'connected' for signaling a disconnect (== false)
- Handle a non blocking read without blocking
- Code cleanup
- Synchronize disconnect() handler with open() and release(), to fix races
- Introduced fsync
- Single user mode
- Eliminated dead code
- Save some bytes in the dev structure
Some word about the open()/release() races with disconnect() of an USB device
(which can happen any time):
- The return interface pointer from usb_find_interface() could be already owned
by an other driver and no more longer handle by the skeleton driver.
- Or the dev pointer return by usb_get_intfdata() could point to an already
release memory.
This races can not handled by a per device mutex. Only a driver global mutex
could do this job, since the kref_put() in the skel_disconnect() must be
protected, otherwise skel_open() could access an already released memory.
I know that this races are very small, but on heavy load or misdesigned or buggy
hardware this could lead in a OOPS or unpredictable behavior.
The patch is against linux 3.5.0 commit eea5b5510fc5545d15b69da8e485a7424ae388cf
Hope this helps
Signed-off-by: Stefani Seibold <stefani@seibold.net>
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 10/11] Introduce single user mode
2012-06-06 16:27 [PATCH] fix usb skeleton driver stefani
@ 2012-06-06 16:27 ` stefani
0 siblings, 0 replies; 4+ messages in thread
From: stefani @ 2012-06-06 16:27 UTC (permalink / raw)
To: linux-kernel, gregkh, oneukum; +Cc: alan, linux-usb, Stefani Seibold
From: Stefani Seibold <stefani@seibold.net>
Most USB devices can only used in a single usage mode. This patch
prevents a reopening on an already opened device.
Signed-off-by: Stefani Seibold <stefani@seibold.net>
---
drivers/usb/usb-skeleton.c | 9 +++++++++
1 files changed, 9 insertions(+), 0 deletions(-)
diff --git a/drivers/usb/usb-skeleton.c b/drivers/usb/usb-skeleton.c
index 11cc97b..81d256f 100644
--- a/drivers/usb/usb-skeleton.c
+++ b/drivers/usb/usb-skeleton.c
@@ -61,6 +61,7 @@ struct usb_skel {
int errors; /* the last request tanked */
bool ongoing_read; /* a read is going on */
bool connected; /* connected flag */
+ bool in_use; /* in use flag */
spinlock_t err_lock; /* lock for errors */
struct kref kref;
struct mutex io_mutex; /* synchronize with disconnect */
@@ -104,6 +105,11 @@ static int skel_open(struct inode *inode, struct file *file)
goto exit;
}
+ if (dev->in_use) {
+ retval = -EBUSY;
+ goto exit;
+ }
+
retval = usb_autopm_get_interface(interface);
if (retval)
goto exit;
@@ -111,6 +117,7 @@ static int skel_open(struct inode *inode, struct file *file)
/* increment our usage count for the device */
kref_get(&dev->kref);
+ dev->in_use = true;
mutex_unlock(&sync_mutex);
/* save our object in the file's private structure */
@@ -131,6 +138,7 @@ static int skel_release(struct inode *inode, struct file *file)
if (dev->connected)
usb_autopm_put_interface(
usb_find_interface(&skel_driver, iminor(inode)));
+ dev->in_use = false;
/* decrement the count on our device */
kref_put(&dev->kref, skel_delete);
@@ -529,6 +537,7 @@ static int skel_probe(struct usb_interface *interface,
dev->udev = usb_get_dev(interface_to_usbdev(interface));
dev->connected = true;
+ dev->in_use = false;
/* set up the endpoint information */
/* use only the first bulk-in and bulk-out endpoints */
--
1.7.8.6
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2012-06-07 17:48 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-06-06 16:31 [PATCH 10/11] Introduce single user mode stefani
2012-06-06 16:31 ` [PATCH 11/11] Bump version number and add aditional author stefani
2012-06-07 17:48 ` [PATCH 10/11] Introduce single user mode Andi Kleen
-- strict thread matches above, loose matches on Subject: below --
2012-06-06 16:27 [PATCH] fix usb skeleton driver stefani
2012-06-06 16:27 ` [PATCH 10/11] Introduce single user mode stefani
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome