Skip to contentSkip to conversation

usb: musb-new: Free the controller if the host probe fails

PATCH In review Yuho Choi 4 participants 16 messages Nov 6 – Nov 9

Subsystems (2)
lore
Share & tools
lore
#1 lore raw
In reply to Yuho Choi
Quoted 8 lines · Yuho Choi

The MUSB glue drivers below allocate the struct musb with musb_init_controller() in their host probe and return the error when musb_lowlevel_init() fails, for example when no device is connected. None of them frees the struct musb on that path, and the driver core then frees the private data that holds the only pointer to it, so each 'usb reset', or 'usb start' after 'usb stop', leaks another one. This series frees it in ux500, sc5xx, pic32, omap2430, ti-musb and da8xx.

Quoted 7 lines

The same fix for sunxi and mt85xx was sent as separate patches:
"usb: musb-new: sunxi: Free the controller if the host probe fails" "usb: musb-new: mt85xx: Free the controller if the host probe fails"

The patches are independent of each other. They are build-tested only, as noted in each patch, and pic32 is not built at all. None was tested on hardware.

How are you finding and investigating these issues, exactly? To cut to the chase, this series (and the other two patches) sure seem like what happens when someone sends an LLM to look for resource leaks. Which are not great. But the bigger concern is if we handle the error case correctly. Thanks.

Signature · 1 line
Tom

Yuho Choi

author
#0 lore raw

cover usb: musb-new: Free the controller if the host probe fails

The MUSB glue drivers below allocate the struct musb with musb_init_controller() in their host probe and return the error when musb_lowlevel_init() fails, for example when no device is connected. None of them frees the struct musb on that path, and the driver core then frees the private data that holds the only pointer to it, so each 'usb reset', or 'usb start' after 'usb stop', leaks another one. This series frees it in ux500, sc5xx, pic32, omap2430, ti-musb and da8xx.

The same fix for sunxi and mt85xx was sent as separate patches:
"usb: musb-new: sunxi: Free the controller if the host probe fails" "usb: musb-new: mt85xx: Free the controller if the host probe fails"

The patches are independent of each other. They are build-tested only, as noted in each patch, and pic32 is not built at all. None was tested on hardware.

Yuho Choi (6):
usb: musb-new: ux500: Free the controller if the host probe fails
usb: musb-new: sc5xx: Free the controller if the host probe fails
usb: musb-new: pic32: Free the controller if the host probe fails
usb: musb-new: omap2430: Free the controller if the host probe fails
usb: musb-new: ti-musb: Free the controller if the host probe fails
usb: musb-new: da8xx: Free the controller if the host probe fails

 drivers/usb/musb-new/da8xx.c    | 2 ++
 drivers/usb/musb-new/omap2430.c | 6 +++++-
 drivers/usb/musb-new/pic32.c    | 2 ++
 drivers/usb/musb-new/sc5xx.c    | 2 ++
 drivers/usb/musb-new/ti-musb.c  | 2 ++
 drivers/usb/musb-new/ux500.c    | 6 +++++-
 6 files changed, 18 insertions(+), 2 deletions(-)

base-commit: feb8913982e4462c01fd914271f92254a25dc101

Signature · 1 line
2.43.0
author
#5 lore raw

PATCH 1/6 usb: musb-new: ux500: Free the controller if the host probe fails

In host mode, ux500_musb_probe() allocates the struct musb with musb_init_controller() and returns the error when musb_lowlevel_init() fails, for example when no device is connected. The struct musb is not freed on this path, and the driver core then frees the private data that holds the only pointer to it. Each 'usb reset', or 'usb start' after 'usb stop', probes the controller again and leaks another one.

Free the struct musb when musb_lowlevel_init() fails.

Fixes 845d9cf61c3a ("usb: musb-new: Add glue driver for ST-Ericsson Ux500")

---
Build-tested only, with stemmy_defconfig and CONFIG_USB_MUSB_HOST=y.

When musb_start() fails, as it does when ux500_musb_enable() cannot power on the PHY, musb_lowlevel_init() returns without calling musb_stop(), so the platform init done by musb_init_controller() is not undone either. That is not changed here.

 drivers/usb/musb-new/ux500.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/usb/musb-new/ux500.c +5 -1
index be0085f403d..c38faa24582 100644
@@ -123,7 +123,11 @@ static int ux500_musb_probe(struct udevice *dev)
 	if (!host->host)
 		return -EIO;
 
-	return musb_lowlevel_init(host);
+	ret = musb_lowlevel_init(host);
+	if (ret)
+		free(host->host);
+
+	return ret;
 #else
 	pdata.mode = MUSB_PERIPHERAL;
 	host->host = musb_init_controller(&pdata, &glue->dev, base);
Signature · 1 line
2.43.0
author
#7 lore raw

PATCH 2/6 usb: musb-new: sc5xx: Free the controller if the host probe fails

In reply to Yuho Choi

In host mode, musb_usb_probe() allocates the struct musb with musb_init_controller() and returns the error when musb_lowlevel_init() fails, for example when no device is connected. The struct musb is not freed on this path, and the driver core then frees the private data that holds the only pointer to it. Each 'usb reset', or 'usb start' after 'usb stop', probes the controller again and leaks another one.

Free the struct musb when musb_lowlevel_init() fails.

Fixes 2f6a86a61256 ("usb: musb-new: Add support for Analog Devices SC5xx SoCs")

---
Build-tested only, with sc584-ezkit_defconfig.

musb_usb_remove() does not free the struct musb either; that is not changed here.

 drivers/usb/musb-new/sc5xx.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/usb/musb-new/sc5xx.c +2 -0
index b1765abe827..c14ba821a2e 100644
@@ -165,6 +165,8 @@ static int musb_usb_probe(struct udevice *dev)
 			return -EIO;
 
 		ret = musb_lowlevel_init(mdata);
+		if (ret)
+			free(mdata->host);
 	} else {
 		sc5xx_musb_plat.mode = MUSB_PERIPHERAL;
 		mdata->host = musb_register(&sc5xx_musb_plat, &pdata->dev, mregs);
Signature · 1 line
2.43.0
author
#12 lore raw

PATCH 3/6 usb: musb-new: pic32: Free the controller if the host probe fails

In reply to Yuho Choi

In host mode, musb_usb_probe() allocates the struct musb with musb_init_controller() and returns the error when musb_lowlevel_init() fails, for example when no device is connected. The struct musb is not freed on this path, and the driver core then frees the private data that holds the only pointer to it. Each 'usb reset', or 'usb start' after 'usb stop', probes the controller again and leaks another one.

Free the struct musb when musb_lowlevel_init() fails.

Fixes 03b8e04632ca ("drivers: musb-new: Add USB DRC driver for Microchip PIC32 OTG controller.")

---
Not build-tested: I have no MIPS toolchain.

musb_usb_remove() does not free the struct musb either; that is not changed here.

 drivers/usb/musb-new/pic32.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/usb/musb-new/pic32.c +2 -0
index b878e979d93..2623e6e526a 100644
@@ -253,6 +253,8 @@ static int musb_usb_probe(struct udevice *dev)
 		return -EIO;
 
 	ret = musb_lowlevel_init(mdata);
+	if (ret)
+		free(mdata->host);
 #else
 	pic32_musb_plat.mode = MUSB_PERIPHERAL;
 	mdata->host = musb_register(&pic32_musb_plat, &pdata->dev, mregs);
Signature · 1 line
2.43.0
author
#13 lore raw

PATCH 4/6 usb: musb-new: omap2430: Free the controller if the host probe fails

In reply to Yuho Choi

In host mode, omap2430_musb_probe() allocates the struct musb with musb_init_controller() and returns the error when musb_lowlevel_init() fails, for example when no device is connected. The struct musb is not freed on this path, and the driver core then frees the private data that holds the only pointer to it. Each 'usb reset', or 'usb start' after 'usb stop', probes the controller again and leaks another one.

Free the struct musb when musb_lowlevel_init() fails.

Fixes 1a35526e1d54 ("usb: musb-new: omap2430: Enable DM_USB and OF support")

---
Build-tested only, with omap3_evm_defconfig and CONFIG_USB_MUSB_HOST=y.

omap2430_musb_remove() does not free the struct musb either; that is not changed here.

 drivers/usb/musb-new/omap2430.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/usb/musb-new/omap2430.c +5 -1
index c8c6bf0c84f..c5607fd388e 100644
@@ -229,7 +229,11 @@ static int omap2430_musb_probe(struct udevice *dev)
 		if (!host->host)
 			return -EIO;
 
-		return musb_lowlevel_init(host);
+		ret = musb_lowlevel_init(host);
+		if (ret)
+			free(host->host);
+
+		return ret;
 	} else if (CONFIG_IS_ENABLED(DM_USB_GADGET)) {
 		struct musb_host_data *host = dev_get_priv(dev);
 
Signature · 1 line
2.43.0
author
#14 lore raw

PATCH 5/6 usb: musb-new: ti-musb: Free the controller if the host probe fails

In reply to Yuho Choi

ti_musb_host_probe() allocates the struct musb with musb_init_controller() and returns the error when musb_lowlevel_init() fails, for example when no device is connected. The struct musb is not freed on this path, and the driver core then frees the private data that holds the only pointer to it. Each 'usb reset', or 'usb start' after 'usb stop', probes the controller again and leaks another one.

Free the struct musb when musb_lowlevel_init() fails.

Fixes ae6acf9fe2c8 ("drivers: usb: musb: add ti musb host driver with driver model support")

---
Build-tested only, with pcm051_rev3_defconfig.

ti_musb_host_remove() does not free the struct musb either, and the PHY stays powered on after the failure; neither is changed here.

 drivers/usb/musb-new/ti-musb.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/usb/musb-new/ti-musb.c +2 -0
index cc6c3b94a65..5a93708bc75 100644
@@ -168,6 +168,8 @@ static int ti_musb_host_probe(struct udevice *dev)
 
 	ti_musb_set_phy_power(dev, 1);
 	ret = musb_lowlevel_init(host);
+	if (ret)
+		free(host->host);
 
 	return ret;
 }
