Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 613655fa authored by Arnd Bergmann's avatar Arnd Bergmann
Browse files

drivers: 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.

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: default avatarArnd Bergmann <arnd@arndb.de>
parent 609146fd
Loading
Loading
Loading
Loading
+4 −3
Original line number Original line Diff line number Diff line
@@ -162,7 +162,7 @@ enum {D_PRT, D_PRO, D_UNI, D_MOD, D_SLV, D_DLY};
#include <linux/pg.h>
#include <linux/pg.h>
#include <linux/device.h>
#include <linux/device.h>
#include <linux/sched.h>	/* current, TASK_* */
#include <linux/sched.h>	/* current, TASK_* */
#include <linux/smp_lock.h>
#include <linux/mutex.h>
#include <linux/jiffies.h>
#include <linux/jiffies.h>


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


#define ATAPI_IDENTIFY		0x12
#define ATAPI_IDENTIFY		0x12


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


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


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


+10 −9
Original line number Original line Diff line number Diff line
@@ -146,7 +146,7 @@ static int (*drives[4])[6] = {&drive0, &drive1, &drive2, &drive3};
#include <linux/mtio.h>
#include <linux/mtio.h>
#include <linux/device.h>
#include <linux/device.h>
#include <linux/sched.h>	/* current, TASK_*, schedule_timeout() */
#include <linux/sched.h>	/* current, TASK_*, schedule_timeout() */
#include <linux/smp_lock.h>
#include <linux/mutex.h>


#include <asm/uaccess.h>
#include <asm/uaccess.h>


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


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


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


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


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


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


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


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


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


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


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


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


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


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


	return as ? 0 : -ENOMEM;
	return as ? 0 : -ENOMEM;
}
}
+5 −4
Original line number Original line Diff line number Diff line
@@ -26,7 +26,7 @@
#include <linux/sched.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/errno.h>
#include <linux/smp_lock.h>
#include <linux/mutex.h>
#include <linux/miscdevice.h>
#include <linux/miscdevice.h>
#include <linux/pci.h>
#include <linux/pci.h>
#include <linux/wait.h>
#include <linux/wait.h>
@@ -60,6 +60,7 @@
#define PCI_DEVICE_ID_APPLICOM_PCI2000PFB     0x0003
#define PCI_DEVICE_ID_APPLICOM_PCI2000PFB     0x0003
#endif
#endif


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


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


@@ -835,7 +836,7 @@ static long ac_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
	}
	}
	Dummy = readb(apbs[IndexCard].RamIO + VERS);
	Dummy = readb(apbs[IndexCard].RamIO + VERS);
	kfree(adgl);
	kfree(adgl);
	unlock_kernel();
	mutex_unlock(&ac_mutex);
	return 0;
	return 0;
}
}
+8 −7
Original line number Original line Diff line number Diff line
@@ -20,7 +20,7 @@
#include <linux/miscdevice.h>
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <linux/delay.h>
#include <linux/bcd.h>
#include <linux/bcd.h>
#include <linux/smp_lock.h>
#include <linux/mutex.h>
#include <linux/uaccess.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/io.h>


@@ -32,6 +32,7 @@


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


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


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


			memset(&rtc_tm, 0, sizeof (struct rtc_time));
			memset(&rtc_tm, 0, sizeof (struct rtc_time));
			lock_kernel();
			mutex_lock(&rtc_mutex);
			get_rtc_time(&rtc_tm);
			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)))
			if (copy_to_user((struct rtc_time*)arg, &rtc_tm, sizeof(struct rtc_time)))
				return -EFAULT;
				return -EFAULT;
			return 0;
			return 0;
@@ -218,7 +219,7 @@ static long rtc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
			mon = bin2bcd(mon);
			mon = bin2bcd(mon);
			yrs = bin2bcd(yrs);
			yrs = bin2bcd(yrs);


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


			/* Notice that at this point, the RTC is updated but
			/* Notice that at this point, the RTC is updated but
			 * the kernel is still running with the old time.
			 * the kernel is still running with the old time.
@@ -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)))
			if(copy_from_user(&tcs_val, (int*)arg, sizeof(int)))
				return -EFAULT;
				return -EFAULT;


			lock_kernel();
			mutex_lock(&rtc_mutex);
			tcs_val = RTC_TCR_PATTERN | (tcs_val & 0x0F);
			tcs_val = RTC_TCR_PATTERN | (tcs_val & 0x0F);
			ds1302_writereg(RTC_TRICKLECHARGER, tcs_val);
			ds1302_writereg(RTC_TRICKLECHARGER, tcs_val);
			unlock_kernel();
			mutex_unlock(&rtc_mutex);
			return 0;
			return 0;
		}
		}
		default:
		default:
Loading