btrfs: add io_uring for encoded reads
Subsystems (2)
cover btrfs: add io_uring for encoded reads
This patch series adds io_uring support for btrfs encoded reads, complementing the ioctl we already have. The first few patches refactor the ioctl code so that the bio wait is moved to the outer function, and so that we can share as much code as possible between the two interfaces.
Mark Harmstone (6):
btrfs: remove iocb from btrfs_encoded_read
btrfs: store encoded read state in struct btrfs_encoded_read_private
btrfs: add btrfs_encoded_read_finish
btrfs: add btrfs_prepare_encoded_read
btrfs: move wait out of btrfs_encoded_read
btrfs: add io_uring interface for encoded reads
fs/btrfs/btrfs_inode.h | 23 +++- fs/btrfs/file.c | 1 + fs/btrfs/inode.c | 292 ++++++++++++++++++++++++----------------- fs/btrfs/ioctl.c | 194 +++++++++++++++++++-------- fs/btrfs/ioctl.h | 3 + 5 files changed, 337 insertions(+), 176 deletions(-)
Signature · 1 line
2.44.2
PATCH 1/6 btrfs: remove iocb from btrfs_encoded_read
We initialize a struct kiocb for encoded reads, but this never gets passed outside the driver. Simplify things by replacing it with a file, offset pair.
--- fs/btrfs/btrfs_inode.h | 3 ++- fs/btrfs/inode.c | 59 ++++++++++++++++++++---------------------- fs/btrfs/ioctl.c | 6 +---- 3 files changed, 31 insertions(+), 37 deletions(-)
fs/btrfs/btrfs_inode.h +2 -1
@@ -605,7 +605,8 @@ int btrfs_encoded_read_regular_fill_pages(struct btrfs_inode *inode, u64 file_offset, u64 disk_bytenr, u64 disk_io_size, struct page **pages); -ssize_t btrfs_encoded_read (struct kiocb *iocb,struct iov_iter *iter, +ssize_t btrfs_encoded_read (struct file *file,loff_t offset, +struct iov_iter *iter,struct btrfs_ioctl_encoded_io_args *encoded);ssize_t btrfs_do_encoded_write (struct kiocb *iocb,struct iov_iter *from,const struct btrfs_ioctl_encoded_io_args *encoded);
fs/btrfs/inode.c +28 -31
159 lines not on this page · raw message
fs/btrfs/ioctl.c +1 -5
@@ -4520,7 +4520,6 @@ static int btrfs_ioctl_encoded_read(struct file *file, void __user *argp,struct iovec *iov = iovstack;struct iov_iter iter;loff_t pos; -struct kiocb kiocb;ssize_t ret;if (!capable (CAP_SYS_ADMIN )) {
@@ -4571,10 +4570,7 @@ static int btrfs_ioctl_encoded_read(struct file *file, void __user *argp,if (ret <0 ) goto out_iov; -init_sync_kiocb (&kiocb, file); - kiocb.ki_pos = pos; - - ret =btrfs_encoded_read (&kiocb, &iter, &args); + ret =btrfs_encoded_read (file, pos, &iter, &args);if (ret >=0 ) {fsnotify_access (file); if (copy_to_user(argp + copy_end,
Signature · 1 line
2.44.2
Re: [PATCH 1/6] btrfs: remove iocb from btrfs_encoded_read
Quoted 3 lines · Mark Harmstone
We initialize a struct kiocb for encoded reads, but this never gets passed outside the driver. Simplify things by replacing it with a file, offset pair.
Please change the subject so it matches the description, so "btrfs: replace iocb with file and offset in btrfs_encoded_read()"
PATCH 2/6 btrfs: store encoded read state in struct btrfs_encoded_read_private
In reply to Mark HarmstoneMove the various stack variables needed for encoded reads into struct btrfs_encoded_read_private, so that we can split it into several functions.
--- fs/btrfs/btrfs_inode.h | 20 ++++- fs/btrfs/inode.c | 170 +++++++++++++++++++++-------------------- fs/btrfs/ioctl.c | 60 ++++++++------- 3 files changed, 135 insertions(+), 115 deletions(-)
fs/btrfs/btrfs_inode.h +17 -3
@@ -605,9 +605,23 @@ int btrfs_encoded_read_regular_fill_pages(struct btrfs_inode *inode, u64 file_offset, u64 disk_bytenr, u64 disk_io_size, struct page **pages); -ssize_t btrfs_encoded_read (struct file *file,loff_t offset, -struct iov_iter *iter, -struct btrfs_ioctl_encoded_io_args *encoded); + +struct btrfs_encoded_read_private { +wait_queue_head_t wait; +atomic_t pending; +blk_status_t status; +unsigned long nr_pages; +struct page **pages; +struct extent_state *cached_state; +size_t count; +struct iovec iovstack[UIO_FASTIOV ]; +struct iovec *iov; +struct iov_iter iter; +struct btrfs_ioctl_encoded_io_args args; +struct file *file; +}; + +ssize_t btrfs_encoded_read (struct btrfs_encoded_read_private *priv);ssize_t btrfs_do_encoded_write (struct kiocb *iocb,struct iov_iter *from,const struct btrfs_ioctl_encoded_io_args *encoded);
fs/btrfs/inode.c +87 -83
328 lines not on this page · raw message
fs/btrfs/ioctl.c +31 -29
103 lines not on this page · raw message
Signature · 1 line
2.44.2
Re: [PATCH 2/6] btrfs: store encoded read state in struct btrfs_encoded_read_private
Quoted 60 lines · Mark Harmstone
Move the various stack variables needed for encoded reads into struct
btrfs_encoded_read_private, so that we can split it into several functions.Signed-off-by Mark Harmstone <[email protected]>--- fs/btrfs/btrfs_inode.h | 20 ++++- fs/btrfs/inode.c | 170 +++++++++++++++++++++-------------------- fs/btrfs/ioctl.c | 60 ++++++++------- 3 files changed, 135 insertions(+), 115 deletions(-)fs/btrfs/btrfs_inode.h +17
-3@@ -605,9 +605,23 @@ int btrfs_encoded_read_regular_fill_pages(struct btrfs_inode *inode, u64 file_offset, u64 disk_bytenr, u64 disk_io_size, struct page **pages); -ssize_t btrfs_encoded_read (struct file *file,loff_t offset, -struct iov_iter *iter, -struct btrfs_ioctl_encoded_io_args *encoded); + +struct btrfs_encoded_read_private { +wait_queue_head_t wait; +atomic_t pending; +blk_status_t status; +unsigned long nr_pages; +struct page **pages; +struct extent_state *cached_state; +size_t count; +struct iovec iovstack[UIO_FASTIOV ]; +struct iovec *iov; +struct iov_iter iter; +struct btrfs_ioctl_encoded_io_args args; +struct file *file; +}; + +ssize_t btrfs_encoded_read (struct btrfs_encoded_read_private *priv);ssize_t btrfs_do_encoded_write (struct kiocb *iocb,struct iov_iter *from,const struct btrfs_ioctl_encoded_io_args *encoded);fs/btrfs/inode.c +0
-6@@ -9078,12 +9078,6 @@ static ssize_t btrfs_encoded_read_inline(return ret; } -struct btrfs_encoded_read_private { -wait_queue_head_t wait; -atomic_t pending; -blk_status_t status; -}; -static void btrfs_encoded_read_endio (struct btrfs_bio *bbio) {struct btrfs_encoded_read_private *priv = bbio->private;
fs/btrfs/inode.c static void btrfs_encoded_read_endio(struct btrfs_bio *bbio) · line 9101
Quoted 7 lines
fs/btrfs/inode.c +1
-3@@ -9104,33 +9098,31 @@ static void btrfs_encoded_read_endio(struct btrfs_bio *bbio)bio_put (&bbio->bio); } -int btrfs_encoded_read_regular_fill_pages (struct btrfs_inode *inode, -u64 file_offset,u64 disk_bytenr, -u64 disk_io_size,struct page **pages) +static void _btrfs_encoded_read_regular_fill_pages(struct btrfs_inode *inode,
Please don't use underscores in function names
https://btrfs.readthedocs.io/en/latest/dev/Development-notes.html#function-declarations
Re: [PATCH 2/6] btrfs: store encoded read state in struct btrfs_encoded_read_private
In reply to Mark HarmstoneQuoted 3 lines · Mark Harmstone
Move the various stack variables needed for encoded reads into struct
btrfs_encoded_read_private, so that we can split it into several functions.
Moving local variables makes sense in some cases but I don't see reason to move all of them.
Quoted 16 lines
Signed-off-by Mark Harmstone <[email protected]>--- fs/btrfs/btrfs_inode.h | 20 ++++- fs/btrfs/inode.c | 170 +++++++++++++++++++++-------------------- fs/btrfs/ioctl.c | 60 ++++++++------- 3 files changed, 135 insertions(+), 115 deletions(-)fs/btrfs/btrfs_inode.h +0
-1@@ -605,9 +605,23 @@ int btrfs_encoded_read_regular_fill_pages(struct btrfs_inode *inode, u64 file_offset, u64 disk_bytenr, u64 disk_io_size, struct page **pages); -ssize_t btrfs_encoded_read(struct file *file, loff_t offset,
fs/btrfs/btrfs_inode.h int btrfs_encoded_read_regular_fill_pages(struct btrfs_inode *inode, · line 615
Quoted 10 lines
fs/btrfs/btrfs_inode.h +8
-2-struct iov_iter *iter, - struct btrfs_ioctl_encoded_io_args *encoded); + +struct btrfs_encoded_read_private { +wait_queue_head_t wait; +atomic_t pending; +blk_status_t status; +unsigned long nr_pages; +struct page **pages; +struct extent_state *cached_state;
The cached state is used as a local variable that is not reused by other functions that also take the private structure, so what's the reason to store it here?
fs/btrfs/btrfs_inode.h int btrfs_encoded_read_regular_fill_pages(struct btrfs_inode *inode, · line 618
Quoted 3 lines
fs/btrfs/btrfs_inode.h +3
-0+size_t count; +struct iovec iovstack[UIO_FASTIOV ]; +struct iovec *iov;
Same, this is used in the leaf functions and not passed around.
fs/btrfs/btrfs_inode.h int btrfs_encoded_read_regular_fill_pages(struct btrfs_inode *inode, · line 622
Quoted 4 lines
fs/btrfs/btrfs_inode.h +4
-0+struct iov_iter iter; +struct btrfs_ioctl_encoded_io_args args; +struct file *file; +};
As a coding pattern, the structure should store data that would be otherwise passed as parameters or repeatedly derived from the parameters.
Re: [PATCH 2/6] btrfs: store encoded read state in struct btrfs_encoded_read_private
In reply to Mark HarmstoneQuoted 13 lines · Mark Harmstone
Move the various stack variables needed for encoded reads into struct
btrfs_encoded_read_private, so that we can split it into several functions.Signed-off-by Mark Harmstone <[email protected]>--- fs/btrfs/btrfs_inode.h | 20 ++++- fs/btrfs/inode.c | 170 +++++++++++++++++++++-------------------- fs/btrfs/ioctl.c | 60 ++++++++------- 3 files changed, 135 insertions(+), 115 deletions(-)fs/btrfs/btrfs_inode.h +0
-0
...
Quoted 13 lines
fs/btrfs/btrfs_inode.h +2
-5-ssize_t btrfs_encoded_read (struct file *file,loff_t offset, -struct iov_iter *iter, -struct btrfs_ioctl_encoded_io_args *encoded) +ssize_t btrfs_encoded_read (struct btrfs_encoded_read_private *priv) { -struct btrfs_inode *inode =BTRFS_I (file_inode (file)); +struct btrfs_inode *inode =BTRFS_I (file_inode (priv->file));struct btrfs_fs_info *fs_info = inode->root->fs_info;struct extent_io_tree *io_tree = &inode->io_tree;ssize_t ret; -size_t count =iov_iter_count (iter);u64 start, lockend, disk_bytenr, disk_io_size;
fs/btrfs/btrfs_inode.h
Quoted 10 lines
fs/btrfs/btrfs_inode.h +3
-2-struct extent_state *cached_state =NULL ;struct extent_map *em;bool unlocked = false; -file_accessed (file); + priv->count =iov_iter_count (&priv->iter); + +file_accessed (priv->file);btrfs_inode_lock (inode,BTRFS_ILOCK_SHARED );
Request submission should usually be short and not block on IO or any locks that might wait for IO.
bool nowait = issue_flags & IO_URING_F_NONBLOCK;
btrfs_encoded_read(..., nowait) {
f = BTRFS_ILOCK_SHARED;
if (nowait)
f |= BTRFS_ILOCK_TRY;
if (btrfs_inode_lock(inode, f))
return -EAGAIN; // io_uring will retry from a blocking context
}so it might need sth like this here as well as for filemap waiting and possibly other places.
Quoted 16 lines
fs/btrfs/btrfs_inode.h +4
-4-if (offset >= inode->vfs_inode.i_size) { +if (priv->args.offset >= inode->vfs_inode.i_size) {btrfs_inode_unlock (inode,BTRFS_ILOCK_SHARED );return 0 ; } - start =ALIGN_DOWN (offset, fs_info->sectorsize); + start =ALIGN_DOWN (priv->args.offset, fs_info->sectorsize);/* -* We don't know how long the extent containing offset is, but if -* it's compressed we know that it won't be longer than this. +* We don't know how long the extent containing priv->args.offset is, +* but if it's compressed we know that it won't be longer than this. */ lockend = start +BTRFS_MAX_UNCOMPRESSED -1 ;
...
Signature · 1 line
Pavel Begunkov
PATCH 3/6 btrfs: add btrfs_encoded_read_finish
In reply to Mark HarmstoneMove the end of btrfs_ioctl_encoded_read, responsible for copying to userspace and cleanup, into its own function.
--- fs/btrfs/btrfs_inode.h | 1 + fs/btrfs/inode.c | 29 +++++++++++----------------- fs/btrfs/ioctl.c | 43 ++++++++++++++++++++++++++++++------------ 3 files changed, 43 insertions(+), 30 deletions(-)
fs/btrfs/btrfs_inode.h +1 -0
@@ -619,6 +619,7 @@ struct btrfs_encoded_read_private {struct iov_iter iter;struct btrfs_ioctl_encoded_io_args args;struct file *file; +void __user *copy_out; };ssize_t btrfs_encoded_read (struct btrfs_encoded_read_private *priv);
fs/btrfs/inode.c +11 -18
@@ -9169,13 +9169,13 @@ static ssize_t btrfs_encoded_read_regular(struct btrfs_encoded_read_private *pri priv->nr_pages =DIV_ROUND_UP (disk_io_size,PAGE_SIZE ); priv->pages =kcalloc (priv->nr_pages,sizeof (struct page *),GFP_NOFS ); -if (!priv->pages) +if (!priv->pages) { + priv->nr_pages =0 ;return -ENOMEM ; + } ret =btrfs_alloc_page_array (priv->nr_pages, priv->pages, false); -if (ret) { - ret = -ENOMEM ; - goto out; - } +if (ret) +return -ENOMEM ;_btrfs_encoded_read_regular_fill_pages (inode, start, disk_bytenr, disk_io_size, priv);
@@ -9185,7 +9185,7 @@ static ssize_t btrfs_encoded_read_regular(struct btrfs_encoded_read_private *pri ret =blk_status_to_errno (READ_ONCE (priv->status));if (ret) - goto out; +return ret;unlock_extent (io_tree, start, lockend, &priv->cached_state);btrfs_inode_unlock (inode,BTRFS_ILOCK_SHARED );
@@ -9204,22 +9204,15 @@ static ssize_t btrfs_encoded_read_regular(struct btrfs_encoded_read_private *pri PAGE_SIZE - page_offset);if (copy_page_to_iter (priv->pages[i], page_offset, bytes, - &priv->iter) != bytes) { - ret = -EFAULT ; - goto out; - } + &priv->iter) != bytes) +return -EFAULT ; + i++; cur += bytes; page_offset =0 ; } - ret = priv->count; -out: -for (i =0 ; i < priv->nr_pages; i++) { -if (priv->pages[i]) -__free_page (priv->pages[i]); - } -kfree (priv->pages); -return ret; + +return priv->count; }ssize_t btrfs_encoded_read (struct btrfs_encoded_read_private *priv)
fs/btrfs/ioctl.c +31 -12
58 lines not on this page · raw message
Signature · 1 line
2.44.2
PATCH 4/6 btrfs: add btrfs_prepare_encoded_read
In reply to Mark HarmstoneMove the beginning of btrfs_ioctl_encoded_read, responsible for initialization of priv and validation, into its own function.
--- fs/btrfs/ioctl.c | 84 ++++++++++++++++++++++++++---------------------- 1 file changed, 46 insertions(+), 38 deletions(-)
fs/btrfs/ioctl.c +46 -38
@@ -4537,23 +4537,23 @@ static ssize_t btrfs_encoded_read_finish(struct btrfs_encoded_read_private *privreturn ret; } -static int btrfs_ioctl_encoded_read (struct file *file,void __user *argp, -bool compat) +static ssize_t btrfs_prepare_encoded_read (struct btrfs_encoded_read_private *priv, +struct file *file,bool compat, +void __user *argp) { size_t copy_end_kernel = offsetofend(struct btrfs_ioctl_encoded_io_args, flags);size_t copy_end;loff_t pos;ssize_t ret; -struct btrfs_encoded_read_private priv = { - .pending =ATOMIC_INIT (1 ), - .file = file, - }; -if (!capable (CAP_SYS_ADMIN )) { - ret = -EPERM ; - goto out; - } +memset (priv,0 ,sizeof (*priv)); + +atomic_set (&priv->pending,1 ); + priv->file = file; + +if (!capable (CAP_SYS_ADMIN )) +return -EPERM ;if (compat) {#if defined(CONFIG_64BIT ) && defined(CONFIG_COMPAT )
@@ -4561,47 +4561,55 @@ static int btrfs_ioctl_encoded_read(struct file *file, void __user *argp, copy_end = offsetofend(struct btrfs_ioctl_encoded_io_args_32, flags); -if (copy_from_user (&args32, argp, copy_end)) { - ret = -EFAULT ; - goto out; - } - priv.args.iov =compat_ptr (args32.iov); - priv.args.iovcnt = args32.iovcnt; - priv.args.offset = args32.offset; - priv.args.flags = args32.flags; +if (copy_from_user (&args32, argp, copy_end)) +return -EFAULT ; + + priv->args.iov =compat_ptr (args32.iov); + priv->args.iovcnt = args32.iovcnt; + priv->args.offset = args32.offset; + priv->args.flags = args32.flags;#else return -ENOTTY ;#endif }else { copy_end = copy_end_kernel; -if (copy_from_user (&priv.args, argp, copy_end)) { - ret = -EFAULT ; - goto out; - } - } -if (priv.args.flags !=0 ) { - ret = -EINVAL ; - goto out; +if (copy_from_user (&priv->args, argp, copy_end)) +return -EFAULT ; } - priv.iov = priv.iovstack; - ret =import_iovec (ITER_DEST , priv.args.iov, priv.args.iovcnt, -ARRAY_SIZE (priv.iovstack), &priv.iov, &priv.iter); +if (priv->args.flags !=0 ) +return -EINVAL ; + + priv->iov = priv->iovstack; + ret =import_iovec (ITER_DEST , priv->args.iov, priv->args.iovcnt, +ARRAY_SIZE (priv->iovstack), &priv->iov, &priv->iter);if (ret <0 ) { - priv.iov =NULL ; - goto out; + priv->iov =NULL ; +return ret; } -if (iov_iter_count (&priv.iter) ==0 ) { - ret =0 ; - goto out; - } - pos = priv.args.offset; - ret =rw_verify_area (READ , file, &pos, priv.args.len); + pos = priv->args.offset; + ret =rw_verify_area (READ , priv->file, &pos, priv->args.len);if (ret <0 ) +return ret; + + priv->copy_out = argp + copy_end; + +return 0 ; +} + +static int btrfs_ioctl_encoded_read (struct file *file,void __user *argp, +bool compat) +{ +ssize_t ret; +struct btrfs_encoded_read_private priv; + + ret =btrfs_prepare_encoded_read (&priv, file, compat, argp); +if (ret) goto out; - priv.copy_out = argp + copy_end; +if (iov_iter_count (&priv.iter) ==0 ) + goto out; ret =btrfs_encoded_read (&priv);
Signature · 1 line
2.44.2
PATCH 5/6 btrfs: move wait out of btrfs_encoded_read
In reply to Mark HarmstoneMakes it so that if btrfs_encoded_read has launched a bio, rather than waiting for it to complete it leaves the extent and inode locked and returns -EIOCBQUEUED. The caller is responsible for waiting on the bio, and calling the completion code in the new btrfs_encoded_read_regular_end.
--- fs/btrfs/btrfs_inode.h | 1 + fs/btrfs/inode.c | 81 +++++++++++++++++++++++------------------- fs/btrfs/ioctl.c | 8 +++++ 3 files changed, 54 insertions(+), 36 deletions(-)
fs/btrfs/btrfs_inode.h +1 -0
@@ -623,6 +623,7 @@ struct btrfs_encoded_read_private { };ssize_t btrfs_encoded_read (struct btrfs_encoded_read_private *priv); +ssize_t btrfs_encoded_read_regular_end (struct btrfs_encoded_read_private *priv);ssize_t btrfs_do_encoded_write (struct kiocb *iocb,struct iov_iter *from,const struct btrfs_ioctl_encoded_io_args *encoded);
fs/btrfs/inode.c +45 -36
144 lines not on this page · raw message
fs/btrfs/ioctl.c +8 -0
@@ -4516,6 +4516,9 @@ static ssize_t btrfs_encoded_read_finish(struct btrfs_encoded_read_private *priv flags);unsigned long i; +if (ret == -EIOCBQUEUED ) + ret =btrfs_encoded_read_regular_end (priv); +if (ret >=0 ) {fsnotify_access (priv->file); if (copy_to_user(priv->copy_out,
@@ -4613,6 +4616,11 @@ static int btrfs_ioctl_encoded_read(struct file *file, void __user *argp, ret =btrfs_encoded_read (&priv); +if (ret == -EIOCBQUEUED ) { +if (atomic_dec_return (&priv.pending)) +io_wait_event (priv.wait, !atomic_read (&priv.pending)); + } + out:return btrfs_encoded_read_finish (&priv, ret); }
Signature · 1 line
2.44.2
Re: [PATCH 5/6] btrfs: move wait out of btrfs_encoded_read
Quoted 75 lines · Mark Harmstone
Makes it so that if
btrfs_encoded_readhas launched a bio, rather than waiting for it to complete it leaves the extent and inode locked and returns-EIOCBQUEUED. The caller is responsible for waiting on the bio, and calling the completion code in the newbtrfs_encoded_read_regular_end.Signed-off-by Mark Harmstone <[email protected]>--- fs/btrfs/btrfs_inode.h | 1 + fs/btrfs/inode.c | 81 +++++++++++++++++++++++------------------- fs/btrfs/ioctl.c | 8 +++++ 3 files changed, 54 insertions(+), 36 deletions(-)fs/btrfs/btrfs_inode.h +1
-0@@ -623,6 +623,7 @@ struct btrfs_encoded_read_private { };ssize_t btrfs_encoded_read (struct btrfs_encoded_read_private *priv); +ssize_t btrfs_encoded_read_regular_end (struct btrfs_encoded_read_private *priv);ssize_t btrfs_do_encoded_write (struct kiocb *iocb,struct iov_iter *from,const struct btrfs_ioctl_encoded_io_args *encoded);fs/btrfs/inode.c +7
-20@@ -8999,7 +8999,7 @@ static ssize_t btrfs_encoded_read_inline(struct extent_state **cached_state, u64 extent_start, size_t count, struct btrfs_ioctl_encoded_io_args *encoded, - bool *unlocked) + bool *need_unlock) { struct btrfs_root *root = inode->root;struct btrfs_fs_info *fs_info = root->fs_info;@@ -9067,7 +9067,7 @@ static ssize_t btrfs_encoded_read_inline(btrfs_release_path (path);unlock_extent (io_tree, start, lockend, cached_state);btrfs_inode_unlock (inode,BTRFS_ILOCK_SHARED ); - *unlocked = true; + *need_unlock = false; ret =copy_to_iter (tmp, count, iter);if (ret != count)@@ -9155,42 +9155,26 @@ int btrfs_encoded_read_regular_fill_pages(struct btrfs_inode *inode,return blk_status_to_errno (READ_ONCE (priv.status)); } -static ssize_t btrfs_encoded_read_regular (struct btrfs_encoded_read_private *priv, -u64 start,u64 lockend, -u64 disk_bytenr,u64 disk_io_size, -bool *unlocked) +ssize_t btrfs_encoded_read_regular_end(struct btrfs_encoded_read_private *priv) - struct btrfs_inode *inode = BTRFS_I(file_inode(priv->file)); -struct extent_io_tree *io_tree = &inode->io_tree; + u64 cur, start, lockend;unsigned long i; -u64 cur;size_t page_offset; -ssize_t ret; - - priv->nr_pages =DIV_ROUND_UP (disk_io_size,PAGE_SIZE ); - priv->pages =kcalloc (priv->nr_pages,sizeof (struct page *),GFP_NOFS ); -if (!priv->pages) { - priv->nr_pages =0 ; -return -ENOMEM ; - } - ret =btrfs_alloc_page_array (priv->nr_pages, priv->pages, false); -if (ret) -return -ENOMEM ; +struct btrfs_inode *inode =BTRFS_I (file_inode (priv->file)); +struct btrfs_fs_info *fs_info = inode->root->fs_info; +struct extent_io_tree *io_tree = &inode->io_tree;
fs/btrfs/inode.c int btrfs_encoded_read_regular_fill_pages(struct btrfs_inode *inode, · line 9170
Quoted 10 lines
fs/btrfs/inode.c +5
-4+int ret; -_btrfs_encoded_read_regular_fill_pages (inode, start, disk_bytenr, - disk_io_size, priv); + start =ALIGN_DOWN (priv->args.offset, fs_info->sectorsize); + lockend = start +BTRFS_MAX_UNCOMPRESSED -1 ; -if (atomic_dec_return (&priv->pending)) -io_wait_event (priv->wait, !atomic_read (&priv->pending)); +unlock_extent (io_tree, start, lockend, &priv->cached_state); +btrfs_inode_unlock (inode,BTRFS_ILOCK_SHARED );
AFAIS, btrfs_encoded_read_regular_end is here to be used asynchronously in a later patch, specifically via bio callback -> io_uring tw callback, and that io_uring tw callback might not get run in any reasonable amount of time as it's controlled by the user space.
So, it takes the inode lock in one context (task executing the request) and releases it in another, it's usually is not allowed. And it also holds the locks potentially semi indefinitely (guaranteed to be executed when io_uring and/or task die). There is also unlock_extent() which I'd assume we don't want to hold locked for too long.
It's in general a bad practice having is_locked variables, especially when it's not on stack, even more so when it's implicit like
regular_read() {
(ret == -EIOCBQUEUED)
need_unlock = false; // delay it
}
btrfs_encoded_read_finish() {
if (ret == -EIOCBQUEUED)
unlock();
}That just too easy to miss when you do anything with the code afterwards.
I hope Josef and btrfs folks can comment what would be a way here, does the inode lock has to be held for the entire duration of IO?
Quoted 15 lines
fs/btrfs/inode.c +0
-4ret =blk_status_to_errno (READ_ONCE (priv->status));if (ret)return ret; -unlock_extent (io_tree, start, lockend, &priv->cached_state); -btrfs_inode_unlock (inode,BTRFS_ILOCK_SHARED ); - *unlocked = true; -if (priv->args.compression) { i =0 ; page_offset =0 ;@@ -9215,6 +9199,30 @@ static ssize_t btrfs_encoded_read_regular(struct btrfs_encoded_read_private *prireturn priv->count; }
Signature · 1 line
Pavel Begunkov
PATCH 6/6 btrfs: add io_uring interface for encoded reads
In reply to Mark HarmstoneAdds an io_uring interface for asynchronous encoded reads, using the same interface as for the ioctl. To use this you would use an SQE opcode of IORING_OP_URING_CMD, the cmd_op would be BTRFS_IOC_ENCODED_READ, and addr would point to the userspace address of the btrfs_ioctl_encoded_io_args struct. As with the ioctl, you need to have CAP_SYS_ADMIN for this to work.
--- fs/btrfs/btrfs_inode.h | 2 ++ fs/btrfs/file.c | 1 + fs/btrfs/inode.c | 57 ++++++++++++++++++++++++++++++++++++---- fs/btrfs/ioctl.c | 59 ++++++++++++++++++++++++++++++++++++++++-- fs/btrfs/ioctl.h | 3 +++ 5 files changed, 115 insertions(+), 7 deletions(-)
fs/btrfs/btrfs_inode.h +2 -0
@@ -620,6 +620,8 @@ struct btrfs_encoded_read_private {struct btrfs_ioctl_encoded_io_args args;struct file *file;void __user *copy_out; +struct io_uring_cmd *cmd; +unsigned int issue_flags; };ssize_t btrfs_encoded_read (struct btrfs_encoded_read_private *priv);
fs/btrfs/file.c +1 -0
@@ -3769,6 +3769,7 @@ const struct file_operations btrfs_file_operations = { .compat_ioctl = btrfs_compat_ioctl, #endif .remap_file_range = btrfs_remap_file_range, + .uring_cmd = btrfs_uring_cmd, .fop_flags = FOP_BUFFER_RASYNC | FOP_BUFFER_WASYNC, };
fs/btrfs/inode.c +52 -5
89 lines not on this page · raw message
fs/btrfs/ioctl.c +57 -2
77 lines not on this page · raw message
fs/btrfs/ioctl.h +3 -0
@@ -22,5 +22,8 @@ void btrfs_sync_inode_flags_to_i_flags(struct inode *inode);int __pure btrfs_is_empty_uuid (const u8 *uuid);void btrfs_update_ioctl_balance_args (struct btrfs_fs_info *fs_info,struct btrfs_ioctl_balance_args *bargs); +int btrfs_uring_cmd (struct io_uring_cmd *cmd,unsigned int issue_flags); +ssize_t btrfs_encoded_read_finish (struct btrfs_encoded_read_private *priv, +ssize_t ret);#endif
Signature · 1 line
2.44.2
Re: [PATCH 6/6] btrfs: add io_uring interface for encoded reads
Quoted 9 lines · Mark Harmstone
Adds an
io_uringinterface for asynchronous encoded reads, using the same interface as for the ioctl. To use this you would use an SQE opcode ofIORING_OP_URING_CMD, thecmd_opwould beBTRFS_IOC_ENCODED_READ, and addr would point to the userspace address of thebtrfs_ioctl_encoded_io_argsstruct. As with the ioctl, you need to haveCAP_SYS_ADMINfor this to work.Signed-off-by Mark Harmstone <[email protected]>---
...
Quoted 12 lines
fs/btrfs/btrfs_inode.h +2
-0@@ -620,6 +620,8 @@ struct btrfs_encoded_read_private {struct btrfs_ioctl_encoded_io_args args;struct file *file;void __user *copy_out; +struct io_uring_cmd *cmd; +unsigned int issue_flags; };
...
Quoted 5 lines
fs/btrfs/btrfs_inode.h +5
-0+static inline struct btrfs_encoded_read_private *btrfs_uring_encoded_read_pdu ( +struct io_uring_cmd *cmd) +{ +return *(struct btrfs_encoded_read_private **)cmd->pdu; +}
fs/btrfs/btrfs_inode.h
Quoted 10 lines
fs/btrfs/btrfs_inode.h +10
-0+static void btrfs_finish_uring_encoded_read (struct io_uring_cmd *cmd, +unsigned int issue_flags) +{ +struct btrfs_encoded_read_private *priv; +ssize_t ret; + + priv =btrfs_uring_encoded_read_pdu (cmd); + ret =btrfs_encoded_read_finish (priv, -EIOCBQUEUED ); + +io_uring_cmd_done (priv->cmd, ret,0 , priv->issue_flags);
s/priv->issue_flags/issue_flags/
You can't use flags that you got somewhere before from a completely different execution context.
fs/btrfs/btrfs_inode.h
Quoted 7 lines
fs/btrfs/btrfs_inode.h +7
-0+ +kfree (priv); +} + +static void btrfs_encoded_read_uring_endio (struct btrfs_bio *bbio) +{ +struct btrfs_encoded_read_private *priv = bbio->private;
Might be cleaner if you combine it with btrfs_encoded_read_ioctl_endio
btrfs_encoded_read_endio()
{
...
if (!atomic_dec_return(&priv->pending)) {
if (priv->cmd)
io_uring_cmd_complete_in_task();
else
wake_up();
...
}Quoted 55 lines
fs/btrfs/btrfs_inode.h +28
-4+ +if (bbio->bio.bi_status) { +/* +* The memory barrier implied by the atomic_dec_return() here +* pairs with the memory barrier implied by the +* atomic_dec_return() or io_wait_event() in +* btrfs_encoded_read_regular_fill_pages() to ensure that this +* write is observed before the load of status in +* btrfs_encoded_read_regular_fill_pages(). +*/ +WRITE_ONCE (priv->status, bbio->bio.bi_status); + } +if (!atomic_dec_return (&priv->pending)) { +io_uring_cmd_complete_in_task (priv->cmd, + btrfs_finish_uring_encoded_read); + } +bio_put (&bbio->bio); +} + static void _btrfs_encoded_read_regular_fill_pages(struct btrfs_inode *inode, u64 file_offset, u64 disk_bytenr, u64 disk_io_size,@@ -9106,11 +9148,16 @@ static void _btrfs_encoded_read_regular_fill_pages(struct btrfs_inode *inode,struct btrfs_fs_info *fs_info = inode->root->fs_info;unsigned long i =0 ;struct btrfs_bio *bbio; +btrfs_bio_end_io_t cb; -init_waitqueue_head (&priv->wait); +if (priv->cmd) { + cb = btrfs_encoded_read_uring_endio; + }else { +init_waitqueue_head (&priv->wait); + cb = btrfs_encoded_read_ioctl_endio; + } - bbio =btrfs_bio_alloc (BIO_MAX_VECS ,REQ_OP_READ , fs_info, - btrfs_encoded_read_endio, priv); + bbio =btrfs_bio_alloc (BIO_MAX_VECS ,REQ_OP_READ , fs_info, cb, priv); bbio->bio.bi_iter.bi_sector = disk_bytenr >>SECTOR_SHIFT ; bbio->inode = inode;@@ -9122,7 +9169,7 @@ static void _btrfs_encoded_read_regular_fill_pages(struct btrfs_inode *inode,btrfs_submit_bio (bbio,0 ); bbio =btrfs_bio_alloc (BIO_MAX_VECS ,REQ_OP_READ , fs_info, - btrfs_encoded_read_endio, priv); + cb, priv); bbio->bio.bi_iter.bi_sector = disk_bytenr >>SECTOR_SHIFT ; bbio->inode = inode;continue ;fs/btrfs/ioctl.c +0
-0
...
fs/btrfs/ioctl.c static int btrfs_ioctl_encoded_write(struct file *file, void __user *argp, bool · line 4733
Quoted 5 lines
fs/btrfs/ioctl.c +5
-0+static void btrfs_uring_encoded_read (struct io_uring_cmd *cmd, +unsigned int issue_flags) +{ +ssize_t ret; +void __user *argp = (void __user *)(uintptr_t )cmd->sqe->addr;
u64_to_user_ptr()
Quoted 11 lines
fs/btrfs/ioctl.c +11
-0+struct btrfs_encoded_read_private *priv; +bool compat = issue_flags &IO_URING_F_COMPAT ; + + priv =kmalloc (sizeof (*priv),GFP_KERNEL ); +if (!priv) { + ret = -ENOMEM ; + goto out_uring; + } + + ret =btrfs_prepare_encoded_read (priv, cmd->file, compat, argp); +if (ret)
fs/btrfs/ioctl.c static int btrfs_ioctl_encoded_write(struct file *file, void __user *argp, bool · line 4754
Quoted 10 lines
fs/btrfs/ioctl.c +10
-0+ goto out_finish; + +if (iov_iter_count (&priv->iter) ==0 ) { + ret =0 ; + goto out_finish; + } + + *(struct btrfs_encoded_read_private **)cmd->pdu = priv; + priv->cmd = cmd; + priv->issue_flags = issue_flags;
You shouldn't be stashing issue_flags, it almost always wrong. Looks btrfs_finish_uring_encoded_read() is the only user, and with my comment above you can kill priv->issue_flags
fs/btrfs/ioctl.c static int btrfs_ioctl_encoded_write(struct file *file, void __user *argp, bool · line 4758
Quoted 4 lines
fs/btrfs/ioctl.c +4
-0+ ret =btrfs_encoded_read (priv); + +if (ret == -EIOCBQUEUED &&atomic_dec_return (&priv->pending)) +return ;
Is gating on -EIOCBQUEUED just an optimisation? I.e. it's fine to swap checks
if (atomic_dec_return(&priv->pending) && ret == -EIOCBQUEUED)
but we know that unless it returned -EIOCBQUEUED nobody should've touched ->pending.
Quoted 9 lines
fs/btrfs/ioctl.c +9
-0+ +out_finish: + ret =btrfs_encoded_read_finish (priv, ret); +kfree (priv); + +out_uring: +io_uring_cmd_done (cmd, ret,0 , issue_flags); +} +
Signature · 1 line
Pavel Begunkov
Re: [PATCH 6/6] btrfs: add io_uring interface for encoded reads
In reply to Mark HarmstoneQuoted 9 lines · Mark Harmstone
Adds an
io_uringinterface for asynchronous encoded reads, using the same interface as for the ioctl. To use this you would use an SQE opcode ofIORING_OP_URING_CMD, thecmd_opwould beBTRFS_IOC_ENCODED_READ, and addr would point to the userspace address of thebtrfs_ioctl_encoded_io_argsstruct. As with the ioctl, you need to haveCAP_SYS_ADMINfor this to work.Signed-off-by Mark Harmstone <[email protected]>---
...
Quoted 28 lines
fs/btrfs/inode.c +5
-1@@ -34,6 +34,7 @@#include <linux/iomap.h> #include <asm/unaligned.h> #include <linux/fsverity.h> +#include <linux/io_uring/cmd.h> #include "misc.h" #include "ctree.h" #include "disk-io.h" @@ -9078,7 +9079,7 @@ static ssize_t btrfs_encoded_read_inline(return ret; } -static void btrfs_encoded_read_endio (struct btrfs_bio *bbio) +static void btrfs_encoded_read_ioctl_endio (struct btrfs_bio *bbio) {struct btrfs_encoded_read_private *priv = bbio->private;@@ -9098,6 +9099,47 @@ static void btrfs_encoded_read_endio(struct btrfs_bio *bbio)bio_put (&bbio->bio); } +static inline struct btrfs_encoded_read_private *btrfs_uring_encoded_read_pdu ( +struct io_uring_cmd *cmd) +{
fs/btrfs/inode.c static void btrfs_encoded_read_endio(struct btrfs_bio *bbio) · line 9114
Quoted 10 lines
fs/btrfs/inode.c +10
-0+return *(struct btrfs_encoded_read_private **)cmd->pdu; +} +static void btrfs_finish_uring_encoded_read (struct io_uring_cmd *cmd, +unsigned int issue_flags) +{ +struct btrfs_encoded_read_private *priv; +ssize_t ret; + + priv =btrfs_uring_encoded_read_pdu (cmd); + ret =btrfs_encoded_read_finish (priv, -EIOCBQUEUED );
tw callback -> btrfs_encoded_read_finish() -> copy_to_user()
That's usually fine except cases when the task and/or io_uring are dying and the callback executes not from a user task context. Same problem as with fuse, we can pass a flag from io_uring into the callback telling btrfs that it should terminate the request and not rely on mm or any other task related pieces.
Quoted 6 lines
fs/btrfs/inode.c +6
-0+ +io_uring_cmd_done (priv->cmd, ret,0 , priv->issue_flags); + +kfree (priv); +} +
...
Signature · 1 line
Pavel Begunkov