linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ch341: Replace memory allocations with stack storage
@ 2020-02-21 19:23 Michael Hanselmann
  2020-02-22  9:50 ` Johan Hovold
  2020-02-23 10:02 ` Greg KH
  0 siblings, 2 replies; 4+ messages in thread
From: Michael Hanselmann @ 2020-02-21 19:23 UTC (permalink / raw)
  To: Johan Hovold; +Cc: linux-usb, Michael Hanselmann

Storing a memory pointer consumes 4 or 8 bytes, depending on the
architecture. The replaced buffers are 2 bytes, so this change not only
avoids the overhead of memory allocation, but it also saves a small
amount of stack storage.

Signed-off-by: Michael Hanselmann <public@hansmi.ch>
---
 drivers/usb/serial/ch341.c | 32 ++++++++------------------------
 1 file changed, 8 insertions(+), 24 deletions(-)

diff --git a/drivers/usb/serial/ch341.c b/drivers/usb/serial/ch341.c
index c5ecdcd51ffc..6875da6e746c 100644
--- a/drivers/usb/serial/ch341.c
+++ b/drivers/usb/serial/ch341.c
@@ -255,16 +255,11 @@ static int ch341_set_handshake(struct usb_device *dev, u8 control)
 
 static int ch341_get_status(struct usb_device *dev, struct ch341_private *priv)
 {
-	const unsigned int size = 2;
-	char *buffer;
+	char buffer[2];
 	int r;
 	unsigned long flags;
 
-	buffer = kmalloc(size, GFP_KERNEL);
-	if (!buffer)
-		return -ENOMEM;
-
-	r = ch341_control_in(dev, CH341_REQ_READ_REG, 0x0706, 0, buffer, size);
+	r = ch341_control_in(dev, CH341_REQ_READ_REG, 0x0706, 0, buffer, sizeof(buffer));
 	if (r < 0)
 		goto out;
 
@@ -272,7 +267,7 @@ static int ch341_get_status(struct usb_device *dev, struct ch341_private *priv)
 	priv->msr = (~(*buffer)) & CH341_BITS_MODEM_STAT;
 	spin_unlock_irqrestore(&priv->lock, flags);
 
-out:	kfree(buffer);
+out:
 	return r;
 }
 
@@ -280,16 +275,11 @@ out:	kfree(buffer);
 
 static int ch341_configure(struct usb_device *dev, struct ch341_private *priv)
 {
-	const unsigned int size = 2;
-	char *buffer;
+	char buffer[2];
 	int r;
 
-	buffer = kmalloc(size, GFP_KERNEL);
-	if (!buffer)
-		return -ENOMEM;
-
 	/* expect two bytes 0x27 0x00 */
-	r = ch341_control_in(dev, CH341_REQ_READ_VERSION, 0, 0, buffer, size);
+	r = ch341_control_in(dev, CH341_REQ_READ_VERSION, 0, 0, buffer, sizeof(buffer));
 	if (r < 0)
 		goto out;
 	dev_dbg(&dev->dev, "Chip version: 0x%02x\n", buffer[0]);
@@ -304,7 +294,7 @@ static int ch341_configure(struct usb_device *dev, struct ch341_private *priv)
 
 	r = ch341_set_handshake(dev, priv->mcr);
 
-out:	kfree(buffer);
+out:
 	return r;
 }
 
@@ -486,18 +476,14 @@ static void ch341_break_ctl(struct tty_struct *tty, int break_state)
 	struct usb_serial_port *port = tty->driver_data;
 	int r;
 	uint16_t reg_contents;
-	uint8_t *break_reg;
-
-	break_reg = kmalloc(2, GFP_KERNEL);
-	if (!break_reg)
-		return;
+	uint8_t break_reg[2];
 
 	r = ch341_control_in(port->serial->dev, CH341_REQ_READ_REG,
 			ch341_break_reg, 0, break_reg, 2);
 	if (r < 0) {
 		dev_err(&port->dev, "%s - USB control read error (%d)\n",
 				__func__, r);
-		goto out;
+		return;
 	}
 	dev_dbg(&port->dev, "%s - initial ch341 break register contents - reg1: %x, reg2: %x\n",
 		__func__, break_reg[0], break_reg[1]);
@@ -518,8 +504,6 @@ static void ch341_break_ctl(struct tty_struct *tty, int break_state)
 	if (r < 0)
 		dev_err(&port->dev, "%s - USB control write error (%d)\n",
 				__func__, r);
-out:
-	kfree(break_reg);
 }
 
 static int ch341_tiocmset(struct tty_struct *tty,
-- 
2.20.1


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

* Re: [PATCH] ch341: Replace memory allocations with stack storage
  2020-02-21 19:23 [PATCH] ch341: Replace memory allocations with stack storage Michael Hanselmann
@ 2020-02-22  9:50 ` Johan Hovold
  2020-02-23 10:02 ` Greg KH
  1 sibling, 0 replies; 4+ messages in thread
From: Johan Hovold @ 2020-02-22  9:50 UTC (permalink / raw)
  To: Michael Hanselmann; +Cc: Johan Hovold, linux-usb

On Fri, Feb 21, 2020 at 07:23:41PM +0000, Michael Hanselmann wrote:
> Storing a memory pointer consumes 4 or 8 bytes, depending on the
> architecture. The replaced buffers are 2 bytes, so this change not only
> avoids the overhead of memory allocation, but it also saves a small
> amount of stack storage.
> 
> Signed-off-by: Michael Hanselmann <public@hansmi.ch>
> ---
>  drivers/usb/serial/ch341.c | 32 ++++++++------------------------
>  1 file changed, 8 insertions(+), 24 deletions(-)
> 
> diff --git a/drivers/usb/serial/ch341.c b/drivers/usb/serial/ch341.c
> index c5ecdcd51ffc..6875da6e746c 100644
> --- a/drivers/usb/serial/ch341.c
> +++ b/drivers/usb/serial/ch341.c
> @@ -255,16 +255,11 @@ static int ch341_set_handshake(struct usb_device *dev, u8 control)
>  
>  static int ch341_get_status(struct usb_device *dev, struct ch341_private *priv)
>  {
> -	const unsigned int size = 2;
> -	char *buffer;
> +	char buffer[2];
>  	int r;
>  	unsigned long flags;
>  
> -	buffer = kmalloc(size, GFP_KERNEL);
> -	if (!buffer)
> -		return -ENOMEM;
> -
> -	r = ch341_control_in(dev, CH341_REQ_READ_REG, 0x0706, 0, buffer, size);
> +	r = ch341_control_in(dev, CH341_REQ_READ_REG, 0x0706, 0, buffer, sizeof(buffer));

These buffers cannot be stack allocated as they need to be DMA-able on
all platforms.

Johan

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

* Re: [PATCH] ch341: Replace memory allocations with stack storage
  2020-02-21 19:23 [PATCH] ch341: Replace memory allocations with stack storage Michael Hanselmann
  2020-02-22  9:50 ` Johan Hovold
@ 2020-02-23 10:02 ` Greg KH
  2020-02-23 10:06   ` Michael Hanselmann
  1 sibling, 1 reply; 4+ messages in thread
From: Greg KH @ 2020-02-23 10:02 UTC (permalink / raw)
  To: Michael Hanselmann; +Cc: Johan Hovold, linux-usb

On Fri, Feb 21, 2020 at 07:23:41PM +0000, Michael Hanselmann wrote:
> Storing a memory pointer consumes 4 or 8 bytes, depending on the
> architecture. The replaced buffers are 2 bytes, so this change not only
> avoids the overhead of memory allocation, but it also saves a small
> amount of stack storage.
> 
> Signed-off-by: Michael Hanselmann <public@hansmi.ch>
> ---
>  drivers/usb/serial/ch341.c | 32 ++++++++------------------------
>  1 file changed, 8 insertions(+), 24 deletions(-)

You should have gotten a runtime error with this change if you tested
it.  Did that not happen somehow?

As Johan said, all USB data has to be dynamically allocated.

thanks,

greg k-h

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

* Re: [PATCH] ch341: Replace memory allocations with stack storage
  2020-02-23 10:02 ` Greg KH
@ 2020-02-23 10:06   ` Michael Hanselmann
  0 siblings, 0 replies; 4+ messages in thread
From: Michael Hanselmann @ 2020-02-23 10:06 UTC (permalink / raw)
  To: Greg KH; +Cc: Johan Hovold, linux-usb

On 23.02.20 11:02, Greg KH wrote:
> You should have gotten a runtime error with this change if you tested
> it.  Did that not happen somehow?
> 
> As Johan said, all USB data has to be dynamically allocated.
I just tried again and now I'm getting one: "transfer buffer is on
stack". Clearly something went wrong when I tried the change--in all
likelyhook my test environment was using an old build.

I'm sorry for not noticing/testing properly before sending.

Michael

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

end of thread, other threads:[~2020-02-23 10:06 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-02-21 19:23 [PATCH] ch341: Replace memory allocations with stack storage Michael Hanselmann
2020-02-22  9:50 ` Johan Hovold
2020-02-23 10:02 ` Greg KH
2020-02-23 10:06   ` Michael Hanselmann

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