Skip to content

Commit db1dd4d

Browse files
author
Jonathan Corbet
committed
Use f_lock to protect f_flags
Traditionally, changes to struct file->f_flags have been done under BKL protection, or with no protection at all. This patch causes all f_flags changes after file open/creation time to be done under protection of f_lock. This allows the removal of some BKL usage and fixes a number of longstanding (if microscopic) races. Reviewed-by: Christoph Hellwig <hch@lst.de> Cc: Al Viro <viro@ZenIV.linux.org.uk> Signed-off-by: Jonathan Corbet <corbet@lwn.net>
1 parent 6849991 commit db1dd4d

13 files changed

Lines changed: 33 additions & 9 deletions

File tree

‎drivers/char/tty_io.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,13 +2162,12 @@ static int fionbio(struct file *file, int __user *p)
21622162
if (get_user(nonblock, p))
21632163
return -EFAULT;
21642164

2165-
/* file->f_flags is still BKL protected in the fs layer - vomit */
2166-
lock_kernel();
2165+
spin_lock(&file->f_lock);
21672166
if (nonblock)
21682167
file->f_flags |= O_NONBLOCK;
21692168
else
21702169
file->f_flags &= ~O_NONBLOCK;
2171-
unlock_kernel();
2170+
spin_unlock(&file->f_lock);
21722171
return 0;
21732172
}
21742173

‎drivers/usb/gadget/file_storage.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1711,7 +1711,9 @@ static int do_write(struct fsg_dev *fsg)
17111711
curlun->sense_data = SS_WRITE_PROTECTED;
17121712
return -EINVAL;
17131713
}
1714+
spin_lock(&curlun->filp->f_lock);
17141715
curlun->filp->f_flags &= ~O_SYNC; // Default is not to wait
1716+
spin_unlock(&curlun->filp->f_lock);
17151717

17161718
/* Get the starting Logical Block Address and check that it's
17171719
* not too big */
@@ -1728,8 +1730,11 @@ static int do_write(struct fsg_dev *fsg)
17281730
curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
17291731
return -EINVAL;
17301732
}
1731-
if (fsg->cmnd[1] & 0x08) // FUA
1733+
if (fsg->cmnd[1] & 0x08) { // FUA
1734+
spin_lock(&curlun->filp->f_lock);
17321735
curlun->filp->f_flags |= O_SYNC;
1736+
spin_unlock(&curlun->filp->f_lock);
1737+
}
17331738
}
17341739
if (lba >= curlun->num_sectors) {
17351740
curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;

‎fs/fcntl.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,9 @@ static int setfl(int fd, struct file * filp, unsigned long arg)
189189
}
190190
}
191191

192+
spin_lock(&filp->f_lock);
192193
filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
194+
spin_unlock(&filp->f_lock);
193195
out:
194196
unlock_kernel();
195197
return error;

‎fs/ioctl.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,10 +404,12 @@ static int ioctl_fionbio(struct file *filp, int __user *argp)
404404
if (O_NONBLOCK != O_NDELAY)
405405
flag |= O_NDELAY;
406406
#endif
407+
spin_lock(&filp->f_lock);
407408
if (on)
408409
filp->f_flags |= flag;
409410
else
410411
filp->f_flags &= ~flag;
412+
spin_unlock(&filp->f_lock);
411413
return error;
412414
}
413415

@@ -432,10 +434,12 @@ static int ioctl_fioasync(unsigned int fd, struct file *filp,
432434
if (error)
433435
return error;
434436

437+
spin_lock(&filp->f_lock);
435438
if (on)
436439
filp->f_flags |= FASYNC;
437440
else
438441
filp->f_flags &= ~FASYNC;
442+
spin_unlock(&filp->f_lock);
439443
return error;
440444
}
441445

@@ -499,10 +503,7 @@ int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
499503
break;
500504

501505
case FIONBIO:
502-
/* BKL needed to avoid races tweaking f_flags */
503-
lock_kernel();
504506
error = ioctl_fionbio(filp, argp);
505-
unlock_kernel();
506507
break;
507508

508509
case FIOASYNC:

‎fs/nfsd/vfs.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -998,8 +998,11 @@ nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
998998

999999
if (!EX_ISSYNC(exp))
10001000
stable = 0;
1001-
if (stable && !EX_WGATHER(exp))
1001+
if (stable && !EX_WGATHER(exp)) {
1002+
spin_lock(&file->f_lock);
10021003
file->f_flags |= O_SYNC;
1004+
spin_unlock(&file->f_lock);
1005+
}
10031006

10041007
/* Write the data. */
10051008
oldfs = get_fs(); set_fs(KERNEL_DS);

‎include/linux/fs.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ struct file {
848848
#define f_dentry f_path.dentry
849849
#define f_vfsmnt f_path.mnt
850850
const struct file_operations *f_op;
851-
spinlock_t f_lock; /* f_ep_links */
851+
spinlock_t f_lock; /* f_ep_links, f_flags */
852852
atomic_long_t f_count;
853853
unsigned int f_flags;
854854
fmode_t f_mode;

‎ipc/mqueue.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,10 +1156,12 @@ SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,
11561156
omqstat.mq_flags = filp->f_flags & O_NONBLOCK;
11571157
if (u_mqstat) {
11581158
audit_mq_getsetattr(mqdes, &mqstat);
1159+
spin_lock(&filp->f_lock);
11591160
if (mqstat.mq_flags & O_NONBLOCK)
11601161
filp->f_flags |= O_NONBLOCK;
11611162
else
11621163
filp->f_flags &= ~O_NONBLOCK;
1164+
spin_unlock(&filp->f_lock);
11631165

11641166
inode->i_atime = inode->i_ctime = CURRENT_TIME;
11651167
}

‎sound/core/oss/pcm_oss.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,7 +1895,9 @@ static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file *pcm_oss_file, unsig
18951895

18961896
static int snd_pcm_oss_nonblock(struct file * file)
18971897
{
1898+
spin_lock(&file->f_lock);
18981899
file->f_flags |= O_NONBLOCK;
1900+
spin_unlock(&file->f_lock);
18991901
return 0;
19001902
}
19011903

‎sound/oss/au1550_ac97.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,7 +1627,9 @@ au1550_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
16271627
sizeof(abinfo)) ? -EFAULT : 0;
16281628

16291629
case SNDCTL_DSP_NONBLOCK:
1630+
spin_lock(&file->f_lock);
16301631
file->f_flags |= O_NONBLOCK;
1632+
spin_unlock(&file->f_lock);
16311633
return 0;
16321634

16331635
case SNDCTL_DSP_GETODELAY:

‎sound/oss/audio.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,9 @@ int audio_ioctl(int dev, struct file *file, unsigned int cmd, void __user *arg)
433433
return dma_ioctl(dev, cmd, arg);
434434

435435
case SNDCTL_DSP_NONBLOCK:
436+
spin_lock(&file->f_lock);
436437
file->f_flags |= O_NONBLOCK;
438+
spin_unlock(&file->f_lock);
437439
return 0;
438440

439441
case SNDCTL_DSP_GETCAPS:

0 commit comments

Comments
 (0)