From: Rob Gardner <rob.gardner@oracle.com>
To: Ma Ke <make24@iscas.ac.cn>,
davem@davemloft.net, andreas@gaisler.com, sam@ravnborg.org,
dawei.li@shingroup.cn
Cc: sparclinux@vger.kernel.org, linux-kernel@vger.kernel.org,
akpm@linux-foundation.org, stable@vger.kernel.org
Subject: Re: [PATCH RESEND] sparc: fix error handling in scan_one_device()
Date: Mon, 14 Apr 2025 12:07:44 -0600 [thread overview]
Message-ID: <82791292-9caf-41b6-8d63-1190ea59e559@oracle.com> (raw)
In-Reply-To: <20250414111845.3084334-1-make24@iscas.ac.cn>
On 4/14/25 05:18, Ma Ke wrote:
> Once of_device_register() failed, we should call put_device() to
> decrement reference count for cleanup. Or it could cause memory leak.
> So fix this by calling put_device(), then the name can be freed in
> kobject_cleanup().
This seems ok but it's not clear why you delete "kfree(op)" here.
scan_one_device() allocates the memory for op via kzalloc, and in the
case where of_device_register() fails, it sets op = NULL. This
superficially looks like you *create* a memory leak here, since I don't
see any other obvious reference to that kzalloc'd memory after
scan_one_device() returns. Is the "op" pointer saved somewhere quietly
by build_device_resources()?
I think you should still free the memory (that you allocated) after the
call to put_device(), or explain why this is not necessary.
>
> As comment of device_add() says, 'if device_add() succeeds, you should
> call device_del() when you want to get rid of it. If device_add() has
> not succeeded, use only put_device() to drop the reference count'.
>
> Found by code review.
>
> Cc: stable@vger.kernel.org
> Fixes: cf44bbc26cf1 ("[SPARC]: Beginnings of generic of_device framework.")
> Signed-off-by: Ma Ke <make24@iscas.ac.cn>
> ---
> arch/sparc/kernel/of_device_64.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/arch/sparc/kernel/of_device_64.c b/arch/sparc/kernel/of_device_64.c
> index f98c2901f335..4272746d7166 100644
> --- a/arch/sparc/kernel/of_device_64.c
> +++ b/arch/sparc/kernel/of_device_64.c
> @@ -677,7 +677,7 @@ static struct platform_device * __init scan_one_device(struct device_node *dp,
>
> if (of_device_register(op)) {
> printk("%pOF: Could not register of device.\n", dp);
> - kfree(op);
> + put_device(&op->dev);
> op = NULL;
> }
>
next prev parent reply other threads:[~2025-04-14 18:08 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-14 11:18 Ma Ke
2025-04-14 18:07 ` Rob Gardner [this message]
-- strict thread matches above, loose matches on Subject: below --
2025-04-07 7:41 Ma Ke
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=82791292-9caf-41b6-8d63-1190ea59e559@oracle.com \
--to=rob.gardner@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=andreas@gaisler.com \
--cc=davem@davemloft.net \
--cc=dawei.li@shingroup.cn \
--cc=linux-kernel@vger.kernel.org \
--cc=make24@iscas.ac.cn \
--cc=sam@ravnborg.org \
--cc=sparclinux@vger.kernel.org \
--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®