linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Dmitry Monakhov <dmonakhov@openvz.org>
To: linux-kernel@vger.kernel.org
Cc: linux-fsdevel@vger.kernel.org, viro@zeniv.linux.org.uk,
	Dmitry Monakhov <dmonakhov@openvz.org>
Subject: [PATCH 01/16] fs: save file->f_flags to kiocb->ki_flags
Date: Sat,  4 Apr 2015 23:13:10 +0400	[thread overview]
Message-ID: <1428174805-853-2-git-send-email-dmonakhov@openvz.org> (raw)
In-Reply-To: <1428174805-853-1-git-send-email-dmonakhov@openvz.org>

There are many places inside vfs/fs where code flow depends on file->f_flags,
but this check is racy because one can change it via fcntl(,F_SETFL,)
For example O_DIRECT usually flag checked twice:
  xxx_file_write_iter -> check O_DIRECT, and perform some optimization
    ->__generic_file_write_iter -> check O_DIRECT,
which may break things: for example http://www.spinics.net/lists/linux-ext4/msg45683.html
For that reason some filesystems simply do not use __generic_file_write_iter()
which result in code duplication. Right way to fix this is to save volatile flags
inside kiocb->ki_flags similar to ->ki_pos
Other private discussion: message-id:20141218105101.GD13705@quack.suse.cz

This patch store O_DIRECT|O_APPEND|O_NONBLOCK|O_NDELAY
to kiocb->ki_flags on kiocb initialization.

Signed-off-by: Dmitry Monakhov <dmonakhov@openvz.org>
---
 fs/aio.c           |    7 ++++---
 fs/read_write.c    |   20 ++++++++++++++++++++
 include/linux/fs.h |   30 +++++++++++++++++++++++++++---
 3 files changed, 51 insertions(+), 6 deletions(-)

