If we pass a software node to a newly created device using struct
platform_device_info, it will not be removed when the device is
released. This may happen when a module creating the device is removed
or on failure in platform_device_add().

When we try to reuse that software node in a subsequent call to
platform_device_register_full(), it will fails with -EBUSY. Add the
missing call to device_remove_software_node() in release path.

In addition to the above change, make sure that we still function
correctly if a software node is used as the primary firmware node.

Fixes: 0fc434bc2c45 ("driver core: platform: allow attaching software nodes 
when creating devices")
Reviewed-by: Andy Shevchenko <[email protected]>
Signed-off-by: Bartosz Golaszewski <[email protected]>
---
 drivers/base/platform.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/drivers/base/platform.c b/drivers/base/platform.c
index 
f617e02b171f6e59a3c40504a71f8915a41e8514..d2f40b348f193db842aa7c17c9c41be59a03d35f
 100644
--- a/drivers/base/platform.c
+++ b/drivers/base/platform.c
@@ -599,6 +599,7 @@ static void platform_device_release(struct device *dev)
        struct platform_object *pa = container_of(dev, struct platform_object,
                                                  pdev.dev);
 
+       device_remove_software_node(dev);
        of_node_put(pa->pdev.dev.of_node);
        kfree(pa->pdev.dev.platform_data);
        kfree(pa->pdev.mfd_cell);
@@ -895,6 +896,16 @@ struct platform_device 
*platform_device_register_full(const struct platform_devi
                pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
        }
 
+       /*
+        * If the primary firmware node is a software node and there's no
+        * secondary firmware node, the primary will be affected by the call
+        * to device_remove_software_node() in platform_device_release() and
+        * its reference count will be dropped by one. Take another reference
+        * here to make it have no effect.
+        */
+       if (is_software_node(pdevinfo->fwnode))
+               fwnode_handle_get(pdevinfo->fwnode);
+
        ret = platform_device_add_resources(pdev, pdevinfo->res, 
pdevinfo->num_res);
        if (ret)
                goto err;

-- 
2.47.3


Reply via email to