Driver core: create lock/unlock functions for struct device

In the future, we are going to be changing the lock type for struct
device (once we get the lockdep infrastructure properly worked out) To
make that changeover easier, and to possibly burry the lock in a
different part of struct device, let's create some functions to lock and
unlock a device so that no out-of-core code needs to be changed in the
future.

This patch creates the device_lock/unlock/trylock() functions, and
converts all in-tree users to them.

Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Jean Delvare <khali@linux-fr.org>
Cc: Dave Young <hidave.darkstar@gmail.com>
Cc: Ming Lei <tom.leiming@gmail.com>
Cc: Jiri Kosina <jkosina@suse.cz>
Cc: Phil Carmody <ext-phil.2.carmody@nokia.com>
Cc: Arjan van de Ven <arjan@linux.intel.com>
Cc: Cornelia Huck <cornelia.huck@de.ibm.com>
Cc: Rafael J. Wysocki <rjw@sisk.pl>
Cc: Pavel Machek <pavel@ucw.cz>
Cc: Len Brown <len.brown@intel.com>
Cc: Magnus Damm <damm@igel.co.jp>
Cc: Alan Stern <stern@rowland.harvard.edu>
Cc: Randy Dunlap <randy.dunlap@oracle.com>
Cc: Stefan Richter <stefanr@s5r6.in-berlin.de>
Cc: David Brownell <dbrownell@users.sourceforge.net>
Cc: Vegard Nossum <vegard.nossum@gmail.com>
Cc: Jesse Barnes <jbarnes@virtuousgeek.org>
Cc: Alex Chiang <achiang@hp.com>
Cc: Kenji Kaneshige <kaneshige.kenji@jp.fujitsu.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andrew Patterson <andrew.patterson@hp.com>
Cc: Yu Zhao <yu.zhao@intel.com>
Cc: Dominik Brodowski <linux@dominikbrodowski.net>
Cc: Samuel Ortiz <sameo@linux.intel.com>
Cc: Wolfram Sang <w.sang@pengutronix.de>
Cc: CHENG Renquan <rqcheng@smu.edu.sg>
Cc: Oliver Neukum <oliver@neukum.org>
Cc: Frans Pop <elendil@planet.nl>
Cc: David Vrabel <david.vrabel@csr.com>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Sarah Sharp <sarah.a.sharp@linux.intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>


