* [PATCH v2] io_uring: introduce add/post_event_and_complete function
@ 2019-11-25 9:04 Bob Liu
2019-11-25 11:22 ` Pavel Begunkov
0 siblings, 1 reply; 2+ messages in thread
From: Bob Liu @ 2019-11-25 9:04 UTC (permalink / raw)
To: axboe; +Cc: io-uring, Bob Liu
* Only complie-tested right now. *
There are many duplicated code doing add/post event, set REQ_F_FAIL_LINK and
then put req. Put them into common funcs io_cqring_event_posted_and_complete()
and io_cqring_add_event_and_complete().
Signed-off-by: Bob Liu <[email protected]>
---
Changes since v1:
- Using FAIL_LINK_* enums.
---
fs/io_uring.c | 131 +++++++++++++++++++++++++++++-----------------------------
1 file changed, 65 insertions(+), 66 deletions(-)
diff --git a/fs/io_uring.c b/fs/io_uring.c
index 9f9c2d4..d91864e 100644
--- a/fs/io_uring.c
+++ b/fs/io_uring.c
@@ -363,6 +363,12 @@ struct io_kiocb {
struct io_wq_work work;
};
+enum set_f_fail_link {
+ FAIL_LINK_NONE,
+ FAIL_LINK_ON_NEGATIVE,
+ FAIL_LINK_ON_NOTEQUAL_RES,
+ FAIL_LINK_ALWAYS,
+};
#define IO_PLUG_THRESHOLD 2
#define IO_IOPOLL_BATCH 8
@@ -1253,34 +1259,66 @@ static void kiocb_end_write(struct io_kiocb *req)
file_end_write(req->file);
}
-static void io_complete_rw_common(struct kiocb *kiocb, long res)
+void set_f_fail_link(struct io_kiocb *req, long ret, unsigned int fail_link)
+{
+ if (fail_link == FAIL_LINK_ALWAYS) {
+ if (req->flags & REQ_F_LINK)
+ req->flags |= REQ_F_FAIL_LINK;
+ } else if (fail_link == FAIL_LINK_ON_NEGATIVE) {
+ if (ret < 0 && (req->flags & REQ_F_LINK))
+ req->flags |= REQ_F_FAIL_LINK;
+ } else if (fail_link == FAIL_LINK_ON_NOTEQUAL_RES) {
+ if ((ret != req->result) && (req->flags & REQ_F_LINK))
+ req->flags |= REQ_F_FAIL_LINK;
+ }
+}
+
+static void io_cqring_add_event_and_complete(struct io_kiocb *req, long ret,
+ unsigned int fail_link, struct io_kiocb **nxt)
+{
+ set_f_fail_link(req, ret, fail_link);
+ io_cqring_add_event(req, ret);
+
+ if (nxt)
+ io_put_req_find_next(req, nxt);
+ else
+ io_put_req(req);
+}
+
+static void io_cqring_ev_posted_and_complete(struct io_kiocb *req, long ret,
+ unsigned int fail_link, struct io_kiocb **nxt)
+{
+ io_cqring_ev_posted(req->ctx);
+ set_f_fail_link(req, ret, fail_link);
+
+ if (nxt)
+ io_put_req_find_next(req, nxt);
+ else
+ io_put_req(req);
+}
+
+static void io_complete_rw_common(struct kiocb *kiocb, long res,
+ struct io_kiocb **nxt)
{
struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
if (kiocb->ki_flags & IOCB_WRITE)
kiocb_end_write(req);
- if ((req->flags & REQ_F_LINK) && res != req->result)
- req->flags |= REQ_F_FAIL_LINK;
- io_cqring_add_event(req, res);
+ io_cqring_add_event_and_complete(req, res,
+ FAIL_LINK_ON_NOTEQUAL_RES, nxt);
}
static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
{
- struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
-
- io_complete_rw_common(kiocb, res);
- io_put_req(req);
+ io_complete_rw_common(kiocb, res, NULL);
}
static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
{
- struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
struct io_kiocb *nxt = NULL;
- io_complete_rw_common(kiocb, res);
- io_put_req_find_next(req, &nxt);
-
+ io_complete_rw_common(kiocb, res, &nxt);
return nxt;
}
@@ -1831,10 +1869,7 @@ static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
end > 0 ? end : LLONG_MAX,
fsync_flags & IORING_FSYNC_DATASYNC);
- if (ret < 0 && (req->flags & REQ_F_LINK))
- req->flags |= REQ_F_FAIL_LINK;
- io_cqring_add_event(req, ret);
- io_put_req_find_next(req, nxt);
+ io_cqring_add_event_and_complete(req, ret, FAIL_LINK_ON_NEGATIVE, nxt);
return 0;
}
@@ -1878,10 +1913,7 @@ static int io_sync_file_range(struct io_kiocb *req,
ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
- if (ret < 0 && (req->flags & REQ_F_LINK))
- req->flags |= REQ_F_FAIL_LINK;
- io_cqring_add_event(req, ret);
- io_put_req_find_next(req, nxt);
+ io_cqring_add_event_and_complete(req, ret, FAIL_LINK_ON_NEGATIVE, nxt);
return 0;
}
@@ -1916,10 +1948,7 @@ static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
return ret;
}
- io_cqring_add_event(req, ret);
- if (ret < 0 && (req->flags & REQ_F_LINK))
- req->flags |= REQ_F_FAIL_LINK;
- io_put_req_find_next(req, nxt);
+ io_cqring_add_event_and_complete(req, ret, FAIL_LINK_ON_NEGATIVE, nxt);
return 0;
}
#endif
@@ -1972,10 +2001,7 @@ static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
}
if (ret == -ERESTARTSYS)
ret = -EINTR;
- if (ret < 0 && (req->flags & REQ_F_LINK))
- req->flags |= REQ_F_FAIL_LINK;
- io_cqring_add_event(req, ret);
- io_put_req_find_next(req, nxt);
+ io_cqring_add_event_and_complete(req, ret, FAIL_LINK_ON_NEGATIVE, nxt);
return 0;
#else
return -EOPNOTSUPP;
@@ -2005,10 +2031,8 @@ static int io_connect(struct io_kiocb *req, const struct io_uring_sqe *sqe,
return -EAGAIN;
if (ret == -ERESTARTSYS)
ret = -EINTR;
- if (ret < 0 && (req->flags & REQ_F_LINK))
- req->flags |= REQ_F_FAIL_LINK;
- io_cqring_add_event(req, ret);
- io_put_req_find_next(req, nxt);
+
+ io_cqring_add_event_and_complete(req, ret, FAIL_LINK_ON_NEGATIVE, nxt);
return 0;
#else
return -EOPNOTSUPP;
@@ -2091,10 +2115,7 @@ static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
ret = io_poll_cancel(ctx, READ_ONCE(sqe->addr));
spin_unlock_irq(&ctx->completion_lock);
- io_cqring_add_event(req, ret);
- if (ret < 0 && (req->flags & REQ_F_LINK))
- req->flags |= REQ_F_FAIL_LINK;
- io_put_req(req);
+ io_cqring_add_event_and_complete(req, ret, FAIL_LINK_ON_NEGATIVE, NULL);
return 0;
}
@@ -2148,11 +2169,7 @@ static void io_poll_complete_work(struct io_wq_work **workptr)
io_poll_complete(req, mask, ret);
spin_unlock_irq(&ctx->completion_lock);
- io_cqring_ev_posted(ctx);
-
- if (ret < 0 && req->flags & REQ_F_LINK)
- req->flags |= REQ_F_FAIL_LINK;
- io_put_req_find_next(req, &nxt);
+ io_cqring_ev_posted_and_complete(req, ret, FAIL_LINK_ON_NEGATIVE, &nxt);
if (nxt)
*workptr = &nxt->work;
}
@@ -2338,10 +2355,7 @@ static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
io_commit_cqring(ctx);
spin_unlock_irqrestore(&ctx->completion_lock, flags);
- io_cqring_ev_posted(ctx);
- if (req->flags & REQ_F_LINK)
- req->flags |= REQ_F_FAIL_LINK;
- io_put_req(req);
+ io_cqring_ev_posted_and_complete(req, 0, FAIL_LINK_ALWAYS, NULL);
return HRTIMER_NORESTART;
}
@@ -2396,10 +2410,7 @@ static int io_timeout_remove(struct io_kiocb *req,
io_cqring_fill_event(req, ret);
io_commit_cqring(ctx);
spin_unlock_irq(&ctx->completion_lock);
- io_cqring_ev_posted(ctx);
- if (ret < 0 && req->flags & REQ_F_LINK)
- req->flags |= REQ_F_FAIL_LINK;
- io_put_req(req);
+ io_cqring_ev_posted_and_complete(req, ret, FAIL_LINK_ON_NEGATIVE, NULL);
return 0;
}
@@ -2560,11 +2571,7 @@ static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
io_cqring_fill_event(req, ret);
io_commit_cqring(ctx);
spin_unlock_irqrestore(&ctx->completion_lock, flags);
- io_cqring_ev_posted(ctx);
-
- if (ret < 0 && (req->flags & REQ_F_LINK))
- req->flags |= REQ_F_FAIL_LINK;
- io_put_req_find_next(req, nxt);
+ io_cqring_ev_posted_and_complete(req, ret, FAIL_LINK_ON_NEGATIVE, nxt);
}
static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
@@ -2738,12 +2745,8 @@ static void io_wq_submit_work(struct io_wq_work **workptr)
/* drop submission reference */
io_put_req(req);
- if (ret) {
- if (req->flags & REQ_F_LINK)
- req->flags |= REQ_F_FAIL_LINK;
- io_cqring_add_event(req, ret);
- io_put_req(req);
- }
+ if (ret)
+ io_cqring_add_event_and_complete(req, ret, FAIL_LINK_ALWAYS, NULL);
/* if a dependent link is ready, pass it back */
if (!ret && nxt) {
@@ -2981,12 +2984,8 @@ static void __io_queue_sqe(struct io_kiocb *req)
}
/* and drop final reference, if we failed */
- if (ret) {
- io_cqring_add_event(req, ret);
- if (req->flags & REQ_F_LINK)
- req->flags |= REQ_F_FAIL_LINK;
- io_put_req(req);
- }
+ if (ret)
+ io_cqring_add_event_and_complete(req, ret, FAIL_LINK_ALWAYS, NULL);
}
static void io_queue_sqe(struct io_kiocb *req)
--
2.9.5
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v2] io_uring: introduce add/post_event_and_complete function
2019-11-25 9:04 [PATCH v2] io_uring: introduce add/post_event_and_complete function Bob Liu
@ 2019-11-25 11:22 ` Pavel Begunkov
0 siblings, 0 replies; 2+ messages in thread
From: Pavel Begunkov @ 2019-11-25 11:22 UTC (permalink / raw)
To: Bob Liu, axboe; +Cc: io-uring
On 11/25/2019 12:04 PM, Bob Liu wrote:
> * Only complie-tested right now. *
> There are many duplicated code doing add/post event, set REQ_F_FAIL_LINK and
> then put req. Put them into common funcs io_cqring_event_posted_and_complete()
> and io_cqring_add_event_and_complete().
There are some comments below. This one looks much better, but I think,
it's still not self-expressing enough. And it looks like
io_cqring_add_event_and_complete() is doing 2 orthogonal things, mostly
because we anyway need to do 2+ puts() for a single request.
How about to try extract something like io_fail_event(req, err_code)
first, which fails it unconditionally? As you noticed, in the most cases
setting REQ_F_FAIL_LINK is followed by io_cqring_add_event().
It may also be interesting to set REQ_F_FAIL_LINK for all request types,
not only for linked (i.e. REQ_F_FAIL). This may (or may not...) prove to
be clearer.
>
> Signed-off-by: Bob Liu <[email protected]>
> ---
> Changes since v1:
> - Using FAIL_LINK_* enums.
>
> ---
> fs/io_uring.c | 131 +++++++++++++++++++++++++++++-----------------------------
> 1 file changed, 65 insertions(+), 66 deletions(-)
>
> diff --git a/fs/io_uring.c b/fs/io_uring.c
> index 9f9c2d4..d91864e 100644
> --- a/fs/io_uring.c
> +++ b/fs/io_uring.c
> @@ -363,6 +363,12 @@ struct io_kiocb {
> struct io_wq_work work;
> };
>
> +enum set_f_fail_link {
> + FAIL_LINK_NONE,
> + FAIL_LINK_ON_NEGATIVE,
> + FAIL_LINK_ON_NOTEQUAL_RES,
> + FAIL_LINK_ALWAYS,
> +};
> #define IO_PLUG_THRESHOLD 2
> #define IO_IOPOLL_BATCH 8
>
> @@ -1253,34 +1259,66 @@ static void kiocb_end_write(struct io_kiocb *req)
> file_end_write(req->file);
> }
>
> -static void io_complete_rw_common(struct kiocb *kiocb, long res)
> +void set_f_fail_link(struct io_kiocb *req, long ret, unsigned int fail_link)
> +{
if (!(req->flags & REQ_F_LINK))
return;
Less bulky and is good for fast-path. And this single check
_should_ be inlined. Could you check assembly for that?
> + if (fail_link == FAIL_LINK_ALWAYS) {
> + if (req->flags & REQ_F_LINK)
> + req->flags |= REQ_F_FAIL_LINK;
> + } else if (fail_link == FAIL_LINK_ON_NEGATIVE) {
> + if (ret < 0 && (req->flags & REQ_F_LINK))
> + req->flags |= REQ_F_FAIL_LINK;
> + } else if (fail_link == FAIL_LINK_ON_NOTEQUAL_RES) {
> + if ((ret != req->result) && (req->flags & REQ_F_LINK))
> + req->flags |= REQ_F_FAIL_LINK;
> + }
> +}
> +
> +static void io_cqring_add_event_and_complete(struct io_kiocb *req, long ret,
> + unsigned int fail_link, struct io_kiocb **nxt)
io_cqring_add_event_and_put() maybe?
That's what it really do.
> +{
> + set_f_fail_link(req, ret, fail_link);
> + io_cqring_add_event(req, ret);
> +
> + if (nxt)
> + io_put_req_find_next(req, nxt);
Just removed this pattern with possible nxt==NULL.
It always tends getting nested multiplying such checks.
> + else
> + io_put_req(req);
> +}
> +
> +static void io_cqring_ev_posted_and_complete(struct io_kiocb *req, long ret,
> + unsigned int fail_link, struct io_kiocb **nxt)
> +{
> + io_cqring_ev_posted(req->ctx);
> + set_f_fail_link(req, ret, fail_link);
> +
> + if (nxt)
> + io_put_req_find_next(req, nxt);
> + else
> + io_put_req(req);
> +}
> +
> +static void io_complete_rw_common(struct kiocb *kiocb, long res,
> + struct io_kiocb **nxt)
> {
> struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
>
> if (kiocb->ki_flags & IOCB_WRITE)
> kiocb_end_write(req);
>
> - if ((req->flags & REQ_F_LINK) && res != req->result)
> - req->flags |= REQ_F_FAIL_LINK;
> - io_cqring_add_event(req, res);
> + io_cqring_add_event_and_complete(req, res,
> + FAIL_LINK_ON_NOTEQUAL_RES, nxt);
> }
>
> static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
> {
> - struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
> -
> - io_complete_rw_common(kiocb, res);
> - io_put_req(req);
> + io_complete_rw_common(kiocb, res, NULL);
> }
>
> static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
> {
> - struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
> struct io_kiocb *nxt = NULL;
>
> - io_complete_rw_common(kiocb, res);
> - io_put_req_find_next(req, &nxt);
> -
> + io_complete_rw_common(kiocb, res, &nxt);
> return nxt;
> }
>
> @@ -1831,10 +1869,7 @@ static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
> end > 0 ? end : LLONG_MAX,
> fsync_flags & IORING_FSYNC_DATASYNC);
>
> - if (ret < 0 && (req->flags & REQ_F_LINK))
> - req->flags |= REQ_F_FAIL_LINK;
> - io_cqring_add_event(req, ret);
> - io_put_req_find_next(req, nxt);
> + io_cqring_add_event_and_complete(req, ret, FAIL_LINK_ON_NEGATIVE, nxt);
> return 0;
> }
>
> @@ -1878,10 +1913,7 @@ static int io_sync_file_range(struct io_kiocb *req,
>
> ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
>
> - if (ret < 0 && (req->flags & REQ_F_LINK))
> - req->flags |= REQ_F_FAIL_LINK;
> - io_cqring_add_event(req, ret);
> - io_put_req_find_next(req, nxt);
> + io_cqring_add_event_and_complete(req, ret, FAIL_LINK_ON_NEGATIVE, nxt);
> return 0;
> }
>
> @@ -1916,10 +1948,7 @@ static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
> return ret;
> }
>
> - io_cqring_add_event(req, ret);
> - if (ret < 0 && (req->flags & REQ_F_LINK))
> - req->flags |= REQ_F_FAIL_LINK;
> - io_put_req_find_next(req, nxt);
> + io_cqring_add_event_and_complete(req, ret, FAIL_LINK_ON_NEGATIVE, nxt);
> return 0;
> }
> #endif
> @@ -1972,10 +2001,7 @@ static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
> }
> if (ret == -ERESTARTSYS)
> ret = -EINTR;
> - if (ret < 0 && (req->flags & REQ_F_LINK))
> - req->flags |= REQ_F_FAIL_LINK;
> - io_cqring_add_event(req, ret);
> - io_put_req_find_next(req, nxt);
> + io_cqring_add_event_and_complete(req, ret, FAIL_LINK_ON_NEGATIVE, nxt);
> return 0;
> #else
> return -EOPNOTSUPP;
> @@ -2005,10 +2031,8 @@ static int io_connect(struct io_kiocb *req, const struct io_uring_sqe *sqe,
> return -EAGAIN;
> if (ret == -ERESTARTSYS)
> ret = -EINTR;
> - if (ret < 0 && (req->flags & REQ_F_LINK))
> - req->flags |= REQ_F_FAIL_LINK;
> - io_cqring_add_event(req, ret);
> - io_put_req_find_next(req, nxt);
> +
> + io_cqring_add_event_and_complete(req, ret, FAIL_LINK_ON_NEGATIVE, nxt);
> return 0;
> #else
> return -EOPNOTSUPP;
> @@ -2091,10 +2115,7 @@ static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
> ret = io_poll_cancel(ctx, READ_ONCE(sqe->addr));
> spin_unlock_irq(&ctx->completion_lock);
>
> - io_cqring_add_event(req, ret);
> - if (ret < 0 && (req->flags & REQ_F_LINK))
> - req->flags |= REQ_F_FAIL_LINK;
> - io_put_req(req);
> + io_cqring_add_event_and_complete(req, ret, FAIL_LINK_ON_NEGATIVE, NULL);
> return 0;
> }
>
> @@ -2148,11 +2169,7 @@ static void io_poll_complete_work(struct io_wq_work **workptr)
> io_poll_complete(req, mask, ret);
> spin_unlock_irq(&ctx->completion_lock);
>
> - io_cqring_ev_posted(ctx);
> -
> - if (ret < 0 && req->flags & REQ_F_LINK)
> - req->flags |= REQ_F_FAIL_LINK;
> - io_put_req_find_next(req, &nxt);
> + io_cqring_ev_posted_and_complete(req, ret, FAIL_LINK_ON_NEGATIVE, &nxt);
> if (nxt)
> *workptr = &nxt->work;
> }
> @@ -2338,10 +2355,7 @@ static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
> io_commit_cqring(ctx);
> spin_unlock_irqrestore(&ctx->completion_lock, flags);
>
> - io_cqring_ev_posted(ctx);
> - if (req->flags & REQ_F_LINK)
> - req->flags |= REQ_F_FAIL_LINK;
> - io_put_req(req);
> + io_cqring_ev_posted_and_complete(req, 0, FAIL_LINK_ALWAYS, NULL);
> return HRTIMER_NORESTART;
> }
>
> @@ -2396,10 +2410,7 @@ static int io_timeout_remove(struct io_kiocb *req,
> io_cqring_fill_event(req, ret);
> io_commit_cqring(ctx);
> spin_unlock_irq(&ctx->completion_lock);
> - io_cqring_ev_posted(ctx);
> - if (ret < 0 && req->flags & REQ_F_LINK)
> - req->flags |= REQ_F_FAIL_LINK;
> - io_put_req(req);
> + io_cqring_ev_posted_and_complete(req, ret, FAIL_LINK_ON_NEGATIVE, NULL);
> return 0;
> }
>
> @@ -2560,11 +2571,7 @@ static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
> io_cqring_fill_event(req, ret);
> io_commit_cqring(ctx);
> spin_unlock_irqrestore(&ctx->completion_lock, flags);
> - io_cqring_ev_posted(ctx);
> -
> - if (ret < 0 && (req->flags & REQ_F_LINK))
> - req->flags |= REQ_F_FAIL_LINK;
> - io_put_req_find_next(req, nxt);
> + io_cqring_ev_posted_and_complete(req, ret, FAIL_LINK_ON_NEGATIVE, nxt);
> }
>
> static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
> @@ -2738,12 +2745,8 @@ static void io_wq_submit_work(struct io_wq_work **workptr)
> /* drop submission reference */
> io_put_req(req);
>
> - if (ret) {
> - if (req->flags & REQ_F_LINK)
> - req->flags |= REQ_F_FAIL_LINK;
> - io_cqring_add_event(req, ret);
> - io_put_req(req);
> - }
> + if (ret)
> + io_cqring_add_event_and_complete(req, ret, FAIL_LINK_ALWAYS, NULL);
>
> /* if a dependent link is ready, pass it back */
> if (!ret && nxt) {
> @@ -2981,12 +2984,8 @@ static void __io_queue_sqe(struct io_kiocb *req)
> }
>
> /* and drop final reference, if we failed */
> - if (ret) {
> - io_cqring_add_event(req, ret);
> - if (req->flags & REQ_F_LINK)
> - req->flags |= REQ_F_FAIL_LINK;
> - io_put_req(req);
> - }
> + if (ret)
> + io_cqring_add_event_and_complete(req, ret, FAIL_LINK_ALWAYS, NULL);
> }
>
> static void io_queue_sqe(struct io_kiocb *req)
>
--
Pavel Begunkov
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2019-11-25 11:23 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-11-25 9:04 [PATCH v2] io_uring: introduce add/post_event_and_complete function Bob Liu
2019-11-25 11:22 ` Pavel Begunkov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox