netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Madge Ambassador ATM Adapter driver: Always release_firmware() in ucode_init() and don't leak memory.
@ 2011-01-06 21:06 Jesper Juhl
  2011-01-07 15:02 ` chas williams - CONTRACTOR
  0 siblings, 1 reply; 4+ messages in thread
From: Jesper Juhl @ 2011-01-06 21:06 UTC (permalink / raw)
  To: linux-atm-general; +Cc: Chas Williams, netdev, linux-kernel


Failure to call release_firmware() will result in memory leak in 
drivers/atm/ambassador.c::ucode_init().
This patch makes sure we always call release_firmware() when needed, thus 
removing the leak(s).

Signed-off-by: Jesper Juhl <jj@chaosbits.net>
---
 ambassador.c |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

  Compile tested only since I have no way to actually test this.

diff --git a/drivers/atm/ambassador.c b/drivers/atm/ambassador.c
index ffe9b65..ab56539 100644
--- a/drivers/atm/ambassador.c
+++ b/drivers/atm/ambassador.c
@@ -1927,7 +1927,7 @@ static int __devinit ucode_init (loader_block * lb, amb_dev * dev) {
   unsigned long start_address;
   const struct ihex_binrec *rec;
   int res;
-  
+
   res = request_ihex_firmware(&fw, "atmsar11.fw", &dev->pci_dev->dev);
   if (res) {
     PRINTK (KERN_ERR, "Cannot load microcode data");
@@ -1937,6 +1937,7 @@ static int __devinit ucode_init (loader_block * lb, amb_dev * dev) {
   /* First record contains just the start address */
   rec = (const struct ihex_binrec *)fw->data;
   if (be16_to_cpu(rec->len) != sizeof(__be32) || be32_to_cpu(rec->addr)) {
+    release_firmware(fw);
     PRINTK (KERN_ERR, "Bad microcode data (no start record)");
     return -EINVAL;
   }
@@ -1950,10 +1951,12 @@ static int __devinit ucode_init (loader_block * lb, amb_dev * dev) {
     PRINTD (DBG_LOAD, "starting region (%x, %u)", be32_to_cpu(rec->addr),
 	    be16_to_cpu(rec->len));
     if (be16_to_cpu(rec->len) > 4 * MAX_TRANSFER_DATA) {
+	    release_firmware(fw);
 	    PRINTK (KERN_ERR, "Bad microcode data (record too long)");
 	    return -EINVAL;
     }
     if (be16_to_cpu(rec->len) & 3) {
+	    release_firmware(fw);
 	    PRINTK (KERN_ERR, "Bad microcode data (odd number of bytes)");
 	    return -EINVAL;
     }



-- 
Jesper Juhl <jj@chaosbits.net>            http://www.chaosbits.net/
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please.

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

* Re: [PATCH] Madge Ambassador ATM Adapter driver: Always release_firmware() in ucode_init() and don't leak memory.
  2011-01-06 21:06 [PATCH] Madge Ambassador ATM Adapter driver: Always release_firmware() in ucode_init() and don't leak memory Jesper Juhl
@ 2011-01-07 15:02 ` chas williams - CONTRACTOR
  2011-01-09 21:32   ` [PATCH v2] " Jesper Juhl
  0 siblings, 1 reply; 4+ messages in thread
From: chas williams - CONTRACTOR @ 2011-01-07 15:02 UTC (permalink / raw)
  To: Jesper Juhl; +Cc: linux-atm-general, netdev, linux-kernel

instead of duplicating the same section again and again, could you
write something like:

	errmsg = "no start record";
	goto fail;

	...

	errmsg = "record to long"
	goto fail;

	.... whatever ...

	return 0;

fail:
	release_firmware(fw)
	PRINTK(KERN_ERR, "Bad microcode data (%s)\n", errmsg);
	return -EINVAL;
}

On Thu, 6 Jan 2011 22:06:37 +0100 (CET)
Jesper Juhl <jj@chaosbits.net> wrote:

> 
> Failure to call release_firmware() will result in memory leak in 
> drivers/atm/ambassador.c::ucode_init().
> This patch makes sure we always call release_firmware() when needed, thus 
> removing the leak(s).
> 
> Signed-off-by: Jesper Juhl <jj@chaosbits.net>
> ---
>  ambassador.c |    5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
>   Compile tested only since I have no way to actually test this.
> 
> diff --git a/drivers/atm/ambassador.c b/drivers/atm/ambassador.c
> index ffe9b65..ab56539 100644
> --- a/drivers/atm/ambassador.c
> +++ b/drivers/atm/ambassador.c
> @@ -1927,7 +1927,7 @@ static int __devinit ucode_init (loader_block * lb, amb_dev * dev) {
>    unsigned long start_address;no start record
>    const struct ihex_binrec *rec;
>    int res;
> -  
> +
>    res = request_ihex_firmware(&fw, "atmsar11.fw", &dev->pci_dev->dev);
>    if (res) {
>      PRINTK (KERN_ERR, "Cannot load microcode data");
> @@ -1937,6 +1937,7 @@ static int __devinit ucode_init (loader_block * lb, amb_dev * dev) {
>    /* First record contains just the start address */
>    rec = (const struct ihex_binrec *)fw->data;
>    if (be16_to_cpu(rec->len) != sizeof(__be32) || be32_to_cpu(rec->addr)) {
> +    release_firmware(fw);
>      PRINTK (KERN_ERR, "Bad microcode data (no start record)");
>      return -EINVAL;
>    }
> @@ -1950,10 +1951,12 @@ static int __devinit ucode_init (loader_block * lb, amb_dev * dev) {
>      PRINTD (DBG_LOAD, "starting region (%x, %u)", be32_to_cpu(rec->addr),
>  	    be16_to_cpu(rec->len));
>      if (be16_to_cpu(rec->len) > 4 * MAX_TRANSFER_DATA) {
> +	    release_firmware(fw);
>  	    PRINTK (KERN_ERR, "Bad microcode data (record too long)");
>  	    return -EINVAL;
>      }
>      if (be16_to_cpu(rec->len) & 3) {
> +	    release_firmware(fw);
>  	    PRINTK (KERN_ERR, "Bad microcode data (odd number of bytes)");
>  	    return -EINVAL;
>      }
> 
> 
> 

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

* [PATCH v2] Madge Ambassador ATM Adapter driver: Always release_firmware() in ucode_init() and don't leak memory.
  2011-01-07 15:02 ` chas williams - CONTRACTOR
