mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "CK Hu (胡俊光)" <ck.hu@mediatek.com>
To: "chunkuang.hu@kernel.org" <chunkuang.hu@kernel.org>,
	"make24@iscas.ac.cn" <make24@iscas.ac.cn>,
	"AngeloGioacchino Del Regno"
	<angelogioacchino.delregno@collabora.com>,
	"Nancy Lin (林欣螢)" <Nancy.Lin@mediatek.com>,
	"airlied@gmail.com" <airlied@gmail.com>,
	"simona@ffwll.ch" <simona@ffwll.ch>,
	"p.zabel@pengutronix.de" <p.zabel@pengutronix.de>,
	"matthias.bgg@gmail.com" <matthias.bgg@gmail.com>
Cc: "dri-devel@lists.freedesktop.org"
	<dri-devel@lists.freedesktop.org>,
	"linux-mediatek@lists.infradead.org"
	<linux-mediatek@lists.infradead.org>,
	"stable@vger.kernel.org" <stable@vger.kernel.org>,
	"linux-arm-kernel@lists.infradead.org"
	<linux-arm-kernel@lists.infradead.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"akpm@linux-foundation.org" <akpm@linux-foundation.org>
Subject: Re: [PATCH] drm/mediatek: Fix device/node reference count leaks in mtk_drm_get_all_drm_priv
Date: Wed, 6 Aug 2025 07:24:01 +0000	[thread overview]
Message-ID: <c3d96d02c250eacb1a42199299fa94db82775ca8.camel@mediatek.com> (raw)
In-Reply-To: <20250718033226.3390054-1-make24@iscas.ac.cn>

On Fri, 2025-07-18 at 11:32 +0800, Ma Ke wrote:
> External email : Please do not click links or open attachments until you have verified the sender or the content.
> 
> 
> Using device_find_child() and of_find_device_by_node() to locate
> devices could cause an imbalance in the device's reference count.
> device_find_child() and of_find_device_by_node() both call
> get_device() to increment the reference count of the found device
> before returning the pointer. In mtk_drm_get_all_drm_priv(), these
> references are never released through put_device(), resulting in
> permanent reference count increments. Additionally, the
> for_each_child_of_node() iterator fails to release node references in
> all code paths. This leaks device node references when loop
> termination occurs before reaching MAX_CRTC. These reference count
> leaks may prevent device/node resources from being properly released
> during driver unbind operations.
> 
> As comment of device_find_child() says, 'NOTE: you will need to drop
> the reference with put_device() after use'.
> 
> Found by code review.
> 
> Cc: stable@vger.kernel.org
> Fixes: 1ef7ed48356c ("drm/mediatek: Modify mediatek-drm for mt8195 multi mmsys support")
> Signed-off-by: Ma Ke <make24@iscas.ac.cn>
> ---
>  drivers/gpu/drm/mediatek/mtk_drm_drv.c | 27 +++++++++++++++++---------
>  1 file changed, 18 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/gpu/drm/mediatek/mtk_drm_drv.c b/drivers/gpu/drm/mediatek/mtk_drm_drv.c
> index 7c0c12dde488..c78186debd3e 100644
> --- a/drivers/gpu/drm/mediatek/mtk_drm_drv.c
> +++ b/drivers/gpu/drm/mediatek/mtk_drm_drv.c
> @@ -388,19 +388,24 @@ static bool mtk_drm_get_all_drm_priv(struct device *dev)
> 
>                 of_id = of_match_node(mtk_drm_of_ids, node);
>                 if (!of_id)
> -                       continue;
> +                       goto next;
> 
>                 pdev = of_find_device_by_node(node);
>                 if (!pdev)
> -                       continue;
> +                       goto next;
> 
>                 drm_dev = device_find_child(&pdev->dev, NULL, mtk_drm_match);
> -               if (!drm_dev)
> -                       continue;
> +               if (!drm_dev) {
> +                       put_device(&pdev->dev);
> +                       goto next;

I've define label next_put_device_pdev_dev below, so

goto next_put_device_pdev_dev;

> +               }
> 
>                 temp_drm_priv = dev_get_drvdata(drm_dev);
> -               if (!temp_drm_priv)
> -                       continue;
> +               if (!temp_drm_priv) {
> +                       put_device(drm_dev);
> +                       put_device(&pdev->dev);
> +                       goto next;


I've define label next_put_device_drm_dev below, so

goto next_put_device_drm_dev;

> +               }
> 
>                 if (temp_drm_priv->data->main_len)
>                         all_drm_priv[CRTC_MAIN] = temp_drm_priv;
> @@ -412,10 +417,14 @@ static bool mtk_drm_get_all_drm_priv(struct device *dev)
>                 if (temp_drm_priv->mtk_drm_bound)
>                         cnt++;
> 
> -               if (cnt == MAX_CRTC) {
> -                       of_node_put(node);

next_put_device_drm_dev:

> +               put_device(drm_dev);

next_put_device_pdev_dev:

> +               put_device(&pdev->dev);
> +
> +next:

next_put_node:

Regards,
CK

> +               of_node_put(node);
> +
> +               if (cnt == MAX_CRTC)
>                         break;
> -               }
>         }
> 
>         if (drm_priv->data->mmsys_dev_num == cnt) {
> --
> 2.25.1
> 
> 


      reply	other threads:[~2025-08-06  7:24 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-18  3:32 Ma Ke
2025-08-06  7:24 ` CK Hu (胡俊光) [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=c3d96d02c250eacb1a42199299fa94db82775ca8.camel@mediatek.com \
    --to=ck.hu@mediatek.com \
    --cc=Nancy.Lin@mediatek.com \
    --cc=airlied@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=angelogioacchino.delregno@collabora.com \
    --cc=chunkuang.hu@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mediatek@lists.infradead.org \
    --cc=make24@iscas.ac.cn \
    --cc=matthias.bgg@gmail.com \
    --cc=p.zabel@pengutronix.de \
    --cc=simona@ffwll.ch \
    --cc=stable@vger.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

all inboxes | Powered by JetHome®