ipmi: 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>
Acked-by: Corey Minyard <cminyard@mvista.com>
Cc: openipmi-developer@lists.sourceforge.net

+11 -11
+7 -7
drivers/char/ipmi/ipmi_devintf.c
··· 44 44 #include <linux/init.h> 45 45 #include <linux/device.h> 46 46 #include <linux/compat.h> 47 - #include <linux/smp_lock.h> 48 47 49 48 struct ipmi_file_private 50 49 { ··· 58 59 unsigned int default_retry_time_ms; 59 60 }; 60 61 62 + static DEFINE_MUTEX(ipmi_mutex); 61 63 static void file_receive_handler(struct ipmi_recv_msg *msg, 62 64 void *handler_data) 63 65 { ··· 102 102 struct ipmi_file_private *priv = file->private_data; 103 103 int result; 104 104 105 - lock_kernel(); /* could race against open() otherwise */ 105 + mutex_lock(&ipmi_mutex); /* could race against open() otherwise */ 106 106 result = fasync_helper(fd, file, on, &priv->fasync_queue); 107 - unlock_kernel(); 107 + mutex_unlock(&ipmi_mutex); 108 108 109 109 return (result); 110 110 } ··· 125 125 if (!priv) 126 126 return -ENOMEM; 127 127 128 - lock_kernel(); 128 + mutex_lock(&ipmi_mutex); 129 129 priv->file = file; 130 130 131 131 rv = ipmi_create_user(if_num, ··· 150 150 priv->default_retry_time_ms = 0; 151 151 152 152 out: 153 - unlock_kernel(); 153 + mutex_unlock(&ipmi_mutex); 154 154 return rv; 155 155 } 156 156 ··· 639 639 { 640 640 int ret; 641 641 642 - lock_kernel(); 642 + mutex_lock(&ipmi_mutex); 643 643 ret = ipmi_ioctl(file, cmd, data); 644 - unlock_kernel(); 644 + mutex_unlock(&ipmi_mutex); 645 645 646 646 return ret; 647 647 }
+4 -4
drivers/char/ipmi/ipmi_watchdog.c
··· 35 35 #include <linux/moduleparam.h> 36 36 #include <linux/ipmi.h> 37 37 #include <linux/ipmi_smi.h> 38 - #include <linux/smp_lock.h> 38 + #include <linux/mutex.h> 39 39 #include <linux/watchdog.h> 40 40 #include <linux/miscdevice.h> 41 41 #include <linux/init.h> ··· 149 149 #define WDIOC_GET_PRETIMEOUT _IOW(WATCHDOG_IOCTL_BASE, 22, int) 150 150 #endif 151 151 152 + static DEFINE_MUTEX(ipmi_watchdog_mutex); 152 153 static int nowayout = WATCHDOG_NOWAYOUT; 153 154 154 155 static ipmi_user_t watchdog_user; ··· 749 748 { 750 749 int ret; 751 750 752 - lock_kernel(); 751 + mutex_lock(&ipmi_watchdog_mutex); 753 752 ret = ipmi_ioctl(file, cmd, arg); 754 - unlock_kernel(); 753 + mutex_unlock(&ipmi_watchdog_mutex); 755 754 756 755 return ret; 757 756 } ··· 845 844 if (test_and_set_bit(0, &ipmi_wdog_open)) 846 845 return -EBUSY; 847 846 848 - cycle_kernel_lock(); 849 847 850 848 /* 851 849 * Don't start the timer now, let it start on the