USB: autoconvert trivial BKL users to private mutex

All these files use the big kernel lock in a trivial
way to serialize their private file operations,
typically resulting from an earlier semi-automatic
pushdown from VFS.

None of these drivers appears to want to lock against
other code, and they all use the BKL as the top-level
lock in their file operations, meaning that there
is no lock-order inversion problem.

Consequently, we can remove the BKL completely,
replacing it with a per-file mutex in every case.
Using a scripted approach means we can avoid
typos.

file=$1
name=$2
if grep -q lock_kernel ${file} ; then
    if grep -q 'include.*linux.mutex.h' ${file} ; then
            sed -i '/include.*<linux\/smp_lock.h>/d' ${file}
    else
            sed -i 's/include.*<linux\/smp_lock.h>.*$/include <linux\/mutex.h>/g' ${file}
    fi
    sed -i ${file} \
        -e "/^#include.*linux.mutex.h/,$ {
                1,/^\(static\|int\|long\)/ {
                     /^\(static\|int\|long\)/istatic DEFINE_MUTEX(${name}_mutex);

} }"  \
    -e "s/\(un\)*lock_kernel\>[ ]*()/mutex_\1lock(\&${name}_mutex)/g" \
    -e '/[      ]*cycle_kernel_lock();/d'
else
    sed -i -e '/include.*\<smp_lock.h\>/d' ${file}  \
                -e '/cycle_kernel_lock()/d'
fi

Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
This commit is contained in:
Arnd Bergmann 2010-07-11 23:18:56 +02:00 committed by Greg Kroah-Hartman
parent 7c7e2d0043
commit 925ce689bb
4 changed files with 28 additions and 25 deletions

View File

@ -25,7 +25,7 @@
#include <linux/ioport.h> #include <linux/ioport.h>
#include <linux/sched.h> #include <linux/sched.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/smp_lock.h> #include <linux/mutex.h>
#include <linux/errno.h> #include <linux/errno.h>
#include <linux/init.h> #include <linux/init.h>
#include <linux/timer.h> #include <linux/timer.h>
@ -70,6 +70,7 @@
#define DRIVER_DESC "Printer Gadget" #define DRIVER_DESC "Printer Gadget"
#define DRIVER_VERSION "2007 OCT 06" #define DRIVER_VERSION "2007 OCT 06"
static DEFINE_MUTEX(printer_mutex);
static const char shortname [] = "printer"; static const char shortname [] = "printer";
static const char driver_desc [] = DRIVER_DESC; static const char driver_desc [] = DRIVER_DESC;
@ -476,7 +477,7 @@ printer_open(struct inode *inode, struct file *fd)
unsigned long flags; unsigned long flags;
int ret = -EBUSY; int ret = -EBUSY;
lock_kernel(); mutex_lock(&printer_mutex);
dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev); dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev);
spin_lock_irqsave(&dev->lock, flags); spin_lock_irqsave(&dev->lock, flags);
@ -492,7 +493,7 @@ printer_open(struct inode *inode, struct file *fd)
spin_unlock_irqrestore(&dev->lock, flags); spin_unlock_irqrestore(&dev->lock, flags);
DBG(dev, "printer_open returned %x\n", ret); DBG(dev, "printer_open returned %x\n", ret);
unlock_kernel(); mutex_unlock(&printer_mutex);
return ret; return ret;
} }

View File

