diff mbox

[U-Boot,RFC,11/12] dm: refactor device_probe() and device_remove() with devm_kzalloc()

Message ID 1436329782-9179-12-git-send-email-yamada.masahiro@socionext.com
State RFC
Delegated to: Simon Glass
Headers show

Commit Message

Masahiro Yamada July 8, 2015, 4:29 a.m. UTC
The memory is automatically released on driver removal, so we do not
need to do so explicitly in device_free().

The helper function alloc_priv() is no longer needed because
devm_kzalloc() now understands the GFP_DMA flag.

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
---

 drivers/core/device-remove.c | 23 -----------------------
 drivers/core/device.c        | 25 +++++++------------------
 2 files changed, 7 insertions(+), 41 deletions(-)

Comments

Heiko Schocher July 8, 2015, 5:14 a.m. UTC | #1
Hello Masahiro,

Am 08.07.2015 um 06:29 schrieb Masahiro Yamada:
> The memory is automatically released on driver removal, so we do not
> need to do so explicitly in device_free().
>
> The helper function alloc_priv() is no longer needed because
> devm_kzalloc() now understands the GFP_DMA flag.
>
> Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
> ---
>
>   drivers/core/device-remove.c | 23 -----------------------
>   drivers/core/device.c        | 25 +++++++------------------
>   2 files changed, 7 insertions(+), 41 deletions(-)

Nice statistic ... this effect should also popup in drivers code.

Reviewed-by: Heiko Schocher<hs@denx.de>

