The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH v2 0/2] phy: sunplus: fix two issues in update_disc_vol()
@ 2026-08-07 13:16 Felix Gu
  2026-08-07 13:16 ` [PATCH v2 1/2] phy: sunplus: fix memory leak " Felix Gu
  2026-08-07 13:17 ` [PATCH v2 2/2] phy: sunplus: fix ERR_PTR deref " Felix Gu
  0 siblings, 2 replies; 3+ messages in thread
From: Felix Gu @ 2026-08-07 13:16 UTC (permalink / raw)
  To: Vincent Shih, Vinod Koul, Neil Armstrong
  Cc: linux-usb, linux-phy, linux-kernel, Felix Gu

Signed-off-by: Felix Gu <ustc.gu@gmail.com>
---
Changes in v2:
- Fix Vinod's comments.
- Split the commit into two patches, one for memory leak, one for error
  pointer deference.
- Link to v1: https://patch.msgid.link/20260804-sunplus-v1-1-79f52226cb50@gmail.com

To: Vincent Shih <vincent.sunplus@gmail.com>
To: Vinod Koul <vkoul@kernel.org>
To: Neil Armstrong <neil.armstrong@linaro.org>
Cc: linux-usb@vger.kernel.org
Cc: linux-phy@lists.infradead.org
Cc: linux-kernel@vger.kernel.org

---
Felix Gu (2):
      phy: sunplus: fix memory leak in update_disc_vol()
      phy: sunplus: fix ERR_PTR deref in update_disc_vol()

 drivers/phy/sunplus/phy-sunplus-usb2.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
---
base-commit: 9a4cdc958dd79fc6c3b20b51a10debec6ca09fec
change-id: 20260804-sunplus-53a7bb1bcd72

Best regards,
--  
Felix Gu <ustc.gu@gmail.com>


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

* [PATCH v2 1/2] phy: sunplus: fix memory leak in update_disc_vol()
  2026-08-07 13:16 [PATCH v2 0/2] phy: sunplus: fix two issues in update_disc_vol() Felix Gu
@ 2026-08-07 13:16 ` Felix Gu
  2026-08-07 13:17 ` [PATCH v2 2/2] phy: sunplus: fix ERR_PTR deref " Felix Gu
  1 sibling, 0 replies; 3+ messages in thread
From: Felix Gu @ 2026-08-07 13:16 UTC (permalink / raw)
  To: Vincent Shih, Vinod Koul, Neil Armstrong
  Cc: linux-usb, linux-phy, linux-kernel, Felix Gu

nvmem_cell_read() returns a kmalloc'd buffer that update_disc_vol()
never freed. Free it after extracting the disconnect-voltage value.

Fixes: 99d9ccd97385 ("phy: usb: Add USB2.0 phy driver for Sunplus SP7021")
Signed-off-by: Felix Gu <ustc.gu@gmail.com>
---
 drivers/phy/sunplus/phy-sunplus-usb2.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/phy/sunplus/phy-sunplus-usb2.c b/drivers/phy/sunplus/phy-sunplus-usb2.c
index 637a5fbae6d9..842087bdfba5 100644
--- a/drivers/phy/sunplus/phy-sunplus-usb2.c
+++ b/drivers/phy/sunplus/phy-sunplus-usb2.c
@@ -81,7 +81,7 @@ static int update_disc_vol(struct sp_usbphy *usbphy)
 	char *disc_name = "disc_vol";
 	ssize_t otp_l = 0;
 	char *otp_v;
-	u32 val, set;
+	u32 val, set = OTP_DISC_LEVEL_DEFAULT;
 
 	cell = nvmem_cell_get(usbphy->dev, disc_name);
 	if (IS_ERR_OR_NULL(cell)) {
@@ -96,11 +96,11 @@ static int update_disc_vol(struct sp_usbphy *usbphy)
 		set = *(otp_v + 1);
 		set = (set << (sizeof(char) * 8)) | *otp_v;
 		set = (set >> usbphy->disc_vol_addr_off) & J_DISC;
+		if (set == 0)
+			set = OTP_DISC_LEVEL_DEFAULT;
+		kfree(otp_v);
 	}
 
-	if (IS_ERR(otp_v) || set == 0)
-		set = OTP_DISC_LEVEL_DEFAULT;
-
 	val = readl(usbphy->phy_regs + CONFIG7);
 	val = (val & ~J_DISC) | set;
 	writel(val, usbphy->phy_regs + CONFIG7);

-- 
2.43.0


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

* [PATCH v2 2/2] phy: sunplus: fix ERR_PTR deref in update_disc_vol()
  2026-08-07 13:16 [PATCH v2 0/2] phy: sunplus: fix two issues in update_disc_vol() Felix Gu
  2026-08-07 13:16 ` [PATCH v2 1/2] phy: sunplus: fix memory leak " Felix Gu
@ 2026-08-07 13:17 ` Felix Gu
  1 sibling, 0 replies; 3+ messages in thread
From: Felix Gu @ 2026-08-07 13:17 UTC (permalink / raw)
  To: Vincent Shih, Vinod Koul, Neil Armstrong
  Cc: linux-usb, linux-phy, linux-kernel, Felix Gu

nvmem_cell_get() returns ERR_PTR on failure. For any error other
than -EPROBE_DEFER the code fell through and passed the invalid
pointer to nvmem_cell_read(), which dereferences it.

Skip the read and write the default disconnect voltage when the
lookup fails.

Fixes: 99d9ccd97385 ("phy: usb: Add USB2.0 phy driver for Sunplus SP7021")
Signed-off-by: Felix Gu <ustc.gu@gmail.com>
---
 drivers/phy/sunplus/phy-sunplus-usb2.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/phy/sunplus/phy-sunplus-usb2.c b/drivers/phy/sunplus/phy-sunplus-usb2.c
index 842087bdfba5..eb834788ea2f 100644
--- a/drivers/phy/sunplus/phy-sunplus-usb2.c
+++ b/drivers/phy/sunplus/phy-sunplus-usb2.c
@@ -84,9 +84,10 @@ static int update_disc_vol(struct sp_usbphy *usbphy)
 	u32 val, set = OTP_DISC_LEVEL_DEFAULT;
 
 	cell = nvmem_cell_get(usbphy->dev, disc_name);
-	if (IS_ERR_OR_NULL(cell)) {
+	if (IS_ERR(cell)) {
 		if (PTR_ERR(cell) == -EPROBE_DEFER)
 			return -EPROBE_DEFER;
+		goto skip_nvmem_read;
 	}
 
 	otp_v = nvmem_cell_read(cell, &otp_l);
@@ -101,6 +102,7 @@ static int update_disc_vol(struct sp_usbphy *usbphy)
 		kfree(otp_v);
 	}
 
+skip_nvmem_read:
 	val = readl(usbphy->phy_regs + CONFIG7);
 	val = (val & ~J_DISC) | set;
 	writel(val, usbphy->phy_regs + CONFIG7);

-- 
2.43.0


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

end of thread, other threads:[~2026-08-07 13:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-07 13:16 [PATCH v2 0/2] phy: sunplus: fix two issues in update_disc_vol() Felix Gu
2026-08-07 13:16 ` [PATCH v2 1/2] phy: sunplus: fix memory leak " Felix Gu
2026-08-07 13:17 ` [PATCH v2 2/2] phy: sunplus: fix ERR_PTR deref " Felix Gu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox