usb: check for signals in chaoskey read function
Subsystems (2)
Keith Packard
maintainerPATCH 1/1 usb: check for signals in chaoskey read function
Call signal_pending before reading a chunk of data from the device so that long read operations can be interrupted with a signal.
--- drivers/usb/misc/chaoskey.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/usb/misc/chaoskey.c +7 -0
@@ -394,6 +394,13 @@ static ssize_t chaoskey_read(struct file *file,if (result) goto bail;if (dev->valid == dev->used) { + +if (signal_pending (current)) { + result = -ERESTARTSYS ; +mutex_unlock (&dev->lock); + goto bail; + } + result =_chaoskey_fill (dev);if (result) {mutex_unlock (&dev->lock);
Signature · 1 line
2.7.0
Quoted 2 lines · Keith Packard
Call
signal_pendingbefore reading a chunk of data from the device so that long read operations can be interrupted with a signal.
Hi,
why is this needed? You are doing this right after a mutex_lock_interruptible().
Regards
Oliver
Keith Packard
maintainerQuoted 2 lines · Oliver Neukum
why is this needed? You are doing this right after a
mutex_lock_interruptible().
When the device isn't contended, this mutex will never block and so mutex_lock_interruptible will never check for a signal.
I had mistakenly assumed that usb_bulk_msg would abort when a signal was delivered, but it doesn't.
I could be convinced that the driver should be using a different path through the USB stack that would allow a signal to wake up while waiting for the URB to complete, but this patch at least avoids needing to wait for a huge read to finish. The other option would be to eliminate the loop reading multiple URBs from the device, but that would reduce the available bandwidth from the device pretty considerably.
Signature · 1 line
-keith
Quoted 8 lines · Keith Packard
I could be convinced that the driver should be using a different path through the USB stack that would allow a signal to wake up while waiting
for the URB to complete, but this patch at least avoids needing to wait
for a huge read to finish. The other option would be to eliminate the loop reading multiple URBs from the device, but that would reduce the available bandwidth from the device pretty considerably.
Do these do the job?
Regards
Oliver
From 73fa56840b4158b203479eabf9be1e60da5ca4d2 Mon Sep 17 00:00:00 2001
From: Oliver Neukum <[email protected]>
Date: Wed, 17 Feb 2016 10:51:56 +0100
Subject: [PATCH 1/2] chaoskey: introduce an URB for asynchronous reads
To get more bandwidth and clean handling of signals an URB is introduced. Also a cleanup of allocation and freeing resources.
--- drivers/usb/misc/chaoskey.c | 46 ++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-)
drivers/usb/misc/chaoskey.c +27 -19
@@ -80,7 +80,8 @@ struct chaoskey {struct mutex lock;struct mutex rng_lock;int open;/* open count */ -int present;/* device not disconnected */ +bool present;/* device not disconnected */ +bool reading;/* ongoing IO */ int size;/* size of buf */ int valid;/* bytes of buf read */ int used;/* bytes of buf consumed */
@@ -88,15 +89,19 @@ struct chaoskey {struct hwrng hwrng;/* Embedded struct for hwrng */ int hwrng_registered;/* registered with hwrng API */ wait_queue_head_t wait_q;/* for timeouts */ +struct urb *urb;/* for performing IO */ char *buf; };static void chaoskey_free (struct chaoskey *dev) { -usb_dbg (dev->interface,"free" ); -kfree (dev->name); -kfree (dev->buf); -kfree (dev); +if (dev) { +usb_dbg (dev->interface,"free" ); +usb_free_urb (dev->urb); +kfree (dev->name); +kfree (dev->buf); +kfree (dev); + } } static int chaoskey_probe(struct usb_interface *interface,
@@ -107,7 +112,7 @@ static int chaoskey_probe(struct usb_interface *interface,int i;int in_ep =-1 ;struct chaoskey *dev; -int result; +int result = -ENOMEM ;int size;usb_dbg (interface,"probe %s-%s" , udev->product, udev->serial);
@@ -142,14 +147,17 @@ static int chaoskey_probe(struct usb_interface *interface, dev =kzalloc (sizeof (struct chaoskey ),GFP_KERNEL );if (dev ==NULL ) -return -ENOMEM ; + goto out; dev->buf =kmalloc (size,GFP_KERNEL ); -if (dev->buf ==NULL ) { -kfree (dev); -return -ENOMEM ; - } +if (dev->buf ==NULL ) + goto out; + + dev->urb =usb_alloc_urb (GFP_KERNEL ,0 ); + +if (!dev->urb) + goto out;/* Construct a name using the product and serial values. Each * device needs a unique name for the hwrng code
@@ -158,11 +166,8 @@ static int chaoskey_probe(struct usb_interface *interface,if (udev->product && udev->serial) { dev->name =kmalloc (strlen (udev->product) +1 +strlen (udev->serial) +1 ,GFP_KERNEL ); -if (dev->name ==NULL ) { -kfree (dev->buf); -kfree (dev); -return -ENOMEM ; - } +if (dev->name ==NULL ) + goto out;strcpy (dev->name, udev->product);strcat (dev->name,"-" );
@@ -186,9 +191,7 @@ static int chaoskey_probe(struct usb_interface *interface, result =usb_register_dev (interface, &chaoskey_class);if (result) {usb_err (interface,"Unable to allocate minor number." ); -usb_set_intfdata (interface,NULL ); -chaoskey_free (dev); -return result; + goto out; } dev->hwrng.name = dev->name ? dev->name : chaoskey_driver.name;
@@ -215,6 +218,11 @@ static int chaoskey_probe(struct usb_interface *interface,usb_dbg (interface,"chaoskey probe success, size %d" , dev->size);return 0 ; + +out: +usb_set_intfdata (interface,NULL ); +chaoskey_free (dev); +return result; }static void chaoskey_disconnect (struct usb_interface *interface)
Signature · 1 line
2.1.4
From 075d334923bee2644bcb2cb59a1f4aee1369c800 Mon Sep 17 00:00:00 2001
From: Oliver Neukum <[email protected]>
Date: Wed, 17 Feb 2016 14:56:57 +0100
Subject: [PATCH 2/2] chaoskey: use an URB for requesting data
This allows handling signals.
--- drivers/usb/misc/chaoskey.c | 76 +++++++++++++++++++++++++++++++++------------ 1 file changed, 56 insertions(+), 20 deletions(-)
drivers/usb/misc/chaoskey.c +56 -20
128 lines not on this page · raw message
Signature · 1 line
2.1.4
Keith Packard
maintainerQuoted 11 lines · Oliver Neukum
Quoted 8 lines · Keith Packard
I could be convinced that the driver should be using a different path through the USB stack that would allow a signal to wake up while waiting
for the URB to complete, but this patch at least avoids needing to wait
for a huge read to finish. The other option would be to eliminate the loop reading multiple URBs from the device, but that would reduce the available bandwidth from the device pretty considerably.Do these do the job?
Yup, with a few minor fixes to pass the right arguments:
drivers/usb/misc/chaoskey.c +2 -2
@@ -156,14 +156,14 @@ static int chaoskey_probe(struct usb_interface *interface,if (dev->buf ==NULL ) goto out; - dev->urb =usb_alloc_urb (GFP_KERNEL ,0 ); + dev->urb =usb_alloc_urb (0 ,GFP_KERNEL );if (!dev->urb) goto out; usb_fill_bulk_urb(dev->urb, udev, - usb_rcvbulkpipe(udev, altsetting->endpoint[in_ep].desc.bEndpointAddress), + usb_rcvbulkpipe(udev, in_ep), dev->buf, size, chaos_read_callback,
The first patch also has the URB allocation, which should be in the second patch. I removed the comment about 'more bandwidth' as the driver is still synchronous and runs at the same speed as before.
Thanks very much for making this 'right', instead of just kludging it.
Here's a fixed sequence:
From 6676d98364b10db5e30816dd6f9305439ee8e658 Mon Sep 17 00:00:00 2001
From: Oliver Neukum <[email protected]>
Date: Wed, 17 Feb 2016 09:58:11 -0800
Subject: [PATCH 1/2] usb/misc/chaoskey: Cleanup probe failure paths
Shares the cleanup code between all probe failure paths, instead of having per-failure cleanup at each point in the function.
--- drivers/usb/misc/chaoskey.c | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-)
drivers/usb/misc/chaoskey.c +19 -18
@@ -93,10 +93,13 @@ struct chaoskey {static void chaoskey_free (struct chaoskey *dev) { -usb_dbg (dev->interface,"free" ); -kfree (dev->name); -kfree (dev->buf); -kfree (dev); +if (dev) { +usb_dbg (dev->interface,"free" ); +usb_free_urb (dev->urb); +kfree (dev->name); +kfree (dev->buf); +kfree (dev); + } } static int chaoskey_probe(struct usb_interface *interface,
@@ -107,7 +110,7 @@ static int chaoskey_probe(struct usb_interface *interface,int i;int in_ep =-1 ;struct chaoskey *dev; -int result; +int result = -ENOMEM ;int size;usb_dbg (interface,"probe %s-%s" , udev->product, udev->serial);
@@ -142,14 +145,12 @@ static int chaoskey_probe(struct usb_interface *interface, dev =kzalloc (sizeof (struct chaoskey ),GFP_KERNEL );if (dev ==NULL ) -return -ENOMEM ; + goto out; dev->buf =kmalloc (size,GFP_KERNEL ); -if (dev->buf ==NULL ) { -kfree (dev); -return -ENOMEM ; - } +if (dev->buf ==NULL ) + goto out;/* Construct a name using the product and serial values. Each * device needs a unique name for the hwrng code
@@ -158,11 +159,8 @@ static int chaoskey_probe(struct usb_interface *interface,if (udev->product && udev->serial) { dev->name =kmalloc (strlen (udev->product) +1 +strlen (udev->serial) +1 ,GFP_KERNEL ); -if (dev->name ==NULL ) { -kfree (dev->buf); -kfree (dev); -return -ENOMEM ; - } +if (dev->name ==NULL ) + goto out;strcpy (dev->name, udev->product);strcat (dev->name,"-" );
@@ -186,9 +184,7 @@ static int chaoskey_probe(struct usb_interface *interface, result =usb_register_dev (interface, &chaoskey_class);if (result) {usb_err (interface,"Unable to allocate minor number." ); -usb_set_intfdata (interface,NULL ); -chaoskey_free (dev); -return result; + goto out; } dev->hwrng.name = dev->name ? dev->name : chaoskey_driver.name;
@@ -215,6 +211,11 @@ static int chaoskey_probe(struct usb_interface *interface,usb_dbg (interface,"chaoskey probe success, size %d" , dev->size);return 0 ; + +out: +usb_set_intfdata (interface,NULL ); +chaoskey_free (dev); +return result; }static void chaoskey_disconnect (struct usb_interface *interface)
Signature · 1 line
2.7.0
From 01d91276c9dd8183ef524d45da3e0a3438fc133a Mon Sep 17 00:00:00 2001
From: Oliver Neukum <[email protected]>
Date: Wed, 17 Feb 2016 10:01:33 -0800
Subject: [PATCH 2/2] usb/misc/chaoskey: introduce an URB for asynchronous
reads
To allow for and clean handling of signals an URB is introduced.
--- drivers/usb/misc/chaoskey.c | 85 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 64 insertions(+), 21 deletions(-)
drivers/usb/misc/chaoskey.c +64 -21
149 lines not on this page · raw message
Signature · 1 line
2.7.0
Signature · 2 lines
-- -keith
Keith Packard
maintainerQuoted 1 line · Keith Packard
Yup, with a few minor fixes to pass the right arguments:
Argh. Just after hitting 'send', I noticed that I'd not moved the URB deallocation out of the way when merging the first patch in this series. That means the driver won't build with only the first patch applied, which is always annoying for other people bisecting through this.
Here's the patches again, this time build tested in the middle.
From d9ae8ca45c86e81a455bf19a4b4c580ec872c7df Mon Sep 17 00:00:00 2001
From: Oliver Neukum <[email protected]>
Date: Wed, 17 Feb 2016 09:58:11 -0800
Subject: [PATCH 1/2] usb/misc/chaoskey: Cleanup probe failure paths
Shares the cleanup code between all probe failure paths, instead of having per-failure cleanup at each point in the function.
--- drivers/usb/misc/chaoskey.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-)
drivers/usb/misc/chaoskey.c +18 -18
@@ -93,10 +93,12 @@ struct chaoskey {static void chaoskey_free (struct chaoskey *dev) { -usb_dbg (dev->interface,"free" ); -kfree (dev->name); -kfree (dev->buf); -kfree (dev); +if (dev) { +usb_dbg (dev->interface,"free" ); +kfree (dev->name); +kfree (dev->buf); +kfree (dev); + } } static int chaoskey_probe(struct usb_interface *interface,
@@ -107,7 +109,7 @@ static int chaoskey_probe(struct usb_interface *interface,int i;int in_ep =-1 ;struct chaoskey *dev; -int result; +int result = -ENOMEM ;int size;usb_dbg (interface,"probe %s-%s" , udev->product, udev->serial);
@@ -142,14 +144,12 @@ static int chaoskey_probe(struct usb_interface *interface, dev =kzalloc (sizeof (struct chaoskey ),GFP_KERNEL );if (dev ==NULL ) -return -ENOMEM ; + goto out; dev->buf =kmalloc (size,GFP_KERNEL ); -if (dev->buf ==NULL ) { -kfree (dev); -return -ENOMEM ; - } +if (dev->buf ==NULL ) + goto out;/* Construct a name using the product and serial values. Each * device needs a unique name for the hwrng code
@@ -158,11 +158,8 @@ static int chaoskey_probe(struct usb_interface *interface,if (udev->product && udev->serial) { dev->name =kmalloc (strlen (udev->product) +1 +strlen (udev->serial) +1 ,GFP_KERNEL ); -if (dev->name ==NULL ) { -kfree (dev->buf); -kfree (dev); -return -ENOMEM ; - } +if (dev->name ==NULL ) + goto out;strcpy (dev->name, udev->product);strcat (dev->name,"-" );
@@ -186,9 +183,7 @@ static int chaoskey_probe(struct usb_interface *interface, result =usb_register_dev (interface, &chaoskey_class);if (result) {usb_err (interface,"Unable to allocate minor number." ); -usb_set_intfdata (interface,NULL ); -chaoskey_free (dev); -return result; + goto out; } dev->hwrng.name = dev->name ? dev->name : chaoskey_driver.name;
@@ -215,6 +210,11 @@ static int chaoskey_probe(struct usb_interface *interface,usb_dbg (interface,"chaoskey probe success, size %d" , dev->size);return 0 ; + +out: +usb_set_intfdata (interface,NULL ); +chaoskey_free (dev); +return result; }static void chaoskey_disconnect (struct usb_interface *interface)
Signature · 1 line
2.7.0
From 7271de7c68a2cd288d2735d617f4e15b17f23c2a Mon Sep 17 00:00:00 2001
From: Oliver Neukum <[email protected]>
Date: Wed, 17 Feb 2016 10:01:33 -0800
Subject: [PATCH 2/2] usb/misc/chaoskey: introduce an URB for asynchronous
reads
To allow for and clean handling of signals an URB is introduced.
--- drivers/usb/misc/chaoskey.c | 86 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 65 insertions(+), 21 deletions(-)
drivers/usb/misc/chaoskey.c +65 -21
156 lines not on this page · raw message
Signature · 1 line
2.7.0
Signature · 2 lines
-- -keith