[U-Boot] [RFC PATCH 08/12] dm: refactor device_bind() and device_unbind() with devm_kzalloc()

Masahiro Yamada yamada.masahiro at socionext.com
Wed Jul 8 06:29:38 CEST 2015


With devm_kzalloc(), device_unbind() and the failure path in
device_bind() can be much cleaner.

We no longer need such flags as DM_FLAG_ALLOC_PDATA etc. because
we do not have to remember if the memory has been really allocated.

The memory is freed free when the device is unbind.

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

 drivers/core/device-remove.c | 12 ------------
 drivers/core/device.c        | 24 ++++++------------------
 include/dm/device.h          |  9 ---------
 3 files changed, 6 insertions(+), 39 deletions(-)

diff --git a/drivers/core/device-remove.c b/drivers/core/device-remove.c
index e1714b2..0417535 100644
--- a/drivers/core/device-remove.c
+++ b/drivers/core/device-remove.c
@@ -91,18 +91,6 @@ int device_unbind(struct udevice *dev)
 	if (ret)
 		return ret;
 
-	if (dev->flags & DM_FLAG_ALLOC_PDATA) {
-		free(dev->platdata);
-		dev->platdata = NULL;
-	}
-	if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
-		free(dev->uclass_platdata);
-		dev->uclass_platdata = NULL;
-	}
-	if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
-		free(dev->parent_platdata);
-		dev->parent_platdata = NULL;
-	}
 	ret = uclass_unbind_device(dev);
 	if (ret)
 		return ret;
diff --git a/drivers/core/device.c b/drivers/core/device.c
index ac2c4f8..f024aa2 100644
--- a/drivers/core/device.c
+++ b/drivers/core/device.c
@@ -75,8 +75,9 @@ int device_bind(struct udevice *parent, const struct driver *drv,
 	}
 
 	if (!dev->platdata && drv->platdata_auto_alloc_size) {
-		dev->flags |= DM_FLAG_ALLOC_PDATA;
-		dev->platdata = calloc(1, drv->platdata_auto_alloc_size);
+		dev->platdata = devm_kzalloc(dev,
+					     drv->platdata_auto_alloc_size,
+					     GFP_KERNEL);
 		if (!dev->platdata) {
 			ret = -ENOMEM;
 			goto fail_alloc1;
@@ -85,8 +86,7 @@ int device_bind(struct udevice *parent, const struct driver *drv,
 
 	size = uc->uc_drv->per_device_platdata_auto_alloc_size;
 	if (size) {
-		dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA;
-		dev->uclass_platdata = calloc(1, size);
+		dev->uclass_platdata = devm_kzalloc(dev, size, GFP_KERNEL);
 		if (!dev->uclass_platdata) {
 			ret = -ENOMEM;
 			goto fail_alloc2;
@@ -100,8 +100,8 @@ int device_bind(struct udevice *parent, const struct driver *drv,
 					per_child_platdata_auto_alloc_size;
 		}
 		if (size) {
-			dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
-			dev->parent_platdata = calloc(1, size);
+			dev->parent_platdata = devm_kzalloc(dev, size,
+							    GFP_KERNEL);
 			if (!dev->parent_platdata) {
 				ret = -ENOMEM;
 				goto fail_alloc3;
@@ -155,21 +155,9 @@ fail_bind:
 fail_uclass_bind:
 	if (IS_ENABLED(CONFIG_DM_DEVICE_REMOVE)) {
 		list_del(&dev->sibling_node);
-		if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
-			free(dev->parent_platdata);
-			dev->parent_platdata = NULL;
-		}
 	}
 fail_alloc3:
-	if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
-		free(dev->uclass_platdata);
-		dev->uclass_platdata = NULL;
-	}
 fail_alloc2:
-	if (dev->flags & DM_FLAG_ALLOC_PDATA) {
-		free(dev->platdata);
-		dev->platdata = NULL;
-	}
 fail_alloc1:
 	devres_release_all(dev);
 
diff --git a/include/dm/device.h b/include/dm/device.h
index fac0868..9525581 100644
--- a/include/dm/device.h
+++ b/include/dm/device.h
@@ -23,18 +23,9 @@ struct driver_info;
 /* Driver is active (probed). Cleared when it is removed */
 #define DM_FLAG_ACTIVATED	(1 << 0)
 
-/* DM is responsible for allocating and freeing platdata */
-#define DM_FLAG_ALLOC_PDATA	(1 << 1)
-
 /* DM should init this device prior to relocation */
 #define DM_FLAG_PRE_RELOC	(1 << 2)
 
-/* DM is responsible for allocating and freeing parent_platdata */
-#define DM_FLAG_ALLOC_PARENT_PDATA	(1 << 3)
-
-/* DM is responsible for allocating and freeing uclass_platdata */
-#define DM_FLAG_ALLOC_UCLASS_PDATA	(1 << 4)
-
 /* Allocate driver private data on a DMA boundary */
 #define DM_FLAG_ALLOC_PRIV_DMA	(1 << 5)
 
-- 
1.9.1



More information about the U-Boot mailing list