diff mbox

[U-Boot,v4,2/4] dm: core: Fix code reentrancy issue in device_probe_child()

Message ID 1440404044-9154-3-git-send-email-bmeng.cn@gmail.com
State Accepted
Delegated to: Simon Glass
Headers show

Commit Message

Bin Meng Aug. 24, 2015, 8:14 a.m. UTC
The device might have already been probed during the call to
device_probe() on its parent device (e.g. PCI bridge devices).
In its parent device's probe routine, it might probe all of
its child devices via device_probe() thus the codes reenter
device_probe_child(). To support code reentrancy, test these
allocated memory against NULL to avoid memory leak, and return
to the caller if dev->flags has DM_FLAG_ACTIVATED set after
device_probe() returns, so that we don't mess up the device.

Signed-off-by: Bin Meng <bmeng.cn@gmail.com>

---

Changes in v4:
- Fix memory leak in device_probe_child()

Changes in v3: None

 drivers/core/device.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

Comments

Simon Glass Aug. 25, 2015, 5:04 a.m. UTC | #1
On 24 August 2015 at 02:14, Bin Meng <bmeng.cn@gmail.com> wrote:
> The device might have already been probed during the call to
> device_probe() on its parent device (e.g. PCI bridge devices).
> In its parent device's probe routine, it might probe all of
> its child devices via device_probe() thus the codes reenter
> device_probe_child(). To support code reentrancy, test these
> allocated memory against NULL to avoid memory leak, and return
> to the caller if dev->flags has DM_FLAG_ACTIVATED set after
> device_probe() returns, so that we don't mess up the device.
>
> Signed-off-by: Bin Meng <bmeng.cn@gmail.com>
>
> ---
>
> Changes in v4:
> - Fix memory leak in device_probe_child()
>
> Changes in v3: None
>
>  drivers/core/device.c | 19 ++++++++++++++-----
>  1 file changed, 14 insertions(+), 5 deletions(-)

Looks like a good solution and it's good to have the comments too.

Acked-by: Simon Glass <sjg@chromium.org>
Simon Glass Aug. 26, 2015, 2:55 p.m. UTC | #2
On 24 August 2015 at 22:04, Simon Glass <sjg@chromium.org> wrote:
> On 24 August 2015 at 02:14, Bin Meng <bmeng.cn@gmail.com> wrote:
>> The device might have already been probed during the call to
>> device_probe() on its parent device (e.g. PCI bridge devices).
>> In its parent device's probe routine, it might probe all of
>> its child devices via device_probe() thus the codes reenter
>> device_probe_child(). To support code reentrancy, test these
>> allocated memory against NULL to avoid memory leak, and return
>> to the caller if dev->flags has DM_FLAG_ACTIVATED set after
>> device_probe() returns, so that we don't mess up the device.
>>
>> Signed-off-by: Bin Meng <bmeng.cn@gmail.com>
>>
>> ---
>>
>> Changes in v4:
>> - Fix memory leak in device_probe_child()
>>
>> Changes in v3: None
>>
>>  drivers/core/device.c | 19 ++++++++++++++-----
>>  1 file changed, 14 insertions(+), 5 deletions(-)
>
> Looks like a good solution and it's good to have the comments too.
>
> Acked-by: Simon Glass <sjg@chromium.org>

Applied to u-boot-x86, thanks!
diff mbox

Patch

diff --git a/drivers/core/device.c b/drivers/core/device.c
index e23a872..a31e25f 100644
--- a/drivers/core/device.c
+++ b/drivers/core/device.c
@@ -226,17 +226,17 @@  int device_probe_child(struct udevice *dev, void *parent_priv)
 	drv = dev->driver;
 	assert(drv);
 
-	/* Allocate private data if requested */
-	if (drv->priv_auto_alloc_size) {
+	/* Allocate private data if requested and not reentered */
+	if (drv->priv_auto_alloc_size && !dev->priv) {
 		dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags);
 		if (!dev->priv) {
 			ret = -ENOMEM;
 			goto fail;
 		}
 	}
-	/* Allocate private data if requested */
+	/* Allocate private data if requested and not reentered */
 	size = dev->uclass->uc_drv->per_device_auto_alloc_size;
-	if (size) {
+	if (size && !dev->uclass_priv) {
 		dev->uclass_priv = calloc(1, size);
 		if (!dev->uclass_priv) {
 			ret = -ENOMEM;
@@ -251,7 +251,7 @@  int device_probe_child(struct udevice *dev, void *parent_priv)
 			size = dev->parent->uclass->uc_drv->
 					per_child_auto_alloc_size;
 		}
-		if (size) {
+		if (size && !dev->parent_priv) {
 			dev->parent_priv = alloc_priv(size, drv->flags);
 			if (!dev->parent_priv) {
 				ret = -ENOMEM;
@@ -264,6 +264,15 @@  int device_probe_child(struct udevice *dev, void *parent_priv)
 		ret = device_probe(dev->parent);
 		if (ret)
 			goto fail;
+
+		/*
+		 * The device might have already been probed during
+		 * the call to device_probe() on its parent device
+		 * (e.g. PCI bridge devices). Test the flags again
+		 * so that we don't mess up the device.
+		 */
+		if (dev->flags & DM_FLAG_ACTIVATED)
+			return 0;
 	}
 
 	seq = uclass_resolve_seq(dev);