[U-Boot] [PATCH v2 11/14] dm: refactor device_probe() and device_remove() with devm_kzalloc()

Masahiro Yamada
2015-07-13T04:17:14Z

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>
Reviewed-by: Heiko Schocher <hs@denx.de>
---

Changes in v2: None

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

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 bcae3ba..e4097c9 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;
--
1.9.1