io_uring: don't wait when under-submitting
PATCH 1/1 io_uring: don't wait when under-submitting
There is no reliable way to submit and wait in a single syscall, as io_submit_sqes() may under-consume sqes (in case of an early error). Then it will wait for not-yet-submitted requests, deadlocking the user in most cases.
Don't wait/poll if can't submit all sqes, and return -EAGAIN
---
I wonder, why it doesn't return 2 error codes for submission and waiting separately? It's a bit puzzling figuring out what to return. I guess the same with the userspace side.
fs/io_uring.c | 6 ++++++ 1 file changed, 6 insertions(+)
fs/io_uring.c +6 -0
@@ -4877,6 +4877,11 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, submitted =io_submit_sqes (ctx, to_submit, f.file, fd, &cur_mm, false);mutex_unlock (&ctx->uring_lock); + +if (submitted != to_submit) { + submitted = -EAGAIN ; + goto out; + } }if (flags &IORING_ENTER_GETEVENTS ) {unsigned nr_events =0 ;
@@ -4890,6 +4895,7 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, } } +out:percpu_ref_put (&ctx->refs); out_fput:fdput (f);
Signature · 1 line
2.24.0
Jens Axboe
maintainerQuoted 4 lines · Pavel Begunkov
There is no reliable way to submit and wait in a single syscall, as
io_submit_sqes()may under-consume sqes (in case of an early error). Then it will wait for not-yet-submitted requests, deadlocking the user in most cases.
Why not just cap the wait_nr? If someone does to_submit = 8, wait_nr = 8, and we only submit 4, just wait for 4? Ala:
fs/io_uring.c +5 -1
@@ -5272,6 +5272,10 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, submitted =io_submit_sqes (ctx, to_submit, f.file, fd, &cur_mm, false);mutex_unlock (&ctx->uring_lock); +if (submitted <=0 ) + goto done; +if (submitted != to_submit && min_complete > submitted) + min_complete = submitted; }if (flags &IORING_ENTER_GETEVENTS ) {unsigned nr_events =0 ;
@@ -5284,7 +5288,7 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, ret =io_cqring_wait (ctx, min_complete, sig, sigsz); } } - +done:percpu_ref_put (&ctx->refs); out_fput:fdput (f);
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerQuoted 26 lines · Jens Axboe
Quoted 4 lines · Pavel Begunkov
There is no reliable way to submit and wait in a single syscall, as
io_submit_sqes()may under-consume sqes (in case of an early error). Then it will wait for not-yet-submitted requests, deadlocking the user in most cases.Why not just cap the
wait_nr? If someone doesto_submit= 8,wait_nr= 8, and we only submit 4, just wait for 4? Ala:fs/io_uring.c +4
-0@@ -5272,6 +5272,10 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, submitted =io_submit_sqes (ctx, to_submit, f.file, fd, &cur_mm, false);mutex_unlock (&ctx->uring_lock); +if (submitted <=0 ) + goto done; +if (submitted != to_submit && min_complete > submitted) + min_complete = submitted; }if (flags &IORING_ENTER_GETEVENTS ) {unsigned nr_events =0 ;
fs/io_uring.c SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, · line 5294
Quoted 8 lines
fs/io_uring.c +1
-1@@ -5284,7 +5288,7 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, ret =io_cqring_wait (ctx, min_complete, sig, sigsz); } } - +done:percpu_ref_put (&ctx->refs); out_fput:fdput (f);
This is probably a bit cleaner, since it only adjusts if we're going to wait.
fs/io_uring.c +5 -1
@@ -5272,11 +5272,15 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, submitted =io_submit_sqes (ctx, to_submit, f.file, fd, &cur_mm, false);mutex_unlock (&ctx->uring_lock); +if (submitted <=0 ) + goto done; }if (flags &IORING_ENTER_GETEVENTS ) {unsigned nr_events =0 ; min_complete =min (min_complete, ctx->cq_entries); +if (submitted != to_submit && min_complete > submitted) + min_complete = submitted;if (ctx->flags &IORING_SETUP_IOPOLL ) { ret =io_iopoll_check (ctx, &nr_events, min_complete);
@@ -5284,7 +5288,7 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, ret =io_cqring_wait (ctx, min_complete, sig, sigsz); } } - +done:percpu_ref_put (&ctx->refs); out_fput:fdput (f);
Signature · 1 line
Jens Axboe
Quoted 10 lines · Jens Axboe
Quoted 9 lines · Jens Axboe
Quoted 4 lines · Pavel Begunkov
There is no reliable way to submit and wait in a single syscall, as
io_submit_sqes()may under-consume sqes (in case of an early error). Then it will wait for not-yet-submitted requests, deadlocking the user in most cases.Why not just cap the
wait_nr? If someone doesto_submit= 8,wait_nr= 8, and we only submit 4, just wait for 4? Ala:
Is it worth entangling the code? I don't expect anyone trying to recover, maybe except full reset/restart. So, failing ASAP seemed to me as the right thing to do. It may also mean nothing to the user if e.g. submit(1), submit(1), ..., submit_and_wait(1, n)
Anyway, this shouldn't even happen in a not buggy code, so I'm fine with any version as long as it doesn't lock up. I'll resend if you still prefer to cap it.
Quoted 60 lines
Quoted 17 lines
fs/io_uring.c +4
-0@@ -5272,6 +5272,10 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, submitted =io_submit_sqes (ctx, to_submit, f.file, fd, &cur_mm, false);mutex_unlock (&ctx->uring_lock); +if (submitted <=0 ) + goto done; +if (submitted != to_submit && min_complete > submitted) + min_complete = submitted; }if (flags &IORING_ENTER_GETEVENTS ) {unsigned nr_events =0 ;fs/io_uring.c SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, · line 5294
Quoted 8 lines
fs/io_uring.c +1
-1@@ -5284,7 +5288,7 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, ret =io_cqring_wait (ctx, min_complete, sig, sigsz); } } - +done:percpu_ref_put (&ctx->refs); out_fput:fdput (f);This is probably a bit cleaner, since it only adjusts if we're going to wait.
fs/io_uring.c +5
-1@@ -5272,11 +5272,15 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, submitted =io_submit_sqes (ctx, to_submit, f.file, fd, &cur_mm, false);mutex_unlock (&ctx->uring_lock); +if (submitted <=0 ) + goto done; }if (flags &IORING_ENTER_GETEVENTS ) {unsigned nr_events =0 ; min_complete =min (min_complete, ctx->cq_entries); +if (submitted != to_submit && min_complete > submitted) + min_complete = submitted;if (ctx->flags &IORING_SETUP_IOPOLL ) { ret =io_iopoll_check (ctx, &nr_events, min_complete);@@ -5284,7 +5288,7 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, ret =io_cqring_wait (ctx, min_complete, sig, sigsz); } } - +done:percpu_ref_put (&ctx->refs); out_fput:fdput (f);
Signature · 1 line
Pavel Begunkov
Jens Axboe
maintainerQuoted 15 lines · Pavel Begunkov
Quoted 10 lines · Jens Axboe
Quoted 10 lines · Jens Axboe
Quoted 4 lines · Pavel Begunkov
There is no reliable way to submit and wait in a single syscall, as
io_submit_sqes()may under-consume sqes (in case of an early error). Then it will wait for not-yet-submitted requests, deadlocking the user in most cases.Why not just cap the
wait_nr? If someone doesto_submit= 8,wait_nr= 8, and we only submit 4, just wait for 4? Ala:
Quoted 8 lines
Is it worth entangling the code? I don't expect anyone trying to recover, maybe except full reset/restart. So, failing ASAP seemed to me as the right thing to do. It may also mean nothing to the user if e.g. submit(1), submit(1), ...,
submit_and_wait(1, n)Anyway, this shouldn't even happen in a not buggy code, so I'm fine with any version as long as it doesn't lock up. I'll resend if you still prefer to cap it.
I like the cap version a lot better, and in fact we did used to have that but lost it early. I like it behaviorally a lot better, too.
Can you resend? Thanks!
PATCH v2 1/1 io_uring: don't wait when under-submitting
In reply to Pavel BegunkovThere is no reliable way to submit and wait in a single syscall, as io_submit_sqes() may under-consume sqes (in case of an early error). Then it will wait for not-yet-submitted requests, deadlocking the user in most cases.
Don't wait/poll if can't submit all sqes, and return -EAGAIN
---
v2: cap min_complete if submitted partially (Jens Axboe)
fs/io_uring.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
fs/io_uring.c +10 -5
@@ -3526,11 +3526,8 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,unsigned int sqe_flags; req =io_get_req (ctx, statep); -if (unlikely (!req)) { -if (!submitted) - submitted = -EAGAIN ; +if (unlikely (!req))break ; - }if (!io_get_sqring (ctx, req)) {__io_free_req (req);break ;
@@ -4910,6 +4907,14 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, submitted =io_submit_sqes (ctx, to_submit, f.file, fd, &cur_mm, false);mutex_unlock (&ctx->uring_lock); + +if (submitted != to_submit) { +if (!submitted) { + submitted = -EAGAIN ; + goto done; + } + min_complete =min (min_complete, (u32 )submitted); + } }if (flags &IORING_ENTER_GETEVENTS ) {unsigned nr_events =0 ;
@@ -4922,7 +4927,7 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, ret =io_cqring_wait (ctx, min_complete, sig, sigsz); } } - +done:percpu_ref_put (&ctx->refs); out_fput:fdput (f);
Signature · 1 line
2.24.0
Jens Axboe
maintainerQuoted 11 lines · Pavel Begunkov
There is no reliable way to submit and wait in a single syscall, as
io_submit_sqes()may under-consume sqes (in case of an early error). Then it will wait for not-yet-submitted requests, deadlocking the user in most cases.Don't wait/poll if can't submit all sqes, and return
-EAGAINSigned-off-by Pavel Begunkov <[email protected]>---
v2: cap
min_completeif submitted partially (Jens Axboe)
Can you update the commit message as well, doesn't really reflect the current state of it... Apart from that, looks good to me!
Signature · 1 line
Jens Axboe
Quoted 8 lines · Jens Axboe
Quoted 7 lines · Pavel Begunkov
There is no reliable way to submit and wait in a single syscall, as
io_submit_sqes()may under-consume sqes (in case of an early error). Then it will wait for not-yet-submitted requests, deadlocking the user in most cases.Don't wait/poll if can't submit all sqes, and return
-EAGAIN
Quoted 8 lines
Quoted 4 lines
Signed-off-by Pavel Begunkov <[email protected]>---
v2: cap
min_completeif submitted partially (Jens Axboe)Can you update the commit message as well, doesn't really reflect the current state of it... Apart from that, looks good to me!
Right, thanks for noticing!
Signature · 1 line
Pavel Begunkov
PATCH v3 1/1 io_uring: don't wait when under-submitting
In reply to Pavel BegunkovThere is no reliable way to submit and wait in a single syscall, as io_submit_sqes() may under-consume sqes (in case of an early error). Then it will wait for not-yet-submitted requests, deadlocking the user in most cases.
In such cases adjust min_complete, so it won't wait for more than what have been submitted in the current call to io_uring_enter(). It may be less than totally in-flight including previous submissions, but this shouldn't do harm and up to a user.
---
v2: cap min_complete if submitted partially (Jens Axboe)
v3: update commit message (Jens Axboe)
fs/io_uring.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
fs/io_uring.c +10 -5
@@ -3763,11 +3763,8 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,unsigned int sqe_flags; req =io_get_req (ctx, statep); -if (unlikely (!req)) { -if (!submitted) - submitted = -EAGAIN ; +if (unlikely (!req))break ; - }if (!io_get_sqring (ctx, req)) {__io_free_req (req);break ;
@@ -5272,6 +5269,14 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, submitted =io_submit_sqes (ctx, to_submit, f.file, fd, &cur_mm, false);mutex_unlock (&ctx->uring_lock); + +if (submitted != to_submit) { +if (!submitted) { + submitted = -EAGAIN ; + goto done; + } + min_complete =min (min_complete, (u32 )submitted); + } }if (flags &IORING_ENTER_GETEVENTS ) {unsigned nr_events =0 ;
@@ -5284,7 +5289,7 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, ret =io_cqring_wait (ctx, min_complete, sig, sigsz); } } - +done:percpu_ref_put (&ctx->refs); out_fput:fdput (f);
Signature · 1 line
2.24.0
Quoted 9 lines · Pavel Begunkov
There is no reliable way to submit and wait in a single syscall, as
io_submit_sqes()may under-consume sqes (in case of an early error). Then it will wait for not-yet-submitted requests, deadlocking the user in most cases.In such cases adjust
min_complete, so it won't wait for more than what have been submitted in the current call toio_uring_enter(). It may be less than totally in-flight including previous submissions, but this shouldn't do harm and up to a user.
Thanks, applied.
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerQuoted 6 lines · Jens Axboe
Quoted 5 lines · Pavel Begunkov
There is no reliable way to submit and wait in a single syscall, as
io_submit_sqes()may under-consume sqes (in case of an early error). Then it will wait for not-yet-submitted requests, deadlocking the user in most cases.
Quoted 6 lines
Quoted 4 lines
In such cases adjust
min_complete, so it won't wait for more than what have been submitted in the current call toio_uring_enter(). It may be less than totally in-flight including previous submissions, but this shouldn't do harm and up to a user.Thanks, applied.
This causes a behavioral change where if you ask to submit 1 but there's nothing in the SQ ring, then you would get 0 before. Now you get -EAGAIN. This doesn't make a lot of sense, since there's no point in retrying as that won't change anything.
Can we please just do something like the one I sent, instead of trying to over-complicate it?
Signature · 1 line
Jens Axboe
Quoted 14 lines · Jens Axboe
Quoted 6 lines · Jens Axboe
Quoted 5 lines · Pavel Begunkov
There is no reliable way to submit and wait in a single syscall, as
io_submit_sqes()may under-consume sqes (in case of an early error). Then it will wait for not-yet-submitted requests, deadlocking the user in most cases.Quoted 6 lines
Quoted 4 lines
In such cases adjust
min_complete, so it won't wait for more than what have been submitted in the current call toio_uring_enter(). It may be less than totally in-flight including previous submissions, but this shouldn't do harm and up to a user.Thanks, applied.
Quoted 8 lines
This causes a behavioral change where if you ask to submit 1 but there's nothing in the SQ ring, then you would get 0 before. Now you get
-EAGAIN. This doesn't make a lot of sense, since there's no point in retrying as that won't change anything.Can we please just do something like the one I sent, instead of trying to over-complicate it?
Ok, when I get to a compiler.
Signature · 1 line
Pavel Begunkov
Jens Axboe
maintainerQuoted 20 lines · Pavel Begunkov
Quoted 19 lines · Jens Axboe
Quoted 6 lines · Jens Axboe
Quoted 5 lines · Pavel Begunkov
There is no reliable way to submit and wait in a single syscall, as
io_submit_sqes()may under-consume sqes (in case of an early error). Then it will wait for not-yet-submitted requests, deadlocking the user in most cases.Quoted 6 lines
Quoted 4 lines
In such cases adjust
min_complete, so it won't wait for more than what have been submitted in the current call toio_uring_enter(). It may be less than totally in-flight including previous submissions, but this shouldn't do harm and up to a user.Thanks, applied.
This causes a behavioral change where if you ask to submit 1 but there's nothing in the SQ ring, then you would get 0 before. Now you get
-EAGAIN. This doesn't make a lot of sense, since there's no point in retrying as that won't change anything.
Quoted 5 lines
Quoted 3 lines
Can we please just do something like the one I sent, instead of trying to over-complicate it?
Ok, when I get to a compiler.
Great, thanks. BTW, I noticed when a regression test failed.
Signature · 1 line
Jens Axboe
PATCH v4 1/1 io_uring: don't wait when under-submitting
There is no reliable way to submit and wait in a single syscall, as io_submit_sqes() may under-consume sqes (in case of an early error). Then it will wait for not-yet-submitted requests, deadlocking the user in most cases.
In such cases adjust min_complete, so it won't wait for more than what have been submitted in the current io_uring_enter() call. It may be less than total in-flight, but that up to a user to handle.
---
v2: cap min_complete if submitted partially (Jens Axboe)
v3: update commit message (Jens Axboe)
v4: fix behavioural change when submitting more than have (Jens Axboe)
fs/io_uring.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
fs/io_uring.c +5 -1
@@ -4463,11 +4463,15 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, submitted =io_submit_sqes (ctx, to_submit, f.file, fd, &cur_mm, false);mutex_unlock (&ctx->uring_lock); +if (submitted <=0 ) + goto done; }if (flags &IORING_ENTER_GETEVENTS ) {unsigned nr_events =0 ; min_complete =min (min_complete, ctx->cq_entries); +if (submitted != to_submit) + min_complete =min (min_complete, (u32 )submitted);if (ctx->flags &IORING_SETUP_IOPOLL ) { ret =io_iopoll_check (ctx, &nr_events, min_complete);
@@ -4475,7 +4479,7 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, ret =io_cqring_wait (ctx, min_complete, sig, sigsz); } } - +done:percpu_ref_put (&ctx->refs); out_fput:fdput (f);
Signature · 1 line
2.24.0
Quoted 21 lines · Jens Axboe
Quoted 20 lines · Pavel Begunkov
Quoted 19 lines · Jens Axboe
Quoted 6 lines · Jens Axboe
Quoted 5 lines · Pavel Begunkov
There is no reliable way to submit and wait in a single syscall, as
io_submit_sqes()may under-consume sqes (in case of an early error). Then it will wait for not-yet-submitted requests, deadlocking the user in most cases.Quoted 6 lines
Quoted 4 lines
In such cases adjust
min_complete, so it won't wait for more than what have been submitted in the current call toio_uring_enter(). It may be less than totally in-flight including previous submissions, but this shouldn't do harm and up to a user.Thanks, applied.
This causes a behavioral change where if you ask to submit 1 but there's nothing in the SQ ring, then you would get 0 before. Now you get
-EAGAIN. This doesn't make a lot of sense, since there's no point in retrying as that won't change anything.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Can we please just do something like the one I sent, instead of trying to over-complicate it?
Ok, when I get to a compiler.
Great, thanks. BTW, I noticed when a regression test failed.
Yeah, I properly tested only the first one. Clearly, not as easy as I thought, and there were more to consider.
I sent the next version, but that's odd basically taking your code. Probably, it would have been easier for you to just commit it yourself.
Signature · 1 line
Pavel Begunkov
Jens Axboe
maintainerQuoted 28 lines · Pavel Begunkov
Quoted 27 lines · Jens Axboe
Quoted 25 lines · Pavel Begunkov
Quoted 14 lines · Jens Axboe
Quoted 6 lines · Jens Axboe
Quoted 5 lines · Pavel Begunkov
There is no reliable way to submit and wait in a single syscall, as
io_submit_sqes()may under-consume sqes (in case of an early error). Then it will wait for not-yet-submitted requests, deadlocking the user in most cases.Quoted 6 lines
Quoted 4 lines
In such cases adjust
min_complete, so it won't wait for more than what have been submitted in the current call toio_uring_enter(). It may be less than totally in-flight including previous submissions, but this shouldn't do harm and up to a user.Thanks, applied.
Quoted 8 lines
This causes a behavioral change where if you ask to submit 1 but there's nothing in the SQ ring, then you would get 0 before. Now you get
-EAGAIN. This doesn't make a lot of sense, since there's no point in retrying as that won't change anything.Can we please just do something like the one I sent, instead of trying to over-complicate it?
Ok, when I get to a compiler.
Quoted 8 lines
Quoted 2 lines
Great, thanks. BTW, I noticed when a regression test failed.
Yeah, I properly tested only the first one. Clearly, not as easy as I thought, and there were more to consider.
I sent the next version, but that's odd basically taking your code. Probably, it would have been easier for you to just commit it yourself.
Nah, I'll keep you attribution, the hard part is finding/spotting the issue, not the actual fix. I've applied v4, thanks Pavel!
Signature · 1 line
Jens Axboe