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