public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Andi Kleen <andi@firstfloor.org>
To: corbet@lwn.net, linux-kernel@vger.kernel.org
Subject: [PATCH] [2/11] Add unlocked_fasync
Date: Tue, 20 May 2008 17:28:43 +0200 (CEST)	[thread overview]
Message-ID: <20080520152843.CF4181B4205@basil.firstfloor.org> (raw)
In-Reply-To: <20080520528.793382059@firstfloor.org>


Add a new fops entry point to allow fasync without BKL. While it's arguably
unclear this entry point is called often enough for it really matters
it was still relatively easy to do. And there are far less async users
in the tree than ioctls so it's likely they can be all converted 
eventually and then the non unlocked async entry point could be dropped.

There was still the problem of the actual flags change being
protected against other setters of flags. Instead of using BKL
for this use the i_mutex now.

I also added a mutex_lock against one other flags change
that was lockless and could potentially lose updates.

There are a couple of potential problems I added comments about on.

Signed-off-by: Andi Kleen <ak@suse.de>
Signed-off-by: Andi Kleen <ak@linux.intel.com>

---
 Documentation/filesystems/vfs.txt |    5 ++++-
 fs/fcntl.c                        |   22 +++++++++++++++-------
 fs/ioctl.c                        |   13 ++++++++++++-
 include/linux/fs.h                |    1 +
 4 files changed, 32 insertions(+), 9 deletions(-)

