Skip to contentSkip to conversation

usb: gadget: udc-xilinx: guard EP0 dequeue against empty queue

PATCH In review Li Ximo 3 participants 3 messages Nov 3 – Nov 4

Subsystems (2)
lore
Share & tools
lore

Li Ximo

author
#0 lore raw

PATCH v2 1/1 usb: gadget: udc-xilinx: guard EP0 dequeue against empty queue

From: Wang Haoran <[email protected]>

The EP0 dequeue side, xudc_ep0_out() and xudc_ep0_in(), calls list_first_entry() on ep0->queue without checking whether a request is queued, while the enqueue side __xudc_ep0_queue() already guards the same queue with list_empty() and the non-control endpoint handler performs the same check before its own list_first_entry().

list_first_entry() never returns NULL on an empty list; it returns the container_of() of the list head, i.e. a non-NULL wild pointer into the enclosing struct xusb_ep. Switch both functions to list_first_entry_or_null() and return early when no request is queued, making the dequeue side consistent with the rest of the driver.

This is a defensive consistency fix found by static analysis.

Fixes 1f7c51660034 ("usb: gadget: Add xilinx usb2 device support")

---
v1: https://lore.kernel.org/r/[email protected]
v2: refactor the commit description as a defensive consistency fix per Suhan's review (the originally described bus-timing trigger is not reachable without software first arming a buffer), and add the Fixes tag. Carry Aldo's Reviewed-by. No code change.

 drivers/usb/gadget/udc/udc-xilinx.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/usb/gadget/udc/udc-xilinx.c +7 -2
index bef06fe75..032d9f04f 100644
@@ -1822,7 +1822,9 @@ static void xudc_ep0_out(struct xusb_udc *udc)
 	unsigned int bytes_to_rx;
 	void *buffer;
 
-	req = list_first_entry(&ep0->queue, struct xusb_req, queue);
+	req = list_first_entry_or_null(&ep0->queue, struct xusb_req, queue);
+	if (!req)
+		return;
 
 	switch (udc->setupseqrx) {
 	case STATUS_PHASE:
@@ -1875,7 +1877,10 @@ static void xudc_ep0_in(struct xusb_udc *udc)
 	u8 *ep0rambase;
 	u8 test_mode = le16_to_cpu(udc->setup.wIndex) >> 8;
 
-	req = list_first_entry(&ep0->queue, struct xusb_req, queue);
+	req = list_first_entry_or_null(&ep0->queue, struct xusb_req, queue);
+	if (!req)
+		return;
+
 	bytes_to_tx = req->usb_req.length - req->usb_req.actual;
 
 	switch (udc->setupseqtx) {
Signature · 1 line
2.30.2
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 14 lines

From: Wang Haoran <[email protected]>

The EP0 dequeue side, xudc_ep0_out() and xudc_ep0_in(), calls list_first_entry() on ep0->queue without checking whether a request is queued, while the enqueue side __xudc_ep0_queue() already guards the same queue with list_empty() and the non-control endpoint handler performs the same check before its own list_first_entry().

list_first_entry() never returns NULL on an empty list; it returns the container_of() of the list head, i.e. a non-NULL wild pointer into the enclosing struct xusb_ep. Switch both functions to list_first_entry_or_null() and return early when no request is queued, making the dequeue side consistent with the rest of the driver.

Quoted 7 lines

This is a defensive consistency fix found by static analysis.

Fixes 1f7c51660034 ("usb: gadget: Add xilinx usb2 device support")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by Shaikh Mohammed Suhan on v2 1/1
#2 lore raw
In reply to Li Ximo
Quoted 14 lines · Li Ximo

From: Wang Haoran <[email protected]>

The EP0 dequeue side, xudc_ep0_out() and xudc_ep0_in(), calls list_first_entry() on ep0->queue without checking whether a request is queued, while the enqueue side __xudc_ep0_queue() already guards the same queue with list_empty() and the non-control endpoint handler performs the same check before its own list_first_entry().

list_first_entry() never returns NULL on an empty list; it returns the container_of() of the list head, i.e. a non-NULL wild pointer into the enclosing struct xusb_ep. Switch both functions to list_first_entry_or_null() and return early when no request is queued, making the dequeue side consistent with the rest of the driver.

Quoted 7 lines

This is a defensive consistency fix found by static analysis.

Fixes 1f7c51660034 ("usb: gadget: Add xilinx usb2 device support")
Quoted 36 lines

v1: https://lore.kernel.org/r/[email protected]
v2: refactor the commit description as a defensive consistency fix per Suhan's review (the originally described bus-timing trigger is not reachable without software first arming a buffer), and add the Fixes tag. Carry Aldo's Reviewed-by. No code change.

  drivers/usb/gadget/udc/udc-xilinx.c | 9 +++++++--
  1 file changed, 7 insertions(+), 2 deletions(-)
drivers/usb/gadget/udc/udc-xilinx.c +7 -2
index bef06fe75..032d9f04f 100644
@@ -1822,7 +1822,9 @@ static void xudc_ep0_out(struct xusb_udc *udc)
  	unsigned int bytes_to_rx;
  	void *buffer;
 
-	req = list_first_entry(&ep0->queue, struct xusb_req, queue);
+	req = list_first_entry_or_null(&ep0->queue, struct xusb_req, queue);
+	if (!req)
+		return;
 
  	switch (udc->setupseqrx) {
  	case STATUS_PHASE:
@@ -1875,7 +1877,10 @@ static void xudc_ep0_in(struct xusb_udc *udc)
  	u8 *ep0rambase;
  	u8 test_mode = le16_to_cpu(udc->setup.wIndex) >> 8;
 
-	req = list_first_entry(&ep0->queue, struct xusb_req, queue);
+	req = list_first_entry_or_null(&ep0->queue, struct xusb_req, queue);
+	if (!req)
+		return;
+
  	bytes_to_tx = req->usb_req.length - req->usb_req.actual;
 
  	switch (udc->setupseqtx) {