Use f_lock to protect f_flags
authorJonathan Corbet <corbet@lwn.net>
Fri, 6 Feb 2009 22:25:24 +0000 (15:25 -0700)
committerJonathan Corbet <corbet@lwn.net>
Mon, 16 Mar 2009 14:32:27 +0000 (08:32 -0600)
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>
13 files changed:
drivers/char/tty_io.c
drivers/usb/gadget/file_storage.c
fs/fcntl.c
fs/ioctl.c
fs/nfsd/vfs.c
include/linux/fs.h
ipc/mqueue.c
sound/core/oss/pcm_oss.c
sound/oss/au1550_ac97.c
sound/oss/audio.c
sound/oss/sh_dac_audio.c
sound/oss/swarm_cs4297a.c
sound/oss/vwsnd.c

index bc84e125c6bc428ca1d51cbcb29909466ea96b87..224f271d8cbe06868d69ce415339efa30f110d55 100644 (file)
@@ -2162,13 +2162,12 @@ static int fionbio(struct file *file, int __user *p)
        if (get_user(nonblock, p))
                return -EFAULT;
 
-       /* file->f_flags is still BKL protected in the fs layer - vomit */
-       lock_kernel();
+       spin_lock(&file->f_lock);
        if (nonblock)
                file->f_flags |= O_NONBLOCK;
        else
                file->f_flags &= ~O_NONBLOCK;
-       unlock_kernel();
+       spin_unlock(&file->f_lock);
        return 0;
 }
 
index 1ab9dac7e12df8eec07e5ed278e48ca90cdd3bc5..33bb76cef33c2ad78aa2c7f3b21e4d3d8a04f4c0 100644 (file)
@@ -1711,7 +1711,9 @@ static int do_write(struct fsg_dev *fsg)
                curlun->sense_data = SS_WRITE_PROTECTED;
                return -EINVAL;
        }
+       spin_lock(&curlun->filp->f_lock);
        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
         * not too big */
@@ -1728,8 +1730,11 @@ static int do_write(struct fsg_dev *fsg)
                        curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
                        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;
+                       spin_unlock(&curlun->filp->f_lock);
+               }
        }
        if (lba >= curlun->num_sectors) {
                curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
index bd215cc791da53eb9713063600a56ab3edb1c92a..04df8570a2d23c88f6340dd74bfb8bd7607a2cc8 100644 (file)
@@ -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);
+       spin_unlock(&filp->f_lock);
  out:
        unlock_kernel();
        return error;
index 240ec63984cb9f5eb5e25f113a508cb518f73d18..421aab465dab69ff3d86c2576f1f544f3e48310b 100644 (file)
@@ -404,10 +404,12 @@ static int ioctl_fionbio(struct file *filp, int __user *argp)
        if (O_NONBLOCK != O_NDELAY)
                flag |= O_NDELAY;
 #endif
+       spin_lock(&filp->f_lock);
        if (on)
                filp->f_flags |= flag;
        else
                filp->f_flags &= ~flag;
+       spin_unlock(&filp->f_lock);
        return error;
 }
 
@@ -432,10 +434,12 @@ static int ioctl_fioasync(unsigned int fd, struct file *filp,
        if (error)
                return error;
 
+       spin_lock(&filp->f_lock);
        if (on)
                filp->f_flags |= FASYNC;
        else
                filp->f_flags &= ~FASYNC;
+       spin_unlock(&filp->f_lock);
        return error;
 }
 
@@ -499,10 +503,7 @@ int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
                break;
 
        case FIONBIO:
-               /* BKL needed to avoid races tweaking f_flags */
-               lock_kernel();
                error = ioctl_fionbio(filp, argp);
-               unlock_kernel();
                break;
 
        case FIOASYNC:
index 6e50aaa56ca25222ab71e0b92cdd66609b5c5b61..c165a6403df03e52219925b875ef420adcbd331a 100644 (file)
@@ -998,8 +998,11 @@ nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
 
        if (!EX_ISSYNC(exp))
                stable = 0;
-       if (stable && !EX_WGATHER(exp))
+       if (stable && !EX_WGATHER(exp)) {
+               spin_lock(&file->f_lock);
                file->f_flags |= O_SYNC;
+               spin_unlock(&file->f_lock);
+       }
 
        /* Write the data. */
        oldfs = get_fs(); set_fs(KERNEL_DS);
