* [PATCH v2 1/3] drivers: base: Don't match devices with NULL of_node/fwnode/etc
2024-12-12 0:31 [PATCH v2 0/3] drivers: base: Don't match device with NULL of_node/fwnode/etc + tests Brian Norris
@ 2024-12-12 0:31 ` Brian Norris
2024-12-13 14:38 ` Rafael J. Wysocki
2024-12-12 0:31 ` [PATCH v2 2/3] drivers: base: test: Enable device model tests with KUNIT_ALL_TESTS Brian Norris
2024-12-12 0:31 ` [PATCH v2 3/3] drivers: base: test: Add ...find_device_by...(... NULL) tests Brian Norris
2 siblings, 1 reply; 7+ messages in thread
From: Brian Norris @ 2024-12-12 0:31 UTC (permalink / raw)
To: Greg Kroah-Hartman
Cc: linux-acpi, linux-kernel, kunit-dev, Maxime Ripard, David Gow,
Rae Moar, linux-kselftest, Rafael J. Wysocki, Rob Herring,
Brian Norris
of_find_device_by_node(), bus_find_device_by_of_node(),
bus_find_device_by_fwnode(), ..., all produce arbitrary results when
provided with a NULL of_node, fwnode, ACPI handle, etc. This is
counterintuitive, and the source of a few bugs, such as the one fixed by
commit 5c8418cf4025 ("PCI/pwrctrl: Unregister platform device only if
one actually exists").
It's hard to imagine a good reason that these device_match_*() APIs
should return 'true' for a NULL argument. Augment these to return 0
(false).
Signed-off-by: Brian Norris <briannorris@chromium.org>
Reviewed-by: Rob Herring (Arm) <robh@kernel.org>
---
Changes in v2:
* Add Rob's Reviewed-by
drivers/base/core.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/drivers/base/core.c b/drivers/base/core.c
index 94865c9d8adc..2b7b13fc36d7 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -5246,13 +5246,13 @@ EXPORT_SYMBOL_GPL(device_match_name);
int device_match_of_node(struct device *dev, const void *np)
{
- return dev->of_node == np;
+ return np && dev->of_node == np;
}
EXPORT_SYMBOL_GPL(device_match_of_node);
int device_match_fwnode(struct device *dev, const void *fwnode)
{
- return dev_fwnode(dev) == fwnode;
+ return fwnode && dev_fwnode(dev) == fwnode;
}
EXPORT_SYMBOL_GPL(device_match_fwnode);
@@ -5264,13 +5264,13 @@ EXPORT_SYMBOL_GPL(device_match_devt);
int device_match_acpi_dev(struct device *dev, const void *adev)
{
- return ACPI_COMPANION(dev) == adev;
+ return adev && ACPI_COMPANION(dev) == adev;
}
EXPORT_SYMBOL(device_match_acpi_dev);
int device_match_acpi_handle(struct device *dev, const void *handle)
{
- return ACPI_HANDLE(dev) == handle;
+ return handle && ACPI_HANDLE(dev) == handle;
}
EXPORT_SYMBOL(device_match_acpi_handle);
--
2.47.0.338.g60cca15819-goog
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH v2 1/3] drivers: base: Don't match devices with NULL of_node/fwnode/etc
2024-12-12 0:31 ` [PATCH v2 1/3] drivers: base: Don't match devices with NULL of_node/fwnode/etc Brian Norris
@ 2024-12-13 14:38 ` Rafael J. Wysocki
0 siblings, 0 replies; 7+ messages in thread
From: Rafael J. Wysocki @ 2024-12-13 14:38 UTC (permalink / raw)
To: Brian Norris
Cc: Greg Kroah-Hartman, linux-acpi, linux-kernel, kunit-dev,
Maxime Ripard, David Gow, Rae Moar, linux-kselftest,
Rafael J. Wysocki, Rob Herring
On Thu, Dec 12, 2024 at 1:32 AM Brian Norris <briannorris@chromium.org> wrote:
>
> of_find_device_by_node(), bus_find_device_by_of_node(),
> bus_find_device_by_fwnode(), ..., all produce arbitrary results when
> provided with a NULL of_node, fwnode, ACPI handle, etc. This is
> counterintuitive, and the source of a few bugs, such as the one fixed by
> commit 5c8418cf4025 ("PCI/pwrctrl: Unregister platform device only if
> one actually exists").
>
> It's hard to imagine a good reason that these device_match_*() APIs
> should return 'true' for a NULL argument. Augment these to return 0
> (false).
>
> Signed-off-by: Brian Norris <briannorris@chromium.org>
> Reviewed-by: Rob Herring (Arm) <robh@kernel.org>
For the ACPI part
Acked-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> ---
>
> Changes in v2:
> * Add Rob's Reviewed-by
>
> drivers/base/core.c | 8 ++++----
> 1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/base/core.c b/drivers/base/core.c
> index 94865c9d8adc..2b7b13fc36d7 100644
> --- a/drivers/base/core.c
> +++ b/drivers/base/core.c
> @@ -5246,13 +5246,13 @@ EXPORT_SYMBOL_GPL(device_match_name);
>
> int device_match_of_node(struct device *dev, const void *np)
> {
> - return dev->of_node == np;
> + return np && dev->of_node == np;
> }
> EXPORT_SYMBOL_GPL(device_match_of_node);
>
> int device_match_fwnode(struct device *dev, const void *fwnode)
> {
> - return dev_fwnode(dev) == fwnode;
> + return fwnode && dev_fwnode(dev) == fwnode;
> }
> EXPORT_SYMBOL_GPL(device_match_fwnode);
>
> @@ -5264,13 +5264,13 @@ EXPORT_SYMBOL_GPL(device_match_devt);
>
> int device_match_acpi_dev(struct device *dev, const void *adev)
> {
> - return ACPI_COMPANION(dev) == adev;
> + return adev && ACPI_COMPANION(dev) == adev;
> }
> EXPORT_SYMBOL(device_match_acpi_dev);
>
> int device_match_acpi_handle(struct device *dev, const void *handle)
> {
> - return ACPI_HANDLE(dev) == handle;
> + return handle && ACPI_HANDLE(dev) == handle;
> }
> EXPORT_SYMBOL(device_match_acpi_handle);
>
> --
> 2.47.0.338.g60cca15819-goog
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 2/3] drivers: base: test: Enable device model tests with KUNIT_ALL_TESTS
2024-12-12 0:31 [PATCH v2 0/3] drivers: base: Don't match device with NULL of_node/fwnode/etc + tests Brian Norris
2024-12-12 0:31 ` [PATCH v2 1/3] drivers: base: Don't match devices with NULL of_node/fwnode/etc Brian Norris
@ 2024-12-12 0:31 ` Brian Norris
2024-12-12 0:31 ` [PATCH v2 3/3] drivers: base: test: Add ...find_device_by...(... NULL) tests Brian Norris
2 siblings, 0 replies; 7+ messages in thread
From: Brian Norris @ 2024-12-12 0:31 UTC (permalink / raw)
To: Greg Kroah-Hartman
Cc: linux-acpi, linux-kernel, kunit-dev, Maxime Ripard, David Gow,
Rae Moar, linux-kselftest, Rafael J. Wysocki, Rob Herring,
Brian Norris
Per commit bebe94b53eb7 ("drivers: base: default KUNIT_* fragments to
KUNIT_ALL_TESTS"), it seems like we should default to KUNIT_ALL_TESTS.
This enables these platform_device tests for common configurations, such
as with:
./tools/testing/kunit/kunit.py run
Signed-off-by: Brian Norris <briannorris@chromium.org>
---
(no changes since v1)
drivers/base/test/Kconfig | 1 +
1 file changed, 1 insertion(+)
diff --git a/drivers/base/test/Kconfig b/drivers/base/test/Kconfig
index 5c7fac80611c..2756870615cc 100644
--- a/drivers/base/test/Kconfig
+++ b/drivers/base/test/Kconfig
@@ -12,6 +12,7 @@ config TEST_ASYNC_DRIVER_PROBE
config DM_KUNIT_TEST
tristate "KUnit Tests for the device model" if !KUNIT_ALL_TESTS
depends on KUNIT
+ default KUNIT_ALL_TESTS
config DRIVER_PE_KUNIT_TEST
tristate "KUnit Tests for property entry API" if !KUNIT_ALL_TESTS
--
2.47.0.338.g60cca15819-goog
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH v2 3/3] drivers: base: test: Add ...find_device_by...(... NULL) tests
2024-12-12 0:31 [PATCH v2 0/3] drivers: base: Don't match device with NULL of_node/fwnode/etc + tests Brian Norris
2024-12-12 0:31 ` [PATCH v2 1/3] drivers: base: Don't match devices with NULL of_node/fwnode/etc Brian Norris
2024-12-12 0:31 ` [PATCH v2 2/3] drivers: base: test: Enable device model tests with KUNIT_ALL_TESTS Brian Norris
@ 2024-12-12 0:31 ` Brian Norris
2024-12-13 11:59 ` Maxime Ripard
2 siblings, 1 reply; 7+ messages in thread
From: Brian Norris @ 2024-12-12 0:31 UTC (permalink / raw)
To: Greg Kroah-Hartman
Cc: linux-acpi, linux-kernel, kunit-dev, Maxime Ripard, David Gow,
Rae Moar, linux-kselftest, Rafael J. Wysocki, Rob Herring,
Brian Norris
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);
+
+ 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>");
--
2.47.0.338.g60cca15819-goog
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH v2 3/3] drivers: base: test: Add ...find_device_by...(... NULL) tests
2024-12-12 0:31 ` [PATCH v2 3/3] drivers: base: test: Add ...find_device_by...(... NULL) tests Brian Norris
@ 2024-12-13 11:59 ` Maxime Ripard
2024-12-13 20:13 ` Brian Norris
0 siblings, 1 reply; 7+ messages in thread
From: Maxime Ripard @ 2024-12-13 11:59 UTC (permalink / raw)
To: Brian Norris
Cc: Greg Kroah-Hartman, linux-acpi, linux-kernel, kunit-dev,
David Gow, Rae Moar, linux-kselftest, Rafael J. Wysocki,
Rob Herring
[-- Attachment #1: Type: text/plain, Size: 2272 bytes --]
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
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 273 bytes --]
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH v2 3/3] drivers: base: test: Add ...find_device_by...(... NULL) tests
2024-12-13 11:59 ` Maxime Ripard
@ 2024-12-13 20:13 ` Brian Norris
0 siblings, 0 replies; 7+ messages in thread
From: Brian Norris @ 2024-12-13 20:13 UTC (permalink / raw)
To: Maxime Ripard
Cc: Greg Kroah-Hartman, linux-acpi, linux-kernel, kunit-dev,
David Gow, Rae Moar, linux-kselftest, Rafael J. Wysocki,
Rob Herring
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
^ permalink raw reply [flat|nested] 7+ messages in thread