All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Morton <akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b@public.gmane.org>
To: Jonathan Corbet <corbet-T1hC0tSOHrs@public.gmane.org>
Cc: linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	andi-Vw/NltI1exuRpAAqCnN02g@public.gmane.org,
	viro-3bDd1+5oDREiFSDQTTA3OLVCufUGDwFn@public.gmane.org,
	oleg-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org,
	linux-api-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	alan-qBU/x9rampVanCEyBjwyrvXRex20P6io@public.gmane.org
Subject: Re: [PATCH, RFC] Remove fasync() BKL usage, take 3325
Date: Thu, 22 Jan 2009 06:51:04 -0800	[thread overview]
Message-ID: <20090122065104.2787df2d.akpm@linux-foundation.org> (raw)
In-Reply-To: <20090115153211.663df310-vw3g6Xz/EtPk1uMJSBkQmQ@public.gmane.org>

> On Thu, 15 Jan 2009 15:32:11 -0700 Jonathan Corbet <corbet-T1hC0tSOHrs@public.gmane.org> wrote:
> One of these years I've got to get this right.  I've fixed the problem
> pointed out by Oleg where f_flags would get changed even if fasync()
> fails.  
> 
> I have also taken out the ABI change.  CCing the linux-api list because
> I still think it's not quite right; fcntl() should not silently let
> applications set the FASYNC flag if the underlying driver/filesystem
> does not support it.  But that's How We've Always Done It, and one
> messes with such things at great risk.  If we want fcntl() to return an
> error in this case, it's an easy change.
> 
> This one's against 2.6.29-rc1.  If I don't hear screaming, I'll drop
> this one into linux-next.
> 

scream.

> 
> jon
> 
> --
> 
> Accesses to the f_flags member of struct file involve read-modify-write
> cycles; they have traditionally been done in a racy way.  This patch
> introduces a global spinlock to protect f_flags against concurrent
> modifications.
> 
> Additionally, changes to the FASYNC flag and resulting calls to
> f_op->fasync() need to be done in an atomic manner.  Here, the BKL is
> removed and FASYNC modifications are protected with a mutex.
> 
> Signed-off-by: Jonathan Corbet <corbet-T1hC0tSOHrs@public.gmane.org>
> ---
>  drivers/char/tty_io.c |    5 +--
>  fs/fcntl.c            |   65 +++++++++++++++++++++++++++++++++++++++----------
>  fs/ioctl.c            |   25 ++++---------------
>  fs/nfsd/vfs.c         |    5 +++-
>  include/linux/fs.h    |   17 +++++++++++++
>  5 files changed, 80 insertions(+), 37 deletions(-)
> 
> diff --git a/drivers/char/tty_io.c b/drivers/char/tty_io.c
> index d33e5ab..8450316 100644
> --- a/drivers/char/tty_io.c
> +++ b/drivers/char/tty_io.c
> @@ -2160,13 +2160,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();
> +	lock_file_flags();
>  	if (nonblock)
>  		file->f_flags |= O_NONBLOCK;
>  	else
>  		file->f_flags &= ~O_NONBLOCK;
> -	unlock_kernel();
> +	unlock_file_flags();

OK, replacing a lock_kernel() with a spin_lock(&global_lock) is pretty
straightforwad.  But it's really really sad.  It basically leaves a great
big FIXME in there.  It'd be better to fix it.

We don't have a handy lock in struct file which could be borrowed.

- We could add one

- We could borrow file->f_path.dentry->d_inode->i_lock

- We could convert that field to long and use bitops (sounds nice?)

>  	return 0;
>  }
>  
> diff --git a/fs/fcntl.c b/fs/fcntl.c
> index cdc1419..ddd497d 100644
> --- a/fs/fcntl.c
> +++ b/fs/fcntl.c
>
> ...
>
> +/*
> + * Change the setting of fasync, let the driver know.
> + * Not static because ioctl_fioasync() uses it too.
> + */
> +int fasync_change(int fd, struct file *filp, int on)
> +{
> +	int ret = 0;
> +	static DEFINE_MUTEX(fasync_mutex);
> +
> +	if (filp->f_op->fasync == NULL)
> +		return -ENOTTY;
> +
> +	mutex_lock(&fasync_mutex);
> +	/* Can test without flags lock, nobody else will change it */
> +	if (((filp->f_flags & FASYNC) == 0) == (on == 0))
> +		goto out;
> +	ret = filp->f_op->fasync(fd, filp, on);
> +	if (ret >= 0) {
> +		lock_file_flags();
> +		if (on)
> +			filp->f_flags |= FASYNC;
> +		else
> +			filp->f_flags &= ~FASYNC;
> +		unlock_file_flags();
> +	}
> +  out:

column zero, please.

> +	mutex_unlock(&fasync_mutex);
> +	return ret;
> +}

It isn't completely obvious what fasync_mutex is protecting, why it exists.

A comment which explains this would be appropriate?


--
To unsubscribe from this list: send the line "unsubscribe linux-api" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

WARNING: multiple messages have this Message-ID (diff)
From: Andrew Morton <akpm@linux-foundation.org>
To: Jonathan Corbet <corbet@lwn.net>
Cc: linux-kernel@vger.kernel.org, andi@firstfloor.org,
	viro@ZenIV.linux.org.uk, oleg@redhat.com,
	linux-api@vger.kernel.org, alan@lxorguk.ukuu.org.uk
Subject: Re: [PATCH, RFC] Remove fasync() BKL usage, take 3325
Date: Thu, 22 Jan 2009 06:51:04 -0800	[thread overview]
Message-ID: <20090122065104.2787df2d.akpm@linux-foundation.org> (raw)
In-Reply-To: <20090115153211.663df310@bike.lwn.net>

> On Thu, 15 Jan 2009 15:32:11 -0700 Jonathan Corbet <corbet@lwn.net> wrote:
> One of these years I've got to get this right.  I've fixed the problem
> pointed out by Oleg where f_flags would get changed even if fasync()
> fails.  
> 
> I have also taken out the ABI change.  CCing the linux-api list because
> I still think it's not quite right; fcntl() should not silently let
> applications set the FASYNC flag if the underlying driver/filesystem
> does not support it.  But that's How We've Always Done It, and one
> messes with such things at great risk.  If we want fcntl() to return an
> error in this case, it's an easy change.
> 
> This one's against 2.6.29-rc1.  If I don't hear screaming, I'll drop
> this one into linux-next.
> 

scream.

> 
> jon
> 
> --
> 
> Accesses to the f_flags member of struct file involve read-modify-write
> cycles; they have traditionally been done in a racy way.  This patch
> introduces a global spinlock to protect f_flags against concurrent
> modifications.
> 
> Additionally, changes to the FASYNC flag and resulting calls to
> f_op->fasync() need to be done in an atomic manner.  Here, the BKL is
> removed and FASYNC modifications are protected with a mutex.
> 
> Signed-off-by: Jonathan Corbet <corbet@lwn.net>
> ---
>  drivers/char/tty_io.c |    5 +--
>  fs/fcntl.c            |   65 +++++++++++++++++++++++++++++++++++++++----------
>  fs/ioctl.c            |   25 ++++---------------
>  fs/nfsd/vfs.c         |    5 +++-
>  include/linux/fs.h    |   17 +++++++++++++
>  5 files changed, 80 insertions(+), 37 deletions(-)
> 
> diff --git a/drivers/char/tty_io.c b/drivers/char/tty_io.c
> index d33e5ab..8450316 100644
> --- a/drivers/char/tty_io.c
> +++ b/drivers/char/tty_io.c
> @@ -2160,13 +2160,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();
> +	lock_file_flags();
>  	if (nonblock)
>  		file->f_flags |= O_NONBLOCK;
>  	else
>  		file->f_flags &= ~O_NONBLOCK;
> -	unlock_kernel();
> +	unlock_file_flags();

OK, replacing a lock_kernel() with a spin_lock(&global_lock) is pretty
straightforwad.  But it's really really sad.  It basically leaves a great
big FIXME in there.  It'd be better to fix it.

We don't have a handy lock in struct file which could be borrowed.

- We could add one

- We could borrow file->f_path.dentry->d_inode->i_lock

- We could convert that field to long and use bitops (sounds nice?)

>  	return 0;
>  }
>  
> diff --git a/fs/fcntl.c b/fs/fcntl.c
> index cdc1419..ddd497d 100644
> --- a/fs/fcntl.c
> +++ b/fs/fcntl.c
>
> ...
>
> +/*
> + * Change the setting of fasync, let the driver know.
> + * Not static because ioctl_fioasync() uses it too.
> + */
> +int fasync_change(int fd, struct file *filp, int on)
> +{
> +	int ret = 0;
> +	static DEFINE_MUTEX(fasync_mutex);
> +
> +	if (filp->f_op->fasync == NULL)
> +		return -ENOTTY;
> +
> +	mutex_lock(&fasync_mutex);
> +	/* Can test without flags lock, nobody else will change it */
> +	if (((filp->f_flags & FASYNC) == 0) == (on == 0))
> +		goto out;
> +	ret = filp->f_op->fasync(fd, filp, on);
> +	if (ret >= 0) {
> +		lock_file_flags();
> +		if (on)
> +			filp->f_flags |= FASYNC;
> +		else
> +			filp->f_flags &= ~FASYNC;
> +		unlock_file_flags();
> +	}
> +  out:

column zero, please.

> +	mutex_unlock(&fasync_mutex);
> +	return ret;
> +}

It isn't completely obvious what fasync_mutex is protecting, why it exists.