+76 -63
+10 -10
drivers/base/bus.c
··· 173 173 dev = bus_find_device_by_name(bus, NULL, buf); 174 174 if (dev && dev->driver == drv) { 175 175 if (dev->parent) /* Needed for USB */ 176 - down(&dev->parent->sem); 176 + device_lock(dev->parent); 177 177 device_release_driver(dev); 178 178 if (dev->parent) 179 - up(&dev->parent->sem); 179 + device_unlock(dev->parent); 180 180 err = count; 181 181 } 182 182 put_device(dev); ··· 200 200 dev = bus_find_device_by_name(bus, NULL, buf); 201 201 if (dev && dev->driver == NULL && driver_match_device(drv, dev)) { 202 202 if (dev->parent) /* Needed for USB */ 203 - down(&dev->parent->sem); 204 - down(&dev->sem); 203 + device_lock(dev->parent); 204 + device_lock(dev); 205 205 err = driver_probe_device(drv, dev); 206 - up(&dev->sem); 206 + device_unlock(dev); 207 207 if (dev->parent) 208 - up(&dev->parent->sem); 208 + device_unlock(dev->parent); 209 209 210 210 if (err > 0) { 211 211 /* success */ ··· 744 744 745 745 if (!dev->driver) { 746 746 if (dev->parent) /* Needed for USB */ 747 - down(&dev->parent->sem); 747 + device_lock(dev->parent); 748 748 ret = device_attach(dev); 749 749 if (dev->parent) 750 - up(&dev->parent->sem); 750 + device_unlock(dev->parent); 751 751 } 752 752 return ret < 0 ? ret : 0; 753 753 } ··· 779 779 { 780 780 if (dev->driver) { 781 781 if (dev->parent) /* Needed for USB */ 782 - down(&dev->parent->sem); 782 + device_lock(dev->parent); 783 783 device_release_driver(dev); 784 784 if (dev->parent) 785 - up(&dev->parent->sem); 785 + device_unlock(dev->parent); 786 786 } 787 787 return bus_rescan_devices_helper(dev, NULL); 788 788 }
+19 -19
drivers/base/dd.c
··· 85 85 * for before calling this. (It is ok to call with no other effort 86 86 * from a driver's probe() method.) 87 87 * 88 - * This function must be called with @dev->sem held. 88 + * This function must be called with the device lock held. 89 89 */ 90 90 int device_bind_driver(struct device *dev) 91 91 { ··· 190 190 * This function returns -ENODEV if the device is not registered, 191 191 * 1 if the device is bound successfully and 0 otherwise. 192 192 * 193 - * This function must be called with @dev->sem held. When called for a 194 - * USB interface, @dev->parent->sem must be held as well. 193 + * This function must be called with @dev lock held. When called for a 194 + * USB interface, @dev->parent lock must be held as well. 195 195 */ 196 196 int driver_probe_device(struct device_driver *drv, struct device *dev) 197 197 { ··· 233 233 * 0 if no matching driver was found; 234 234 * -ENODEV if the device is not registered. 235 235 * 236 - * When called for a USB interface, @dev->parent->sem must be held. 236 + * When called for a USB interface, @dev->parent lock must be held. 237 237 */ 238 238 int device_attach(struct device *dev) 239 239 { 240 240 int ret = 0; 241 241 242 - down(&dev->sem); 242 + device_lock(dev); 243 243 if (dev->driver) { 244 244 ret = device_bind_driver(dev); 245 245 if (ret == 0) ··· 253 253 ret = bus_for_each_drv(dev->bus, NULL, dev, __device_attach); 254 254 pm_runtime_put_sync(dev); 255 255 } 256 - up(&dev->sem); 256 + device_unlock(dev); 257 257 return ret; 258 258 } 259 259 EXPORT_SYMBOL_GPL(device_attach); ··· 276 276 return 0; 277 277 278 278 if (dev->parent) /* Needed for USB */ 279 - down(&dev->parent->sem); 280 - down(&dev->sem); 279 + device_lock(dev->parent); 280 + device_lock(dev); 281 281 if (!dev->driver) 282 282 driver_probe_device(drv, dev); 283 - up(&dev->sem); 283 + device_unlock(dev); 284 284 if (dev->parent) 285 - up(&dev->parent->sem); 285 + device_unlock(dev->parent); 286 286 287 287 return 0; 288 288 } ··· 303 303 EXPORT_SYMBOL_GPL(driver_attach); 304 304 305 305 /* 306 - * __device_release_driver() must be called with @dev->sem held. 307 - * When called for a USB interface, @dev->parent->sem must be held as well. 306 + * __device_release_driver() must be called with @dev lock held. 307 + * When called for a USB interface, @dev->parent lock must be held as well. 308 308 */ 309 309 static void __device_release_driver(struct device *dev) 310 310 { ··· 343 343 * @dev: device. 344 344 * 345 345 * Manually detach device from driver. 346 - * When called for a USB interface, @dev->parent->sem must be held. 346 + * When called for a USB interface, @dev->parent lock must be held. 347 347 */ 348 348 void device_release_driver(struct device *dev) 349 349 { ··· 352 352 * within their ->remove callback for the same device, they 353 353 * will deadlock right here. 354 354 */ 355 - down(&dev->sem); 355 + device_lock(dev); 356 356 __device_release_driver(dev); 357 - up(&dev->sem); 357 + device_unlock(dev); 358 358 } 359 359 EXPORT_SYMBOL_GPL(device_release_driver); 360 360 ··· 381 381 spin_unlock(&drv->p->klist_devices.k_lock); 382 382 383 383 if (dev->parent) /* Needed for USB */ 384 - down(&dev->parent->sem); 385 - down(&dev->sem); 384 + device_lock(dev->parent); 385 + device_lock(dev); 386 386 if (dev->driver == drv) 387 387 __device_release_driver(dev); 388 - up(&dev->sem); 388 + device_unlock(dev); 389 389 if (dev->parent) 390 - up(&dev->parent->sem); 390 + device_unlock(dev->parent); 391 391 put_device(dev); 392 392 } 393 393 }
+10 -10
drivers/base/power/main.c
··· 35 35 * because children are guaranteed to be discovered after parents, and 36 36 * are inserted at the back of the list on discovery. 37 37 * 38 - * Since device_pm_add() may be called with a device semaphore held, 39 - * we must never try to acquire a device semaphore while holding 38 + * Since device_pm_add() may be called with a device lock held, 39 + * we must never try to acquire a device lock while holding 40 40 * dpm_list_mutex. 41 41 */ 42 42 ··· 508 508 TRACE_RESUME(0); 509 509 510 510 dpm_wait(dev->parent, async); 511 - down(&dev->sem); 511 + device_lock(dev); 512 512 513 513 dev->power.status = DPM_RESUMING; 514 514 ··· 543 543 } 544 544 } 545 545 End: 546 - up(&dev->sem); 546 + device_unlock(dev); 547 547 complete_all(&dev->power.completion); 548 548 549 549 TRACE_RESUME(error); ··· 629 629 */ 630 630 static void device_complete(struct device *dev, pm_message_t state) 631 631 { 632 - down(&dev->sem); 632 + device_lock(dev); 633 633 634 634 if (dev->class && dev->class->pm && dev->class->pm->complete) { 635 635 pm_dev_dbg(dev, state, "completing class "); ··· 646 646 dev->bus->pm->complete(dev); 647 647 } 648 648 649 - up(&dev->sem); 649 + device_unlock(dev); 650 650 } 651 651 652 652 /** ··· 809 809 int error = 0; 810 810 811 811 dpm_wait_for_children(dev, async); 812 - down(&dev->sem); 812 + device_lock(dev); 813 813 814 814 if (async_error) 815 815 goto End; ··· 849 849 dev->power.status = DPM_OFF; 850 850 851 851 End: 852 - up(&dev->sem); 852 + device_unlock(dev); 853 853 complete_all(&dev->power.completion); 854 854 855 855 return error; ··· 938 938 { 939 939 int error = 0; 940 940 941 - down(&dev->sem); 941 + device_lock(dev); 942 942 943 943 if (dev->bus && dev->bus->pm && dev->bus->pm->prepare) { 944 944 pm_dev_dbg(dev, state, "preparing "); ··· 962 962 suspend_report_result(dev->class->pm->prepare, error); 963 963 } 964 964 End: 965 - up(&dev->sem); 965 + device_unlock(dev); 966 966 967 967 return error; 968 968 }
+2 -3
drivers/firewire/core-device.c
··· 33 33 #include <linux/module.h> 34 34 #include <linux/mutex.h> 35 35 #include <linux/rwsem.h> 36 - #include <linux/semaphore.h> 37 36 #include <linux/spinlock.h> 38 37 #include <linux/string.h> 39 38 #include <linux/workqueue.h> ··· 827 828 struct fw_driver *driver = (struct fw_driver *)dev->driver; 828 829 829 830 if (is_fw_unit(dev) && driver != NULL && driver->update != NULL) { 830 - down(&dev->sem); 831 + device_lock(dev); 831 832 driver->update(unit); 832 - up(&dev->sem); 833 + device_unlock(dev); 833 834 } 834 835 835 836 return 0;
+2 -3
drivers/ieee1394/nodemgr.c
··· 19 19 #include <linux/moduleparam.h> 20 20 #include <linux/mutex.h> 21 21 #include <linux/freezer.h> 22 - #include <linux/semaphore.h> 23 22 #include <asm/atomic.h> 24 23 25 24 #include "csr.h" ··· 1396 1397 pdrv = container_of(drv, struct hpsb_protocol_driver, 1397 1398 driver); 1398 1399 if (pdrv->update) { 1399 - down(&ud->device.sem); 1400 + device_lock(&ud->device); 1400 1401 error = pdrv->update(ud); 1401 - up(&ud->device.sem); 1402 + device_unlock(&ud->device); 1402 1403 } 1403 1404 if (error) 1404 1405 device_release_driver(&ud->device);
+2 -2
drivers/pci/bus.c
··· 288 288 next = dev->bus_list.next; 289 289 290 290 /* Run device routines with the device locked */ 291 - down(&dev->dev.sem); 291 + device_lock(&dev->dev); 292 292 retval = cb(dev, userdata); 293 - up(&dev->dev.sem); 293 + device_unlock(&dev->dev); 294 294 if (retval) 295 295 break; 296 296 }
+2 -2
drivers/pci/pci.c
··· 2486 2486 if (!probe) { 2487 2487 pci_block_user_cfg_access(dev); 2488 2488 /* block PM suspend, driver probe, etc. */ 2489 - down(&dev->dev.sem); 2489 + device_lock(&dev->dev); 2490 2490 } 2491 2491 2492 2492 rc = pci_dev_specific_reset(dev, probe); ··· 2508 2508 rc = pci_parent_bus_reset(dev, probe); 2509 2509 done: 2510 2510 if (!probe) { 2511 - up(&dev->dev.sem); 2511 + device_unlock(&dev->dev); 2512 2512 pci_unblock_user_cfg_access(dev); 2513 2513 } 2514 2514
+4 -4
drivers/pcmcia/ds.c
··· 971 971 { 972 972 int rc; 973 973 974 - down(&dev->sem); 974 + device_lock(dev); 975 975 rc = pcmcia_dev_suspend(dev, PMSG_SUSPEND); 976 - up(&dev->sem); 976 + device_unlock(dev); 977 977 return rc; 978 978 } 979 979 ··· 981 981 { 982 982 int rc; 983 983 984 - down(&dev->sem); 984 + device_lock(dev); 985 985 rc = pcmcia_dev_resume(dev); 986 - up(&dev->sem); 986 + device_unlock(dev); 987 987 return rc; 988 988 } 989 989
+2 -2
drivers/usb/core/driver.c
··· 489 489 if (device_is_registered(dev)) { 490 490 device_release_driver(dev); 491 491 } else { 492 - down(&dev->sem); 492 + device_lock(dev); 493 493 usb_unbind_interface(dev); 494 494 dev->driver = NULL; 495 - up(&dev->sem); 495 + device_unlock(dev); 496 496 } 497 497 } 498 498 EXPORT_SYMBOL_GPL(usb_driver_release_interface);
+2 -2
drivers/uwb/umc-bus.c
··· 62 62 struct device *parent = umc->dev.parent; 63 63 int ret = 0; 64 64 65 - if(down_trylock(&parent->sem)) 65 + if (device_trylock(parent)) 66 66 return -EAGAIN; 67 67 ret = device_for_each_child(parent, parent, umc_bus_pre_reset_helper); 68 68 if (ret >= 0) 69 69 ret = device_for_each_child(parent, parent, umc_bus_post_reset_helper); 70 - up(&parent->sem); 70 + device_unlock(parent); 71 71 72 72 return ret; 73 73 }
+2 -2
drivers/uwb/uwb-internal.h
··· 366 366 367 367 static inline void uwb_dev_lock(struct uwb_dev *uwb_dev) 368 368 { 369 - down(&uwb_dev->dev.sem); 369 + device_lock(&uwb_dev->dev); 370 370 } 371 371 372 372 static inline void uwb_dev_unlock(struct uwb_dev *uwb_dev) 373 373 { 374 - up(&uwb_dev->dev.sem); 374 + device_unlock(&uwb_dev->dev); 375 375 } 376 376 377 377 #endif /* #ifndef __UWB_INTERNAL_H__ */
+16 -1
include/linux/device.h
··· 106 106 107 107 /* All 4 notifers below get called with the target struct device * 108 108 * as an argument. Note that those functions are likely to be called 109 - * with the device semaphore held in the core, so be careful. 109 + * with the device lock held in the core, so be careful. 110 110 */ 111 111 #define BUS_NOTIFY_ADD_DEVICE 0x00000001 /* device added */ 112 112 #define BUS_NOTIFY_DEL_DEVICE 0x00000002 /* device removed */ ··· 506 506 static inline bool device_async_suspend_enabled(struct device *dev) 507 507 { 508 508 return !!dev->power.async_suspend; 509 + } 510 + 511 + static inline void device_lock(struct device *dev) 512 + { 513 + down(&dev->sem); 514 + } 515 + 516 + static inline int device_trylock(struct device *dev) 517 + { 518 + return down_trylock(&dev->sem); 519 + } 520 + 521 + static inline void device_unlock(struct device *dev) 522 + { 523 + up(&dev->sem); 509 524 } 510 525 511 526 void driver_init(void);
+3 -3
include/linux/usb.h
··· 512 512 extern void usb_put_dev(struct usb_device *dev); 513 513 514 514 /* USB device locking */ 515 - #define usb_lock_device(udev) down(&(udev)->dev.sem) 516 - #define usb_unlock_device(udev) up(&(udev)->dev.sem) 517 - #define usb_trylock_device(udev) down_trylock(&(udev)->dev.sem) 515 + #define usb_lock_device(udev) device_lock(&(udev)->dev) 516 + #define usb_unlock_device(udev) device_unlock(&(udev)->dev) 517 + #define usb_trylock_device(udev) device_trylock(&(udev)->dev) 518 518 extern int usb_lock_device_for_reset(struct usb_device *udev, 519 519 const struct usb_interface *iface); 520 520