Skip to contentSkip to conversation

io_uring: introduce add/post_event_and_complete function

PATCH In review Bob Liu 2 participants 2 messages Nov 25, 2019

Subsystems (2)
lore
Share & tools
lore

Bob Liu

author
#0 lore raw

PATCH v2 1/1 io_uring: introduce add/post_event_and_complete function

* 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().

---
Changes since v1:
- Using FAIL_LINK_* enums.

---
 fs/io_uring.c | 131 +++++++++++++++++++++++++++++-----------------------------
 1 file changed, 65 insertions(+), 66 deletions(-)
fs/io_uring.c +65 -66
index 9f9c2d4..d91864e 100644
@@ -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)
Signature · 1 line
2.9.5
#1 lore raw
Quoted 4 lines · 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().

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.

Quoted 28 lines

---
Changes since v1:
- Using FAIL_LINK_* enums.

---
 fs/io_uring.c | 131 +++++++++++++++++++++++++++++-----------------------------
 1 file changed, 65 insertions(+), 66 deletions(-)
fs/io_uring.c +6 -0
index 9f9c2d4..d91864e 100644
@@ -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
 

fs/io_uring.c static void kiocb_end_write(struct io_kiocb *req) · line 1263

Quoted 6 lines
fs/io_uring.c +2 -1
@@ -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?

Quoted 4 lines
fs/io_uring.c +4 -0
+	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) {

fs/io_uring.c static void kiocb_end_write(struct io_kiocb *req) · line 1277

Quoted 10 lines
fs/io_uring.c +10 -0
+		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.

fs/io_uring.c static void kiocb_end_write(struct io_kiocb *req) · line 1283

Quoted 6 lines
fs/io_uring.c +6 -0
+{
+	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.

Quoted 206 lines
fs/io_uring.c +37 -65
+	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)
Signature · 1 line
Pavel Begunkov