All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 0/2] usb: udc: indroduce more api for lower gadget driver
@ 2021-06-19 15:43 Linyu Yuan
  2021-06-19 15:43 ` [PATCH v3 1/2] usb: udc: core: hide struct usb_gadget_driver to " Linyu Yuan
  2021-06-19 15:43 ` [PATCH v3 2/2] usb: dwc3: fix race of usb_gadget_driver operation Linyu Yuan
  0 siblings, 2 replies; 9+ messages in thread
From: Linyu Yuan @ 2021-06-19 15:43 UTC (permalink / raw)
  To: Felipe Balbi, Thinh Nguyen, Greg Kroah-Hartman
  Cc: linux-usb, linux-kernel, Jack Pham

void usb_gadget_udc_disconnect(struct usb_gadget *);
void usb_gadget_udc_suspend(struct usb_gadget *);
void usb_gadget_udc_resume(struct usb_gadget *);
int usb_gadget_udc_setup(struct usb_gadget *,
			const struct usb_ctrlrequest *);

dwc3 is first driver to use these apis.

v3: fix mail format
v3: fix mail format

Linyu Yuan (2):
  usb: udc: core: hide struct usb_gadget_driver to gadget driver
  usb: dwc3: fix race of usb_gadget_driver operation

 drivers/usb/dwc3/core.h       |  2 --
 drivers/usb/dwc3/ep0.c        |  6 +---
 drivers/usb/dwc3/gadget.c     | 53 +++++++++--------------------------
 drivers/usb/gadget/udc/core.c | 47 ++++++++++++++++++++++++++++++-
 include/linux/usb/gadget.h    |  6 ++++
 5 files changed, 66 insertions(+), 48 deletions(-)

-- 
2.25.1


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

* [PATCH v3 1/2] usb: udc: core: hide struct usb_gadget_driver to gadget driver
  2021-06-19 15:43 [PATCH v3 0/2] usb: udc: indroduce more api for lower gadget driver Linyu Yuan
