Skip to contentSkip to conversation

usb: gadget: f_mass_storage: Synchronously tear interface down

PATCH In review Marek Vasut 2 participants 6 messages Sep 3 – Sep 26 USB

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: gadget: f_mass_storage: Synchronously tear interface down

After fsg_unbind() is called on UMS teardown, the fsg_main_thread() which also invokes handle_exception() is unlikely to run afterward. Therefore, any exceptions raised in fsg_unbind(), and also exceptions raised in fsg_common_release() which is called from both fsg_unbind() and fsg_common_init() fail path, will remain unhandled.

This leads to a problem where FSG_STATE_TERMINATED/EXIT handling is not performed on UMS exit and the do_set_interface(..., NULL) which shuts down endpoints is also not performed, leaving the endpoints enabled.

The CI UDC driver does check for repeated enablement of endpoints and returns -EBUSY if an endpoint is enabled repeatedly. That prevents UMS from being started after it was exited once already. That is, running "ums 0 mmc 0 ; ums 0 mmc 0" on U-Boot command line will lead to one successful and one failed UMS session. The later session ends up triggering fsg_is_set() assertion in fsg_setup() and triggers a panic().

Fix this by synchronously invoking the do_set_interface() from both fsg_unbind() and fsg_common_release(). That makes the unbind behave as intended, as if the fsg_main_thread() ran after fsg_unbind() and handled the outstanding raised exceptions.

---
Cc: Mattijs Korpershoek <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: [email protected]

---
 drivers/usb/gadget/f_mass_storage.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/usb/gadget/f_mass_storage.c +4 -4
index 7eb667c130d..76ee08dc5e8 100644
@@ -2565,8 +2565,8 @@ static void fsg_common_release(struct fsg_common *common)
 {
 	/* If the thread isn't already dead, tell it to exit now */
 	if (common->state != FSG_STATE_TERMINATED) {
-		raise_exception(common, FSG_STATE_EXIT);
-		wait_for_completion(&common->thread_notifier);
+		do_set_interface(common, NULL);
+		common->state = FSG_STATE_EXIT;
 	}
 
 	if (likely(common->luns)) {
@@ -2647,8 +2647,8 @@ static void fsg_unbind(struct usb_configuration *c, struct usb_function *f)
 
 	DBG(fsg, "unbind\n");
 	if (fsg->common->fsg == fsg) {
-		fsg->common->new_fsg = NULL;
-		raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
+		do_set_interface(fsg->common, NULL);
+		fsg->common->state = FSG_STATE_CONFIG_CHANGE;
 	}
 
 	fsg_common_release(fsg->common);
Signature · 1 line
2.53.0
Reviewed-by on v1 1/1
#1 lore raw

Hi Marek,

Thank you for the patch.

Quoted 19 lines · Marek Vasut

After fsg_unbind() is called on UMS teardown, the fsg_main_thread() which also invokes handle_exception() is unlikely to run afterward. Therefore, any exceptions raised in fsg_unbind(), and also exceptions raised in fsg_common_release() which is called from both fsg_unbind() and fsg_common_init() fail path, will remain unhandled.

This leads to a problem where FSG_STATE_TERMINATED/EXIT handling is not performed on UMS exit and the do_set_interface(..., NULL) which shuts down endpoints is also not performed, leaving the endpoints enabled.

The CI UDC driver does check for repeated enablement of endpoints and returns -EBUSY if an endpoint is enabled repeatedly. That prevents UMS from being started after it was exited once already. That is, running "ums 0 mmc 0 ; ums 0 mmc 0" on U-Boot command line will lead to one successful and one failed UMS session. The later session ends up triggering fsg_is_set() assertion in fsg_setup() and triggers a panic().

Quoted 6 lines

Fix this by synchronously invoking the do_set_interface() from both fsg_unbind() and fsg_common_release(). That makes the unbind behave as intended, as if the fsg_main_thread() ran after fsg_unbind() and handled the outstanding raised exceptions.

The change looks fine to me. I hard a hard time understanding why there was not an equivalent fix in Linux.
I think it's because we have cyclic in U-Boot and real threading in the kernel.

In any case:

Quoted 36 lines

---
Cc: Mattijs Korpershoek <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: [email protected]

---
 drivers/usb/gadget/f_mass_storage.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/usb/gadget/f_mass_storage.c +4 -4
index 7eb667c130d..76ee08dc5e8 100644
@@ -2565,8 +2565,8 @@ static void fsg_common_release(struct fsg_common *common)
 {
 	/* If the thread isn't already dead, tell it to exit now */
 	if (common->state != FSG_STATE_TERMINATED) {
-		raise_exception(common, FSG_STATE_EXIT);
-		wait_for_completion(&common->thread_notifier);
+		do_set_interface(common, NULL);
+		common->state = FSG_STATE_EXIT;
 	}
 
 	if (likely(common->luns)) {
@@ -2647,8 +2647,8 @@ static void fsg_unbind(struct usb_configuration *c, struct usb_function *f)
 
 	DBG(fsg, "unbind\n");
 	if (fsg->common->fsg == fsg) {
-		fsg->common->new_fsg = NULL;
-		raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
+		do_set_interface(fsg->common, NULL);
+		fsg->common->state = FSG_STATE_CONFIG_CHANGE;
 	}
 
 	fsg_common_release(fsg->common);
Signature · 1 line
2.53.0
#2 lore raw

On 9/7/26 10:47 AM, Mattijs Korpershoek wrote:

Hello Mattijs,

Quoted 30 lines

Thank you for the patch.

Quoted 19 lines · Marek Vasut

After fsg_unbind() is called on UMS teardown, the fsg_main_thread() which also invokes handle_exception() is unlikely to run afterward. Therefore, any exceptions raised in fsg_unbind(), and also exceptions raised in fsg_common_release() which is called from both fsg_unbind() and fsg_common_init() fail path, will remain unhandled.

This leads to a problem where FSG_STATE_TERMINATED/EXIT handling is not performed on UMS exit and the do_set_interface(..., NULL) which shuts down endpoints is also not performed, leaving the endpoints enabled.

The CI UDC driver does check for repeated enablement of endpoints and returns -EBUSY if an endpoint is enabled repeatedly. That prevents UMS from being started after it was exited once already. That is, running "ums 0 mmc 0 ; ums 0 mmc 0" on U-Boot command line will lead to one successful and one failed UMS session. The later session ends up triggering fsg_is_set() assertion in fsg_setup() and triggers a panic().

Quoted 6 lines

Fix this by synchronously invoking the do_set_interface() from both fsg_unbind() and fsg_common_release(). That makes the unbind behave as intended, as if the fsg_main_thread() ran after fsg_unbind() and handled the outstanding raised exceptions.

Quoted 8 lines

The change looks fine to me. I hard a hard time understanding why there was not an equivalent fix in Linux.
I think it's because we have cyclic in U-Boot and real threading in the kernel.

In any case:

Maybe this could be added into 2026.10 release ?

#3 lore raw

Hi Marek,

Quoted 39 lines · Marek Vasut

On 9/7/26 10:47 AM, Mattijs Korpershoek wrote:

Hello Mattijs,

Quoted 35 lines

Thank you for the patch.

Quoted 19 lines · Marek Vasut

After fsg_unbind() is called on UMS teardown, the fsg_main_thread() which also invokes handle_exception() is unlikely to run afterward. Therefore, any exceptions raised in fsg_unbind(), and also exceptions raised in fsg_common_release() which is called from both fsg_unbind() and fsg_common_init() fail path, will remain unhandled.

This leads to a problem where FSG_STATE_TERMINATED/EXIT handling is not performed on UMS exit and the do_set_interface(..., NULL) which shuts down endpoints is also not performed, leaving the endpoints enabled.

The CI UDC driver does check for repeated enablement of endpoints and returns -EBUSY if an endpoint is enabled repeatedly. That prevents UMS from being started after it was exited once already. That is, running "ums 0 mmc 0 ; ums 0 mmc 0" on U-Boot command line will lead to one successful and one failed UMS session. The later session ends up triggering fsg_is_set() assertion in fsg_setup() and triggers a panic().

Quoted 6 lines

Fix this by synchronously invoking the do_set_interface() from both fsg_unbind() and fsg_common_release(). That makes the unbind behave as intended, as if the fsg_main_thread() ran after fsg_unbind() and handled the outstanding raised exceptions.

The change looks fine to me. I hard a hard time understanding why there was not an equivalent fix in Linux.
I think it's because we have cyclic in U-Boot and real threading in the kernel.

Quoted 4 lines
Quoted 3 lines

In any case:

Maybe this could be added into 2026.10 release ?

I'm sorry, this completely dissapeared from my radar (It was not assigned to me on patchwork).

I'll queue it up.

Thanks for the reminder
Mattijs

#4 lore raw

On 9/25/26 10:17 AM, Mattijs Korpershoek wrote:

Hello Mattijs,

Quoted 41 lines · Marek Vasut
Quoted 40 lines · Mattijs Korpershoek

Hello Mattijs,

Quoted 35 lines

Thank you for the patch.

Quoted 19 lines · Marek Vasut

After fsg_unbind() is called on UMS teardown, the fsg_main_thread() which also invokes handle_exception() is unlikely to run afterward. Therefore, any exceptions raised in fsg_unbind(), and also exceptions raised in fsg_common_release() which is called from both fsg_unbind() and fsg_common_init() fail path, will remain unhandled.

This leads to a problem where FSG_STATE_TERMINATED/EXIT handling is not performed on UMS exit and the do_set_interface(..., NULL) which shuts down endpoints is also not performed, leaving the endpoints enabled.

The CI UDC driver does check for repeated enablement of endpoints and returns -EBUSY if an endpoint is enabled repeatedly. That prevents UMS from being started after it was exited once already. That is, running "ums 0 mmc 0 ; ums 0 mmc 0" on U-Boot command line will lead to one successful and one failed UMS session. The later session ends up triggering fsg_is_set() assertion in fsg_setup() and triggers a panic().

Quoted 6 lines

Fix this by synchronously invoking the do_set_interface() from both fsg_unbind() and fsg_common_release(). That makes the unbind behave as intended, as if the fsg_main_thread() ran after fsg_unbind() and handled the outstanding raised exceptions.

The change looks fine to me. I hard a hard time understanding why there was not an equivalent fix in Linux.
I think it's because we have cyclic in U-Boot and real threading in the kernel.

Quoted 7 lines
Quoted 4 lines
Quoted 3 lines

In any case:

Maybe this could be added into 2026.10 release ?

I'm sorry, this completely dissapeared from my radar (It was not assigned to me on patchwork).

No worries, thank you !

#5 lore raw
In reply to Marek Vasut

Hi,

Quoted 6 lines · Marek Vasut

After fsg_unbind() is called on UMS teardown, the fsg_main_thread() which also invokes handle_exception() is unlikely to run afterward. Therefore, any exceptions raised in fsg_unbind(), and also exceptions raised in fsg_common_release() which is called from both fsg_unbind() and fsg_common_init() fail path, will remain unhandled.

Quoted 6 lines

This leads to a problem where FSG_STATE_TERMINATED/EXIT handling is not performed on UMS exit and the do_set_interface(..., NULL) which shuts down endpoints is also not performed, leaving the endpoints enabled.

[...]

Thanks, Applied to https://git.u-boot-project.org/u-boot/custodians/u-boot-dfu (u-boot-dfu)

[1/1] usb: gadget: f_mass_storage: Synchronously tear interface down

      https://git.u-boot-project.org/u-boot/custodians/u-boot-dfu/-/commit/b49d784c2346c0cd84330fe11e0a1a5edb674a67

--
Mattijs