Signature · 1 line
2.43.0
author
#15 lore raw

PATCH 6/6 usb: musb-new: da8xx: Free the controller if the host probe fails

In reply to Yuho Choi

da8xx_musb_probe() allocates the struct musb with musb_init_controller() and returns the error when musb_lowlevel_init() fails, for example when no device is connected. The error path turns off the PHY and the PSC but does not free the struct musb, and the driver core then frees the private data that holds the only pointer to it. Each 'usb reset', or 'usb start' after 'usb stop', probes the controller again and leaks another one.

Free the struct musb when musb_lowlevel_init() fails.

Fixes 5608331d8136 ("usb: musb-new: Add support for da8xx-musb")

---
Build-tested only, with omapl138_lcdk_defconfig.

da8xx_musb_remove() does not free the struct musb either; that is not changed here.

 drivers/usb/musb-new/da8xx.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/usb/musb-new/da8xx.c +2 -0
index 7caf03cc2e1..17a953c9744 100644
@@ -319,6 +319,8 @@ static int da8xx_musb_probe(struct udevice *dev)
 
 	if (ret == 0)
 		return 0;
+
+	free(host->host);
 shutdown:
 	/* Turn off the phy if we fail */
 	generic_phy_power_off(&plat->phy);
Signature · 1 line
2.43.0
Reviewed-by on v1 1/6
#6 lore raw

Re: [PATCH 1/6] usb: musb-new: ux500: Free the controller if the host probe fails

In reply to Yuho Choi
Quoted 11 lines · Yuho Choi

In host mode, ux500_musb_probe() allocates the struct musb with musb_init_controller() and returns the error when musb_lowlevel_init() fails, for example when no device is connected. The struct musb is not freed on this path, and the driver core then frees the private data that holds the only pointer to it. Each 'usb reset', or 'usb start' after 'usb stop', probes the controller again and leaks another one.

Free the struct musb when musb_lowlevel_init() fails.

Fixes 845d9cf61c3a ("usb: musb-new: Add glue driver for ST-Ericsson Ux500")

Yours,
Linus Walleij

author
#2 lore raw
In reply to Tom Rini
Quoted 6 lines · Tom Rini

How are you finding and investigating these issues, exactly? To cut to the chase, this series (and the other two patches) sure seem like what happens when someone sends an LLM to look for resource leaks. Which are not great. But the bigger concern is if we handle the error case correctly. Thanks.

Thanks for the review.
The bugs were found as part of ongoing static analysis research on configuration-dependent resource management bug patterns. I used an LLM to help verify the bugs.

The patches only fix the memory leak. They do not restore the hardware state on the error path. Would you want patches that restore all of the hardware state on these error paths instead?

Thanks.

#8 lore raw

Re: [PATCH 2/6] usb: musb-new: sc5xx: Free the controller if the host probe fails

In reply to Yuho Choi

Hi Yuho,

Quoted 35 lines · Yuho Choi

In host mode, musb_usb_probe() allocates the struct musb with musb_init_controller() and returns the error when musb_lowlevel_init() fails, for example when no device is connected. The struct musb is not freed on this path, and the driver core then frees the private data that holds the only pointer to it. Each 'usb reset', or 'usb start' after 'usb stop', probes the controller again and leaks another one.

Free the struct musb when musb_lowlevel_init() fails.

Fixes 2f6a86a61256 ("usb: musb-new: Add support for Analog Devices SC5xx SoCs")

---
Build-tested only, with sc584-ezkit_defconfig.

musb_usb_remove() does not free the struct musb either; that is not changed here.

 drivers/usb/musb-new/sc5xx.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/usb/musb-new/sc5xx.c +2 -1
index b1765abe827..c14ba821a2e 100644
@@ -165,6 +165,8 @@ static int musb_usb_probe(struct udevice *dev)
                        return -EIO;
 
                ret = musb_lowlevel_init(mdata);
