diff mbox series

[v2,3/3] drivers: base: test: Add ...find_device_by...(... NULL) tests

Message ID 20241212003201.2098123-4-briannorris@chromium.org (mailing list archive)
State Handled Elsewhere, archived
Headers show
Series drivers: base: Don't match device with NULL of_node/fwnode/etc + tests | expand

Commit Message

Brian Norris Dec. 12, 2024, 12:31 a.m. UTC
We recently updated these device_match*() (and therefore, various
*find_device_by*()) functions to return a consistent 'false' value when
trying to match a NULL handle. Add tests for this.

This provides regression-testing coverage for the sorts of bugs that
underly commit 5c8418cf4025 ("PCI/pwrctrl: Unregister platform device
only if one actually exists").

Signed-off-by: Brian Norris <briannorris@chromium.org>
---

Changes in v2:
 * Keep "devm" and "match" tests in separate suites

 drivers/base/test/platform-device-test.c | 42 +++++++++++++++++++++++-
 1 file changed, 41 insertions(+), 1 deletion(-)

Comments

Maxime Ripard Dec. 13, 2024, 11:59 a.m. UTC | #1
Hi,

On Wed, Dec 11, 2024 at 04:31:41PM -0800, Brian Norris wrote:
> We recently updated these device_match*() (and therefore, various
> *find_device_by*()) functions to return a consistent 'false' value when
> trying to match a NULL handle. Add tests for this.
> 
> This provides regression-testing coverage for the sorts of bugs that
> underly commit 5c8418cf4025 ("PCI/pwrctrl: Unregister platform device
> only if one actually exists").
> 
> Signed-off-by: Brian Norris <briannorris@chromium.org>
> ---
> 
> Changes in v2:
>  * Keep "devm" and "match" tests in separate suites
> 
>  drivers/base/test/platform-device-test.c | 42 +++++++++++++++++++++++-
>  1 file changed, 41 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/base/test/platform-device-test.c b/drivers/base/test/platform-device-test.c
> index ea05b8785743..c8d4b0a385f2 100644
> --- a/drivers/base/test/platform-device-test.c
> +++ b/drivers/base/test/platform-device-test.c
> @@ -3,6 +3,8 @@
>  #include <kunit/resource.h>
>  
>  #include <linux/device.h>
> +#include <linux/device/bus.h>
> +#include <linux/of_platform.h>
>  #include <linux/platform_device.h>
>  
>  #define DEVICE_NAME "test"
> @@ -217,7 +219,45 @@ static struct kunit_suite platform_device_devm_test_suite = {
>  	.test_cases = platform_device_devm_tests,
>  };
>  
> -kunit_test_suite(platform_device_devm_test_suite);
> +static void platform_device_find_by_null_test(struct kunit *test)
> +{
> +	struct platform_device *pdev;
> +	int ret;
> +
> +	pdev = platform_device_alloc(DEVICE_NAME, PLATFORM_DEVID_NONE);
> +	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
> +
> +	ret = platform_device_add(pdev);
> +	KUNIT_ASSERT_EQ(test, ret, 0);

I *think* you have a bug there: if platform_device_add fails,
KUNIT_ASSERT will stop the test execution and thus you will leak the
platform_device you just allocated.

You need to call platform_device_put in such a case, but if
platform_device_add succeeds then you need to call
platform_device_unregister instead.

It would be better to use kunit_platform_device_alloc and
kunit_platform_device_add that already deal with this.

The rest looks good to me, once fixed:
Reviewed-by: Maxime Ripard <mripard@kernel.org>

Maxime
Brian Norris Dec. 13, 2024, 8:13 p.m. UTC | #2
Hi Maxime,

On Fri, Dec 13, 2024 at 12:59:57PM +0100, Maxime Ripard wrote:
> On Wed, Dec 11, 2024 at 04:31:41PM -0800, Brian Norris wrote:
> > --- a/drivers/base/test/platform-device-test.c
> > +++ b/drivers/base/test/platform-device-test.c

> > @@ -217,7 +219,45 @@ static struct kunit_suite platform_device_devm_test_suite = {
> >  	.test_cases = platform_device_devm_tests,
> >  };
> >  
> > -kunit_test_suite(platform_device_devm_test_suite);
> > +static void platform_device_find_by_null_test(struct kunit *test)
> > +{
> > +	struct platform_device *pdev;
> > +	int ret;
> > +
> > +	pdev = platform_device_alloc(DEVICE_NAME, PLATFORM_DEVID_NONE);
> > +	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
> > +
> > +	ret = platform_device_add(pdev);
> > +	KUNIT_ASSERT_EQ(test, ret, 0);
> 
> I *think* you have a bug there: if platform_device_add fails,
> KUNIT_ASSERT will stop the test execution and thus you will leak the
> platform_device you just allocated.
> 
> You need to call platform_device_put in such a case, but if
> platform_device_add succeeds then you need to call
> platform_device_unregister instead.

Hehe, well I'm imitating the existing leaks in the other tests in this
file, then ;) But admittedly, those are a little more complex, because
the unregistration is actually part of the test flow.

> It would be better to use kunit_platform_device_alloc and
> kunit_platform_device_add that already deal with this.

Cool, thanks, I'll use those in v3 for my new test.

> The rest looks good to me, once fixed:
> Reviewed-by: Maxime Ripard <mripard@kernel.org>

Thanks for the tips and review.

Brian
diff mbox series

Patch

diff --git a/drivers/base/test/platform-device-test.c b/drivers/base/test/platform-device-test.c
index ea05b8785743..c8d4b0a385f2 100644
--- a/drivers/base/test/platform-device-test.c
+++ b/drivers/base/test/platform-device-test.c
@@ -3,6 +3,8 @@ 
 #include <kunit/resource.h>
 
 #include <linux/device.h>
+#include <linux/device/bus.h>
+#include <linux/of_platform.h>
 #include <linux/platform_device.h>
 
 #define DEVICE_NAME "test"
@@ -217,7 +219,45 @@  static struct kunit_suite platform_device_devm_test_suite = {
 	.test_cases = platform_device_devm_tests,
 };
 
-kunit_test_suite(platform_device_devm_test_suite);
+static void platform_device_find_by_null_test(struct kunit *test)
+{
+	struct platform_device *pdev;
+	int ret;
+
+	pdev = platform_device_alloc(DEVICE_NAME, PLATFORM_DEVID_NONE);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
+
+	ret = platform_device_add(pdev);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	KUNIT_EXPECT_PTR_EQ(test, of_find_device_by_node(NULL), NULL);
+
+	KUNIT_EXPECT_PTR_EQ(test, bus_find_device_by_of_node(&platform_bus_type, NULL), NULL);
+	KUNIT_EXPECT_PTR_EQ(test, bus_find_device_by_fwnode(&platform_bus_type, NULL), NULL);
+	KUNIT_EXPECT_PTR_EQ(test, bus_find_device_by_acpi_dev(&platform_bus_type, NULL), NULL);
+
+	KUNIT_EXPECT_FALSE(test, device_match_of_node(&pdev->dev, NULL));
+	KUNIT_EXPECT_FALSE(test, device_match_fwnode(&pdev->dev, NULL));
+	KUNIT_EXPECT_FALSE(test, device_match_acpi_dev(&pdev->dev, NULL));
+	KUNIT_EXPECT_FALSE(test, device_match_acpi_handle(&pdev->dev, NULL));
+
+	platform_device_unregister(pdev);
+}
+
+static struct kunit_case platform_device_match_tests[] = {
+	KUNIT_CASE(platform_device_find_by_null_test),
+	{}
+};
+
+static struct kunit_suite platform_device_match_test_suite = {
+	.name = "platform-device-match",
+	.test_cases = platform_device_match_tests,
+};
+
+kunit_test_suites(
+	&platform_device_devm_test_suite,
+	&platform_device_match_test_suite,
+);
 
 MODULE_DESCRIPTION("Test module for platform devices");
 MODULE_AUTHOR("Maxime Ripard <mripard@kernel.org>");