Skip to content

Commit 925ce68

Browse files
arndbgregkh
authored andcommitted
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>
1 parent 7c7e2d0 commit 925ce68

4 files changed

Lines changed: 28 additions & 25 deletions

File tree

‎drivers/usb/gadget/printer.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include <linux/ioport.h>
2626
#include <linux/sched.h>
2727
#include <linux/slab.h>
28-
#include <linux/smp_lock.h>
28+
#include <linux/mutex.h>
2929
#include <linux/errno.h>
3030
#include <linux/init.h>
3131
#include <linux/timer.h>
@@ -70,6 +70,7 @@
7070
#define DRIVER_DESC "Printer Gadget"
7171
#define DRIVER_VERSION "2007 OCT 06"
7272

73+
static DEFINE_MUTEX(printer_mutex);
7374
static const char shortname [] = "printer";
7475
static const char driver_desc [] = DRIVER_DESC;
7576

@@ -476,7 +477,7 @@ printer_open(struct inode *inode, struct file *fd)
476477
unsigned long flags;
477478
int ret = -EBUSY;
478479

479-
lock_kernel();
480+
mutex_lock(&printer_mutex);
480481
dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev);
481482

482483
spin_lock_irqsave(&dev->lock, flags);
@@ -492,7 +493,7 @@ printer_open(struct inode *inode, struct file *fd)
492493
spin_unlock_irqrestore(&dev->lock, flags);
493494

494495
DBG(dev, "printer_open returned %x\n", ret);
495-
unlock_kernel();
496+
mutex_unlock(&printer_mutex);
496497
return ret;
497498
}
498499

‎drivers/usb/misc/iowarrior.c‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include <linux/init.h>
1919
#include <linux/slab.h>
2020
#include <linux/sched.h>
21-
#include <linux/smp_lock.h>
21+
#include <linux/mutex.h>
2222
#include <linux/poll.h>
2323
#include <linux/usb/iowarrior.h>
2424

@@ -61,6 +61,7 @@ MODULE_DESCRIPTION(DRIVER_DESC);
6161
MODULE_LICENSE("GPL");
6262

6363
/* Module parameters */
64+
static DEFINE_MUTEX(iowarrior_mutex);
6465
static int debug = 0;
6566
module_param(debug, bool, 0644);
6667
MODULE_PARM_DESC(debug, "debug=1 enables debugging messages");
@@ -493,7 +494,7 @@ static long iowarrior_ioctl(struct file *file, unsigned int cmd,
493494
return -ENOMEM;
494495

495496
/* lock this object */
496-
lock_kernel();
497+
mutex_lock(&iowarrior_mutex);
497498
mutex_lock(&dev->mutex);
498499

499500
/* verify that the device wasn't unplugged */
@@ -585,7 +586,7 @@ static long iowarrior_ioctl(struct file *file, unsigned int cmd,
585586
error_out:
586587
/* unlock the device */
587588
mutex_unlock(&dev->mutex);
588-
unlock_kernel();
589+
mutex_unlock(&iowarrior_mutex);
589590
kfree(buffer);
590591
return retval;
591592
}
@@ -602,12 +603,12 @@ static int iowarrior_open(struct inode *inode, struct file *file)
602603

603604
dbg("%s", __func__);
604605

605-
lock_kernel();
606+
mutex_lock(&iowarrior_mutex);
606607
subminor = iminor(inode);
607608

608609
interface = usb_find_interface(&iowarrior_driver, subminor);
609610
if (!interface) {
610-
unlock_kernel();
611+
mutex_unlock(&iowarrior_mutex);
611612
err("%s - error, can't find device for minor %d", __func__,
612613
subminor);
613614
return -ENODEV;
@@ -617,7 +618,7 @@ static int iowarrior_open(struct inode *inode, struct file *file)
617618
dev = usb_get_intfdata(interface);
618619
if (!dev) {
619620
mutex_unlock(&iowarrior_open_disc_lock);
620-
unlock_kernel();
621+
mutex_unlock(&iowarrior_mutex);
621622
return -ENODEV;
622623
}
623624

@@ -644,7 +645,7 @@ static int iowarrior_open(struct inode *inode, struct file *file)
644645

645646
out:
646647
mutex_unlock(&dev->mutex);
647-
unlock_kernel();
648+
mutex_unlock(&iowarrior_mutex);
648649
return retval;
649650
}
650651

‎drivers/usb/misc/rio500.c‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include <linux/kernel.h>
3333
#include <linux/signal.h>
3434
#include <linux/sched.h>
35-
#include <linux/smp_lock.h>
35+
#include <linux/mutex.h>
3636
#include <linux/errno.h>
3737
#include <linux/random.h>
3838
#include <linux/poll.h>
@@ -72,19 +72,20 @@ struct rio_usb_data {
7272
struct mutex lock; /* general race avoidance */
7373
};
7474

75+
static DEFINE_MUTEX(rio500_mutex);
7576
static struct rio_usb_data rio_instance;
7677

7778
static int open_rio(struct inode *inode, struct file *file)
7879
{
7980
struct rio_usb_data *rio = &rio_instance;
8081

8182
/* against disconnect() */
82-
lock_kernel();
83+
mutex_lock(&rio500_mutex);
8384
mutex_lock(&(rio->lock));
8485

8586
if (rio->isopen || !rio->present) {
8687
mutex_unlock(&(rio->lock));
87-
unlock_kernel();
88+
mutex_unlock(&rio500_mutex);
8889
return -EBUSY;
8990
}
9091
rio->isopen = 1;
@@ -94,7 +95,7 @@ static int open_rio(struct inode *inode, struct file *file)
9495
mutex_unlock(&(rio->lock));
9596

9697
dev_info(&rio->rio_dev->dev, "Rio opened.\n");
97-
unlock_kernel();
98+
mutex_unlock(&rio500_mutex);
9899

99100
return 0;
100101
}
@@ -491,7 +492,7 @@ static void disconnect_rio(struct usb_interface *intf)
491492
struct rio_usb_data *rio = usb_get_intfdata (intf);
492493

493494
usb_set_intfdata (intf, NULL);
494-
lock_kernel();
495+
mutex_lock(&rio500_mutex);
495496
if (rio) {
496497
usb_deregister_dev(intf, &usb_rio_class);
497498

@@ -501,7 +502,7 @@ static void disconnect_rio(struct usb_interface *intf)
501502
/* better let it finish - the release will do whats needed */
502503
rio->rio_dev = NULL;
503504
mutex_unlock(&(rio->lock));
504-
unlock_kernel();
505+
mutex_unlock(&rio500_mutex);
505506
return;
506507
}
507508
kfree(rio->ibuf);
@@ -512,7 +513,7 @@ static void disconnect_rio(struct usb_interface *intf)
512513
rio->present = 0;
513514
mutex_unlock(&(rio->lock));
514515
}
515-
unlock_kernel();
516+
mutex_unlock(&rio500_mutex);
516517
}
517518

