All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] usb: gadget: pch_udc: add checks for dma mapping errors
@ 2017-08-23 22:47 Alexey Khoroshilov
  2017-08-24  1:02 ` Jack Pham
  0 siblings, 1 reply; 4+ messages in thread
From: Alexey Khoroshilov @ 2017-08-23 22:47 UTC (permalink / raw)
  To: Felipe Balbi, Greg Kroah-Hartman
  Cc: Alexey Khoroshilov, linux-usb, linux-kernel, ldv-project

There are no checks for dma mapping errors in pch_udc.
Tha patch adds the checks and error handling code.
Compile tested only.

Found by Linux Driver Verification project (linuxtesting.org).

Signed-off-by: Alexey Khoroshilov <khoroshilov@ispras.ru>
---
 drivers/usb/gadget/udc/pch_udc.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/gadget/udc/pch_udc.c b/drivers/usb/gadget/udc/pch_udc.c
index 84dcbcd756f0..a305f8392082 100644
--- a/drivers/usb/gadget/udc/pch_udc.c
+++ b/drivers/usb/gadget/udc/pch_udc.c
@@ -1767,7 +1767,7 @@ static struct usb_request *pch_udc_alloc_request(struct usb_ep *usbep,
 	req->req.dma = DMA_ADDR_INVALID;
 	req->dma = DMA_ADDR_INVALID;
 	INIT_LIST_HEAD(&req->queue);
-	if (!ep->dev->dma_addr)
+	if (ep->dev->dma_addr != DMA_ADDR_INVALID)
 		return &req->req;
 	/* ep0 in requests are allocated from data pool here */
 	dma_desc = dma_pool_alloc(ep->dev->data_requests, gfp,
@@ -1879,6 +1879,13 @@ static int pch_udc_pcd_queue(struct usb_ep *usbep, struct usb_request *usbreq,
 							  usbreq->length,
 							  DMA_FROM_DEVICE);
 		}
+		if (dma_mapping_error(&dev->pdev->dev, req->dma)) {
+			req->dma = DMA_ADDR_INVALID;
+			retval = -ENOMEM;
+			if ((unsigned long)(usbreq->buf) & 0x03)
+				kfree(req->buf);
+			goto probe_end;
+		}
 		req->dma_mapped = 1;
 	}
 	if (usbreq->length > 0) {
@@ -2961,6 +2968,10 @@ static int init_dma_pools(struct pch_udc_dev *dev)
 	dev->dma_addr = dma_map_single(&dev->pdev->dev, ep0out_buf,
 				       UDC_EP0OUT_BUFF_SIZE * 4,
 				       DMA_FROM_DEVICE);
+	if (dma_mapping_error(&dev->pdev->dev, dev->dma_addr)) {
+		dev->dma_addr = DMA_ADDR_INVALID;
+		return -ENOMEM;
+	}
 	return 0;
 }
 
@@ -3038,7 +3049,7 @@ static void pch_udc_remove(struct pci_dev *pdev)
 		dma_pool_destroy(dev->stp_requests);
 	}
 
-	if (dev->dma_addr)
+	if (dev->dma_addr != DMA_ADDR_INVALID)
 		dma_unmap_single(&dev->pdev->dev, dev->dma_addr,
 				 UDC_EP0OUT_BUFF_SIZE * 4, DMA_FROM_DEVICE);
 
-- 
2.7.4

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

* Re: [PATCH] usb: gadget: pch_udc: add checks for dma mapping errors
  2017-08-23 22:47 [PATCH] usb: gadget: pch_udc: add checks for dma mapping errors Alexey Khoroshilov
@ 2017-08-24  1:02 ` Jack Pham
  2017-09-02 18:35   ` Alexey Khoroshilov
  0 siblings, 1 reply; 4+ messages in thread
From: Jack Pham @ 2017-08-24  1:02 UTC (permalink / raw)
  To: Alexey Khoroshilov
  Cc: Felipe Balbi, Greg Kroah-Hartman, linux-usb, linux-kernel, ldv-project

On Thu, Aug 24, 2017 at 01:47:08AM +0300, Alexey Khoroshilov wrote:
> There are no checks for dma mapping errors in pch_udc.
> Tha patch adds the checks and error handling code.
> Compile tested only.
> 
> Found by Linux Driver Verification project (linuxtesting.org).
> 
> Signed-off-by: Alexey Khoroshilov <khoroshilov@ispras.ru>
> ---
>  drivers/usb/gadget/udc/pch_udc.c | 15 +++++++++++++--
>  1 file changed, 13 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/usb/gadget/udc/pch_udc.c b/drivers/usb/gadget/udc/pch_udc.c
> index 84dcbcd756f0..a305f8392082 100644
> --- a/drivers/usb/gadget/udc/pch_udc.c
> +++ b/drivers/usb/gadget/udc/pch_udc.c
> @@ -1767,7 +1767,7 @@ static struct usb_request *pch_udc_alloc_request(struct usb_ep *usbep,
>  	req->req.dma = DMA_ADDR_INVALID;
>  	req->dma = DMA_ADDR_INVALID;
>  	INIT_LIST_HEAD(&req->queue);
> -	if (!ep->dev->dma_addr)
> +	if (ep->dev->dma_addr != DMA_ADDR_INVALID)
>  		return &req->req;
>  	/* ep0 in requests are allocated from data pool here */
>  	dma_desc = dma_pool_alloc(ep->dev->data_requests, gfp,
> @@ -1879,6 +1879,13 @@ static int pch_udc_pcd_queue(struct usb_ep *usbep, struct usb_request *usbreq,
>  							  usbreq->length,
>  							  DMA_FROM_DEVICE);
>  		}
> +		if (dma_mapping_error(&dev->pdev->dev, req->dma)) {
> +			req->dma = DMA_ADDR_INVALID;
> +			retval = -ENOMEM;
> +			if ((unsigned long)(usbreq->buf) & 0x03)
> +				kfree(req->buf);
> +			goto probe_end;
> +		}
>  		req->dma_mapped = 1;
>  	}
>  	if (usbreq->length > 0) {
> @@ -2961,6 +2968,10 @@ static int init_dma_pools(struct pch_udc_dev *dev)
>  	dev->dma_addr = dma_map_single(&dev->pdev->dev, ep0out_buf,
>  				       UDC_EP0OUT_BUFF_SIZE * 4,
>  				       DMA_FROM_DEVICE);
> +	if (dma_mapping_error(&dev->pdev->dev, dev->dma_addr)) {
> +		dev->dma_addr = DMA_ADDR_INVALID;
> +		return -ENOMEM;
> +	}

Wouldn't this driver be better off using the
usb_gadget_{map,unmap}_request() functions provided by UDC core.c?
dma_mapping_error() is provided for free that way.

Jack
-- 
The Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project

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

* Re: [PATCH] usb: gadget: pch_udc: add checks for dma mapping errors
  2017-08-24  1:02 ` Jack Pham
