Subsystems (8)
Jens Axboe
maintainerWe technically support this already through io_uring, but it's implemented with a thread backend to support cases where we would block. This isn't ideal.
After a few prep patches, the core of this patchset is adding support for async callbacks on page unlock. With this primitive, we can simply retry the IO operation. With io_uring, this works a lot like poll based retry for files that support it. If a page is currently locked and needed, -EIOCBQUEUED is returned with a callback armed. The callers callback is responsible for restarting the operation.
With this callback primitive, we can add support for generic_file_buffered_read(), which is what most file systems end up using for buffered reads. XFS/ext4/btrfs/bdev is wired up, but probably trivial to add more.
The file flags support for this by setting FMODE_BUF_RASYNC, similar to what we do for FMODE_NOWAIT. Open to suggestions here if this is the preferred method or not.
In terms of results, I wrote a small test app that randomly reads 4G of data in 4K chunks from a file hosted by ext4. The app uses a queue depth of 32. If you want to test yourself, you can just use buffered=1 with ioengine=io_uring with fio. No application changes are needed to use the more optimized buffered async read.
preadv for comparison:
real 1m13.821s user 0m0.558s sys 0m11.125s CPU ~13%
Mainline:
real 0m12.054s user 0m0.111s sys 0m5.659s CPU ~32% + ~50% == ~82%
This patchset:
real 0m9.283s user 0m0.147s sys 0m4.619s CPU ~52%
The CPU numbers are just a rough estimate. For the mainline io_uring run, this includes the app itself and all the threads doing IO on its behalf (32% for the app, ~1.6% per worker and 32 of them). Context switch rate is much smaller with the patchset, since we only have the one task performing IO.
Also ran a simple fio based test case, varying the queue depth from 1 to 16, doubling every time:
[buf-test]
filename=/data/file
direct=0
ioengine=io_uring
norandommap
rw=randread
bs=4k
iodepth=${QD}
randseed=89
runtime=10s
QD/Test Patchset IOPS Mainline IOPS
1 9046 8294
2 19.8k 18.9k
4 39.2k 28.5k
8 64.4k 31.4k
16 65.7k 37.8k
Outside of my usual environment, so this is just running on a virtualized NVMe device in qemu, using ext4 as the file system. NVMe isn't very efficient virtualized, so we run out of steam at ~65K which is why we flatline on the patched side (nvme_submit_cmd() eats ~75% of the test app CPU). Before that happens, it's a linear increase. Not shown is context switch rate, which is massively lower with the new code. The old thread offload adds a blocking thread per pending IO, so context rate quickly goes through the roof.
The goal here is efficiency. Async thread offload adds latency, and it also adds noticable overhead on items such as adding pages to the page cache. By allowing proper async buffered read support, we don't have X threads hammering on the same inode page cache, we have just the single app actually doing IO.
Been beating on this and it's solid for me, and I'm now pretty happy with how it all turned out. Not aware of any missing bits/pieces or code cleanups that need doing.
Series can also be found here:
https://git.kernel.dk/cgit/linux-block/log/?h=async-buffered.7
or pull from:
git://git.kernel.dk/linux-block async-buffered.7
block/blk-core.c | 6 + fs/block_dev.c | 2 +- fs/btrfs/file.c | 2 +- fs/ext4/file.c | 2 +- fs/io_uring.c | 336 ++++++++++++++++++++++++++++++++++------ fs/xfs/xfs_file.c | 2 +- include/linux/blkdev.h | 1 + include/linux/fs.h | 10 +- include/linux/pagemap.h | 75 +++++++++ mm/filemap.c | 110 ++++++++----- 10 files changed, 454 insertions(+), 92 deletions(-)
Changes since v6:
- Properly catch and resubmit requests that would have blocked in submission.
- Fix sqthread mm usage for async buffered retry
- Fix a retry condition iter setup
- Rebase on master + for-5.9/io_uring
Changes since v5:
- Correct commit message, iocb->private -> iocb->ki_waitq
- Get rid of io_uring goto, use an iter read helper
Changes since v3:
- io_uring: don't retry if REQ_F_NOWAIT is set
- io_uring: alloc req->io if the request type didn't already
- Add iocb->ki_waitq instead of (ab)using iocb->private Changes since v2:
- Get rid of unnecessary wait_page_async struct, just use wait_page_async
- Add another prep handler, adding wake_page_match()
- Use wake_page_match() in both callers
Changes since v1:
- Fix an issue with inline page locking
- Fix a potential race with __wait_on_page_locked_async()
- Fix a hang related to not setting page_match, thus missing a wakeup
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerPATCH 1/15 block: provide plug based way of signaling forced no-wait semantics
Provide a way for the caller to specify that IO should be marked with REQ_NOWAIT to avoid blocking on allocation.
--- block/blk-core.c | 6 ++++++ include/linux/blkdev.h | 1 + 2 files changed, 7 insertions(+)
block/blk-core.c +6 -0
@@ -958,6 +958,7 @@ generic_make_request_checks(struct bio *bio)struct request_queue *q;int nr_sectors =bio_sectors (bio);blk_status_t status =BLK_STS_IOERR ; +struct blk_plug *plug;char b[BDEVNAME_SIZE ];might_sleep ();
@@ -971,6 +972,10 @@ generic_make_request_checks(struct bio *bio) goto end_io; } + plug =blk_mq_plug (q, bio); +if (plug && plug->nowait) + bio->bi_opf |=REQ_NOWAIT ; +/* * For a REQ_NOWAIT based request, return -EOPNOTSUPP * if queue is not a request based queue.
@@ -1800,6 +1805,7 @@ void blk_start_plug(struct blk_plug *plug)INIT_LIST_HEAD (&plug->cb_list); plug->rq_count =0 ; plug->multiple_queues = false; + plug->nowait = false;/* * Store ordering should not be needed here, since a potential
include/linux/blkdev.h +1 -0
@@ -1189,6 +1189,7 @@ struct blk_plug {struct list_head cb_list;/* md requires an unplug callback */ unsigned short rq_count;bool multiple_queues; +bool nowait; };#define BLK_MAX_REQUEST_COUNT 16#define BLK_PLUG_FLUSH_SIZE (128 * 1024)
Signature · 1 line
2.27.0
Jens Axboe
maintainerPATCH 2/15 io_uring: always plug for any number of IOs
In reply to Jens AxboeCurrently we only plug if we're doing more than two request. We're going to be relying on always having the plug there to pass down information, so plug unconditionally.
--- fs/io_uring.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-)
fs/io_uring.c +5 -10
@@ -676,7 +676,6 @@ struct io_kiocb { }; }; -#define IO_PLUG_THRESHOLD 2#define IO_IOPOLL_BATCH 8struct io_submit_state {
@@ -5914,7 +5913,7 @@ static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,static int io_submit_sqes (struct io_ring_ctx *ctx,unsigned int nr,struct file *ring_file,int ring_fd) { -struct io_submit_state state, *statep =NULL ; +struct io_submit_state state;struct io_kiocb *link =NULL ;int i, submitted =0 ;
@@ -5931,10 +5930,7 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,if (!percpu_ref_tryget_many (&ctx->refs, nr))return -EAGAIN ; -if (nr >IO_PLUG_THRESHOLD ) { -io_submit_state_start (&state, nr); - statep = &state; - } +io_submit_state_start (&state, nr); ctx->ring_fd = ring_fd; ctx->ring_file = ring_file;
@@ -5949,14 +5945,14 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,io_consume_sqe (ctx);break ; } - req =io_alloc_req (ctx, statep); + req =io_alloc_req (ctx, &state);if (unlikely (!req)) {if (!submitted) submitted = -EAGAIN ;break ; } - err =io_init_req (ctx, req, sqe, statep); + err =io_init_req (ctx, req, sqe, &state);io_consume_sqe (ctx);/* will complete beyond this point, count as submitted */ submitted++;
@@ -5982,8 +5978,7 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr, }if (link)io_queue_link_head (link); -if (statep) -io_submit_state_end (&state); +io_submit_state_end (&state);/* Commit SQ ring head once we've consumed and submitted all SQEs */ io_commit_sqring (ctx);
Signature · 1 line
2.27.0
Jens Axboe
maintainerPATCH 3/15 io_uring: catch -EIO from buffered issue request failure
In reply to Jens Axboe-EIO bubbles up like -EAGAIN if we fail to allocate a request at the lower level. Play it safe and treat it like -EAGAIN in terms of sync retry, to avoid passing back an errant -EIO.
Catch some of these early for block based file, as non-mq devices generally do not support NOWAIT. That saves us some overhead by not first trying, then retrying from async context. We can go straight to async punt instead.
--- fs/io_uring.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-)
fs/io_uring.c +23 -5
@@ -2088,6 +2088,15 @@ static struct file *__io_file_get(struct io_submit_state *state, int fd)return state->file; } +static bool io_bdev_nowait (struct block_device *bdev) +{ +#ifdef CONFIG_BLOCK +return !bdev ||queue_is_mq (bdev_get_queue (bdev)); +#else +return true; +#endif +} +/* * If we tracked the file through the SCM inflight mechanism, we could support * any file. For now, just ensure that anything potentially problematic is done
@@ -2097,10 +2106,19 @@ static bool io_file_supports_async(struct file *file, int rw) {umode_t mode =file_inode (file)->i_mode; -if (S_ISBLK (mode) ||S_ISCHR (mode) ||S_ISSOCK (mode)) -return true; -if (S_ISREG (mode) && file->f_op != &io_uring_fops) +if (S_ISBLK (mode)) { +if (io_bdev_nowait (file->f_inode->i_bdev)) +return true; +return false; + } +if (S_ISCHR (mode) ||S_ISSOCK (mode))return true; +if (S_ISREG (mode)) { +if (io_bdev_nowait (file->f_inode->i_sb->s_bdev) && + file->f_op != &io_uring_fops) +return true; +return false; + }/* any ->read/write should understand O_NONBLOCK */ if (file->f_flags &O_NONBLOCK )
@@ -2650,7 +2668,7 @@ static int io_read(struct io_kiocb *req, bool force_nonblock) iov_count =iov_iter_count (&iter); ret =rw_verify_area (READ , req->file, &kiocb->ki_pos, iov_count);if (!ret) { -ssize_t ret2; +ssize_t ret2 =0 ;if (req->file->f_op->read_iter) ret2 =call_read_iter (req->file, kiocb, &iter);
@@ -2658,7 +2676,7 @@ static int io_read(struct io_kiocb *req, bool force_nonblock) ret2 =loop_rw_iter (READ , req->file, kiocb, &iter);/* Catch -EAGAIN return for forced non-blocking submission */ -if (!force_nonblock || ret2 != -EAGAIN ) { +if (!force_nonblock || (ret2 != -EAGAIN && ret2 != -EIO )) {kiocb_done (kiocb, ret2); }else { copy_iov:
Signature · 1 line
2.27.0
Jens Axboe
maintainerPATCH 4/15 io_uring: re-issue block requests that failed because of resources
In reply to Jens AxboeMark the plug with nowait == true, which will cause requests to avoid blocking on request allocation. If they do, we catch them and reissue them from a task_work based handler.
Normally we can catch -EAGAIN directly, but the hard case is for split requests. As an example, the application issues a 512KB request. The block core will split this into 128KB if that's the max size for the device. The first request issues just fine, but we run into -EAGAIN for some latter splits for the same request. As the bio is split, we don't get to see the -EAGAIN until one of the actual reads complete, and hence we cannot handle it inline as part of submission.
This does potentially cause re-reads of parts of the range, as the whole request is reissued. There's currently no better way to handle this.
--- fs/io_uring.c | 148 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 124 insertions(+), 24 deletions(-)
fs/io_uring.c +124 -24
@@ -900,6 +900,13 @@ static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,static void __io_queue_sqe (struct io_kiocb *req,const struct io_uring_sqe *sqe); +static ssize_t io_import_iovec (int rw,struct io_kiocb *req, +struct iovec **iovec,struct iov_iter *iter, +bool needs_lock); +static int io_setup_async_rw (struct io_kiocb *req,ssize_t io_size, +struct iovec *iovec,struct iovec *fast_iov, +struct iov_iter *iter); +static struct kmem_cache *req_cachep;static const struct file_operations io_uring_fops;
@@ -1978,12 +1985,115 @@ static void io_complete_rw_common(struct kiocb *kiocb, long res)__io_cqring_add_event (req, res, cflags); } +static void io_sq_thread_drop_mm (struct io_ring_ctx *ctx) +{ +struct mm_struct *mm = current->mm; + +if (mm) { +kthread_unuse_mm (mm); +mmput (mm); + } +} + +static int io_sq_thread_acquire_mm (struct io_ring_ctx *ctx, +struct io_kiocb *req) +{ +if (io_op_defs[req->opcode].needs_mm && !current->mm) { +if (unlikely (!mmget_not_zero (ctx->sqo_mm))) +return -EFAULT ; +kthread_use_mm (ctx->sqo_mm); + } + +return 0 ; +} + +#ifdef CONFIG_BLOCK +static bool io_resubmit_prep (struct io_kiocb *req,int error) +{ +struct iovec inline_vecs[UIO_FASTIOV ], *iovec = inline_vecs; +ssize_t ret = -ECANCELED ; +struct iov_iter iter; +int rw; + +if (error) { + ret = error; + goto end_req; + } + +switch (req->opcode) { +case IORING_OP_READV : +case IORING_OP_READ_FIXED : +case IORING_OP_READ : + rw =READ ; +break ; +case IORING_OP_WRITEV : +case IORING_OP_WRITE_FIXED : +case IORING_OP_WRITE : + rw =WRITE ; +break ; +default : +printk_once (KERN_WARNING "io_uring: bad opcode in resubmit %d\n" , + req->opcode); + goto end_req; + } + + ret =io_import_iovec (rw, req, &iovec, &iter, false); +if (ret <0 ) + goto end_req; + ret =io_setup_async_rw (req, ret, iovec, inline_vecs, &iter); +if (!ret) +return true; +kfree (iovec); +end_req: +io_cqring_add_event (req, ret); +req_set_fail_links (req); +io_put_req (req); +return false; +} + +static void io_rw_resubmit (struct callback_head *cb) +{ + struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); +struct io_ring_ctx *ctx = req->ctx; +int err; + +__set_current_state (TASK_RUNNING ); + + err =io_sq_thread_acquire_mm (ctx, req); + +if (io_resubmit_prep (req, err)) { +refcount_inc (&req->refs); +io_queue_async_work (req); + } +} +#endif + +static bool io_rw_reissue (struct io_kiocb *req,long res) +{ +#ifdef CONFIG_BLOCK +struct task_struct *tsk; +int ret; + +if ((res != -EAGAIN && res != -EOPNOTSUPP ) ||io_wq_current_is_worker ()) +return false; + + tsk = req->task; +init_task_work (&req->task_work, io_rw_resubmit); + ret =task_work_add (tsk, &req->task_work, true); +if (!ret) +return true; +#endif +return false; +} +static void io_complete_rw (struct kiocb *kiocb,long res,long res2) { struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); -io_complete_rw_common (kiocb, res); -io_put_req (req); +if (!io_rw_reissue (req, res)) { +io_complete_rw_common (kiocb, res); +io_put_req (req); + } }static void io_complete_rw_iopoll (struct kiocb *kiocb,long res,long res2)
@@ -2169,6 +2279,9 @@ static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,if (kiocb->ki_flags &IOCB_NOWAIT ) req->flags |=REQ_F_NOWAIT ; +if (kiocb->ki_flags &IOCB_DIRECT ) +io_get_req_task (req); +if (force_nonblock) kiocb->ki_flags |=IOCB_NOWAIT ;
@@ -2668,6 +2781,7 @@ static int io_read(struct io_kiocb *req, bool force_nonblock) iov_count =iov_iter_count (&iter); ret =rw_verify_area (READ , req->file, &kiocb->ki_pos, iov_count);if (!ret) { +unsigned long nr_segs = iter.nr_segs;ssize_t ret2 =0 ;if (req->file->f_op->read_iter)
@@ -2679,6 +2793,8 @@ static int io_read(struct io_kiocb *req, bool force_nonblock)if (!force_nonblock || (ret2 != -EAGAIN && ret2 != -EIO )) {kiocb_done (kiocb, ret2); }else { + iter.count = iov_count; + iter.nr_segs = nr_segs; copy_iov: ret =io_setup_async_rw (req, io_size, iovec, inline_vecs, &iter);
@@ -2765,6 +2881,7 @@ static int io_write(struct io_kiocb *req, bool force_nonblock) iov_count =iov_iter_count (&iter); ret =rw_verify_area (WRITE , req->file, &kiocb->ki_pos, iov_count);if (!ret) { +unsigned long nr_segs = iter.nr_segs;ssize_t ret2;/*
@@ -2802,6 +2919,8 @@ static int io_write(struct io_kiocb *req, bool force_nonblock)if (!force_nonblock || ret2 != -EAGAIN ) {kiocb_done (kiocb, ret2); }else { + iter.count = iov_count; + iter.nr_segs = nr_segs; copy_iov: ret =io_setup_async_rw (req, io_size, iovec, inline_vecs, &iter);
@@ -4282,28 +4401,6 @@ static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,__io_queue_proc (&pt->req->apoll->poll, pt, head); } -static void io_sq_thread_drop_mm (struct io_ring_ctx *ctx) -{ -struct mm_struct *mm = current->mm; - -if (mm) { -kthread_unuse_mm (mm); -mmput (mm); - } -} - -static int io_sq_thread_acquire_mm (struct io_ring_ctx *ctx, -struct io_kiocb *req) -{ -if (io_op_defs[req->opcode].needs_mm && !current->mm) { -if (unlikely (!mmget_not_zero (ctx->sqo_mm))) -return -EFAULT ; -kthread_use_mm (ctx->sqo_mm); - } - -return 0 ; -} -static void io_async_task_func (struct callback_head *cb) { struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
@@ -5814,6 +5911,9 @@ static void io_submit_state_start(struct io_submit_state *state, unsigned int max_ios) {blk_start_plug (&state->plug); +#ifdef CONFIG_BLOCK + state->plug.nowait = true; +#endif state->free_reqs =0 ; state->file =NULL ; state->ios_left = max_ios;
Signature · 1 line
2.27.0
Re: [PATCH 04/15] io_uring: re-issue block requests that failed because of resources
Quoted 25 lines · Jens Axboe
Mark the plug with nowait == true, which will cause requests to avoid blocking on request allocation. If they do, we catch them and reissue them from a
task_workbased handler.Normally we can catch
-EAGAINdirectly, but the hard case is for split requests. As an example, the application issues a 512KB request. The block core will split this into 128KB if that's the max size for the device. The first request issues just fine, but we run into-EAGAINfor some latter splits for the same request. As the bio is split, we don't get to see the-EAGAINuntil one of the actual reads complete, and hence we cannot handle it inline as part of submission.This does potentially cause re-reads of parts of the range, as the whole request is reissued. There's currently no better way to handle this.
Signed-off-by Jens Axboe <[email protected]>--- fs/io_uring.c | 148 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 124 insertions(+), 24 deletions(-)fs/io_uring.c +0
-0
fs/io_uring.c static int io_file_get(struct io_submit_state *state, struct io_kiocb *req, · line 902
Quoted 3 lines
fs/io_uring.c +0
-0@@ -900,6 +900,13 @@ static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,static void __io_queue_sqe (struct io_kiocb *req,const struct io_uring_sqe *sqe);
...> +
Quoted 4 lines
fs/io_uring.c +4
-0+static void io_rw_resubmit (struct callback_head *cb) +{ + struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); +struct io_ring_ctx *ctx = req->ctx;
fs/io_uring.c static void io_complete_rw_common(struct kiocb *kiocb, long res) · line 2067
Quoted 10 lines
fs/io_uring.c +10
-0+int err; + +__set_current_state (TASK_RUNNING ); + + err =io_sq_thread_acquire_mm (ctx, req); + +if (io_resubmit_prep (req, err)) { +refcount_inc (&req->refs); +io_queue_async_work (req); + }
Hmm, I have similar stuff but for iopoll. On top removing grab_env* for linked reqs and some extra. I think I'll rebase on top of this.
Quoted 5 lines
fs/io_uring.c +5
-0+} +#endif + +static bool io_rw_reissue (struct io_kiocb *req,long res) +{
fs/io_uring.c static void io_complete_rw_common(struct kiocb *kiocb, long res) · line 2082
Quoted 10 lines
fs/io_uring.c +10
-0+#ifdef CONFIG_BLOCK +struct task_struct *tsk; +int ret; + +if ((res != -EAGAIN && res != -EOPNOTSUPP ) ||io_wq_current_is_worker ()) +return false; + + tsk = req->task; +init_task_work (&req->task_work, io_rw_resubmit); + ret =task_work_add (tsk, &req->task_work, true);
I don't like that the request becomes un-discoverable for cancellation awhile sitting in the task_work list. Poll stuff at least have hash_node for that.
Quoted 6 lines
fs/io_uring.c +6
-0+if (!ret) +return true; +#endif +return false; +} +
Signature · 1 line
Pavel Begunkov
Jens Axboe
maintainerRe: [PATCH 04/15] io_uring: re-issue block requests that failed because of resources
Quoted 39 lines · Pavel Begunkov
Quoted 25 lines · Jens Axboe
Mark the plug with nowait == true, which will cause requests to avoid blocking on request allocation. If they do, we catch them and reissue them from a
task_workbased handler.Normally we can catch
-EAGAINdirectly, but the hard case is for split requests. As an example, the application issues a 512KB request. The block core will split this into 128KB if that's the max size for the device. The first request issues just fine, but we run into-EAGAINfor some latter splits for the same request. As the bio is split, we don't get to see the-EAGAINuntil one of the actual reads complete, and hence we cannot handle it inline as part of submission.This does potentially cause re-reads of parts of the range, as the whole request is reissued. There's currently no better way to handle this.
Signed-off-by Jens Axboe <[email protected]>--- fs/io_uring.c | 148 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 124 insertions(+), 24 deletions(-)fs/io_uring.c +0
-0fs/io_uring.c static int io_file_get(struct io_submit_state *state, struct io_kiocb *req, · line 902
Quoted 3 lines
fs/io_uring.c +0
-0@@ -900,6 +900,13 @@ static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,static void __io_queue_sqe (struct io_kiocb *req,const struct io_uring_sqe *sqe);...> +
Quoted 9 lines
fs/io_uring.c +9
-0+static void io_rw_resubmit (struct callback_head *cb) +{ + struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); +struct io_ring_ctx *ctx = req->ctx; +int err; + +__set_current_state (TASK_RUNNING ); + + err =io_sq_thread_acquire_mm (ctx, req);
Quoted 8 lines
fs/io_uring.c static void io_complete_rw_common(struct kiocb *kiocb, long res) · line 2067
Quoted 5 lines
fs/io_uring.c +5
-0+ +if (io_resubmit_prep (req, err)) { +refcount_inc (&req->refs); +io_queue_async_work (req); + }Hmm, I have similar stuff but for iopoll. On top removing grab_env* for linked reqs and some extra. I think I'll rebase on top of this.
Yes, there's certainly overlap there. I consider this series basically wrapped up, so feel free to just base on top of it.
Quoted 8 lines
Quoted 8 lines
fs/io_uring.c +8
-0+static bool io_rw_reissue (struct io_kiocb *req,long res) +{ +#ifdef CONFIG_BLOCK +struct task_struct *tsk; +int ret; + +if ((res != -EAGAIN && res != -EOPNOTSUPP ) ||io_wq_current_is_worker ()) +return false;
Quoted 8 lines
fs/io_uring.c static void io_complete_rw_common(struct kiocb *kiocb, long res) · line 2082
Quoted 4 lines
fs/io_uring.c +4
-0+ + tsk = req->task; +init_task_work (&req->task_work, io_rw_resubmit); + ret =task_work_add (tsk, &req->task_work, true);I don't like that the request becomes un-discoverable for cancellation awhile sitting in the
task_worklist. Poll stuff at least havehash_nodefor that.
Async buffered IO was never cancelable, so it doesn't really matter. It's tied to the task, so we know it'll get executed - either run, or canceled if the task is going away. This is really not that different from having the work discoverable through io-wq queueing before, since the latter could never be canceled anyway as it sits there uninterruptibly waiting for IO completion.
Signature · 1 line
Jens Axboe
Re: [PATCH 04/15] io_uring: re-issue block requests that failed because of resources
Quoted 69 lines · Jens Axboe
Quoted 39 lines · Pavel Begunkov
Quoted 25 lines · Jens Axboe
Mark the plug with nowait == true, which will cause requests to avoid blocking on request allocation. If they do, we catch them and reissue them from a
task_workbased handler.Normally we can catch
-EAGAINdirectly, but the hard case is for split requests. As an example, the application issues a 512KB request. The block core will split this into 128KB if that's the max size for the device. The first request issues just fine, but we run into-EAGAINfor some latter splits for the same request. As the bio is split, we don't get to see the-EAGAINuntil one of the actual reads complete, and hence we cannot handle it inline as part of submission.This does potentially cause re-reads of parts of the range, as the whole request is reissued. There's currently no better way to handle this.
Signed-off-by Jens Axboe <[email protected]>--- fs/io_uring.c | 148 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 124 insertions(+), 24 deletions(-)fs/io_uring.c +0
-0fs/io_uring.c static int io_file_get(struct io_submit_state *state, struct io_kiocb *req, · line 902
Quoted 3 lines
fs/io_uring.c +0
-0@@ -900,6 +900,13 @@ static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,static void __io_queue_sqe (struct io_kiocb *req,const struct io_uring_sqe *sqe);...> +
Quoted 9 lines
fs/io_uring.c +9
-0+static void io_rw_resubmit (struct callback_head *cb) +{ + struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); +struct io_ring_ctx *ctx = req->ctx; +int err; + +__set_current_state (TASK_RUNNING ); + + err =io_sq_thread_acquire_mm (ctx, req);Quoted 8 lines
fs/io_uring.c static void io_complete_rw_common(struct kiocb *kiocb, long res) · line 2067
Quoted 5 lines
fs/io_uring.c +5
-0+ +if (io_resubmit_prep (req, err)) { +refcount_inc (&req->refs); +io_queue_async_work (req); + }Hmm, I have similar stuff but for iopoll. On top removing grab_env* for linked reqs and some extra. I think I'll rebase on top of this.
Yes, there's certainly overlap there. I consider this series basically wrapped up, so feel free to just base on top of it.
Quoted 8 lines
Quoted 8 lines
fs/io_uring.c +8
-0+static bool io_rw_reissue (struct io_kiocb *req,long res) +{ +#ifdef CONFIG_BLOCK +struct task_struct *tsk; +int ret; + +if ((res != -EAGAIN && res != -EOPNOTSUPP ) ||io_wq_current_is_worker ()) +return false;Quoted 8 lines
fs/io_uring.c static void io_complete_rw_common(struct kiocb *kiocb, long res) · line 2082
Quoted 4 lines
fs/io_uring.c +4
-0+ + tsk = req->task; +init_task_work (&req->task_work, io_rw_resubmit); + ret =task_work_add (tsk, &req->task_work, true);I don't like that the request becomes un-discoverable for cancellation awhile sitting in the
task_worklist. Poll stuff at least havehash_nodefor that.
Quoted 6 lines
Async buffered IO was never cancelable, so it doesn't really matter. It's tied to the task, so we know it'll get executed - either run, or canceled if the task is going away. This is really not that different from having the work discoverable through io-wq queueing before, since the latter could never be canceled anyway as it sits there uninterruptibly waiting for IO completion.
Makes sense. I was thinking about using this task-requeue for all kinds of requests. Though, instead of speculating it'd be better for me to embody ideas into patches and see.
Signature · 1 line
Pavel Begunkov
Jens Axboe
maintainerRe: [PATCH 04/15] io_uring: re-issue block requests that failed because of resources
Quoted 77 lines · Pavel Begunkov
Quoted 69 lines · Jens Axboe
Quoted 39 lines · Pavel Begunkov
Quoted 25 lines · Jens Axboe
Mark the plug with nowait == true, which will cause requests to avoid blocking on request allocation. If they do, we catch them and reissue them from a
task_workbased handler.Normally we can catch
-EAGAINdirectly, but the hard case is for split requests. As an example, the application issues a 512KB request. The block core will split this into 128KB if that's the max size for the device. The first request issues just fine, but we run into-EAGAINfor some latter splits for the same request. As the bio is split, we don't get to see the-EAGAINuntil one of the actual reads complete, and hence we cannot handle it inline as part of submission.This does potentially cause re-reads of parts of the range, as the whole request is reissued. There's currently no better way to handle this.
Signed-off-by Jens Axboe <[email protected]>--- fs/io_uring.c | 148 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 124 insertions(+), 24 deletions(-)fs/io_uring.c +0
-0fs/io_uring.c static int io_file_get(struct io_submit_state *state, struct io_kiocb *req, · line 902
Quoted 3 lines
fs/io_uring.c +0
-0@@ -900,6 +900,13 @@ static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,static void __io_queue_sqe (struct io_kiocb *req,const struct io_uring_sqe *sqe);...> +
Quoted 9 lines
fs/io_uring.c +9
-0+static void io_rw_resubmit (struct callback_head *cb) +{ + struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); +struct io_ring_ctx *ctx = req->ctx; +int err; + +__set_current_state (TASK_RUNNING ); + + err =io_sq_thread_acquire_mm (ctx, req);Quoted 8 lines
fs/io_uring.c static void io_complete_rw_common(struct kiocb *kiocb, long res) · line 2067
Quoted 5 lines
fs/io_uring.c +5
-0+ +if (io_resubmit_prep (req, err)) { +refcount_inc (&req->refs); +io_queue_async_work (req); + }Hmm, I have similar stuff but for iopoll. On top removing grab_env* for linked reqs and some extra. I think I'll rebase on top of this.
Yes, there's certainly overlap there. I consider this series basically wrapped up, so feel free to just base on top of it.
Quoted 8 lines
Quoted 8 lines
fs/io_uring.c +8
-0+static bool io_rw_reissue (struct io_kiocb *req,long res) +{ +#ifdef CONFIG_BLOCK +struct task_struct *tsk; +int ret; + +if ((res != -EAGAIN && res != -EOPNOTSUPP ) ||io_wq_current_is_worker ()) +return false;Quoted 8 lines
fs/io_uring.c static void io_complete_rw_common(struct kiocb *kiocb, long res) · line 2082
Quoted 4 lines
fs/io_uring.c +4
-0+ + tsk = req->task; +init_task_work (&req->task_work, io_rw_resubmit); + ret =task_work_add (tsk, &req->task_work, true);I don't like that the request becomes un-discoverable for cancellation awhile sitting in the
task_worklist. Poll stuff at least havehash_nodefor that.Quoted 6 lines
Async buffered IO was never cancelable, so it doesn't really matter. It's tied to the task, so we know it'll get executed - either run, or canceled if the task is going away. This is really not that different from having the work discoverable through io-wq queueing before, since the latter could never be canceled anyway as it sits there uninterruptibly waiting for IO completion.
Quoted 3 lines
Makes sense. I was thinking about using this task-requeue for all kinds of requests. Though, instead of speculating it'd be better for me to embody ideas into patches and see.
And that's fine, for requests where it matters, on-the-side discoverability can still be a thing. If we're in the task itself where it is queued, that provides us safey from the work going way from under us. Then we just have to mark it appropriately, if it needs to get canceled instead of run to completion.
Some care needed, of course, but there's nothing that would prevent this from working. Ideally we'd be able to peal off a task_work entry, but that's kind of difficult with the singly linked non-locked list.
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerPATCH 5/15 mm: allow read-ahead with IOCB_NOWAIT set
In reply to Jens AxboeThe read-ahead shouldn't block, so allow it to be done even if IOCB_NOWAIT is set in the kiocb.
--- mm/filemap.c | 2 -- 1 file changed, 2 deletions(-)
mm/filemap.c +0 -2
@@ -2028,8 +2028,6 @@ ssize_t generic_file_buffered_read(struct kiocb *iocb, page =find_get_page (mapping, index);if (!page) { -if (iocb->ki_flags &IOCB_NOWAIT ) - goto would_block;page_cache_sync_readahead (mapping, ra, filp, index, last_index - index);
Signature · 1 line
2.27.0
Re: [PATCH 05/15] mm: allow read-ahead with IOCB_NOWAIT set
Quoted 14 lines · Jens Axboe
The read-ahead shouldn't block, so allow it to be done even if
IOCB_NOWAITis set in the kiocb.Acked-by Johannes Weiner <[email protected]>Signed-off-by Jens Axboe <[email protected]>--- mm/filemap.c | 2 -- 1 file changed, 2 deletions(-)mm/filemap.c +0
-0
mm/filemap.c ssize_t generic_file_buffered_read(struct kiocb *iocb, · line 2033
Quoted 8 lines
mm/filemap.c +0
-2@@ -2028,8 +2028,6 @@ ssize_t generic_file_buffered_read(struct kiocb *iocb, page =find_get_page (mapping, index);if (!page) { -if (iocb->ki_flags &IOCB_NOWAIT ) - goto would_block;page_cache_sync_readahead (mapping, ra, filp, index, last_index - index);
Doesn't think break preadv2(RWF_NOWAIT) semantics for on buffered reads? i.e. this can now block on memory allocation for the page cache, which is something RWF_NOWAIT IO should not do....
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
Matthew Wilcox
maintainerRe: [PATCH 05/15] mm: allow read-ahead with IOCB_NOWAIT set
Quoted 7 lines · Dave Chinner
Quoted 2 lines · Jens Axboe
The read-ahead shouldn't block, so allow it to be done even if
IOCB_NOWAITis set in the kiocb.Doesn't think break
preadv2(RWF_NOWAIT)semantics for on buffered reads? i.e. this can now block on memory allocation for the page cache, which is somethingRWF_NOWAITIO should not do....
Yes. This eventually ends up in page_cache_readahead_unbounded() which gets its gfp flags from readahead_gfp_mask(mapping).
I'd be quite happy to add a gfp_t to struct readahead_control. The other thing I've been looking into for other reasons is adding a memalloc_nowait_{save,restore}, which would avoid passing down the gfp_t.
Jens Axboe
maintainerRe: [PATCH 05/15] mm: allow read-ahead with IOCB_NOWAIT set
Quoted 9 lines · Matthew Wilcox
Quoted 7 lines · Dave Chinner
Quoted 2 lines · Jens Axboe
The read-ahead shouldn't block, so allow it to be done even if
IOCB_NOWAITis set in the kiocb.Doesn't think break
preadv2(RWF_NOWAIT)semantics for on buffered reads? i.e. this can now block on memory allocation for the page cache, which is somethingRWF_NOWAITIO should not do....
Quoted 7 lines
Yes. This eventually ends up in
page_cache_readahead_unbounded()which gets its gfp flags fromreadahead_gfp_mask(mapping).I'd be quite happy to add a
gfp_tto structreadahead_control. The other thing I've been looking into for other reasons is adding a memalloc_nowait_{save,restore}, which would avoid passing down thegfp_t.
That was my first thought, having the memalloc_foo_save/restore for this. I don't think adding a gfp_t to readahead_control is going to be super useful, seems like the kind of thing that should be non-blocking by default.
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerRe: [PATCH 05/15] mm: allow read-ahead with IOCB_NOWAIT set
Quoted 18 lines · Jens Axboe
Quoted 9 lines · Matthew Wilcox
Quoted 7 lines · Dave Chinner
Quoted 2 lines · Jens Axboe
The read-ahead shouldn't block, so allow it to be done even if
IOCB_NOWAITis set in the kiocb.Doesn't think break
preadv2(RWF_NOWAIT)semantics for on buffered reads? i.e. this can now block on memory allocation for the page cache, which is somethingRWF_NOWAITIO should not do....Quoted 7 lines
Yes. This eventually ends up in
page_cache_readahead_unbounded()which gets its gfp flags fromreadahead_gfp_mask(mapping).I'd be quite happy to add a
gfp_tto structreadahead_control. The other thing I've been looking into for other reasons is adding a memalloc_nowait_{save,restore}, which would avoid passing down thegfp_t.
Quoted 4 lines
That was my first thought, having the memalloc_foo_save/restore for this. I don't think adding a
gfp_ttoreadahead_controlis going to be super useful, seems like the kind of thing that should be non-blocking by default.
We're already doing memalloc_nofs_save/restore in page_cache_readahead_unbounded(), so I think all we need is to just do a noio dance in generic_file_buffered_read() and that should be enough.
mm/filemap.c +10 -2
@@ -41,6 +41,7 @@#include <linux/delayacct.h> #include <linux/psi.h> #include <linux/ramfs.h> +#include <linux/sched/mm.h> #include "internal.h" #define CREATE_TRACE_POINTS
@@ -2011,6 +2012,7 @@ ssize_t generic_file_buffered_read(struct kiocb *iocb,struct address_space *mapping = filp->f_mapping;struct inode *inode = mapping->host;struct file_ra_state *ra = &filp->f_ra; +const bool nowait = (iocb->ki_flags &IOCB_NOWAIT ) !=0 ;loff_t *ppos = &iocb->ki_pos;pgoff_t index;pgoff_t last_index;
@@ -2044,9 +2046,15 @@ ssize_t generic_file_buffered_read(struct kiocb *iocb, page =find_get_page (mapping, index);if (!page) { +unsigned int flags; + +if (nowait) + flags =memalloc_noio_save ();page_cache_sync_readahead (mapping, ra, filp, index, last_index - index); +if (nowait) +memalloc_noio_restore (flags); page =find_get_page (mapping, index);if (unlikely (page ==NULL )) goto no_cached_page;
@@ -2070,7 +2078,7 @@ ssize_t generic_file_buffered_read(struct kiocb *iocb, error =wait_on_page_locked_async (page, iocb->ki_waitq); }else { -if (iocb->ki_flags &IOCB_NOWAIT ) { +if (nowait) {put_page (page); goto would_block; }
@@ -2185,7 +2193,7 @@ ssize_t generic_file_buffered_read(struct kiocb *iocb, } readpage: -if (iocb->ki_flags &IOCB_NOWAIT ) { +if (nowait) {unlock_page (page);put_page (page); goto would_block;
Signature · 1 line
Jens Axboe
Matthew Wilcox
maintainerRe: [PATCH 05/15] mm: allow read-ahead with IOCB_NOWAIT set
Quoted 7 lines · Jens Axboe
Quoted 6 lines · Jens Axboe
Quoted 4 lines · Matthew Wilcox
I'd be quite happy to add a
gfp_tto structreadahead_control. The other thing I've been looking into for other reasons is adding a memalloc_nowait_{save,restore}, which would avoid passing down thegfp_t.
Quoted 8 lines
Quoted 4 lines
That was my first thought, having the memalloc_foo_save/restore for this. I don't think adding a
gfp_ttoreadahead_controlis going to be super useful, seems like the kind of thing that should be non-blocking by default.We're already doing memalloc_nofs_save/restore in
page_cache_readahead_unbounded(), so I think all we need is to just do a noio dance ingeneric_file_buffered_read()and that should be enough.
I think we can still sleep though, right? I was thinking more like this:
http://git.infradead.org/users/willy/linux.git/shortlog/refs/heads/memalloc
Jens Axboe
maintainerRe: [PATCH 05/15] mm: allow read-ahead with IOCB_NOWAIT set
Quoted 13 lines · Matthew Wilcox
Quoted 12 lines · Jens Axboe
Quoted 10 lines · Jens Axboe
Quoted 4 lines · Matthew Wilcox
I'd be quite happy to add a
gfp_tto structreadahead_control. The other thing I've been looking into for other reasons is adding a memalloc_nowait_{save,restore}, which would avoid passing down thegfp_t.That was my first thought, having the memalloc_foo_save/restore for this. I don't think adding a
gfp_ttoreadahead_controlis going to be super useful, seems like the kind of thing that should be non-blocking by default.
Quoted 8 lines
Quoted 3 lines
We're already doing memalloc_nofs_save/restore in
page_cache_readahead_unbounded(), so I think all we need is to just do a noio dance ingeneric_file_buffered_read()and that should be enough.I think we can still sleep though, right? I was thinking more like this:
http://git.infradead.org/users/willy/linux.git/shortlog/refs/heads/memalloc
Yeah, that's probably better. How do we want to handle this? I've already got the other bits queued up. I can either add them to the series, or pull a branch that'll go into Linus as well.
Signature · 1 line
Jens Axboe
Re: [PATCH 05/15] mm: allow read-ahead with IOCB_NOWAIT set
Am Mi., 24. Juni 2020 um 18:48 Uhr schrieb Jens Axboe <[email protected]>:
Quoted 19 lines · Matthew Wilcox
Quoted 17 lines · Jens Axboe
Quoted 15 lines · Jens Axboe
Quoted 10 lines · Matthew Wilcox
Quoted 4 lines
I'd be quite happy to add a
gfp_tto structreadahead_control. The other thing I've been looking into for other reasons is adding a memalloc_nowait_{save,restore}, which would avoid passing down thegfp_t.That was my first thought, having the memalloc_foo_save/restore for this. I don't think adding a
gfp_ttoreadahead_controlis going to be super useful, seems like the kind of thing that should be non-blocking by default.We're already doing memalloc_nofs_save/restore in
page_cache_readahead_unbounded(), so I think all we need is to just do a noio dance ingeneric_file_buffered_read()and that should be enough.
Quoted 8 lines
Quoted 4 lines
I think we can still sleep though, right? I was thinking more like this:
http://git.infradead.org/users/willy/linux.git/shortlog/refs/heads/memalloc
Yeah, that's probably better. How do we want to handle this? I've already got the other bits queued up. I can either add them to the series, or pull a branch that'll go into Linus as well.
Also note my conflicting patch that introduces a IOCB_NOIO flag for fixing a gfs2 regression:
https://lore.kernel.org/linux-fsdevel/[email protected]/
Thanks,
Andreas
Jens Axboe
maintainerRe: [PATCH 05/15] mm: allow read-ahead with IOCB_NOWAIT set
Quoted 25 lines · Andreas Grünbacher
Am Mi., 24. Juni 2020 um 18:48 Uhr schrieb Jens Axboe <[email protected]>:
Quoted 24 lines · Matthew Wilcox
Quoted 21 lines · Jens Axboe
Quoted 7 lines · Jens Axboe
Quoted 6 lines · Matthew Wilcox
Quoted 4 lines
I'd be quite happy to add a
gfp_tto structreadahead_control. The other thing I've been looking into for other reasons is adding a memalloc_nowait_{save,restore}, which would avoid passing down thegfp_t.Quoted 8 lines
Quoted 4 lines
That was my first thought, having the memalloc_foo_save/restore for this. I don't think adding a
gfp_ttoreadahead_controlis going to be super useful, seems like the kind of thing that should be non-blocking by default.We're already doing memalloc_nofs_save/restore in
page_cache_readahead_unbounded(), so I think all we need is to just do a noio dance ingeneric_file_buffered_read()and that should be enough.I think we can still sleep though, right? I was thinking more like this:
http://git.infradead.org/users/willy/linux.git/shortlog/refs/heads/memalloc
Quoted 8 lines
Quoted 3 lines
Yeah, that's probably better. How do we want to handle this? I've already got the other bits queued up. I can either add them to the series, or pull a branch that'll go into Linus as well.
Also note my conflicting patch that introduces a
IOCB_NOIOflag for fixing a gfs2 regression:
Yeah I noticed, pretty easy to resolve though.
Signature · 1 line
Jens Axboe
Re: [PATCH 05/15] mm: allow read-ahead with IOCB_NOWAIT set
In reply to Jens AxboeQuoted 18 lines · Jens Axboe
Quoted 17 lines · Matthew Wilcox
Quoted 15 lines · Jens Axboe
Quoted 10 lines · Jens Axboe
Quoted 4 lines · Matthew Wilcox
I'd be quite happy to add a
gfp_tto structreadahead_control. The other thing I've been looking into for other reasons is adding a memalloc_nowait_{save,restore}, which would avoid passing down thegfp_t.That was my first thought, having the memalloc_foo_save/restore for this. I don't think adding a
gfp_ttoreadahead_controlis going to be super useful, seems like the kind of thing that should be non-blocking by default.We're already doing memalloc_nofs_save/restore in
page_cache_readahead_unbounded(), so I think all we need is to just do a noio dance ingeneric_file_buffered_read()and that should be enough.
Quoted 8 lines
Quoted 4 lines
I think we can still sleep though, right? I was thinking more like this:
http://git.infradead.org/users/willy/linux.git/shortlog/refs/heads/memalloc
Yeah, that's probably better. How do we want to handle this? I've already got the other bits queued up. I can either add them to the series, or pull a branch that'll go into Linus as well.
Jens, Willy,
Now that this patch has been merged and IOCB_NOWAIT semantics ifor buffered reads are broken in Linus' tree, what's the plan to get this regression fixed before 5.9 releases?
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
Jens Axboe
maintainerRe: [PATCH 05/15] mm: allow read-ahead with IOCB_NOWAIT set
Quoted 28 lines · Dave Chinner
Quoted 18 lines · Jens Axboe
Quoted 17 lines · Matthew Wilcox
Quoted 15 lines · Jens Axboe
Quoted 10 lines · Jens Axboe
Quoted 4 lines · Matthew Wilcox
I'd be quite happy to add a
gfp_tto structreadahead_control. The other thing I've been looking into for other reasons is adding a memalloc_nowait_{save,restore}, which would avoid passing down thegfp_t.That was my first thought, having the memalloc_foo_save/restore for this. I don't think adding a
gfp_ttoreadahead_controlis going to be super useful, seems like the kind of thing that should be non-blocking by default.We're already doing memalloc_nofs_save/restore in
page_cache_readahead_unbounded(), so I think all we need is to just do a noio dance ingeneric_file_buffered_read()and that should be enough.Quoted 8 lines
Quoted 4 lines
I think we can still sleep though, right? I was thinking more like this:
http://git.infradead.org/users/willy/linux.git/shortlog/refs/heads/memalloc
Yeah, that's probably better. How do we want to handle this? I've already got the other bits queued up. I can either add them to the series, or pull a branch that'll go into Linus as well.
Quoted 5 lines
Jens, Willy,
Now that this patch has been merged and
IOCB_NOWAITsemantics ifor buffered reads are broken in Linus' tree, what's the plan to get this regression fixed before 5.9 releases?
Not sure where Willy's work went on this topic, but it is on my radar. But I think we can do something truly simple now that we have IOCB_NOIO:
include/linux/fs.h +1 -1
@@ -3293,7 +3293,7 @@ static inline int kiocb_set_rw_flags(struct kiocb *ki, rwf_t flags)if (flags &RWF_NOWAIT ) {if (!(ki->ki_filp->f_mode &FMODE_NOWAIT ))return -EOPNOTSUPP ; - kiocb_flags |=IOCB_NOWAIT ; + kiocb_flags |=IOCB_NOWAIT |IOCB_NOIO ; }if (flags &RWF_HIPRI ) kiocb_flags |=IOCB_HIPRI ;
Signature · 1 line
Jens Axboe
Re: [PATCH 05/15] mm: allow read-ahead with IOCB_NOWAIT set
In reply to Jens AxboeQuoted 5 lines · Jens Axboe
The read-ahead shouldn't block, so allow it to be done even if
IOCB_NOWAITis set in the kiocb.Acked-by Johannes Weiner <[email protected]>Signed-off-by Jens Axboe <[email protected]>
BTW, Jens, in case nobody had mentioned it, the Reply-To field for the patches in this patchset is screwed up:
| Reply-To: [email protected], [email protected], [email protected], | [email protected], [email protected], | [email protected]
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
Jens Axboe
maintainerRe: [PATCH 05/15] mm: allow read-ahead with IOCB_NOWAIT set
Quoted 7 lines · Dave Chinner
Quoted 5 lines · Jens Axboe
The read-ahead shouldn't block, so allow it to be done even if
IOCB_NOWAITis set in the kiocb.Acked-by Johannes Weiner <[email protected]>Signed-off-by Jens Axboe <[email protected]>
Quoted 6 lines
BTW, Jens, in case nobody had mentioned it, the Reply-To field for the patches in this patchset is screwed up:
| Reply-To: [email protected], [email protected], [email protected], | [email protected], [email protected], | [email protected]
Yeah, I pasted the subject line into the wrong spot for git send-email, hence the reply-to is boogered, and the subject line was empty for the cover letter...
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerPATCH 6/15 mm: abstract out wake_page_match() from wake_page_function()
In reply to Jens AxboeNo functional changes in this patch, just in preparation for allowing more callers.
--- include/linux/pagemap.h | 37 +++++++++++++++++++++++++++++++++++++ mm/filemap.c | 35 ++++------------------------------- 2 files changed, 41 insertions(+), 31 deletions(-)
include/linux/pagemap.h +37 -0
@@ -496,6 +496,43 @@ static inline pgoff_t linear_page_index(struct vm_area_struct *vma,return pgoff; } +/* This has the same layout as wait_bit_key - see fs/cachefiles/rdwr.c */ +struct wait_page_key { +struct page *page; +int bit_nr; +int page_match; +}; + +struct wait_page_queue { +struct page *page; +int bit_nr; +wait_queue_entry_t wait; +}; + +static inline int wake_page_match (struct wait_page_queue *wait_page, +struct wait_page_key *key) +{ +if (wait_page->page != key->page) +return 0 ; + key->page_match =1 ; + +if (wait_page->bit_nr != key->bit_nr) +return 0 ; + +/* +* Stop walking if it's locked. +* Is this safe if put_and_wait_on_page_locked() is in use? +* Yes: the waker must hold a reference to this page, and if PG_locked +* has now already been set by another task, that task must also hold +* a reference to the *same usage* of this page; so there is no need +* to walk on to wake even the put_and_wait_on_page_locked() callers. +*/ +if (test_bit (key->bit_nr, &key->page->flags)) +return -1 ; + +return 1 ; +} +extern void __lock_page (struct page *page);extern int __lock_page_killable (struct page *page); extern int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
mm/filemap.c +4 -31
@@ -987,43 +987,16 @@ void __init pagecache_init(void)page_writeback_init (); } -/* This has the same layout as wait_bit_key - see fs/cachefiles/rdwr.c */ -struct wait_page_key { -struct page *page; -int bit_nr; -int page_match; -}; - -struct wait_page_queue { -struct page *page; -int bit_nr; -wait_queue_entry_t wait; -}; -static int wake_page_function (wait_queue_entry_t *wait,unsigned mode,int sync,void *arg) {struct wait_page_key *key = arg;struct wait_page_queue *wait_page = container_of(wait, struct wait_page_queue, wait); +int ret; -if (wait_page->page != key->page) -return 0 ; - key->page_match =1 ; - -if (wait_page->bit_nr != key->bit_nr) -return 0 ; - -/* -* Stop walking if it's locked. -* Is this safe if put_and_wait_on_page_locked() is in use? -* Yes: the waker must hold a reference to this page, and if PG_locked -* has now already been set by another task, that task must also hold -* a reference to the *same usage* of this page; so there is no need -* to walk on to wake even the put_and_wait_on_page_locked() callers. -*/ -if (test_bit (key->bit_nr, &key->page->flags)) -return -1 ; - + ret =wake_page_match (wait_page, key); +if (ret !=1 ) +return ret;return autoremove_wake_function (wait, mode, sync, key); }
Signature · 1 line
2.27.0
Jens Axboe
maintainerPATCH 7/15 mm: add support for async page locking
In reply to Jens AxboeNormally waiting for a page to become unlocked, or locking the page, requires waiting for IO to complete. Add support for lock_page_async() and wait_on_page_locked_async(), which are callback based instead. This allows a caller to get notified when a page becomes unlocked, rather than wait for it.
We add a new iocb field, ki_waitq, to pass in the necessary data for this to happen. We can unionize this with ki_cookie, since that is only used for polled IO. Polled IO can never co-exist with async callbacks, as it is (by definition) polled completions. struct wait_page_key is made public, and we define struct wait_page_async as the interface between the caller and the core.
--- include/linux/fs.h | 7 ++++++- include/linux/pagemap.h | 17 ++++++++++++++++ mm/filemap.c | 45 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 67 insertions(+), 2 deletions(-)
include/linux/fs.h +6 -1
@@ -315,6 +315,8 @@ enum rw_hint {#define IOCB_SYNC (1 << 5)#define IOCB_WRITE (1 << 6)#define IOCB_NOWAIT (1 << 7) +/* iocb->ki_waitq is valid */ +#define IOCB_WAITQ (1 << 8)struct kiocb {struct file *ki_filp;
@@ -328,7 +330,10 @@ struct kiocb {int ki_flags;u16 ki_hint;u16 ki_ioprio;/* See linux/ioprio.h */ -unsigned int ki_cookie;/* for ->iopoll */ +union { +unsigned int ki_cookie;/* for ->iopoll */ +struct wait_page_queue *ki_waitq;/* for async buffered IO */ + }; randomized_struct_fields_end };
include/linux/pagemap.h +17 -0
@@ -535,6 +535,7 @@ static inline int wake_page_match(struct wait_page_queue *wait_page,extern void __lock_page (struct page *page);extern int __lock_page_killable (struct page *page); +extern int __lock_page_async (struct page *page,struct wait_page_queue *wait);extern int __lock_page_or_retry (struct page *page,struct mm_struct *mm,unsigned int flags);extern void unlock_page (struct page *page);
@@ -571,6 +572,22 @@ static inline int lock_page_killable(struct page *page)return 0 ; } +/* +* lock_page_async - Lock the page, unless this would block. If the page +* is already locked, then queue a callback when the page becomes unlocked. +* This callback can then retry the operation. +* +* Returns 0 if the page is locked successfully, or -EIOCBQUEUED if the page +* was already locked and the callback defined in 'wait' was queued. +*/ +static inline int lock_page_async (struct page *page, +struct wait_page_queue *wait) +{ +if (!trylock_page (page)) +return __lock_page_async (page, wait); +return 0 ; +} +/* * lock_page_or_retry - Lock the page, unless this would block and the * caller indicated that it can handle a retry.
mm/filemap.c +44 -1
69 lines not on this page · raw message
Signature · 1 line
2.27.0
Re: [PATCH 07/15] mm: add support for async page locking
Jens,
Am Do., 18. Juni 2020 um 16:47 Uhr schrieb Jens Axboe <[email protected]>:
Quoted 135 lines
Normally waiting for a page to become unlocked, or locking the page, requires waiting for IO to complete. Add support for
lock_page_async()andwait_on_page_locked_async(), which are callback based instead. This allows a caller to get notified when a page becomes unlocked, rather than wait for it.We add a new iocb field,
ki_waitq, to pass in the necessary data for this to happen. We can unionize this withki_cookie, since that is only used for polled IO. Polled IO can never co-exist with async callbacks, as it is (by definition) polled completions. structwait_page_keyis made public, and we define structwait_page_asyncas the interface between the caller and the core.Acked-by Johannes Weiner <[email protected]>Signed-off-by Jens Axboe <[email protected]>--- include/linux/fs.h | 7 ++++++- include/linux/pagemap.h | 17 ++++++++++++++++ mm/filemap.c | 45 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 67 insertions(+), 2 deletions(-)include/linux/fs.h +6
-1@@ -315,6 +315,8 @@ enum rw_hint {#define IOCB_SYNC (1 << 5)#define IOCB_WRITE (1 << 6)#define IOCB_NOWAIT (1 << 7) +/* iocb->ki_waitq is valid */ +#define IOCB_WAITQ (1 << 8)struct kiocb {struct file *ki_filp;@@ -328,7 +330,10 @@ struct kiocb {int ki_flags;u16 ki_hint;u16 ki_ioprio;/* See linux/ioprio.h */ -unsigned int ki_cookie;/* for ->iopoll */ +union { +unsigned int ki_cookie;/* for ->iopoll */ +struct wait_page_queue *ki_waitq;/* for async buffered IO */ + }; randomized_struct_fields_end };include/linux/pagemap.h +17
-0@@ -535,6 +535,7 @@ static inline int wake_page_match(struct wait_page_queue *wait_page,extern void __lock_page (struct page *page);extern int __lock_page_killable (struct page *page); +extern int __lock_page_async (struct page *page,struct wait_page_queue *wait);extern int __lock_page_or_retry (struct page *page,struct mm_struct *mm,unsigned int flags);extern void unlock_page (struct page *page);@@ -571,6 +572,22 @@ static inline int lock_page_killable(struct page *page)return 0 ; } +/* +* lock_page_async - Lock the page, unless this would block. If the page +* is already locked, then queue a callback when the page becomes unlocked. +* This callback can then retry the operation. +* +* Returns 0 if the page is locked successfully, or -EIOCBQUEUED if the page +* was already locked and the callback defined in 'wait' was queued. +*/ +static inline int lock_page_async (struct page *page, +struct wait_page_queue *wait) +{ +if (!trylock_page (page)) +return __lock_page_async (page, wait); +return 0 ; +} +/* * lock_page_or_retry - Lock the page, unless this would block and the * caller indicated that it can handle a retry. mm/filemap.c +35
-0@@ -1180,6 +1180,36 @@ int wait_on_page_bit_killable(struct page *page, int bit_nr) }EXPORT_SYMBOL (wait_on_page_bit_killable); +static int __wait_on_page_locked_async (struct page *page, +struct wait_page_queue *wait,bool set) +{ +struct wait_queue_head *q =page_waitqueue (page); +int ret =0 ; + + wait->page = page; + wait->bit_nr = PG_locked; + +spin_lock_irq (&q->lock); +__add_wait_queue_entry_tail (q, &wait->wait); +SetPageWaiters (page); +if (set) + ret = !trylock_page (page); +else + ret =PageLocked (page); +/* +* If we were succesful now, we know we're still on the +* waitqueue as we're still under the lock. This means it's +* safe to remove and return success, we know the callback +* isn't going to trigger. +*/ +if (!ret) +__remove_wait_queue (q, &wait->wait); +else + ret = -EIOCBQUEUED ; +spin_unlock_irq (&q->lock); +return ret; +} +/** * put_and_wait_on_page_locked - Drop a reference and wait for it to be unlocked * @page: The page to wait for. @@ -1342,6 +1372,11 @@ int __lock_page_killable(struct page *__page) }EXPORT_SYMBOL_GPL (__lock_page_killable); +int __lock_page_async (struct page *page,struct wait_page_queue *wait) +{ +return __wait_on_page_locked_async (page, wait, true); +} +/* * Return values: * 1 - page is locked; mmap_lock is still held.
mm/filemap.c ssize_t generic_file_buffered_read(struct kiocb *iocb, · line 2173
Quoted 8 lines
mm/filemap.c +5
-0@@ -2131,6 +2166,11 @@ ssize_t generic_file_buffered_read(struct kiocb *iocb, } readpage: +if (iocb->ki_flags &IOCB_NOWAIT ) { +unlock_page (page); +put_page (page); + goto would_block; + }
This hunk should have been part of "mm: allow read-ahead with IOCB_NOWAIT set" ...
Quoted 16 lines
mm/filemap.c +4
-2/* * A previous I/O error may have been due to temporary * failures, eg. multipath errors. @@ -2150,7 +2190,10 @@ ssize_t generic_file_buffered_read(struct kiocb *iocb, }if (!PageUptodate (page)) { - error =lock_page_killable (page); +if (iocb->ki_flags &IOCB_WAITQ ) + error =lock_page_async (page, iocb->ki_waitq); +else + error =lock_page_killable (page);if (unlikely (error)) goto readpage_error;if (!PageUptodate (page)) { --
Quoted 2 lines
2.27.0
Andreas
Jens Axboe
maintainerRe: [PATCH 07/15] mm: add support for async page locking
Quoted 12 lines · Andreas Grünbacher
mm/filemap.c ssize_t generic_file_buffered_read(struct kiocb *iocb, · line 2173
Quoted 9 lines
mm/filemap.c +5
-0@@ -2131,6 +2166,11 @@ ssize_t generic_file_buffered_read(struct kiocb *iocb, } readpage: +if (iocb->ki_flags &IOCB_NOWAIT ) { +unlock_page (page); +put_page (page); + goto would_block; + }This hunk should have been part of "mm: allow read-ahead with
IOCB_NOWAITset" ...
It probably should have... Oh well, it's been queued up multiple weeks at this point, not worth rebasing to move this hunk.
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerPATCH 8/15 mm: support async buffered reads in generic_file_buffered_read()
In reply to Jens AxboeUse the async page locking infrastructure, if IOCB_WAITQ is set in the passed in iocb. The caller must expect an -EIOCBQUEUED return value, which means that IO is started but not done yet. This is similar to how O_DIRECT signals the same operation. Once the callback is received by the caller for IO completion, the caller must retry the operation.
--- mm/filemap.c | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-)
mm/filemap.c +27 -11
@@ -1210,6 +1210,14 @@ static int __wait_on_page_locked_async(struct page *page,return ret; } +static int wait_on_page_locked_async (struct page *page, +struct wait_page_queue *wait) +{ +if (!PageLocked (page)) +return 0 ; +return __wait_on_page_locked_async (compound_head (page), wait, false); +} +/** * put_and_wait_on_page_locked - Drop a reference and wait for it to be unlocked * @page: The page to wait for.
@@ -2049,17 +2057,25 @@ ssize_t generic_file_buffered_read(struct kiocb *iocb, index, last_index - index); }if (!PageUptodate (page)) { -if (iocb->ki_flags &IOCB_NOWAIT ) { -put_page (page); - goto would_block; - } -/* * See comment in do_read_cache_page on why * wait_on_page_locked is used to avoid unnecessarily * serialisations and why it's safe. */ - error =wait_on_page_locked_killable (page); +if (iocb->ki_flags &IOCB_WAITQ ) { +if (written) { +put_page (page); + goto out; + } + error =wait_on_page_locked_async (page, + iocb->ki_waitq); + }else { +if (iocb->ki_flags &IOCB_NOWAIT ) { +put_page (page); + goto would_block; + } + error =wait_on_page_locked_killable (page); + }if (unlikely (error)) goto readpage_error;if (PageUptodate (page))
@@ -2147,7 +2163,10 @@ ssize_t generic_file_buffered_read(struct kiocb *iocb, page_not_up_to_date:/* Get exclusive access to the page ... */ - error =lock_page_killable (page); +if (iocb->ki_flags &IOCB_WAITQ ) + error =lock_page_async (page, iocb->ki_waitq); +else + error =lock_page_killable (page);if (unlikely (error)) goto readpage_error;
@@ -2190,10 +2209,7 @@ ssize_t generic_file_buffered_read(struct kiocb *iocb, }if (!PageUptodate (page)) { -if (iocb->ki_flags &IOCB_WAITQ ) - error =lock_page_async (page, iocb->ki_waitq); -else - error =lock_page_killable (page); + error =lock_page_killable (page);if (unlikely (error)) goto readpage_error;if (!PageUptodate (page)) {
Signature · 1 line
2.27.0
Jens Axboe
maintainerPATCH 9/15 fs: add FMODE_BUF_RASYNC
In reply to Jens AxboeIf set, this indicates that the file system supports IOCB_WAITQ for buffered reads.
--- include/linux/fs.h | 3 +++ 1 file changed, 3 insertions(+)
include/linux/fs.h +3 -0
@@ -175,6 +175,9 @@ typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset,/* File does not contribute to nr_files count */ #define FMODE_NOACCOUNT ((__force fmode_t)0x20000000) +/* File supports async buffered reads */ +#define FMODE_BUF_RASYNC ((__force fmode_t)0x40000000) +/* * Flag for rw_copy_check_uvector and compat_rw_copy_check_uvector * that indicates that they should check the contents of the iovec are
Signature · 1 line
2.27.0
Jens Axboe
maintainerPATCH 10/15 block: flag block devices as supporting IOCB_WAITQ
In reply to Jens Axboe--- fs/block_dev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/block_dev.c +1 -1
@@ -1850,7 +1850,7 @@ static int blkdev_open(struct inode * inode, struct file * filp)*/ filp->f_flags |=O_LARGEFILE ; - filp->f_mode |=FMODE_NOWAIT ; + filp->f_mode |=FMODE_NOWAIT |FMODE_BUF_RASYNC ;if (filp->f_flags &O_NDELAY ) filp->f_mode |=FMODE_NDELAY ;
Signature · 1 line
2.27.0
Jens Axboe
maintainerPATCH 11/15 xfs: flag files as supporting buffered async reads
In reply to Jens AxboeXFS uses generic_file_read_iter(), which already supports this.
--- fs/xfs/xfs_file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/xfs/xfs_file.c +1 -1
@@ -1080,7 +1080,7 @@ xfs_file_open(return -EFBIG ;if (XFS_FORCED_SHUTDOWN (XFS_M (inode->i_sb)))return -EIO ; - file->f_mode |=FMODE_NOWAIT ; + file->f_mode |=FMODE_NOWAIT |FMODE_BUF_RASYNC ;return 0 ; }
Signature · 1 line
2.27.0
Jens Axboe
maintainerPATCH 12/15 btrfs: flag files as supporting buffered async reads
In reply to Jens Axboebtrfs uses generic_file_read_iter(), which already supports this.
--- fs/btrfs/file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/file.c +1 -1
@@ -3472,7 +3472,7 @@ static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int whence)static int btrfs_file_open (struct inode *inode,struct file *filp) { - filp->f_mode |=FMODE_NOWAIT ; + filp->f_mode |=FMODE_NOWAIT |FMODE_BUF_RASYNC ;return generic_file_open (inode, filp); }
Signature · 1 line
2.27.0
Re: [PATCH 12/15] btrfs: flag files as supporting buffered async reads
Quoted 4 lines · Jens Axboe
btrfs uses
generic_file_read_iter(), which already supports this.Acked-by Chris Mason <[email protected]>Signed-off-by Jens Axboe <[email protected]>
Can you please CC the mailinglist for btrfs patches? Thanks.
Jens Axboe
maintainerPATCH 13/15 ext4: flag as supporting buffered async reads
In reply to Jens Axboeext4 uses generic_file_read_iter(), which already supports this.
--- fs/ext4/file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ext4/file.c +1 -1
@@ -839,7 +839,7 @@ static int ext4_file_open(struct inode * inode, struct file * filp)return ret; } - filp->f_mode |=FMODE_NOWAIT ; + filp->f_mode |=FMODE_NOWAIT |FMODE_BUF_RASYNC ;return dquot_file_open (inode, filp); }
Signature · 1 line
2.27.0
Jens Axboe
maintainerPATCH 14/15 mm: add kiocb_wait_page_queue_init() helper
In reply to Jens AxboeChecks if the file supports it, and initializes the values that we need. Caller passes in 'data' pointer, if any, and the callback function to be used.
--- include/linux/pagemap.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
include/linux/pagemap.h +21 -0
@@ -533,6 +533,27 @@ static inline int wake_page_match(struct wait_page_queue *wait_page,return 1 ; } +static inline int kiocb_wait_page_queue_init (struct kiocb *kiocb, +struct wait_page_queue *wait, +wait_queue_func_t func, +void *data) +{ +/* Can't support async wakeup with polled IO */ +if (kiocb->ki_flags &IOCB_HIPRI ) +return -EINVAL ; +if (kiocb->ki_filp->f_mode &FMODE_BUF_RASYNC ) { + wait->wait.func = func; + wait->wait.private = data; + wait->wait.flags =0 ; +INIT_LIST_HEAD (&wait->wait.entry); + kiocb->ki_flags |=IOCB_WAITQ ; + kiocb->ki_waitq = wait; +return 0 ; + } + +return -EOPNOTSUPP ; +} +extern void __lock_page (struct page *page);extern int __lock_page_killable (struct page *page);extern int __lock_page_async (struct page *page,struct wait_page_queue *wait);
Signature · 1 line
2.27.0
Jens Axboe
maintainerPATCH 15/15 io_uring: support true async buffered reads, if file provides it
In reply to Jens AxboeIf the file is flagged with FMODE_BUF_RASYNC, then we don't have to punt the buffered read to an io-wq worker. Instead we can rely on page unlocking callbacks to support retry based async IO. This is a lot more efficient than doing async thread offload.
The retry is done similarly to how we handle poll based retry. From the unlock callback, we simply queue the retry to a task_work based handler.
--- fs/io_uring.c | 145 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 137 insertions(+), 8 deletions(-)
fs/io_uring.c +137 -8
@@ -78,6 +78,7 @@#include <linux/fs_struct.h> #include <linux/splice.h> #include <linux/task_work.h> +#include <linux/pagemap.h> #define CREATE_TRACE_POINTS #include <trace/events/io_uring.h>
@@ -503,6 +504,8 @@ struct io_async_rw {struct iovec *iov;ssize_t nr_segs;ssize_t size; +struct wait_page_queue wpq; +struct callback_head task_work; };struct io_async_ctx {
@@ -2750,6 +2753,126 @@ static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,return 0 ; } +static void __io_async_buf_error (struct io_kiocb *req,int error) +{ +struct io_ring_ctx *ctx = req->ctx; + +spin_lock_irq (&ctx->completion_lock); +io_cqring_fill_event (req, error); +io_commit_cqring (ctx); +spin_unlock_irq (&ctx->completion_lock); + +io_cqring_ev_posted (ctx); +req_set_fail_links (req); +io_double_put_req (req); +} + +static void io_async_buf_cancel (struct callback_head *cb) +{ +struct io_async_rw *rw; +struct io_kiocb *req; + + rw = container_of(cb, struct io_async_rw, task_work); + req = rw->wpq.wait.private; +__io_async_buf_error (req, -ECANCELED ); +} + +static void io_async_buf_retry (struct callback_head *cb) +{ +struct io_async_rw *rw; +struct io_ring_ctx *ctx; +struct io_kiocb *req; + + rw = container_of(cb, struct io_async_rw, task_work); + req = rw->wpq.wait.private; + ctx = req->ctx; + +__set_current_state (TASK_RUNNING ); +if (!io_sq_thread_acquire_mm (ctx, req)) { +mutex_lock (&ctx->uring_lock); +__io_queue_sqe (req,NULL ); +mutex_unlock (&ctx->uring_lock); + }else { +__io_async_buf_error (req, -EFAULT ); + } +} + +static int io_async_buf_func (struct wait_queue_entry *wait,unsigned mode, +int sync,void *arg) +{ +struct wait_page_queue *wpq; +struct io_kiocb *req = wait->private; +struct io_async_rw *rw = &req->io->rw; +struct wait_page_key *key = arg; +struct task_struct *tsk; +int ret; + + wpq = container_of(wait, struct wait_page_queue, wait); + + ret =wake_page_match (wpq, key); +if (ret !=1 ) +return ret; + +list_del_init (&wait->entry); + +init_task_work (&rw->task_work, io_async_buf_retry); +/* submit ref gets dropped, acquire a new one */ +refcount_inc (&req->refs); + tsk = req->task; + ret =task_work_add (tsk, &rw->task_work, true); +if (unlikely (ret)) { +/* queue just for cancelation */ +init_task_work (&rw->task_work, io_async_buf_cancel); + tsk =io_wq_get_task (req->ctx->io_wq); +task_work_add (tsk, &rw->task_work, true); + } +wake_up_process (tsk); +return 1 ; +} + +static bool io_rw_should_retry (struct io_kiocb *req) +{ +struct kiocb *kiocb = &req->rw.kiocb; +int ret; + +/* never retry for NOWAIT, we just complete with -EAGAIN */ +if (req->flags &REQ_F_NOWAIT ) +return false; + +/* already tried, or we're doing O_DIRECT */ +if (kiocb->ki_flags & (IOCB_DIRECT |IOCB_WAITQ )) +return false; +/* +* just use poll if we can, and don't attempt if the fs doesn't +* support callback based unlocks +*/ +if (file_can_poll (req->file) || !(req->file->f_mode &FMODE_BUF_RASYNC )) +return false; + +/* +* If request type doesn't require req->io to defer in general, +* we need to allocate it here +*/ +if (!req->io &&__io_alloc_async_ctx (req)) +return false; + + ret =kiocb_wait_page_queue_init (kiocb, &req->io->rw.wpq, + io_async_buf_func, req); +if (!ret) { +io_get_req_task (req); +return true; + } + +return false; +} + +static int io_iter_do_read (struct io_kiocb *req,struct iov_iter *iter) +{ +if (req->file->f_op->read_iter) +return call_read_iter (req->file, &req->rw.kiocb, iter); +return loop_rw_iter (READ , req->file, &req->rw.kiocb, iter); +} +static int io_read (struct io_kiocb *req,bool force_nonblock) {struct iovec inline_vecs[UIO_FASTIOV ], *iovec = inline_vecs;
@@ -2784,10 +2907,7 @@ static int io_read(struct io_kiocb *req, bool force_nonblock)unsigned long nr_segs = iter.nr_segs;ssize_t ret2 =0 ; -if (req->file->f_op->read_iter) - ret2 =call_read_iter (req->file, kiocb, &iter); -else - ret2 =loop_rw_iter (READ , req->file, kiocb, &iter); + ret2 =io_iter_do_read (req, &iter);/* Catch -EAGAIN return for forced non-blocking submission */ if (!force_nonblock || (ret2 != -EAGAIN && ret2 != -EIO )) {
@@ -2799,17 +2919,26 @@ static int io_read(struct io_kiocb *req, bool force_nonblock) ret =io_setup_async_rw (req, io_size, iovec, inline_vecs, &iter);if (ret) - goto out_free; + goto out;/* any defer here is final, must blocking retry */ if (!(req->flags &REQ_F_NOWAIT ) && !file_can_poll (req->file)) req->flags |=REQ_F_MUST_PUNT ; +/* if we can retry, do so with the callbacks armed */ +if (io_rw_should_retry (req)) { + ret2 =io_iter_do_read (req, &iter); +if (ret2 == -EIOCBQUEUED ) { + goto out; + }else if (ret2 != -EAGAIN ) { +kiocb_done (kiocb, ret2); + goto out; + } + } + kiocb->ki_flags &= ~IOCB_WAITQ ;return -EAGAIN ; } } -out_free: -kfree (iovec); - req->flags &= ~REQ_F_NEED_CLEANUP ; +out:return ret; }
Signature · 1 line
2.27.0
Re: [PATCH 15/15] io_uring: support true async buffered reads, if file provides it
Quoted 14 lines · Jens Axboe
If the file is flagged with
FMODE_BUF_RASYNC, then we don't have to punt the buffered read to an io-wq worker. Instead we can rely on page unlocking callbacks to support retry based async IO. This is a lot more efficient than doing async thread offload.The retry is done similarly to how we handle poll based retry. From the unlock callback, we simply queue the retry to a
task_workbased handler.Signed-off-by Jens Axboe <[email protected]>--- fs/io_uring.c | 145 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 137 insertions(+), 8 deletions(-)
...
Quoted 32 lines
static int
io_read(struct io_kiocb *req, bool force_nonblock){fs/io_uring.c +9
-5struct iovec inline_vecs[UIO_FASTIOV ], *iovec = inline_vecs;@@ -2784,10 +2907,7 @@ static int io_read(struct io_kiocb *req, bool force_nonblock)unsigned long nr_segs = iter.nr_segs;ssize_t ret2 =0 ; -if (req->file->f_op->read_iter) - ret2 =call_read_iter (req->file, kiocb, &iter); -else - ret2 =loop_rw_iter (READ , req->file, kiocb, &iter); + ret2 =io_iter_do_read (req, &iter);/* Catch -EAGAIN return for forced non-blocking submission */ if (!force_nonblock || (ret2 != -EAGAIN && ret2 != -EIO )) {@@ -2799,17 +2919,26 @@ static int io_read(struct io_kiocb *req, bool force_nonblock) ret =io_setup_async_rw (req, io_size, iovec, inline_vecs, &iter);if (ret) - goto out_free; + goto out;/* any defer here is final, must blocking retry */ if (!(req->flags &REQ_F_NOWAIT ) && !file_can_poll (req->file)) req->flags |=REQ_F_MUST_PUNT ; +/* if we can retry, do so with the callbacks armed */ +if (io_rw_should_retry (req)) { + ret2 =io_iter_do_read (req, &iter); +if (ret2 == -EIOCBQUEUED ) { + goto out; + }else if (ret2 != -EAGAIN ) { +kiocb_done (kiocb, ret2);
fs/io_uring.c static int io_read(struct io_kiocb *req, bool force_nonblock) · line 2812
Quoted 10 lines
fs/io_uring.c +4
-3+ goto out; + } + } + kiocb->ki_flags &= ~IOCB_WAITQ ;return -EAGAIN ; } } -out_free: -kfree (iovec); - req->flags &= ~REQ_F_NEED_CLEANUP ;
This looks fishy. For instance, if it fails early on rw_verify_area(), how would it free yet on-stack iovec? Is it handled somehow?
Quoted 5 lines
fs/io_uring.c +1
-0+out:return ret; }
Signature · 1 line
Pavel Begunkov
Jens Axboe
maintainerRe: [PATCH 15/15] io_uring: support true async buffered reads, if file provides it
Quoted 53 lines · Pavel Begunkov
Quoted 14 lines · Jens Axboe
If the file is flagged with
FMODE_BUF_RASYNC, then we don't have to punt the buffered read to an io-wq worker. Instead we can rely on page unlocking callbacks to support retry based async IO. This is a lot more efficient than doing async thread offload.The retry is done similarly to how we handle poll based retry. From the unlock callback, we simply queue the retry to a
task_workbased handler.Signed-off-by Jens Axboe <[email protected]>--- fs/io_uring.c | 145 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 137 insertions(+), 8 deletions(-)...
Quoted 37 lines
static int
io_read(struct io_kiocb *req, bool force_nonblock){fs/io_uring.c +13
-5struct iovec inline_vecs[UIO_FASTIOV ], *iovec = inline_vecs;@@ -2784,10 +2907,7 @@ static int io_read(struct io_kiocb *req, bool force_nonblock)unsigned long nr_segs = iter.nr_segs;ssize_t ret2 =0 ; -if (req->file->f_op->read_iter) - ret2 =call_read_iter (req->file, kiocb, &iter); -else - ret2 =loop_rw_iter (READ , req->file, kiocb, &iter); + ret2 =io_iter_do_read (req, &iter);/* Catch -EAGAIN return for forced non-blocking submission */ if (!force_nonblock || (ret2 != -EAGAIN && ret2 != -EIO )) {@@ -2799,17 +2919,26 @@ static int io_read(struct io_kiocb *req, bool force_nonblock) ret =io_setup_async_rw (req, io_size, iovec, inline_vecs, &iter);if (ret) - goto out_free; + goto out;/* any defer here is final, must blocking retry */ if (!(req->flags &REQ_F_NOWAIT ) && !file_can_poll (req->file)) req->flags |=REQ_F_MUST_PUNT ; +/* if we can retry, do so with the callbacks armed */ +if (io_rw_should_retry (req)) { + ret2 =io_iter_do_read (req, &iter); +if (ret2 == -EIOCBQUEUED ) { + goto out; + }else if (ret2 != -EAGAIN ) { +kiocb_done (kiocb, ret2); + goto out; + } + } + kiocb->ki_flags &= ~IOCB_WAITQ ;return -EAGAIN ;
Quoted 8 lines
fs/io_uring.c static int io_read(struct io_kiocb *req, bool force_nonblock) · line 2812
Quoted 5 lines
fs/io_uring.c +0
-3} } -out_free: -kfree (iovec); - req->flags &= ~REQ_F_NEED_CLEANUP ;This looks fishy. For instance, if it fails early on
rw_verify_area(), how would it free yet on-stack iovec? Is it handled somehow?
This was tweaked and rebased on top of the REQ_F_NEED_CLEANUP change, it should be correct in the tree:
https://git.kernel.dk/cgit/linux-block/tree/fs/io_uring.c?h=for-5.9/io_uring#n2908
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerv7 cover Add support for async buffered reads
In reply to Jens AxboeQuoted 3 lines · Jens Axboe
We technically support this already through
io_uring, but it's implemented with a thread backend to support cases where we would block. This isn't ideal.
Apparently I'm cursed when I send these out, corrected the subject line to indicate this is v7 of the series...
Signature · 1 line
Jens Axboe