From foo@baz Tue Apr  9 12:12:43 2002
To: Greg KH <gregkh@suse.de>
Date: 21 Mar 2005 10:41:04 -08:00
From: mochel@digitalimplant.org
Subject: [driver core] Add a semaphore to struct device to synchronize calls to its driver.

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>


Index: linux-2.6.12-rc2/drivers/base/bus.c
===================================================================
--- linux-2.6.12-rc2.orig/drivers/base/bus.c	2005-04-05 14:30:57.000000000 -0700
+++ linux-2.6.12-rc2/drivers/base/bus.c	2005-04-05 14:33:43.000000000 -0700
@@ -283,18 +283,22 @@
  */
 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 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");
@@ -395,6 +402,7 @@
 			drv->remove(dev);
 		dev->driver = NULL;
 	}
+	up(&dev->sem);
 }
 
 
Index: linux-2.6.12-rc2/drivers/base/core.c
===================================================================
--- linux-2.6.12-rc2.orig/drivers/base/core.c	2005-04-05 14:33:40.000000000 -0700
+++ linux-2.6.12-rc2/drivers/base/core.c	2005-04-05 14:33:43.000000000 -0700
@@ -215,6 +215,7 @@
 	INIT_LIST_HEAD(&dev->driver_list);
 	INIT_LIST_HEAD(&dev->bus_list);
 	INIT_LIST_HEAD(&dev->dma_pools);
+	init_MUTEX(&dev->sem);
 }
 
 /**
Index: linux-2.6.12-rc2/drivers/base/power/resume.c
===================================================================
--- linux-2.6.12-rc2.orig/drivers/base/power/resume.c	2005-04-05 14:30:57.000000000 -0700
+++ linux-2.6.12-rc2/drivers/base/power/resume.c	2005-04-05 14:33:43.000000000 -0700
@@ -22,9 +22,13 @@
 
 int resume_device(struct device * dev)
 {
+	int error = 0;
+
+	down(&dev->sem);
 	if (dev->bus && dev->bus->resume)
-		return dev->bus->resume(dev);
-	return 0;
+		error = dev->bus->resume(dev);
+	up(&dev->sem);
+	return error;
 }
 
 
Index: linux-2.6.12-rc2/drivers/base/power/suspend.c
===================================================================
--- linux-2.6.12-rc2.orig/drivers/base/power/suspend.c	2005-04-05 14:30:57.000000000 -0700
+++ linux-2.6.12-rc2/drivers/base/power/suspend.c	2005-04-05 14:33:43.000000000 -0700
@@ -41,11 +41,11 @@
 
 	dev_dbg(dev, "suspending\n");
 
+	down(&dev->sem);
 	dev->power.prev_state = dev->power.power_state;
-
 	if (dev->bus && dev->bus->suspend && !dev->power.power_state)
 		error = dev->bus->suspend(dev, state);
-
+	up(&dev->sem);
 	return error;
 }
 
Index: linux-2.6.12-rc2/include/linux/device.h
===================================================================
--- linux-2.6.12-rc2.orig/include/linux/device.h	2005-04-05 14:33:38.000000000 -0700
+++ linux-2.6.12-rc2/include/linux/device.h	2005-04-05 14:33:43.000000000 -0700
@@ -264,6 +264,10 @@
 	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 */