diff mbox series

[17/30] pci: Use kmemdup rather than duplicating its implementation

Message ID 20190703131627.25455-1-huangfq.daxian@gmail.com (mailing list archive)
State New, archived
Delegated to: Bjorn Helgaas
Headers show
Series None | expand

Commit Message

Fuqian Huang July 3, 2019, 1:16 p.m. UTC
kmemdup is introduced to duplicate a region of memory in a neat way.
Rather than kmalloc/kzalloc + memset, which the programmer needs to
write the size twice (sometimes lead to mistakes), kmemdup improves
readability, leads to smaller code and also reduce the chances of mistakes.
Suggestion to use kmemdup rather than using kmalloc/kzalloc + memset.

Signed-off-by: Fuqian Huang <huangfq.daxian@gmail.com>
---
 drivers/pci/hotplug/ibmphp_core.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

Comments

David Laight July 3, 2019, 4:22 p.m. UTC | #1
From: Fuqian Huang
> Sent: 03 July 2019 14:16
> 
> kmemdup is introduced to duplicate a region of memory in a neat way.
> Rather than kmalloc/kzalloc + memset, which the programmer needs to
> write the size twice (sometimes lead to mistakes), kmemdup improves
> readability, leads to smaller code and also reduce the chances of mistakes.
> Suggestion to use kmemdup rather than using kmalloc/kzalloc + memset.
> 
> Signed-off-by: Fuqian Huang <huangfq.daxian@gmail.com>
> ---
>  drivers/pci/hotplug/ibmphp_core.c | 13 ++++++-------
>  1 file changed, 6 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/pci/hotplug/ibmphp_core.c b/drivers/pci/hotplug/ibmphp_core.c
> index 17124254d897..0e340e105c3b 100644
> --- a/drivers/pci/hotplug/ibmphp_core.c
> +++ b/drivers/pci/hotplug/ibmphp_core.c
> @@ -1261,19 +1261,18 @@ static int __init ibmphp_init(void)
> 
>  	info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
> 
> -	ibmphp_pci_bus = kmalloc(sizeof(*ibmphp_pci_bus), GFP_KERNEL);
> -	if (!ibmphp_pci_bus) {
> -		rc = -ENOMEM;
> -		goto exit;
> -	}
> -
>  	bus = pci_find_bus(0, 0);
>  	if (!bus) {
>  		err("Can't find the root pci bus, can not continue\n");
>  		rc = -ENODEV;
>  		goto error;
>  	}
> -	memcpy(ibmphp_pci_bus, bus, sizeof(*ibmphp_pci_bus));
> +
> +	ibmphp_pci_bus = kmemdup(bus, sizeof(*ibmphp_pci_bus), GFP_KERNEL);
> +	if (!ibmphp_pci_bus) {
> +		rc = -ENOMEM;
> +		goto exit;
> +	}

Not sure why I even looked as this...

But the error path if pci_find_bus() fails is now wrong.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
diff mbox series

Patch

diff --git a/drivers/pci/hotplug/ibmphp_core.c b/drivers/pci/hotplug/ibmphp_core.c
index 17124254d897..0e340e105c3b 100644
--- a/drivers/pci/hotplug/ibmphp_core.c
+++ b/drivers/pci/hotplug/ibmphp_core.c
@@ -1261,19 +1261,18 @@  static int __init ibmphp_init(void)
 
 	info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
 
-	ibmphp_pci_bus = kmalloc(sizeof(*ibmphp_pci_bus), GFP_KERNEL);
-	if (!ibmphp_pci_bus) {
-		rc = -ENOMEM;
-		goto exit;
-	}
-
 	bus = pci_find_bus(0, 0);
 	if (!bus) {
 		err("Can't find the root pci bus, can not continue\n");
 		rc = -ENODEV;
 		goto error;
 	}
-	memcpy(ibmphp_pci_bus, bus, sizeof(*ibmphp_pci_bus));
+
+	ibmphp_pci_bus = kmemdup(bus, sizeof(*ibmphp_pci_bus), GFP_KERNEL);
+	if (!ibmphp_pci_bus) {
+		rc = -ENOMEM;
+		goto exit;
+	}
 
 	ibmphp_debug = debug;