--- /dev/null
+From 73ea73affe8622bdf292de898da869d441da6a9d Mon Sep 17 00:00:00 2001
+From: Roy Luo <royluo@google.com>
+Date: Tue, 28 Nov 2023 22:17:56 +0000
+Subject: USB: gadget: core: adjust uevent timing on gadget unbind
+
+From: Roy Luo <royluo@google.com>
+
+commit 73ea73affe8622bdf292de898da869d441da6a9d upstream.
+
+The KOBJ_CHANGE uevent is sent before gadget unbind is actually
+executed, resulting in inaccurate uevent emitted at incorrect timing
+(the uevent would have USB_UDC_DRIVER variable set while it would
+soon be removed).
+Move the KOBJ_CHANGE uevent to the end of the unbind function so that
+uevent is sent only after the change has been made.
+
+Fixes: 2ccea03a8f7e ("usb: gadget: introduce UDC Class")
+Cc: stable@vger.kernel.org
+Signed-off-by: Roy Luo <royluo@google.com>
+Link: https://lore.kernel.org/r/20231128221756.2591158-1-royluo@google.com
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+---
+ drivers/usb/gadget/udc/core.c | 4 ++--
+ 1 file changed, 2 insertions(+), 2 deletions(-)
+
+--- a/drivers/usb/gadget/udc/core.c
++++ b/drivers/usb/gadget/udc/core.c
+@@ -1451,8 +1451,6 @@ static void usb_gadget_remove_driver(str
+ dev_dbg(&udc->dev, "unregistering UDC driver [%s]\n",
+ udc->driver->function);
+
+- kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
+-
+ usb_gadget_disconnect(udc->gadget);
+ usb_gadget_disable_async_callbacks(udc);
+ if (udc->gadget->irq)
+@@ -1462,6 +1460,8 @@ static void usb_gadget_remove_driver(str
+
+ udc->driver = NULL;
+ udc->gadget->dev.driver = NULL;
++
++ kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
+ }
+
+ /**