[PATCH 01/16] fs: save file->f_flags to kiocb->ki_flags

From: Dmitry Monakhov
Date: Sat Apr 04 2015 - 15:13:47 EST


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@xxxxxxxxxxxxx

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

Signed-off-by: Dmitry Monakhov <dmonakhov@xxxxxxxxxx>
---
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

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/