bye,
Heiko
>
> diff --git a/drivers/core/device-remove.c b/drivers/core/device-remove.c
> index 0417535..78551e4 100644
> --- a/drivers/core/device-remove.c
> +++ b/drivers/core/device-remove.c
> @@ -111,29 +111,6 @@ int device_unbind(struct udevice *dev)
>    */
>   void device_free(struct udevice *dev)
>   {
> -	int size;
> -
> -	if (dev->driver->priv_auto_alloc_size) {
> -		free(dev->priv);
> -		dev->priv = NULL;
> -	}
> -	size = dev->uclass->uc_drv->per_device_auto_alloc_size;
> -	if (size) {
> -		free(dev->uclass_priv);
> -		dev->uclass_priv = NULL;
> -	}
> -	if (dev->parent) {
> -		size = dev->parent->driver->per_child_auto_alloc_size;
> -		if (!size) {
> -			size = dev->parent->uclass->uc_drv->
> -					per_child_auto_alloc_size;
> -		}
> -		if (size) {
> -			free(dev->parent_priv);
> -			dev->parent_priv = NULL;
> -		}
> -	}
> -
>   	devres_release_probe(dev);
>   }
>
> diff --git a/drivers/core/device.c b/drivers/core/device.c
> index 25b9b63..e5291e2 100644
> --- a/drivers/core/device.c
> +++ b/drivers/core/device.c
> @@ -179,27 +179,13 @@ int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
>   			   -1, devp);
>   }
>
> -static void *alloc_priv(int size, uint flags)
> -{
> -	void *priv;
> -
> -	if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
> -		priv = memalign(ARCH_DMA_MINALIGN, size);
> -		if (priv)
> -			memset(priv, '\0', size);
> -	} else {
> -		priv = calloc(1, size);
> -	}
> -
> -	return priv;
> -}
> -
>   int device_probe_child(struct udevice *dev, void *parent_priv)
>   {
>   	const struct driver *drv;
>   	int size = 0;
>   	int ret;
>   	int seq;
> +	gfp_t flags = GFP_KERNEL;
>
>   	if (!dev)
>   		return -EINVAL;
> @@ -210,9 +196,12 @@ int device_probe_child(struct udevice *dev, void *parent_priv)
>   	drv = dev->driver;
>   	assert(drv);
>
> +	if (drv->flags & DM_FLAG_ALLOC_PRIV_DMA)
> +		flags |= GFP_DMA;
> +
>   	/* Allocate private data if requested */
>   	if (drv->priv_auto_alloc_size) {
> -		dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags);
> +		dev->priv = devm_kzalloc(dev, drv->priv_auto_alloc_size, flags);
>   		if (!dev->priv) {
>   			ret = -ENOMEM;
>   			goto fail;
> @@ -221,7 +210,7 @@ int device_probe_child(struct udevice *dev, void *parent_priv)
>   	/* Allocate private data if requested */
>   	size = dev->uclass->uc_drv->per_device_auto_alloc_size;
>   	if (size) {
> -		dev->uclass_priv = calloc(1, size);
> +		dev->uclass_priv = devm_kzalloc(dev, size, GFP_KERNEL);
>   		if (!dev->uclass_priv) {
>   			ret = -ENOMEM;
>   			goto fail;
> @@ -236,7 +225,7 @@ int device_probe_child(struct udevice *dev, void *parent_priv)
>   					per_child_auto_alloc_size;
>   		}
>   		if (size) {
> -			dev->parent_priv = alloc_priv(size, drv->flags);
> +			dev->parent_priv = devm_kzalloc(dev, size, flags);
>   			if (!dev->parent_priv) {
>   				ret = -ENOMEM;
>   				goto fail;
>
diff mbox

Patch

diff --git a/drivers/core/device-remove.c b/drivers/core/device-remove.c
index 0417535..78551e4 100644
--- a/drivers/core/device-remove.c
+++ b/drivers/core/device-remove.c
@@ -111,29 +111,6 @@  int device_unbind(struct udevice *dev)
  */
 void device_free(struct udevice *dev)
 {
-	int size;
-
-	if (dev->driver->priv_auto_alloc_size) {
-		free(dev->priv);
-		dev->priv = NULL;
-	}
-	size = dev->uclass->uc_drv->per_device_auto_alloc_size;
-	if (size) {
-		free(dev->uclass_priv);
-		dev->uclass_priv = NULL;
-	}
-	if (dev->parent) {
-		size = dev->parent->driver->per_child_auto_alloc_size;
-		if (!size) {
-			size = dev->parent->uclass->uc_drv->
-					per_child_auto_alloc_size;
-		}
-		if (size) {
-			free(dev->parent_priv);
-			dev->parent_priv = NULL;
-		}
-	}
-
 	devres_release_probe(dev);
 }
 
diff --git a/drivers/core/device.c b/drivers/core/device.c
index 25b9b63..e5291e2 100644
--- a/drivers/core/device.c
+++ b/drivers/core/device.c
@@ -179,27 +179,13 @@  int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
 			   -1, devp);
 }
 
-static void *alloc_priv(int size, uint flags)
-{
-	void *priv;
-
-	if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
-		priv = memalign(ARCH_DMA_MINALIGN, size);
-		if (priv)
-			memset(priv, '\0', size);
-	} else {
-		priv = calloc(1, size);
-	}
-
-	return priv;
-}
-
 int device_probe_child(struct udevice *dev, void *parent_priv)
 {
 	const struct driver *drv;
 	int size = 0;
 	int ret;
 	int seq;
+	gfp_t flags = GFP_KERNEL;
 
 	if (!dev)
 		return -EINVAL;
@@ -210,9 +196,12 @@  int device_probe_child(struct udevice *dev, void *parent_priv)
 	drv = dev->driver;
 	assert(drv);
 
+	if (drv->flags & DM_FLAG_ALLOC_PRIV_DMA)
+		flags |= GFP_DMA;
+
 	/* Allocate private data if requested */
 	if (drv->priv_auto_alloc_size) {
-		dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags);
+		dev->priv = devm_kzalloc(dev, drv->priv_auto_alloc_size, flags);
 		if (!dev->priv) {
 			ret = -ENOMEM;
 			goto fail;
@@ -221,7 +210,7 @@  int device_probe_child(struct udevice *dev, void *parent_priv)
 	/* Allocate private data if requested */
 	size = dev->uclass->uc_drv->per_device_auto_alloc_size;
 	if (size) {
-		dev->uclass_priv = calloc(1, size);
+		dev->uclass_priv = devm_kzalloc(dev, size, GFP_KERNEL);
 		if (!dev->uclass_priv) {
 			ret = -ENOMEM;
 			goto fail;
@@ -236,7 +225,7 @@  int device_probe_child(struct udevice *dev, void *parent_priv)
 					per_child_auto_alloc_size;
 		}
 		if (size) {
-			dev->parent_priv = alloc_priv(size, drv->flags);
+			dev->parent_priv = devm_kzalloc(dev, size, flags);
 			if (!dev->parent_priv) {
 				ret = -ENOMEM;
 				goto fail;