mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
To: Brendan Higgins <brendan.higgins@linux.dev>,
	David Gow <david@davidgow.net>, Rae Moar <raemoar63@gmail.com>,
	Linus Walleij <linusw@kernel.org>,
	Bartosz Golaszewski <brgl@kernel.org>
Cc: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com,
	linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org,
	Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Subject: [PATCH v3 2/3] kunit: provide kunit_platform_device_unregister()
Date: Fri, 22 May 2026 15:42:17 +0200	[thread overview]
Message-ID: <20260522-gpiolib-kunit-v3-2-b15fe6987430@oss.qualcomm.com> (raw)
In-Reply-To: <20260522-gpiolib-kunit-v3-0-b15fe6987430@oss.qualcomm.com>

Tests may want to unregister a platform device as part of the test case
logic. Using the regular platform_device_register() with kunit
assertions may result in a platform device leak or otherwise requires
cumbersome error handling. Provide a function that unregisters a
kunit-managed platform device and drops the release action from the
test's list.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
---
 include/kunit/platform_device.h |  2 ++
 lib/kunit/platform.c            | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/include/kunit/platform_device.h b/include/kunit/platform_device.h
index 8cad6e1c3e7efba862862b579089f2f317784a73..eee565d5d1d35c1d1bc82b45eb91d21d00c68428 100644
--- a/include/kunit/platform_device.h
+++ b/include/kunit/platform_device.h
@@ -14,6 +14,8 @@ int kunit_platform_device_add(struct kunit *test, struct platform_device *pdev);
 struct platform_device *
 kunit_platform_device_register_full(struct kunit *test,
 				    const struct platform_device_info *pdevinfo);
+void kunit_platform_device_unregister(struct kunit *test,
+				      struct platform_device *pdev);
 
 int kunit_platform_device_prepare_wait_for_probe(struct kunit *test,
 						 struct platform_device *pdev,
diff --git a/lib/kunit/platform.c b/lib/kunit/platform.c
index 583b50b538c79599ebbf33e261fe2e9ced35efa9..737758d710b2839fab29c5cbcf3bc5ba00e20094 100644
--- a/lib/kunit/platform.c
+++ b/lib/kunit/platform.c
@@ -161,6 +161,39 @@ kunit_platform_device_register_full(struct kunit *test,
 }
 EXPORT_SYMBOL_GPL(kunit_platform_device_register_full);
 
+static bool
+kunit_platform_device_add_match(struct kunit *test, struct kunit_resource *res,
+				void *match_data)
+{
+	struct platform_device *pdev = match_data;
+
+	return res->data == pdev && res->free == kunit_platform_device_add_exit;
+}
+
+/**
+ * kunit_platform_device_unregister() - Unregister a KUnit-managed platform device
+ * @test: test context
+ * @pdev: platform device to unregister
+ *
+ * Unregister a test-managed platform device and cancel its release action.
+ */
+void kunit_platform_device_unregister(struct kunit *test,
+				      struct platform_device *pdev)
+{
+	struct kunit_resource *res;
+
+	res = kunit_find_resource(test, kunit_platform_device_add_match, pdev);
+	if (res) {
+		res->free = NULL;
+		kunit_put_resource(res);
+	} else {
+		kunit_remove_action(test, platform_device_unregister_wrapper, pdev);
+	}
+
+	platform_device_unregister(pdev);
+}
+EXPORT_SYMBOL_GPL(kunit_platform_device_unregister);
+
 struct kunit_platform_device_probe_nb {
 	struct completion *x;
 	struct device *dev;

-- 
2.47.3


  parent reply	other threads:[~2026-05-22 13:42 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-22 13:42 [PATCH v3 0/3] gpio: add kunit tests for GPIO core Bartosz Golaszewski
2026-05-22 13:42 ` [PATCH v3 1/3] kunit: provide kunit_platform_device_register_full() Bartosz Golaszewski
2026-05-26  8:58   ` David Gow
2026-05-22 13:42 ` Bartosz Golaszewski [this message]
2026-05-26  8:58   ` [PATCH v3 2/3] kunit: provide kunit_platform_device_unregister() David Gow
2026-05-22 13:42 ` [PATCH v3 3/3] gpio: add kunit test cases for the GPIO subsystem Bartosz Golaszewski
2026-05-26  8:58   ` David Gow
2026-05-26  9:19     ` Bartosz Golaszewski
2026-05-25 13:33 ` [PATCH v3 0/3] gpio: add kunit tests for GPIO core Linus Walleij
2026-05-26  9:20 ` Bartosz Golaszewski

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=20260522-gpiolib-kunit-v3-2-b15fe6987430@oss.qualcomm.com \
    --to=bartosz.golaszewski@oss.qualcomm.com \
    --cc=brendan.higgins@linux.dev \
    --cc=brgl@kernel.org \
    --cc=david@davidgow.net \
    --cc=kunit-dev@googlegroups.com \
    --cc=linusw@kernel.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=raemoar63@gmail.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®