@ 2021-06-19 15:43 ` Linyu Yuan
  2021-06-20  2:13   ` Alan Stern
  2021-06-19 15:43 ` [PATCH v3 2/2] usb: dwc3: fix race of usb_gadget_driver operation Linyu Yuan
  1 sibling, 1 reply; 9+ messages in thread
From: Linyu Yuan @ 2021-06-19 15:43 UTC (permalink / raw)
  To: Felipe Balbi, Thinh Nguyen, Greg Kroah-Hartman
  Cc: linux-usb, linux-kernel, Jack Pham, Linyu Yuan

currently most gadget driver have a pointer to save
struct usb_gadget_driver from upper layer,
it allow upper layer set and unset of the pointer.

there is race that upper layer unset the pointer first,
but gadget driver use the pointer later,
and it cause system crash due to NULL pointer access.

Signed-off-by: Linyu Yuan <linyyuan@codeaurora.org>
---

v3: no change
v2: no change

 drivers/usb/gadget/udc/core.c | 47 ++++++++++++++++++++++++++++++++++-
 include/linux/usb/gadget.h    |  6 +++++
 2 files changed, 52 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c
index b7f0b1ebaaa8..44af6ff281d3 100644
--- a/drivers/usb/gadget/udc/core.c
+++ b/drivers/usb/gadget/udc/core.c
@@ -1052,6 +1052,45 @@ void usb_udc_vbus_handler(struct usb_gadget *gadget, bool status)
 }
 EXPORT_SYMBOL_GPL(usb_udc_vbus_handler);
 
+void usb_gadget_udc_disconnect(struct usb_gadget *gadget)
+{
+	struct usb_udc *udc = gadget->udc;
+
+	if (udc && udc->driver && udc->driver->disconnect)
+		udc->driver->disconnect(gadget);
+}
+EXPORT_SYMBOL_GPL(usb_gadget_udc_disconnect);
+
+void usb_gadget_udc_suspend(struct usb_gadget *gadget)
+{
+	struct usb_udc *udc = gadget->udc;
+
+	if (udc && udc->driver && udc->driver->suspend)
+		udc->driver->suspend(gadget);
+}
+EXPORT_SYMBOL_GPL(usb_gadget_udc_suspend);
+
+void usb_gadget_udc_resume(struct usb_gadget *gadget)
+{
+	struct usb_udc *udc = gadget->udc;
+
+	if (udc && udc->driver && udc->driver->resume)
+		udc->driver->resume(gadget);
+}
+EXPORT_SYMBOL_GPL(usb_gadget_udc_resume);
+
+int usb_gadget_udc_setup(struct usb_gadget *gadget,
+			const struct usb_ctrlrequest *ctrl)
+{
+	struct usb_udc *udc = gadget->udc;
+
+	if (udc && udc->driver)
+		return udc->driver->setup(gadget, ctrl);
+
+	return -EINVAL;
+}
+EXPORT_SYMBOL_GPL(usb_gadget_udc_setup);
+
 /**
  * usb_gadget_udc_reset - notifies the udc core that bus reset occurs
  * @gadget: The gadget which bus reset occurs
@@ -1064,7 +1103,13 @@ EXPORT_SYMBOL_GPL(usb_udc_vbus_handler);
 void usb_gadget_udc_reset(struct usb_gadget *gadget,
 		struct usb_gadget_driver *driver)
 {
-	driver->reset(gadget);
+	struct usb_udc *udc = gadget->udc;
+
+	if (driver)
+		driver->reset(gadget);
+	else if (udc && udc->driver)
+		udc->driver->reset(gadget);
+
 	usb_gadget_set_state(gadget, USB_STATE_DEFAULT);
 }
 EXPORT_SYMBOL_GPL(usb_gadget_udc_reset);
diff --git a/include/linux/usb/gadget.h b/include/linux/usb/gadget.h
index 75c7538e350a..85f4c87ba5b3 100644
--- a/include/linux/usb/gadget.h
+++ b/include/linux/usb/gadget.h
@@ -881,6 +881,12 @@ extern void usb_gadget_set_state(struct usb_gadget *gadget,
 
 /*-------------------------------------------------------------------------*/
 
+extern void usb_gadget_udc_disconnect(struct usb_gadget *gadget);
+extern void usb_gadget_udc_suspend(struct usb_gadget *gadget);
+extern void usb_gadget_udc_resume(struct usb_gadget *gadget);
+extern int usb_gadget_udc_setup(struct usb_gadget *gadget,
+			const struct usb_ctrlrequest *ctrl);
+
 /* utility to tell udc core that the bus reset occurs */
 extern void usb_gadget_udc_reset(struct usb_gadget *gadget,
 		struct usb_gadget_driver *driver);
-- 
2.25.1


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

* [PATCH v3 2/2] usb: dwc3: fix race of usb_gadget_driver operation
  2021-06-19 15:43 [PATCH v3 0/2] usb: udc: indroduce more api for lower gadget driver Linyu Yuan
  2021-06-19 15:43 ` [PATCH v3 1/2] usb: udc: core: hide struct usb_gadget_driver to " Linyu Yuan