@ 2017-09-02 18:35   ` Alexey Khoroshilov
  2017-09-20 11:53     ` Felipe Balbi
  0 siblings, 1 reply; 4+ messages in thread
From: Alexey Khoroshilov @ 2017-09-02 18:35 UTC (permalink / raw)
  To: Jack Pham
  Cc: Felipe Balbi, Greg Kroah-Hartman, linux-usb, linux-kernel, ldv-project

On 24.08.2017 04:02, Jack Pham wrote:
> On Thu, Aug 24, 2017 at 01:47:08AM +0300, Alexey Khoroshilov wrote:
>> There are no checks for dma mapping errors in pch_udc.
>> Tha patch adds the checks and error handling code.
>> Compile tested only.
>>
>> Found by Linux Driver Verification project (linuxtesting.org).
>>
>> Signed-off-by: Alexey Khoroshilov <khoroshilov@ispras.ru>
>> ---
>>  drivers/usb/gadget/udc/pch_udc.c | 15 +++++++++++++--
>>  1 file changed, 13 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/usb/gadget/udc/pch_udc.c b/drivers/usb/gadget/udc/pch_udc.c
>> index 84dcbcd756f0..a305f8392082 100644
>> --- a/drivers/usb/gadget/udc/pch_udc.c
>> +++ b/drivers/usb/gadget/udc/pch_udc.c
>> @@ -1767,7 +1767,7 @@ static struct usb_request *pch_udc_alloc_request(struct usb_ep *usbep,
>>  	req->req.dma = DMA_ADDR_INVALID;
>>  	req->dma = DMA_ADDR_INVALID;
>>  	INIT_LIST_HEAD(&req->queue);
>> -	if (!ep->dev->dma_addr)
>> +	if (ep->dev->dma_addr != DMA_ADDR_INVALID)
>>  		return &req->req;
>>  	/* ep0 in requests are allocated from data pool here */
>>  	dma_desc = dma_pool_alloc(ep->dev->data_requests, gfp,
>> @@ -1879,6 +1879,13 @@ static int pch_udc_pcd_queue(struct usb_ep *usbep, struct usb_request *usbreq,
>>  							  usbreq->length,
>>  							  DMA_FROM_DEVICE);
>>  		}
>> +		if (dma_mapping_error(&dev->pdev->dev, req->dma)) {
>> +			req->dma = DMA_ADDR_INVALID;
>> +			retval = -ENOMEM;
>> +			if ((unsigned long)(usbreq->buf) & 0x03)
>> +				kfree(req->buf);
>> +			goto probe_end;
>> +		}
>>  		req->dma_mapped = 1;
>>  	}
>>  	if (usbreq->length > 0) {
>> @@ -2961,6 +2968,10 @@ static int init_dma_pools(struct pch_udc_dev *dev)
>>  	dev->dma_addr = dma_map_single(&dev->pdev->dev, ep0out_buf,
>>  				       UDC_EP0OUT_BUFF_SIZE * 4,
>>  				       DMA_FROM_DEVICE);
>> +	if (dma_mapping_error(&dev->pdev->dev, dev->dma_addr)) {
>> +		dev->dma_addr = DMA_ADDR_INVALID;
>> +		return -ENOMEM;
>> +	}
> 
> Wouldn't this driver be better off using the
> usb_gadget_{map,unmap}_request() functions provided by UDC core.c?
> dma_mapping_error() is provided for free that way.
> 

I think so, but it requires quite significant rework of the driver.
I would not do that without access to hardware.

--
Alexey

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

* Re: [PATCH] usb: gadget: pch_udc: add checks for dma mapping errors
  2017-09-02 18:35   ` Alexey Khoroshilov
@ 2017-09-20 11:53     ` Felipe Balbi
  0 siblings, 0 replies; 4+ messages in thread
From: Felipe Balbi @ 2017-09-20 11:53 UTC (permalink / raw)
  To: Alexey Khoroshilov, Jack Pham
  Cc: Greg Kroah-Hartman, linux-usb, linux-kernel, ldv-project

[-- Attachment #1: Type: text/plain, Size: 1956 bytes --]


Hi,

Alexey Khoroshilov <khoroshilov@ispras.ru> writes:
>>> diff --git a/drivers/usb/gadget/udc/pch_udc.c b/drivers/usb/gadget/udc/pch_udc.c
>>> index 84dcbcd756f0..a305f8392082 100644
>>> --- a/drivers/usb/gadget/udc/pch_udc.c
>>> +++ b/drivers/usb/gadget/udc/pch_udc.c
>>> @@ -1767,7 +1767,7 @@ static struct usb_request *pch_udc_alloc_request(struct usb_ep *usbep,
>>>  	req->req.dma = DMA_ADDR_INVALID;
>>>  	req->dma = DMA_ADDR_INVALID;
>>>  	INIT_LIST_HEAD(&req->queue);
>>> -	if (!ep->dev->dma_addr)
>>> +	if (ep->dev->dma_addr != DMA_ADDR_INVALID)
>>>  		return &req->req;
>>>  	/* ep0 in requests are allocated from data pool here */
>>>  	dma_desc = dma_pool_alloc(ep->dev->data_requests, gfp,
>>> @@ -1879,6 +1879,13 @@ static int pch_udc_pcd_queue(struct usb_ep *usbep, struct usb_request *usbreq,
>>>  							  usbreq->length,
>>>  							  DMA_FROM_DEVICE);
>>>  		}
>>> +		if (dma_mapping_error(&dev->pdev->dev, req->dma)) {
>>> +			req->dma = DMA_ADDR_INVALID;
>>> +			retval = -ENOMEM;
>>> +			if ((unsigned long)(usbreq->buf) & 0x03)
>>> +				kfree(req->buf);
>>> +			goto probe_end;
>>> +		}
>>>  		req->dma_mapped = 1;
>>>  	}
>>>  	if (usbreq->length > 0) {
>>> @@ -2961,6 +2968,10 @@ static int init_dma_pools(struct pch_udc_dev *dev)
>>>  	dev->dma_addr = dma_map_single(&dev->pdev->dev, ep0out_buf,
>>>  				       UDC_EP0OUT_BUFF_SIZE * 4,
>>>  				       DMA_FROM_DEVICE);
>>> +	if (dma_mapping_error(&dev->pdev->dev, dev->dma_addr)) {
>>> +		dev->dma_addr = DMA_ADDR_INVALID;
>>> +		return -ENOMEM;
>>> +	}
>> 
>> Wouldn't this driver be better off using the
>> usb_gadget_{map,unmap}_request() functions provided by UDC core.c?
>> dma_mapping_error() is provided for free that way.
>> 
>
> I think so, but it requires quite significant rework of the driver.
> I would not do that without access to hardware.

I'd prefer to see a conversion to the generic helpers.

-- 
balbi

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 832 bytes --]

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

end of thread, other threads:[~2017-09-20 11:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-23 22:47 [PATCH] usb: gadget: pch_udc: add checks for dma mapping errors Alexey Khoroshilov
2017-08-24  1:02 ` Jack Pham
2017-09-02 18:35   ` Alexey Khoroshilov
2017-09-20 11:53     ` Felipe Balbi

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.