@ 2011-01-09 21:32   ` Jesper Juhl
  2011-01-09 23:46     ` David Miller
  0 siblings, 1 reply; 4+ messages in thread
From: Jesper Juhl @ 2011-01-09 21:32 UTC (permalink / raw)
  To: chas williams - CONTRACTOR; +Cc: linux-atm-general, netdev, linux-kernel

On Fri, 7 Jan 2011, chas williams - CONTRACTOR wrote:

> instead of duplicating the same section again and again, could you
> write something like:
> 
> 	errmsg = "no start record";
> 	goto fail;
> 
> 	...
> 
> 	errmsg = "record to long"
> 	goto fail;
> 
> 	.... whatever ...
> 
> 	return 0;
> 
> fail:
> 	release_firmware(fw)
> 	PRINTK(KERN_ERR, "Bad microcode data (%s)\n", errmsg);
> 	return -EINVAL;
> }
> 

Sure, we can do that instead.

Failure to call release_firmware() will result in memory leak in
drivers/atm/ambassador.c::ucode_init().
This patch makes sure we always call release_firmware() when needed, 
thus removing the leak(s).

Yes, I know checkpatch complains about this patch, but it was either that 
or completely mess up the existing style, so I opted to use the existing 
style and live with the checkpatch related flak.


Signed-off-by: Jesper Juhl <jj@chaosbits.net>
---
 ambassador.c |   19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

  compile tested only.

diff --git a/drivers/atm/ambassador.c b/drivers/atm/ambassador.c
index ffe9b65..9f47e86 100644
--- a/drivers/atm/ambassador.c
+++ b/drivers/atm/ambassador.c
@@ -1926,8 +1926,9 @@ static int __devinit ucode_init (loader_block * lb, amb_dev * dev) {
   const struct firmware *fw;
   unsigned long start_address;
   const struct ihex_binrec *rec;
+  const char *errmsg = 0;
   int res;
-  
+
   res = request_ihex_firmware(&fw, "atmsar11.fw", &dev->pci_dev->dev);
   if (res) {
     PRINTK (KERN_ERR, "Cannot load microcode data");
@@ -1937,8 +1938,8 @@ static int __devinit ucode_init (loader_block * lb, amb_dev * dev) {
   /* First record contains just the start address */
   rec = (const struct ihex_binrec *)fw->data;
   if (be16_to_cpu(rec->len) != sizeof(__be32) || be32_to_cpu(rec->addr)) {
-    PRINTK (KERN_ERR, "Bad microcode data (no start record)");
-    return -EINVAL;
+    errmsg = "no start record";
+    goto fail;
   }
   start_address = be32_to_cpup((__be32 *)rec->data);
 
@@ -1950,12 +1951,12 @@ static int __devinit ucode_init (loader_block * lb, amb_dev * dev) {
     PRINTD (DBG_LOAD, "starting region (%x, %u)", be32_to_cpu(rec->addr),
 	    be16_to_cpu(rec->len));
     if (be16_to_cpu(rec->len) > 4 * MAX_TRANSFER_DATA) {
-	    PRINTK (KERN_ERR, "Bad microcode data (record too long)");
-	    return -EINVAL;
+	    errmsg = "record too long";
+	    goto fail;
     }
     if (be16_to_cpu(rec->len) & 3) {
-	    PRINTK (KERN_ERR, "Bad microcode data (odd number of bytes)");
-	    return -EINVAL;
+	    errmsg = "odd number of bytes";
+	    goto fail;
     }
     res = loader_write(lb, dev, rec);
     if (res)
@@ -1970,6 +1971,10 @@ static int __devinit ucode_init (loader_block * lb, amb_dev * dev) {
     res = loader_start(lb, dev, start_address);
 
   return res;
+fail:
+  release_firmware(fw);
+  PRINTK(KERN_ERR, "Bad microcode data (%s)", errmsg);
+  return -EINVAL;
 }
 
 /********** give adapter parameters **********/



-- 
Jesper Juhl <jj@chaosbits.net>            http://www.chaosbits.net/
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please.

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

* Re: [PATCH v2] Madge Ambassador ATM Adapter driver: Always release_firmware() in ucode_init() and don't leak memory.
  2011-01-09 21:32   ` [PATCH v2] " Jesper Juhl
@ 2011-01-09 23:46     ` David Miller
  0 siblings, 0 replies; 4+ messages in thread
From: David Miller @ 2011-01-09 23:46 UTC (permalink / raw)
  To: jj; +Cc: chas, linux-atm-general, netdev, linux-kernel

From: Jesper Juhl <jj@chaosbits.net>
Date: Sun, 9 Jan 2011 22:32:38 +0100 (CET)

> Failure to call release_firmware() will result in memory leak in
> drivers/atm/ambassador.c::ucode_init().
> This patch makes sure we always call release_firmware() when needed, 
> thus removing the leak(s).
> 
> Yes, I know checkpatch complains about this patch, but it was either that 
> or completely mess up the existing style, so I opted to use the existing 
> style and live with the checkpatch related flak.
> 
> 
> Signed-off-by: Jesper Juhl <jj@chaosbits.net>

Applied.

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

end of thread, other threads:[~2011-01-09 23:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-01-06 21:06 [PATCH] Madge Ambassador ATM Adapter driver: Always release_firmware() in ucode_init() and don't leak memory Jesper Juhl
2011-01-07 15:02 ` chas williams - CONTRACTOR
2011-01-09 21:32   ` [PATCH v2] " Jesper Juhl
2011-01-09 23:46     ` David Miller

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