@ -18,7 +18,7 @@
#include <linux/init.h> #include <linux/init.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/sched.h> #include <linux/sched.h>
#include <linux/smp_lock.h> #include <linux/mutex.h>
#include <linux/poll.h> #include <linux/poll.h>
#include <linux/usb/iowarrior.h> #include <linux/usb/iowarrior.h>
@ -61,6 +61,7 @@ MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL"); MODULE_LICENSE("GPL");
/* Module parameters */ /* Module parameters */
static DEFINE_MUTEX(iowarrior_mutex);
static int debug = 0; static int debug = 0;
module_param(debug, bool, 0644); module_param(debug, bool, 0644);
MODULE_PARM_DESC(debug, "debug=1 enables debugging messages"); MODULE_PARM_DESC(debug, "debug=1 enables debugging messages");
@ -493,7 +494,7 @@ static long iowarrior_ioctl(struct file *file, unsigned int cmd,
return -ENOMEM; return -ENOMEM;
/* lock this object */ /* lock this object */
lock_kernel(); mutex_lock(&iowarrior_mutex);
mutex_lock(&dev->mutex); mutex_lock(&dev->mutex);
/* verify that the device wasn't unplugged */ /* verify that the device wasn't unplugged */
@ -585,7 +586,7 @@ static long iowarrior_ioctl(struct file *file, unsigned int cmd,
error_out: error_out:
/* unlock the device */ /* unlock the device */
mutex_unlock(&dev->mutex); mutex_unlock(&dev->mutex);
unlock_kernel(); mutex_unlock(&iowarrior_mutex);
kfree(buffer); kfree(buffer);
return retval; return retval;
} }
@ -602,12 +603,12 @@ static int iowarrior_open(struct inode *inode, struct file *file)
dbg("%s", __func__); dbg("%s", __func__);
lock_kernel(); mutex_lock(&iowarrior_mutex);
subminor = iminor(inode); subminor = iminor(inode);
interface = usb_find_interface(&iowarrior_driver, subminor); interface = usb_find_interface(&iowarrior_driver, subminor);
if (!interface) { if (!interface) {
unlock_kernel(); mutex_unlock(&iowarrior_mutex);
err("%s - error, can't find device for minor %d", __func__, err("%s - error, can't find device for minor %d", __func__,
subminor); subminor);
return -ENODEV; return -ENODEV;
@ -617,7 +618,7 @@ static int iowarrior_open(struct inode *inode, struct file *file)
dev = usb_get_intfdata(interface); dev = usb_get_intfdata(interface);
if (!dev) { if (!dev) {
mutex_unlock(&iowarrior_open_disc_lock); mutex_unlock(&iowarrior_open_disc_lock);
unlock_kernel(); mutex_unlock(&iowarrior_mutex);
return -ENODEV; return -ENODEV;
} }
@ -644,7 +645,7 @@ static int iowarrior_open(struct inode *inode, struct file *file)
out: out:
mutex_unlock(&dev->mutex); mutex_unlock(&dev->mutex);
unlock_kernel(); mutex_unlock(&iowarrior_mutex);
return retval; return retval;
} }

View File

