netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] wan: lmc: Switch to using managed resources
@ 2016-02-27 17:04 Amitoj Kaur Chawla
  2016-03-01 22:21 ` David Miller
  0 siblings, 1 reply; 4+ messages in thread
From: Amitoj Kaur Chawla @ 2016-02-27 17:04 UTC (permalink / raw)
  To: netdev, linux-kernel; +Cc: julia.lawall

Use managed resource functions devm_kzalloc and pcim_enable_device
to simplify error handling. Subsequently, remove unnecessary
kfree, pci_disable_device and pci_release_regions.

To be compatible with the change, various gotos are replaced with
direct returns and unneeded labels are dropped.

Also, `sc` was only being freed in the probe function and not the
remove function before the change. By using devm_kzalloc this patch
also fixes this memory leak.

Signed-off-by: Amitoj Kaur Chawla <amitoj1606@gmail.com>
---
I was not able to find anywhere that `sc` might be freed. However, 
if a free has been overlooked, there will be a double free, due to 
the free implicitly performed by devm_kzalloc.

 drivers/net/wan/lmc/lmc_main.c | 27 +++++++--------------------
 1 file changed, 7 insertions(+), 20 deletions(-)

diff --git a/drivers/net/wan/lmc/lmc_main.c b/drivers/net/wan/lmc/lmc_main.c
index 317bc79..bb33b24 100644
--- a/drivers/net/wan/lmc/lmc_main.c
+++ b/drivers/net/wan/lmc/lmc_main.c
@@ -826,7 +826,7 @@ static int lmc_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
 
 	/* lmc_trace(dev, "lmc_init_one in"); */
 
-	err = pci_enable_device(pdev);
+	err = pcim_enable_device(pdev);
 	if (err) {
 		printk(KERN_ERR "lmc: pci enable failed: %d\n", err);
 		return err;
@@ -835,23 +835,20 @@ static int lmc_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
 	err = pci_request_regions(pdev, "lmc");
 	if (err) {
 		printk(KERN_ERR "lmc: pci_request_region failed\n");
-		goto err_req_io;
+		return err;
 	}
 
 	/*
 	 * Allocate our own device structure
 	 */
-	sc = kzalloc(sizeof(lmc_softc_t), GFP_KERNEL);
-	if (!sc) {
-		err = -ENOMEM;
-		goto err_kzalloc;
-	}
+	sc = devm_kzalloc(&pdev->dev, sizeof(lmc_softc_t), GFP_KERNEL);
+	if (!sc)
+		return -ENOMEM;
 
 	dev = alloc_hdlcdev(sc);
 	if (!dev) {
 		printk(KERN_ERR "lmc:alloc_netdev for device failed\n");
-		err = -ENOMEM;
-		goto err_hdlcdev;
+		return -ENOMEM;
 	}
 
 
@@ -888,7 +885,7 @@ static int lmc_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
 	if (err) {
 		printk(KERN_ERR "%s: register_netdev failed.\n", dev->name);
 		free_netdev(dev);
-		goto err_hdlcdev;
+		return err;
 	}
 
     sc->lmc_cardtype = LMC_CARDTYPE_UNKNOWN;
@@ -971,14 +968,6 @@ static int lmc_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
 
     lmc_trace(dev, "lmc_init_one out");
     return 0;
-
-err_hdlcdev:
-	kfree(sc);
-err_kzalloc:
-	pci_release_regions(pdev);
-err_req_io:
-	pci_disable_device(pdev);
-	return err;
 }
 
 /*
@@ -992,8 +981,6 @@ static void lmc_remove_one(struct pci_dev *pdev)
 		printk(KERN_DEBUG "%s: removing...\n", dev->name);
 		unregister_hdlc_device(dev);
 		free_netdev(dev);
-		pci_release_regions(pdev);
-		pci_disable_device(pdev);
 	}
 }
 
-- 
1.9.1

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

* Re: [PATCH] wan: lmc: Switch to using managed resources
  2016-02-27 17:04 [PATCH] wan: lmc: Switch to using managed resources Amitoj Kaur Chawla
@ 2016-03-01 22:21 ` David Miller
  2016-03-02 14:22   ` Amitoj Kaur Chawla
  0 siblings, 1 reply; 4+ messages in thread
From: David Miller @ 2016-03-01 22:21 UTC (permalink / raw)
  To: amitoj1606; +Cc: netdev, linux-kernel, julia.lawall

From: Amitoj Kaur Chawla <amitoj1606@gmail.com>
Date: Sat, 27 Feb 2016 22:34:16 +0530

> @@ -835,23 +835,20 @@ static int lmc_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
>  	err = pci_request_regions(pdev, "lmc");
>  	if (err) {
>  		printk(KERN_ERR "lmc: pci_request_region failed\n");
> -		goto err_req_io;
> +		return err;
>  	}
>  
>  	/*
>  	 * Allocate our own device structure
>  	 */
> -	sc = kzalloc(sizeof(lmc_softc_t), GFP_KERNEL);
> -	if (!sc) {
> -		err = -ENOMEM;
> -		goto err_kzalloc;

You can't get rid of the error paths from here on out, because you still need to
release the PCI regions obtained from pci_request_regions() above.

To be quite honest, unless you are fixing real bugs, managed resource
converstions are more likely to add bugs than do anything truly
useful.

I strongly consider you just drop this change.

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

* Re: [PATCH] wan: lmc: Switch to using managed resources
  2016-03-01 22:21 ` David Miller