Index: linux/fs/fcntl.c
===================================================================
--- linux.orig/fs/fcntl.c
+++ linux/fs/fcntl.c
@@ -227,18 +227,26 @@ static int setfl(int fd, struct file * f
 	if (error)
 		return error;
 
-	lock_kernel();
+	/* AK: potential race here. Since test is unlocked fasync could
+	   be called several times in a row with on. We hope the drivers
+	   deal with it. */
 	if ((arg ^ filp->f_flags) & FASYNC) {
-		if (filp->f_op && filp->f_op->fasync) {
-			error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
-			if (error < 0)
-				goto out;
+		int on = !!(arg & FASYNC);
+		if (filp->f_op && filp->f_op->unlocked_fasync)
+			error = filp->f_op->unlocked_fasync(fd, filp, on);
+		else if (filp->f_op && filp->f_op->fasync) {
+			lock_kernel();
+			error = filp->f_op->fasync(fd, filp, on);
+			unlock_kernel();
 		}
+		/* AK: no else error = -EINVAL here? */
+		if (error < 0)
+			return error;
 	}
 
+	mutex_lock(&filp->f_dentry->d_inode->i_mutex);
 	filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
- out:
-	unlock_kernel();
+	mutex_unlock(&filp->f_dentry->d_inode->i_mutex);
 	return error;
 }
 
Index: linux/fs/ioctl.c
===================================================================
--- linux.orig/fs/ioctl.c
+++ linux/fs/ioctl.c
@@ -103,10 +103,13 @@ static int ioctl_fionbio(struct file *fi
 	if (O_NONBLOCK != O_NDELAY)
 		flag |= O_NDELAY;
 #endif
+	/* Protect f_flags */
+	mutex_lock(&filp->f_dentry->d_inode->i_mutex);
 	if (on)
 		filp->f_flags |= flag;
 	else
 		filp->f_flags &= ~flag;
+	mutex_unlock(&filp->f_dentry->d_inode->i_mutex);
 	return error;
 }
 
@@ -122,8 +125,13 @@ static int ioctl_fioasync(unsigned int f
 	flag = on ? FASYNC : 0;
 
 	/* Did FASYNC state change ? */
+	/* AK: potential race here: ->fasync could be called with on two times
+	   in a row. We hope the drivers deal with it. */
 	if ((flag ^ filp->f_flags) & FASYNC) {
-		if (filp->f_op && filp->f_op->fasync) {
+		if (filp->f_op && filp->f_op->unlocked_fasync) {
+			error = filp->f_op->unlocked_fasync(fd,
+							    filp, on);
+		} else if (filp->f_op && filp->f_op->fasync) {
 			lock_kernel();
 			error = filp->f_op->fasync(fd, filp, on);
 			unlock_kernel();
@@ -133,10 +141,13 @@ static int ioctl_fioasync(unsigned int f
 	if (error)
 		return error;
 
+	/* Protect f_flags */
+	mutex_lock(&filp->f_dentry->d_inode->i_mutex);
 	if (on)
 		filp->f_flags |= FASYNC;
 	else
 		filp->f_flags &= ~FASYNC;
+	mutex_unlock(&filp->f_dentry->d_inode->i_mutex);
 	return error;
 }
 
Index: linux/include/linux/fs.h
===================================================================
--- linux.orig/include/linux/fs.h
+++ linux/include/linux/fs.h
@@ -1237,6 +1237,7 @@ struct file_operations {
 	int (*fsync) (struct file *, struct dentry *, int datasync);
 	int (*aio_fsync) (struct kiocb *, int datasync);
 	int (*fasync) (int, struct file *, int);
+	int (*unlocked_fasync) (int, struct file *, int);
 	int (*lock) (struct file *, int, struct file_lock *);
 	ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
 	unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
Index: linux/Documentation/filesystems/vfs.txt
===================================================================
--- linux.orig/Documentation/filesystems/vfs.txt
+++ linux/Documentation/filesystems/vfs.txt
@@ -755,6 +755,7 @@ struct file_operations {
 	int (*fsync) (struct file *, struct dentry *, int datasync);
 	int (*aio_fsync) (struct kiocb *, int datasync);
 	int (*fasync) (int, struct file *, int);
+	int (*unlocked_fasync) (int, struct file *, int);
 	int (*lock) (struct file *, int, struct file_lock *);
 	ssize_t (*readv) (struct file *, const struct iovec *, unsigned long, loff_t *);
 	ssize_t (*writev) (struct file *, const struct iovec *, unsigned long, loff_t *);
@@ -814,7 +815,9 @@ otherwise noted.
   fsync: called by the fsync(2) system call
 
   fasync: called by the fcntl(2) system call when asynchronous
-	(non-blocking) mode is enabled for a file
+	(non-blocking) mode is enabled for a file. BKL hold
+
+  unlocked_fasync: like fasync, but without BKL
 
   lock: called by the fcntl(2) system call for F_GETLK, F_SETLK, and F_SETLKW
   	commands

  parent reply	other threads:[~2008-05-20 15:33 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-05-20 15:28 [PATCH] [0/11] REPOST: Early exception fixes Andi Kleen
2008-05-20 15:28 ` [PATCH] [1/11] Remove BKL from remote_llseek v2 Andi Kleen
2008-05-20 15:28 ` Andi Kleen [this message]
2008-05-20 15:58   ` [PATCH] [2/11] Add unlocked_fasync Arjan van de Ven
2008-05-20 18:30     ` Andi Kleen
2008-05-20 20:06       ` Arjan van de Ven
2008-05-20 23:35         ` Andi Kleen
2008-05-21  4:47           ` Arjan van de Ven
2008-05-20 18:33     ` Alan Cox
2008-05-20 18:59       ` Andi Kleen
2008-05-20 15:58   ` Randy Dunlap
2008-05-20 15:58   ` Jonathan Corbet
2008-05-20 18:31     ` Andi Kleen
2008-05-20 15:28 ` [PATCH] [3/11] Convert pipe over to unlocked_fasync Andi Kleen
2008-05-20 15:28 ` [PATCH] [4/11] Convert socket fasync " Andi Kleen
2008-05-20 15:28 ` [PATCH] [5/11] Convert fuse " Andi Kleen
2008-05-20 15:28 ` [PATCH] [6/11] Convert bad_inode " Andi Kleen
2008-05-20 15:28 ` [PATCH] [7/11] Convert DRM " Andi Kleen
2008-05-21  6:36   ` Dave Airlie
2008-05-20 15:28 ` [PATCH] [8/11] Use unlocked_fasync in random.c Andi Kleen
2008-05-20 15:28 ` [PATCH] [9/11] Convert hpet to unlocked_fasync Andi Kleen
2008-05-21  9:01   ` Clemens Ladisch
2008-05-20 15:28 ` [PATCH] [10/11] Use unlocked_fasync in RTC Andi Kleen
2008-05-20 15:28 ` [PATCH] [11/11] Convert uio to fasync_unlocked Andi Kleen
  -- strict thread matches above, loose matches on Subject: below --
2008-05-19 12:31 [PATCH] [0/11] Repost of old VFS BKL patchkit Andi Kleen
2008-05-19 12:31 ` [PATCH] [2/11] Add unlocked_fasync Andi Kleen
2008-05-19 14:03   ` Christoph Hellwig
2008-05-19 14:43     ` Andi Kleen
2008-05-19 15:12       ` Alan Cox
2008-05-19 15:29         ` Andi Kleen
2008-05-19 15:22           ` Alan Cox
2008-05-19 15:45             ` Andi Kleen
2008-05-19 17:29   ` Randy Dunlap

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=20080520152843.CF4181B4205@basil.firstfloor.org \
    --to=andi@firstfloor.org \
    --cc=corbet@lwn.net \
    --cc=linux-kernel@vger.kernel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox