public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Greg KH <gregkh@suse.de>
To: linux-kernel@vger.kernel.org
Cc: stern@rowland.harvard.edu
Subject: [PATCH] Hold the device's parent's lock during probe and remove
Date: Wed, 4 Jan 2006 16:49:30 -0800	[thread overview]
Message-ID: <11364221701993@kroah.com> (raw)
In-Reply-To: <11364221692430@kroah.com>

[PATCH] Hold the device's parent's lock during probe and remove

This patch (as604) makes the driver core hold a device's parent's lock
as well as the device's lock during calls to the probe and remove
methods in a driver.  This facility is needed by USB device drivers,
owing to the peculiar way USB devices work:

	A device provides multiple interfaces, and drivers are bound
	to interfaces rather than to devices;

	Nevertheless a reset, reset-configuration, suspend, or resume
	affects the entire device and requires the caller to hold the
	lock for the device, not just a lock for one of the interfaces.

Since a USB driver's probe method is always called with the interface
lock held, the locking order rules (always lock parent before child)
prevent these methods from acquiring the device lock.  The solution
provided here is to call all probe and remove methods, for all devices
(not just USB), with the parent lock already acquired.

Although currently only the USB subsystem requires these changes, people
have mentioned in prior discussion that the overhead of acquiring an
extra semaphore in all the prove/remove sequences is not overly large.

Up to now, the USB core has been using its own set of private
semaphores.  A followup patch will remove them, relying entirely on the
device semaphores provided by the driver core.

The code paths affected by this patch are:

	device_add and device_del: The USB core already holds the parent
	lock, so no actual change is needed.

	driver_register and driver_unregister: The driver core will now
	lock both the parent and the device before probing or removing.

	driver_bind and driver_unbind (in sysfs): These routines will
	now lock both the parent and the device before binding or
	unbinding.

	bus_rescan_devices: The helper routine will lock the parent
	before probing a device.

I have not tested this patch for conflicts with other subsystems.  As
far as I can see, the only possibility of conflict would lie in the
bus_rescan_devices pathway, and it seems pretty remote.  Nevertheless,
it would be good for this to get a lot of testing in -mm.

Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

---
commit bf74ad5bc41727d5f2f1c6bedb2c1fac394de731
tree 1e46f41550a9fe6df40fedeace23f5aff656b478
parent 6d20b035dee4300e9786c6e1cb77a765c7f9460a
author Alan Stern <stern@rowland.harvard.edu> Thu, 17 Nov 2005 16:54:12 -0500
committer Greg Kroah-Hartman <gregkh@suse.de> Wed, 04 Jan 2006 16:18:08 -0800

 drivers/base/bus.c |   15 ++++++++++++++-
 drivers/base/dd.c  |   15 ++++++++++++++-
 2 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/drivers/base/bus.c b/drivers/base/bus.c
index fa601b0..e3f915a 100644
--- a/drivers/base/bus.c
+++ b/drivers/base/bus.c
@@ -152,7 +152,11 @@ static ssize_t driver_unbind(struct devi
 
 	dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
 	if (dev && dev->driver == drv) {
+		if (dev->parent)	/* Needed for USB */
+			down(&dev->parent->sem);
 		device_release_driver(dev);
+		if (dev->parent)
+			up(&dev->parent->sem);
 		err = count;
 	}
 	put_device(dev);
@@ -175,9 +179,13 @@ static ssize_t driver_bind(struct device
 
 	dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
 	if (dev && dev->driver == NULL) {
+		if (dev->parent)	/* Needed for USB */
+			down(&dev->parent->sem);
 		down(&dev->sem);
 		err = driver_probe_device(drv, dev);
 		up(&dev->sem);
+		if (dev->parent)
+			up(&dev->parent->sem);
 	}
 	put_device(dev);
 	put_bus(bus);
@@ -484,8 +492,13 @@ void bus_remove_driver(struct device_dri
 /* Helper for bus_rescan_devices's iter */
 static int bus_rescan_devices_helper(struct device *dev, void *data)
 {
-	if (!dev->driver)
+	if (!dev->driver) {
+		if (dev->parent)	/* Needed for USB */
+			down(&dev->parent->sem);
 		device_attach(dev);
+		if (dev->parent)
+			up(&dev->parent->sem);
+	}
 	return 0;
 }
 
diff --git a/drivers/base/dd.c b/drivers/base/dd.c
index 3b419c9..2b90501 100644
--- a/drivers/base/dd.c
+++ b/drivers/base/dd.c
@@ -65,7 +65,8 @@ void device_bind_driver(struct device * 
  *	This function returns 1 if a match is found, an error if one
  *	occurs (that is not -ENODEV or -ENXIO), and 0 otherwise.
  *
- *	This function must be called with @dev->sem held.
+ *	This function must be called with @dev->sem held.  When called
+ *	for a USB interface, @dev->parent->sem must be held as well.
  */
 int driver_probe_device(struct device_driver * drv, struct device * dev)
 {
@@ -123,6 +124,8 @@ static int __device_attach(struct device
  *
  *	Returns 1 if the device was bound to a driver;
  *	0 if no matching device was found; error code otherwise.
+ *
+ *	When called for a USB interface, @dev->parent->sem must be held.
  */
 int device_attach(struct device * dev)
 {
@@ -152,10 +155,14 @@ static int __driver_attach(struct device
 	 * is an error.
 	 */
 
+	if (dev->parent)	/* Needed for USB */
+		down(&dev->parent->sem);
 	down(&dev->sem);
 	if (!dev->driver)
 		driver_probe_device(drv, dev);
 	up(&dev->sem);
+	if (dev->parent)
+		up(&dev->parent->sem);
 
 	return 0;
 }
@@ -181,6 +188,8 @@ void driver_attach(struct device_driver 
  *	Manually detach device from driver.
  *
  *	__device_release_driver() must be called with @dev->sem held.
+ *	When called for a USB interface, @dev->parent->sem must be held
+ *	as well.
  */
 
 static void __device_release_driver(struct device * dev)
@@ -233,10 +242,14 @@ void driver_detach(struct device_driver 
 		get_device(dev);
 		spin_unlock(&drv->klist_devices.k_lock);
 
+		if (dev->parent)	/* Needed for USB */
+			down(&dev->parent->sem);
 		down(&dev->sem);
 		if (dev->driver == drv)
 			__device_release_driver(dev);
 		up(&dev->sem);
+		if (dev->parent)
+			up(&dev->parent->sem);
 		put_device(dev);
 	}
 }


  reply	other threads:[~2006-01-05  0:55 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-01-05  0:48 [GIT PATCH] Driver Core patches for 2.6.15 Greg KH
2006-01-05  0:49 ` [PATCH] remove CONFIG_KOBJECT_UEVENT option Greg KH
2006-01-05  0:49   ` [PATCH] remove mount/umount uevents from superblock handling Greg KH
2006-01-05  0:49     ` [PATCH] keep pnpbios usermod_helper away from hotplug_path[] Greg KH
2006-01-05  0:49       ` [PATCH] add uevent_helper control in /sys/kernel/ Greg KH
2006-01-05  0:49         ` [PATCH] merge kobject_uevent and kobject_hotplug Greg KH
2006-01-05  0:49           ` [PATCH] driver core: replace "hotplug" by "uevent" Greg KH
2006-01-05  0:49             ` [PATCH] driver kill hotplug word from sn and others fix Greg KH
2006-01-05  0:49               ` [PATCH] HOTPLUG: always enable the .config option, unless EMBEDDED Greg KH
2006-01-05  0:49                 ` Greg KH [this message]
2006-01-05  0:49                   ` [PATCH] Allow overlapping resources for platform devices Greg KH
2006-01-05  0:49                     ` [PATCH] klist: Fix broken kref counting in find functions Greg KH
2006-01-05  0:49                       ` [PATCH] kobject_uevent CONFIG_NET=n fix Greg KH
2006-01-05  0:49                         ` [PATCH] Input: add modalias support Greg KH
2006-01-05  0:49                           ` [PATCH] ide: MODALIAS support for autoloading of ide-cd, ide-disk, Greg KH
2006-01-05  0:49                             ` [PATCH] Driver core: Make block devices create the proper symlink name Greg KH
2006-01-05  0:49                               ` [PATCH] Driver core: only all userspace bind/unbind if CONFIG_HOTPLUG is enabled Greg KH
2006-01-05  0:49                                 ` [PATCH] Driver Core: Add platform_device_del() Greg KH
2006-01-05  0:49                                   ` [PATCH] Driver Core: Rearrange exports in platform.c Greg KH
2006-01-05  0:49                                     ` [PATCH] Input: fix add modalias support build error Greg KH
2006-01-05  0:49                                       ` [PATCH] sysfs: handle failures in sysfs_make_dirent Greg KH
2006-01-05  0:49                                         ` [PATCH] drivers/base/power/runtime.c: #if 0 dpm_set_power_state() Greg KH
2006-01-05  0:49                                           ` [PATCH] net: swich device attribute creation to default attrs Greg KH
2006-01-05  1:38 ` [GIT PATCH] Driver Core patches for 2.6.15 Linus Torvalds
2006-01-05  2:07   ` Greg KH
2006-01-05  2:40     ` Linus Torvalds
2006-01-05  3:31       ` Greg KH
2006-01-05  3:36         ` Linus Torvalds
2006-01-05  3:44           ` devfs going away, last chance to complain (was Re: [GIT PATCH] Driver Core patches for 2.6.15) Greg KH
2006-01-05  7:44             ` Steven Noonan
2006-01-05  9:17               ` Andrew Walrond
2006-01-05  9:18               ` Kyle Moffett
2006-01-05 14:04           ` [GIT PATCH] Driver Core patches for 2.6.15 John Stoffel
  -- strict thread matches above, loose matches on Subject: below --
2005-11-17 21:54 [PATCH] Hold the device's parent's lock during probe and remove Alan Stern

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=11364221701993@kroah.com \
    --to=gregkh@suse.de \
    --cc=greg@kroah.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stern@rowland.harvard.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox