From: Rob Herring <robh@kernel.org>
To: Ma Ke <make24@iscas.ac.cn>
Cc: saravanak@google.com, lizhi.hou@amd.com,
devicetree@vger.kernel.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: Re: [PATCH] of: unittest: Fix device reference count leak in of_unittest_pci_node_verify
Date: Mon, 22 Sep 2025 15:20:14 -0500 [thread overview]
Message-ID: <20250922202014.GA1254345-robh@kernel.org> (raw)
In-Reply-To: <20250920085135.21835-1-make24@iscas.ac.cn>
On Sat, Sep 20, 2025 at 04:51:35PM +0800, Ma Ke wrote:
> In of_unittest_pci_node_verify(), when the add parameter is false,
> device_find_any_child() obtains a reference to a child device. This
> function implicitly calls get_device() to increment the device's
> reference count before returning the pointer. However, the caller
> fails to properly release this reference by calling put_device(),
> leading to a device reference count leak.
>
> As the comment of device_find_any_child states: "NOTE: you will need
> to drop the reference with put_device() after use".
>
> Cc: stable@vger.kernel.org
> Fixes: 26409dd04589 ("of: unittest: Add pci_dt_testdrv pci driver")
> Signed-off-by: Ma Ke <make24@iscas.ac.cn>
> ---
> drivers/of/unittest.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/of/unittest.c b/drivers/of/unittest.c
> index e3503ec20f6c..d225e73781fe 100644
> --- a/drivers/of/unittest.c
> +++ b/drivers/of/unittest.c
> @@ -4271,7 +4271,7 @@ static struct platform_driver unittest_pci_driver = {
> static int of_unittest_pci_node_verify(struct pci_dev *pdev, bool add)
> {
> struct device_node *pnp, *np = NULL;
> - struct device *child_dev;
> + struct device *child_dev = NULL;
> char *path = NULL;
> const __be32 *reg;
> int rc = 0;
> @@ -4306,6 +4306,8 @@ static int of_unittest_pci_node_verify(struct pci_dev *pdev, bool add)
> kfree(path);
> if (np)
> of_node_put(np);
> + if (child_dev)
> + put_device(child_dev);
This can go in the else clause. Then child_dev doesn't need to be
initialized to NULL.
>
> return rc;
> }
> --
> 2.17.1
>
prev parent reply other threads:[~2025-09-22 20:20 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-20 8:51 [PATCH] of: unittest: Fix device reference count leak in of_unittest_pci_node_verify Ma Ke
2025-09-22 20:20 ` Rob Herring [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=20250922202014.GA1254345-robh@kernel.org \
--to=robh@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lizhi.hou@amd.com \
--cc=make24@iscas.ac.cn \
--cc=saravanak@google.com \
--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;
as well as URLs for NNTP newsgroup(s).