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.
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")
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
75b4698d0e582e67adafa78c312d75c72fd654cf..43ea7dcd338dd3ddae57e6d0677e5cb2673f6ed5
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);
@@ -885,6 +886,16 @@ struct platform_device
*platform_device_register_full(const struct platform_devi
goto err;
}
+ /*
+ * 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) && !pdevinfo->swnode)
+ fwnode_handle_get(pdevinfo->fwnode);
+
ret = platform_device_add(pdev);
if (ret) {
err:
--
2.47.3