linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] usb: core: config.c: use buffer on stack rather then on heap
@ 2010-04-17 12:38 Michal Nazarewicz
  2010-04-17 14:42 ` Alan Stern
  0 siblings, 1 reply; 5+ messages in thread
From: Michal Nazarewicz @ 2010-04-17 12:38 UTC (permalink / raw)
  To: linux-usb; +Cc: Linux-kernel, Greg Kroah-Hartman

usb_get_configuration() uses a temporary buffer allocated on heap
to read USB configuration descriptor.  The buffer is just nine
bytes an so it is a waste to allocate it on heap where it can be
allocated on stack with the rest of local variables.  This
simplifies the code and minimises memory usage.

Signed-off-by: Michal Nazarewicz <mina86@mina86.com>
---
 drivers/usb/core/config.c |   16 ++++------------
 1 files changed, 4 insertions(+), 12 deletions(-)

diff --git a/drivers/usb/core/config.c b/drivers/usb/core/config.c
index 0d3af6a..78d3f87 100644
--- a/drivers/usb/core/config.c
+++ b/drivers/usb/core/config.c
@@ -722,13 +722,12 @@ int usb_get_configuration(struct usb_device *dev)
 	int ncfg = dev->descriptor.bNumConfigurations;
 	int result = 0;
 	unsigned int cfgno, length;
-	unsigned char *buffer;
 	unsigned char *bigbuffer;
-	struct usb_config_descriptor *desc;
+	struct usb_config_descriptor desc;
 
 	cfgno = 0;
 	if (dev->authorized == 0)	/* Not really an error */
-		goto out_not_authorized;
+		goto err;
 	result = -ENOMEM;
 	if (ncfg > USB_MAXCONFIG) {
 		dev_warn(ddev, "too many configurations: %d, "
@@ -751,17 +750,12 @@ int usb_get_configuration(struct usb_device *dev)
 	if (!dev->rawdescriptors)
 		goto err2;
 
-	buffer = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
-	if (!buffer)
-		goto err2;
-	desc = (struct usb_config_descriptor *)buffer;
-
 	result = 0;
 	for (; cfgno < ncfg; cfgno++) {
 		/* We grab just the first descriptor so we know how long
 		 * the whole configuration is */
 		result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
-		    buffer, USB_DT_CONFIG_SIZE);
+		    &desc, USB_DT_CONFIG_SIZE);
 		if (result < 0) {
 			dev_err(ddev, "unable to read config index %d "
 			    "descriptor/%s: %d\n", cfgno, "start", result);
@@ -775,7 +769,7 @@ int usb_get_configuration(struct usb_device *dev)
 			result = -EINVAL;
 			goto err;
 		}
-		length = max((int) le16_to_cpu(desc->wTotalLength),
+		length = max((int) le16_to_cpu(desc.wTotalLength),
 		    USB_DT_CONFIG_SIZE);
 
 		/* Now that we know the length, get the whole thing */
@@ -810,8 +804,6 @@ int usb_get_configuration(struct usb_device *dev)
 	result = 0;
 
 err:
-	kfree(buffer);
-out_not_authorized:
 	dev->descriptor.bNumConfigurations = cfgno;
 err2:
 	if (result == -ENOMEM)
-- 
Best regards,                                         _     _
 .o. | Liege of Serenly Enlightened Majesty of      o' \,=./ `o
 ..o | Computer Science,  Michal "mina86" Nazarewicz   (o o)
 ooo +--<mina86-tlen.pl>--<jid:mina86-jabber.org>--ooO--(_)--Ooo--

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

* Re: [PATCH] usb: core: config.c: use buffer on stack rather then on heap
  2010-04-17 12:38 [PATCH] usb: core: config.c: use buffer on stack rather then on heap Michal Nazarewicz
@ 2010-04-17 14:42 ` Alan Stern
  2010-04-17 15:12   ` [PATCH] usb: core: config.c: usb_get_configuration() simplified Michal Nazarewicz
  0 siblings, 1 reply; 5+ messages in thread
From: Alan Stern @ 2010-04-17 14:42 UTC (permalink / raw)
  To: Michal Nazarewicz; +Cc: linux-usb, Linux-kernel, Greg Kroah-Hartman

On Sat, 17 Apr 2010, Michal Nazarewicz wrote:

> usb_get_configuration() uses a temporary buffer allocated on heap
> to read USB configuration descriptor.  The buffer is just nine
> bytes an so it is a waste to allocate it on heap where it can be
> allocated on stack with the rest of local variables.  This
> simplifies the code and minimises memory usage.

This is completely wrong.  You are not allowed to do DMA to buffers on 
the stack; some architectures are not capable of handling it.

Alan Stern


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

* [PATCH] usb: core: config.c: usb_get_configuration() simplified
  2010-04-17 14:42 ` Alan Stern
@ 2010-04-17 15:12   ` Michal Nazarewicz
  2010-04-17 16:13     ` Alan Stern
  2010-04-29 23:21     ` patch usb-core-config.c-usb_get_configuration-simplified.patch added to gregkh-2.6 tree gregkh
  0 siblings, 2 replies; 5+ messages in thread
From: Michal Nazarewicz @ 2010-04-17 15:12 UTC (permalink / raw)
  To: Alan Stern; +Cc: linux-usb, Linux-kernel, Greg Kroah-Hartman

usb_gat_configuratio() used two pointers to point to the same
memory.  Code simplified, by removing one of them.

Signed-off-by: Michal Nazarewicz <mina86@mina86.com>
---
 drivers/usb/core/config.c |   10 ++++------
 1 files changed, 4 insertions(+), 6 deletions(-)

> On Sat, 17 Apr 2010, Michal Nazarewicz wrote:
>> usb_get_configuration() uses a temporary buffer allocated on heap
>> to read USB configuration descriptor.  The buffer is just nine
>> bytes an so it is a waste to allocate it on heap where it can be
>> allocated on stack with the rest of local variables.  This
>> simplifies the code and minimises memory usage.

Alan Stern <stern@rowland.harvard.edu> writes:
> This is completely wrong.  You are not allowed to do DMA to buffers on 
> the stack; some architectures are not capable of handling it.

That makes sense; I haven't considered this thinking that copying nine
bytes by CPU, rather then using DMA, is not a big issue.

Still, the change proposed by attached commit does not suffer from DMA
issue and still simplify the code.

diff --git a/drivers/usb/core/config.c b/drivers/usb/core/config.c
index 0d3af6a..4b23e7f 100644
--- a/drivers/usb/core/config.c
+++ b/drivers/usb/core/config.c
@@ -722,7 +722,6 @@ int usb_get_configuration(struct usb_device *dev)
 	int ncfg = dev->descriptor.bNumConfigurations;
 	int result = 0;
 	unsigned int cfgno, length;
-	unsigned char *buffer;
 	unsigned char *bigbuffer;
 	struct usb_config_descriptor *desc;
 
@@ -751,17 +750,16 @@ int usb_get_configuration(struct usb_device *dev)
 	if (!dev->rawdescriptors)
 		goto err2;
 
-	buffer = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
-	if (!buffer)
+	desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
+	if (!desc)
 		goto err2;
