Hi,

On Thu, Jul 18, 2013 at 09:53:22AM -0400, Alan Stern wrote:
> On Thu, 18 Jul 2013, Felipe Balbi wrote:
> 
> > usb_gadget_set_state() will call sysfs_notify()
> > which might sleep. Some users might want to call
> > usb_gadget_set_state() from the very IRQ handler
> > which actually changes the gadget state.
> > 
> > Instead of having every UDC driver add their own
> > workqueue for such a simple notification, we're
> > adding it generically to our struct usb_gadget,
> > so the details are hidden from all UDC drivers.
> > 
> > Signed-off-by: Felipe Balbi <[email protected]>
> > ---
> > 
> > Tested on OMAP5 uEVM.
> > 
> >  drivers/usb/gadget/udc-core.c | 11 ++++++++++-
> >  include/linux/usb/gadget.h    |  4 ++++
> >  2 files changed, 14 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/usb/gadget/udc-core.c b/drivers/usb/gadget/udc-core.c
> > index ffd8fa5..b0d91b1 100644
> > --- a/drivers/usb/gadget/udc-core.c
> > +++ b/drivers/usb/gadget/udc-core.c
> > @@ -23,6 +23,7 @@
> >  #include <linux/list.h>
> >  #include <linux/err.h>
> >  #include <linux/dma-mapping.h>
> > +#include <linux/workqueue.h>
> >  
> >  #include <linux/usb/ch9.h>
> >  #include <linux/usb/gadget.h>
> > @@ -101,11 +102,18 @@ EXPORT_SYMBOL_GPL(usb_gadget_unmap_request);
> >  
> >  /* 
> > ------------------------------------------------------------------------- */
> >  
> > +static void usb_gadget_state_work(struct work_struct *work)
> > +{
> > +   struct usb_gadget       *gadget = work_to_gadget(work);
> > +
> > +   sysfs_notify(&gadget->dev.kobj, NULL, "status");
> > +}
> > +
> >  void usb_gadget_set_state(struct usb_gadget *gadget,
> >             enum usb_device_state state)
> >  {
> >     gadget->state = state;
> > -   sysfs_notify(&gadget->dev.kobj, NULL, "status");
> > +   schedule_work(&gadget->work);
> >  }
> >  EXPORT_SYMBOL_GPL(usb_gadget_set_state);
> >  
> > @@ -192,6 +200,7 @@ int usb_add_gadget_udc_release(struct device *parent, 
> > struct usb_gadget *gadget,
> >             goto err1;
> >  
> >     dev_set_name(&gadget->dev, "gadget");
> > +   INIT_WORK(&gadget->work, usb_gadget_state_work);
> >     gadget->dev.parent = parent;
> >  
> >     dma_set_coherent_mask(&gadget->dev, parent->coherent_dma_mask);
> 
> Deallocation of the gadget structure races with the work routine.  You 
> need to wait for any scheduled work to complete when the gadget is 
> unregistered.

indeed. Added a flush_work() call to usb_del_gadget_udc()

> Also, what happens if two state transitions occur before the work queue 
> gets around to executing the work routine?

do we need to care about that at all ? It's a queue anyway, transitions
will still be notified in order.

Here's a new diff:

commit b2e025e9d347bd0858a50dbf222408c070d6801e
Author: Felipe Balbi <[email protected]>
Date:   Wed Jul 17 11:09:49 2013 +0300

    usb: gadget: udc-core: move sysfs_notify() to a workqueue
    
    usb_gadget_set_state() will call sysfs_notify()
    which might sleep. Some users might want to call
    usb_gadget_set_state() from the very IRQ handler
    which actually changes the gadget state.
    
    Instead of having every UDC driver add their own
    workqueue for such a simple notification, we're
    adding it generically to our struct usb_gadget,
    so the details are hidden from all UDC drivers.
    
    Signed-off-by: Felipe Balbi <[email protected]>

diff --git a/drivers/usb/gadget/udc-core.c b/drivers/usb/gadget/udc-core.c
index ffd8fa5..ee5a0fa 100644
--- a/drivers/usb/gadget/udc-core.c
+++ b/drivers/usb/gadget/udc-core.c
@@ -23,6 +23,7 @@
 #include <linux/list.h>
 #include <linux/err.h>
 #include <linux/dma-mapping.h>
+#include <linux/workqueue.h>
 
 #include <linux/usb/ch9.h>
 #include <linux/usb/gadget.h>
@@ -101,11 +102,18 @@ EXPORT_SYMBOL_GPL(usb_gadget_unmap_request);
 
 /* ------------------------------------------------------------------------- */
 
+static void usb_gadget_state_work(struct work_struct *work)
+{
+       struct usb_gadget       *gadget = work_to_gadget(work);
+
+       sysfs_notify(&gadget->dev.kobj, NULL, "status");
+}
+
 void usb_gadget_set_state(struct usb_gadget *gadget,
                enum usb_device_state state)
 {
        gadget->state = state;
-       sysfs_notify(&gadget->dev.kobj, NULL, "status");
+       schedule_work(&gadget->work);
 }
 EXPORT_SYMBOL_GPL(usb_gadget_set_state);
 
@@ -192,6 +200,7 @@ int usb_add_gadget_udc_release(struct device *parent, 
struct usb_gadget *gadget,
                goto err1;
 
        dev_set_name(&gadget->dev, "gadget");
+       INIT_WORK(&gadget->work, usb_gadget_state_work);
        gadget->dev.parent = parent;
 
        dma_set_coherent_mask(&gadget->dev, parent->coherent_dma_mask);
@@ -311,6 +320,7 @@ found:
        kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
        device_unregister(&udc->dev);
        device_unregister(&gadget->dev);
+       flush_work(&gadget->work);
 }
 EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
 
diff --git a/include/linux/usb/gadget.h b/include/linux/usb/gadget.h
index f1b0dca..942ef5e 100644
--- a/include/linux/usb/gadget.h
+++ b/include/linux/usb/gadget.h
@@ -22,6 +22,7 @@
 #include <linux/slab.h>
 #include <linux/scatterlist.h>
 #include <linux/types.h>
+#include <linux/workqueue.h>
 #include <linux/usb/ch9.h>
 
 struct usb_ep;
@@ -475,6 +476,7 @@ struct usb_gadget_ops {
 
 /**
  * struct usb_gadget - represents a usb slave device
+ * @work: (internal use) Workqueue to be used for sysfs_notify()
  * @ops: Function pointers used to access hardware-specific operations.
  * @ep0: Endpoint zero, used when reading or writing responses to
  *     driver setup() requests
@@ -520,6 +522,7 @@ struct usb_gadget_ops {
  * device is acting as a B-Peripheral (so is_a_peripheral is false).
  */
 struct usb_gadget {
+       struct work_struct              work;
        /* readonly to gadget driver */
        const struct usb_gadget_ops     *ops;
        struct usb_ep                   *ep0;
@@ -538,6 +541,7 @@ struct usb_gadget {
        unsigned                        out_epnum;
        unsigned                        in_epnum;
 };
+#define work_to_gadget(w)      (container_of((w), struct usb_gadget, work))
 
 static inline void set_gadget_data(struct usb_gadget *gadget, void *data)
        { dev_set_drvdata(&gadget->dev, data); }

-- 
balbi

Attachment: signature.asc
Description: Digital signature

Reply via email to