an iomap-based direct I/O implementation V4
Subsystems (4)
Hi all,
this series adds a new direct I/O implementation based on the iomap interface, and switches XFS to use it.
The first two patches are a resend of my earlier series to remove the XFS iolock. They are needed for the lockdep assert in the new iomap code.
The rest implements a new iomap_dio_rw direct I/O implementation and switches XFS to use it.
It requires a patch from the block tree which we'll need to cherry-pick into the XFS tree to avoid pulling in the whole block tree:
9cd56d91 ("block: add bio_iov_iter_get_pages()")
To make everyones lifer easie I also have a git tree that includes this cherry pick:
git://git.infradead.org/users/hch/vfs.git iomap-dio.4
Note that with this patch I see somewhat increased failure rates on generic/209. This is not because of any locking or invalidation changes in this series, but just because it's always been fundamentally race, and the new direct I/O code is a lot faster, which causes the direct writes in this test to race significantly more often with readahead.
Changes since V3:
- untangle from the block tree
Changes since V2:
- added a few comments to unconfuse reviewers
- added a iomap_dio_set_error helper
- dropped support for not filling holes on writes
- pulled in a more recent block tree (blk_poll -> blk_mq_poll)
Changes since V1:
- dropped the bio_iov_iter_get_pages patch, now merged in the block tree
- rebased on top of a merge of the XFS tree with the block tree
- stopped using WRITE_ODIRECT which has been removed in the block tree
PATCH 1/5 locking/lockdep: Provide a type check for lock_is_held
From: Peter Zijlstra <[email protected]>
Christoph requested lockdep_assert_held() variants that distinguish between held-for-read or held-for-write.
Provide:
int lock_is_held_type(struct lockdep_map *lock, int read)
which takes the same argument as lock_acquire(.read) and matches it to the held_lock instance.
Use of this function should be gated by the debug_locks variable. When that is 0 the return value of the lock_is_held_type() function is undefined. This is done to allow both negative and positive tests for holding locks.
By default we provide (positive) lockdep_assert_held{,_exclusive,_read}() macros.
--- include/linux/lockdep.h | 25 +++++++++++++++++++++++-- kernel/locking/lockdep.c | 20 ++++++++++++-------- 2 files changed, 35 insertions(+), 10 deletions(-)
include/linux/lockdep.h +23 -2
@@ -338,9 +338,18 @@ extern void lock_acquire(struct lockdep_map *lock, unsigned int subclass,extern void lock_release (struct lockdep_map *lock,int nested,unsigned long ip); -#define lockdep_is_held (lock) lock_is_held(&(lock)->dep_map) +/* +* Same "read" as for lock_acquire(), except -1 means any. +*/ +extern int lock_is_held_type (struct lockdep_map *lock,int read); + +static inline int lock_is_held (struct lockdep_map *lock) +{ +return lock_is_held_type (lock,-1 ); +} -extern int lock_is_held (struct lockdep_map *lock); +#define lockdep_is_held (lock) lock_is_held(&(lock)->dep_map) +#define lockdep_is_held_type (lock, r) lock_is_held_type(&(lock)->dep_map, (r)) extern void lock_set_class(struct lockdep_map *lock, const char *name, struct lock_class_key *key, unsigned int subclass,
@@ -372,6 +381,14 @@ extern void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie);WARN_ON (debug_locks && !lockdep_is_held (l)); \ } while (0) +#define lockdep_assert_held_exclusive (l) do { \ + WARN_ON(debug_locks && !lockdep_is_held_type(l, 0)); \ + } while (0) + +#define lockdep_assert_held_read (l) do { \ + WARN_ON(debug_locks && !lockdep_is_held_type(l, 1)); \ + } while (0) +#define lockdep_assert_held_once (l) do { \ WARN_ON_ONCE(debug_locks && !lockdep_is_held(l)); \ } while (0)
@@ -428,7 +445,11 @@ struct lock_class_key { };#define lockdep_depth (tsk) (0) +#define lockdep_is_held_type (l, r) (1) +#define lockdep_assert_held (l) do { (void)(l); } while (0) +#define lockdep_assert_held_exclusive (l) do { (void)(l); } while (0) +#define lockdep_assert_held_read (l) do { (void)(l); } while (0)#define lockdep_assert_held_once (l) do { (void)(l); } while (0)#define lockdep_recursing (tsk) (0)
kernel/locking/lockdep.c +12 -8
@@ -3188,7 +3188,7 @@ print_lock_nested_lock_not_held(struct task_struct *curr,return 0 ; } -static int __lock_is_held (struct lockdep_map *lock); +static int __lock_is_held (struct lockdep_map *lock,int read);/* * This gets called for every mutex_lock*()/spin_lock*() operation.
@@ -3329,7 +3329,7 @@ static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass, } chain_key =iterate_chain_key (chain_key, class_idx); -if (nest_lock && !__lock_is_held (nest_lock)) +if (nest_lock && !__lock_is_held (nest_lock,-1 ))return print_lock_nested_lock_not_held (curr, hlock, ip);if (!validate_chain (curr, lock, hlock, chain_head, chain_key))
@@ -3576,7 +3576,7 @@ __lock_release(struct lockdep_map *lock, int nested, unsigned long ip)return 1 ; } -static int __lock_is_held (struct lockdep_map *lock) +static int __lock_is_held (struct lockdep_map *lock,int read) {struct task_struct *curr = current;int i;
@@ -3584,8 +3584,12 @@ static int __lock_is_held(struct lockdep_map *lock)for (i =0 ; i < curr->lockdep_depth; i++) {struct held_lock *hlock = curr->held_locks + i; -if (match_held_lock (hlock, lock)) -return 1 ; +if (match_held_lock (hlock, lock)) { +if (read ==-1 || hlock->read == read) +return 1 ; + +return 0 ; + } }return 0 ;
@@ -3769,7 +3773,7 @@ void lock_release(struct lockdep_map *lock, int nested, }EXPORT_SYMBOL_GPL (lock_release); -int lock_is_held (struct lockdep_map *lock) +int lock_is_held_type (struct lockdep_map *lock,int read) {unsigned long flags;int ret =0 ;
@@ -3781,13 +3785,13 @@ int lock_is_held(struct lockdep_map *lock)check_flags (flags); current->lockdep_recursion =1 ; - ret =__lock_is_held (lock); + ret =__lock_is_held (lock, read); current->lockdep_recursion =0 ;raw_local_irq_restore (flags);return ret; } -EXPORT_SYMBOL_GPL (lock_is_held); +EXPORT_SYMBOL_GPL (lock_is_held_type);struct pin_cookie lock_pin_lock (struct lockdep_map *lock) {
Signature · 1 line
2.1.4
PATCH 2/5 xfs: remove i_iolock and use i_rwsem in the VFS inode instead
In reply to Christoph HellwigThis patch drops the XFS-own i_iolock and uses the VFS i_rwsem which recently replaced i_mutex instead. This means we only have to take one looks instead of two in many fast path operations, and we can also shrink the xfs_inode structure. Thanks to the xfs_ilock family there is very little churn, the only thing of note is that we need to switch to use the lock_two_directory helper for taking the i_rwsem on two inodes in a few places to make sure our lock order matches the one used in the VFS.
--- fs/xfs/xfs_aops.c | 7 ++-- fs/xfs/xfs_bmap_util.c | 12 +++---- fs/xfs/xfs_dir2_readdir.c | 2 -- fs/xfs/xfs_file.c | 79 +++++++++++++-------------------------------- fs/xfs/xfs_icache.c | 6 ++-- fs/xfs/xfs_inode.c | 82 +++++++++++++++++++---------------------------- fs/xfs/xfs_inode.h | 7 ++-- fs/xfs/xfs_ioctl.c | 2 +- fs/xfs/xfs_iops.c | 14 ++++---- fs/xfs/xfs_pnfs.c | 7 +--- fs/xfs/xfs_pnfs.h | 4 +-- fs/xfs/xfs_reflink.c | 14 +++----- fs/xfs/xfs_super.c | 2 +- fs/xfs/xfs_symlink.c | 7 ++-- 14 files changed, 86 insertions(+), 159 deletions(-)
fs/xfs/xfs_aops.c +2 -5
@@ -1585,7 +1585,6 @@ xfs_vm_bmap(struct xfs_inode *ip =XFS_I (inode);trace_xfs_vm_bmap (XFS_I (inode)); -xfs_ilock (ip,XFS_IOLOCK_SHARED );/* * The swap code (ab-)uses ->bmap to get a block mapping and then
@@ -1593,12 +1592,10 @@ xfs_vm_bmap(* that on reflinks inodes, so we have to skip out here. And yes, * 0 is the magic code for a bmap error.. */ -if (xfs_is_reflink_inode (ip)) { -xfs_iunlock (ip,XFS_IOLOCK_SHARED ); +if (xfs_is_reflink_inode (ip))return 0 ; - } +filemap_write_and_wait (mapping); -xfs_iunlock (ip,XFS_IOLOCK_SHARED );return generic_block_bmap (mapping, block, xfs_get_blocks); }
fs/xfs/xfs_bmap_util.c +5 -7
@@ -1935,8 +1935,8 @@ xfs_swap_extents(* page cache safely. Once we have done this we can take the ilocks and * do the rest of the checks. */ - lock_flags =XFS_IOLOCK_EXCL |XFS_MMAPLOCK_EXCL ; -xfs_lock_two_inodes (ip, tip,XFS_IOLOCK_EXCL ); +lock_two_nondirectories (VFS_I (ip),VFS_I (tip)); + lock_flags =XFS_MMAPLOCK_EXCL ;xfs_lock_two_inodes (ip, tip,XFS_MMAPLOCK_EXCL );/* Verify that both files have the same format */
@@ -2076,15 +2076,13 @@ xfs_swap_extents(trace_xfs_swap_extent_after (ip,0 );trace_xfs_swap_extent_after (tip,1 ); +out_unlock:xfs_iunlock (ip, lock_flags);xfs_iunlock (tip, lock_flags); +unlock_two_nondirectories (VFS_I (ip),VFS_I (tip));return error; out_trans_cancel:xfs_trans_cancel (tp); - -out_unlock: -xfs_iunlock (ip, lock_flags); -xfs_iunlock (tip, lock_flags); -return error; + goto out_unlock; }
fs/xfs/xfs_dir2_readdir.c +0 -2
@@ -677,7 +677,6 @@ xfs_readdir( args.dp = dp; args.geo = dp->i_mount->m_dir_geo; -xfs_ilock (dp,XFS_IOLOCK_SHARED );if (dp->i_d.di_format ==XFS_DINODE_FMT_LOCAL ) rval =xfs_dir2_sf_getdents (&args, ctx);else if ((rval =xfs_dir2_isblock (&args, &v)))
@@ -686,7 +685,6 @@ xfs_readdir( rval =xfs_dir2_block_getdents (&args, ctx); else rval =xfs_dir2_leaf_getdents (&args, ctx, bufsize); -xfs_iunlock (dp,XFS_IOLOCK_SHARED );return rval; }
fs/xfs/xfs_file.c +23 -56
183 lines not on this page · raw message
fs/xfs/xfs_icache.c +2 -4
@@ -70,8 +70,6 @@ xfs_inode_alloc(ASSERT (!xfs_isiflocked (ip));ASSERT (ip->i_ino ==0 ); -mrlock_init (&ip->i_iolock,MRLOCK_BARRIER ,"xfsio" , ip->i_ino); -/* initialise the xfs inode */ ip->i_ino = ino; ip->i_mount = mp;
@@ -394,8 +392,8 @@ xfs_iget_cache_hit(xfs_inode_clear_reclaim_tag (pag, ip->i_ino); inode->i_state =I_NEW ; -ASSERT (!rwsem_is_locked (&ip->i_iolock.mr_lock)); -mrlock_init (&ip->i_iolock,MRLOCK_BARRIER ,"xfsio" , ip->i_ino); +ASSERT (!rwsem_is_locked (&inode->i_rwsem)); +init_rwsem (&inode->i_rwsem);spin_unlock (&ip->i_flags_lock);spin_unlock (&pag->pag_ici_lock);
fs/xfs/xfs_inode.c +33 -49
231 lines not on this page · raw message
fs/xfs/xfs_inode.h +2 -5
@@ -56,7 +56,6 @@ typedef struct xfs_inode {/* Transaction and locking information. */ struct xfs_inode_log_item *i_itemp;/* logging information */ mrlock_t i_lock;/* inode lock */ -mrlock_t i_iolock;/* inode IO lock */ mrlock_t i_mmaplock;/* inode mmap IO lock */ atomic_t i_pincount;/* inode pin count */ spinlock_t i_flags_lock;/* inode i_flags lock */
@@ -333,7 +332,7 @@ static inline void xfs_ifunlock(struct xfs_inode *ip)* IOLOCK values * * 0-3 subclass value -* 4-7 PARENT subclass values +* 4-7 unused * * MMAPLOCK values *
@@ -348,10 +347,8 @@ static inline void xfs_ifunlock(struct xfs_inode *ip)* */ #define XFS_IOLOCK_SHIFT 16 -#define XFS_IOLOCK_PARENT_VAL 4 -#define XFS_IOLOCK_MAX_SUBCLASS (XFS_IOLOCK_PARENT_VAL - 1) +#define XFS_IOLOCK_MAX_SUBCLASS 3#define XFS_IOLOCK_DEP_MASK 0x000f0000 -#define XFS_IOLOCK_PARENT (XFS_IOLOCK_PARENT_VAL << XFS_IOLOCK_SHIFT)#define XFS_MMAPLOCK_SHIFT 20#define XFS_MMAPLOCK_NUMORDER 0
fs/xfs/xfs_ioctl.c +1 -1
@@ -639,7 +639,7 @@ xfs_ioc_space(return error;xfs_ilock (ip, iolock); - error =xfs_break_layouts (inode, &iolock, false); + error =xfs_break_layouts (inode, &iolock);if (error) goto out_unlock;
fs/xfs/xfs_iops.c +6 -8
@@ -983,15 +983,13 @@ xfs_vn_setattr(struct xfs_inode *ip =XFS_I (d_inode (dentry));uint iolock =XFS_IOLOCK_EXCL ; -xfs_ilock (ip, iolock); - error =xfs_break_layouts (d_inode (dentry), &iolock, true); -if (!error) { -xfs_ilock (ip,XFS_MMAPLOCK_EXCL ); - iolock |=XFS_MMAPLOCK_EXCL ; + error =xfs_break_layouts (d_inode (dentry), &iolock); +if (error) +return error; - error =xfs_vn_setattr_size (dentry, iattr); - } -xfs_iunlock (ip, iolock); +xfs_ilock (ip,XFS_MMAPLOCK_EXCL ); + error =xfs_setattr_size (ip, iattr); +xfs_iunlock (ip,XFS_MMAPLOCK_EXCL ); }else { error =xfs_vn_setattr_nonsize (dentry, iattr); }
fs/xfs/xfs_pnfs.c +1 -6
@@ -32,8 +32,7 @@int xfs_break_layouts (struct inode *inode, -uint *iolock, -bool with_imutex) +uint *iolock) {struct xfs_inode *ip =XFS_I (inode);int error;
@@ -42,12 +41,8 @@ xfs_break_layouts(while ((error =break_layout (inode, false) == -EWOULDBLOCK )) {xfs_iunlock (ip, *iolock); -if (with_imutex && (*iolock &XFS_IOLOCK_EXCL )) -inode_unlock (inode); error =break_layout (inode, true); *iolock =XFS_IOLOCK_EXCL ; -if (with_imutex) -inode_lock (inode);xfs_ilock (ip, *iolock); }
fs/xfs/xfs_pnfs.h +2 -2
@@ -8,10 +8,10 @@ int xfs_fs_map_blocks(struct inode *inode, loff_t offset, u64 length,int xfs_fs_commit_blocks (struct inode *inode,struct iomap *maps,int nr_maps,struct iattr *iattr); -int xfs_break_layouts (struct inode *inode,uint *iolock,bool with_imutex); +int xfs_break_layouts (struct inode *inode,uint *iolock);#else static inline int -xfs_break_layouts (struct inode *inode,uint *iolock,bool with_imutex) +xfs_break_layouts (struct inode *inode,uint *iolock) {return 0 ; }
fs/xfs/xfs_reflink.c +5 -9
@@ -1251,13 +1251,11 @@ xfs_reflink_remap_range(return -EIO ;/* Lock both files against IO */ -if (same_inode) { -xfs_ilock (src,XFS_IOLOCK_EXCL ); +lock_two_nondirectories (inode_in, inode_out); +if (same_inode)xfs_ilock (src,XFS_MMAPLOCK_EXCL ); - }else { -xfs_lock_two_inodes (src, dest,XFS_IOLOCK_EXCL ); +else xfs_lock_two_inodes (src, dest,XFS_MMAPLOCK_EXCL ); - }/* Don't touch certain kinds of inodes */ ret = -EPERM ;
@@ -1402,11 +1400,9 @@ xfs_reflink_remap_range( out_unlock:xfs_iunlock (src,XFS_MMAPLOCK_EXCL ); -xfs_iunlock (src,XFS_IOLOCK_EXCL ); -if (src->i_ino != dest->i_ino) { +if (!same_inode)xfs_iunlock (dest,XFS_MMAPLOCK_EXCL ); -xfs_iunlock (dest,XFS_IOLOCK_EXCL ); - } +unlock_two_nondirectories (inode_in, inode_out);if (ret)trace_xfs_reflink_remap_range_error (dest, ret, _RET_IP_);return ret;
fs/xfs/xfs_super.c +1 -1
@@ -943,7 +943,7 @@ xfs_fs_destroy_inode(trace_xfs_destroy_inode (ip); -ASSERT (!rwsem_is_locked (&ip->i_iolock.mr_lock)); +ASSERT (!rwsem_is_locked (&inode->i_rwsem));XFS_STATS_INC (ip->i_mount, vn_rele);XFS_STATS_INC (ip->i_mount, vn_remove);
fs/xfs/xfs_symlink.c +3 -4
@@ -238,8 +238,7 @@ xfs_symlink(if (error) goto out_release_inode; -xfs_ilock (dp,XFS_IOLOCK_EXCL |XFS_ILOCK_EXCL | -XFS_IOLOCK_PARENT |XFS_ILOCK_PARENT ); +xfs_ilock (dp,XFS_ILOCK_EXCL |XFS_ILOCK_PARENT ); unlock_dp_on_error = true;/*
@@ -287,7 +286,7 @@ xfs_symlink(* the transaction cancel unlocking dp so don't do it explicitly in the * error path. */ -xfs_trans_ijoin (tp, dp,XFS_IOLOCK_EXCL |XFS_ILOCK_EXCL ); +xfs_trans_ijoin (tp, dp,XFS_ILOCK_EXCL ); unlock_dp_on_error = false;/*
@@ -412,7 +411,7 @@ xfs_symlink(xfs_qm_dqrele (pdqp);if (unlock_dp_on_error) -xfs_iunlock (dp,XFS_IOLOCK_EXCL |XFS_ILOCK_EXCL ); +xfs_iunlock (dp,XFS_ILOCK_EXCL );return error; }
Signature · 1 line
2.1.4
PATCH 3/5 fs: make sb_init_dio_done_wq available outside of direct-io.c
In reply to Christoph HellwigWe want to use the per-sb completion workqueue from the new iomap direct I/O code.
--- fs/direct-io.c | 2 +- fs/internal.h | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-)
fs/direct-io.c +1 -1
@@ -554,7 +554,7 @@ static inline int dio_bio_reap(struct dio *dio, struct dio_submit *sdio)* filesystems that don't need it and also allows us to create the workqueue * late enough so the we can include s_id in the name of the workqueue. */ -static int sb_init_dio_done_wq (struct super_block *sb) +int sb_init_dio_done_wq (struct super_block *sb) {struct workqueue_struct *old; struct workqueue_struct *wq = alloc_workqueue("dio/%s",
fs/internal.h +3 -0
@@ -184,3 +184,6 @@ typedef loff_t (*iomap_actor_t)(struct inode *inode, loff_t pos, loff_t len,loff_t iomap_apply (struct inode *inode,loff_t pos,loff_t length,unsigned flags,struct iomap_ops *ops,void *data,iomap_actor_t actor); + +/* direct-io.c: */ +int sb_init_dio_done_wq (struct super_block *sb);
Signature · 1 line
2.1.4
PATCH 4/5 iomap: implement direct I/O
In reply to Christoph HellwigThis adds a full fledget direct I/O implementation using the iomap interface. Full fledged in this case means all features are supported:
AIO, vectored I/O, any iov_iter type including kernel pointers, bvecs and pipes, support for hole filling and async apending writes. It does not mean supporting all the warts of the old generic code. We expect i_rwsem to be held over the duration of the call, and we expect to maintain i_dio_count ourselves, and we pass on any kinds of mapping to the file system for now.
The algorithm used is very simple: We use iomap_apply to iterate over the range of the I/O, and then we use the new bio_iov_iter_get_pages helper to lock down the user range for the size of the extent. bio_iov_iter_get_pages can currently lock down twice as many pages as the old direct I/O code did, which means that we will have a better batch factor for everything but overwrites of badly fragmented files.
--- fs/iomap.c | 373 ++++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/iomap.h | 11 ++ 2 files changed, 384 insertions(+)
fs/iomap.c +373 -0
@@ -24,6 +24,7 @@#include <linux/uio.h> #include <linux/backing-dev.h> #include <linux/buffer_head.h> +#include <linux/task_io_accounting_ops.h> #include <linux/dax.h> #include "internal.h"
@@ -584,3 +585,375 @@ int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fi,return 0 ; }EXPORT_SYMBOL_GPL (iomap_fiemap); + +/* +* Private flags for iomap_dio, must not overlap with the public ones in +* iomap.h: +*/ +#define IOMAP_DIO_WRITE (1 << 30) +#define IOMAP_DIO_DIRTY (1 << 31) + +struct iomap_dio { +struct kiocb *iocb; +iomap_dio_end_io_t *end_io; +loff_t i_size; +loff_t size; +atomic_t ref; +unsigned flags; +int error; + +union { +/* used during submission and for synchronous completion: */ +struct { +struct iov_iter *iter; +struct task_struct *waiter; +struct request_queue *last_queue; +blk_qc_t cookie; + } submit; + +/* used for aio completion: */ +struct { +struct work_struct work; + } aio; + }; +}; + +static ssize_t iomap_dio_complete (struct iomap_dio *dio) +{ +struct kiocb *iocb = dio->iocb; +ssize_t ret; + +if (dio->end_io) { + ret = dio->end_io (iocb, + dio->error ? dio->error : dio->size, + dio->flags); + }else { + ret = dio->error; + } + +if (likely (!ret)) { + ret = dio->size; +/* check for short read */ +if (iocb->ki_pos + ret > dio->i_size && + !(dio->flags &IOMAP_DIO_WRITE )) + ret = dio->i_size - iocb->ki_pos; + iocb->ki_pos += ret; + } + +inode_dio_end (file_inode (iocb->ki_filp)); +kfree (dio); + +return ret; +} + +static void iomap_dio_complete_work (struct work_struct *work) +{ + struct iomap_dio *dio = container_of(work, struct iomap_dio, aio.work); +struct kiocb *iocb = dio->iocb; +bool is_write = (dio->flags &IOMAP_DIO_WRITE ); +ssize_t ret; + + ret =iomap_dio_complete (dio); +if (is_write && ret >0 ) + ret =generic_write_sync (iocb, ret); + iocb->ki_complete (iocb, ret,0 ); +} + +/* +* Set an error in the dio if none is set yet. We have to use cmpxchg +* as the submission context and the completion context(s) can race to +* update the error. +*/ +static inline void iomap_dio_set_error (struct iomap_dio *dio,int ret) +{ +cmpxchg (&dio->error,0 , ret); +} + +static void iomap_dio_bio_end_io (struct bio *bio) +{ +struct iomap_dio *dio = bio->bi_private; +bool should_dirty = (dio->flags &IOMAP_DIO_DIRTY ); + +if (bio->bi_error) +iomap_dio_set_error (dio, bio->bi_error); + +if (atomic_dec_and_test (&dio->ref)) { +if (is_sync_kiocb (dio->iocb)) { +struct task_struct *waiter = dio->submit.waiter; + +WRITE_ONCE (dio->submit.waiter,NULL ); +wake_up_process (waiter); + }else if (dio->flags &IOMAP_DIO_WRITE ) { +struct inode *inode =file_inode (dio->iocb->ki_filp); + +INIT_WORK (&dio->aio.work, iomap_dio_complete_work); +queue_work (inode->i_sb->s_dio_done_wq, &dio->aio.work); + }else { +iomap_dio_complete_work (&dio->aio.work); + } + } + +if (should_dirty) { +bio_check_pages_dirty (bio); + }else { +struct bio_vec *bvec; +int i; + +bio_for_each_segment_all (bvec, bio, i) +put_page (bvec->bv_page); +bio_put (bio); + } +} + +static blk_qc_t +iomap_dio_zero (struct iomap_dio *dio,struct iomap *iomap,loff_t pos, +unsigned len) +{ +struct page *page =ZERO_PAGE (0 ); +struct bio *bio; + + bio =bio_alloc (GFP_KERNEL ,1 ); + bio->bi_bdev = iomap->bdev; + bio->bi_iter.bi_sector = + iomap->blkno + ((pos - iomap->offset) >>9 ); + bio->bi_private = dio; + bio->bi_end_io = iomap_dio_bio_end_io; + +get_page (page); +if (bio_add_page (bio, page, len,0 ) != len) +BUG (); +bio_set_op_attrs (bio,REQ_OP_WRITE ,WRITE_ODIRECT ); + +atomic_inc (&dio->ref); +return submit_bio (bio); +} + +static loff_t +iomap_dio_actor (struct inode *inode,loff_t pos,loff_t length, +void *data,struct iomap *iomap) +{ +struct iomap_dio *dio = data; +unsigned blkbits =blksize_bits (bdev_logical_block_size (iomap->bdev)); +unsigned fs_block_size = (1 << inode->i_blkbits), pad; +unsigned align =iov_iter_alignment (dio->submit.iter); +struct iov_iter iter; +struct bio *bio; +bool need_zeroout = false; +int nr_pages, ret; + +if ((pos | length | align) & ((1 << blkbits) -1 )) +return -EINVAL ; + +switch (iomap->type) { +case IOMAP_HOLE : +if (WARN_ON_ONCE (dio->flags &IOMAP_DIO_WRITE )) +return -EIO ; +/*FALLTHRU*/ +case IOMAP_UNWRITTEN : +if (!(dio->flags &IOMAP_DIO_WRITE )) { +iov_iter_zero (length, dio->submit.iter); + dio->size += length; +return length; + } + dio->flags |=IOMAP_DIO_UNWRITTEN ; + need_zeroout = true; +break ; +case IOMAP_MAPPED : +if (iomap->flags &IOMAP_F_SHARED ) + dio->flags |=IOMAP_DIO_COW ; +if (iomap->flags &IOMAP_F_NEW ) + need_zeroout = true; +break ; +default : +WARN_ON_ONCE (1 ); +return -EIO ; + } + +/* +* Operate on a partial iter trimmed to the extent we were called for. +* We'll update the iter in the dio once we're done with this extent. +*/ + iter = *dio->submit.iter; +iov_iter_truncate (&iter, length); + + nr_pages =iov_iter_npages (&iter,BIO_MAX_PAGES ); +if (nr_pages <=0 ) +return nr_pages; + +if (need_zeroout) { +/* zero out from the start of the block to the write offset */ + pad = pos & (fs_block_size -1 ); +if (pad) +iomap_dio_zero (dio, iomap, pos - pad, pad); + } + +do { +if (dio->error) +return 0 ; + + bio =bio_alloc (GFP_KERNEL , nr_pages); + bio->bi_bdev = iomap->bdev; + bio->bi_iter.bi_sector = + iomap->blkno + ((pos - iomap->offset) >>9 ); + bio->bi_private = dio; + bio->bi_end_io = iomap_dio_bio_end_io; + + ret =bio_iov_iter_get_pages (bio, &iter); +if (unlikely (ret)) { +bio_put (bio); +return ret; + } + +if (dio->flags &IOMAP_DIO_WRITE ) { +bio_set_op_attrs (bio,REQ_OP_WRITE ,WRITE_ODIRECT ); +task_io_account_write (bio->bi_iter.bi_size); + }else { +bio_set_op_attrs (bio,REQ_OP_READ ,0 ); +if (dio->flags &IOMAP_DIO_DIRTY ) +bio_set_pages_dirty (bio); + } + + dio->size += bio->bi_iter.bi_size; + pos += bio->bi_iter.bi_size; + + nr_pages =iov_iter_npages (&iter,BIO_MAX_PAGES ); + +atomic_inc (&dio->ref); + + dio->submit.last_queue =bdev_get_queue (iomap->bdev); + dio->submit.cookie =submit_bio (bio); + }while (nr_pages); + +if (need_zeroout) { +/* zero out from the end of the write to the end of the block */ + pad = pos & (fs_block_size -1 ); +if (pad) +iomap_dio_zero (dio, iomap, pos, fs_block_size - pad); + } + +iov_iter_advance (dio->submit.iter, length); +return length; +} + +ssize_t +iomap_dio_rw (struct kiocb *iocb,struct iov_iter *iter,struct iomap_ops *ops, +iomap_dio_end_io_t end_io) +{ +struct address_space *mapping = iocb->ki_filp->f_mapping; +struct inode *inode =file_inode (iocb->ki_filp); +size_t count =iov_iter_count (iter); +loff_t pos = iocb->ki_pos, end = iocb->ki_pos + count -1 , ret =0 ; +unsigned int flags =IOMAP_DIRECT ; +struct blk_plug plug; +struct iomap_dio *dio; + +lockdep_assert_held (&inode->i_rwsem); + +if (!count) +return 0 ; + + dio =kmalloc (sizeof (*dio),GFP_KERNEL ); +if (!dio) +return -ENOMEM ; + + dio->iocb = iocb; +atomic_set (&dio->ref,1 ); + dio->size =0 ; + dio->i_size =i_size_read (inode); + dio->end_io = end_io; + dio->error =0 ; + dio->flags =0 ; + + dio->submit.iter = iter; +if (is_sync_kiocb (iocb)) { + dio->submit.waiter = current; + dio->submit.cookie =BLK_QC_T_NONE ; + dio->submit.last_queue =NULL ; + } + +if (iov_iter_rw (iter) ==READ ) { +if (pos >= dio->i_size) + goto out_free_dio; + +if (iter->type ==ITER_IOVEC ) + dio->flags |=IOMAP_DIO_DIRTY ; + }else { + dio->flags |=IOMAP_DIO_WRITE ; + flags |=IOMAP_WRITE ; + } + +if (mapping->nrpages) { + ret =filemap_write_and_wait_range (mapping, iocb->ki_pos, end); +if (ret) + goto out_free_dio; + + ret =invalidate_inode_pages2_range (mapping, + iocb->ki_pos >>PAGE_SHIFT , end >>PAGE_SHIFT ); +WARN_ON_ONCE (ret); + ret =0 ; + } + +inode_dio_begin (inode); + +blk_start_plug (&plug); +do { + ret =iomap_apply (inode, pos, count, flags, ops, dio, + iomap_dio_actor); +if (ret <=0 ) { +/* magic error code to fall back to buffered I/O */ +if (ret == -ENOTBLK ) + ret =0 ; +break ; + } + pos += ret; + }while ((count =iov_iter_count (iter)) >0 ); +blk_finish_plug (&plug); + +if (ret <0 ) +iomap_dio_set_error (dio, ret); + +if (ret >=0 &&iov_iter_rw (iter) ==WRITE && !is_sync_kiocb (iocb) && + !inode->i_sb->s_dio_done_wq) { + ret =sb_init_dio_done_wq (inode->i_sb); +if (ret <0 ) +iomap_dio_set_error (dio, ret); + } + +if (!atomic_dec_and_test (&dio->ref)) { +if (!is_sync_kiocb (iocb)) +return -EIOCBQUEUED ; + +for (;;) { +set_current_state (TASK_UNINTERRUPTIBLE ); +if (!READ_ONCE (dio->submit.waiter)) +break ; + +if (!(iocb->ki_flags &IOCB_HIPRI ) || + !dio->submit.last_queue || + !blk_poll (dio->submit.last_queue, + dio->submit.cookie)) +io_schedule (); + } +__set_current_state (TASK_RUNNING ); + } + +/* +* Try again to invalidate clean pages which might have been cached by +* non-direct readahead, or faulted in by get_user_pages() if the source +* of the write was an mmap'ed region of the file we're writing. Either +* one is a pretty crazy thing to do, so we don't support it 100%. If +* this invalidation fails, tough, the write still worked... +*/ +if (iov_iter_rw (iter) ==WRITE && mapping->nrpages) { + ret =invalidate_inode_pages2_range (mapping, + iocb->ki_pos >>PAGE_SHIFT , end >>PAGE_SHIFT ); +WARN_ON_ONCE (ret); + } + +return iomap_dio_complete (dio); + +out_free_dio: +kfree (dio); +return ret; +} +EXPORT_SYMBOL_GPL (iomap_dio_rw);
include/linux/iomap.h +11 -0
@@ -50,6 +50,7 @@ struct iomap {#define IOMAP_ZERO (1 << 1)/* zeroing operation, may skip holes */ #define IOMAP_REPORT (1 << 2)/* report extent status, e.g. FIEMAP */ #define IOMAP_FAULT (1 << 3)/* mapping for page fault */ +#define IOMAP_DIRECT (1 << 4)/* direct I/O */ struct iomap_ops {/*
@@ -83,4 +84,14 @@ int iomap_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,int iomap_fiemap (struct inode *inode,struct fiemap_extent_info *fieinfo,loff_t start,loff_t len,struct iomap_ops *ops); +/* +* Flags for direct I/O ->end_io: +*/ +#define IOMAP_DIO_UNWRITTEN (1 << 0)/* covers unwritten extent(s) */ +#define IOMAP_DIO_COW (1 << 1)/* covers COW extent(s) */ +typedef int (iomap_dio_end_io_t )(struct kiocb *iocb,ssize_t ret, +unsigned flags); +ssize_t iomap_dio_rw (struct kiocb *iocb,struct iov_iter *iter, +struct iomap_ops *ops,iomap_dio_end_io_t end_io); +#endif /* LINUX_IOMAP_H */
Signature · 1 line
2.1.4
PATCH 5/5 xfs: use iomap_dio_rw
In reply to Christoph HellwigStraight switch over to using iomap for direct I/O - we already have the non-COW dio path in write_begin for DAX and files with extent size hints, so nothing to add there. The COW path is ported over from the old get_blocks version and a bit of a mess, but I have some work in progress to make it look more like the buffered I/O COW path.
This gets rid of xfs_get_blocks_direct and the last caller of xfs_get_blocks with the create flag set, so all that code can be removed.
Last but not least I've removed a comment in xfs_filemap_fault that refers to xfs_get_blocks entirely instead of updating it - while the reference is correct, the whole DAX fault path looks different than the non-DAX one, so it seems rather pointless.
--- fs/iomap.c | 2 +- fs/xfs/xfs_aops.c | 291 ++--------------------------------------------------- fs/xfs/xfs_aops.h | 6 -- fs/xfs/xfs_file.c | 149 +++++++++++---------------- fs/xfs/xfs_iomap.c | 50 +++++++-- 5 files changed, 111 insertions(+), 387 deletions(-)
fs/iomap.c +1 -1
@@ -591,7 +591,7 @@ EXPORT_SYMBOL_GPL(iomap_fiemap);* iomap.h: */ #define IOMAP_DIO_WRITE (1 << 30) -#define IOMAP_DIO_DIRTY (1 << 31) +#define IOMAP_DIO_DIRTY (1 << 31)struct iomap_dio {struct kiocb *iocb;
fs/xfs/xfs_aops.c +8 -283
360 lines not on this page · raw message
fs/xfs/xfs_aops.h +0 -6
@@ -55,12 +55,6 @@ struct xfs_ioend {extern const struct address_space_operations xfs_address_space_operations; -int xfs_get_blocks (struct inode *inode,sector_t offset, -struct buffer_head *map_bh,int create); -int xfs_get_blocks_direct (struct inode *inode,sector_t offset, -struct buffer_head *map_bh,int create); -int xfs_end_io_direct_write (struct kiocb *iocb,loff_t offset, -ssize_t size,void *private);int xfs_setfilesize (struct xfs_inode *ip,xfs_off_t offset,size_t size);extern void xfs_count_page_state (struct page *,int *,int *);
fs/xfs/xfs_file.c +60 -89
197 lines not on this page · raw message
fs/xfs/xfs_iomap.c +42 -8
89 lines not on this page · raw message
Signature · 1 line
2.1.4
Quoted 20 lines · Christoph Hellwig
Hi all,
this series adds a new direct I/O implementation based on the iomap interface, and switches XFS to use it.
The first two patches are a resend of my earlier series to remove the XFS iolock. They are needed for the lockdep assert in the new iomap code.
The rest implements a new
iomap_dio_rwdirect I/O implementation and switches XFS to use it.It requires a patch from the block tree which we'll need to cherry-pick into the XFS tree to avoid pulling in the whole block tree:
9cd56d91("block: addbio_iov_iter_get_pages()")To make everyones lifer easie I also have a git tree that includes this cherry pick:
Quoted 7 lines
git://git.infradead.org/users/hch/vfs.gitiomap-dio.4Note that with this patch I see somewhat increased failure rates on generic/209. This is not because of any locking or invalidation changes in this series, but just because it's always been fundamentally race, and the new direct I/O code is a lot faster, which causes the direct writes in this test to race significantly more often with readahead.
Ran some testing here, and it seems to perform well. Lower CPU utilization, or increased performance in the cases where we are maxing out the CPUs.
You can add my Tested-by to the series.
Signature · 1 line
Jens Axboe
Darrick J. Wong
Reviewed-byQuoted 20 lines · Christoph Hellwig
Hi all,
this series adds a new direct I/O implementation based on the iomap interface, and switches XFS to use it.
The first two patches are a resend of my earlier series to remove the XFS iolock. They are needed for the lockdep assert in the new iomap code.
The rest implements a new
iomap_dio_rwdirect I/O implementation and switches XFS to use it.It requires a patch from the block tree which we'll need to cherry-pick into the XFS tree to avoid pulling in the whole block tree:
9cd56d91("block: addbio_iov_iter_get_pages()")To make everyones lifer easie I also have a git tree that includes this cherry pick:
Quoted 7 lines
git://git.infradead.org/users/hch/vfs.gitiomap-dio.4Note that with this patch I see somewhat increased failure rates on generic/209. This is not because of any locking or invalidation changes in this series, but just because it's always been fundamentally race, and the new direct I/O code is a lot faster, which causes the direct writes in this test to race significantly more often with readahead.
I looked through the patches and didn't have any questions, so:
--D
Quoted 19 lines
Changes since V3:
- untangle from the block treeChanges since V2:
- added a few comments to unconfuse reviewers
- added aiomap_dio_set_errorhelper
- dropped support for not filling holes on writes
- pulled in a more recent block tree (blk_poll->blk_mq_poll)Changes since V1:
- dropped thebio_iov_iter_get_pagespatch, now merged in the block tree
- rebased on top of a merge of the XFS tree with the block tree
- stopped usingWRITE_ODIRECTwhich has been removed in the block tree
--
To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html