Skip to contentSkip to conversation

phy: Fix error handling in tegra_xusb_pad_init

PATCH In review Ma Ke 3 participants 3 messages Nov 4, 2025

Subsystems (4)
lore
Share & tools
lore

Ma Ke

author
#0 lore raw

PATCH v2 1/1 phy: Fix error handling in tegra_xusb_pad_init

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.

Fixes 53d2a715c240 ("phy: Add Tegra XUSB pad controller support")

---
Changes in v2:
- modified the Fixes tag;
- modified the patch description.

---
 drivers/phy/tegra/xusb.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/phy/tegra/xusb.c +4 -4
index c89df95aa6ca..d89493d68699 100644
@@ -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;
 }
 
Signature · 7 lines
2.17.1


-- 
linux-phy mailing list
[email protected]
https://lists.infradead.org/mailman/listinfo/linux-phy
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 10 lines · Ma Ke

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

Quoted 8 lines

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

Quoted 28 lines

Found by code review.

Fixes 53d2a715c240 ("phy: Add Tegra XUSB pad controller support")

---
Changes in v2:
- modified the Fixes tag;
- modified the patch description.

---
  drivers/phy/tegra/xusb.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
drivers/phy/tegra/xusb.c +2 -2
index c89df95aa6ca..d89493d68699 100644
@@ -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;

drivers/phy/tegra/xusb.c int tegra_xusb_pad_init(struct tegra_xusb_pad *pad, · line 186

Quoted 10 lines
drivers/phy/tegra/xusb.c +2 -2
 
  	return 0;
 
-unregister:
-	device_unregister(&pad->dev);
+put_device:
+	put_device(&pad->dev);
  	return err;
  }
 

Thanks
Jon

Signature · 1 line
nvpublic
#2 lore raw
In reply to Ma Ke

…

Quoted 1 line

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