diff mbox series

usb: udc: core: Use lock for soft_connect

Message ID 8262fabe3aa7c02981f3b9d302461804c451ea5a.1610493934.git.Thinh.Nguyen@synopsys.com
State New
Headers show
Series usb: udc: core: Use lock for soft_connect | expand

Commit Message

Thinh Nguyen Jan. 12, 2021, 11:26 p.m. UTC
Use lock to guard against concurrent access for soft-connect/disconnect
operations when writing to soft_connect sysfs.

Cc: stable@vger.kernel.org
Fixes: 2ccea03a8f7e ("usb: gadget: introduce UDC Class")
Signed-off-by: Thinh Nguyen <Thinh.Nguyen@synopsys.com>
---
 drivers/usb/gadget/udc/core.c | 4 ++++
 1 file changed, 4 insertions(+)


base-commit: 4e0dcf62ab4cf917d0cbe751b8bf229a065248d4

Comments

Peter Chen Jan. 13, 2021, 1:27 a.m. UTC | #1
> To: Felipe Balbi <balbi@kernel.org>; Greg Kroah-Hartman
> <gregkh@linuxfoundation.org>; linux-usb@vger.kernel.org; Peter Chen
> <peter.chen@nxp.com>; Lee Jones <lee.jones@linaro.org>; Alan Stern
> <stern@rowland.harvard.edu>; Thomas Gleixner <tglx@linutronix.de>; Dejin
> Zheng <zhengdejin5@gmail.com>; Sebastian Andrzej Siewior
> <bigeasy@linutronix.de>; Ahmed S. Darwish <a.darwish@linutronix.de>;
> Marek Szyprowski <m.szyprowski@samsung.com>; Michal Nazarewicz
> <mina86@mina86.com>
> Cc: stable@vger.kernel.org
> Subject: [PATCH] usb: udc: core: Use lock for soft_connect
> 
> Use lock to guard against concurrent access for soft-connect/disconnect
> operations when writing to soft_connect sysfs.
> 
> Cc: stable@vger.kernel.org
> Fixes: 2ccea03a8f7e ("usb: gadget: introduce UDC Class")
> Signed-off-by: Thinh Nguyen <Thinh.Nguyen@synopsys.com>

Reviewed-by: Peter Chen <peter.chen@kernel.org>

Peter

> ---
>  drivers/usb/gadget/udc/core.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c
> index 6a62bbd01324..44c67e765167 100644
> --- a/drivers/usb/gadget/udc/core.c
> +++ b/drivers/usb/gadget/udc/core.c
> @@ -1530,7 +1530,9 @@ static ssize_t soft_connect_store(struct device *dev,
> {
>  	struct usb_udc		*udc = container_of(dev, struct usb_udc, dev);
> 
> +	mutex_lock(&udc_lock);
>  	if (!udc->driver) {
> +		mutex_unlock(&udc_lock);
>  		dev_err(dev, "soft-connect without a gadget driver\n");
>  		return -EOPNOTSUPP;
>  	}
> @@ -1542,10 +1544,12 @@ static ssize_t soft_connect_store(struct device
> *dev,
>  		usb_gadget_disconnect(udc->gadget);
>  		usb_gadget_udc_stop(udc);
>  	} else {
> +		mutex_unlock(&udc_lock);
>  		dev_err(dev, "unsupported command '%s'\n", buf);
>  		return -EINVAL;
>  	}
> 
> +	mutex_unlock(&udc_lock);
>  	return n;
>  }
>  static DEVICE_ATTR_WO(soft_connect);
> 
> base-commit: 4e0dcf62ab4cf917d0cbe751b8bf229a065248d4
> --
> 2.28.0
Ahmed S. Darwish Jan. 13, 2021, 8:08 a.m. UTC | #2
On Tue, Jan 12, 2021 at 03:26:21PM -0800, Thinh Nguyen wrote:
...
>

> +	mutex_lock(&udc_lock);

>  	if (!udc->driver) {

> +		mutex_unlock(&udc_lock);

>  		dev_err(dev, "soft-connect without a gadget driver\n");

>  		return -EOPNOTSUPP;

>  	}

> @@ -1542,10 +1544,12 @@ static ssize_t soft_connect_store(struct device *dev,

>  		usb_gadget_disconnect(udc->gadget);

>  		usb_gadget_udc_stop(udc);

>  	} else {

> +		mutex_unlock(&udc_lock);

>  		dev_err(dev, "unsupported command '%s'\n", buf);

>  		return -EINVAL;

>  	}

>

> +	mutex_unlock(&udc_lock);

>  	return n;

>  }


Please use "goto out" instead of repeating the mutex unlock line three
times.

Thanks,

--
Ahmed S. Darwish
Linutronix GmbH
Thinh Nguyen Jan. 14, 2021, 2:39 a.m. UTC | #3
Peter Chen wrote:
>  

>> To: Felipe Balbi <balbi@kernel.org>; Greg Kroah-Hartman

>> <gregkh@linuxfoundation.org>; linux-usb@vger.kernel.org; Peter Chen

>> <peter.chen@nxp.com>; Lee Jones <lee.jones@linaro.org>; Alan Stern

>> <stern@rowland.harvard.edu>; Thomas Gleixner <tglx@linutronix.de>; Dejin

>> Zheng <zhengdejin5@gmail.com>; Sebastian Andrzej Siewior

>> <bigeasy@linutronix.de>; Ahmed S. Darwish <a.darwish@linutronix.de>;

>> Marek Szyprowski <m.szyprowski@samsung.com>; Michal Nazarewicz

>> <mina86@mina86.com>

>> Cc: stable@vger.kernel.org

>> Subject: [PATCH] usb: udc: core: Use lock for soft_connect

>>

>> Use lock to guard against concurrent access for soft-connect/disconnect

>> operations when writing to soft_connect sysfs.

>>

>> Cc: stable@vger.kernel.org

>> Fixes: 2ccea03a8f7e ("usb: gadget: introduce UDC Class")

>> Signed-off-by: Thinh Nguyen <Thinh.Nguyen@synopsys.com>

> Reviewed-by: Peter Chen <peter.chen@kernel.org>

>

> Peter


Thank you for the review.

Thinh
Thinh Nguyen Jan. 14, 2021, 2:41 a.m. UTC | #4
Ahmed S. Darwish wrote:
> On Tue, Jan 12, 2021 at 03:26:21PM -0800, Thinh Nguyen wrote:

> ...

>> +	mutex_lock(&udc_lock);

>>  	if (!udc->driver) {

>> +		mutex_unlock(&udc_lock);

>>  		dev_err(dev, "soft-connect without a gadget driver\n");

>>  		return -EOPNOTSUPP;

>>  	}

>> @@ -1542,10 +1544,12 @@ static ssize_t soft_connect_store(struct device *dev,

>>  		usb_gadget_disconnect(udc->gadget);

>>  		usb_gadget_udc_stop(udc);

>>  	} else {

>> +		mutex_unlock(&udc_lock);

>>  		dev_err(dev, "unsupported command '%s'\n", buf);

>>  		return -EINVAL;

>>  	}

>>

>> +	mutex_unlock(&udc_lock);

>>  	return n;

>>  }

> Please use "goto out" instead of repeating the mutex unlock line three

> times.

>

> Thanks,

>

> --

> Ahmed S. Darwish

> Linutronix GmbH


Sure. We can do that.

Thanks,
Thinh
diff mbox series

Patch

diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c
index 6a62bbd01324..44c67e765167 100644
--- a/drivers/usb/gadget/udc/core.c
+++ b/drivers/usb/gadget/udc/core.c
@@ -1530,7 +1530,9 @@  static ssize_t soft_connect_store(struct device *dev,
 {
 	struct usb_udc		*udc = container_of(dev, struct usb_udc, dev);
 
+	mutex_lock(&udc_lock);
 	if (!udc->driver) {
+		mutex_unlock(&udc_lock);
 		dev_err(dev, "soft-connect without a gadget driver\n");
 		return -EOPNOTSUPP;
 	}
@@ -1542,10 +1544,12 @@  static ssize_t soft_connect_store(struct device *dev,
 		usb_gadget_disconnect(udc->gadget);
 		usb_gadget_udc_stop(udc);
 	} else {
+		mutex_unlock(&udc_lock);
 		dev_err(dev, "unsupported command '%s'\n", buf);
 		return -EINVAL;
 	}
 
+	mutex_unlock(&udc_lock);
 	return n;
 }
 static DEVICE_ATTR_WO(soft_connect);