-	desc = (struct usb_config_descriptor *)buffer;
 
 	result = 0;
 	for (; cfgno < ncfg; cfgno++) {
 		/* We grab just the first descriptor so we know how long
 		 * the whole configuration is */
 		result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
-		    buffer, USB_DT_CONFIG_SIZE);
+		    desc, USB_DT_CONFIG_SIZE);
 		if (result < 0) {
 			dev_err(ddev, "unable to read config index %d "
 			    "descriptor/%s: %d\n", cfgno, "start", result);
@@ -810,7 +808,7 @@ int usb_get_configuration(struct usb_device *dev)
 	result = 0;
 
 err:
-	kfree(buffer);
+	kfree(desc);
 out_not_authorized:
 	dev->descriptor.bNumConfigurations = cfgno;
 err2:
-- 
Best regards,                                         _     _
 .o. | Liege of Serenly Enlightened Majesty of      o' \,=./ `o
 ..o | Computer Science,  Michal "mina86" Nazarewicz   (o o)
 ooo +--<mina86-tlen.pl>--<jid:mina86-jabber.org>--ooO--(_)--Ooo--

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

* Re: [PATCH] usb: core: config.c: usb_get_configuration() simplified
  2010-04-17 15:12   ` [PATCH] usb: core: config.c: usb_get_configuration() simplified Michal Nazarewicz
@ 2010-04-17 16:13     ` Alan Stern
  2010-04-29 23:21     ` patch usb-core-config.c-usb_get_configuration-simplified.patch added to gregkh-2.6 tree gregkh
  1 sibling, 0 replies; 5+ messages in thread
From: Alan Stern @ 2010-04-17 16:13 UTC (permalink / raw)
  To: Michal Nazarewicz; +Cc: linux-usb, Linux-kernel, Greg Kroah-Hartman

On Sat, 17 Apr 2010, Michal Nazarewicz wrote:

> usb_gat_configuratio() used two pointers to point to the same
> memory.  Code simplified, by removing one of them.
> 
> Signed-off-by: Michal Nazarewicz <mina86@mina86.com>
> ---
>  drivers/usb/core/config.c |   10 ++++------
>  1 files changed, 4 insertions(+), 6 deletions(-)
> 
> > On Sat, 17 Apr 2010, Michal Nazarewicz wrote:
> >> usb_get_configuration() uses a temporary buffer allocated on heap
> >> to read USB configuration descriptor.  The buffer is just nine
> >> bytes an so it is a waste to allocate it on heap where it can be
> >> allocated on stack with the rest of local variables.  This
> >> simplifies the code and minimises memory usage.
> 
> Alan Stern <stern@rowland.harvard.edu> writes:
> > This is completely wrong.  You are not allowed to do DMA to buffers on 
> > the stack; some architectures are not capable of handling it.
> 
> That makes sense; I haven't considered this thinking that copying nine
> bytes by CPU, rather then using DMA, is not a big issue.
> 
> Still, the change proposed by attached commit does not suffer from DMA
> issue and still simplify the code.

Yes, this is fine.

Alan Stern


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

* patch usb-core-config.c-usb_get_configuration-simplified.patch added to gregkh-2.6 tree
  2010-04-17 15:12   ` [PATCH] usb: core: config.c: usb_get_configuration() simplified Michal Nazarewicz
  2010-04-17 16:13     ` Alan Stern
@ 2010-04-29 23:21     ` gregkh
  1 sibling, 0 replies; 5+ messages in thread
From: gregkh @ 2010-04-29 23:21 UTC (permalink / raw)
  To: mina86, gregkh, Linux-kernel, stern


This is a note to let you know that I've just added the patch titled

    Subject: USB: core: config.c: usb_get_configuration() simplified

to my gregkh-2.6 tree.  Its filename is

    usb-core-config.c-usb_get_configuration-simplified.patch

This tree can be found at 
    http://www.kernel.org/pub/linux/kernel/people/gregkh/gregkh-2.6/patches/


>From mina86@mina86.com  Thu Apr 29 16:05:40 2010
From: Michal Nazarewicz <mina86@mina86.com>
Date: Sat, 17 Apr 2010 17:12:58 +0200
Subject: USB: core: config.c: usb_get_configuration() simplified
To: Alan Stern <stern@rowland.harvard.edu>
Cc: linux-usb@vger.kernel.org, <Linux-kernel@vger.kernel.org>, Greg Kroah-Hartman <gregkh@suse.de>
Message-ID: <87sk6uaz9h.fsf@erwin.mina86.com>


usb_gat_configuratio() used two pointers to point to the same
memory.  Code simplified, by removing one of them.

Signed-off-by: Michal Nazarewicz <mina86@mina86.com>
Cc: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

---
 drivers/usb/core/config.c |   10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

--- a/drivers/usb/core/config.c
+++ b/drivers/usb/core/config.c
@@ -735,7 +735,6 @@ int usb_get_configuration(struct usb_dev
 	int ncfg = dev->descriptor.bNumConfigurations;
 	int result = 0;
 	unsigned int cfgno, length;
-	unsigned char *buffer;
 	unsigned char *bigbuffer;
 	struct usb_config_descriptor *desc;
 
@@ -764,17 +763,16 @@ int usb_get_configuration(struct usb_dev
 	if (!dev->rawdescriptors)
 		goto err2;
 
-	buffer = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
-	if (!buffer)
+	desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
+	if (!desc)
 		goto err2;
-	desc = (struct usb_config_descriptor *)buffer;
 
 	result = 0;
 	for (; cfgno < ncfg; cfgno++) {
 		/* We grab just the first descriptor so we know how long
 		 * the whole configuration is */
 		result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
-		    buffer, USB_DT_CONFIG_SIZE);
+		    desc, USB_DT_CONFIG_SIZE);
 		if (result < 0) {
 			dev_err(ddev, "unable to read config index %d "
 			    "descriptor/%s: %d\n", cfgno, "start", result);
@@ -823,7 +821,7 @@ int usb_get_configuration(struct usb_dev
 	result = 0;
 
 err:
-	kfree(buffer);
+	kfree(desc);
 out_not_authorized:
 	dev->descriptor.bNumConfigurations = cfgno;
 err2:


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

end of thread, other threads:[~2010-04-30 16:59 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-04-17 12:38 [PATCH] usb: core: config.c: use buffer on stack rather then on heap Michal Nazarewicz
2010-04-17 14:42 ` Alan Stern
2010-04-17 15:12   ` [PATCH] usb: core: config.c: usb_get_configuration() simplified Michal Nazarewicz
2010-04-17 16:13     ` Alan Stern
2010-04-29 23:21     ` patch usb-core-config.c-usb_get_configuration-simplified.patch added to gregkh-2.6 tree gregkh

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).