mtd: 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>
Cc: David Woodhouse <David.Woodhouse@intel.com>
Cc: linux-mtd@lists.infradead.org

+8 -7
+8 -7
drivers/mtd/mtdchar.c
··· 26 26 #include <linux/module.h> 27 27 #include <linux/slab.h> 28 28 #include <linux/sched.h> 29 - #include <linux/smp_lock.h> 29 + #include <linux/mutex.h> 30 30 #include <linux/backing-dev.h> 31 31 #include <linux/compat.h> 32 32 #include <linux/mount.h> ··· 37 37 #include <asm/uaccess.h> 38 38 39 39 #define MTD_INODE_FS_MAGIC 0x11307854 40 + static DEFINE_MUTEX(mtd_mutex); 40 41 static struct vfsmount *mtd_inode_mnt __read_mostly; 41 42 42 43 /* ··· 91 90 if ((file->f_mode & FMODE_WRITE) && (minor & 1)) 92 91 return -EACCES; 93 92 94 - lock_kernel(); 93 + mutex_lock(&mtd_mutex); 95 94 mtd = get_mtd_device(NULL, devnum); 96 95 97 96 if (IS_ERR(mtd)) { ··· 139 138 file->private_data = mfi; 140 139 141 140 out: 142 - unlock_kernel(); 141 + mutex_unlock(&mtd_mutex); 143 142 return ret; 144 143 } /* mtd_open */ 145 144 ··· 867 866 { 868 867 int ret; 869 868 870 - lock_kernel(); 869 + mutex_lock(&mtd_mutex); 871 870 ret = mtd_ioctl(file, cmd, arg); 872 - unlock_kernel(); 871 + mutex_unlock(&mtd_mutex); 873 872 874 873 return ret; 875 874 } ··· 893 892 void __user *argp = compat_ptr(arg); 894 893 int ret = 0; 895 894 896 - lock_kernel(); 895 + mutex_lock(&mtd_mutex); 897 896 898 897 switch (cmd) { 899 898 case MEMWRITEOOB32: ··· 928 927 ret = mtd_ioctl(file, cmd, (unsigned long)argp); 929 928 } 930 929 931 - unlock_kernel(); 930 + mutex_unlock(&mtd_mutex); 932 931 933 932 return ret; 934 933 }