diff --git a/fs/aio.c b/fs/aio.c
index 3b8467a..f58c4d6 100644
--- a/fs/aio.c
+++ b/fs/aio.c
@@ -1482,6 +1482,7 @@ static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
 			 struct iocb *iocb, bool compat)
 {
 	struct aio_kiocb *req;
+	struct file* filp;
 	ssize_t ret;
 
 	/* enforce forwards compatibility on users */
@@ -1504,14 +1505,14 @@ static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
 	if (unlikely(!req))
 		return -EAGAIN;
 
-	req->common.ki_filp = fget(iocb->aio_fildes);
-	if (unlikely(!req->common.ki_filp)) {
+	filp = fget(iocb->aio_fildes);
+	if (unlikely(!filp)) {
 		ret = -EBADF;
 		goto out_put_req;
 	}
+	kiocb_init_file(&req->common, filp);
 	req->common.ki_pos = iocb->aio_offset;
 	req->common.ki_complete = aio_complete;
-	req->common.ki_flags = 0;
 
 	if (iocb->aio_flags & IOCB_FLAG_RESFD) {
 		/*
diff --git a/fs/read_write.c b/fs/read_write.c
index 69128b3..00e1ca4 100644
--- a/fs/read_write.c
+++ b/fs/read_write.c
@@ -41,6 +41,26 @@ static inline int unsigned_offsets(struct file *file)
 	return file->f_mode & FMODE_UNSIGNED_OFFSET;
 }
 
+void kiocb_init_file(struct kiocb *kiocb, struct file *filp)
+{
+	kiocb->ki_flags = 0;
+	kiocb->ki_filp = filp;
+
+	/* Socket aio */
+	if (kiocb->ki_filp == NULL)
+		return;
+
+	if (filp->f_flags & O_APPEND)
+		kiocb->ki_flags |= IOCB_APPEND;
+	if (filp->f_flags & O_NONBLOCK)
+		kiocb->ki_flags |= IOCB_NONBLOCK;
+	if (filp->f_flags & O_NDELAY)
+		kiocb->ki_flags |= IOCB_NDELAY;
+	if (filp->f_flags & O_DIRECT)
+		kiocb->ki_flags |= IOCB_DIRECT;
+}
+EXPORT_SYMBOL(kiocb_init_file);
+
 /**
  * vfs_setpos - update the file offset for lseek
  * @file:	file structure in question
diff --git a/include/linux/fs.h b/include/linux/fs.h
index dfbd88a..4c20030 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -315,6 +315,10 @@ struct address_space;
 struct writeback_control;
 
 #define IOCB_EVENTFD		(1 << 0)
+#define IOCB_APPEND		(1 << 1)
+#define IOCB_NONBLOCK		(1 << 2)
+#define IOCB_NDELAY		(1 << 3)
+#define IOCB_DIRECT		(1 << 4)
 
 struct kiocb {
 	struct file		*ki_filp;
@@ -329,11 +333,11 @@ static inline bool is_sync_kiocb(struct kiocb *kiocb)
 	return kiocb->ki_complete == NULL;
 }
 
+extern void kiocb_init_file(struct kiocb *kiocb, struct file *filp);
 static inline void init_sync_kiocb(struct kiocb *kiocb, struct file *filp)
 {
-	*kiocb = (struct kiocb) {
-		.ki_filp = filp,
-	};
+	memset(kiocb, 0 , sizeof(*kiocb));
+	kiocb_init_file(kiocb, filp);
 }
 
 /*
@@ -2776,6 +2780,26 @@ extern int generic_show_options(struct seq_file *m, struct dentry *root);
 extern void save_mount_options(struct super_block *sb, char *options);
 extern void replace_mount_options(struct super_block *sb, char *options);
 
+static inline bool is_append_kiocb(struct kiocb *kiocb)
+{
+	return kiocb->ki_flags & IOCB_APPEND;
+}
+
+static inline bool is_direct_kiocb(struct kiocb *kiocb)
+{
+	return (kiocb->ki_flags & IOCB_DIRECT) |
+		IS_DAX(file_inode(kiocb->ki_filp));
+
+}
+
+
+static inline bool is_nonblock_kiocb(struct kiocb *kiocb)
+{
+	return kiocb->ki_flags & IOCB_NONBLOCK;
+}
+
+/* XXX: this is obsolete helper, and will be removed soon.
+ * One should use io_direct_kiocb() instead */
 static inline bool io_is_direct(struct file *filp)
 {
 	return (filp->f_flags & O_DIRECT) || IS_DAX(file_inode(filp));
-- 
1.7.1


  reply	other threads:[~2015-04-04 19:13 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-04 19:13 [PATCH 00/16] fs: fixup racy check file->f_flags for xxx_write_iter Dmitry Monakhov
2015-04-04 19:13 ` Dmitry Monakhov [this message]
2015-04-04 19:13 ` [PATCH 02/16] vfs: check kiocb->ki_flags instead filp->fl_flags Dmitry Monakhov
2015-04-04 21:36   ` Al Viro
2015-04-05 11:03     ` Dmitry Monakhov
2015-04-05 18:11       ` Al Viro
2015-04-05 21:54         ` Al Viro
2015-04-04 19:13 ` [PATCH 03/16] ext4: use is_xxx_kiocb instead of filp->fl_flags Dmitry Monakhov
2015-04-04 19:13 ` [PATCH 04/16] 9p: " Dmitry Monakhov
2015-04-04 19:13 ` [PATCH 05/16] btrfs: " Dmitry Monakhov
2015-04-04 19:13 ` [PATCH 06/16] ceph: " Dmitry Monakhov
2015-04-04 19:13 ` [PATCH 07/16] cifs: " Dmitry Monakhov
2015-04-04 19:13 ` [PATCH 08/16] gfs2: " Dmitry Monakhov
2015-04-07 13:11   ` [Cluster-devel] " Steven Whitehouse
2015-04-04 19:13 ` [PATCH 09/16] nfs: " Dmitry Monakhov
2015-04-04 19:13 ` [PATCH 10/16] ntfs: " Dmitry Monakhov
2015-04-04 19:13 ` [PATCH 11/16] ocfs2: " Dmitry Monakhov
2015-04-04 19:13 ` [PATCH 12/16] udf: " Dmitry Monakhov
2015-04-04 19:13 ` [PATCH 13/16] xfs: " Dmitry Monakhov
2015-04-04 19:13 ` [PATCH 14/16] fuse: " Dmitry Monakhov
2015-04-04 19:13 ` [PATCH 15/16] pipe: " Dmitry Monakhov
2015-04-04 19:13 ` [PATCH 16/16] splice: fix race beween splice_write vs fcntl(,F_SETFL,) Dmitry Monakhov

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=1428174805-853-2-git-send-email-dmonakhov@openvz.org \
    --to=dmonakhov@openvz.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /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;
as well as URLs for NNTP newsgroup(s).