All of lore.kernel.org
 help / color / mirror / Atom feed
From: Dmitry Torokhov <dmitry.torokhov@gmail.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	 "Rafael J. Wysocki" <rafael@kernel.org>,
	Danilo Krummrich <dakr@kernel.org>
Cc: Johan Hovold <johan@kernel.org>,
	 Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	 Daniel Scally <djrscally@gmail.com>,
	 Heikki Krogerus <heikki.krogerus@linux.intel.com>,
	 Sakari Ailus <sakari.ailus@linux.intel.com>,
	 Bartosz Golaszewski <brgl@kernel.org>,
	driver-core@lists.linux.dev,  linux-acpi@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH 3/3] driver core: platform: tests: add shared primary fwnode swnode tests
Date: Wed, 02 Dec 2026 17:34:51 -0800	[thread overview]
Message-ID: <20261202-swnode-shared-primary-fix-v1-3-7ff23caaa1ed@gmail.com> (raw)
In-Reply-To: <20261202-swnode-shared-primary-fix-v1-0-7ff23caaa1ed@gmail.com>

Add KUnit regression tests covering child platform devices that share a
parent's primary firmware node with a secondary software node, including
both unmanaged and managed software nodes, detachment via
set_primary_fwnode() before release, child-owned secondary software
nodes on a shared primary node, release of unregistered devices with a
managed software node, and removal via device_remove_software_node() on
a registered managed device.

Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
 drivers/base/test/platform-device-test.c | 328 +++++++++++++++++++++++++++++++
 1 file changed, 328 insertions(+)

diff --git a/drivers/base/test/platform-device-test.c b/drivers/base/test/platform-device-test.c
index b0cc37d166ff..fd4f0e449089 100644
--- a/drivers/base/test/platform-device-test.c
+++ b/drivers/base/test/platform-device-test.c
@@ -421,10 +421,338 @@ static void platform_device_two_swnodes(struct kunit *test)
 			    "Expected errno == -EINVAL, got: %pe", pdev);
 }
 