@ 2021-06-19 15:43 ` Linyu Yuan
  1 sibling, 0 replies; 9+ messages in thread
From: Linyu Yuan @ 2021-06-19 15:43 UTC (permalink / raw)
  To: Felipe Balbi, Thinh Nguyen, Greg Kroah-Hartman
  Cc: linux-usb, linux-kernel, Jack Pham, Linyu Yuan

there is following race condition,

      CPU1                         CPU2
dwc3_runtime_suspend()        dwc3_gadget_stop()
spin_lock(&dwc->lock)
dwc3_gadget_suspend()
dwc3_disconnect_gadget()
dwc->gadget_driver != NULL
spin_unlock(&dwc->lock)
                              spin_lock(&dwc->lock)
                              dwc->gadget_driver = NULL
                              spin_unlock(&dwc->lock)
dwc->gadget_driver->disconnect(dwc->gadget);

system will crash when access NULL pointer.

this change will remove pointer of upper layer
struct usb_gadget_driver,
use UDC safe api to access necessary function.

Signed-off-by: Linyu Yuan <linyyuan@codeaurora.org>
---

v3: no change
v2: no change

 drivers/usb/dwc3/core.h   |  2 --
 drivers/usb/dwc3/ep0.c    |  6 +----
 drivers/usb/dwc3/gadget.c | 53 ++++++++++-----------------------------
 3 files changed, 14 insertions(+), 47 deletions(-)

diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h
index dccdf13b5f9e..4db64d103446 100644
--- a/drivers/usb/dwc3/core.h
+++ b/drivers/usb/dwc3/core.h
@@ -962,7 +962,6 @@ struct dwc3_scratchpad_array {
  * @ev_buf: struct dwc3_event_buffer pointer
  * @eps: endpoint array
  * @gadget: device side representation of the peripheral controller
- * @gadget_driver: pointer to the gadget driver
  * @clks: array of clocks
  * @num_clks: number of clocks
  * @reset: reset control
@@ -1108,7 +1107,6 @@ struct dwc3 {
 	struct dwc3_ep		*eps[DWC3_ENDPOINTS_NUM];
 
 	struct usb_gadget	*gadget;
-	struct usb_gadget_driver *gadget_driver;
 
 	struct clk_bulk_data	*clks;
 	int			num_clks;
diff --git a/drivers/usb/dwc3/ep0.c b/drivers/usb/dwc3/ep0.c
index 3cd294264372..89f63be00902 100644
--- a/drivers/usb/dwc3/ep0.c
+++ b/drivers/usb/dwc3/ep0.c
@@ -600,7 +600,7 @@ static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
 	int ret;
 
 	spin_unlock(&dwc->lock);
-	ret = dwc->gadget_driver->setup(dwc->gadget, ctrl);
+	ret = usb_gadget_udc_setup(dwc->gadget, ctrl);
 	spin_lock(&dwc->lock);
 	return ret;
 }
@@ -795,9 +795,6 @@ static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
 	int ret = -EINVAL;
 	u32 len;
 
-	if (!dwc->gadget_driver)
-		goto out;
-
 	trace_dwc3_ctrl_req(ctrl);
 
 	len = le16_to_cpu(ctrl->wLength);
@@ -819,7 +816,6 @@ static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
 	if (ret == USB_GADGET_DELAYED_STATUS)
 		dwc->delayed_status = true;
 
-out:
 	if (ret < 0)
 		dwc3_ep0_stall_and_restart(dwc);
 }
diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c
index af6d7f157989..8cb7a9826ee1 100644
--- a/drivers/usb/dwc3/gadget.c
+++ b/drivers/usb/dwc3/gadget.c
@@ -2464,7 +2464,6 @@ static int dwc3_gadget_start(struct usb_gadget *g,
 		struct usb_gadget_driver *driver)
 {
 	struct dwc3		*dwc = gadget_to_dwc(g);
-	unsigned long		flags;
 	int			ret;
 	int			irq;
 
@@ -2477,10 +2476,6 @@ static int dwc3_gadget_start(struct usb_gadget *g,
 		return ret;
 	}
 
-	spin_lock_irqsave(&dwc->lock, flags);
-	dwc->gadget_driver	= driver;
-	spin_unlock_irqrestore(&dwc->lock, flags);
-
 	return 0;
 }
 
@@ -2494,11 +2489,6 @@ static void __dwc3_gadget_stop(struct dwc3 *dwc)
 static int dwc3_gadget_stop(struct usb_gadget *g)
 {
 	struct dwc3		*dwc = gadget_to_dwc(g);
-	unsigned long		flags;
-
-	spin_lock_irqsave(&dwc->lock, flags);
-	dwc->gadget_driver	= NULL;
-	spin_unlock_irqrestore(&dwc->lock, flags);
 
 	free_irq(dwc->irq_gadget, dwc->ev_buf);
 
@@ -3231,39 +3221,30 @@ static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
 
 static void dwc3_disconnect_gadget(struct dwc3 *dwc)
 {
-	if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
-		spin_unlock(&dwc->lock);
-		dwc->gadget_driver->disconnect(dwc->gadget);
-		spin_lock(&dwc->lock);
-	}
+	spin_unlock(&dwc->lock);
+	usb_gadget_udc_disconnect(dwc->gadget);
+	spin_lock(&dwc->lock);
 }
 
 static void dwc3_suspend_gadget(struct dwc3 *dwc)
 {
-	if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
-		spin_unlock(&dwc->lock);
-		dwc->gadget_driver->suspend(dwc->gadget);
-		spin_lock(&dwc->lock);
-	}
+	spin_unlock(&dwc->lock);
+	usb_gadget_udc_suspend(dwc->gadget);
+	spin_lock(&dwc->lock);
 }
 
 static void dwc3_resume_gadget(struct dwc3 *dwc)
 {
-	if (dwc->gadget_driver && dwc->gadget_driver->resume) {
-		spin_unlock(&dwc->lock);
-		dwc->gadget_driver->resume(dwc->gadget);
-		spin_lock(&dwc->lock);
-	}
+	spin_unlock(&dwc->lock);
+	usb_gadget_udc_resume(dwc->gadget);
+	spin_lock(&dwc->lock);
 }
 
 static void dwc3_reset_gadget(struct dwc3 *dwc)
 {
-	if (!dwc->gadget_driver)
-		return;
-
 	if (dwc->gadget->speed != USB_SPEED_UNKNOWN) {
 		spin_unlock(&dwc->lock);
-		usb_gadget_udc_reset(dwc->gadget, dwc->gadget_driver);
+		usb_gadget_udc_reset(dwc->gadget, NULL);
 		spin_lock(&dwc->lock);
 	}
 }
@@ -3585,11 +3566,9 @@ static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
 	 * implemented.
 	 */
 
-	if (dwc->gadget_driver && dwc->gadget_driver->resume) {
-		spin_unlock(&dwc->lock);
-		dwc->gadget_driver->resume(dwc->gadget);
-		spin_lock(&dwc->lock);
-	}
+	spin_unlock(&dwc->lock);
+	usb_gadget_udc_resume(dwc->gadget);
+	spin_lock(&dwc->lock);
 }
 
 static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
@@ -4085,9 +4064,6 @@ void dwc3_gadget_exit(struct dwc3 *dwc)
 
 int dwc3_gadget_suspend(struct dwc3 *dwc)
 {
-	if (!dwc->gadget_driver)
-		return 0;
-
 	dwc3_gadget_run_stop(dwc, false, false);
 	dwc3_disconnect_gadget(dwc);
 	__dwc3_gadget_stop(dwc);
@@ -4099,9 +4075,6 @@ int dwc3_gadget_resume(struct dwc3 *dwc)
 {
 	int			ret;
 
-	if (!dwc->gadget_driver)
-		return 0;
-
 	ret = __dwc3_gadget_start(dwc);
 	if (ret < 0)
 		goto err0;
-- 
2.25.1


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

* Re: [PATCH v3 1/2] usb: udc: core: hide struct usb_gadget_driver to gadget driver
  2021-06-19 15:43 ` [PATCH v3 1/2] usb: udc: core: hide struct usb_gadget_driver to " Linyu Yuan
