[PATCH] usb: core: config.c: use buffer on stack rather then on heap

Previous thread: Re: Your AUFS donation will be doubled by Tomas M by Hans-Peter Jansen on Saturday, April 17, 2010 - 4:57 am. (1 message)

Next thread: [patch 2/5] rcu head introduce rcu head init on stack by Mathieu Desnoyers on Saturday, April 17, 2010 - 5:48 am. (1 message)
From: Michal Nazarewicz
Date: Saturday, April 17, 2010 - 5:38 am

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) ...
Previous thread: Re: Your AUFS donation will be doubled by Tomas M by Hans-Peter Jansen on Saturday, April 17, 2010 - 4:57 am. (1 message)

Next thread: [patch 2/5] rcu head introduce rcu head init on stack by Mathieu Desnoyers on Saturday, April 17, 2010 - 5:48 am. (1 message)