USB: wdm: fix WWAN integration issue
Subsystems (3)
cover USB: wdm: fix WWAN integration issue
The original integration of WWAN left a few race conditions and deficiencies in error handling
PATCH 2/4 USB: wdm: close race between wdm_open and wdm_wwan_port_stop
Clearing WDM_WWAN_IN_USE must be the last action or we can open a chardev whose URBs are still poisoned
--- drivers/usb/class/cdc-wdm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/usb/class/cdc-wdm.c +2 -1
@@ -870,8 +870,9 @@ 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); +/* 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
PATCH 1/4 USB: wdm: handle IO errors in wdm_wwan_port_start
In reply to Oliver NeukumIn case submitting the URB fails we must undo
what we've done so far.
--- drivers/usb/class/cdc-wdm.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/usb/class/cdc-wdm.c +10 -1
@@ -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
PATCH 3/4 USB: wdm: wdm_wwan_port_tx_complete mutex in atomic context
In reply to Oliver Neukumwdm_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.
--- drivers/usb/class/cdc-wdm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/class/cdc-wdm.c +1 -1
@@ -880,7 +880,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
PATCH 4/4 USB: wdm: add annotation
In reply to Oliver NeukumThis is not understandable without a comment on endianness
--- drivers/usb/class/cdc-wdm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/class/cdc-wdm.c +1 -1
@@ -910,7 +910,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
Re: [PATCH 2/4] USB: wdm: close race between wdm_open and wdm_wwan_port_stop
In reply to Oliver NeukumQuoted 14 lines · Oliver Neukum
Clearing
WDM_WWAN_IN_USEmust be the last action or we can open a chardev whose URBs are still poisonedFixes cac6fb015f71 ("usb: class: cdc-wdm: WWAN framework integration")Signed-off-by Oliver Neukum <[email protected]>--- drivers/usb/class/cdc-wdm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)drivers/usb/class/cdc-wdm.c +0
-0
drivers/usb/class/cdc-wdm.c static void wdm_wwan_port_stop(struct wwan_port *port) · line 876
Quoted 8 lines
drivers/usb/class/cdc-wdm.c +2
-1@@ -870,8 +870,9 @@ 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); +/* this must be last lest we open a poisoned device */ +clear_bit (WDM_WWAN_IN_USE , &desc->flags); }
This is a good example of a place where a memory barrier is needed. Neither unpoison_urbs() nor clear_bit() includes an explicit memory barrier. So even though patch ensures that unpoison_urb() occurs before clear_bit() in the source code, there is still no guarantee that a CPU will execute them in that order. Or even if they are executed in order, there is no guarantee that a different CPU will see their effects occurring in that order.
In this case you almost certainly need to have an smp_wmb() between the two statements, with a corresponding smp_rmb() (or equivalent) in the code that checks whether the WDM_WWAIN_IN_USE flag is set.
Alan Stern