diff mbox series

[v3,1/3] drivers: base: Don't match devices with NULL of_node/fwnode/etc

Message ID 20241216201148.535115-2-briannorris@chromium.org
State New
Headers show
Series drivers: base: Don't match device with NULL of_node/fwnode/etc + tests | expand

Commit Message

Brian Norris Dec. 16, 2024, 8:11 p.m. UTC
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).

Reviewed-by: Rob Herring (Arm) <robh@kernel.org>
Acked-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Brian Norris <briannorris@chromium.org>
---

Changes in v3:
 * Add Rafael's Acked-by

Changes in v2:
 * Add Rob's Reviewed-by

 drivers/base/core.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Comments

David Gow Dec. 19, 2024, 5:45 a.m. UTC | #1
On Tue, 17 Dec 2024 at 04:12, 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).
>
> Reviewed-by: Rob Herring (Arm) <robh@kernel.org>
> Acked-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> Signed-off-by: Brian Norris <briannorris@chromium.org>
> ---

Seems sensible enough to me.

Acked-by: David Gow <davidgow@google.com>

I assume this series (including the KUnit test changes) will go in via Greg.

Cheers,
-- David

>
> Changes in v3:
>  * Add Rafael's Acked-by
>
> 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.1.613.gc27f4b7a9f-goog
>
Shuah Khan Dec. 20, 2024, 5:33 p.m. UTC | #2
On 12/18/24 22:45, David Gow wrote:
> On Tue, 17 Dec 2024 at 04:12, 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).
>>
>> Reviewed-by: Rob Herring (Arm) <robh@kernel.org>
>> Acked-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
>> Signed-off-by: Brian Norris <briannorris@chromium.org>
>> ---
> 
> Seems sensible enough to me.
> 
> Acked-by: David Gow <davidgow@google.com>
> 
> I assume this series (including the KUnit test changes) will go in via Greg.
> 

Works for me.

Acked-by: Shuah Khan <skhan@linuxfoundation.org>

thanks,
-- Shuah
Greg Kroah-Hartman Dec. 20, 2024, 5:44 p.m. UTC | #3
On Fri, Dec 20, 2024 at 10:33:51AM -0700, Shuah Khan wrote:
> On 12/18/24 22:45, David Gow wrote:
> > On Tue, 17 Dec 2024 at 04:12, 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).
> > > 
> > > Reviewed-by: Rob Herring (Arm) <robh@kernel.org>
> > > Acked-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> > > Signed-off-by: Brian Norris <briannorris@chromium.org>
> > > ---
> > 
> > Seems sensible enough to me.
> > 
> > Acked-by: David Gow <davidgow@google.com>
> > 
> > I assume this series (including the KUnit test changes) will go in via Greg.
> > 
> 
> Works for me.
> 
> Acked-by: Shuah Khan <skhan@linuxfoundation.org>

Thanks, I'll take these in a few days.

greg k-h
diff mbox series

Patch

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);