From db1dd4d376134eba0e08af523b61cc566a4ea1cd Mon Sep 17 00:00:00 2001 From: Jonathan Corbet Date: Fri, 6 Feb 2009 15:25:24 -0700 Subject: 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 Cc: Al Viro Signed-off-by: Jonathan Corbet --- drivers/char/tty_io.c | 5 ++--- drivers/usb/gadget/file_storage.c | 7 ++++++- 2 files changed, 8 insertions(+), 4 deletions(-) (limited to 'drivers') diff --git a/drivers/char/tty_io.c b/drivers/char/tty_io.c index bc84e125c6bc..224f271d8cbe 100644 --- a/drivers/char/tty_io.c +++ b/drivers/char/tty_io.c @@ -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; } diff --git a/drivers/usb/gadget/file_storage.c b/drivers/usb/gadget/file_storage.c index 1ab9dac7e12d..33bb76cef33c 100644 --- a/drivers/usb/gadget/file_storage.c +++ b/drivers/usb/gadget/file_storage.c @@ -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; -- cgit v1.2.3-59-g8ed1b From 60aa49243d09afc873f082567d2e3c16634ced84 Mon Sep 17 00:00:00 2001 From: Jonathan Corbet Date: Sun, 1 Feb 2009 14:52:56 -0700 Subject: Rationalize fasync return values Most fasync implementations do something like: return fasync_helper(...); But fasync_helper() will return a positive value at times - a feature used in at least one place. Thus, a number of other drivers do: err = fasync_helper(...); if (err < 0) return err; return 0; In the interests of consistency and more concise code, it makes sense to map positive return values onto zero where ->fasync() is called. Cc: Al Viro Signed-off-by: Jonathan Corbet --- drivers/char/sonypi.c | 7 +------ drivers/gpu/drm/drm_fops.c | 6 +----- drivers/hid/usbhid/hiddev.c | 5 +---- drivers/ieee1394/dv1394.c | 6 +----- drivers/input/evdev.c | 5 +---- drivers/input/joydev.c | 5 +---- drivers/input/mousedev.c | 5 +---- drivers/input/serio/serio_raw.c | 4 +--- drivers/net/wan/cosa.c | 4 ++-- drivers/platform/x86/sony-laptop.c | 7 +------ drivers/scsi/sg.c | 4 +--- fs/fcntl.c | 2 ++ fs/ioctl.c | 2 +- fs/pipe.c | 16 +++------------- sound/core/control.c | 7 ++----- sound/core/pcm_native.c | 4 +--- sound/core/timer.c | 6 +----- 17 files changed, 22 insertions(+), 73 deletions(-) (limited to 'drivers') diff --git a/drivers/char/sonypi.c b/drivers/char/sonypi.c index f4374437a033..fd3dced97776 100644 --- a/drivers/char/sonypi.c +++ b/drivers/char/sonypi.c @@ -888,12 +888,7 @@ found: static int sonypi_misc_fasync(int fd, struct file *filp, int on) { - int retval; - - retval = fasync_helper(fd, filp, on, &sonypi_device.fifo_async); - if (retval < 0) - return retval; - return 0; + return fasync_helper(fd, filp, on, &sonypi_device.fifo_async); } static int sonypi_misc_release(struct inode *inode, struct file *file) diff --git a/drivers/gpu/drm/drm_fops.c b/drivers/gpu/drm/drm_fops.c index f52663ebe016..e13cb62bbaee 100644 --- a/drivers/gpu/drm/drm_fops.c +++ b/drivers/gpu/drm/drm_fops.c @@ -337,14 +337,10 @@ int drm_fasync(int fd, struct file *filp, int on) { struct drm_file *priv = filp->private_data; struct drm_device *dev = priv->minor->dev; - int retcode; DRM_DEBUG("fd = %d, device = 0x%lx\n", fd, (long)old_encode_dev(priv->minor->device)); - retcode = fasync_helper(fd, filp, on, &dev->buf_async); - if (retcode < 0) - return retcode; - return 0; + return fasync_helper(fd, filp, on, &dev->buf_async); } EXPORT_SYMBOL(drm_fasync); diff --git a/drivers/hid/usbhid/hiddev.c b/drivers/hid/usbhid/hiddev.c index 4940e4d70c2d..3a7b4fe192a3 100644 --- a/drivers/hid/usbhid/hiddev.c +++ b/drivers/hid/usbhid/hiddev.c @@ -227,12 +227,9 @@ void hiddev_report_event(struct hid_device *hid, struct hid_report *report) */ static int hiddev_fasync(int fd, struct file *file, int on) { - int retval; struct hiddev_list *list = file->private_data; - retval = fasync_helper(fd, file, on, &list->fasync); - - return retval < 0 ? retval : 0; + return fasync_helper(fd, file, on, &list->fasync); } diff --git a/drivers/ieee1394/dv1394.c b/drivers/ieee1394/dv1394.c index 3838bc4acaba..cb15bfa38d70 100644 --- a/drivers/ieee1394/dv1394.c +++ b/drivers/ieee1394/dv1394.c @@ -1325,11 +1325,7 @@ static int dv1394_fasync(int fd, struct file *file, int on) struct video_card *video = file_to_video_card(file); - int retval = fasync_helper(fd, file, on, &video->fasync); - - if (retval < 0) - return retval; - return 0; + return fasync_helper(fd, file, on, &video->fasync); } static ssize_t dv1394_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos) diff --git a/drivers/input/evdev.c b/drivers/input/evdev.c index ed8baa0aec3c..7a7a026ba712 100644 --- a/drivers/input/evdev.c +++ b/drivers/input/evdev.c @@ -94,11 +94,8 @@ static void evdev_event(struct input_handle *handle, static int evdev_fasync(int fd, struct file *file, int on) { struct evdev_client *client = file->private_data; - int retval; - - retval = fasync_helper(fd, file, on, &client->fasync); - return retval < 0 ? retval : 0; + return fasync_helper(fd, file, on, &client->fasync); } static int evdev_flush(struct file *file, fl_owner_t id) diff --git a/drivers/input/joydev.c b/drivers/input/joydev.c index 6f2366220a50..4224f0112849 100644 --- a/drivers/input/joydev.c +++ b/drivers/input/joydev.c @@ -159,12 +159,9 @@ static void joydev_event(struct input_handle *handle, static int joydev_fasync(int fd, struct file *file, int on) { - int retval; struct joydev_client *client = file->private_data; - retval = fasync_helper(fd, file, on, &client->fasync); - - return retval < 0 ? retval : 0; + return fasync_helper(fd, file, on, &client->fasync); } static void joydev_free(struct device *dev) diff --git a/drivers/input/mousedev.c b/drivers/input/mousedev.c index ef99a7e6d40c..17fd6d46d082 100644 --- a/drivers/input/mousedev.c +++ b/drivers/input/mousedev.c @@ -403,12 +403,9 @@ static void mousedev_event(struct input_handle *handle, static int mousedev_fasync(int fd, struct file *file, int on) { - int retval; struct mousedev_client *client = file->private_data; - retval = fasync_helper(fd, file, on, &client->fasync); - - return retval < 0 ? retval : 0; + return fasync_helper(fd, file, on, &client->fasync); } static void mousedev_free(struct device *dev) diff --git a/drivers/input/serio/serio_raw.c b/drivers/input/serio/serio_raw.c index 06bbd0e74c6f..b03009bb7468 100644 --- a/drivers/input/serio/serio_raw.c +++ b/drivers/input/serio/serio_raw.c @@ -58,10 +58,8 @@ static unsigned int serio_raw_no; static int serio_raw_fasync(int fd, struct file *file, int on) { struct serio_raw_list *list = file->private_data; - int retval; - retval = fasync_helper(fd, file, on, &list->fasync); - return retval < 0 ? retval : 0; + return fasync_helper(fd, file, on, &list->fasync); } static struct serio_raw *serio_raw_locate(int minor) diff --git a/drivers/net/wan/cosa.c b/drivers/net/wan/cosa.c index d80b72e22dea..ce753e9c576b 100644 --- a/drivers/net/wan/cosa.c +++ b/drivers/net/wan/cosa.c @@ -993,8 +993,8 @@ static struct fasync_struct *fasync[256] = { NULL, }; static int cosa_fasync(struct inode *inode, struct file *file, int on) { int port = iminor(inode); - int rv = fasync_helper(inode, file, on, &fasync[port]); - return rv < 0 ? rv : 0; + + return fasync_helper(inode, file, on, &fasync[port]); } #endif diff --git a/drivers/platform/x86/sony-laptop.c b/drivers/platform/x86/sony-laptop.c index 537959d07148..bc8996c849ac 100644 --- a/drivers/platform/x86/sony-laptop.c +++ b/drivers/platform/x86/sony-laptop.c @@ -1917,12 +1917,7 @@ static struct sonypi_compat_s sonypi_compat = { static int sonypi_misc_fasync(int fd, struct file *filp, int on) { - int retval; - - retval = fasync_helper(fd, filp, on, &sonypi_compat.fifo_async); - if (retval < 0) - return retval; - return 0; + return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async); } static int sonypi_misc_release(struct inode *inode, struct file *file) diff --git a/drivers/scsi/sg.c b/drivers/scsi/sg.c index 516925d8b570..b4ef2f84ea32 100644 --- a/drivers/scsi/sg.c +++ b/drivers/scsi/sg.c @@ -1154,7 +1154,6 @@ sg_poll(struct file *filp, poll_table * wait) static int sg_fasync(int fd, struct file *filp, int mode) { - int retval; Sg_device *sdp; Sg_fd *sfp; @@ -1163,8 +1162,7 @@ sg_fasync(int fd, struct file *filp, int mode) SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n", sdp->disk->disk_name, mode)); - retval = fasync_helper(fd, filp, mode, &sfp->async_qp); - return (retval < 0) ? retval : 0; + return fasync_helper(fd, filp, mode, &sfp->async_qp); } static int diff --git a/fs/fcntl.c b/fs/fcntl.c index 431bb6459273..d865ca66ccba 100644 --- a/fs/fcntl.c +++ b/fs/fcntl.c @@ -184,6 +184,8 @@ static int setfl(int fd, struct file * filp, unsigned long arg) error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0); if (error < 0) goto out; + if (error > 0) + error = 0; } spin_lock(&filp->f_lock); filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK); diff --git a/fs/ioctl.c b/fs/ioctl.c index e8e89edba576..ac2d47e43926 100644 --- a/fs/ioctl.c +++ b/fs/ioctl.c @@ -432,7 +432,7 @@ static int ioctl_fioasync(unsigned int fd, struct file *filp, else error = -ENOTTY; } - return error; + return error < 0 ? error : 0; } static int ioctl_fsfreeze(struct file *filp) diff --git a/fs/pipe.c b/fs/pipe.c index 14f502b89cf5..94ad15967cf9 100644 --- a/fs/pipe.c +++ b/fs/pipe.c @@ -667,10 +667,7 @@ pipe_read_fasync(int fd, struct file *filp, int on) retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers); mutex_unlock(&inode->i_mutex); - if (retval < 0) - return retval; - - return 0; + return retval; } @@ -684,10 +681,7 @@ pipe_write_fasync(int fd, struct file *filp, int on) retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers); mutex_unlock(&inode->i_mutex); - if (retval < 0) - return retval; - - return 0; + return retval; } @@ -706,11 +700,7 @@ pipe_rdwr_fasync(int fd, struct file *filp, int on) fasync_helper(-1, filp, 0, &pipe->fasync_readers); } mutex_unlock(&inode->i_mutex); - - if (retval < 0) - return retval; - - return 0; + return retval; } diff --git a/sound/core/control.c b/sound/core/control.c index 636b3b52ef8b..4b20fa2b7e6d 100644 --- a/sound/core/control.c +++ b/sound/core/control.c @@ -1373,12 +1373,9 @@ EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat); static int snd_ctl_fasync(int fd, struct file * file, int on) { struct snd_ctl_file *ctl; - int err; + ctl = file->private_data; - err = fasync_helper(fd, file, on, &ctl->fasync); - if (err < 0) - return err; - return 0; + return fasync_helper(fd, file, on, &ctl->fasync); } /* diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c index a789efc9df39..a75c194e629e 100644 --- a/sound/core/pcm_native.c +++ b/sound/core/pcm_native.c @@ -3246,9 +3246,7 @@ static int snd_pcm_fasync(int fd, struct file * file, int on) err = fasync_helper(fd, file, on, &runtime->fasync); out: unlock_kernel(); - if (err < 0) - return err; - return 0; + return err; } /* diff --git a/sound/core/timer.c b/sound/core/timer.c index 796532081e81..3f0050d0b71e 100644 --- a/sound/core/timer.c +++ b/sound/core/timer.c @@ -1825,13 +1825,9 @@ static long snd_timer_user_ioctl(struct file *file, unsigned int cmd, static int snd_timer_user_fasync(int fd, struct file * file, int on) { struct snd_timer_user *tu; - int err; tu = file->private_data; - err = fasync_helper(fd, file, on, &tu->fasync); - if (err < 0) - return err; - return 0; + return fasync_helper(fd, file, on, &tu->fasync); } static ssize_t snd_timer_user_read(struct file *file, char __user *buffer, -- cgit v1.2.3-59-g8ed1b