index 2011600d12c70b28f80f2909bcc00b28dbc38a3c..7428c6d35e6504312fcb7fec85e0db6552917708 100644 (file)
@@ -848,7 +848,7 @@ struct file {
 #define f_dentry       f_path.dentry
 #define f_vfsmnt       f_path.mnt
        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;
        unsigned int            f_flags;
        fmode_t                 f_mode;
index 54b4077fed79aaac0cd03da6c725d5d322b1159c..a8ddadbc74594cc6822c706b92f9f429359d0cce 100644 (file)
@@ -1156,10 +1156,12 @@ SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,
        omqstat.mq_flags = filp->f_flags & O_NONBLOCK;
        if (u_mqstat) {
                audit_mq_getsetattr(mqdes, &mqstat);
+               spin_lock(&filp->f_lock);
                if (mqstat.mq_flags & O_NONBLOCK)
                        filp->f_flags |= O_NONBLOCK;
                else
                        filp->f_flags &= ~O_NONBLOCK;
+               spin_unlock(&filp->f_lock);
 
                inode->i_atime = inode->i_ctime = CURRENT_TIME;
        }
index 0a1798eafb0b5400a0cceea85c072aa0e805f31a..d4460f18e76cc609af690a2d2427bbfeae3dc2d2 100644 (file)
@@ -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)
 {
+       spin_lock(&file->f_lock);
        file->f_flags |= O_NONBLOCK;
+       spin_unlock(&file->f_lock);
        return 0;
 }
 
index 81e1f443d0948b223cdfdaf4d58aeb68452e59fa..4191acccbcdb93fab12373847688b91103d52c65 100644 (file)
@@ -1627,7 +1627,9 @@ au1550_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
                                    sizeof(abinfo)) ? -EFAULT : 0;
 
        case SNDCTL_DSP_NONBLOCK:
+               spin_lock(&file->f_lock);
                file->f_flags |= O_NONBLOCK;
+               spin_unlock(&file->f_lock);
                return 0;
 
        case SNDCTL_DSP_GETODELAY:
index 89bd27a5e865adc5a8ff1870aabff63f889b6172..b69c05b7ea7b07d835ce9f360c8dede872df1032 100644 (file)
@@ -433,7 +433,9 @@ int audio_ioctl(int dev, struct file *file, unsigned int cmd, void __user *arg)
                        return dma_ioctl(dev, cmd, arg);
                
                case SNDCTL_DSP_NONBLOCK:
+                       spin_lock(&file->f_lock);
                        file->f_flags |= O_NONBLOCK;
+                       spin_unlock(&file->f_lock);
                        return 0;
 
                case SNDCTL_DSP_GETCAPS:
index e5d4239949182fd5649456e524436a7fb515e39e..78cfb66e4c59cecf83c51d55b8b7f7baf9bf683b 100644 (file)
@@ -135,7 +135,9 @@ static int dac_audio_ioctl(struct inode *inode, struct file *file,
                return put_user(AFMT_U8, (int *)arg);
 
        case SNDCTL_DSP_NONBLOCK:
+               spin_lock(&file->f_lock);
                file->f_flags |= O_NONBLOCK;
+               spin_unlock(&file->f_lock);
                return 0;
 
        case SNDCTL_DSP_GETCAPS:
index 41562ecde5bbd9fcc6cc41a1b0c5ec1b5f14a181..1edab7b4ea83b26a41311671983d65625d0edb33 100644 (file)
@@ -2200,7 +2200,9 @@ static int cs4297a_ioctl(struct inode *inode, struct file *file,
                                    sizeof(abinfo)) ? -EFAULT : 0;
 
        case SNDCTL_DSP_NONBLOCK:
+               spin_lock(&file->f_lock);
                file->f_flags |= O_NONBLOCK;
+               spin_unlock(&file->f_lock);
                return 0;
 
        case SNDCTL_DSP_GETODELAY:
index 78b8acc7c3b96358330f8223906b3fb64dcc84db..187f72750e8f9796734bf34c511ab8791868f247 100644 (file)
@@ -2673,7 +2673,9 @@ static int vwsnd_audio_do_ioctl(struct inode *inode,
 
        case SNDCTL_DSP_NONBLOCK:       /* _SIO  ('P',14) */
                DBGX("SNDCTL_DSP_NONBLOCK\n");
+               spin_lock(&file->f_lock);
                file->f_flags |= O_NONBLOCK;
+               spin_unlock(&file->f_lock);
                return 0;
 
        case SNDCTL_DSP_RESET:          /* _SIO  ('P', 0) */