@ -32,7 +32,7 @@
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/signal.h> #include <linux/signal.h>
#include <linux/sched.h> #include <linux/sched.h>
#include <linux/smp_lock.h> #include <linux/mutex.h>
#include <linux/errno.h> #include <linux/errno.h>
#include <linux/random.h> #include <linux/random.h>
#include <linux/poll.h> #include <linux/poll.h>
@ -72,6 +72,7 @@ struct rio_usb_data {
struct mutex lock; /* general race avoidance */ struct mutex lock; /* general race avoidance */
}; };
static DEFINE_MUTEX(rio500_mutex);
static struct rio_usb_data rio_instance; static struct rio_usb_data rio_instance;
static int open_rio(struct inode *inode, struct file *file) static int open_rio(struct inode *inode, struct file *file)
@ -79,12 +80,12 @@ static int open_rio(struct inode *inode, struct file *file)
struct rio_usb_data *rio = &rio_instance; struct rio_usb_data *rio = &rio_instance;
/* against disconnect() */ /* against disconnect() */
lock_kernel(); mutex_lock(&rio500_mutex);
mutex_lock(&(rio->lock)); mutex_lock(&(rio->lock));
if (rio->isopen || !rio->present) { if (rio->isopen || !rio->present) {
mutex_unlock(&(rio->lock)); mutex_unlock(&(rio->lock));
unlock_kernel(); mutex_unlock(&rio500_mutex);
return -EBUSY; return -EBUSY;
} }
rio->isopen = 1; rio->isopen = 1;
@ -94,7 +95,7 @@ static int open_rio(struct inode *inode, struct file *file)
mutex_unlock(&(rio->lock)); mutex_unlock(&(rio->lock));
dev_info(&rio->rio_dev->dev, "Rio opened.\n"); dev_info(&rio->rio_dev->dev, "Rio opened.\n");
unlock_kernel(); mutex_unlock(&rio500_mutex);
return 0; return 0;
} }
@ -491,7 +492,7 @@ static void disconnect_rio(struct usb_interface *intf)
struct rio_usb_data *rio = usb_get_intfdata (intf); struct rio_usb_data *rio = usb_get_intfdata (intf);
usb_set_intfdata (intf, NULL); usb_set_intfdata (intf, NULL);
lock_kernel(); mutex_lock(&rio500_mutex);
if (rio) { if (rio) {
usb_deregister_dev(intf, &usb_rio_class); usb_deregister_dev(intf, &usb_rio_class);
@ -501,7 +502,7 @@ static void disconnect_rio(struct usb_interface *intf)
/* better let it finish - the release will do whats needed */ /* better let it finish - the release will do whats needed */
rio->rio_dev = NULL; rio->rio_dev = NULL;
mutex_unlock(&(rio->lock)); mutex_unlock(&(rio->lock));
unlock_kernel(); mutex_unlock(&rio500_mutex);
return; return;
} }
kfree(rio->ibuf); kfree(rio->ibuf);
@ -512,7 +513,7 @@ static void disconnect_rio(struct usb_interface *intf)
rio->present = 0; rio->present = 0;
mutex_unlock(&(rio->lock)); mutex_unlock(&(rio->lock));
} }
unlock_kernel(); mutex_unlock(&rio500_mutex);
} }
static const struct usb_device_id rio_table[] = { static const struct usb_device_id rio_table[] = {

View File

@ -16,7 +16,6 @@
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/init.h> #include <linux/init.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/smp_lock.h>
#include <linux/errno.h> #include <linux/errno.h>
#include <linux/mutex.h> #include <linux/mutex.h>
#include <asm/uaccess.h> #include <asm/uaccess.h>
@ -30,6 +29,7 @@
#define IOCTL_GET_DRV_VERSION 2 #define IOCTL_GET_DRV_VERSION 2
static DEFINE_MUTEX(lcd_mutex);
static const struct usb_device_id id_table[] = { static const struct usb_device_id id_table[] = {
{ .idVendor = 0x10D2, .match_flags = USB_DEVICE_ID_MATCH_VENDOR, }, { .idVendor = 0x10D2, .match_flags = USB_DEVICE_ID_MATCH_VENDOR, },
{ }, { },
@ -74,12 +74,12 @@ static int lcd_open(struct inode *inode, struct file *file)
struct usb_interface *interface; struct usb_interface *interface;
int subminor, r; int subminor, r;
lock_kernel(); mutex_lock(&lcd_mutex);
subminor = iminor(inode); subminor = iminor(inode);
interface = usb_find_interface(&lcd_driver, subminor); interface = usb_find_interface(&lcd_driver, subminor);
if (!interface) { if (!interface) {
unlock_kernel(); mutex_unlock(&lcd_mutex);
err ("USBLCD: %s - error, can't find device for minor %d", err ("USBLCD: %s - error, can't find device for minor %d",
__func__, subminor); __func__, subminor);
return -ENODEV; return -ENODEV;
@ -89,7 +89,7 @@ static int lcd_open(struct inode *inode, struct file *file)
dev = usb_get_intfdata(interface); dev = usb_get_intfdata(interface);
if (!dev) { if (!dev) {
mutex_unlock(&open_disc_mutex); mutex_unlock(&open_disc_mutex);
unlock_kernel(); mutex_unlock(&lcd_mutex);
return -ENODEV; return -ENODEV;
} }
@ -101,13 +101,13 @@ static int lcd_open(struct inode *inode, struct file *file)
r = usb_autopm_get_interface(interface); r = usb_autopm_get_interface(interface);
if (r < 0) { if (r < 0) {
kref_put(&dev->kref, lcd_delete); kref_put(&dev->kref, lcd_delete);
unlock_kernel(); mutex_unlock(&lcd_mutex);
return r; return r;
} }
/* save our object in the file's private structure */ /* save our object in the file's private structure */
file->private_data = dev; file->private_data = dev;
unlock_kernel(); mutex_unlock(&lcd_mutex);
return 0; return 0;
} }
@ -164,14 +164,14 @@ static long lcd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
switch (cmd) { switch (cmd) {
case IOCTL_GET_HARD_VERSION: case IOCTL_GET_HARD_VERSION:
lock_kernel(); mutex_lock(&lcd_mutex);
bcdDevice = le16_to_cpu((dev->udev)->descriptor.bcdDevice); bcdDevice = le16_to_cpu((dev->udev)->descriptor.bcdDevice);
sprintf(buf,"%1d%1d.%1d%1d", sprintf(buf,"%1d%1d.%1d%1d",
(bcdDevice & 0xF000)>>12, (bcdDevice & 0xF000)>>12,
(bcdDevice & 0xF00)>>8, (bcdDevice & 0xF00)>>8,
(bcdDevice & 0xF0)>>4, (bcdDevice & 0xF0)>>4,
(bcdDevice & 0xF)); (bcdDevice & 0xF));
unlock_kernel(); mutex_unlock(&lcd_mutex);
if (copy_to_user((void __user *)arg,buf,strlen(buf))!=0) if (copy_to_user((void __user *)arg,buf,strlen(buf))!=0)
return -EFAULT; return -EFAULT;
break; break;