Skip to contentSkip to conversation

usb: chipidea: udc: _ep_queue and _hw_queue cleanup

PATCH In review [email protected] 2 participants 7 messages Sep 18, 2015 – Oct 27, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/3 usb: chipidea: udc: _ep_queue and _hw_queue cleanup

From: "Felipe F. Tonello" <[email protected]>

Update comments to reflect current state of functions.

---

Changes for v2:
- Introduced this patch.

 drivers/usb/chipidea/udc.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/usb/chipidea/udc.c +6 -2
index 764f668..c936c72 100644
@@ -404,9 +404,9 @@ static inline u8 _usb_addr(struct ci_hw_ep *ep)
 }
 
 /**
- * _hardware_queue: configures a request at hardware level
- * @gadget: gadget
+ * _hardware_enqueue: configures a request at hardware level
  * @hwep:   endpoint
+ * @hwreq:  request
  *
  * This function returns an error code
  */
@@ -750,8 +750,12 @@ static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
 
 /**
  * _ep_queue: queues (submits) an I/O request to an endpoint
+ * @ep:        endpoint
+ * @req:       request
+ * @gfp_flags: GFP flags (not used)
  *
  * Caller must hold lock
+ * This function returns an error code
  */
 static int _ep_queue(struct usb_ep *ep, struct usb_request *req,
 		    gfp_t __maybe_unused gfp_flags)
Signature · 1 line
2.1.4
#1 lore raw

PATCH 2/3 usb: chipidea: udc: improve error handling on ep_queue

From: "Felipe F. Tonello" <[email protected]>

_ep_queue() didn't check for errors when using add_td_to_list() which can fail if dma_pool_alloc fails, thus causing a kernel panic when lastnode->ptr is NULL.

---

Changes for v2:
- Use separate patch for cleanups.

 drivers/usb/chipidea/udc.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
drivers/usb/chipidea/udc.c +13 -5
index c936c72..7169113e 100644
@@ -435,19 +435,27 @@ static int _hardware_enqueue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
 	if (hwreq->req.dma % PAGE_SIZE)
 		pages--;
 
-	if (rest == 0)
-		add_td_to_list(hwep, hwreq, 0);
+	if (rest == 0) {
+		ret = add_td_to_list(hwep, hwreq, 0);
+		if (ret < 0)
+			goto done;
+	}
 
 	while (rest > 0) {
 		unsigned count = min(hwreq->req.length - hwreq->req.actual,
 					(unsigned)(pages * CI_HDRC_PAGE_SIZE));
-		add_td_to_list(hwep, hwreq, count);
+		ret = add_td_to_list(hwep, hwreq, count);
+		if (ret < 0)
+			goto done;
 		rest -= count;
 	}
 
 	if (hwreq->req.zero && hwreq->req.length
-	    && (hwreq->req.length % hwep->ep.maxpacket == 0))
-		add_td_to_list(hwep, hwreq, 0);
+	    && (hwreq->req.length % hwep->ep.maxpacket == 0)) {
+		ret = add_td_to_list(hwep, hwreq, 0);
+		if (ret < 0)
+			goto done;
+	}
 
 	firstnode = list_first_entry(&hwreq->tds, struct td_node, td);
 
Signature · 1 line
2.1.4
#2 lore raw

Re: [PATCH 2/3] usb: chipidea: udc: improve error handling on ep_queue

Quoted 55 lines · On Fri, Sep 18, 2015 at 06:30:20PM +0100, [email protected] wrote:

From: "Felipe F. Tonello" <[email protected]>

_ep_queue() didn't check for errors when using add_td_to_list() which can fail if dma_pool_alloc fails, thus causing a kernel panic when lastnode->ptr is NULL.

---

Changes for v2:
- Use separate patch for cleanups.

 drivers/usb/chipidea/udc.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
drivers/usb/chipidea/udc.c +13 -5
index c936c72..7169113e 100644
@@ -435,19 +435,27 @@ static int _hardware_enqueue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
 	if (hwreq->req.dma % PAGE_SIZE)
 		pages--;
 
