Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

sbus: 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: David S. Miller <davem@davemloft.net>

authored by

Arnd Bergmann and committed by
David S. Miller
a3108ca2 f8324e20

+24 -23
+4 -4
drivers/sbus/char/display7seg.c
··· 13 13 #include <linux/miscdevice.h> 14 14 #include <linux/ioport.h> /* request_region */ 15 15 #include <linux/slab.h> 16 - #include <linux/smp_lock.h> 16 + #include <linux/mutex.h> 17 17 #include <linux/of.h> 18 18 #include <linux/of_device.h> 19 19 #include <asm/atomic.h> ··· 26 26 #define DRIVER_NAME "d7s" 27 27 #define PFX DRIVER_NAME ": " 28 28 29 + static DEFINE_MUTEX(d7s_mutex); 29 30 static int sol_compat = 0; /* Solaris compatibility mode */ 30 31 31 32 /* Solaris compatibility flag - ··· 75 74 { 76 75 if (D7S_MINOR != iminor(inode)) 77 76 return -ENODEV; 78 - cycle_kernel_lock(); 79 77 atomic_inc(&d7s_users); 80 78 return 0; 81 79 } ··· 110 110 if (D7S_MINOR != iminor(file->f_path.dentry->d_inode)) 111 111 return -ENODEV; 112 112 113 - lock_kernel(); 113 + mutex_lock(&d7s_mutex); 114 114 switch (cmd) { 115 115 case D7SIOCWR: 116 116 /* assign device register values we mask-out D7S_FLIP ··· 151 151 writeb(regs, p->regs); 152 152 break; 153 153 }; 154 - unlock_kernel(); 154 + mutex_unlock(&d7s_mutex); 155 155 156 156 return error; 157 157 }
-2
drivers/sbus/char/envctrl.c
··· 27 27 #include <linux/kmod.h> 28 28 #include <linux/reboot.h> 29 29 #include <linux/slab.h> 30 - #include <linux/smp_lock.h> 31 30 #include <linux/of.h> 32 31 #include <linux/of_device.h> 33 32 ··· 698 699 static int 699 700 envctrl_open(struct inode *inode, struct file *file) 700 701 { 701 - cycle_kernel_lock(); 702 702 file->private_data = NULL; 703 703 return 0; 704 704 }
+8 -7
drivers/sbus/char/flash.c
··· 10 10 #include <linux/fcntl.h> 11 11 #include <linux/poll.h> 12 12 #include <linux/init.h> 13 - #include <linux/smp_lock.h> 13 + #include <linux/mutex.h> 14 14 #include <linux/spinlock.h> 15 15 #include <linux/mm.h> 16 16 #include <linux/of.h> ··· 22 22 #include <asm/io.h> 23 23 #include <asm/upa.h> 24 24 25 + static DEFINE_MUTEX(flash_mutex); 25 26 static DEFINE_SPINLOCK(flash_lock); 26 27 static struct { 27 28 unsigned long read_base; /* Physical read address */ ··· 81 80 static long long 82 81 flash_llseek(struct file *file, long long offset, int origin) 83 82 { 84 - lock_kernel(); 83 + mutex_lock(&flash_mutex); 85 84 switch (origin) { 86 85 case 0: 87 86 file->f_pos = offset; ··· 95 94 file->f_pos = flash.read_size; 96 95 break; 97 96 default: 98 - unlock_kernel(); 97 + mutex_unlock(&flash_mutex); 99 98 return -EINVAL; 100 99 } 101 - unlock_kernel(); 100 + mutex_unlock(&flash_mutex); 102 101 return file->f_pos; 103 102 } 104 103 ··· 126 125 static int 127 126 flash_open(struct inode *inode, struct file *file) 128 127 { 129 - lock_kernel(); 128 + mutex_lock(&flash_mutex); 130 129 if (test_and_set_bit(0, (void *)&flash.busy) != 0) { 131 - unlock_kernel(); 130 + mutex_unlock(&flash_mutex); 132 131 return -EBUSY; 133 132 } 134 133 135 - unlock_kernel(); 134 + mutex_unlock(&flash_mutex); 136 135 return 0; 137 136 } 138 137
+8 -7
drivers/sbus/char/openprom.c
··· 33 33 #include <linux/kernel.h> 34 34 #include <linux/errno.h> 35 35 #include <linux/slab.h> 36 - #include <linux/smp_lock.h> 36 + #include <linux/mutex.h> 37 37 #include <linux/string.h> 38 38 #include <linux/miscdevice.h> 39 39 #include <linux/init.h> ··· 61 61 } DATA; 62 62 63 63 /* ID of the PROM node containing all of the EEPROM options. */ 64 + static DEFINE_MUTEX(openprom_mutex); 64 65 static struct device_node *options_node; 65 66 66 67 /* ··· 317 316 if (bufsize < 0) 318 317 return bufsize; 319 318 320 - lock_kernel(); 319 + mutex_lock(&openprom_mutex); 321 320 322 321 switch (cmd) { 323 322 case OPROMGETOPT: ··· 368 367 } 369 368 370 369 kfree(opp); 371 - unlock_kernel(); 370 + mutex_unlock(&openprom_mutex); 372 371 373 372 return error; 374 373 } ··· 559 558 void __user *argp = (void __user *)arg; 560 559 int err; 561 560 562 - lock_kernel(); 561 + mutex_lock(&openprom_mutex); 563 562 switch (cmd) { 564 563 case OPIOCGET: 565 564 err = opiocget(argp, data); ··· 590 589 err = -EINVAL; 591 590 break; 592 591 }; 593 - unlock_kernel(); 592 + mutex_unlock(&openprom_mutex); 594 593 595 594 return err; 596 595 } ··· 698 697 if (!data) 699 698 return -ENOMEM; 700 699 701 - lock_kernel(); 700 + mutex_lock(&openprom_mutex); 702 701 data->current_node = of_find_node_by_path("/"); 703 702 data->lastnode = data->current_node; 704 703 file->private_data = (void *) data; 705 - unlock_kernel(); 704 + mutex_unlock(&openprom_mutex); 706 705 707 706 return 0; 708 707 }
+4 -3
drivers/sbus/char/uctrl.c
··· 9 9 #include <linux/delay.h> 10 10 #include <linux/interrupt.h> 11 11 #include <linux/slab.h> 12 - #include <linux/smp_lock.h> 12 + #include <linux/mutex.h> 13 13 #include <linux/ioport.h> 14 14 #include <linux/init.h> 15 15 #include <linux/miscdevice.h> ··· 72 72 #define UCTRL_STAT_RXNE_STA 0x04 /* receive FIFO not empty status */ 73 73 #define UCTRL_STAT_RXO_STA 0x08 /* receive FIFO overflow status */ 74 74 75 + static DEFINE_MUTEX(uctrl_mutex); 75 76 static const char *uctrl_extstatus[16] = { 76 77 "main power available", 77 78 "internal battery attached", ··· 211 210 static int 212 211 uctrl_open(struct inode *inode, struct file *file) 213 212 { 214 - lock_kernel(); 213 + mutex_lock(&uctrl_mutex); 215 214 uctrl_get_event_status(global_driver); 216 215 uctrl_get_external_status(global_driver); 217 - unlock_kernel(); 216 + mutex_unlock(&uctrl_mutex); 218 217 return 0; 219 218 } 220 219