Skip to content

Commit 49b420a

Browse files
ming1gregkh
authored andcommitted
driver core: check bus->match without holding device lock
This patch moves bus->match out from driver_probe_device and does not hold device lock to check the match between a device and a driver. The idea has been verified by the commit 6cd4958, which leads to a faster boot. But the commit 6cd4958 has the following drawbacks: 1),only does the quick check in the path of __driver_attach->driver_probe_device, not in other paths; 2),for a matched device and driver, check the same match twice. It is a waste of cpu ,especially for some drivers with long device id table (eg. usb-storage driver). This patch adds a helper of driver_match_device to check the match in all paths, and testes the match only once. Signed-off-by: Ming Lei <tom.leiming@gmail.com> Acked-by: Cornelia Huck <cornelia.huck@de.ibm.com> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
1 parent 4a67a1b commit 49b420a

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

drivers/base/base.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ extern void bus_remove_driver(struct device_driver *drv);
8686

8787
extern void driver_detach(struct device_driver *drv);
8888
extern int driver_probe_device(struct device_driver *drv, struct device *dev);
89+
static inline int driver_match_device(struct device_driver *drv,
90+
struct device *dev)
91+
{
92+
return drv->bus->match && drv->bus->match(dev, drv);
93+
}
8994

9095
extern void sysdev_shutdown(void);
9196

drivers/base/bus.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ static ssize_t driver_bind(struct device_driver *drv,
198198
int err = -ENODEV;
199199

200200
dev = bus_find_device_by_name(bus, NULL, buf);
201-
if (dev && dev->driver == NULL) {
201+
if (dev && dev->driver == NULL && driver_match_device(drv, dev)) {
202202
if (dev->parent) /* Needed for USB */
203203
down(&dev->parent->sem);
204204
down(&dev->sem);

drivers/base/dd.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,8 @@ int wait_for_device_probe(void)
189189
* @drv: driver to bind a device to
190190
* @dev: device to try to bind to the driver
191191
*
192-
* First, we call the bus's match function, if one present, which should
193-
* compare the device IDs the driver supports with the device IDs of the
194-
* device. Note we don't do this ourselves because we don't know the
195-
* format of the ID structures, nor what is to be considered a match and
196-
* what is not.
197-
*
198-
* This function returns 1 if a match is found, -ENODEV if the device is
199-
* not registered, and 0 otherwise.
192+
* This function returns -ENODEV if the device is not registered,
193+
* 1 if the device is bound sucessfully and 0 otherwise.
200194
*
201195
* This function must be called with @dev->sem held. When called for a
202196
* USB interface, @dev->parent->sem must be held as well.
@@ -207,21 +201,22 @@ int driver_probe_device(struct device_driver *drv, struct device *dev)
207201

208202
if (!device_is_registered(dev))
209203
return -ENODEV;
210-
if (drv->bus->match && !drv->bus->match(dev, drv))
211-
goto done;
212204

213205
pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
214206
drv->bus->name, __func__, dev_name(dev), drv->name);
215207

216208
ret = really_probe(dev, drv);
217209

218-
done:
219210
return ret;
220211
}
221212

222213
static int __device_attach(struct device_driver *drv, void *data)
223214
{
224215
struct device *dev = data;
216+
217+
if (!driver_match_device(drv, dev))
218+
return 0;
219+
225220
return driver_probe_device(drv, dev);
226221
}
227222

@@ -274,7 +269,7 @@ static int __driver_attach(struct device *dev, void *data)
274269
* is an error.
275270
*/
276271

277-
if (drv->bus->match && !drv->bus->match(dev, drv))
272+
if (!driver_match_device(drv, dev))
278273
return 0;
279274

280275
if (dev->parent) /* Needed for USB */

0 commit comments

Comments
 (0)