Unaligned DIO read error path fix and clean ups
v2 cover Unaligned DIO read error path fix and clean ups
Hi,
This follows up on my previous submision of "direct-io: defer alignment check until after the EOF check". This version applies the suggestions from Jan Kara (thank you!) and it was tested with xfstests aio group over f2fs and fio workloads.
Gabriel Krisman Bertazi (3):
direct-io: clean up error paths of do_blockdev_direct_IO
direct-io: don't force writeback for reads beyond EOF
direct-io: defer alignment check until after the EOF check
fs/direct-io.c | 64 ++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 33 deletions(-)
Signature · 1 line
2.28.0
PATCH v2 1/3 direct-io: clean up error paths of do_blockdev_direct_IO
In preparation to resort DIO checks, reduce code duplication of error handling in do_blockdev_direct_IO.
--- fs/direct-io.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-)
fs/direct-io.c +15 -15
@@ -1170,7 +1170,7 @@ do_blockdev_direct_IO(struct kiocb *iocb, struct inode *inode, blkbits =blksize_bits (bdev_logical_block_size (bdev)); blocksize_mask = (1 << blkbits) -1 ;if (align & blocksize_mask) - goto out; +return -EINVAL ; }/* watch out for a 0 len io from a tricksy fs */
@@ -1178,9 +1178,8 @@ do_blockdev_direct_IO(struct kiocb *iocb, struct inode *inode,return 0 ; dio =kmem_cache_alloc (dio_cache,GFP_KERNEL ); - retval = -ENOMEM ;if (!dio) - goto out; +return -ENOMEM ;/* * Believe it or not, zeroing out the page array caused a .5% * performance regression in a database benchmark. So, we take
@@ -1199,22 +1198,16 @@ do_blockdev_direct_IO(struct kiocb *iocb, struct inode *inode, retval =filemap_write_and_wait_range (mapping, offset, end -1 ); -if (retval) { -inode_unlock (inode); -kmem_cache_free (dio_cache, dio); - goto out; - } +if (retval) + goto fail_dio; } }/* Once we sampled i_size check for reads beyond EOF */ dio->i_size =i_size_read (inode);if (iov_iter_rw (iter) ==READ && offset >= dio->i_size) { -if (dio->flags &DIO_LOCKING ) -inode_unlock (inode); -kmem_cache_free (dio_cache, dio); retval =0 ; - goto out; + goto fail_dio; }/*
@@ -1263,8 +1256,7 @@ do_blockdev_direct_IO(struct kiocb *iocb, struct inode *inode,* We grab i_mutex only for reads so we don't have * to release it here */ -kmem_cache_free (dio_cache, dio); - goto out; + goto fail_dio_unlocked; } }
@@ -1368,7 +1360,15 @@ do_blockdev_direct_IO(struct kiocb *iocb, struct inode *inode, }else BUG_ON (retval != -EIOCBQUEUED ); -out: +return retval; + +fail_dio: +if (dio->flags &DIO_LOCKING ) { +inode_unlock (inode); + } +fail_dio_unlocked: +kmem_cache_free (dio_cache, dio); +return retval; }
Signature · 1 line
2.28.0
Jan Kara
reviewerRe: [PATCH v2 1/3] direct-io: clean up error paths of do_blockdev_direct_IO
Quoted 4 lines · Gabriel Krisman Bertazi
In preparation to resort DIO checks, reduce code duplication of error handling in
do_blockdev_direct_IO.Signed-off-by Gabriel Krisman Bertazi <[email protected]>
Two comments below:
fs/direct-io.c do_blockdev_direct_IO(struct kiocb *iocb, struct inode *inode, · line 1368
Quoted 11 lines
fs/direct-io.c +6
-1@@ -1368,7 +1360,15 @@ do_blockdev_direct_IO(struct kiocb *iocb, struct inode *inode, }else BUG_ON (retval != -EIOCBQUEUED ); -out: +return retval; + +fail_dio: +if (dio->flags &DIO_LOCKING ) { +inode_unlock (inode); + }
No need for braces here. Also please add '&& iov_iter_rw(iter) == READ' to the condition for unlocking to make fail_dio safe also for writes. Currently you jump to fail_dio only for reads but in 3/3 you can jump to it also for writes and that is a bug.
Honza
Quoted 9 lines
fs/direct-io.c +3
-0+fail_dio_unlocked: +kmem_cache_free (dio_cache, dio); +return retval; }Signature · 1 line
2.28.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
PATCH v2 2/3 direct-io: don't force writeback for reads beyond EOF
In reply to Gabriel Krisman BertaziIf a DIO read starts past EOF, the kernel won't attempt it, so we don't need to flush dirty pages before failing the syscall.
--- fs/direct-io.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-)
fs/direct-io.c +11 -13
@@ -1188,19 +1188,9 @@ do_blockdev_direct_IO(struct kiocb *iocb, struct inode *inode,memset (dio,0 , offsetof(struct dio , pages)); dio->flags = flags; -if (dio->flags &DIO_LOCKING ) { -if (iov_iter_rw (iter) ==READ ) { -struct address_space *mapping = - iocb->ki_filp->f_mapping; - -/* will be released by direct_io_worker */ -inode_lock (inode); - - retval =filemap_write_and_wait_range (mapping, offset, - end -1 ); -if (retval) - goto fail_dio; - } +if (dio->flags &DIO_LOCKING &&iov_iter_rw (iter) ==READ ) { +/* will be released by direct_io_worker */ +inode_lock (inode); }/* Once we sampled i_size check for reads beyond EOF */
@@ -1210,6 +1200,14 @@ do_blockdev_direct_IO(struct kiocb *iocb, struct inode *inode, goto fail_dio; } +if (dio->flags &DIO_LOCKING &&iov_iter_rw (iter) ==READ ) { +struct address_space *mapping = iocb->ki_filp->f_mapping; + + retval =filemap_write_and_wait_range (mapping, offset, end -1 ); +if (retval) + goto fail_dio; + } +/* * For file extending writes updating i_size before data writeouts * complete can expose uninitialized blocks in dumb filesystems.
Signature · 1 line
2.28.0
Re: [PATCH v2 2/3] direct-io: don't force writeback for reads beyond EOF
Quoted 5 lines · Gabriel Krisman Bertazi
If a DIO read starts past EOF, the kernel won't attempt it, so we don't need to flush dirty pages before failing the syscall.
Suggested-by Jan Kara <[email protected]>Signed-off-by Gabriel Krisman Bertazi <[email protected]>
This patch looks good to me. You can add:
Honza
Quoted 49 lines
--- fs/direct-io.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-)fs/direct-io.c +11
-13@@ -1188,19 +1188,9 @@ do_blockdev_direct_IO(struct kiocb *iocb, struct inode *inode,memset (dio,0 , offsetof(struct dio , pages)); dio->flags = flags; -if (dio->flags &DIO_LOCKING ) { -if (iov_iter_rw (iter) ==READ ) { -struct address_space *mapping = - iocb->ki_filp->f_mapping; - -/* will be released by direct_io_worker */ -inode_lock (inode); - - retval =filemap_write_and_wait_range (mapping, offset, - end -1 ); -if (retval) - goto fail_dio; - } +if (dio->flags &DIO_LOCKING &&iov_iter_rw (iter) ==READ ) { +/* will be released by direct_io_worker */ +inode_lock (inode); }/* Once we sampled i_size check for reads beyond EOF */ @@ -1210,6 +1200,14 @@ do_blockdev_direct_IO(struct kiocb *iocb, struct inode *inode, goto fail_dio; } +if (dio->flags &DIO_LOCKING &&iov_iter_rw (iter) ==READ ) { +struct address_space *mapping = iocb->ki_filp->f_mapping; + + retval =filemap_write_and_wait_range (mapping, offset, end -1 ); +if (retval) + goto fail_dio; + } +/* * For file extending writes updating i_size before data writeouts * complete can expose uninitialized blocks in dumb filesystems. Signature · 1 line
2.28.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
PATCH v2 3/3 direct-io: defer alignment check until after the EOF check
In reply to Gabriel Krisman BertaziPrior to commit 9fe55eea7e4b ("Fix race when checking i_size on direct i/o read"), an unaligned direct read past end of file would trigger EOF, since generic_file_aio_read detected this read-at-EOF condition and skipped the direct IO read entirely, returning 0. After that change, the read now reaches dio_generic, which detects the misalignment and returns EINVAL.
This consolidates the generic direct-io to follow the same behavior of filesystems. Apparently, this fix will only affect ocfs2 since other filesystems do this verification before calling do_blockdev_direct_IO, with the exception of f2fs, which has the same bug, but is fixed in the next patch.
it can be verified by a read loop on a file that does a partial read before EOF (On file that doesn't end at an aligned address). The following code fails on an unaligned file on filesystems without prior validation without this patch, but not on btrfs, ext4, and xfs.
while (done < total) {
ssize_t delta = pread(fd, buf + done, total - done, off + done);
if (!delta)
break;
...
}Fix this regression by moving the misalignment check to after the EOF check added by commit 74cedf9b6c60 ("direct-io: Fix negative return from dio read beyond eof").
Based on a patch by Jamie Liu.
--- fs/direct-io.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
fs/direct-io.c +8 -8
@@ -1165,14 +1165,6 @@ do_blockdev_direct_IO(struct kiocb *iocb, struct inode *inode,* the early prefetch in the caller enough time. */ -if (align & blocksize_mask) { -if (bdev) - blkbits =blksize_bits (bdev_logical_block_size (bdev)); - blocksize_mask = (1 << blkbits) -1 ; -if (align & blocksize_mask) -return -EINVAL ; - } -/* watch out for a 0 len io from a tricksy fs */ if (iov_iter_rw (iter) ==READ && !count)return 0 ;
@@ -1200,6 +1192,14 @@ do_blockdev_direct_IO(struct kiocb *iocb, struct inode *inode, goto fail_dio; } +if (align & blocksize_mask) { +if (bdev) + blkbits =blksize_bits (bdev_logical_block_size (bdev)); + blocksize_mask = (1 << blkbits) -1 ; +if (align & blocksize_mask) + goto fail_dio; + } +if (dio->flags &DIO_LOCKING &&iov_iter_rw (iter) ==READ ) {struct address_space *mapping = iocb->ki_filp->f_mapping;
Signature · 1 line
2.28.0
Jan Kara
reviewerRe: [PATCH v2 3/3] direct-io: defer alignment check until after the EOF check
Quoted 25 lines · Gabriel Krisman Bertazi
Prior to commit
9fe55eea7e4b("Fix race when checkingi_sizeon direct i/o read"), an unaligned direct read past end of file would trigger EOF, sincegeneric_file_aio_readdetected this read-at-EOF condition and skipped the direct IO read entirely, returning 0. After that change, the read now reachesdio_generic, which detects the misalignment and returns EINVAL.This consolidates the generic direct-io to follow the same behavior of filesystems. Apparently, this fix will only affect ocfs2 since other filesystems do this verification before calling
do_blockdev_direct_IO, with the exception of f2fs, which has the same bug, but is fixed in the next patch.it can be verified by a read loop on a file that does a partial read before EOF (On file that doesn't end at an aligned address). The following code fails on an unaligned file on filesystems without prior validation without this patch, but not on btrfs, ext4, and xfs.
while (done < total) { ssize_t delta = pread(fd, buf + done, total - done, off + done); if (!delta) break; ... }
Quoted 8 lines
Fix this regression by moving the misalignment check to after the EOF check added by commit
74cedf9b6c60("direct-io: Fix negative return from dio read beyond eof").Based on a patch by Jamie Liu.
Reported-by Jamie Liu <[email protected]>Signed-off-by Gabriel Krisman Bertazi <[email protected]>
This patch also looks good except for the fail_dio jump...
Honza
Quoted 41 lines
--- fs/direct-io.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)fs/direct-io.c +8
-8@@ -1165,14 +1165,6 @@ do_blockdev_direct_IO(struct kiocb *iocb, struct inode *inode,* the early prefetch in the caller enough time. */ -if (align & blocksize_mask) { -if (bdev) - blkbits =blksize_bits (bdev_logical_block_size (bdev)); - blocksize_mask = (1 << blkbits) -1 ; -if (align & blocksize_mask) -return -EINVAL ; - } -/* watch out for a 0 len io from a tricksy fs */ if (iov_iter_rw (iter) ==READ && !count)return 0 ;@@ -1200,6 +1192,14 @@ do_blockdev_direct_IO(struct kiocb *iocb, struct inode *inode, goto fail_dio; } +if (align & blocksize_mask) { +if (bdev) + blkbits =blksize_bits (bdev_logical_block_size (bdev)); + blocksize_mask = (1 << blkbits) -1 ; +if (align & blocksize_mask) + goto fail_dio; + } +if (dio->flags &DIO_LOCKING &&iov_iter_rw (iter) ==READ ) {struct address_space *mapping = iocb->ki_filp->f_mapping;Signature · 1 line
2.28.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR