Skip to content

Commit 613655f

Browse files
committed
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: Arnd Bergmann <arnd@arndb.de>
1 parent 609146f commit 613655f

35 files changed

Lines changed: 216 additions & 190 deletions

‎drivers/block/paride/pg.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ enum {D_PRT, D_PRO, D_UNI, D_MOD, D_SLV, D_DLY};
162162
#include <linux/pg.h>
163163
#include <linux/device.h>
164164
#include <linux/sched.h> /* current, TASK_* */
165-
#include <linux/smp_lock.h>
165+
#include <linux/mutex.h>
166166
#include <linux/jiffies.h>
167167

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

194194
#define ATAPI_IDENTIFY 0x12
195195

196+
static DEFINE_MUTEX(pg_mutex);
196197
static int pg_open(struct inode *inode, struct file *file);
197198
static int pg_release(struct inode *inode, struct file *file);
198199
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)
518519
struct pg *dev = &devices[unit];
519520
int ret = 0;
520521

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

549550
out:
550-
unlock_kernel();
551+
mutex_unlock(&pg_mutex);
551552
return ret;
552553
}
553554

‎drivers/block/paride/pt.c‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ static int (*drives[4])[6] = {&drive0, &drive1, &drive2, &drive3};
146146
#include <linux/mtio.h>
147147
#include <linux/device.h>
148148
#include <linux/sched.h> /* current, TASK_*, schedule_timeout() */
149-
#include <linux/smp_lock.h>
149+
#include <linux/mutex.h>
150150

151151
#include <asm/uaccess.h>
152152

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

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

653-
lock_kernel();
654+
mutex_lock(&pt_mutex);
654655
if (unit >= PT_UNITS || (!tape->present)) {
655-
unlock_kernel();
656+
mutex_unlock(&pt_mutex);
656657
return -ENODEV;
657658
}
658659

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

683684
file->private_data = tape;
684-
unlock_kernel();
685+
mutex_unlock(&pt_mutex);
685686
return 0;
686687

687688
out:
688689
atomic_inc(&tape->available);
689-
unlock_kernel();
690+
mutex_unlock(&pt_mutex);
690691
return err;
691692
}
692693

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

706707
case MTREW:
707-
lock_kernel();
708+
mutex_lock(&pt_mutex);
708709
pt_rewind(tape);
709-
unlock_kernel();
710+
mutex_unlock(&pt_mutex);
710711
return 0;
711712

712713
case MTWEOF:
713-
lock_kernel();
714+
mutex_lock(&pt_mutex);
714715
pt_write_fm(tape);
715-
unlock_kernel();
716+
mutex_unlock(&pt_mutex);
716717
return 0;
717718

718719
default:

‎drivers/char/apm-emulation.c‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <linux/module.h>
1414
#include <linux/poll.h>
1515
#include <linux/slab.h>
16-
#include <linux/smp_lock.h>
16+
#include <linux/mutex.h>
1717
#include <linux/proc_fs.h>
1818
#include <linux/seq_file.h>
1919
#include <linux/miscdevice.h>
@@ -126,6 +126,7 @@ struct apm_user {
126126
/*
127127
* Local variables
128128
*/
129+
static DEFINE_MUTEX(apm_mutex);
129130
static atomic_t suspend_acks_pending = ATOMIC_INIT(0);
130131
static atomic_t userspace_notification_inhibit = ATOMIC_INIT(0);
131132
static int apm_disabled;
@@ -274,7 +275,7 @@ apm_ioctl(struct file *filp, u_int cmd, u_long arg)
274275
if (!as->suser || !as->writer)
275276
return -EPERM;
276277

277-
lock_kernel();
278+
mutex_lock(&apm_mutex);
278279
switch (cmd) {
279280
case APM_IOC_SUSPEND:
280281
mutex_lock(&state_lock);
@@ -335,7 +336,7 @@ apm_ioctl(struct file *filp, u_int cmd, u_long arg)
335336
mutex_unlock(&state_lock);
336337
break;
337338
}
338-
unlock_kernel();
339+
mutex_unlock(&apm_mutex);
339340

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

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

391392
filp->private_data = as;
392393
}
393-
unlock_kernel();
394+
mutex_unlock(&apm_mutex);
394395

395396
return as ? 0 : -ENOMEM;
396397
}

‎drivers/char/applicom.c‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include <linux/sched.h>
2727
#include <linux/slab.h>
2828
#include <linux/errno.h>
29-
#include <linux/smp_lock.h>
29+
#include <linux/mutex.h>
3030
#include <linux/miscdevice.h>
3131
#include <linux/pci.h>
3232
#include <linux/wait.h>
@@ -60,6 +60,7 @@
6060
#define PCI_DEVICE_ID_APPLICOM_PCI2000PFB 0x0003
6161
#endif
6262

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

710-
lock_kernel();
711+
mutex_lock(&ac_mutex);
711712
IndexCard = adgl->num_card-1;
712713

713714
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)
717718
warncount--;
718719
}
719720
kfree(adgl);
720-
unlock_kernel();
721+
mutex_unlock(&ac_mutex);
721722
return -EINVAL;
722723
}
723724

@@ -835,7 +836,7 @@ static long ac_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
835836
}
836837
Dummy = readb(apbs[IndexCard].RamIO + VERS);
837838
kfree(adgl);
838-
unlock_kernel();
839+
mutex_unlock(&ac_mutex);
839840
return 0;
840841
}
841842

‎drivers/char/ds1302.c‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include <linux/miscdevice.h>
2121
#include <linux/delay.h>
2222
#include <linux/bcd.h>
23-
#include <linux/smp_lock.h>
23+
#include <linux/mutex.h>
2424
#include <linux/uaccess.h>
2525
#include <linux/io.h>
2626

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

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

35+
static DEFINE_MUTEX(rtc_mutex);
3536
static const char ds1302_name[] = "ds1302";
3637

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

166167
memset(&rtc_tm, 0, sizeof (struct rtc_time));
167-
lock_kernel();
168+
mutex_lock(&rtc_mutex);
168169
get_rtc_time(&rtc_tm);
169-
unlock_kernel();
170+
mutex_unlock(&rtc_mutex);
170171
if (copy_to_user((struct rtc_time*)arg, &rtc_tm, sizeof(struct rtc_time)))
171172
return -EFAULT;
172173
return 0;
@@ -218,7 +219,7 @@ static long rtc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
218219
mon = bin2bcd(mon);
219220
yrs = bin2bcd(yrs);
220221

221-
lock_kernel();
222+
mutex_lock(&rtc_mutex);
222223
local_irq_save(flags);
223224
CMOS_WRITE(yrs, RTC_YEAR);
224225
CMOS_WRITE(mon, RTC_MONTH);
@@ -227,7 +228,7 @@ static long rtc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
227228
CMOS_WRITE(min, RTC_MINUTES);
228229
CMOS_WRITE(sec, RTC_SECONDS);
229230
local_irq_restore(flags);
230-
unlock_kernel();
231+
mutex_unlock(&rtc_mutex);
231232

232233
/* Notice that at this point, the RTC is updated but
233234
* 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)
247248
if(copy_from_user(&tcs_val, (int*)arg, sizeof(int)))
248249
return -EFAULT;
249250

250-
lock_kernel();
251+
mutex_lock(&rtc_mutex);
251252
tcs_val = RTC_TCR_PATTERN | (tcs_val & 0x0F);
252253
ds1302_writereg(RTC_TRICKLECHARGER, tcs_val);
253-
unlock_kernel();
254+
mutex_unlock(&rtc_mutex);
254255
return 0;
255256
}
256257
default:

‎drivers/char/ds1620.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <linux/proc_fs.h>
99
#include <linux/capability.h>
1010
#include <linux/init.h>
11-
#include <linux/smp_lock.h>
11+
#include <linux/mutex.h>
1212

1313
#include <mach/hardware.h>
1414
#include <asm/mach-types.h>
@@ -34,6 +34,7 @@
3434
#define CFG_CPU 2
3535
#define CFG_1SHOT 1
3636

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

3940
/*
@@ -210,7 +211,6 @@ static void ds1620_read_state(struct therm *therm)
210211

211212
static int ds1620_open(struct inode *inode, struct file *file)
212213
{
213-
cycle_kernel_lock();
214214
return nonseekable_open(inode, file);
215215
}
216216

@@ -321,9 +321,9 @@ ds1620_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
321321
{
322322
int ret;
323323

324-
lock_kernel();
324+
mutex_lock(&ds1620_mutex);
325325
ret = ds1620_ioctl(file, cmd, arg);
326-
unlock_kernel();
326+
mutex_unlock(&ds1620_mutex);
327327

328328
return ret;
329329
}

‎drivers/char/dsp56k.c‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include <linux/mm.h>
3333
#include <linux/init.h>
3434
#include <linux/device.h>
35-
#include <linux/smp_lock.h>
35+
#include <linux/mutex.h>
3636
#include <linux/firmware.h>
3737
#include <linux/platform_device.h>
3838
#include <linux/uaccess.h> /* For put_user and get_user */
@@ -94,6 +94,7 @@
9494
} \
9595
}
9696

97+
static DEFINE_MUTEX(dsp56k_mutex);
9798
static struct dsp56k_device {
9899
unsigned long in_use;
99100
long maxio, timeout;
@@ -330,9 +331,9 @@ static long dsp56k_ioctl(struct file *file, unsigned int cmd,
330331
if (len > DSP56K_MAX_BINARY_LENGTH) {
331332
return -EINVAL;
332333
}
333-
lock_kernel();
334+
mutex_lock(&dsp56k_mutex);
334335
r = dsp56k_upload(bin, len);
335-
unlock_kernel();
336+
mutex_unlock(&dsp56k_mutex);
336337
if (r < 0) {
337338
return r;
338339
}
@@ -342,16 +343,16 @@ static long dsp56k_ioctl(struct file *file, unsigned int cmd,
342343
case DSP56K_SET_TX_WSIZE:
343344
if (arg > 4 || arg < 1)
344345
return -EINVAL;
345-
lock_kernel();
346+
mutex_lock(&dsp56k_mutex);
346347
dsp56k.tx_wsize = (int) arg;
347-
unlock_kernel();
348+
mutex_unlock(&dsp56k_mutex);
348349
break;
349350
case DSP56K_SET_RX_WSIZE:
350351
if (arg > 4 || arg < 1)
351352
return -EINVAL;
352-
lock_kernel();
353+
mutex_lock(&dsp56k_mutex);
353354
dsp56k.rx_wsize = (int) arg;
354-
unlock_kernel();
355+
mutex_unlock(&dsp56k_mutex);
355356
break;
356357
case DSP56K_HOST_FLAGS:
357358
{
@@ -363,7 +364,7 @@ static long dsp56k_ioctl(struct file *file, unsigned int cmd,
363364
if(get_user(out, &hf->out) < 0)
364365
return -EFAULT;
365366

366-
lock_kernel();
367+
mutex_lock(&dsp56k_mutex);
367368
if ((dir & 0x1) && (out & 0x1))
368369
dsp56k_host_interface.icr |= DSP56K_ICR_HF0;
369370
else if (dir & 0x1)
@@ -378,16 +379,16 @@ static long dsp56k_ioctl(struct file *file, unsigned int cmd,
378379
if (dsp56k_host_interface.icr & DSP56K_ICR_HF1) status |= 0x2;
379380
if (dsp56k_host_interface.isr & DSP56K_ISR_HF2) status |= 0x4;
380381
if (dsp56k_host_interface.isr & DSP56K_ISR_HF3) status |= 0x8;
381-
unlock_kernel();
382+
mutex_unlock(&dsp56k_mutex);
382383
return put_user(status, &hf->status);
383384
}
384385
case DSP56K_HOST_CMD:
385386
if (arg > 31 || arg < 0)
386387
return -EINVAL;
387-
lock_kernel();
388+
mutex_lock(&dsp56k_mutex);
388389
dsp56k_host_interface.cvr = (u_char)((arg & DSP56K_CVR_HV_MASK) |
389390
DSP56K_CVR_HC);
390-
unlock_kernel();
391+
mutex_unlock(&dsp56k_mutex);
391392
break;
392393
default:
393394
return -EINVAL;
@@ -427,7 +428,7 @@ static int dsp56k_open(struct inode *inode, struct file *file)
427428
int dev = iminor(inode) & 0x0f;
428429
int ret = 0;
429430

430-
lock_kernel();
431+
mutex_lock(&dsp56k_mutex);
431432
switch(dev)
432433
{
433434
case DSP56K_DEV_56001:
@@ -454,7 +455,7 @@ static int dsp56k_open(struct inode *inode, struct file *file)
454455
ret = -ENODEV;
455456
}
456457
out:
457-
unlock_kernel();
458+
mutex_unlock(&dsp56k_mutex);
458459
return ret;
459460
}
460461

0 commit comments

Comments
 (0)