[PATCH v4 05/43] usb: gadget: composite: fix recursive spinlock locking
From: Robert Baldyga
Date: Wed Feb 03 2016 - 07:55:58 EST
Avoid recursive spinlock locking, which could occur during
usb_gadget_deactivate() call. At it's execution path it could call
composite_disconnect() function which locks cdev->lock, previously
locked in usb_function_deactivate() to protect deactivation counter.
To fix this we introduce additional spinlock protecting deactivation
counter.
Signed-off-by: Robert Baldyga <r.baldyga@xxxxxxxxxxx>
---
drivers/usb/gadget/composite.c | 9 +++++----
include/linux/usb/composite.h | 1 +
2 files changed, 6 insertions(+), 4 deletions(-)
diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c
index 8b14c2a..65abc24 100644
--- a/drivers/usb/gadget/composite.c
+++ b/drivers/usb/gadget/composite.c
@@ -283,14 +283,14 @@ int usb_function_deactivate(struct usb_function *function)
unsigned long flags;
int status = 0;
- spin_lock_irqsave(&cdev->lock, flags);
+ spin_lock_irqsave(&cdev->deactivation_lock, flags);
if (cdev->deactivations == 0)
status = usb_gadget_deactivate(cdev->gadget);
if (status == 0)
cdev->deactivations++;
- spin_unlock_irqrestore(&cdev->lock, flags);
+ spin_unlock_irqrestore(&cdev->deactivation_lock, flags);
return status;
}
EXPORT_SYMBOL_GPL(usb_function_deactivate);
@@ -311,7 +311,7 @@ int usb_function_activate(struct usb_function *function)
unsigned long flags;
int status = 0;
- spin_lock_irqsave(&cdev->lock, flags);
+ spin_lock_irqsave(&cdev->deactivation_lock, flags);
if (WARN_ON(cdev->deactivations == 0))
status = -EINVAL;
@@ -321,7 +321,7 @@ int usb_function_activate(struct usb_function *function)
status = usb_gadget_activate(cdev->gadget);
}
- spin_unlock_irqrestore(&cdev->lock, flags);
+ spin_unlock_irqrestore(&cdev->deactivation_lock, flags);
return status;
}
EXPORT_SYMBOL_GPL(usb_function_activate);
@@ -2072,6 +2072,7 @@ static int composite_bind(struct usb_gadget *gadget,
return status;
spin_lock_init(&cdev->lock);
+ spin_lock_init(&cdev->deactivation_lock);
cdev->gadget = gadget;
set_gadget_data(gadget, cdev);
INIT_LIST_HEAD(&cdev->configs);
diff --git a/include/linux/usb/composite.h b/include/linux/usb/composite.h
index 1074b89..9911c29 100644
--- a/include/linux/usb/composite.h
+++ b/include/linux/usb/composite.h
@@ -487,6 +487,7 @@ struct usb_composite_dev {
* while the deactivation count is nonzero.
*/
unsigned deactivations;
+ spinlock_t deactivation_lock;
/* the composite driver won't complete the control transfer's
* data/status stages till delayed_status is zero.
--
1.9.1