Commit db1dd4d3 authored by Jonathan Corbet's avatar Jonathan Corbet

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: default avatarChristoph Hellwig <hch@lst.de>
Cc: Al Viro <viro@ZenIV.linux.org.uk>
Signed-off-by: default avatarJonathan Corbet <corbet@lwn.net>
parent 68499914
...@@ -2162,13 +2162,12 @@ static int fionbio(struct file *file, int __user *p) ...@@ -2162,13 +2162,12 @@ static int fionbio(struct file *file, int __user *p)
if (get_user(nonblock, p)) if (get_user(nonblock, p))
return -EFAULT; return -EFAULT;
/* file->f_flags is still BKL protected in the fs layer - vomit */ spin_lock(&file->f_lock);
lock_kernel();
if (nonblock) if (nonblock)
file->f_flags |= O_NONBLOCK; file->f_flags |= O_NONBLOCK;
else else
file->f_flags &= ~O_NONBLOCK; file->f_flags &= ~O_NONBLOCK;
unlock_kernel(); spin_unlock(&file->f_lock);
return 0; return 0;
} }
......
...@@ -1711,7 +1711,9 @@ static int do_write(struct fsg_dev *fsg) ...@@ -1711,7 +1711,9 @@ static int do_write(struct fsg_dev *fsg)
curlun->sense_data = SS_WRITE_PROTECTED; curlun->sense_data = SS_WRITE_PROTECTED;
return -EINVAL; return -EINVAL;
} }
spin_lock(&curlun->filp->f_lock);
curlun->filp->f_flags &= ~O_SYNC; // Default is not to wait curlun->filp->f_flags &= ~O_SYNC; // Default is not to wait
spin_unlock(&curlun->filp->f_lock);
/* Get the starting Logical Block Address and check that it's /* Get the starting Logical Block Address and check that it's
* not too big */ * not too big */
...@@ -1728,8 +1730,11 @@ static int do_write(struct fsg_dev *fsg) ...@@ -1728,8 +1730,11 @@ static int do_write(struct fsg_dev *fsg)
curlun->sense_data = SS_INVALID_FIELD_IN_CDB; curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
return -EINVAL; return -EINVAL;
} }
if (fsg->cmnd[1] & 0x08) // FUA if (fsg->cmnd[1] & 0x08) { // FUA
spin_lock(&curlun->filp->f_lock);
curlun->filp->f_flags |= O_SYNC; curlun->filp->f_flags |= O_SYNC;
spin_unlock(&curlun->filp->f_lock);
}
} }
if (lba >= curlun->num_sectors) { if (lba >= curlun->num_sectors) {
curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
......
...@@ -189,7 +189,9 @@ static int setfl(int fd, struct file * filp, unsigned long arg) ...@@ -189,7 +189,9 @@ static int setfl(int fd, struct file * filp, unsigned long arg)
} }
} }
spin_lock(&filp->f_lock);
filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK); filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
spin_unlock(&filp->f_lock);
out: out:
unlock_kernel(); unlock_kernel();
return error; return error;
......
...@@ -404,10 +404,12 @@ static int ioctl_fionbio(struct file *filp, int __user *argp) ...@@ -404,10 +404,12 @@ static int ioctl_fionbio(struct file *filp, int __user *argp)
if (O_NONBLOCK != O_NDELAY) if (O_NONBLOCK != O_NDELAY)
flag |= O_NDELAY; flag |= O_NDELAY;
#endif #endif
spin_lock(&filp->f_lock);
if (on) if (on)
filp->f_flags |= flag; filp->f_flags |= flag;
else else
filp->f_flags &= ~flag; filp->f_flags &= ~flag;
spin_unlock(&filp->f_lock);
return error; return error;
} }
...@@ -432,10 +434,12 @@ static int ioctl_fioasync(unsigned int fd, struct file *filp, ...@@ -432,10 +434,12 @@ static int ioctl_fioasync(unsigned int fd, struct file *filp,
if (error) if (error)
return error; return error;
spin_lock(&filp->f_lock);
if (on) if (on)
filp->f_flags |= FASYNC; filp->f_flags |= FASYNC;
else else
filp->f_flags &= ~FASYNC; filp->f_flags &= ~FASYNC;
spin_unlock(&filp->f_lock);
return error; return error;
} }
...@@ -499,10 +503,7 @@ int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd, ...@@ -499,10 +503,7 @@ int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
break; break;
case FIONBIO: case FIONBIO:
/* BKL needed to avoid races tweaking f_flags */
lock_kernel();
error = ioctl_fionbio(filp, argp); error = ioctl_fionbio(filp, argp);
unlock_kernel();
break; break;
case FIOASYNC: case FIOASYNC:
......
...@@ -998,8 +998,11 @@ nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file, ...@@ -998,8 +998,11 @@ nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
if (!EX_ISSYNC(exp)) if (!EX_ISSYNC(exp))
stable = 0; stable = 0;
if (stable && !EX_WGATHER(exp)) if (stable && !EX_WGATHER(exp)) {
spin_lock(&file->f_lock);
file->f_flags |= O_SYNC; file->f_flags |= O_SYNC;
spin_unlock(&file->f_lock);
}
/* Write the data. */ /* Write the data. */
oldfs = get_fs(); set_fs(KERNEL_DS); oldfs = get_fs(); set_fs(KERNEL_DS);
......
...@@ -848,7 +848,7 @@ struct file { ...@@ -848,7 +848,7 @@ struct file {
#define f_dentry f_path.dentry #define f_dentry f_path.dentry
#define f_vfsmnt f_path.mnt #define f_vfsmnt f_path.mnt
const struct file_operations *f_op; const struct file_operations *f_op;
spinlock_t f_lock; /* f_ep_links */ spinlock_t f_lock; /* f_ep_links, f_flags */
atomic_long_t f_count; atomic_long_t f_count;
unsigned int f_flags; unsigned int f_flags;
fmode_t f_mode; fmode_t f_mode;
......
...@@ -1156,10 +1156,12 @@ SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes, ...@@ -1156,10 +1156,12 @@ SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,
omqstat.mq_flags = filp->f_flags & O_NONBLOCK; omqstat.mq_flags = filp->f_flags & O_NONBLOCK;
if (u_mqstat) { if (u_mqstat) {
audit_mq_getsetattr(mqdes, &mqstat); audit_mq_getsetattr(mqdes, &mqstat);
spin_lock(&filp->f_lock);
if (mqstat.mq_flags & O_NONBLOCK) if (mqstat.mq_flags & O_NONBLOCK)
filp->f_flags |= O_NONBLOCK; filp->f_flags |= O_NONBLOCK;
else else
filp->f_flags &= ~O_NONBLOCK; filp->f_flags &= ~O_NONBLOCK;
spin_unlock(&filp->f_lock);
inode->i_atime = inode->i_ctime = CURRENT_TIME; inode->i_atime = inode->i_ctime = CURRENT_TIME;
} }
......
...@@ -1895,7 +1895,9 @@ static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file *pcm_oss_file, unsig ...@@ -1895,7 +1895,9 @@ static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file *pcm_oss_file, unsig
static int snd_pcm_oss_nonblock(struct file * file) static int snd_pcm_oss_nonblock(struct file * file)
{ {
spin_lock(&file->f_lock);
file->f_flags |= O_NONBLOCK; file->f_flags |= O_NONBLOCK;
spin_unlock(&file->f_lock);
return 0; return 0;
} }
......
...@@ -1627,7 +1627,9 @@ au1550_ioctl(struct inode *inode, struct file *file, unsigned int cmd, ...@@ -1627,7 +1627,9 @@ au1550_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
sizeof(abinfo)) ? -EFAULT : 0; sizeof(abinfo)) ? -EFAULT : 0;
case SNDCTL_DSP_NONBLOCK: case SNDCTL_DSP_NONBLOCK:
spin_lock(&file->f_lock);
file->f_flags |= O_NONBLOCK; file->f_flags |= O_NONBLOCK;
spin_unlock(&file->f_lock);
return 0; return 0;
case SNDCTL_DSP_GETODELAY: case SNDCTL_DSP_GETODELAY:
......
...@@ -433,7 +433,9 @@ int audio_ioctl(int dev, struct file *file, unsigned int cmd, void __user *arg) ...@@ -433,7 +433,9 @@ int audio_ioctl(int dev, struct file *file, unsigned int cmd, void __user *arg)
return dma_ioctl(dev, cmd, arg); return dma_ioctl(dev, cmd, arg);
case SNDCTL_DSP_NONBLOCK: case SNDCTL_DSP_NONBLOCK:
spin_lock(&file->f_lock);
file->f_flags |= O_NONBLOCK; file->f_flags |= O_NONBLOCK;
spin_unlock(&file->f_lock);
return 0; return 0;
case SNDCTL_DSP_GETCAPS: case SNDCTL_DSP_GETCAPS:
......
...@@ -135,7 +135,9 @@ static int dac_audio_ioctl(struct inode *inode, struct file *file, ...@@ -135,7 +135,9 @@ static int dac_audio_ioctl(struct inode *inode, struct file *file,
return put_user(AFMT_U8, (int *)arg); return put_user(AFMT_U8, (int *)arg);
case SNDCTL_DSP_NONBLOCK: case SNDCTL_DSP_NONBLOCK:
spin_lock(&file->f_lock);
file->f_flags |= O_NONBLOCK; file->f_flags |= O_NONBLOCK;
spin_unlock(&file->f_lock);
return 0; return 0;
case SNDCTL_DSP_GETCAPS: case SNDCTL_DSP_GETCAPS:
......
...@@ -2200,7 +2200,9 @@ static int cs4297a_ioctl(struct inode *inode, struct file *file, ...@@ -2200,7 +2200,9 @@ static int cs4297a_ioctl(struct inode *inode, struct file *file,
sizeof(abinfo)) ? -EFAULT : 0; sizeof(abinfo)) ? -EFAULT : 0;
case SNDCTL_DSP_NONBLOCK: case SNDCTL_DSP_NONBLOCK:
spin_lock(&file->f_lock);
file->f_flags |= O_NONBLOCK; file->f_flags |= O_NONBLOCK;
spin_unlock(&file->f_lock);
return 0; return 0;
case SNDCTL_DSP_GETODELAY: case SNDCTL_DSP_GETODELAY:
......
...@@ -2673,7 +2673,9 @@ static int vwsnd_audio_do_ioctl(struct inode *inode, ...@@ -2673,7 +2673,9 @@ static int vwsnd_audio_do_ioctl(struct inode *inode,
case SNDCTL_DSP_NONBLOCK: /* _SIO ('P',14) */ case SNDCTL_DSP_NONBLOCK: /* _SIO ('P',14) */
DBGX("SNDCTL_DSP_NONBLOCK\n"); DBGX("SNDCTL_DSP_NONBLOCK\n");
spin_lock(&file->f_lock);
file->f_flags |= O_NONBLOCK; file->f_flags |= O_NONBLOCK;
spin_unlock(&file->f_lock);
return 0; return 0;
case SNDCTL_DSP_RESET: /* _SIO ('P', 0) */ case SNDCTL_DSP_RESET: /* _SIO ('P', 0) */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment