[PATCH] Add a semaphore to struct device to synchronize calls to its driver.
authormochel@digitalimplant.org <mochel@digitalimplant.org>
Mon, 21 Mar 2005 18:41:04 +0000 (10:41 -0800)
committerGreg Kroah-Hartman <gregkh@suse.de>
Mon, 20 Jun 2005 22:15:12 +0000 (15:15 -0700)
This adds a per-device semaphore that is taken before every call from the core to a
driver method. This prevents e.g. simultaneous calls to the ->suspend() or ->resume()
and ->probe() or ->release(), potentially saving a whole lot of headaches.

It also moves us a step closer to removing the bus rwsem, since it protects the fields
in struct device that are modified by the core.

Signed-off-by: Patrick Mochel <mochel@digitalimplant.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
drivers/base/bus.c
drivers/base/core.c
drivers/base/power/resume.c
drivers/base/power/suspend.c
include/linux/device.h

index 80ce88de56fa65ce505742458c32d3e67f63772a..aa27f76d28cddb19a578c761ba1762b9e6fbd02e 100644 (file)
@@ -283,18 +283,22 @@ void device_bind_driver(struct device * dev)
  */
 int driver_probe_device(struct device_driver * drv, struct device * dev)
 {
+       int error = 0;
+
        if (drv->bus->match && !drv->bus->match(dev, drv))
                return -ENODEV;
 
+       down(&dev->sem);
        dev->driver = drv;
        if (drv->probe) {
-               int error = drv->probe(dev);
+               error = drv->probe(dev);
                if (error) {
                        dev->driver = NULL;
+                       up(&dev->sem);
                        return error;
                }
        }
-
+       up(&dev->sem);
        device_bind_driver(dev);
        return 0;
 }
@@ -385,7 +389,10 @@ void driver_attach(struct device_driver * drv)
 
 void device_release_driver(struct device * dev)
 {
-       struct device_driver * drv = dev->driver;
+       struct device_driver * drv;
+
+       down(&dev->sem);
+       drv = dev->driver;
        if (drv) {
                sysfs_remove_link(&drv->kobj, kobject_name(&dev->kobj));
                sysfs_remove_link(&dev->kobj, "driver");
@@ -394,6 +401,7 @@ void device_release_driver(struct device * dev)
                        drv->remove(dev);
                dev->driver = NULL;
        }
+       up(&dev->sem);
 }
 
 
index a293a788abd42fa4847bfb2f014d4d309039819f..93440824b80056277c7497389f47a6e63f07beca 100644 (file)
@@ -212,6 +212,7 @@ void device_initialize(struct device *dev)
        INIT_LIST_HEAD(&dev->driver_list);
        INIT_LIST_HEAD(&dev->bus_list);
        INIT_LIST_HEAD(&dev->dma_pools);
+       init_MUTEX(&dev->sem);
 }
 
 /**
index 26468971ef5a7711179e87f0d500cdc680888157..bdd96b03b8857a14367535bf408224b1eb939503 100644 (file)
@@ -22,6 +22,9 @@ extern int sysdev_resume(void);
 
 int resume_device(struct device * dev)
 {
+       int error = 0;
+
+       down(&dev->sem);
        if (dev->power.pm_parent
                        && dev->power.pm_parent->power.power_state) {
                dev_err(dev, "PM: resume from %d, parent %s still %d\n",
@@ -31,9 +34,10 @@ int resume_device(struct device * dev)
        }
        if (dev->bus && dev->bus->resume) {
                dev_dbg(dev,"resuming\n");
-               return dev->bus->resume(dev);
+               error = dev->bus->resume(dev);
        }
-       return 0;
+       up(&dev->sem);
+       return error;
 }
 
 
index 0ec44ef840beaf42c89d2fdb897be5f60685a369..807e13fb205b92c51c9e51407f2e553e40f1ed0a 100644 (file)
@@ -39,6 +39,7 @@ int suspend_device(struct device * dev, pm_message_t state)
 {
        int error = 0;
 
+       down(&dev->sem);
        if (dev->power.power_state) {
                dev_dbg(dev, "PM: suspend %d-->%d\n",
                        dev->power.power_state, state);
@@ -58,7 +59,7 @@ int suspend_device(struct device * dev, pm_message_t state)
                dev_dbg(dev, "suspending\n");
                error = dev->bus->suspend(dev, state);
        }
-
+       up(&dev->sem);
        return error;
 }
 
index 68a95358013dbefb6f020ecf450f2ef2fd839e14..195b327f3e19d0ec6d4a09bca8656e4b65f9e2f4 100644 (file)
@@ -264,6 +264,10 @@ struct device {
        struct kobject kobj;
        char    bus_id[BUS_ID_SIZE];    /* position on parent bus */
 
+       struct semaphore        sem;    /* semaphore to synchronize calls to
+                                        * its driver.
+                                        */
+
        struct bus_type * bus;          /* type of bus device is on */
        struct device_driver *driver;   /* which driver has allocated this
                                           device */