The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH v2] mfd: cros_ec: Drop OF node reference in ec_device_probe()
@ 2026-11-18  6:29 Xiangwu Lai
  2026-11-18 17:20 ` Benson Leung
  0 siblings, 1 reply; 2+ messages in thread
From: Xiangwu Lai @ 2026-11-18  6:29 UTC (permalink / raw)
  To: bleung, lee, tzungbi
  Cc: groeck, jthies, chrome-platform, mfd, linux-kernel, Xiangwu Lai

ec_device_probe() calls of_find_compatible_node() only to check
whether a "google,cros-ec-ucsi" node exists in the device tree, but
the reference held by the returned node is never dropped.

Scope the lookup to the UCSI block and mark the node pointer with
__free(device_node), so that the reference is released automatically
when the block is left.  Keeping the lookup inside the block also
preserves the short-circuit evaluation of the original condition:
the device tree is only walked when the EC_FEATURE_USB_PD,
EC_FEATURE_UCSI_PPM and ACPI checks already passed, instead of on
every probe of every cros_ec device.

Fixes: 17f95281839d ("mfd: cros_ec: Don't add cros_ec_ucsi if it is defined in OF or ACPI")
Suggested-by: Benson Leung <bleung@chromium.org>
Signed-off-by: Xiangwu Lai <laixiangwu@cmss.chinamobile.com>
---

Changes in v2:
- Scope the lookup to the UCSI block with __free(device_node) instead
  of a function-scope variable with a manual of_node_put() before the
  condition, per Benson's review: the v1 lookup ran on every probe
  (breaking the short-circuit evaluation) and the node pointer was
  tested after its reference had already been dropped.

Link: https://lore.kernel.org/r/20261118013935.1573-1-laixiangwu@cmss.chinamobile.com

 drivers/mfd/cros_ec_dev.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/drivers/mfd/cros_ec_dev.c b/drivers/mfd/cros_ec_dev.c
index 529a23b80..b8d363b7a 100644
--- a/drivers/mfd/cros_ec_dev.c
+++ b/drivers/mfd/cros_ec_dev.c
@@ -271,14 +271,20 @@ static int ec_device_probe(struct platform_device *pdev)
 	 */
 	if (cros_ec_check_features(ec, EC_FEATURE_USB_PD) &&
 	    cros_ec_check_features(ec, EC_FEATURE_UCSI_PPM) &&
-	    !acpi_dev_found("GOOG0021") &&
-	    !of_find_compatible_node(NULL, NULL, "google,cros-ec-ucsi")) {
-		retval = mfd_add_hotplug_devices(ec->dev,
-						 cros_ec_ucsi_cells,
-						 ARRAY_SIZE(cros_ec_ucsi_cells));
+	    !acpi_dev_found("GOOG0021")) {
+		struct device_node *np __free(device_node) =
+			of_find_compatible_node(NULL, NULL, "google,cros-ec-ucsi");
 
-		if (retval)
-			dev_warn(ec->dev, "failed to add cros_ec_ucsi: %d\n", retval);
+		if (!np) {
+			retval = mfd_add_hotplug_devices(ec->dev,
+							  cros_ec_ucsi_cells,
+							  ARRAY_SIZE(cros_ec_ucsi_cells));
+
+			if (retval)
+				dev_warn(ec->dev,
+					 "failed to add cros_ec_ucsi: %d\n",
+					 retval);
+		}
 	}
 
 	/*
-- 
2.50.1.windows.1




^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] mfd: cros_ec: Drop OF node reference in ec_device_probe()
  2026-11-18  6:29 [PATCH v2] mfd: cros_ec: Drop OF node reference in ec_device_probe() Xiangwu Lai
@ 2026-11-18 17:20 ` Benson Leung
  0 siblings, 0 replies; 2+ messages in thread
