Skip to contentSkip to conversation

USB: wdm: fix WWAN integration issue

PATCH New Oliver Neukum 1 participant 5 messages Apr 1, 2025

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

v2 cover USB: wdm: fix WWAN integration issue

The original integration of WWAN left a few race conditions and deficiencies in error handling

V2: Added memory barriers Alan Stern pointed out the need for

#1 lore raw

PATCH v2 1/4 USB: wdm: handle IO errors in wdm_wwan_port_start

In case submitting the URB fails we must undo
what we've done so far.

Fixes cac6fb015f71 ("usb: class: cdc-wdm: WWAN framework integration")
---
 drivers/usb/class/cdc-wdm.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/usb/class/cdc-wdm.c +10 -1
index 856488a7cb6b..12038aa43942 100644
@@ -831,6 +831,7 @@ static struct usb_class_driver wdm_class = {
 static int wdm_wwan_port_start(struct wwan_port *port)
 {
 	struct wdm_device *desc = wwan_port_get_drvdata(port);
+	int rv;
 
 	/* The interface is both exposed via the WWAN framework and as a
 	 * legacy usbmisc chardev. If chardev is already open, just fail
@@ -850,7 +851,15 @@ static int wdm_wwan_port_start(struct wwan_port *port)
 	wwan_port_txon(port);
 
 	/* Start getting events */
-	return usb_submit_urb(desc->validity, GFP_KERNEL);
+	rv = usb_submit_urb(desc->validity, GFP_KERNEL);
+	if (rv < 0) {
+		wwan_port_txoff(port);
+		desc->manage_power(desc->intf, 0);
+		/* this must be last lest we race with chardev open */
+		clear_bit(WDM_WWAN_IN_USE, &desc->flags);
+	}
+
+	return rv;
 }
 
 static void wdm_wwan_port_stop(struct wwan_port *port)
Signature · 1 line
2.49.0
#2 lore raw

PATCH v2 2/4 USB: wdm: close race between wdm_open and wdm_wwan_port_stop

In reply to Oliver Neukum

Clearing WDM_WWAN_IN_USE must be the last action or we can open a chardev whose URBs are still poisoned

V2: added memory barriers

Fixes cac6fb015f71 ("usb: class: cdc-wdm: WWAN framework integration")
---
 drivers/usb/class/cdc-wdm.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/usb/class/cdc-wdm.c +4 -2
index 12038aa43942..355627982343 100644
@@ -728,7 +728,7 @@ static int wdm_open(struct inode *inode, struct file *file)
 		rv = -EBUSY;
 		goto out;
 	}
-
+	smp_rmb(); /* ordered against wdm_wwan_port_stop() */
 	rv = usb_autopm_get_interface(desc->intf);
 	if (rv < 0) {
 		dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
@@ -870,8 +870,10 @@ static void wdm_wwan_port_stop(struct wwan_port *port)
 	poison_urbs(desc);
 	desc->manage_power(desc->intf, 0);
 	clear_bit(WDM_READ, &desc->flags);
-	clear_bit(WDM_WWAN_IN_USE, &desc->flags);
 	unpoison_urbs(desc);
+	smp_wmb(); /* ordered against wdm_open() */
+	/* this must be last lest we open a poisoned device */
+	clear_bit(WDM_WWAN_IN_USE, &desc->flags);
 }
 
 static void wdm_wwan_port_tx_complete(struct urb *urb)
Signature · 1 line
2.49.0
#3 lore raw

PATCH v2 3/4 USB: wdm: wdm_wwan_port_tx_complete mutex in atomic context

In reply to Oliver Neukum

wdm_wwan_port_tx_complete is called from a completion handler with irqs disabled and possible in IRQ context usb_autopm_put_interface can take a mutex. Hence usb_autopm_put_interface_async must be used.

Fixes cac6fb015f71 ("usb: class: cdc-wdm: WWAN framework integration")
---
 drivers/usb/class/cdc-wdm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/class/cdc-wdm.c +1 -1
index 355627982343..8b99db506b71 100644
@@ -881,7 +881,7 @@ static void wdm_wwan_port_tx_complete(struct urb *urb)
 	struct sk_buff *skb = urb->context;
 	struct wdm_device *desc = skb_shinfo(skb)->destructor_arg;
 
-	usb_autopm_put_interface(desc->intf);
+	usb_autopm_put_interface_async(desc->intf);
 	wwan_port_txon(desc->wwanp);
 	kfree_skb(skb);
 }
Signature · 1 line
2.49.0
#4 lore raw

PATCH v2 4/4 USB: wdm: add annotation

In reply to Oliver Neukum

This is not understandable without a comment on endianness

Fixes afba937e540c9 ("USB: CDC WDM driver")
---
 drivers/usb/class/cdc-wdm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/class/cdc-wdm.c +1 -1
index 8b99db506b71..840cf45b791e 100644
@@ -911,7 +911,7 @@ static int wdm_wwan_port_tx(struct wwan_port *port, struct sk_buff *skb)
 	req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
 	req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
 	req->wValue = 0;
-	req->wIndex = desc->inum;
+	req->wIndex = desc->inum; /* already converted */
 	req->wLength = cpu_to_le16(skb->len);
 
 	skb_shinfo(skb)->destructor_arg = desc;
Signature · 1 line
2.49.0