@ 2021-06-20  2:13   ` Alan Stern
  2021-06-20  3:46     ` linyyuan
  0 siblings, 1 reply; 9+ messages in thread
From: Alan Stern @ 2021-06-20  2:13 UTC (permalink / raw)
  To: Linyu Yuan
  Cc: Felipe Balbi, Thinh Nguyen, Greg Kroah-Hartman, linux-usb,
	linux-kernel, Jack Pham

On Sat, Jun 19, 2021 at 11:43:08PM +0800, Linyu Yuan wrote:
> currently most gadget driver have a pointer to save
> struct usb_gadget_driver from upper layer,
> it allow upper layer set and unset of the pointer.
> 
> there is race that upper layer unset the pointer first,
> but gadget driver use the pointer later,
> and it cause system crash due to NULL pointer access.

This race has already been fixed in Greg's usb-next branch.  See commit 
7dc0c55e9f30 ("USB: UDC core: Add udc_async_callbacks gadget op") and 
following commits 04145a03db9d ("USB: UDC: Implement 
udc_async_callbacks in dummy-hcd") and b42e8090ba93 ("USB: UDC: 
Implement udc_async_callbacks in net2280").

You just need to write a corresponding patch implementing the 
async_callbacks op for dwc3.

Alan Stern

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

* Re: [PATCH v3 1/2] usb: udc: core: hide struct usb_gadget_driver to gadget driver
  2021-06-20  2:13   ` Alan Stern
@ 2021-06-20  3:46     ` linyyuan
  2021-06-20  3:53       ` linyyuan
  0 siblings, 1 reply; 9+ messages in thread
From: linyyuan @ 2021-06-20  3:46 UTC (permalink / raw)
  To: Alan Stern
  Cc: Felipe Balbi, Thinh Nguyen, Greg Kroah-Hartman, linux-usb,
	linux-kernel, Jack Pham

On 2021-06-20 10:13, Alan Stern wrote:
> On Sat, Jun 19, 2021 at 11:43:08PM +0800, Linyu Yuan wrote:
>> currently most gadget driver have a pointer to save
>> struct usb_gadget_driver from upper layer,
>> it allow upper layer set and unset of the pointer.
>> 
>> there is race that upper layer unset the pointer first,
>> but gadget driver use the pointer later,
>> and it cause system crash due to NULL pointer access.
> 
> This race has already been fixed in Greg's usb-next branch.  See commit
> 7dc0c55e9f30 ("USB: UDC core: Add udc_async_callbacks gadget op") and
> following commits 04145a03db9d ("USB: UDC: Implement
> udc_async_callbacks in dummy-hcd") and b42e8090ba93 ("USB: UDC:
> Implement udc_async_callbacks in net2280").
> 
thanks, this is better, lower driver only need change several places.
> You just need to write a corresponding patch implementing the
> async_callbacks op for dwc3.
yes, i will do.
> 
> Alan Stern

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

* Re: [PATCH v3 1/2] usb: udc: core: hide struct usb_gadget_driver to gadget driver
  2021-06-20  3:46     ` linyyuan
@ 2021-06-20  3:53       ` linyyuan
  2021-06-20 13:47         ` Alan Stern
  0 siblings, 1 reply; 9+ messages in thread
From: linyyuan @ 2021-06-20  3:53 UTC (permalink / raw)
  To: Alan Stern
  Cc: Felipe Balbi, Thinh Nguyen, Greg Kroah-Hartman, linux-usb,
	linux-kernel, Jack Pham

On 2021-06-20 11:46, linyyuan@codeaurora.org wrote:
> On 2021-06-20 10:13, Alan Stern wrote:
>> On Sat, Jun 19, 2021 at 11:43:08PM +0800, Linyu Yuan wrote:
>>> currently most gadget driver have a pointer to save
>>> struct usb_gadget_driver from upper layer,
>>> it allow upper layer set and unset of the pointer.
>>> 
>>> there is race that upper layer unset the pointer first,
>>> but gadget driver use the pointer later,
>>> and it cause system crash due to NULL pointer access.
>> 
>> This race has already been fixed in Greg's usb-next branch.  See 
>> commit
>> 7dc0c55e9f30 ("USB: UDC core: Add udc_async_callbacks gadget op") and
>> following commits 04145a03db9d ("USB: UDC: Implement
>> udc_async_callbacks in dummy-hcd") and b42e8090ba93 ("USB: UDC:
>> Implement udc_async_callbacks in net2280").
>> 
> thanks, this is better, lower driver only need change several places.
>> You just need to write a corresponding patch implementing the
>> async_callbacks op for dwc3.
> yes, i will do.
>> 
Alan, i want to discuss your suggestion again in b42e8090ba93 ("USB: 
UDC:
Implement udc_async_callbacks in net2280")

+                       if (dev->async_callbacks) { ----> if CPU1 saw 
this is true
+                               spin_unlock(&dev->lock); ---> CPU2 get 
lock after this unlock,
it will set async_callbacks to false, then follow call also crash, right 
?
+                               tmp = dev->driver->setup(&dev->gadget, 
&u.r);
+                               spin_lock(&dev->lock);
+                       }

>> Alan Stern

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

* Re: [PATCH v3 1/2] usb: udc: core: hide struct usb_gadget_driver to gadget driver
  2021-06-20  3:53       ` linyyuan
@ 2021-06-20 13:47         ` Alan Stern
  2021-06-21  1:37           ` linyyuan
  0 siblings, 1 reply; 9+ messages in thread
From: Alan Stern @ 2021-06-20 13:47 UTC (permalink / raw)
  To: linyyuan
  Cc: Felipe Balbi, Thinh Nguyen, Greg Kroah-Hartman, linux-usb,
	linux-kernel, Jack Pham

On Sun, Jun 20, 2021 at 11:53:18AM +0800, linyyuan@codeaurora.org wrote:
> On 2021-06-20 11:46, linyyuan@codeaurora.org wrote:
> > On 2021-06-20 10:13, Alan Stern wrote:
> > > On Sat, Jun 19, 2021 at 11:43:08PM +0800, Linyu Yuan wrote:
> > > > currently most gadget driver have a pointer to save
> > > > struct usb_gadget_driver from upper layer,
> > > > it allow upper layer set and unset of the pointer.
> > > > 
> > > > there is race that upper layer unset the pointer first,
> > > > but gadget driver use the pointer later,
> > > > and it cause system crash due to NULL pointer access.
> > > 
> > > This race has already been fixed in Greg's usb-next branch.  See
> > > commit
> > > 7dc0c55e9f30 ("USB: UDC core: Add udc_async_callbacks gadget op") and
> > > following commits 04145a03db9d ("USB: UDC: Implement
> > > udc_async_callbacks in dummy-hcd") and b42e8090ba93 ("USB: UDC:
> > > Implement udc_async_callbacks in net2280").
> > > 
> > thanks, this is better, lower driver only need change several places.
> > > You just need to write a corresponding patch implementing the
> > > async_callbacks op for dwc3.
> > yes, i will do.
> > > 
> Alan, i want to discuss your suggestion again in b42e8090ba93 ("USB: UDC:
> Implement udc_async_callbacks in net2280")
> 
> +                       if (dev->async_callbacks) { ----> if CPU1 saw this
> is true
> +                               spin_unlock(&dev->lock); ---> CPU2 get lock
> after this unlock,
> it will set async_callbacks to false, then follow call also crash, right ?
> +                               tmp = dev->driver->setup(&dev->gadget,
> &u.r);
> +                               spin_lock(&dev->lock);
> +                       }

No, this is okay.  The reason is because usb_gadget_remove_driver (CPU2 
in your example) does this:

        usb_gadget_disable_async_callbacks(udc);
        if (udc->gadget->irq)
                synchronize_irq(udc->gadget->irq);
        udc->driver->unbind(udc->gadget);
        usb_gadget_udc_stop(udc);

The synchronize_irq call will make CPU2 wait until CPU1 has finished 
handling the interrupt for the setup packet.  The system won't crash, 
because dev->driver->setup will be called before unbind and udc_stop 
instead of after.

Alan Stern

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

* Re: [PATCH v3 1/2] usb: udc: core: hide struct usb_gadget_driver to gadget driver
  2021-06-20 13:47         ` Alan Stern
@ 2021-06-21  1:37           ` linyyuan
  2021-06-21 13:53             ` Alan Stern
  0 siblings, 1 reply; 9+ messages in thread
From: linyyuan @ 2021-06-21  1:37 UTC (permalink / raw)
  To: Alan Stern
  Cc: Felipe Balbi, Thinh Nguyen, Greg Kroah-Hartman, linux-usb,
	linux-kernel, Jack Pham

On 2021-06-20 21:47, Alan Stern wrote:
> On Sun, Jun 20, 2021 at 11:53:18AM +0800, linyyuan@codeaurora.org 
> wrote:
>> On 2021-06-20 11:46, linyyuan@codeaurora.org wrote:
>> > On 2021-06-20 10:13, Alan Stern wrote:
>> > > On Sat, Jun 19, 2021 at 11:43:08PM +0800, Linyu Yuan wrote:
>> > > > currently most gadget driver have a pointer to save
>> > > > struct usb_gadget_driver from upper layer,
>> > > > it allow upper layer set and unset of the pointer.
>> > > >
>> > > > there is race that upper layer unset the pointer first,
>> > > > but gadget driver use the pointer later,
>> > > > and it cause system crash due to NULL pointer access.
>> > >
>> > > This race has already been fixed in Greg's usb-next branch.  See
>> > > commit
>> > > 7dc0c55e9f30 ("USB: UDC core: Add udc_async_callbacks gadget op") and
>> > > following commits 04145a03db9d ("USB: UDC: Implement
>> > > udc_async_callbacks in dummy-hcd") and b42e8090ba93 ("USB: UDC:
>> > > Implement udc_async_callbacks in net2280").
>> > >
>> > thanks, this is better, lower driver only need change several places.
>> > > You just need to write a corresponding patch implementing the
>> > > async_callbacks op for dwc3.
>> > yes, i will do.
>> > >
>> Alan, i want to discuss your suggestion again in b42e8090ba93 ("USB: 
>> UDC:
>> Implement udc_async_callbacks in net2280")
>> 
>> +                       if (dev->async_callbacks) { ----> if CPU1 saw 
>> this
>> is true
>> +                               spin_unlock(&dev->lock); ---> CPU2 get 
>> lock
>> after this unlock,
>> it will set async_callbacks to false, then follow call also crash, 
>> right ?
>> +                               tmp = dev->driver->setup(&dev->gadget,
>> &u.r);
>> +                               spin_lock(&dev->lock);
>> +                       }
> 
> No, this is okay.  The reason is because usb_gadget_remove_driver (CPU2
> in your example) does this:
> 
>         usb_gadget_disable_async_callbacks(udc);
>         if (udc->gadget->irq)
>                 synchronize_irq(udc->gadget->irq);
>         udc->driver->unbind(udc->gadget);
>         usb_gadget_udc_stop(udc);
> 
> The synchronize_irq call will make CPU2 wait until CPU1 has finished
> handling the interrupt for the setup packet.  The system won't crash,
> because dev->driver->setup will be called before unbind and udc_stop
> instead of after.
still several question,
1. how about suspend calll dev->driver->suspend ?
2. will 04145a03db9d ("USB: UDC: Implement udc_async_callbacks in 
dummy-hcd") backport to LTS branch ?
3. how about coding style ? so following code
if (foo->gadget_driver && foo->gadget_driver->resume)
change to
if (foo->asnyc_callbacks && foo->gadget_driver->resume)
> 
> Alan Stern

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

* Re: [PATCH v3 1/2] usb: udc: core: hide struct usb_gadget_driver to gadget driver
  2021-06-21  1:37           ` linyyuan
@ 2021-06-21 13:53             ` Alan Stern
  0 siblings, 0 replies; 9+ messages in thread
From: Alan Stern @ 2021-06-21 13:53 UTC (permalink / raw)
  To: linyyuan
  Cc: Felipe Balbi, Thinh Nguyen, Greg Kroah-Hartman, linux-usb,
	linux-kernel, Jack Pham

On Mon, Jun 21, 2021 at 09:37:34AM +0800, linyyuan@codeaurora.org wrote:
> On 2021-06-20 21:47, Alan Stern wrote:
> > On Sun, Jun 20, 2021 at 11:53:18AM +0800, linyyuan@codeaurora.org wrote:
> > > On 2021-06-20 11:46, linyyuan@codeaurora.org wrote:
> > > > On 2021-06-20 10:13, Alan Stern wrote:
> > > > > On Sat, Jun 19, 2021 at 11:43:08PM +0800, Linyu Yuan wrote:
> > > > > > currently most gadget driver have a pointer to save
> > > > > > struct usb_gadget_driver from upper layer,
> > > > > > it allow upper layer set and unset of the pointer.
> > > > > >
> > > > > > there is race that upper layer unset the pointer first,
> > > > > > but gadget driver use the pointer later,
> > > > > > and it cause system crash due to NULL pointer access.
> > > > >
> > > > > This race has already been fixed in Greg's usb-next branch.  See
> > > > > commit
> > > > > 7dc0c55e9f30 ("USB: UDC core: Add udc_async_callbacks gadget op") and
> > > > > following commits 04145a03db9d ("USB: UDC: Implement
> > > > > udc_async_callbacks in dummy-hcd") and b42e8090ba93 ("USB: UDC:
> > > > > Implement udc_async_callbacks in net2280").
> > > > >
> > > > thanks, this is better, lower driver only need change several places.
> > > > > You just need to write a corresponding patch implementing the
> > > > > async_callbacks op for dwc3.
> > > > yes, i will do.
> > > > >
> > > Alan, i want to discuss your suggestion again in b42e8090ba93 ("USB:
> > > UDC:
> > > Implement udc_async_callbacks in net2280")
> > > 
> > > +                       if (dev->async_callbacks) { ----> if CPU1
> > > saw this
> > > is true
> > > +                               spin_unlock(&dev->lock); ---> CPU2
> > > get lock
> > > after this unlock,
> > > it will set async_callbacks to false, then follow call also crash,
> > > right ?
> > > +                               tmp = dev->driver->setup(&dev->gadget,
> > > &u.r);
> > > +                               spin_lock(&dev->lock);
> > > +                       }
> > 
> > No, this is okay.  The reason is because usb_gadget_remove_driver (CPU2
> > in your example) does this:
> > 
> >         usb_gadget_disable_async_callbacks(udc);
> >         if (udc->gadget->irq)
> >                 synchronize_irq(udc->gadget->irq);
> >         udc->driver->unbind(udc->gadget);
> >         usb_gadget_udc_stop(udc);
> > 
> > The synchronize_irq call will make CPU2 wait until CPU1 has finished
> > handling the interrupt for the setup packet.  The system won't crash,
> > because dev->driver->setup will be called before unbind and udc_stop
> > instead of after.

> still several question,
> 1. how about suspend calll dev->driver->suspend ?

The same reasoning applies.  The synchronize_irq call will make CPU2 
wait until CPU1 has finished handling the interrupt for the USB bus 
suspend.  The system won't crash, because dev->driver->suspend will be 
called before unbind and udc_stop instead of after.

> 2. will 04145a03db9d ("USB: UDC: Implement udc_async_callbacks in
> dummy-hcd") backport to LTS branch ?

None of these commits are marked for back-porting to the -stable 
kernels.  The race they fix does not occur often.

If you the commits to be applied to the LTS stable kernels, you can ask 
Greg KH to do it.

> 3. how about coding style ? so following code
> if (foo->gadget_driver && foo->gadget_driver->resume)
> change to
> if (foo->asnyc_callbacks && foo->gadget_driver->resume)

I don't understand this question.

Alan Stern

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

end of thread, other threads:[~2021-06-21 13:53 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-19 15:43 [PATCH v3 0/2] usb: udc: indroduce more api for lower gadget driver Linyu Yuan
2021-06-19 15:43 ` [PATCH v3 1/2] usb: udc: core: hide struct usb_gadget_driver to " Linyu Yuan
2021-06-20  2:13   ` Alan Stern
2021-06-20  3:46     ` linyyuan
2021-06-20  3:53       ` linyyuan
2021-06-20 13:47         ` Alan Stern
2021-06-21  1:37           ` linyyuan
2021-06-21 13:53             ` Alan Stern
2021-06-19 15:43 ` [PATCH v3 2/2] usb: dwc3: fix race of usb_gadget_driver operation Linyu Yuan

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.