The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] mfd: cros_ec: Drop OF node reference in ec_device_probe()
@ 2026-11-18  1:39 Xiangwu Lai
  2026-11-18  3:48 ` Benson Leung
  0 siblings, 1 reply; 2+ messages in thread
From: Xiangwu Lai @ 2026-11-18  1:39 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.

Keep the node pointer in a local variable and release the reference
with of_node_put() right after the lookup.

Fixes: 17f95281839d ("mfd: cros_ec: Don't add cros_ec_ucsi if it is defined in OF or ACPI")
Signed-off-by: Xiangwu Lai <laixiangwu@cmss.chinamobile.com>
---
 drivers/mfd/cros_ec_dev.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/mfd/cros_ec_dev.c b/drivers/mfd/cros_ec_dev.c
index 529a23b80..593297df5 100644
--- a/drivers/mfd/cros_ec_dev.c
+++ b/drivers/mfd/cros_ec_dev.c
@@ -184,6 +184,7 @@ static int ec_device_probe(struct platform_device *pdev)
 	struct device *dev = &pdev->dev;
 	struct cros_ec_platform *ec_platform = dev_get_platdata(dev);
 	struct cros_ec_dev *ec = kzalloc_obj(*ec);
+	struct device_node *np;
 	struct ec_response_pchg_count pchg_count;
 	int i;
 
@@ -269,10 +270,12 @@ static int ec_device_probe(struct platform_device *pdev)
 	 * the required nodes. On PDC systems without FW nodes for cros_ec_ucsi,
 	 * the driver should be added as an mfd subdevice.
 	 */
+	np = of_find_compatible_node(NULL, NULL, "google,cros-ec-ucsi");
+	of_node_put(np);
+
 	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")) {
+	    !acpi_dev_found("GOOG0021") && !np) {
 		retval = mfd_add_hotplug_devices(ec->dev,
 						 cros_ec_ucsi_cells,
 						 ARRAY_SIZE(cros_ec_ucsi_cells));
-- 
2.50.1.windows.1




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

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

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

Hi Xiangwu,

Thanks for catching this OF node reference leak problem. A few comments below.

On Wed, Nov 18, 2026 at 09:39:34AM +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.
> 
> Keep the node pointer in a local variable and release the reference
> with of_node_put() right after the lookup.
> 
> Fixes: 17f95281839d ("mfd: cros_ec: Don't add cros_ec_ucsi if it is defined in OF or ACPI")
> Signed-off-by: Xiangwu Lai <laixiangwu@cmss.chinamobile.com>
> ---
>  drivers/mfd/cros_ec_dev.c | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/mfd/cros_ec_dev.c b/drivers/mfd/cros_ec_dev.c
> index 529a23b80..593297df5 100644
> --- a/drivers/mfd/cros_ec_dev.c
> +++ b/drivers/mfd/cros_ec_dev.c
> @@ -184,6 +184,7 @@ static int ec_device_probe(struct platform_device *pdev)
>  	struct device *dev = &pdev->dev;
>  	struct cros_ec_platform *ec_platform = dev_get_platdata(dev);
>  	struct cros_ec_dev *ec = kzalloc_obj(*ec);
> +	struct device_node *np;
>  	struct ec_response_pchg_count pchg_count;
>  	int i;
>  

ec_device_probe() already has a "struct device_node *node;" declared
immediately before this context. Please don't add another device_node pointer
here just for this. Instead, scope it to the block below using
"__free(device_node)".

> @@ -269,10 +270,12 @@ static int ec_device_probe(struct platform_device *pdev)
>  	 * the required nodes. On PDC systems without FW nodes for cros_ec_ucsi,
>  	 * the driver should be added as an mfd subdevice.
>  	 */
> +	np = of_find_compatible_node(NULL, NULL, "google,cros-ec-ucsi");
> +	of_node_put(np);
> +
>  	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")) {
> +	    !acpi_dev_found("GOOG0021") && !np) {
>  		retval = mfd_add_hotplug_devices(ec->dev,
>  						 cros_ec_ucsi_cells,
>  						 ARRAY_SIZE(cros_ec_ucsi_cells));

Moving the of_find_compatible_node() to before the "if" condition breaks
the short-circuit evaluation, causing every ec_device_probe() call (including
secondary MCUs such as FP/ISH/SCP/TP and non-UCSI Chromebooks) to walk
the entire Device Tree unnecessarily looking for "google,cros-ec-ucsi".

We want to only run this search if we passed the other 3 checks in the if.

Also, testing "!np" after calling of_node_put(np) evaluates a pointer after
its reference has already been dropped.

Instead, please look into using the automatic cleanup attribute
__free(device_node) added in:
9448e55d032d ("of: Add cleanup.h based auto release via __free(device_node) markings")

Scope the device_node pointer inside of the block (so we keep the short circuit)
and the cleanup will automatically drop the reference when leaving scope.

Something like this:
	if (cros_ec_check_features(ec, EC_FEATURE_USB_PD) &&
	    cros_ec_check_features(ec, EC_FEATURE_UCSI_PPM) &&
	    !acpi_dev_found("GOOG0021")) {
		struct device_node *np __free(device_node) =
			of_find_compatible_node(NULL, NULL,
						"google,cros-ec-ucsi");
		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);
		}
	}

Thanks!
Benson
 

[-- 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  3:48 UTC | newest]

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

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