+/*
+ * Check that unregistering a child platform device sharing its parent's
+ * primary firmware node leaves the parent's secondary software node intact
+ * and its reference count balanced.
+ */
+static void platform_device_swnode_shared_primary(struct kunit *test)
+{
+	struct platform_device_info parent_info, child_info;
+	struct platform_device *parent, *child;
+	struct fwnode_handle *fwnode, *swnode;
+
+	fwnode = kunit_kzalloc(test, sizeof(*fwnode), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fwnode);
+	fwnode_init(fwnode, NULL);
+
+	parent_info = (struct platform_device_info){
+		.name = DEVICE_NAME,
+		.id = 0,
+		.fwnode = fwnode,
+		.swnode = &platform_device_test_swnode,
+	};
+
+	parent = kunit_platform_device_register_full(test, &parent_info);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
+
+	swnode = software_node_fwnode(&platform_device_test_swnode);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, swnode);
+	KUNIT_EXPECT_PTR_EQ(test, fwnode->secondary, swnode);
+
+	child_info = (struct platform_device_info){
+		.parent = &parent->dev,
+		.name = DEVICE_NAME,
+		.id = 1,
+		.fwnode = fwnode,
+	};
+
+	child = kunit_platform_device_register_full(test, &child_info);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, child);
+
+	kunit_platform_device_unregister(test, child);
+
+	KUNIT_EXPECT_PTR_EQ(test, fwnode->secondary, swnode);
+	KUNIT_EXPECT_PTR_EQ(test, software_node_fwnode(&platform_device_test_swnode),
+			    swnode);
+
+	kunit_platform_device_unregister(test, parent);
+
+	KUNIT_EXPECT_PTR_EQ(test, fwnode->secondary, NULL);
+	KUNIT_EXPECT_PTR_EQ(test, software_node_fwnode(&platform_device_test_swnode),
+			    NULL);
+
+	parent = kunit_platform_device_register_full(test, &parent_info);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
+	kunit_platform_device_unregister(test, parent);
+}
+
+/*
+ * Check that detaching a shared primary firmware node via set_primary_fwnode()
+ * before final release of a child device clears the child's fwnode without
+ * promoting or releasing the parent's secondary software node.
+ */
+static void platform_device_swnode_shared_primary_detached(struct kunit *test)
+{
+	struct platform_device_info parent_info, child_info;
+	struct platform_device *parent, *child;
+	struct fwnode_handle *fwnode, *swnode;
+
+	fwnode = kunit_kzalloc(test, sizeof(*fwnode), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fwnode);
+	fwnode_init(fwnode, NULL);
+
+	parent_info = (struct platform_device_info){
+		.name = DEVICE_NAME,
+		.id = 0,
+		.fwnode = fwnode,
+		.swnode = &platform_device_test_swnode,
+	};
+
+	parent = kunit_platform_device_register_full(test, &parent_info);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
+
+	swnode = software_node_fwnode(&platform_device_test_swnode);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, swnode);
+
+	child_info = (struct platform_device_info){
+		.parent = &parent->dev,
+		.name = DEVICE_NAME,
+		.id = 1,
+		.fwnode = fwnode,
+	};
+
+	child = kunit_platform_device_register_full(test, &child_info);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, child);
+
+	/* Keep child alive past device_del() to simulate ACPI unbind before release. */
+	get_device(&child->dev);
+	kunit_platform_device_unregister(test, child);
+
+	set_primary_fwnode(&child->dev, NULL);
+	KUNIT_EXPECT_PTR_EQ(test, dev_fwnode(&child->dev), NULL);
+	KUNIT_EXPECT_PTR_EQ(test, fwnode->secondary, swnode);
+
+	put_device(&child->dev);
+
+	KUNIT_EXPECT_PTR_EQ(test, fwnode->secondary, swnode);
+	KUNIT_EXPECT_PTR_EQ(test, software_node_fwnode(&platform_device_test_swnode),
+			    swnode);
+
+	kunit_platform_device_unregister(test, parent);
+
+	KUNIT_EXPECT_PTR_EQ(test, fwnode->secondary, NULL);
+	KUNIT_EXPECT_PTR_EQ(test, software_node_fwnode(&platform_device_test_swnode),
+			    NULL);
+
+	parent = kunit_platform_device_register_full(test, &parent_info);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
+	kunit_platform_device_unregister(test, parent);
+}
+
+/*
+ * Check that unregistering a child platform device sharing a parent's primary
+ * firmware node with a managed secondary software node leaves the managed node
+ * attached and alive until the parent is unregistered.
+ */
+static void platform_device_managed_swnode_shared_primary(struct kunit *test)
+{
+	static const struct property_entry properties[] = {
+		PROPERTY_ENTRY_U32("foo", 42),
+		{ }
+	};
+	struct platform_device_info parent_info, child_info;
+	struct platform_device *parent, *child;
+	struct fwnode_handle *fwnode, *swnode;
+	u32 val = 0;
+	int ret;
+
+	fwnode = kunit_kzalloc(test, sizeof(*fwnode), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fwnode);
+	fwnode_init(fwnode, NULL);
+
+	parent_info = (struct platform_device_info){
+		.name = DEVICE_NAME,
+		.id = 0,
+		.fwnode = fwnode,
+		.properties = properties,
+	};
+
+	parent = kunit_platform_device_register_full(test, &parent_info);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
+
+	swnode = fwnode->secondary;
+	KUNIT_ASSERT_TRUE(test, is_software_node(swnode));
+
+	child_info = (struct platform_device_info){
+		.parent = &parent->dev,
+		.name = DEVICE_NAME,
+		.id = 1,
+		.fwnode = fwnode,
+	};
+
+	child = kunit_platform_device_register_full(test, &child_info);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, child);
+
+	kunit_platform_device_unregister(test, child);
+
+	KUNIT_EXPECT_PTR_EQ(test, fwnode->secondary, swnode);
+	ret = fwnode_property_read_u32(swnode, "foo", &val);
+	KUNIT_EXPECT_EQ(test, ret, 0);
+	KUNIT_EXPECT_EQ(test, val, 42);
+
+	child_info.id = 2;
+	child = kunit_platform_device_register_full(test, &child_info);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, child);
+
+	get_device(&child->dev);
+	kunit_platform_device_unregister(test, child);
+
+	set_primary_fwnode(&child->dev, NULL);
+	KUNIT_EXPECT_PTR_EQ(test, dev_fwnode(&child->dev), NULL);
+	KUNIT_EXPECT_PTR_EQ(test, fwnode->secondary, swnode);
+
+	put_device(&child->dev);
+
+	val = 0;
+	KUNIT_EXPECT_PTR_EQ(test, fwnode->secondary, swnode);
+	ret = fwnode_property_read_u32(swnode, "foo", &val);
+	KUNIT_EXPECT_EQ(test, ret, 0);
+	KUNIT_EXPECT_EQ(test, val, 42);
+
+	kunit_platform_device_unregister(test, parent);
+
+	KUNIT_EXPECT_PTR_EQ(test, fwnode->secondary, NULL);
+}
+
+/*
+ * Check that a child platform device owning its own secondary software node on
+ * a shared parent primary firmware node detaches and releases that secondary
+ * software node when set_primary_fwnode(..., NULL) is called before release.
+ */
+static void platform_device_swnode_child_owned_on_shared_primary(struct kunit *test)
+{
+	struct platform_device_info parent_info, child_info;
+	struct platform_device *parent, *child;
+	struct fwnode_handle *fwnode, *swnode;
+
+	fwnode = kunit_kzalloc(test, sizeof(*fwnode), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fwnode);
+	fwnode_init(fwnode, NULL);
+
+	parent_info = (struct platform_device_info){
+		.name = DEVICE_NAME,
+		.id = 0,
+		.fwnode = fwnode,
+	};
+
+	parent = kunit_platform_device_register_full(test, &parent_info);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
+
+	child_info = (struct platform_device_info){
+		.parent = &parent->dev,
+		.name = DEVICE_NAME,
+		.id = 1,
+		.fwnode = fwnode,
+		.swnode = &platform_device_test_swnode,
+	};
+
+	child = kunit_platform_device_register_full(test, &child_info);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, child);
+
+	swnode = software_node_fwnode(&platform_device_test_swnode);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, swnode);
+	KUNIT_EXPECT_PTR_EQ(test, fwnode->secondary, swnode);
+
+	get_device(&child->dev);
+	kunit_platform_device_unregister(test, child);
+
+	set_primary_fwnode(&child->dev, NULL);
+	KUNIT_EXPECT_PTR_EQ(test, fwnode->secondary, NULL);
+	KUNIT_EXPECT_PTR_EQ(test, dev_fwnode(&child->dev), swnode);
+
+	put_device(&child->dev);
+
+	KUNIT_EXPECT_PTR_EQ(test, software_node_fwnode(&platform_device_test_swnode),
+			    NULL);
+
+	child = kunit_platform_device_register_full(test, &child_info);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, child);
+
+	kunit_platform_device_unregister(test, child);
+	kunit_platform_device_unregister(test, parent);
+}
+
+/*
+ * Check that releasing an unregistered platform device with a managed
+ * secondary software node detaches and frees the managed node cleanly.
+ */
+static void platform_device_managed_swnode_unregistered_release(struct kunit *test)
+{
+	static const struct property_entry properties[] = {
+		PROPERTY_ENTRY_U32("foo", 42),
+		{ }
+	};
+	struct platform_device *pdev;
+	struct fwnode_handle *fwnode;
+	int ret;
+
+	fwnode = kunit_kzalloc(test, sizeof(*fwnode), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fwnode);
+	fwnode_init(fwnode, NULL);
+
+	pdev = platform_device_alloc(DEVICE_NAME, PLATFORM_DEVID_NONE);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
+
+	platform_device_set_fwnode(pdev, fwnode);
+	ret = device_create_managed_software_node(&pdev->dev, properties, NULL);
+	if (ret)
+		platform_device_put(pdev);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	KUNIT_EXPECT_TRUE(test, is_software_node(fwnode->secondary));
+
+	platform_device_put(pdev);
+
+	KUNIT_EXPECT_PTR_EQ(test, fwnode->secondary, NULL);
+}
+
+/*
+ * Check that calling device_remove_software_node() on a registered platform
+ * device with a managed secondary software node detaches the node cleanly and
+ * subsequent unregistration does not double-put it.
+ */
+static void platform_device_managed_swnode_registered_remove(struct kunit *test)
+{
+	static const struct property_entry properties[] = {
+		PROPERTY_ENTRY_U32("foo", 42),
+		{ }
+	};
+	struct platform_device_info pdevinfo;
+	struct platform_device *pdev;
+	struct fwnode_handle *fwnode;
+
+	fwnode = kunit_kzalloc(test, sizeof(*fwnode), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fwnode);
+	fwnode_init(fwnode, NULL);
+
+	pdevinfo = (struct platform_device_info){
+		.name = DEVICE_NAME,
+		.id = PLATFORM_DEVID_NONE,
+		.fwnode = fwnode,
+		.properties = properties,
+	};
+
+	pdev = kunit_platform_device_register_full(test, &pdevinfo);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
+	KUNIT_ASSERT_TRUE(test, is_software_node(fwnode->secondary));
+
+	device_remove_software_node(&pdev->dev);
+	KUNIT_EXPECT_PTR_EQ(test, fwnode->secondary, NULL);
+
+	kunit_platform_device_unregister(test, pdev);
+}
+
 static struct kunit_case platform_device_swnode_tests[] = {
 	KUNIT_CASE(platform_device_swnode_add_twice),
 	KUNIT_CASE(platform_device_swnode_as_primary),
 	KUNIT_CASE(platform_device_two_swnodes),
+	KUNIT_CASE(platform_device_swnode_shared_primary),
+	KUNIT_CASE(platform_device_swnode_shared_primary_detached),
+	KUNIT_CASE(platform_device_managed_swnode_shared_primary),
+	KUNIT_CASE(platform_device_swnode_child_owned_on_shared_primary),
+	KUNIT_CASE(platform_device_managed_swnode_unregistered_release),
+	KUNIT_CASE(platform_device_managed_swnode_registered_remove),
 	{}
 };
 

-- 
2.56.1.915.g69d5ec7298-goog


  parent reply	other threads:[~2026-12-03  1:35 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03  1:34 [PATCH 0/3] driver core: swnode: fix release with shared primary fwnode Dmitry Torokhov
2026-12-03  1:34 ` [PATCH 1/3] driver core: swnode: only release owned secondary software nodes Dmitry Torokhov
2026-12-05 23:03   ` Danilo Krummrich
2026-12-03  1:34 ` [PATCH 2/3] software node: replace owner pointer with managed bitfield Dmitry Torokhov
2026-12-03  1:34 ` Dmitry Torokhov [this message]
2026-12-03 11:35 ` [PATCH 0/3] driver core: swnode: fix release with shared primary fwnode Andy Shevchenko
2026-12-05 21:00 ` Cesar Cibrian

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202-swnode-shared-primary-fix-v1-3-7ff23caaa1ed@gmail.com \
    --to=dmitry.torokhov@gmail.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=brgl@kernel.org \
    --cc=dakr@kernel.org \
    --cc=djrscally@gmail.com \
    --cc=driver-core@lists.linux.dev \
    --cc=gregkh@linuxfoundation.org \
    --cc=heikki.krogerus@linux.intel.com \
    --cc=johan@kernel.org \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rafael@kernel.org \
    --cc=sakari.ailus@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.