Skip to contentSkip to conversation

usb: cdc-wdm: Fix a sleep-in-atomic-context bug in service_outstanding_interrupt()

PATCH In review Jia-Ju Bai 3 participants 4 messages Sep 1, 2018 – Sep 11, 2018 USB

lore
Share & tools
lore

Jia-Ju Bai

author
#0 lore raw

PATCH v2 1/1 usb: cdc-wdm: Fix a sleep-in-atomic-context bug in service_outstanding_interrupt()

wdm_in_callback() is a completion handler function for the USB driver. So it should not sleep. But it calls service_outstanding_interrupt(), which calls usb_submit_urb() with GFP_KERNEL.

To fix this bug, GFP_KERNEL is replaced with GFP_ATOMIC.

This bug is found by my static analysis tool DSAC.

---
v2:
* Add more description.

---
 drivers/usb/class/cdc-wdm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/class/cdc-wdm.c +1 -1
index a0d284ef3f40..632a2bfabc08 100644
@@ -458,7 +458,7 @@ static int service_outstanding_interrupt(struct wdm_device *desc)
 
 	set_bit(WDM_RESPONDING, &desc->flags);
 	spin_unlock_irq(&desc->iuspin);
-	rv = usb_submit_urb(desc->response, GFP_KERNEL);
+	rv = usb_submit_urb(desc->response, GFP_ATOMIC);
 	spin_lock_irq(&desc->iuspin);
 	if (rv) {
 		dev_err(&desc->intf->dev,
#1 lore raw
Quoted 3 lines · Jia-Ju Bai

wdm_in_callback() is a completion handler function for the USB driver. So it should not sleep. But it calls service_outstanding_interrupt(), which calls usb_submit_urb() with GFP_KERNEL.

At which point does wdm_in_callback() invoke service_outstanding_interrupt()? I don't see it. I see one invocation from wdm_read() and another from service_interrupt_work().

Also, if that would be the case, then spin_unlock_irq() in an USB completion handler (which might run in IRQ context with interrupts disabled) would be wrong.

Quoted 17 lines

To fix this bug, GFP_KERNEL is replaced with GFP_ATOMIC.

This bug is found by my static analysis tool DSAC.

---
v2:
* Add more description.

---
 drivers/usb/class/cdc-wdm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/class/cdc-wdm.c +0 -0
index a0d284ef3f40..632a2bfabc08 100644

drivers/usb/class/cdc-wdm.c static int service_outstanding_interrupt(struct wdm_device *desc) · line 464

Quoted 8 lines
drivers/usb/class/cdc-wdm.c +1 -1
@@ -458,7 +458,7 @@ static int service_outstanding_interrupt(struct wdm_device *desc)
 
 	set_bit(WDM_RESPONDING, &desc->flags);
 	spin_unlock_irq(&desc->iuspin);
-	rv = usb_submit_urb(desc->response, GFP_KERNEL);
+	rv = usb_submit_urb(desc->response, GFP_ATOMIC);
 	spin_lock_irq(&desc->iuspin);
 	if (rv) {
 		dev_err(&desc->intf->dev,

Sebastian

#2 lore raw

PATCH 1/1 Revert "usb: cdc-wdm: Fix a sleep-in-atomic-context bug in service_outstanding_interrupt()"

This reverts commit 6e22e3af7bb3a7b9dc53cb4687659f6e63fca427.

The bug the patch describes to, has been already fixed in commit 2df6948428542 ("USB: cdc-wdm: don't enable interrupts in USB-giveback") so need to this, revert it.

---
 drivers/usb/class/cdc-wdm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/class/cdc-wdm.c +1 -1
index 656d247819c9d..bec581fb7c636 100644
@@ -460,7 +460,7 @@ static int service_outstanding_interrupt(struct wdm_device *desc)
 
 	set_bit(WDM_RESPONDING, &desc->flags);
 	spin_unlock_irq(&desc->iuspin);
-	rv = usb_submit_urb(desc->response, GFP_ATOMIC);
+	rv = usb_submit_urb(desc->response, GFP_KERNEL);
 	spin_lock_irq(&desc->iuspin);
 	if (rv) {
 		dev_err(&desc->intf->dev,
#3 lore raw
In reply to Sebastian Andrzej Siewior
Quoted 11 lines · Sebastian Andrzej Siewior
Quoted 3 lines · Jia-Ju Bai

wdm_in_callback() is a completion handler function for the USB driver. So it should not sleep. But it calls service_outstanding_interrupt(), which calls usb_submit_urb() with GFP_KERNEL.

At which point does wdm_in_callback() invoke service_outstanding_interrupt()? I don't see it. I see one invocation from wdm_read() and another from service_interrupt_work().

Also, if that would be the case, then spin_unlock_irq() in an USB completion handler (which might run in IRQ context with interrupts disabled) would be wrong.

Yes, you are right.
I checked an old kernel version Linux-4.16 and got this report. The current code looks much different from the code that I checked. Sorry for my false report, and thanks for your correction.

Best wishes,
Jia-Ju Bai