-	if (rest == 0)
-		add_td_to_list(hwep, hwreq, 0);
+	if (rest == 0) {
+		ret = add_td_to_list(hwep, hwreq, 0);
+		if (ret < 0)
+			goto done;
+	}
 
 	while (rest > 0) {
 		unsigned count = min(hwreq->req.length - hwreq->req.actual,
 					(unsigned)(pages * CI_HDRC_PAGE_SIZE));
-		add_td_to_list(hwep, hwreq, count);
+		ret = add_td_to_list(hwep, hwreq, count);
+		if (ret < 0)
+			goto done;
 		rest -= count;
 	}
 
 	if (hwreq->req.zero && hwreq->req.length
-	    && (hwreq->req.length % hwep->ep.maxpacket == 0))
-		add_td_to_list(hwep, hwreq, 0);
+	    && (hwreq->req.length % hwep->ep.maxpacket == 0)) {
+		ret = add_td_to_list(hwep, hwreq, 0);
+		if (ret < 0)
+			goto done;
+	}
 
 	firstnode = list_first_entry(&hwreq->tds, struct td_node, td);
 
Signature · 1 line
2.1.4

Hi Felipe,

I can't apply it in my tree, would you please create it based on chipdea tree?

https://git.kernel.org/pub/scm/linux/kernel/git/peter.chen/usb.git
branch: ci-for-usb-next

Signature · 2 lines
Best Regards,
Peter Chen
#3 lore raw

Re: [PATCH 2/3] usb: chipidea: udc: improve error handling on ep_queue

In reply to [email protected]

Hi Peter,

Have you seen this patch? I saw that you didn't apply it to your tree, so I wonder if it is good or do I have to change anything.

This patch is a bug fix for a memory leak, so it is quite important.

Signature · 1 line
Felipe
Quoted 53 lines · On 18/09/15 18:30, [email protected] wrote:

From: "Felipe F. Tonello" <[email protected]>

_ep_queue() didn't check for errors when using add_td_to_list() which can fail if dma_pool_alloc fails, thus causing a kernel panic when lastnode->ptr is NULL.

---

Changes for v2:
- Use separate patch for cleanups.

 drivers/usb/chipidea/udc.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
drivers/usb/chipidea/udc.c +13 -5
index c936c72..7169113e 100644
@@ -435,19 +435,27 @@ static int _hardware_enqueue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
 	if (hwreq->req.dma % PAGE_SIZE)
 		pages--;
 
-	if (rest == 0)
-		add_td_to_list(hwep, hwreq, 0);
+	if (rest == 0) {
+		ret = add_td_to_list(hwep, hwreq, 0);
+		if (ret < 0)
+			goto done;
+	}
 
 	while (rest > 0) {
 		unsigned count = min(hwreq->req.length - hwreq->req.actual,
 					(unsigned)(pages * CI_HDRC_PAGE_SIZE));
-		add_td_to_list(hwep, hwreq, count);
+		ret = add_td_to_list(hwep, hwreq, count);
+		if (ret < 0)
+			goto done;
 		rest -= count;
 	}
 
 	if (hwreq->req.zero && hwreq->req.length
-	    && (hwreq->req.length % hwep->ep.maxpacket == 0))
-		add_td_to_list(hwep, hwreq, 0);
+	    && (hwreq->req.length % hwep->ep.maxpacket == 0)) {
+		ret = add_td_to_list(hwep, hwreq, 0);
+		if (ret < 0)
+			goto done;
+	}
 
 	firstnode = list_first_entry(&hwreq->tds, struct td_node, td);
 
#4 lore raw

RE: [PATCH 2/3] usb: chipidea: udc: improve error handling on ep_queue

Quoted 8 lines

Hi Peter,

Have you seen this patch? I saw that you didn't apply it to your tree, so I wonder if it is good or do I have to change anything.

This patch is a bug fix for a memory leak, so it is quite important.

Would you please create it based on my tree, branch ci-for-usb-next? I can't apply it.

Peter

Quoted 59 lines

--
Felipe

drivers/usb/chipidea/udc.c static int _hardware_enqueue(struct ci_hw_ep

Quoted 20 lines · On 18/09/15 18:30, [email protected] wrote:

From: "Felipe F. Tonello" <[email protected]>

_ep_queue() didn't check for errors when using add_td_to_list() which can fail if dma_pool_alloc fails, thus causing a kernel panic when lastnode->ptr is NULL.

---

Changes for v2:
- Use separate patch for cleanups.

 drivers/usb/chipidea/udc.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
drivers/usb/chipidea/udc.c +0 -0
index c936c72..7169113e 100644
@@ -435,19 +435,27 @@ static int _hardware_enqueue(struct ci_hw_ep

*hwep, struct ci_hw_req *hwreq)

Quoted 4 lines
drivers/usb/chipidea/udc.c +0 -1
 	if (hwreq->req.dma % PAGE_SIZE)
 		pages--;
 
-	if (rest == 0)

drivers/usb/chipidea/udc.c static int _hardware_enqueue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)

