Skip to content

Commit

Permalink
drivers: autoconvert trivial BKL users to private mutex
Browse files Browse the repository at this point in the history
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.

These drivers do not seem to be under active
maintainance from my brief investigation. Apologies
to those maintainers that I have missed.

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 <[email protected]>
  • Loading branch information
arndb committed Oct 5, 2010
1 parent 609146f commit 613655f
Show file tree
Hide file tree
Showing 35 changed files with 216 additions and 190 deletions.
7 changes: 4 additions & 3 deletions drivers/block/paride/pg.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ enum {D_PRT, D_PRO, D_UNI, D_MOD, D_SLV, D_DLY};
#include <linux/pg.h>
#include <linux/device.h>
#include <linux/sched.h> /* current, TASK_* */
#include <linux/smp_lock.h>
#include <linux/mutex.h>
#include <linux/jiffies.h>

#include <asm/uaccess.h>
Expand Down Expand Up @@ -193,6 +193,7 @@ module_param_array(drive3, int, NULL, 0);

#define ATAPI_IDENTIFY 0x12

static DEFINE_MUTEX(pg_mutex);
static int pg_open(struct inode *inode, struct file *file);
static int pg_release(struct inode *inode, struct file *file);
static ssize_t pg_read(struct file *filp, char __user *buf,
Expand Down Expand Up @@ -518,7 +519,7 @@ static int pg_open(struct inode *inode, struct file *file)
struct pg *dev = &devices[unit];
int ret = 0;

lock_kernel();
mutex_lock(&pg_mutex);
if ((unit >= PG_UNITS) || (!dev->present)) {
ret = -ENODEV;
goto out;
Expand Down Expand Up @@ -547,7 +548,7 @@ static int pg_open(struct inode *inode, struct file *file)
file->private_data = dev;

out:
unlock_kernel();
mutex_unlock(&pg_mutex);
return ret;
}

Expand Down
19 changes: 10 additions & 9 deletions drivers/block/paride/pt.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ static int (*drives[4])[6] = {&drive0, &drive1, &drive2, &drive3};
#include <linux/mtio.h>
#include <linux/device.h>
#include <linux/sched.h> /* current, TASK_*, schedule_timeout() */
#include <linux/smp_lock.h>
#include <linux/mutex.h>

#include <asm/uaccess.h>

Expand Down Expand Up @@ -189,6 +189,7 @@ module_param_array(drive3, int, NULL, 0);
#define ATAPI_MODE_SENSE 0x1a
#define ATAPI_LOG_SENSE 0x4d

static DEFINE_MUTEX(pt_mutex);
static int pt_open(struct inode *inode, struct file *file);
static long pt_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
static int pt_release(struct inode *inode, struct file *file);
Expand Down Expand Up @@ -650,9 +651,9 @@ static int pt_open(struct inode *inode, struct file *file)
struct pt_unit *tape = pt + unit;
int err;

lock_kernel();
mutex_lock(&pt_mutex);
if (unit >= PT_UNITS || (!tape->present)) {
unlock_kernel();
mutex_unlock(&pt_mutex);
return -ENODEV;
}

Expand Down Expand Up @@ -681,12 +682,12 @@ static int pt_open(struct inode *inode, struct file *file)
}

file->private_data = tape;
unlock_kernel();
mutex_unlock(&pt_mutex);
return 0;

out:
atomic_inc(&tape->available);
unlock_kernel();
mutex_unlock(&pt_mutex);
return err;
}

Expand All @@ -704,15 +705,15 @@ static long pt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
switch (mtop.mt_op) {

case MTREW:
lock_kernel();
mutex_lock(&pt_mutex);
pt_rewind(tape);
unlock_kernel();
mutex_unlock(&pt_mutex);
return 0;

case MTWEOF:
lock_kernel();
mutex_lock(&pt_mutex);
pt_write_fm(tape);
unlock_kernel();
mutex_unlock(&pt_mutex);
return 0;

default:
Expand Down
11 changes: 6 additions & 5 deletions drivers/char/apm-emulation.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <linux/module.h>
#include <linux/poll.h>
#include <linux/slab.h>
#include <linux/smp_lock.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/miscdevice.h>
Expand Down Expand Up @@ -126,6 +126,7 @@ struct apm_user {
/*
* Local variables
*/
static DEFINE_MUTEX(apm_mutex);
static atomic_t suspend_acks_pending = ATOMIC_INIT(0);
static atomic_t userspace_notification_inhibit = ATOMIC_INIT(0);
static int apm_disabled;
Expand Down Expand Up @@ -274,7 +275,7 @@ apm_ioctl(struct file *filp, u_int cmd, u_long arg)
if (!as->suser || !as->writer)
return -EPERM;

lock_kernel();
mutex_lock(&apm_mutex);
switch (cmd) {
case APM_IOC_SUSPEND:
mutex_lock(&state_lock);
Expand Down Expand Up @@ -335,7 +336,7 @@ apm_ioctl(struct file *filp, u_int cmd, u_long arg)
mutex_unlock(&state_lock);
break;
}
unlock_kernel();
mutex_unlock(&apm_mutex);

return err;
}
Expand Down Expand Up @@ -370,7 +371,7 @@ static int apm_open(struct inode * inode, struct file * filp)
{
struct apm_user *as;

lock_kernel();
mutex_lock(&apm_mutex);
as = kzalloc(sizeof(*as), GFP_KERNEL);
if (as) {
/*
Expand All @@ -390,7 +391,7 @@ static int apm_open(struct inode * inode, struct file * filp)

filp->private_data = as;
}
unlock_kernel();
mutex_unlock(&apm_mutex);

return as ? 0 : -ENOMEM;
}
Expand Down
9 changes: 5 additions & 4 deletions drivers/char/applicom.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/smp_lock.h>
#include <linux/mutex.h>
#include <linux/miscdevice.h>
#include <linux/pci.h>
#include <linux/wait.h>
Expand Down Expand Up @@ -60,6 +60,7 @@
#define PCI_DEVICE_ID_APPLICOM_PCI2000PFB 0x0003
#endif

static DEFINE_MUTEX(ac_mutex);
static char *applicom_pci_devnames[] = {
"PCI board",
"PCI2000IBS / PCI2000CAN",
Expand Down Expand Up @@ -707,7 +708,7 @@ static long ac_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
if (IS_ERR(adgl))
return PTR_ERR(adgl);

lock_kernel();
mutex_lock(&ac_mutex);
IndexCard = adgl->num_card-1;

if(cmd != 6 && ((IndexCard >= MAX_BOARD) || !apbs[IndexCard].RamIO)) {
Expand All @@ -717,7 +718,7 @@ static long ac_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
warncount--;
}
kfree(adgl);
unlock_kernel();
mutex_unlock(&ac_mutex);
return -EINVAL;
}

Expand Down Expand Up @@ -835,7 +836,7 @@ static long ac_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
}
Dummy = readb(apbs[IndexCard].RamIO + VERS);
kfree(adgl);
unlock_kernel();
mutex_unlock(&ac_mutex);
return 0;
}

15 changes: 8 additions & 7 deletions drivers/char/ds1302.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <linux/bcd.h>
#include <linux/smp_lock.h>
#include <linux/mutex.h>
#include <linux/uaccess.h>
#include <linux/io.h>

Expand All @@ -32,6 +32,7 @@

#define RTC_MAJOR_NR 121 /* local major, change later */

static DEFINE_MUTEX(rtc_mutex);
static const char ds1302_name[] = "ds1302";

/* Send 8 bits. */
Expand Down Expand Up @@ -164,9 +165,9 @@ static long rtc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
struct rtc_time rtc_tm;

memset(&rtc_tm, 0, sizeof (struct rtc_time));
lock_kernel();
mutex_lock(&rtc_mutex);
get_rtc_time(&rtc_tm);
unlock_kernel();
mutex_unlock(&rtc_mutex);
if (copy_to_user((struct rtc_time*)arg, &rtc_tm, sizeof(struct rtc_time)))
return -EFAULT;
return 0;
Expand Down Expand Up @@ -218,7 +219,7 @@ static long rtc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
mon = bin2bcd(mon);
yrs = bin2bcd(yrs);

lock_kernel();
mutex_lock(&rtc_mutex);
local_irq_save(flags);
CMOS_WRITE(yrs, RTC_YEAR);
CMOS_WRITE(mon, RTC_MONTH);
Expand All @@ -227,7 +228,7 @@ static long rtc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
CMOS_WRITE(min, RTC_MINUTES);
CMOS_WRITE(sec, RTC_SECONDS);
local_irq_restore(flags);
unlock_kernel();
mutex_unlock(&rtc_mutex);

/* Notice that at this point, the RTC is updated but
* the kernel is still running with the old time.
Expand All @@ -247,10 +248,10 @@ static long rtc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
if(copy_from_user(&tcs_val, (int*)arg, sizeof(int)))
return -EFAULT;

lock_kernel();
mutex_lock(&rtc_mutex);
tcs_val = RTC_TCR_PATTERN | (tcs_val & 0x0F);
ds1302_writereg(RTC_TRICKLECHARGER, tcs_val);
unlock_kernel();
mutex_unlock(&rtc_mutex);
return 0;
}
default:
Expand Down
8 changes: 4 additions & 4 deletions drivers/char/ds1620.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <linux/proc_fs.h>
#include <linux/capability.h>
#include <linux/init.h>
#include <linux/smp_lock.h>
#include <linux/mutex.h>

#include <mach/hardware.h>
#include <asm/mach-types.h>
Expand All @@ -34,6 +34,7 @@
#define CFG_CPU 2
#define CFG_1SHOT 1

static DEFINE_MUTEX(ds1620_mutex);
static const char *fan_state[] = { "off", "on", "on (hardwired)" };

/*
Expand Down Expand Up @@ -210,7 +211,6 @@ static void ds1620_read_state(struct therm *therm)

static int ds1620_open(struct inode *inode, struct file *file)
{
cycle_kernel_lock();
return nonseekable_open(inode, file);
}

Expand Down Expand Up @@ -321,9 +321,9 @@ ds1620_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int ret;

lock_kernel();
mutex_lock(&ds1620_mutex);
ret = ds1620_ioctl(file, cmd, arg);
unlock_kernel();
mutex_unlock(&ds1620_mutex);

return ret;
}
Expand Down
27 changes: 14 additions & 13 deletions drivers/char/dsp56k.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include <linux/mm.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/smp_lock.h>
#include <linux/mutex.h>
#include <linux/firmware.h>
#include <linux/platform_device.h>
#include <linux/uaccess.h> /* For put_user and get_user */
Expand Down Expand Up @@ -94,6 +94,7 @@
} \
}

static DEFINE_MUTEX(dsp56k_mutex);
static struct dsp56k_device {
unsigned long in_use;
long maxio, timeout;
Expand Down Expand Up @@ -330,9 +331,9 @@ static long dsp56k_ioctl(struct file *file, unsigned int cmd,
if (len > DSP56K_MAX_BINARY_LENGTH) {
return -EINVAL;
}
lock_kernel();
mutex_lock(&dsp56k_mutex);
r = dsp56k_upload(bin, len);
unlock_kernel();
mutex_unlock(&dsp56k_mutex);
if (r < 0) {
return r;
}
Expand All @@ -342,16 +343,16 @@ static long dsp56k_ioctl(struct file *file, unsigned int cmd,
case DSP56K_SET_TX_WSIZE:
if (arg > 4 || arg < 1)
return -EINVAL;
lock_kernel();
mutex_lock(&dsp56k_mutex);
dsp56k.tx_wsize = (int) arg;
unlock_kernel();
mutex_unlock(&dsp56k_mutex);
break;
case DSP56K_SET_RX_WSIZE:
if (arg > 4 || arg < 1)
return -EINVAL;
lock_kernel();
mutex_lock(&dsp56k_mutex);
dsp56k.rx_wsize = (int) arg;
unlock_kernel();
mutex_unlock(&dsp56k_mutex);
break;
case DSP56K_HOST_FLAGS:
{
Expand All @@ -363,7 +364,7 @@ static long dsp56k_ioctl(struct file *file, unsigned int cmd,
if(get_user(out, &hf->out) < 0)
return -EFAULT;

lock_kernel();
mutex_lock(&dsp56k_mutex);
if ((dir & 0x1) && (out & 0x1))
dsp56k_host_interface.icr |= DSP56K_ICR_HF0;
else if (dir & 0x1)
Expand All @@ -378,16 +379,16 @@ static long dsp56k_ioctl(struct file *file, unsigned int cmd,
if (dsp56k_host_interface.icr & DSP56K_ICR_HF1) status |= 0x2;
if (dsp56k_host_interface.isr & DSP56K_ISR_HF2) status |= 0x4;
if (dsp56k_host_interface.isr & DSP56K_ISR_HF3) status |= 0x8;
unlock_kernel();
mutex_unlock(&dsp56k_mutex);
return put_user(status, &hf->status);
}
case DSP56K_HOST_CMD:
if (arg > 31 || arg < 0)
return -EINVAL;
lock_kernel();
mutex_lock(&dsp56k_mutex);
dsp56k_host_interface.cvr = (u_char)((arg & DSP56K_CVR_HV_MASK) |
DSP56K_CVR_HC);
unlock_kernel();
mutex_unlock(&dsp56k_mutex);
break;
default:
return -EINVAL;
Expand Down Expand Up @@ -427,7 +428,7 @@ static int dsp56k_open(struct inode *inode, struct file *file)
int dev = iminor(inode) & 0x0f;
int ret = 0;

lock_kernel();
mutex_lock(&dsp56k_mutex);
switch(dev)
{
case DSP56K_DEV_56001:
Expand All @@ -454,7 +455,7 @@ static int dsp56k_open(struct inode *inode, struct file *file)
ret = -ENODEV;
}
out:
unlock_kernel();
mutex_unlock(&dsp56k_mutex);
return ret;
}

Expand Down
Loading

0 comments on commit 613655f

Please sign in to comment.