Commit 5c8b0b54 authored by Jens Axboe's avatar Jens Axboe

io_uring: have submission side sqe errors post a cqe

Currently we only post a cqe if we get an error OUTSIDE of submission.
For submission, we return the error directly through io_uring_enter().
This is a bit awkward for applications, and it makes more sense to
always post a cqe with an error, if the error happens on behalf of an
sqe.

This changes submission behavior a bit. io_uring_enter() returns -ERROR
for an error, and > 0 for number of sqes submitted. Before this change,
if you wanted to submit 8 entries and had an error on the 5th entry,
io_uring_enter() would return 4 (for number of entries successfully
submitted) and rewind the sqring. The application would then have to
peek at the sqring and figure out what was wrong with the head sqe, and
then skip it itself. With this change, we'll return 5 since we did
consume 5 sqes, and the last sqe (with the error) will result in a cqe
being posted with the error.

This makes the logic easier to handle in the application, and it cleans
up the submission part.
Suggested-by: default avatarStefan Bühler <source@stbuehler.de>
Signed-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent 62977281
...@@ -1801,14 +1801,6 @@ static void io_commit_sqring(struct io_ring_ctx *ctx) ...@@ -1801,14 +1801,6 @@ static void io_commit_sqring(struct io_ring_ctx *ctx)
} }
} }
/*
* Undo last io_get_sqring()
*/
static void io_drop_sqring(struct io_ring_ctx *ctx)
{
ctx->cached_sq_head--;
}
/* /*
* Fetch an sqe, if one is available. Note that s->sqe will point to memory * Fetch an sqe, if one is available. Note that s->sqe will point to memory
* that is mapped by userspace. This means that care needs to be taken to * that is mapped by userspace. This means that care needs to be taken to
...@@ -2018,7 +2010,7 @@ static int io_sq_thread(void *data) ...@@ -2018,7 +2010,7 @@ static int io_sq_thread(void *data)
static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit) static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit)
{ {
struct io_submit_state state, *statep = NULL; struct io_submit_state state, *statep = NULL;
int i, ret = 0, submit = 0; int i, submit = 0;
if (to_submit > IO_PLUG_THRESHOLD) { if (to_submit > IO_PLUG_THRESHOLD) {
io_submit_state_start(&state, ctx, to_submit); io_submit_state_start(&state, ctx, to_submit);
...@@ -2027,6 +2019,7 @@ static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit) ...@@ -2027,6 +2019,7 @@ static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit)
for (i = 0; i < to_submit; i++) { for (i = 0; i < to_submit; i++) {
struct sqe_submit s; struct sqe_submit s;
int ret;
if (!io_get_sqring(ctx, &s)) if (!io_get_sqring(ctx, &s))
break; break;
...@@ -2034,21 +2027,18 @@ static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit) ...@@ -2034,21 +2027,18 @@ static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit)
s.has_user = true; s.has_user = true;
s.needs_lock = false; s.needs_lock = false;
s.needs_fixed_file = false; s.needs_fixed_file = false;
submit++;
ret = io_submit_sqe(ctx, &s, statep); ret = io_submit_sqe(ctx, &s, statep);
if (ret) { if (ret)
io_drop_sqring(ctx); io_cqring_add_event(ctx, s.sqe->user_data, ret, 0);
break;
}
submit++;
} }
io_commit_sqring(ctx); io_commit_sqring(ctx);
if (statep) if (statep)
io_submit_state_end(statep); io_submit_state_end(statep);
return submit ? submit : ret; return submit;
} }
static unsigned io_cqring_events(struct io_cq_ring *ring) static unsigned io_cqring_events(struct io_cq_ring *ring)
...@@ -2779,24 +2769,12 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, ...@@ -2779,24 +2769,12 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
mutex_lock(&ctx->uring_lock); mutex_lock(&ctx->uring_lock);
submitted = io_ring_submit(ctx, to_submit); submitted = io_ring_submit(ctx, to_submit);
mutex_unlock(&ctx->uring_lock); mutex_unlock(&ctx->uring_lock);
if (submitted < 0)
goto out_ctx;
} }
if (flags & IORING_ENTER_GETEVENTS) { if (flags & IORING_ENTER_GETEVENTS) {
unsigned nr_events = 0; unsigned nr_events = 0;
min_complete = min(min_complete, ctx->cq_entries); min_complete = min(min_complete, ctx->cq_entries);
/*
* The application could have included the 'to_submit' count
* in how many events it wanted to wait for. If we failed to
* submit the desired count, we may need to adjust the number
* of events to poll/wait for.
*/
if (submitted < to_submit)
min_complete = min_t(unsigned, submitted, min_complete);
if (ctx->flags & IORING_SETUP_IOPOLL) { if (ctx->flags & IORING_SETUP_IOPOLL) {
mutex_lock(&ctx->uring_lock); mutex_lock(&ctx->uring_lock);
ret = io_iopoll_check(ctx, &nr_events, min_complete); ret = io_iopoll_check(ctx, &nr_events, min_complete);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment