linux-phy.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] phy: Fix error handling in tegra_xusb_pad_init
@ 2025-11-04  1:28 Ma Ke
  2025-11-04 13:46 ` Jon Hunter
  2025-11-04 16:17 ` Markus Elfring
  0 siblings, 2 replies; 3+ messages in thread
From: Ma Ke @ 2025-11-04  1:28 UTC (permalink / raw)
  To: jckuo, vkoul, kishon, thierry.reding, jonathanh
  Cc: linux-phy, linux-tegra, linux-kernel, akpm, Ma Ke, stable

If device_add() fails, do not use device_unregister() for error
handling. device_unregister() consists two functions: device_del() and
put_device(). device_unregister() should only be called after
device_add() succeeded because device_del() undoes what device_add()
does if successful.

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'.

In tegra_xusb_pad_init(), both dev_set_name() and device_add() may
fail. In either case, we should only use put_device(). After
device_initialize(), the device has a reference count of 1. If
dev_set_name() fails, device_add() has not been called. If
device_add() fails, it has already cleaned up after itself.
device_unregister() would incorrectly call device_del() when
device_add() was never successful. Therefore, change both error paths
to use put_device() instead of device_unregister().

Found by code review.

Cc: stable@vger.kernel.org
Fixes: 53d2a715c240 ("phy: Add Tegra XUSB pad controller support")
Signed-off-by: Ma Ke <make24@iscas.ac.cn>
---
Changes in v2:
- modified the Fixes tag;
- modified the patch description.
---
 drivers/phy/tegra/xusb.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/phy/tegra/xusb.c b/drivers/phy/tegra/xusb.c
index c89df95aa6ca..d89493d68699 100644
--- a/drivers/phy/tegra/xusb.c
+++ b/drivers/phy/tegra/xusb.c
@@ -171,16 +171,16 @@ int tegra_xusb_pad_init(struct tegra_xusb_pad *pad,
 
 	err = dev_set_name(&pad->dev, "%s", pad->soc->name);
 	if (err < 0)
-		goto unregister;
+		goto put_device;
 
 	err = device_add(&pad->dev);
 	if (err < 0)
-		goto unregister;
+		goto put_device;
 
 	return 0;
 
-unregister:
-	device_unregister(&pad->dev);
+put_device:
+	put_device(&pad->dev);
 	return err;
 }
 
-- 
2.17.1


-- 
linux-phy mailing list
linux-phy@lists.infradead.org
https://lists.infradead.org/mailman/listinfo/linux-phy

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

* Re: [PATCH v2] phy: Fix error handling in tegra_xusb_pad_init
  2025-11-04  1:28 [PATCH v2] phy: Fix error handling in tegra_xusb_pad_init Ma Ke
@ 2025-11-04 13:46 ` Jon Hunter
  2025-11-04 16:17 ` Markus Elfring
  1 sibling, 0 replies; 3+ messages in thread
From: Jon Hunter @ 2025-11-04 13:46 UTC (permalink / raw)
  To: Ma Ke, jckuo, vkoul, kishon, thierry.reding
  Cc: linux-phy, linux-tegra, linux-kernel, akpm, stable


On 04/11/2025 01:28, Ma Ke wrote:
> If device_add() fails, do not use device_unregister() for error
> handling. device_unregister() consists two functions: device_del() and
> put_device(). device_unregister() should only be called after
> device_add() succeeded because device_del() undoes what device_add()
> does if successful.
> 
> 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'.
> 
> In tegra_xusb_pad_init(), both dev_set_name() and device_add() may
> fail. In either case, we should only use put_device(). After
> device_initialize(), the device has a reference count of 1. If
> dev_set_name() fails, device_add() has not been called. If
> device_add() fails, it has already cleaned up after itself.
> device_unregister() would incorrectly call device_del() when
> device_add() was never successful. Therefore, change both error paths
> to use put_device() instead of device_unregister().

Ideally, we would make all of the above a bit more concise/simple. 
However, this does look correct to me ...
> Found by code review.
> 
> Cc: stable@vger.kernel.org
> Fixes: 53d2a715c240 ("phy: Add Tegra XUSB pad controller support")
> Signed-off-by: Ma Ke <make24@iscas.ac.cn>
> ---
> Changes in v2:
> - modified the Fixes tag;
> - modified the patch description.
> ---
>   drivers/phy/tegra/xusb.c | 8 ++++----
>   1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/phy/tegra/xusb.c b/drivers/phy/tegra/xusb.c
> index c89df95aa6ca..d89493d68699 100644
> --- a/drivers/phy/tegra/xusb.c
> +++ b/drivers/phy/tegra/xusb.c
> @@ -171,16 +171,16 @@ int tegra_xusb_pad_init(struct tegra_xusb_pad *pad,
>   
>   	err = dev_set_name(&pad->dev, "%s", pad->soc->name);
>   	if (err < 0)
> -		goto unregister;
> +		goto put_device;
>   
>   	err = device_add(&pad->dev);
>   	if (err < 0)
> -		goto unregister;
> +		goto put_device;
>   
>   	return 0;
>   
> -unregister:
> -	device_unregister(&pad->dev);
> +put_device:
> +	put_device(&pad->dev);
>   	return err;
>   }
>   
Reviewed-by: Jon Hunter <jonathanh@nvidia.com>

Thanks
Jon

-- 
nvpublic


-- 
linux-phy mailing list
linux-phy@lists.infradead.org
https://lists.infradead.org/mailman/listinfo/linux-phy

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

* Re: [PATCH v2] phy: Fix error handling in tegra_xusb_pad_init
  2025-11-04  1:28 [PATCH v2] phy: Fix error handling in tegra_xusb_pad_init Ma Ke
  2025-11-04 13:46 ` Jon Hunter
@ 2025-11-04 16:17 ` Markus Elfring
  1 sibling, 0 replies; 3+ messages in thread
From: Markus Elfring @ 2025-11-04 16:17 UTC (permalink / raw)
  To: make24, linux-phy, linux-tegra
  Cc: stable, LKML, Andrew Morton, JC Kuo, Jonathan Hunter,
	Kishon Vijay Abraham I, Thierry Reding, Vinod Koul

…
> In tegra_xusb_pad_init(), both dev_set_name() and device_add() may
…

Would it be helpful to append parentheses also to the function name
in the summary phrase?

Regards,
Markus

-- 
linux-phy mailing list
linux-phy@lists.infradead.org
https://lists.infradead.org/mailman/listinfo/linux-phy

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

end of thread, other threads:[~2025-11-04 16:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-04  1:28 [PATCH v2] phy: Fix error handling in tegra_xusb_pad_init Ma Ke
2025-11-04 13:46 ` Jon Hunter
2025-11-04 16:17 ` Markus Elfring

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).