@ 2016-03-02 14:22   ` Amitoj Kaur Chawla
  2016-03-02 18:44     ` David Miller
  0 siblings, 1 reply; 4+ messages in thread
From: Amitoj Kaur Chawla @ 2016-03-02 14:22 UTC (permalink / raw)
  To: David Miller; +Cc: netdev, Linux-Kernel@Vger. Kernel. Org, Julia Lawall

On Wed, Mar 2, 2016 at 3:51 AM, David Miller <davem@davemloft.net> wrote:
> From: Amitoj Kaur Chawla <amitoj1606@gmail.com>
> Date: Sat, 27 Feb 2016 22:34:16 +0530
>
>> @@ -835,23 +835,20 @@ static int lmc_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
>>       err = pci_request_regions(pdev, "lmc");
>>       if (err) {
>>               printk(KERN_ERR "lmc: pci_request_region failed\n");
>> -             goto err_req_io;
>> +             return err;
>>       }
>>
>>       /*
>>        * Allocate our own device structure
>>        */
>> -     sc = kzalloc(sizeof(lmc_softc_t), GFP_KERNEL);
>> -     if (!sc) {
>> -             err = -ENOMEM;
>> -             goto err_kzalloc;
>
> You can't get rid of the error paths from here on out, because you still need to
> release the PCI regions obtained from pci_request_regions() above.
>
> To be quite honest, unless you are fixing real bugs, managed resource
> converstions are more likely to add bugs than do anything truly
> useful.
>
> I strongly consider you just drop this change.

Hi David,

I checked pcim_enable_device() before sending the patch, it has a call
to pcim_release() which does disabling of the PCI device and the
releasing of PCI regions obtained from pci_request_regions so there is
no need for pci_release_regions or pci_disable_device anymore.

Specifically, pcim_release contains the following code:

for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
  if (this->region_mask & (1 << i))
    pci_release_region(dev, i);

Also, commit id add243d5bc371eef66f81c9da4fd4b55a18dad23 is a similar
change that further made me believe that the change is a correct one.

However, if you think I am wrong somewhere and I understood things
incorrectly, please correct me.

Thanks,
Amitoj

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

* Re: [PATCH] wan: lmc: Switch to using managed resources
  2016-03-02 14:22   ` Amitoj Kaur Chawla
@ 2016-03-02 18:44     ` David Miller
  0 siblings, 0 replies; 4+ messages in thread
From: David Miller @ 2016-03-02 18:44 UTC (permalink / raw)
  To: amitoj1606; +Cc: netdev, linux-kernel, julia.lawall

From: Amitoj Kaur Chawla <amitoj1606@gmail.com>
Date: Wed, 2 Mar 2016 19:52:42 +0530

> Specifically, pcim_release contains the following code:

How incredibly unintuitive that PCI helper functions magically become
managed just because a driver invoked pcim_enable_device().

Well, if that's what is it, that's what it is.

Applied, thanks.

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

end of thread, other threads:[~2016-03-02 18:44 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-02-27 17:04 [PATCH] wan: lmc: Switch to using managed resources Amitoj Kaur Chawla
2016-03-01 22:21 ` David Miller
2016-03-02 14:22   ` Amitoj Kaur Chawla
2016-03-02 18:44     ` 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).