+               if (ret)
+                       free(mdata->host);
        } else {
                sc5xx_musb_plat.mode = MUSB_PERIPHERAL;
                mdata->host = musb_register(&sc5xx_musb_plat, &pdata->dev, mregs);
--
Quoted 2 lines

2.43.0

Is freeing mdata->host the appropriate cleanup action? musb_init_controller registers the pointed-to struct with the pm subsystem, potentially allocates a dma controller to store in ->c, and does many other things I haven't traced through yet. I would need to review in more detail but it seems a more appropriate action would be to use musb_free() which is currently a static function inside musb_core.c.

Taking an even further step back, musb_lowlevel_init fails under two
circumstances: host->host is not set (impossible due to the earlier check) or musb_start fails which only happens when the musb_usb_ops->enable() function is present and fails (via musb_platform_enable) which is not part of the default musb_ops that we are using. It is therefore impossible for musb_lowlevel_init to set ret to a nonzero value in a way that leaks resources on this platform. I have not checked the other drivers referenced in this series, but I honestly assume we all copied the same driver and behave in basically the same way, including default musb_ops.

I don't think we should make this change for now.

Thanks,
Greg

#9 lore raw

Re: [PATCH 2/6] usb: musb-new: sc5xx: Free the controller if the host probe fails

Quoted 11 lines · Greg Malysa

Taking an even further step back, musb_lowlevel_init fails under two
circumstances: host->host is not set (impossible due to the earlier check) or musb_start fails which only happens when the musb_usb_ops->enable() function is present and fails (via musb_platform_enable) which is not part of the default musb_ops that we are using. It is therefore impossible for musb_lowlevel_init to set ret to a nonzero value in a way that leaks resources on this platform. I have not checked the other drivers referenced in this series, but I honestly assume we all copied the same driver and behave in basically the same way, including default musb_ops.

I think I was off here (sorry I didn't write this driver initially and musb is a bit of a labyrinth), so it probably actually calls sc5xx_musb_enable through the sc5xx_musb_ops struct, but that function also cannot fail and always returns zero, so musb_start() will not fail either way.

#11 lore raw

Re: [PATCH 2/6] usb: musb-new: sc5xx: Free the controller if the host probe fails

In reply to Yuho Choi

Hi Yuho,

Quoted 22 lines · Yuho Choi
Quoted 8 lines · Greg Malysa

Is freeing mdata->host the appropriate cleanup action? musb_init_controller registers the pointed-to struct with the pm subsystem, potentially allocates a dma controller to store in ->c, and does many other things I haven't traced through yet. I would need to review in more detail but it seems a more appropriate action would be to use musb_free() which is currently a static function inside musb_core.c.

Thanks for the review. I agree that musb_free() is the better choice.

Quoted 6 lines · Greg Malysa

I think I was off here (sorry I didn't write this driver initially and musb is a bit of a labyrinth), so it probably actually calls sc5xx_musb_enable through the sc5xx_musb_ops struct, but that function also cannot fail and always returns zero, so musb_start() will not fail either way.

Quoted 5 lines

Agreed, musb_start() cannot fail on sc5xx. However, musb_lowlevel_init() has a third failure path. After musb_start() succeeds, it waits up to one second for MUSB_DEVCTL_HM to be set. If the bit is not set by then, it calls musb_stop() and returns -ENODEV.

Sorry I missed that third one when looking at it earlier. I agree we should get a fix in place to avoid leaking when this happens. Let's see what Marek says about the best way to deallocate the resources from outside of musb_core. It looks like there is a lot of Linux code that is removed by #ifndef __UBOOT__ blocks so I want to hear from someone more familiar about whether it is reasonable to just expose musb_free() and call it in this case, or if we need to migrate more of the logic from the Linux code for musb_remove() such as musb_shutdown(), which is also not included in the uboot build for now.

Also it seems the pm_runtime_* functions I was worried about earlier expand to empty macros per include/linux/compat.h so there may be less to do than I had thought.

Quoted 3 lines

The leak is small so I am fine with dropping this patch if this is not needed.

No, the third condition you identified is worth handling and fixing.

Quoted 3 lines

Thanks,
Yuho

Thanks,
Greg

author
#10 lore raw

Re: [PATCH 2/6] usb: musb-new: sc5xx: Free the controller if the host probe fails

In reply to Greg Malysa
Quoted 8 lines · Greg Malysa

Is freeing mdata->host the appropriate cleanup action? musb_init_controller registers the pointed-to struct with the pm subsystem, potentially allocates a dma controller to store in ->c, and does many other things I haven't traced through yet. I would need to review in more detail but it seems a more appropriate action would be to use musb_free() which is currently a static function inside musb_core.c.

Thanks for the review. I agree that musb_free() is the better choice.

Quoted 6 lines · Greg Malysa

I think I was off here (sorry I didn't write this driver initially and musb is a bit of a labyrinth), so it probably actually calls sc5xx_musb_enable through the sc5xx_musb_ops struct, but that function also cannot fail and always returns zero, so musb_start() will not fail either way.

Agreed, musb_start() cannot fail on sc5xx. However, musb_lowlevel_init() has a third failure path. After musb_start() succeeds, it waits up to one second for MUSB_DEVCTL_HM to be set. If the bit is not set by then, it calls musb_stop() and returns -ENODEV.

The leak is small so I am fine with dropping this patch if this is not needed.

Thanks,
Yuho

#3 lore raw
In reply to Yuho Choi
Quoted 8 lines · Yuho Choi
Quoted 6 lines · Tom Rini

How are you finding and investigating these issues, exactly? To cut to the chase, this series (and the other two patches) sure seem like what happens when someone sends an LLM to look for resource leaks. Which are not great. But the bigger concern is if we handle the error case correctly. Thanks.

Quoted 8 lines

Thanks for the review.
The bugs were found as part of ongoing static analysis research on configuration-dependent resource management bug patterns. I used an LLM to help verify the bugs.

The patches only fix the memory leak. They do not restore the hardware state on the error path. Would you want patches that restore all of the hardware state on these error paths instead?

So, the general project policy for LLMs is please don't. Using this as an example, it's less interesting that we might have a small leak (we aren't a persistent OS), but as the part of this thread with Greg and the sc5xx driver show, there are potentially interesting problems in the failure case (and we don't need to restore the HW state, just ensure our internal state is consistent). Digging past the initial leak to fully understand the error path and that on the software side we handle errors gracefully would be an interesting task.

Signature · 1 line
Tom
author
#4 lore raw
Quoted 8 lines · Tom Rini

So, the general project policy for LLMs is please don't. Using this as an example, it's less interesting that we might have a small leak (we aren't a persistent OS), but as the part of this thread with Greg and the sc5xx driver show, there are potentially interesting problems in the failure case (and we don't need to restore the HW state, just ensure our internal state is consistent). Digging past the initial leak to fully understand the error path and that on the software side we handle errors gracefully would be an interesting task.

Understood, thank you for explaining. I will not send a v2 of this series or of the separate sunxi and mt85xx patches, and will leave it to Marek whether any of them are worth taking as they are.

Thanks,
Yuho