A comment which explains this would be appropriate?



  parent reply	other threads:[~2009-01-22 14:51 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-01-15 22:32 [PATCH, RFC] Remove fasync() BKL usage, take 3325 Jonathan Corbet
2009-01-15 22:32 ` Jonathan Corbet
     [not found] ` <20090115153211.663df310-vw3g6Xz/EtPk1uMJSBkQmQ@public.gmane.org>
2009-01-22 14:51   ` Andrew Morton [this message]
2009-01-22 14:51     ` Andrew Morton
     [not found]     ` <20090122065104.2787df2d.akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b@public.gmane.org>
2009-01-22 16:09       ` Andi Kleen
2009-01-22 16:09         ` Andi Kleen
     [not found]         ` <20090122160935.GI15750-qrUzlfsMFqo/4alezvVtWx2eb7JE58TQ@public.gmane.org>
2009-01-23  5:21           ` Jonathan Corbet
2009-01-23  5:21             ` Jonathan Corbet
2009-01-22 20:32       ` Christoph Hellwig
2009-01-22 20:32         ` Christoph Hellwig
     [not found]         ` <20090122203248.GA20159-wEGCiKHe2LqWVfeAwA7xHQ@public.gmane.org>
2009-01-23  4:56           ` Andi Kleen
2009-01-23  4:56             ` Andi Kleen
     [not found]             ` <20090123045646.GK15750-qrUzlfsMFqo/4alezvVtWx2eb7JE58TQ@public.gmane.org>
2009-01-28  0:53               ` Andrew Morton
2009-01-28  0:53                 ` Andrew Morton
2009-01-28  0:55               ` Andrew Morton
2009-01-28  0:55                 ` Andrew Morton
     [not found]                 ` <20090127165504.53ed7a2d.akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b@public.gmane.org>
2009-01-28  3:14                   ` Oleg Nesterov
2009-01-28  3:14                     ` Oleg Nesterov
     [not found]                     ` <20090128031439.GA11025-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2009-01-28  3:57                       ` Jonathan Corbet
2009-01-28  3:57                         ` Jonathan Corbet
     [not found]                         ` <20090127205739.1384343f-vw3g6Xz/EtPk1uMJSBkQmQ@public.gmane.org>
2009-01-28  4:23                           ` Oleg Nesterov
2009-01-28  4:23                             ` Oleg Nesterov
     [not found]                             ` <20090128042337.GA15060-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2009-01-28 14:13                               ` Jonathan Corbet
2009-01-28 14:13                                 ` Jonathan Corbet
2009-01-28 17:36                       ` Christoph Hellwig
2009-01-28 17:36                         ` Christoph Hellwig
     [not found]                         ` <20090128173618.GA3174-wEGCiKHe2LqWVfeAwA7xHQ@public.gmane.org>
2009-01-28 17:44                           ` Jonathan Corbet
2009-01-28 17:44                             ` Jonathan Corbet
     [not found]                             ` <20090128104414.09aee3f9-vw3g6Xz/EtPk1uMJSBkQmQ@public.gmane.org>
2009-01-28 17:55                               ` Christoph Hellwig
2009-01-28 17:55                                 ` Christoph Hellwig
     [not found]                                 ` <20090128175541.GA7074-wEGCiKHe2LqWVfeAwA7xHQ@public.gmane.org>
2009-01-28 18:13                                   ` Matt Mackall
2009-01-28 18:13                                     ` Matt Mackall
2009-01-28 21:05                                     ` Jonathan Corbet
2009-01-28 21:05                                       ` Jonathan Corbet
2009-01-28 18:14                           ` David Daney
2009-01-28 18:14                             ` David Daney
2009-01-29 14:37                           ` Andi Kleen
2009-01-29 14:37                             ` Andi Kleen
2009-01-23  5:15       ` Jonathan Corbet
2009-01-23  5:15         ` Jonathan Corbet
2009-01-23  5:31         ` Andrew Morton
2009-01-23  5:31           ` Andrew Morton
     [not found]           ` <20090122213105.74142908.akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b@public.gmane.org>
2009-01-23  5:45             ` Matt Mackall
2009-01-23  5:45               ` Matt Mackall
2009-01-23  6:15               ` Andi Kleen
2009-01-23  6:15                 ` Andi Kleen
2009-01-23 10:45                 ` Bernd Petrovitsch
2009-01-23  5:54             ` Andi Kleen
2009-01-23  5:54               ` Andi Kleen
     [not found]               ` <20090123055404.GL15750-qrUzlfsMFqo/4alezvVtWx2eb7JE58TQ@public.gmane.org>
2009-01-23  6:01                 ` Jonathan Corbet
2009-01-23  6:01                   ` Jonathan Corbet
2009-01-23  6:57                 ` Andrew Morton
2009-01-23  6:57                   ` Andrew Morton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20090122065104.2787df2d.akpm@linux-foundation.org \
    --to=akpm-de/tnxtf+jlsfhdxvbkv3wd2fqjk+8+b@public.gmane.org \
    --cc=alan-qBU/x9rampVanCEyBjwyrvXRex20P6io@public.gmane.org \
    --cc=andi-Vw/NltI1exuRpAAqCnN02g@public.gmane.org \
    --cc=corbet-T1hC0tSOHrs@public.gmane.org \
    --cc=linux-api-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=oleg-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org \
    --cc=viro-3bDd1+5oDREiFSDQTTA3OLVCufUGDwFn@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.