Quoted 10 lines
drivers/usb/chipidea/udc.c +5 -1
-		add_td_to_list(hwep, hwreq, 0);
+	if (rest == 0) {
+		ret = add_td_to_list(hwep, hwreq, 0);
+		if (ret < 0)
+			goto done;
+	}
 
 	while (rest > 0) {
 		unsigned count = min(hwreq->req.length - hwreq->req.actual,
 					(unsigned)(pages *

CI_HDRC_PAGE_SIZE));

Quoted 19 lines
drivers/usb/chipidea/udc.c +8 -3
-		add_td_to_list(hwep, hwreq, count);
+		ret = add_td_to_list(hwep, hwreq, count);
+		if (ret < 0)
+			goto done;
 		rest -= count;
 	}
 
 	if (hwreq->req.zero && hwreq->req.length
-	    && (hwreq->req.length % hwep->ep.maxpacket == 0))
-		add_td_to_list(hwep, hwreq, 0);
+	    && (hwreq->req.length % hwep->ep.maxpacket == 0)) {
+		ret = add_td_to_list(hwep, hwreq, 0);
+		if (ret < 0)
+			goto done;
+	}
 
 	firstnode = list_first_entry(&hwreq->tds, struct td_node, td);
 
#5 lore raw

PATCH 3/3 usb: gadget: f_midi: check for error on usb_ep_queue

In reply to [email protected]

From: "Felipe F. Tonello" <[email protected]>

f_midi is not checking weather the is an error on usb_ep_queue request, ignoring potential problems, such as memory leaks.

---

Changes for v2:
- Update code style.

 drivers/usb/gadget/function/f_midi.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/usb/gadget/function/f_midi.c +9 -3
index ad50a67..c04133422 100644
@@ -543,10 +543,16 @@ static void f_midi_transmit(struct f_midi *midi, struct usb_request *req)
 		}
 	}
 
-	if (req->length > 0)
-		usb_ep_queue(ep, req, GFP_ATOMIC);
-	else
+	if (req->length > 0) {
+		int err;
+
+		err = usb_ep_queue(ep, req, GFP_ATOMIC);
+		if (err < 0)
+			ERROR(midi, "%s queue req: %d\n",
+			      midi->out_ep->name, err);
+	} else {
 		free_ep_req(ep, req);
+	}
 }
 
 static void f_midi_in_tasklet(unsigned long data)
Signature · 1 line
2.1.4
#6 lore raw

Re: [PATCH 3/3] usb: gadget: f_midi: check for error on usb_ep_queue

Quoted 22 lines · On Fri, Sep 18, 2015 at 6:30 PM, <[email protected]> wrote:

From: "Felipe F. Tonello" <[email protected]>

f_midi is not checking weather the is an error on usb_ep_queue request, ignoring potential problems, such as memory leaks.

---

Changes for v2:
- Update code style.

 drivers/usb/gadget/function/f_midi.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/usb/gadget/function/f_midi.c +0 -0
index ad50a67..c04133422 100644
@@ -543,10 +543,16 @@ static void f_midi_transmit(struct f_midi *midi, struct usb_request *req)
                }
        }
 

drivers/usb/gadget/function/f_midi.c static void f_midi_transmit(struct f_midi *midi, struct usb_request *req) · line 552

Quoted 10 lines
drivers/usb/gadget/function/f_midi.c +7 -3
-       if (req->length > 0)
-               usb_ep_queue(ep, req, GFP_ATOMIC);
-       else
+       if (req->length > 0) {
+               int err;
+
+               err = usb_ep_queue(ep, req, GFP_ATOMIC);
+               if (err < 0)
+                       ERROR(midi, "%s queue req: %d\n",
+                             midi->out_ep->name, err);

I just realised there is a problem here. It is in_ep in this case. I will fix it in v3.

Quoted 9 lines
drivers/usb/gadget/function/f_midi.c +2 -1
+       } else {
                free_ep_req(ep, req);
+       }
 }
 
 static void f_midi_in_tasklet(unsigned long data)
--

2.1.4