FROMLIST: usb: dwc3: avoid NULL access of usb_gadget_driver

we found crash in dwc3_disconnect_gadget(),
it is because dwc->gadget_driver become NULL before async access.
7dc0c55e9f30 ('USB: UDC core: Add udc_async_callbacks gadget op')
suggest a common way to avoid such kind of issue.

this change implment the callback in dwc3 and
change related functions which have callback to usb gadget driver.

Signed-off-by: Linyu Yuan <linyyuan@codeaurora.org>
Bug: 193006095
Link: https://lore.kernel.org/linux-usb/20210629015118.7944-1-linyyuan@codeaurora.org
Change-Id: Id6774f7f6b7c8d31338128ffc33f01f5575f4d16
Signed-off-by: Jack Pham <jackp@codeaurora.org>
This commit is contained in:
Linyu Yuan
2021-06-29 09:51:18 +08:00
committed by Todd Kjos
parent 5bb2dd8d39
commit 26920e0f3a
3 changed files with 23 additions and 9 deletions

View File

@@ -1304,6 +1304,7 @@ struct dwc3 {
unsigned dis_metastability_quirk:1; unsigned dis_metastability_quirk:1;
unsigned dis_split_quirk:1; unsigned dis_split_quirk:1;
unsigned async_callbacks:1;
u16 imod_interval; u16 imod_interval;

View File

@@ -594,11 +594,13 @@ static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
{ {
int ret; int ret = -EINVAL;
spin_unlock(&dwc->lock); if (dwc->async_callbacks) {
ret = dwc->gadget_driver->setup(dwc->gadget, ctrl); spin_unlock(&dwc->lock);
spin_lock(&dwc->lock); ret = dwc->gadget_driver->setup(dwc->gadget, ctrl);
spin_lock(&dwc->lock);
}
return ret; return ret;
} }

View File

@@ -2828,6 +2828,16 @@ static int dwc3_gadget_check_config(struct usb_gadget *g)
return 0; return 0;
} }
static void dwc3_gadget_async_callbacks(struct usb_gadget *g, bool enable)
{
struct dwc3 *dwc = gadget_to_dwc(g);
unsigned long flags;
spin_lock_irqsave(&dwc->lock, flags);
dwc->async_callbacks = enable;
spin_unlock_irqrestore(&dwc->lock, flags);
}
static const struct usb_gadget_ops dwc3_gadget_ops = { static const struct usb_gadget_ops dwc3_gadget_ops = {
.get_frame = dwc3_gadget_get_frame, .get_frame = dwc3_gadget_get_frame,
.wakeup = dwc3_gadget_wakeup, .wakeup = dwc3_gadget_wakeup,
@@ -2840,6 +2850,7 @@ static const struct usb_gadget_ops dwc3_gadget_ops = {
.get_config_params = dwc3_gadget_config_params, .get_config_params = dwc3_gadget_config_params,
.vbus_draw = dwc3_gadget_vbus_draw, .vbus_draw = dwc3_gadget_vbus_draw,
.check_config = dwc3_gadget_check_config, .check_config = dwc3_gadget_check_config,
.udc_async_callbacks = dwc3_gadget_async_callbacks,
}; };
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
@@ -3473,7 +3484,7 @@ static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
static void dwc3_disconnect_gadget(struct dwc3 *dwc) static void dwc3_disconnect_gadget(struct dwc3 *dwc)
{ {
if (dwc->gadget_driver && dwc->gadget_driver->disconnect) { if (dwc->async_callbacks && dwc->gadget_driver->disconnect) {
spin_unlock(&dwc->lock); spin_unlock(&dwc->lock);
dwc->gadget_driver->disconnect(dwc->gadget); dwc->gadget_driver->disconnect(dwc->gadget);
spin_lock(&dwc->lock); spin_lock(&dwc->lock);
@@ -3482,7 +3493,7 @@ static void dwc3_disconnect_gadget(struct dwc3 *dwc)
static void dwc3_suspend_gadget(struct dwc3 *dwc) static void dwc3_suspend_gadget(struct dwc3 *dwc)
{ {
if (dwc->gadget_driver && dwc->gadget_driver->suspend) { if (dwc->async_callbacks && dwc->gadget_driver->suspend) {
spin_unlock(&dwc->lock); spin_unlock(&dwc->lock);
dwc->gadget_driver->suspend(dwc->gadget); dwc->gadget_driver->suspend(dwc->gadget);
spin_lock(&dwc->lock); spin_lock(&dwc->lock);
@@ -3491,7 +3502,7 @@ static void dwc3_suspend_gadget(struct dwc3 *dwc)
static void dwc3_resume_gadget(struct dwc3 *dwc) static void dwc3_resume_gadget(struct dwc3 *dwc)
{ {
if (dwc->gadget_driver && dwc->gadget_driver->resume) { if (dwc->async_callbacks && dwc->gadget_driver->resume) {
spin_unlock(&dwc->lock); spin_unlock(&dwc->lock);
dwc->gadget_driver->resume(dwc->gadget); dwc->gadget_driver->resume(dwc->gadget);
spin_lock(&dwc->lock); spin_lock(&dwc->lock);
@@ -3503,7 +3514,7 @@ static void dwc3_reset_gadget(struct dwc3 *dwc)
if (!dwc->gadget_driver) if (!dwc->gadget_driver)
return; return;
if (dwc->gadget->speed != USB_SPEED_UNKNOWN) { if (dwc->async_callbacks && dwc->gadget->speed != USB_SPEED_UNKNOWN) {
spin_unlock(&dwc->lock); spin_unlock(&dwc->lock);
usb_gadget_udc_reset(dwc->gadget, dwc->gadget_driver); usb_gadget_udc_reset(dwc->gadget, dwc->gadget_driver);
spin_lock(&dwc->lock); spin_lock(&dwc->lock);
@@ -3833,7 +3844,7 @@ static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
* implemented. * implemented.
*/ */
if (dwc->gadget_driver && dwc->gadget_driver->resume) { if (dwc->async_callbacks && dwc->gadget_driver->resume) {
spin_unlock(&dwc->lock); spin_unlock(&dwc->lock);
dwc->gadget_driver->resume(dwc->gadget); dwc->gadget_driver->resume(dwc->gadget);
spin_lock(&dwc->lock); spin_lock(&dwc->lock);