518519
static const struct usb_device_id rio_table[] = {

‎drivers/usb/misc/usblcd.c‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include <linux/kernel.h>
1717
#include <linux/init.h>
1818
#include <linux/slab.h>
19-
#include <linux/smp_lock.h>
2019
#include <linux/errno.h>
2120
#include <linux/mutex.h>
2221
#include <asm/uaccess.h>
@@ -30,6 +29,7 @@
3029
#define IOCTL_GET_DRV_VERSION 2
3130

3231

32+
static DEFINE_MUTEX(lcd_mutex);
3333
static const struct usb_device_id id_table[] = {
3434
{ .idVendor = 0x10D2, .match_flags = USB_DEVICE_ID_MATCH_VENDOR, },
3535
{ },
@@ -74,12 +74,12 @@ static int lcd_open(struct inode *inode, struct file *file)
7474
struct usb_interface *interface;
7575
int subminor, r;
7676

77-
lock_kernel();
77+
mutex_lock(&lcd_mutex);
7878
subminor = iminor(inode);
7979

8080
interface = usb_find_interface(&lcd_driver, subminor);
8181
if (!interface) {
82-
unlock_kernel();
82+
mutex_unlock(&lcd_mutex);
8383
err ("USBLCD: %s - error, can't find device for minor %d",
8484
__func__, subminor);
8585
return -ENODEV;
@@ -89,7 +89,7 @@ static int lcd_open(struct inode *inode, struct file *file)
8989
dev = usb_get_intfdata(interface);
9090
if (!dev) {
9191
mutex_unlock(&open_disc_mutex);
92-
unlock_kernel();
92+
mutex_unlock(&lcd_mutex);
9393
return -ENODEV;
9494
}
9595

@@ -101,13 +101,13 @@ static int lcd_open(struct inode *inode, struct file *file)
101101
r = usb_autopm_get_interface(interface);
102102
if (r < 0) {
103103
kref_put(&dev->kref, lcd_delete);
104-
unlock_kernel();
104+
mutex_unlock(&lcd_mutex);
105105
return r;
106106
}
107107

108108
/* save our object in the file's private structure */
109109
file->private_data = dev;
110-
unlock_kernel();
110+
mutex_unlock(&lcd_mutex);
111111

112112
return 0;
113113
}
@@ -164,14 +164,14 @@ static long lcd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
164164

165165
switch (cmd) {
166166
case IOCTL_GET_HARD_VERSION:
167-
lock_kernel();
167+
mutex_lock(&lcd_mutex);
168168
bcdDevice = le16_to_cpu((dev->udev)->descriptor.bcdDevice);
169169
sprintf(buf,"%1d%1d.%1d%1d",
170170
(bcdDevice & 0xF000)>>12,
171171
(bcdDevice & 0xF00)>>8,
172172
(bcdDevice & 0xF0)>>4,
173173
(bcdDevice & 0xF));
174-
unlock_kernel();
174+
mutex_unlock(&lcd_mutex);
175175
if (copy_to_user((void __user *)arg,buf,strlen(buf))!=0)
176176
return -EFAULT;
177177
break;

0 commit comments

Comments
 (0)