From: Benson Leung @ 2026-11-18 17:20 UTC (permalink / raw)
  To: Xiangwu Lai
  Cc: bleung, lee, tzungbi, groeck, jthies, chrome-platform, mfd,
	linux-kernel

[-- Attachment #1: Type: text/plain, Size: 2821 bytes --]

Hi Xiangwu,

Thanks for the follow up. Looks good.

On Wed, Nov 18, 2026 at 02:29:48PM +0800, Xiangwu Lai wrote:
> ec_device_probe() calls of_find_compatible_node() only to check
> whether a "google,cros-ec-ucsi" node exists in the device tree, but
> the reference held by the returned node is never dropped.
> 
> Scope the lookup to the UCSI block and mark the node pointer with
> __free(device_node), so that the reference is released automatically
> when the block is left.  Keeping the lookup inside the block also
> preserves the short-circuit evaluation of the original condition:
> the device tree is only walked when the EC_FEATURE_USB_PD,
> EC_FEATURE_UCSI_PPM and ACPI checks already passed, instead of on
> every probe of every cros_ec device.
> 
> Fixes: 17f95281839d ("mfd: cros_ec: Don't add cros_ec_ucsi if it is defined in OF or ACPI")
> Suggested-by: Benson Leung <bleung@chromium.org>
> Signed-off-by: Xiangwu Lai <laixiangwu@cmss.chinamobile.com>

Reviewed-by: Benson Leung <bleung@chromium.org>

> ---
> 
> Changes in v2:
> - Scope the lookup to the UCSI block with __free(device_node) instead
>   of a function-scope variable with a manual of_node_put() before the
>   condition, per Benson's review: the v1 lookup ran on every probe
>   (breaking the short-circuit evaluation) and the node pointer was
>   tested after its reference had already been dropped.
> 
> Link: https://lore.kernel.org/r/20261118013935.1573-1-laixiangwu@cmss.chinamobile.com
> 
>  drivers/mfd/cros_ec_dev.c | 20 +++++++++++++-------
>  1 file changed, 13 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/mfd/cros_ec_dev.c b/drivers/mfd/cros_ec_dev.c
> index 529a23b80..b8d363b7a 100644
> --- a/drivers/mfd/cros_ec_dev.c
> +++ b/drivers/mfd/cros_ec_dev.c
> @@ -271,14 +271,20 @@ static int ec_device_probe(struct platform_device *pdev)
>  	 */
>  	if (cros_ec_check_features(ec, EC_FEATURE_USB_PD) &&
>  	    cros_ec_check_features(ec, EC_FEATURE_UCSI_PPM) &&
> -	    !acpi_dev_found("GOOG0021") &&
> -	    !of_find_compatible_node(NULL, NULL, "google,cros-ec-ucsi")) {
> -		retval = mfd_add_hotplug_devices(ec->dev,
> -						 cros_ec_ucsi_cells,
> -						 ARRAY_SIZE(cros_ec_ucsi_cells));
> +	    !acpi_dev_found("GOOG0021")) {
> +		struct device_node *np __free(device_node) =
> +			of_find_compatible_node(NULL, NULL, "google,cros-ec-ucsi");
>  
> -		if (retval)
> -			dev_warn(ec->dev, "failed to add cros_ec_ucsi: %d\n", retval);
> +		if (!np) {
> +			retval = mfd_add_hotplug_devices(ec->dev,
> +							  cros_ec_ucsi_cells,
> +							  ARRAY_SIZE(cros_ec_ucsi_cells));
> +
> +			if (retval)
> +				dev_warn(ec->dev,
> +					 "failed to add cros_ec_ucsi: %d\n",
> +					 retval);
> +		}
>  	}
>  
>  	/*
> -- 
> 2.50.1.windows.1
> 
> 
> 

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-18 17:20 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18  6:29 [PATCH v2] mfd: cros_ec: Drop OF node reference in ec_device_probe() Xiangwu Lai
2026-11-18 17:20 ` Benson Leung

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox