Chrome platform driver development
 help / color / mirror / Atom feed
From: Benson Leung <bleung@chromium.org>
To: Xiangwu Lai <laixiangwu@cmss.chinamobile.com>
Cc: bleung@chromium.org, lee@kernel.org, tzungbi@kernel.org,
	groeck@chromium.org, jthies@google.com,
	chrome-platform@lists.linux.dev, mfd@lists.linux.dev,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] mfd: cros_ec: Drop OF node reference in ec_device_probe()
Date: Tue, 17 Nov 2026 19:48:20 -0800	[thread overview]
Message-ID: <av0gBCp63QZ2F8gy@google.com> (raw)
In-Reply-To: <20261118013935.1573-1-laixiangwu@cmss.chinamobile.com>

[-- 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 --]

      reply	other threads:[~2026-11-18  3:48 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=av0gBCp63QZ2F8gy@google.com \
    --to=bleung@chromium.org \
    --cc=chrome-platform@lists.linux.dev \
    --cc=groeck@chromium.org \
    --cc=jthies@google.com \
    --cc=laixiangwu@cmss.chinamobile.com \
    --cc=lee@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mfd@lists.linux.dev \
    --cc=tzungbi@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox