All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] driver core: remove software node from platform devices on device release
@ 2026-04-10 11:50 Bartosz Golaszewski
  2026-04-10 11:50 ` [PATCH 1/2] driver core: platform: remove software node on release() Bartosz Golaszewski
  2026-04-10 11:50 ` [PATCH 2/2] driver core: platform: tests: add a test case for correct swnode removal Bartosz Golaszewski
  0 siblings, 2 replies; 5+ messages in thread
From: Bartosz Golaszewski @ 2026-04-10 11:50 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov
  Cc: brgl, driver-core, linux-kernel, Bartosz Golaszewski

This fixes an issue in platform device code where, if we specify a
software node for a platform device using struct platform_device_info,
it will not be removed on device .release().

The second patch adds a test-case that can be used to reproduce the
problem and prove that the fix works.

First patch should go into v7.1 while patch 2/2 can be queued for v7.2.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
---
Bartosz Golaszewski (2):
      driver core: platform: remove software node on release()
      driver core: platform: tests: add a test case for correct swnode removal

 drivers/base/platform.c                  |  1 +
 drivers/base/test/platform-device-test.c | 62 ++++++++++++++++++++++++++++++++
 2 files changed, 63 insertions(+)
---
base-commit: 3fa7d958829eb9bc3b469ed07f11de3d2804ef71
change-id: 20260410-swnode-remove-on-dev-unreg-42bfc4b23ba8

Best regards,
-- 
Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/2] driver core: platform: remove software node on release()
  2026-04-10 11:50 [PATCH 0/2] driver core: remove software node from platform devices on device release Bartosz Golaszewski
@ 2026-04-10 11:50 ` Bartosz Golaszewski
  2026-04-13  3:56   ` Dmitry Torokhov
  2026-04-10 11:50 ` [PATCH 2/2] driver core: platform: tests: add a test case for correct swnode removal Bartosz Golaszewski
  1 sibling, 1 reply; 5+ messages in thread
From: Bartosz Golaszewski @ 2026-04-10 11:50 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov
  Cc: brgl, driver-core, linux-kernel, Bartosz Golaszewski

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.

Fixes: 0fc434bc2c45 ("driver core: platform: allow attaching software nodes when creating devices")
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
---
 drivers/base/platform.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/base/platform.c b/drivers/base/platform.c
index 75b4698d0e582e67adafa78c312d75c72fd654cf..a442abb4f1028a91f3c56b51211335678f7f18a2 100644
--- a/drivers/base/platform.c
+++ b/drivers/base/platform.c
@@ -600,6 +600,7 @@ static void platform_device_release(struct device *dev)
 						  pdev.dev);
 
 	of_node_put(pa->pdev.dev.of_node);
+	device_remove_software_node(dev);
 	kfree(pa->pdev.dev.platform_data);
 	kfree(pa->pdev.mfd_cell);
 	kfree(pa->pdev.resource);

-- 
2.47.3


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/2] driver core: platform: tests: add a test case for correct swnode removal
  2026-04-10 11:50 [PATCH 0/2] driver core: remove software node from platform devices on device release Bartosz Golaszewski
  2026-04-10 11:50 ` [PATCH 1/2] driver core: platform: remove software node on release() Bartosz Golaszewski
@ 2026-04-10 11:50 ` Bartosz Golaszewski
  1 sibling, 0 replies; 5+ messages in thread
From: Bartosz Golaszewski @ 2026-04-10 11:50 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov
  Cc: brgl, driver-core, linux-kernel, Bartosz Golaszewski

Extend the kunit module for platform devices with a test case verifying
that the same software node can be added to platform devices repeatedly.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
---
 drivers/base/test/platform-device-test.c | 62 ++++++++++++++++++++++++++++++++
 1 file changed, 62 insertions(+)

diff --git a/drivers/base/test/platform-device-test.c b/drivers/base/test/platform-device-test.c
index 6355a2231b741791b54eb78af42e13f31f745184..9046be1cd0f68b57f70eddb15227609c925318ce 100644
--- a/drivers/base/test/platform-device-test.c
+++ b/drivers/base/test/platform-device-test.c
@@ -5,8 +5,10 @@
 
 #include <linux/device.h>
 #include <linux/device/bus.h>
+#include <linux/fwnode.h>
 #include <linux/of_platform.h>
 #include <linux/platform_device.h>
+#include <linux/property.h>
 
 #define DEVICE_NAME "test"
 
@@ -253,9 +255,69 @@ static struct kunit_suite platform_device_match_test_suite = {
 	.test_cases = platform_device_match_tests,
 };
 
+static int platform_device_swnode_test_probe(struct platform_device *pdev)
+{
+	return 0;
+}
+
+static struct platform_driver platform_swnode_test_driver = {
+	.probe = platform_device_swnode_test_probe,
+	.driver = {
+		.name = DEVICE_NAME,
+	},
+};
+
+static const struct software_node platform_device_test_swnode = { };
+
+static void platform_device_swnode_add_twice(struct kunit *test)
+{
+	struct platform_device_info pdevinfo;
+	struct platform_device *pdev;
+	struct fwnode_handle fwnode;
+	int ret;
+
+	ret = kunit_platform_driver_register(test, &platform_swnode_test_driver);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	fwnode_init(&fwnode, NULL);
+	pdevinfo = (struct platform_device_info){
+		.name = DEVICE_NAME,
+		.id = PLATFORM_DEVID_NONE,
+		.fwnode = &fwnode,
+		.swnode = &platform_device_test_swnode,
+	};
+
+	pdev = platform_device_register_full(&pdevinfo);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
+
+	wait_for_device_probe();
+	KUNIT_ASSERT_TRUE(test, device_is_bound(&pdev->dev));
+
+	platform_device_unregister(pdev);
+
+	pdev = platform_device_register_full(&pdevinfo);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
+
+	wait_for_device_probe();
+	KUNIT_ASSERT_TRUE(test, device_is_bound(&pdev->dev));
+
+	platform_device_unregister(pdev);
+}
+
+static struct kunit_case platform_device_swnode_tests[] = {
+	KUNIT_CASE(platform_device_swnode_add_twice),
+	{}
+};
+
+static struct kunit_suite platform_device_swnode_test_suite = {
+	.name = "platform-device-swnode",
+	.test_cases = platform_device_swnode_tests,
+};
+
 kunit_test_suites(
 	&platform_device_devm_test_suite,
 	&platform_device_match_test_suite,
+	&platform_device_swnode_test_suite,
 );
 
 MODULE_DESCRIPTION("Test module for platform devices");

-- 
2.47.3


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] driver core: platform: remove software node on release()
  2026-04-10 11:50 ` [PATCH 1/2] driver core: platform: remove software node on release() Bartosz Golaszewski
@ 2026-04-13  3:56   ` Dmitry Torokhov
  2026-04-21  8:44     ` Bartosz Golaszewski
  0 siblings, 1 reply; 5+ messages in thread
From: Dmitry Torokhov @ 2026-04-13  3:56 UTC (permalink / raw)
  To: Bartosz Golaszewski
  Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich, brgl,
	driver-core, linux-kernel

Hi Bartosz,

On Fri, Apr 10, 2026 at 01:50:45PM +0200, Bartosz Golaszewski wrote:
> 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.

Thank you for spotting this, but I do not believe the patch is correct
as it stands. We need to differentiate between nodes registered by
platform devices vs nodes that already registered where we simply use
them.

I think we need to mark nodes that platform_device_register_full()
registers form swnodes as "managed = true" so that they are cleaned up
properly.

See also sashiko feedback.

https://sashiko.dev/#/patchset/20260410-swnode-remove-on-dev-unreg-v1-0-cd7d305f3db2%40oss.qualcomm.com

Thanks.

-- 
Dmitry

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] driver core: platform: remove software node on release()
  2026-04-13  3:56   ` Dmitry Torokhov
@ 2026-04-21  8:44     ` Bartosz Golaszewski
  0 siblings, 0 replies; 5+ messages in thread
From: Bartosz Golaszewski @ 2026-04-21  8:44 UTC (permalink / raw)
  To: Dmitry Torokhov
  Cc: Bartosz Golaszewski, Greg Kroah-Hartman, Rafael J. Wysocki,
	Danilo Krummrich, driver-core, linux-kernel

On Mon, Apr 13, 2026 at 5:56 AM Dmitry Torokhov
<dmitry.torokhov@gmail.com> wrote:
>
> Hi Bartosz,
>
> On Fri, Apr 10, 2026 at 01:50:45PM +0200, Bartosz Golaszewski wrote:
> > 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.
>
> Thank you for spotting this, but I do not believe the patch is correct
> as it stands. We need to differentiate between nodes registered by
> platform devices vs nodes that already registered where we simply use
> them.
>

We already do: platform_device_release() is only set for devices
dynamically allocated with platform_device_alloc() which is what
happens in platform_device_register_full(). platform_device_alloc()
already imposes limitations on the caller - like having to provide
platform data that can be shallow-copied as platform_device_release()
calls kfree() on it unconditionally. Even if the user adds the
software node manually, it needs to be removed.

> I think we need to mark nodes that platform_device_register_full()
> registers form swnodes as "managed = true" so that they are cleaned up
> properly.
>
> See also sashiko feedback.
>
> https://sashiko.dev/#/patchset/20260410-swnode-remove-on-dev-unreg-v1-0-cd7d305f3db2%40oss.qualcomm.com
>

I'll see about reproducing it and possibly adding a kunit test-case as well.

Bart

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-04-21  8:44 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-10 11:50 [PATCH 0/2] driver core: remove software node from platform devices on device release Bartosz Golaszewski
2026-04-10 11:50 ` [PATCH 1/2] driver core: platform: remove software node on release() Bartosz Golaszewski
2026-04-13  3:56   ` Dmitry Torokhov
2026-04-21  8:44     ` Bartosz Golaszewski
2026-04-10 11:50 ` [PATCH 2/2] driver core: platform: tests: add a test case for correct swnode removal Bartosz Golaszewski

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.