public inbox for [email protected]
 help / color / mirror / Atom feed
From: Bob Liu <[email protected]>
To: [email protected]
Cc: [email protected], Bob Liu <[email protected]>
Subject: [PATCH v2] io_uring: introduce add/post_event_and_complete function
Date: Mon, 25 Nov 2019 17:04:12 +0800	[thread overview]
Message-ID: <[email protected]> (raw)

* 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


             reply	other threads:[~2019-11-25  9:04 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-25  9:04 Bob Liu [this message]
2019-11-25 11:22 ` [PATCH v2] io_uring: introduce add/post_event_and_complete function Pavel Begunkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox